1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2013 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
27 #include <sys/socket.h>
42 #ifdef HAVE_SYS_WAIT_H
51 #include "perl-plugin.h"
66 #include "filebrowser.h"
72 #define COPYRIGHT "Copyright (C) 2002-2013 " PACKAGE_BUGREPORT
73 #define LINE_GRAPHIC(c) ((!config.linegraphics) ? ' ' : c)
74 #define ROWTOMATRIX(r) ((8 - r) * 2 + 2 - 1)
75 #define COLTOMATRIX(c) ((c == 1) ? 1 : c * 4 - 3)
76 #define STATUS_HEIGHT 12
77 #define MEGA_BOARD (LINES >= 50 && COLS >= 144)
78 #define BOARD_HEIGHT_MB 50
79 #define BOARD_WIDTH_MB 98
80 #define STATUS_WIDTH_MB (COLS - BOARD_WIDTH_MB)
81 #define TAG_HEIGHT_MB 31
82 #define TAG_WIDTH_MB (COLS - BOARD_WIDTH_MB)
83 #define HISTORY_HEIGHT_MB (LINES - (STATUS_HEIGHT + TAG_HEIGHT_MB + 1))
84 #define HISTORY_WIDTH_MB (COLS - BOARD_WIDTH_MB)
85 #define BIG_BOARD (LINES >= 40 && COLS >= 112)
86 #define BOARD_HEIGHT ((MEGA_BOARD) ? BOARD_HEIGHT_MB : (BIG_BOARD) ? 34 : 18)
87 #define BOARD_WIDTH ((MEGA_BOARD) ? BOARD_WIDTH_MB : (BIG_BOARD) ? 66 : 34)
88 #define STATUS_WIDTH ((MEGA_BOARD) ? STATUS_WIDTH_MB : COLS - BOARD_WIDTH)
89 #define TAG_HEIGHT ((MEGA_BOARD) ? TAG_HEIGHT_MB : LINES - STATUS_HEIGHT - 1)
90 #define TAG_WIDTH ((MEGA_BOARD) ? TAG_WIDTH_MB : COLS - BOARD_WIDTH)
91 #define HISTORY_HEIGHT ((MEGA_BOARD) ? HISTORY_HEIGHT_MB : LINES - BOARD_HEIGHT)
92 #define HISTORY_WIDTH ((MEGA_BOARD) ? HISTORY_WIDTH_MB : COLS - STATUS_WIDTH)
93 #define MAX_VALUE_WIDTH (COLS - 8)
99 static WINDOW
*boardw
;
100 static PANEL
*boardp
;
103 static WINDOW
*statusw
;
104 static PANEL
*statusp
;
105 static WINDOW
*historyw
;
106 static PANEL
*historyp
;
107 static WINDOW
*loadingw
;
108 static PANEL
*loadingp
;
109 static WINDOW
*enginew
;
110 static PANEL
*enginep
;
112 static char gameexp
[255];
113 static char moveexp
[255];
114 static struct itimerval clock_timer
;
115 static int delete_count
= 0;
116 static int markstart
= -1, markend
= -1;
118 static char loadfile
[FILENAME_MAX
];
120 static wint_t input_c
;
122 // Loaded filename from the command line or from the file input dialog.
125 FILE_NONE
, FILE_PGN
, FILE_FEN
, FILE_EPD
129 static int nag_total
;
130 static int macro_match
;
132 // Primer movimiento de juego cargado
133 // First move loaded game
134 static char fm_loaded_file
= FALSE
;
135 // Movimiento resultado de la función 'do_play_go'
136 // Movement function result 'do_play_go'
137 static int go_move
= 0;
138 // Controla rotación de tablero
139 // Rotation control board
140 static int rotate
= FALSE
;
142 static int COLS_OLD
, LINES_OLD
;
146 wchar_t *notify
; // The status window notification line buffer.
149 static int curses_initialized
;
151 // When in history mode a full step is to the next move of the same playing
152 // side. Half stepping is alternating sides.
155 static wchar_t *w_pawn_wchar
;
156 static wchar_t *w_rook_wchar
;
157 static wchar_t *w_bishop_wchar
;
158 static wchar_t *w_knight_wchar
;
159 static wchar_t *w_queen_wchar
;
160 static wchar_t *w_king_wchar
;
161 static wchar_t *b_pawn_wchar
;
162 static wchar_t *b_rook_wchar
;
163 static wchar_t *b_bishop_wchar
;
164 static wchar_t *b_knight_wchar
;
165 static wchar_t *b_queen_wchar
;
166 static wchar_t *b_king_wchar
;
167 static wchar_t *empty_wchar
;
168 static wchar_t *enpassant_wchar
;
170 static wchar_t *yes_wchar
;
171 static wchar_t *all_wchar
; // do_save_game_overwrite_confirm()
172 static wchar_t *overwrite_wchar
; // do_save_game_overwrite_confirm()
173 static wchar_t *resume_wchar
; // do_history_mode_confirm()
174 static wchar_t *current_wchar
; // do_game_save_multi_confirm()
175 static wchar_t *append_wchar
; // save_pgn()
177 static const char piece_chars
[] = "PpRrNnBbQqKkxx";
178 static char *translatable_tag_names
[7];
179 static const char *f_pieces
[] = {
203 static const bool cb
[8][8] = {
214 static void free_userdata_once(GAME g
);
215 static void do_more_help(WIN
*);
217 void coordofmove(GAME g
, char *move
, char *prow
, char *pcol
)
219 char l
= strlen(move
);
222 *prow
= (g
->turn
== WHITE
) ? 8 : 1;
232 while (!isdigit(*move
))
235 *prow
= RANKTOINT(*move
--);
236 *pcol
= FILETOINT(*move
);
239 // Posición por rotación de tablero.
240 // Rotation board position.
241 static void rotate_position(int p
)
243 struct userdata_s
*d
= gp
->data
;
245 char fr2
= 8, fc2
= 8;
247 for (fr
= 1; fr
< 9; fr
++) {
250 for (fc
= 1; fc
< 9; fc
++){
253 if (p
== CURSOR_POSITION
&&
254 d
->c_row
== fr
&& d
->c_col
== fc
) {
259 else if (p
== SP_POSITION
&&
260 d
->sp
.row
== fr
&& d
->sp
.col
== fc
) {
265 else if (p
== SPS_POSITION
&&
266 d
->sp
.srow
== fr
&& d
->sp
.scol
== fc
) {
277 void update_cursor(GAME g
, int idx
)
279 int t
= pgn_history_total(g
->hp
);
280 struct userdata_s
*d
= g
->data
;
283 * If not deincremented then r and c would be the next move.
287 if (idx
> t
|| idx
< 0 || !t
|| !g
->hp
[idx
]->move
)
288 d
->c_row
= 2, d
->c_col
= 5;
290 coordofmove(g
, g
->hp
[idx
]->move
, &d
->c_row
, &d
->c_col
);
292 if (d
->mode
== MODE_HISTORY
&& rotate
)
293 rotate_position(CURSOR_POSITION
);
296 static int init_nag()
302 if ((fp
= fopen(config
.nagfile
, "r")) == NULL
) {
303 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.nagfile
, strerror(errno
));
307 nags
= Realloc(nags
, (i
+2) * sizeof(char *));
308 nags
[i
++] = strdup(_("none"));
312 if (fscanf(fp
, " %[^\n] ", line
) == 1) {
313 nags
= Realloc(nags
, (i
+ 2) * sizeof(char *));
314 nags
[i
++] = strdup(line
);
323 void edit_nag_toggle_item(struct menu_input_s
*m
)
325 struct input_s
*in
= m
->data
;
326 struct input_data_s
*id
= in
->data
;
327 HISTORY
*h
= id
->data
;
330 if (m
->selected
== 0) {
331 for (i
= 0; i
< MAX_PGN_NAG
; i
++)
334 for (i
= 0; m
->items
[i
]; i
++)
335 m
->items
[i
]->selected
= 0;
340 for (i
= 0; i
< MAX_PGN_NAG
; i
++) {
341 if (h
->nag
[i
] == m
->selected
)
342 h
->nag
[i
] = m
->selected
= 0;
345 h
->nag
[i
] = m
->selected
;
352 void edit_nag_save(struct menu_input_s
*m
)
357 void edit_nag_help(struct menu_input_s
*m
)
359 message(_("NAG Menu Keys"), ANY_KEY_STR
, "%s",
361 " UP/DOWN - previous/next menu item\n"
362 " HOME/END - first/last menu item\n"
363 " PGDN/PGUP - next/previous page\n"
364 " a-zA-Z0-9 - jump to item\n"
365 " SPACE - toggle selected item\n"
366 " CTRL-X - quit with changes"
370 struct menu_item_s
**get_nag_items(WIN
*win
)
373 struct menu_input_s
*m
= win
->data
;
374 struct input_s
*in
= m
->data
;
375 struct input_data_s
*id
= in
->data
;
376 struct menu_item_s
**items
= m
->items
;
377 HISTORY
*h
= id
->data
;
380 for (i
= 0; items
[i
]; i
++)
384 for (i
= 0; nags
[i
]; i
++) {
385 items
= Realloc(items
, (i
+2) * sizeof(struct menu_item_s
*));
386 items
[i
] = Malloc(sizeof(struct menu_item_s
));
387 items
[i
]->name
= nags
[i
];
388 items
[i
]->value
= NULL
;
390 for (n
= 0; n
< MAX_PGN_NAG
; n
++) {
391 if (h
->nag
[n
] == i
) {
392 items
[i
]->selected
= 1;
399 items
[i
]->selected
= 0;
408 void nag_print(WIN
*win
)
410 struct menu_input_s
*m
= win
->data
;
412 mvwprintw(win
->w
, m
->print_line
, 1, "%-*s", win
->cols
- 2, m
->item
->name
);
415 void edit_nag(void *arg
)
417 struct menu_key_s
**keys
= NULL
;
424 add_menu_key(&keys
, ' ', edit_nag_toggle_item
);
425 add_menu_key(&keys
, CTRL_KEY('x'), edit_nag_save
);
426 add_menu_key(&keys
, KEY_F(1), edit_nag_help
);
427 construct_menu(0, 0, -1, -1, _("Numeric Annotation Glyphs"), 1, get_nag_items
, keys
, arg
,
432 static void *view_nag(void *arg
)
434 HISTORY
*h
= (HISTORY
*)arg
;
436 char line
[LINE_MAX
] = {0};
439 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Viewing NAG for"), h
->move
);
446 for (i
= 0; i
< MAX_PGN_NAG
; i
++) {
452 if (h
->nag
[i
] >= nag_total
)
453 strncat(line
, itoa(h
->nag
[i
], buf2
), sizeof(line
)-1);
455 strncat(line
, nags
[h
->nag
[i
]], sizeof(line
)-1);
457 strncat(line
, "\n", sizeof(line
)-1);
460 line
[strlen(line
) - 1] = 0;
461 message(buf
, ANY_KEY_STR
, "%s", line
);
465 void view_annotation(HISTORY
*h
)
467 char buf
[MAX_SAN_MOVE_LEN
+ strlen(_("Viewing Annotation for")) + 4];
468 int nag
= 0, comment
= 0;
473 if (h
->comment
&& h
->comment
[0])
479 if (!nag
&& !comment
)
482 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Viewing Annotation for"), h
->move
);
485 construct_message(buf
, (nag
) ? _("Any other key to continue") : ANY_KEY_STR
, 0, 1,
486 (nag
) ? _("Press 'n' to view NAG") : NULL
,
487 (nag
) ? view_nag
: NULL
, (nag
) ? h
: NULL
, NULL
,
488 (nag
) ? 'n' : 0, 0, "%s", h
->comment
);
490 construct_message(buf
, _("Any other key to continue"), 0, 1, _("Press 'n' to view NAG"), view_nag
, h
, NULL
,
491 'n', 0, "%s", _("No comment text for this move"));
494 int do_game_write(char *filename
, char *mode
, int start
, int end
)
497 struct userdata_s
*d
;
500 i
= pgn_open(filename
, mode
, &pgn
);
502 if (i
== E_PGN_ERR
) {
503 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", filename
, strerror(errno
));
506 else if (i
== E_PGN_INVALID
) {
507 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", filename
, _("Not a regular file"));
511 for (i
= (start
== -1) ? 0 : start
; i
< end
; i
++) {
513 pgn_write(pgn
, game
[i
]);
514 CLEAR_FLAG(d
->flags
, CF_MODIFIED
);
517 if (pgn_close(pgn
) != E_PGN_OK
)
518 message(ERROR_STR
, ANY_KEY_STR
, "%s", strerror(errno
));
521 strncpy(loadfile
, filename
, sizeof(loadfile
));
522 loadfile
[sizeof(loadfile
)-1] = 0;
535 void do_save_game_overwrite_confirm(WIN
*win
)
538 struct save_game_s
*s
= win
->data
;
539 wchar_t str
[] = { win
->c
, 0 };
541 if (!wcscmp (str
, append_wchar
))
543 else if (!wcscmp (str
, overwrite_wchar
))
548 if (do_game_write(s
->filename
, mode
, s
->start
, s
->end
))
549 update_status_notify(gp
, "%s", _("Save game failed."));
551 update_status_notify(gp
, "%s", _("Game saved."));
558 /* If the saveindex argument is -1, all games will be saved. Otherwise it's a
561 void save_pgn(char *filename
, int saveindex
)
563 char buf
[FILENAME_MAX
];
565 int end
= (saveindex
== -1) ? gtotal
: saveindex
+ 1;
566 struct save_game_s
*s
;
568 if (filename
[0] != '/' && config
.savedirectory
) {
569 if (stat(config
.savedirectory
, &st
) == -1) {
570 if (errno
== ENOENT
) {
571 if (mkdir(config
.savedirectory
, 0755) == -1) {
572 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
,
578 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
,
584 stat(config
.savedirectory
, &st
);
586 if (!S_ISDIR(st
.st_mode
)) {
587 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
, _("Not a directory."));
591 snprintf(buf
, sizeof(buf
), "%s/%s", config
.savedirectory
, filename
);
595 if (access(filename
, W_OK
) == 0) {
596 s
= Malloc(sizeof(struct save_game_s
));
597 s
->filename
= strdup(filename
);
598 s
->start
= saveindex
;
600 construct_message(NULL
, _("What would you like to do?"), 0, 1, NULL
,
601 NULL
, s
, do_save_game_overwrite_confirm
, 0, 0,
602 "%s \"%s\"\nPress \"%ls\" to append to this file, \"%ls\" to overwrite or any other key to cancel.",
603 _("File exists:"), filename
, append_wchar
,
608 if (do_game_write(filename
, "a", saveindex
, end
))
609 update_status_notify(gp
, "%s", _("Save game failed."));
611 update_status_notify(gp
, "%s", _("Game saved."));
614 static int castling_state(GAME g
, BOARD b
, int row
, int col
, int piece
, int mod
)
616 if (pgn_piece_to_int(piece
) == ROOK
&& col
== 7
618 (TEST_FLAG(g
->flags
, GF_WK_CASTLE
) || mod
) &&
619 pgn_piece_to_int(b
[7][4].icon
) == KING
&& isupper(piece
)) {
621 TOGGLE_FLAG(g
->flags
, GF_WK_CASTLE
);
624 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 0
626 (TEST_FLAG(g
->flags
, GF_WQ_CASTLE
) || mod
) &&
627 pgn_piece_to_int(b
[7][4].icon
) == KING
&& isupper(piece
)) {
629 TOGGLE_FLAG(g
->flags
, GF_WQ_CASTLE
);
632 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 7
634 (TEST_FLAG(g
->flags
, GF_BK_CASTLE
) || mod
) &&
635 pgn_piece_to_int(b
[0][4].icon
) == KING
&& islower(piece
)) {
637 TOGGLE_FLAG(g
->flags
, GF_BK_CASTLE
);
640 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 0
642 (TEST_FLAG(g
->flags
, GF_BQ_CASTLE
) || mod
) &&
643 pgn_piece_to_int(b
[0][4].icon
) == KING
&& islower(piece
)) {
645 TOGGLE_FLAG(g
->flags
, GF_BQ_CASTLE
);
648 else if (pgn_piece_to_int(piece
) == KING
&& col
== 4
650 (mod
|| (pgn_piece_to_int(b
[7][7].icon
) == ROOK
&&
651 TEST_FLAG(g
->flags
, GF_WK_CASTLE
))
653 (pgn_piece_to_int(b
[7][0].icon
) == ROOK
&&
654 TEST_FLAG(g
->flags
, GF_WQ_CASTLE
))) && isupper(piece
)) {
656 if (TEST_FLAG(g
->flags
, GF_WK_CASTLE
) ||
657 TEST_FLAG(g
->flags
, GF_WQ_CASTLE
))
658 CLEAR_FLAG(g
->flags
, GF_WK_CASTLE
|GF_WQ_CASTLE
);
660 SET_FLAG(g
->flags
, GF_WK_CASTLE
|GF_WQ_CASTLE
);
664 else if (pgn_piece_to_int(piece
) == KING
&& col
== 4
666 (mod
|| (pgn_piece_to_int(b
[0][7].icon
) == ROOK
&&
667 TEST_FLAG(g
->flags
, GF_BK_CASTLE
))
669 (pgn_piece_to_int(b
[0][0].icon
) == ROOK
&&
670 TEST_FLAG(g
->flags
, GF_BQ_CASTLE
))) && islower(piece
)) {
672 if (TEST_FLAG(g
->flags
, GF_BK_CASTLE
) ||
673 TEST_FLAG(g
->flags
, GF_BQ_CASTLE
))
674 CLEAR_FLAG(g
->flags
, GF_BK_CASTLE
|GF_BQ_CASTLE
);
676 SET_FLAG(g
->flags
, GF_BK_CASTLE
|GF_BQ_CASTLE
);
684 #define IS_ENPASSANT(c) (c == 'x') ? CP_BOARD_ENPASSANT : isupper(c) ? CP_BOARD_WHITE : CP_BOARD_BLACK
685 #define ATTRS(cp) (cp & (A_BOLD|A_STANDOUT|A_BLINK|A_DIM|A_UNDERLINE|A_INVIS|A_REVERSE))
690 w_pawn_wchar
= str_to_wchar (config
.utf8_pieces
? "♙" : "P");
691 w_rook_wchar
= str_to_wchar (config
.utf8_pieces
? "♖" : "R");
692 w_bishop_wchar
= str_to_wchar (config
.utf8_pieces
? "♗" : "B");
693 w_knight_wchar
= str_to_wchar (config
.utf8_pieces
? "♘" : "N");
694 w_queen_wchar
= str_to_wchar (config
.utf8_pieces
? "♕" : "Q");
695 w_king_wchar
= str_to_wchar (config
.utf8_pieces
? "♔" : "K");
696 b_pawn_wchar
= str_to_wchar (config
.utf8_pieces
? "♟" : "p");
697 b_rook_wchar
= str_to_wchar (config
.utf8_pieces
? "♜" : "r");
698 b_bishop_wchar
= str_to_wchar (config
.utf8_pieces
? "♝" : "b");
699 b_knight_wchar
= str_to_wchar (config
.utf8_pieces
? "♞" : "n");
700 b_queen_wchar
= str_to_wchar (config
.utf8_pieces
? "♛" : "q");
701 b_king_wchar
= str_to_wchar (config
.utf8_pieces
? "♚" : "k");
702 empty_wchar
= str_to_wchar (" ");
703 enpassant_wchar
= str_to_wchar ("x");
707 piece_to_wchar (unsigned char p
)
720 return w_bishop_wchar
;
722 return b_bishop_wchar
;
724 return w_knight_wchar
;
726 return b_knight_wchar
;
728 return w_queen_wchar
;
730 return b_queen_wchar
;
736 return enpassant_wchar
;
742 static int piece_can_attack (GAME g
, int rank
, int file
)
744 struct userdata_s
*d
= g
->data
;
745 char *m
, *frfr
= NULL
;
747 int row
, col
, p
, v
, pi
, cpi
;
750 rotate_position(CURSOR_POSITION
);
751 rotate_position(SPS_POSITION
);
754 v
= d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].valid
;
755 pi
= pgn_piece_to_int (d
->b
[RANKTOBOARD (rank
)][FILETOBOARD (file
)].icon
);
757 ? pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
)
758 : pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].icon
);
760 if (pi
== OPEN_SQUARE
|| cpi
== OPEN_SQUARE
|| !VALIDFILE (file
)
761 || !VALIDRANK (rank
)) {
763 rotate_position(CURSOR_POSITION
);
764 rotate_position(SPS_POSITION
);
771 col
= v
? d
->c_col
: d
->sp
.scol
;
772 row
= v
? d
->c_row
: d
->sp
.srow
;
779 m
= malloc(MAX_SAN_MOVE_LEN
+1);
780 m
[0] = INTTOFILE (file
);
781 m
[1] = INTTORANK (rank
);
782 m
[2] = INTTOFILE (col
);
783 m
[3] = INTTORANK (row
);
786 if (d
->sp
.icon
&& v
) {
789 memcpy (b
, d
->b
, sizeof(BOARD
));
790 p
= b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
;
791 b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
792 pgn_int_to_piece (WHITE
, OPEN_SQUARE
);
793 b
[RANKTOBOARD (row
)][FILETOBOARD (col
)].icon
= p
;
795 e
= pgn_validate_move (g
, b
, &m
, &frfr
);
800 if (e
!= E_PGN_OK
&& pgn_piece_to_int (d
->sp
.icon
) == PAWN
) {
801 int n
= (d
->sp
.srow
== 7 && rank
== 5) ? 6 :
802 (d
->sp
.srow
== 2 && rank
== 4) ? 3 : 0;
804 if (n
&& (file
== d
->c_col
-1 || file
== d
->c_col
+1)) {
805 memcpy (b
, d
->b
, sizeof(BOARD
));
806 p
= b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
;
807 b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
808 pgn_int_to_piece (WHITE
, OPEN_SQUARE
);
809 b
[RANKTOBOARD (row
)][FILETOBOARD (col
)].icon
= p
;
810 b
[RANKTOBOARD (n
)][FILETOBOARD (d
->sp
.scol
)].enpassant
= 1;
811 m
= malloc(MAX_SAN_MOVE_LEN
+1);
812 m
[0] = INTTOFILE (file
);
813 m
[1] = INTTORANK (rank
);
814 m
[2] = INTTOFILE (col
);
815 m
[3] = INTTORANK (n
);
818 SET_FLAG (g
->flags
, GF_ENPASSANT
);
819 e
= pgn_validate_move (g
, b
, &m
, &frfr
);
820 CLEAR_FLAG (g
->flags
, GF_ENPASSANT
);
828 rotate_position(CURSOR_POSITION
);
829 rotate_position(SPS_POSITION
);
832 return e
== E_PGN_OK
? 1 : 0;
836 e
= pgn_validate_move (g
, d
->b
, &m
, &frfr
);
839 if (!strcmp (m
, "O-O") || !strcmp (m
, "O-O-O"))
843 int sf
= FILETOINT (frfr
[0]), sr
= RANKTOINT (frfr
[1]);
844 int df
= FILETOINT (frfr
[2]);
846 pi
= d
->b
[RANKTOBOARD (sr
)][FILETOBOARD (sf
)].icon
;
847 pi
= pgn_piece_to_int (pi
);
848 if (pi
== PAWN
&& sf
== df
)
856 rotate_position(CURSOR_POSITION
);
857 rotate_position(SPS_POSITION
);
860 return e
== E_PGN_OK
? 1 : 0;
863 void print_piece(WINDOW
*w
, int l
, int c
, char p
)
867 for (i
= 0; i
< 13; i
+= 2) {
868 if (p
== piece_chars
[i
] || p
== piece_chars
[i
+ 1]) {
869 for (y
= 0; y
< 3; y
++)
870 mvwprintw(w
, l
+ y
, c
, "%s", f_pieces
[i
+ ff
+ y
]);
877 for (y
= 0; y
< 3; y
++)
878 mvwprintw(w
, l
+ y
, c
, f_pieces
[0]);
885 for (fx
= 1; fx
< 9; fx
++) {
894 static int is_prev_move (struct userdata_s
*d
, int brow
, int bcol
,
898 row
== ROWTOMATRIX(((rotate
) ? inv_int(d
->pm_row
) : d
->pm_row
)) &&
899 col
== COLTOMATRIX(((rotate
) ? inv_int(d
->pm_col
) : d
->pm_col
)))
901 brow
+ 1 == ((rotate
) ? d
->pm_row
: inv_int(d
->pm_row
)) &&
902 bcol
+ 1 == ((rotate
) ? inv_int(d
->pm_col
) : d
->pm_col
)))
908 void update_board_window(GAME g
)
911 int bcol
= 0, brow
= 0;
912 int l
= config
.coordsyleft
;
913 int maxy
= BOARD_HEIGHT
, maxx
= BOARD_WIDTH
;
914 int ncols
= 0, offset
= 1;
915 int rowr
= (MEGA_BOARD
) ? 6 : (BIG_BOARD
) ? 4 : 2;
916 int colr
= (MEGA_BOARD
) ? 12 : (BIG_BOARD
) ? 8 : 4;
917 unsigned coords_y
= 8, cxgc
= 0;
919 struct userdata_s
*d
= g
->data
;
922 h
= pgn_history_by_n(g
->hp
, g
->hindex
- 1);
923 if (h
&& h
->move
&& d
->mode
== MODE_PLAY
)
924 coordofmove(g
, h
->move
, &d
->pm_row
, &d
->pm_col
);
930 if (d
->mode
!= MODE_PLAY
&& d
->mode
!= MODE_EDIT
)
931 update_cursor(g
, g
->hindex
);
948 for (row
= 0; row
< maxy
; row
++) {
962 for (col
= 0; col
< maxx
; col
++) {
964 chtype attrs
= 0, old_attrs
= 0;
969 if (row
== 0 || row
== maxy
- 2) {
971 mvwaddch(boardw
, row
, col
+ l
,
973 ? ACS_LLCORNER
| CP_BOARD_GRAPHICS
974 : ACS_ULCORNER
| CP_BOARD_GRAPHICS
));
975 else if (col
== maxx
- 2)
976 mvwaddch(boardw
, row
, col
+ l
,
978 ? ACS_LRCORNER
| CP_BOARD_GRAPHICS
979 : ACS_URCORNER
| CP_BOARD_GRAPHICS
));
980 else if (!(col
% colr
))
981 mvwaddch(boardw
, row
, col
+ l
,
983 ? ACS_BTEE
| CP_BOARD_GRAPHICS
984 : ACS_TTEE
| CP_BOARD_GRAPHICS
));
987 mvwaddch(boardw
, row
, col
+ l
,
988 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
994 if ((row
% 2) && col
== maxx
- 1 &&
995 (coords_y
> 0 && coords_y
< 9)) {
996 wattron(boardw
, CP_BOARD_COORDS
);
998 (BIG_BOARD
) ? row
* ((MEGA_BOARD
) ? 3 : 2)
999 : row
, (l
) ? 0 : col
, "%d",
1000 (rotate
) ? coords_y
++ : coords_y
--);
1001 wattroff(boardw
, CP_BOARD_COORDS
);
1005 if ((col
== 0 || col
== maxx
- 2) && row
!= maxy
- 1) {
1007 mvwaddch(boardw
, row
, col
+ l
,
1008 LINE_GRAPHIC((col
) ?
1009 ACS_RTEE
| CP_BOARD_GRAPHICS
:
1010 ACS_LTEE
| CP_BOARD_GRAPHICS
));
1012 mvwaddch(boardw
, row
, col
+ l
,
1013 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
1018 if ((row
% rowr
) && !(col
% colr
) && row
!= maxy
- 1) {
1019 mvwaddch(boardw
, row
, col
+ l
,
1020 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
1024 if (!(col
% colr
) && row
!= maxy
- 1) {
1025 mvwaddch(boardw
, row
, col
+ l
,
1026 LINE_GRAPHIC(ACS_PLUS
| CP_BOARD_GRAPHICS
));
1033 attrwhich
= (cb
[brow
][bcol
]) ? WHITE
: BLACK
;
1040 if (((ncols
% 2) && !(offset
% 2))
1041 || (!(ncols
% 2) && (offset
% 2)))
1047 if (BIG_BOARD
&& rotate
) {
1048 brow
= inv_int(brow
+ 1) - 1;
1049 bcol
= inv_int(bcol
+ 1) - 1;
1053 p
= d
->b
[brow
][bcol
].icon
;
1055 p
= d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].icon
;
1057 int pi
= pgn_piece_to_int(p
);
1059 if (config
.details
&&
1060 ((!BIG_BOARD
&& d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].enpassant
)
1061 || (BIG_BOARD
&& d
->b
[brow
][bcol
].enpassant
))) {
1063 attrs
= mix_cp(CP_BOARD_ENPASSANT
,
1064 (attrwhich
== WHITE
) ? CP_BOARD_WHITE
: CP_BOARD_BLACK
,
1065 ATTRS(CP_BOARD_ENPASSANT
), A_FG_B_BG
);
1068 if (config
.showattacks
&& config
.details
1069 && piece_can_attack (g
,
1070 BIG_BOARD
? inv_int (brow
+1) : brow
,
1071 BIG_BOARD
? bcol
+1 : bcol
)) {
1072 attrs
= CP_BOARD_ATTACK
;
1077 if (config
.validmoves
&&
1078 ((!BIG_BOARD
&& d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].valid
)
1079 || (BIG_BOARD
&& d
->b
[brow
][bcol
].valid
))) {
1083 if (attrwhich
== WHITE
)
1084 attrs
= mix_cp(CP_BOARD_MOVES_WHITE
,
1086 ATTRS(CP_BOARD_MOVES_WHITE
),
1089 attrs
= mix_cp(CP_BOARD_MOVES_BLACK
,
1091 ATTRS(CP_BOARD_MOVES_BLACK
),
1094 else if (p
!= 'x' && !can_attack
)
1095 attrs
= (attrwhich
== WHITE
) ? CP_BOARD_WHITE
: CP_BOARD_BLACK
;
1097 if (BIG_BOARD
&& rotate
) {
1098 brow
= inv_int(brow
+ 1) - 1;
1099 bcol
= inv_int(bcol
+ 1) - 1;
1102 if ((!BIG_BOARD
&& row
== ROWTOMATRIX(d
->c_row
)
1103 && col
== COLTOMATRIX(d
->c_col
))
1104 || (BIG_BOARD
&& brow
+ 1 == inv_int(d
->c_row
)
1105 && bcol
+ 1 == d
->c_col
)) {
1106 attrs
= mix_cp(CP_BOARD_CURSOR
, IS_ENPASSANT(p
),
1107 ATTRS(CP_BOARD_CURSOR
), B_FG_A_BG
);
1110 else if ((!BIG_BOARD
&& row
== ROWTOMATRIX(d
->sp
.srow
) &&
1111 col
== COLTOMATRIX(d
->sp
.scol
)) ||
1112 (BIG_BOARD
&& brow
+ 1 == inv_int(d
->sp
.srow
) &&
1113 bcol
+ 1 == d
->sp
.scol
)) {
1114 attrs
= mix_cp(CP_BOARD_SELECTED
, IS_ENPASSANT(p
),
1115 ATTRS(CP_BOARD_SELECTED
), B_FG_A_BG
);
1118 else if (is_prev_move (d
, brow
, bcol
, row
, col
) && !valid
)
1120 attrs
= mix_cp(CP_BOARD_PREVMOVE
, IS_ENPASSANT(p
),
1121 ATTRS(CP_BOARD_PREVMOVE
), B_FG_A_BG
);
1125 if (row
== maxy
- 1)
1129 int n
= is_prev_move (d
, brow
, bcol
, row
, col
);
1130 chtype a
= n
&& !valid
1131 ? CP_BOARD_PREVMOVE
: attrwhich
== WHITE
? valid
? CP_BOARD_MOVES_WHITE
: CP_BOARD_WHITE
: valid
? CP_BOARD_MOVES_BLACK
: CP_BOARD_BLACK
;
1132 attrs
= mix_cp(CP_BOARD_ATTACK
, a
,
1133 ATTRS (CP_BOARD_ATTACK
), A_FG_B_BG
);
1138 wmove(boardw
, row
, col
+ ((MEGA_BOARD
) ? 5 : 3) + l
);
1140 mvwaddch(boardw
, row
, col
+ l
, ' ' | attrs
);
1142 if (row
== maxy
- 1 && cxgc
< 8) {
1143 waddch(boardw
, "abcdefgh"[(BIG_BOARD
) ? bcol
: bcol
- 1] | CP_BOARD_COORDS
);
1147 if (old_attrs
== -1) {
1154 if (pi
!= OPEN_SQUARE
&& p
!= 'x' && !can_attack
) {
1155 if (attrwhich
== WHITE
) {
1157 attrs
= CP_BOARD_W_W
;
1159 attrs
= CP_BOARD_W_B
;
1163 attrs
= CP_BOARD_B_W
;
1165 attrs
= CP_BOARD_B_B
;
1171 if (config
.details
&& !can_attack
1172 && castling_state(g
, d
->b
,
1173 (rotate
) ? inv_int(brow
+ 1) - 1: brow
,
1174 (rotate
) ? inv_int(bcol
+ 1) - 1: bcol
, p
, 0))
1175 attrs
= mix_cp(CP_BOARD_CASTLING
, attrs
,
1176 ATTRS(CP_BOARD_CASTLING
),
1180 if (config
.details
&& !can_attack
1181 && castling_state(g
, d
->b
, RANKTOBOARD (brow
),
1182 FILETOBOARD (bcol
), p
, 0)) {
1183 attrs
= mix_cp(CP_BOARD_CASTLING
, attrs
,
1184 ATTRS(CP_BOARD_CASTLING
),
1190 // FIXME: Reimpresión de piezas(+4).
1192 wattron (boardw
, attrs
);
1194 for (i
= 0; i
< 5; i
++)
1195 mvwprintw(boardw
, i
+ brow
* 6 + 1,
1198 if (pi
!= OPEN_SQUARE
)
1199 print_piece(boardw
, brow
* 6 + 2,
1200 bcol
* 12 + 3 + l
, p
);
1203 print_piece(boardw
, brow
* 4 + 1,
1205 (pi
!= OPEN_SQUARE
) ? p
: 0);
1208 wattroff (boardw
, attrs
);
1213 wattron (boardw
, attrs
);
1214 waddwstr (boardw
, piece_to_wchar (pi
!= OPEN_SQUARE
? p
: 0));
1215 wattroff (boardw
, attrs
);
1222 col
+= (MEGA_BOARD
) ? 10 : 6;
1224 waddch(boardw
, ' ' | attrs
);
1242 if (col
!= maxx
- 1)
1243 mvwaddch(boardw
, row
, col
+ l
,
1244 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
1264 void invalid_move(int n
, int e
, const char *m
)
1266 if (curses_initialized
)
1267 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s \"%s\" (round #%i)", (e
== E_PGN_AMBIGUOUS
)
1268 ? _("Ambiguous move") : _("Invalid move"), m
, n
);
1270 warnx("%s: %s \"%s\" (round #%i)", loadfile
, (e
== E_PGN_AMBIGUOUS
)
1271 ? _("Ambiguous move") : _("Invalid move"), m
, n
);
1274 void gameover(GAME g
)
1276 struct userdata_s
*d
= g
->data
;
1278 SET_FLAG(g
->flags
, GF_GAMEOVER
);
1279 d
->mode
= MODE_HISTORY
;
1283 static void update_clock(GAME g
, struct itimerval it
)
1285 struct userdata_s
*d
= g
->data
;
1287 if (TEST_FLAG(d
->flags
, CF_CLOCK
) && g
->turn
== WHITE
) {
1288 d
->wclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1289 d
->wclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1291 if (d
->wclock
.elapsed
.tv_usec
> 1000000 - 1) {
1292 d
->wclock
.elapsed
.tv_sec
+= d
->wclock
.elapsed
.tv_usec
/ 1000000;
1293 d
->wclock
.elapsed
.tv_usec
= d
->wclock
.elapsed
.tv_usec
% 1000000;
1296 if (d
->wclock
.tc
[d
->wclock
.tcn
][1] &&
1297 d
->wclock
.elapsed
.tv_sec
>= d
->wclock
.tc
[d
->wclock
.tcn
][1]) {
1298 pgn_tag_add(&g
->tag
, "Result", "0-1");
1302 else if (TEST_FLAG(d
->flags
, CF_CLOCK
) && g
->turn
== BLACK
) {
1303 d
->bclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1304 d
->bclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1306 if (d
->bclock
.elapsed
.tv_usec
> 1000000 - 1) {
1307 d
->bclock
.elapsed
.tv_sec
+= d
->bclock
.elapsed
.tv_usec
/ 1000000;
1308 d
->bclock
.elapsed
.tv_usec
= d
->bclock
.elapsed
.tv_usec
% 1000000;
1311 if (d
->bclock
.tc
[d
->bclock
.tcn
][1] &&
1312 d
->bclock
.elapsed
.tv_sec
>= d
->bclock
.tc
[d
->bclock
.tcn
][1]) {
1313 pgn_tag_add(&g
->tag
, "Result", "1-0");
1318 d
->elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1319 d
->elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1321 if (d
->elapsed
.tv_usec
> 1000000 - 1) {
1322 d
->elapsed
.tv_sec
+= d
->elapsed
.tv_usec
/ 1000000;
1323 d
->elapsed
.tv_usec
= d
->elapsed
.tv_usec
% 1000000;
1327 static void update_time_control(GAME g
)
1329 struct userdata_s
*d
= g
->data
;
1330 struct clock_s
*clk
= (g
->turn
== WHITE
) ? &d
->wclock
: &d
->bclock
;
1333 clk
->tc
[clk
->tcn
][1] += clk
->incr
;
1335 if (!clk
->tc
[clk
->tcn
][1])
1340 if (!clk
->tc
[clk
->tcn
][0] || clk
->move
>= clk
->tc
[clk
->tcn
][0]) {
1342 clk
->tc
[clk
->tcn
+ 1][1] += abs(clk
->elapsed
.tv_sec
- clk
->tc
[clk
->tcn
][1]);
1343 memset(&clk
->elapsed
, 0, sizeof(clk
->elapsed
));
1348 void update_history_window(GAME g
)
1350 char buf
[HISTORY_WIDTH
- 1];
1353 int t
= pgn_history_total(g
->hp
);
1355 n
= (g
->hindex
+ 1) / 2;
1358 total
= (t
+ 1) / 2;
1363 snprintf(buf
, sizeof(buf
), "%u %s %u%s", n
, _("of"), total
,
1364 (movestep
== 1) ? _(" (ply)") : "");
1366 strncpy(buf
, _("not available"), sizeof(buf
)-1);
1368 buf
[sizeof(buf
)-1] = 0;
1369 mvwprintw(historyw
, 2, 1, "%*s %-*s", 10, _("Move:"),
1370 HISTORY_WIDTH
- 14, buf
);
1372 h
= pgn_history_by_n(g
->hp
, g
->hindex
);
1373 snprintf(buf
, sizeof(buf
), "%s",
1374 (h
&& h
->move
) ? h
->move
1375 : (LINES
< 24) ? _("empty") : _("not available"));
1378 if (h
&& ((h
->comment
) || h
->nag
[0])) {
1379 strncat(buf
, _(" (Annotated"), sizeof(buf
)-1);
1384 strncat(buf
, (n
) ? ",+" : " (+", sizeof(buf
)-1);
1389 strncat(buf
, (n
) ? ",-" : " (-", sizeof(buf
)-1);
1394 strncat(buf
, ")", sizeof(buf
)-1);
1396 mvwprintw(historyw
, 3, ((LINES
< 24) ? 17 : 1), "%s %-*s",
1397 (LINES
< 24) ? _("Next:") :_("Next move:"),
1398 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1400 h
= pgn_history_by_n(g
->hp
, g
->hindex
- 1);
1401 snprintf(buf
, sizeof(buf
), "%s",
1402 (h
&& h
->move
) ? h
->move
1403 : (LINES
< 24) ? _("empty") : _("not available"));
1406 if (h
&& ((h
->comment
) || h
->nag
[0])) {
1407 strncat(buf
, _(" (Annotated"), sizeof(buf
)-1);
1412 strncat(buf
, (n
) ? ",+" : " (+", sizeof(buf
)-1);
1417 strncat(buf
, (n
) ? ",-" : " (-", sizeof(buf
)-1);
1422 strncat(buf
, ")", sizeof(buf
)-1);
1424 mvwprintw(historyw
, ((LINES
< 24) ? 3 : 4), 1, "%s %-*s",
1425 (LINES
< 24) ? _("Prev.:") : _("Prev move:"),
1426 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1429 void do_validate_move(char **move
)
1431 struct userdata_s
*d
= gp
->data
;
1435 if (TEST_FLAG(d
->flags
, CF_HUMAN
)) {
1436 if ((n
= pgn_parse_move(gp
, d
->b
, move
, &frfr
)) != E_PGN_OK
) {
1437 invalid_move(d
->n
+ 1, n
, *move
);
1441 update_time_control(gp
);
1442 pgn_history_add(gp
, d
->b
, *move
);
1443 pgn_switch_turn(gp
);
1446 if ((n
= pgn_validate_move(gp
, d
->b
, move
, &frfr
)) != E_PGN_OK
) {
1447 invalid_move(d
->n
+ 1, n
, *move
);
1451 add_engine_command(gp
, ENGINE_THINKING
, "%s\n",
1452 (config
.engine_protocol
== 1) ? frfr
: *move
);
1455 d
->sp
.srow
= d
->sp
.scol
= d
->sp
.icon
= 0;
1457 if (config
.validmoves
)
1458 pgn_reset_valid_moves(d
->b
);
1460 if (TEST_FLAG(gp
->flags
, GF_GAMEOVER
))
1461 d
->mode
= MODE_HISTORY
;
1463 SET_FLAG(d
->flags
, CF_MODIFIED
);
1467 update_history_window(gp
);
1468 update_board_window(gp
);
1472 void do_promotion_piece_finalize(WIN
*win
)
1474 char *p
, *str
= win
->data
;
1476 if (pgn_piece_to_int(win
->c
) == -1)
1479 p
= str
+ strlen(str
);
1480 *p
++ = toupper(win
->c
);
1482 do_validate_move(&str
);
1487 static void move_to_engine(GAME g
)
1489 struct userdata_s
*d
= g
->data
;
1493 if (config
.validmoves
&&
1494 !d
->b
[RANKTOBOARD(d
->sp
.row
)][FILETOBOARD(d
->sp
.col
)].valid
)
1497 str
= Malloc(MAX_SAN_MOVE_LEN
+ 1);
1498 snprintf(str
, MAX_SAN_MOVE_LEN
+ 1, "%c%i%c%i",
1499 _("abcdefgh")[d
->sp
.scol
- 1],
1500 d
->sp
.srow
, _("abcdefgh")[d
->sp
.col
- 1], d
->sp
.row
);
1502 piece
= pgn_piece_to_int(d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
);
1504 if (piece
== PAWN
&& (d
->sp
.row
== 8 || d
->sp
.row
== 1)) {
1505 construct_message(_("Select Pawn Promotion Piece"), _ ("R/N/B/Q"), 1, 1, NULL
, NULL
,
1506 str
, do_promotion_piece_finalize
, 0, 0, "%s", _("R = Rook, N = Knight, B = Bishop, Q = Queen"));
1510 do_validate_move(&str
);
1514 static char *clock_to_char(long n
)
1516 static char buf
[16];
1517 int h
= 0, m
= 0, s
= 0;
1520 m
= (n
% 3600) / 60;
1521 s
= (n
% 3600) % 60;
1522 snprintf(buf
, sizeof(buf
), "%.2i:%.2i:%.2i", h
, m
, s
);
1526 static char *timeval_to_char(struct timeval t
, long limit
)
1529 int h
= 0, m
= 0, s
= 0;
1530 int n
= limit
? abs(limit
- t
.tv_sec
) : 0;
1533 m
= (n
% 3600) / 60;
1534 s
= (n
% 3600) % 60;
1535 snprintf(buf
, sizeof(buf
), "%.2i:%.2i:%.2i", h
, m
, s
);
1539 static char *time_control_status(struct clock_s
*clk
)
1541 static char buf
[80] = {0};
1545 if (clk
->tc
[clk
->tcn
][0] && clk
->tc
[clk
->tcn
+ 1][1])
1546 snprintf(buf
, sizeof(buf
), " M%.2i/%s", abs(clk
->tc
[clk
->tcn
][0] - clk
->move
),
1547 clock_to_char(clk
->tc
[clk
->tcn
+ 1][1]));
1548 else if (!clk
->incr
)
1553 strncat(buf
, " I", sizeof(buf
)-1);
1554 strncat(buf
, itoa(clk
->incr
, buf
), sizeof(buf
)-1);
1560 void update_status_window(GAME g
)
1564 char tmp
[15] = {0}, *engine
, *mode
;
1570 struct userdata_s
*d
= g
->data
;
1574 if (!curses_initialized
)
1577 getmaxyx(statusw
, maxy
, maxx
);
1584 wchar_t *loadfilew
= loadfile
[0] ? str_etc (loadfile
, w
, 1) : str_to_wchar (_ ("not available"));
1585 mvwprintw(statusw
, y
++, 1, "%*s %-*ls", 7, _("File:"), w
, loadfilew
);
1587 snprintf(buf
, len
, "%i %s %i", gindex
+ 1, _("of"), gtotal
);
1588 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Game:"), w
, buf
);
1593 if (config
.details
) {
1598 if (TEST_FLAG(d
->flags
, CF_DELETE
)) {
1606 if (TEST_FLAG(g
->flags
, GF_PERROR
)) {
1614 if (TEST_FLAG(d
->flags
, CF_MODIFIED
)) {
1622 pgn_config_get(PGN_STRICT_CASTLING
, &n
);
1632 if (TEST_FLAG(d
->flags
, CF_PERL
)) {
1642 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Flags:"), w
, (tmp
[0]) ? tmp
: "-");
1646 mode
= _("move history");
1655 mode
= _("(empty value)");
1659 snprintf(buf
, len
- 1, "%*s %s", 7, _("Mode:"), mode
);
1661 if (d
->mode
== MODE_PLAY
) {
1662 if (TEST_FLAG(d
->flags
, CF_HUMAN
))
1663 strncat(buf
, _(" (human/human)"), len
- 1);
1664 else if (TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
))
1665 strncat(buf
, _(" (engine/engine)"), len
- 1);
1667 strncat(buf
, (d
->play_mode
== PLAY_EH
) ?
1668 _(" (engine/human)") : _(" (human/engine)"), len
- 1);
1672 mvwprintw(statusw
, y
++, 1, "%-*s", len
, buf
);
1676 switch (d
->engine
->status
) {
1677 case ENGINE_THINKING
:
1678 engine
= _("pondering...");
1681 engine
= _("ready");
1683 case ENGINE_INITIALIZING
:
1684 engine
= _("initializing...");
1686 case ENGINE_OFFLINE
:
1687 engine
= _("offline");
1690 engine
= _("(empty value)");
1695 engine
= _("offline");
1697 mvwprintw(statusw
, y
, 1, "%*s %-*s", 7, _("Engine:"), w
, " ");
1698 wattron(statusw
, CP_STATUS_ENGINE
);
1699 mvwaddstr(statusw
, y
++, 9, engine
);
1700 wattroff(statusw
, CP_STATUS_ENGINE
);
1702 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Turn:"), w
,
1703 (g
->turn
== WHITE
) ? _("white") : _("black"));
1705 strncpy(tmp
, _("white"), sizeof(tmp
)-1);
1706 tmp
[0] = toupper(tmp
[0]);
1707 snprintf(t
, sizeof(t
), "%s%s",
1708 timeval_to_char(d
->wclock
.elapsed
, d
->wclock
.tc
[d
->wclock
.tcn
][1]),
1709 time_control_status(&d
->wclock
));
1710 mvwprintw(statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1712 strncpy(tmp
, _("black"), sizeof(tmp
)-1);
1713 tmp
[0] = toupper(tmp
[0]);
1714 snprintf(t
, sizeof(t
), "%s%s",
1715 timeval_to_char(d
->bclock
.elapsed
, d
->bclock
.tc
[d
->bclock
.tcn
][1]),
1716 time_control_status(&d
->bclock
));
1717 mvwprintw(statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1719 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Total:"), w
,
1720 clock_to_char(d
->elapsed
.tv_sec
));
1722 // for (i = 0; i < STATUS_WIDTH; i++)
1723 // mvwprintw(stdscr, STATUS_HEIGHT, i, " ");
1726 status
.notify
= str_to_wchar(_("Type F1 for help"));
1728 wattron(stdscr
, CP_STATUS_NOTIFY
);
1729 for (i
= (config
.boardleft
) ? BOARD_WIDTH
: 0;
1730 i
< ((config
.boardleft
) ? COLS
: STATUS_WIDTH
); i
++)
1731 mvwprintw(stdscr
, STATUS_HEIGHT
, i
, " ");
1732 mvwprintw(stdscr
, STATUS_HEIGHT
, CENTERX(STATUS_WIDTH
, status
.notify
)
1733 + ((config
.boardleft
) ? BOARD_WIDTH
: 0),
1734 "%ls", status
.notify
);
1735 wattroff(stdscr
, CP_STATUS_NOTIFY
);
1738 wchar_t *translate_tag_name(const char *tag
)
1740 if (!strcmp (tag
, "Event"))
1741 return str_to_wchar (translatable_tag_names
[0]);
1742 else if (!strcmp (tag
, "Site"))
1743 return str_to_wchar (translatable_tag_names
[1]);
1744 else if (!strcmp (tag
, "Date"))
1745 return str_to_wchar (translatable_tag_names
[2]);
1746 else if (!strcmp (tag
, "Round"))
1747 return str_to_wchar (translatable_tag_names
[3]);
1748 else if (!strcmp (tag
, "White"))
1749 return str_to_wchar (translatable_tag_names
[4]);
1750 else if (!strcmp (tag
, "Black"))
1751 return str_to_wchar (translatable_tag_names
[5]);
1752 else if (!strcmp (tag
, "Result"))
1753 return str_to_wchar (translatable_tag_names
[6]);
1755 return str_to_wchar (tag
);
1758 void update_tag_window(TAG
**t
)
1763 for (i
= 0; t
[i
]; i
++) {
1764 wchar_t *namewc
= translate_tag_name(t
[i
]->name
);
1772 w
= TAG_WIDTH
- namel
- 4;
1774 for (i
= 0; t
[i
] && i
< TAG_HEIGHT
- 3; i
++) {
1775 wchar_t *namewc
= translate_tag_name(t
[i
]->name
);
1776 wchar_t *valuewc
= str_etc(t
[i
]->value
, w
, 0);
1778 mvwprintw(tagw
, (i
+ 2), 1, "%*ls: %-*ls", namel
, namewc
, w
, valuewc
);
1783 for (; i
< TAG_HEIGHT
- 3; i
++)
1784 mvwprintw(tagw
, (i
+ 2), 1, "%*s", namel
+ w
+ 2, " ");
1787 void append_enginebuf(GAME g
, char *line
)
1790 struct userdata_s
*d
= g
->data
;
1792 if (d
->engine
->enginebuf
)
1793 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++);
1795 if (i
>= LINES
- 3) {
1796 free(d
->engine
->enginebuf
[0]);
1798 for (i
= 0; d
->engine
->enginebuf
[i
+1]; i
++)
1799 d
->engine
->enginebuf
[i
] = d
->engine
->enginebuf
[i
+1];
1801 d
->engine
->enginebuf
[i
] = strdup(line
);
1804 d
->engine
->enginebuf
= Realloc(d
->engine
->enginebuf
, (i
+ 2) * sizeof(char *));
1805 d
->engine
->enginebuf
[i
++] = strdup(line
);
1806 d
->engine
->enginebuf
[i
] = NULL
;
1810 void update_engine_window(GAME g
)
1813 struct userdata_s
*d
= g
->data
;
1815 wmove(enginew
, 0, 0);
1818 if (d
->engine
&& d
->engine
->enginebuf
) {
1819 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++)
1820 mvwprintw(enginew
, i
+ 2, 1, "%s", d
->engine
->enginebuf
[i
]);
1823 window_draw_title(enginew
, _("Engine IO Window"), COLS
, CP_MESSAGE_TITLE
,
1827 void update_all(GAME g
)
1829 struct userdata_s
*d
= g
->data
;
1832 * In the middle of a macro. Don't update the screen.
1834 if (macro_match
!= -1)
1837 wmove(boardw
, ROWTOMATRIX(d
->c_row
), COLTOMATRIX(d
->c_col
));
1838 update_board_window(g
);
1839 update_status_window(g
);
1840 update_history_window(g
);
1841 update_tag_window(g
->tag
);
1842 update_engine_window(g
);
1847 static void game_next_prev(GAME g
, int n
, int count
)
1853 if (gindex
+ count
> gtotal
- 1) {
1855 gindex
= gtotal
- 1;
1863 if (gindex
- count
< 0) {
1867 gindex
= gtotal
- 1;
1876 static void delete_game(int which
)
1879 struct userdata_s
*d
;
1881 for (i
= 0; i
< gtotal
; i
++) {
1884 if (i
== w
|| TEST_FLAG(d
->flags
, CF_DELETE
)) {
1887 free_userdata_once(game
[i
]);
1890 for (n
= i
; n
+1 < gtotal
; n
++)
1891 game
[n
] = game
[n
+1];
1900 if (which
+ 1 >= gtotal
)
1901 gindex
= gtotal
- 1;
1906 gindex
= gtotal
- 1;
1909 gp
->hp
= gp
->history
;
1913 * FIXME find across multiple games.
1915 static int find_move_exp(GAME g
, regex_t r
, int which
, int count
)
1923 incr
= (which
== 0) ? -1 : 1;
1925 for (i
= g
->hindex
+ incr
- 1, found
= 0; ; i
+= incr
) {
1926 if (i
== g
->hindex
- 1)
1929 if (i
>= pgn_history_total(g
->hp
))
1932 i
= pgn_history_total(g
->hp
) - 1;
1935 ret
= regexec(&r
, g
->hp
[i
]->move
, 0, 0, 0);
1938 if (count
== ++found
) {
1943 if (ret
!= REG_NOMATCH
) {
1944 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
1945 cmessage(_("Error Matching Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
1954 static int toggle_delete_flag(int n
)
1957 struct userdata_s
*d
= game
[n
]->data
;
1959 TOGGLE_FLAG(d
->flags
, CF_DELETE
);
1962 for (i
= x
= 0; i
< gtotal
; i
++) {
1965 if (TEST_FLAG(d
->flags
, CF_DELETE
))
1970 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
1972 CLEAR_FLAG(d
->flags
, CF_DELETE
);
1979 static int find_game_exp(char *str
, int which
, int count
)
1981 char *nstr
= NULL
, *exp
= NULL
;
1985 char buf
[255] = {0}, *tmp
;
1988 int incr
= (which
== 0) ? -(1) : 1;
1990 strncpy(buf
, str
, sizeof(buf
)-1);
1993 if (strstr(tmp
, ":") != NULL
) {
1994 nstr
= strsep(&tmp
, ":");
1996 if ((ret
= regcomp(&nexp
, nstr
,
1997 REG_ICASE
|REG_EXTENDED
|REG_NOSUB
)) != 0) {
1998 regerror(ret
, &nexp
, errbuf
, sizeof(errbuf
));
1999 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
2007 while (*exp
&& isspace(*exp
))
2013 if ((ret
= regcomp(&vexp
, exp
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
2014 regerror(ret
, &vexp
, errbuf
, sizeof(errbuf
));
2015 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
2022 for (g
= gindex
+ incr
, found
= 0; ; g
+= incr
) {
2033 for (t
= 0; game
[g
]->tag
[t
]; t
++) {
2035 if (regexec(&nexp
, game
[g
]->tag
[t
]->name
, 0, 0, 0) == 0) {
2036 if (regexec(&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0) {
2037 if (count
== ++found
) {
2045 if (regexec(&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0) {
2046 if (count
== ++found
) {
2068 * Updates the notification line in the status window then refreshes the
2071 void update_status_notify(GAME g
, char *fmt
, ...)
2074 #ifdef HAVE_VASPRINTF
2080 free(status
.notify
);
2081 status
.notify
= NULL
;
2087 #ifdef HAVE_VASPRINTF
2088 vasprintf(&line
, fmt
, ap
);
2090 vsnprintf(line
, sizeof(line
), fmt
, ap
);
2094 status
.notify
= str_to_wchar(line
);
2096 #ifdef HAVE_VASPRINTF
2101 int rav_next_prev(GAME g
, BOARD b
, int n
)
2105 if ((!g
->ravlevel
&& g
->hindex
&& g
->hp
[g
->hindex
- 1]->rav
== NULL
) ||
2106 (!g
->ravlevel
&& !g
->hindex
&& g
->hp
[g
->hindex
]->rav
== NULL
) ||
2107 (g
->ravlevel
&& g
->hp
[g
->hindex
]->rav
== NULL
))
2110 g
->rav
= Realloc(g
->rav
, (g
->ravlevel
+ 1) * sizeof(RAV
));
2111 g
->rav
[g
->ravlevel
].hp
= g
->hp
;
2112 g
->rav
[g
->ravlevel
].flags
= g
->flags
;
2113 g
->rav
[g
->ravlevel
].fen
= pgn_game_to_fen(g
, b
);
2114 g
->rav
[g
->ravlevel
].hindex
= g
->hindex
;
2115 g
->hp
= (!g
->ravlevel
) ? (g
->hindex
) ? g
->hp
[g
->hindex
- 1]->rav
: g
->hp
[g
->hindex
]->rav
: g
->hp
[g
->hindex
]->rav
;
2118 pgn_board_update(g
, b
, g
->hindex
+ 1);
2122 if (g
->ravlevel
- 1 < 0)
2127 pgn_board_init_fen(g
, b
, g
->rav
[g
->ravlevel
].fen
);
2128 free(g
->rav
[g
->ravlevel
].fen
);
2129 g
->hp
= g
->rav
[g
->ravlevel
].hp
;
2130 g
->flags
= g
->rav
[g
->ravlevel
].flags
;
2131 g
->hindex
= g
->rav
[g
->ravlevel
].hindex
;
2135 static void draw_window_decor()
2137 move_panel(boardp
, 0,
2138 (config
.boardleft
) ? 0 : COLS
- BOARD_WIDTH
);
2139 move_panel(historyp
, LINES
- HISTORY_HEIGHT
,
2140 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
: 0 :
2141 (MEGA_BOARD
) ? 0 : COLS
- HISTORY_WIDTH
);
2142 move_panel(statusp
, 0,
2143 (config
.boardleft
) ? BOARD_WIDTH
: 0);
2144 move_panel(tagp
, STATUS_HEIGHT
+ 1,
2145 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
:
2148 wbkgd(boardw
, CP_BOARD_WINDOW
);
2149 wbkgd(statusw
, CP_STATUS_WINDOW
);
2150 window_draw_title(statusw
, _("Game Status"), STATUS_WIDTH
,
2151 CP_STATUS_TITLE
, CP_STATUS_BORDER
);
2152 wbkgd(tagw
, CP_TAG_WINDOW
);
2153 window_draw_title(tagw
, _("Roster Tags"), TAG_WIDTH
, CP_TAG_TITLE
,
2155 wbkgd(historyw
, CP_HISTORY_WINDOW
);
2156 window_draw_title(historyw
, _("Move History"), HISTORY_WIDTH
,
2157 CP_HISTORY_TITLE
, CP_HISTORY_BORDER
);
2160 void do_window_resize()
2162 if (LINES
< 23 || COLS
< 74)
2165 resizeterm(LINES
, COLS
);
2167 wresize(boardw
, BOARD_HEIGHT
, BOARD_WIDTH
);
2168 wresize(historyw
, HISTORY_HEIGHT
, HISTORY_WIDTH
);
2169 wresize(statusw
, STATUS_HEIGHT
, STATUS_WIDTH
);
2170 wresize(tagw
, TAG_HEIGHT
, TAG_WIDTH
);
2178 draw_window_decor();
2180 keypad(boardw
, TRUE
);
2186 void do_global_redraw ()
2188 do_window_resize ();
2193 memset(&clock_timer
, 0, sizeof(struct itimerval
));
2194 setitimer(ITIMER_REAL
, &clock_timer
, NULL
);
2197 void start_clock(GAME g
)
2199 struct userdata_s
*d
= g
->data
;
2201 if (clock_timer
.it_interval
.tv_usec
)
2204 memset(&d
->elapsed
, 0, sizeof(struct timeval
));
2205 clock_timer
.it_value
.tv_sec
= 0;
2206 clock_timer
.it_value
.tv_usec
= 100000;
2207 clock_timer
.it_interval
.tv_sec
= 0;
2208 clock_timer
.it_interval
.tv_usec
= 100000;
2209 setitimer(ITIMER_REAL
, &clock_timer
, NULL
);
2212 static void update_clocks()
2215 struct userdata_s
*d
;
2216 struct itimerval it
;
2219 getitimer(ITIMER_REAL
, &it
);
2221 for (i
= 0; i
< gtotal
; i
++) {
2224 if (d
&& d
->mode
== MODE_PLAY
) {
2225 if (d
->paused
== 1 || TEST_FLAG(d
->flags
, CF_NEW
))
2227 else if (d
->paused
== -1) {
2228 if (game
[i
]->side
== game
[i
]->turn
) {
2234 update_clock(game
[i
], it
);
2242 update_status_window(gp
);
2248 #define SKIP_SPACE(str) { while (isspace(*str)) str++; }
2250 static int parse_clock_time(char **str
)
2305 static int parse_clock_input(struct clock_s
*clk
, char *str
, int *incr
)
2327 memset(clk
, 0, sizeof(struct clock_s
));
2331 if (strncasecmp(p
, "SD", 2) == 0) {
2337 n
= parse_clock_time(&p
);
2351 /* Sudden death without a previous time control. */
2357 n
= parse_clock_time(&p
);
2363 message(ERROR_STR
, ANY_KEY_STR
, "%s (%i)", _("Maximum number of time controls reached"), MAX_TC
);
2368 clk
->tc
[tc
++][1] = n
;
2383 clk
->tc
[clk
->tcn
][1] = (n
<= clk
->elapsed
.tv_sec
) ? clk
->elapsed
.tv_sec
+ n
: n
;
2391 n
= parse_clock_time(&p
);
2411 static int parse_which_clock(struct clock_s
*clk
, char *str
)
2416 memcpy(&tmp
, clk
, sizeof(struct clock_s
));
2418 if (parse_clock_input(&tmp
, str
, &incr
)) {
2419 cmessage(ERROR_STR
, ANY_KEY_STR
, _("Invalid clock specification"));
2423 memcpy(clk
, &tmp
, sizeof(struct clock_s
));
2424 clk
->tc
[clk
->tcn
][1] += incr
;
2428 void do_clock_input_finalize(WIN
*win
)
2430 struct userdata_s
*d
= gp
->data
;
2431 struct input_data_s
*in
= win
->data
;
2441 if (tolower(*p
) == 'w') {
2444 if (parse_which_clock(&d
->wclock
, p
))
2447 else if (tolower(*p
) == 'b') {
2450 if (parse_which_clock(&d
->bclock
, p
))
2454 if (parse_which_clock(&d
->wclock
, p
))
2457 if (parse_which_clock(&d
->bclock
, p
))
2461 if (!d
->wclock
.tc
[0][1] && !d
->bclock
.tc
[0][1])
2462 CLEAR_FLAG(d
->flags
, CF_CLOCK
);
2464 SET_FLAG(d
->flags
, CF_CLOCK
);
2471 void do_engine_command_finalize(WIN
*win
)
2473 struct userdata_s
*d
= gp
->data
;
2474 struct input_data_s
*in
= win
->data
;
2485 x
= d
->engine
->status
;
2486 send_to_engine(gp
, -1, "%s\n", in
->str
);
2487 d
->engine
->status
= x
;
2494 void do_board_details()
2496 config
.details
= (config
.details
) ? 0 : 1;
2499 void do_toggle_strict_castling()
2503 pgn_config_get(PGN_STRICT_CASTLING
, &n
);
2506 pgn_config_set(PGN_STRICT_CASTLING
, 1);
2508 pgn_config_set(PGN_STRICT_CASTLING
, 0);
2511 void do_play_set_clock()
2513 struct input_data_s
*in
;
2515 in
= Calloc(1, sizeof(struct input_data_s
));
2516 in
->efunc
= do_clock_input_finalize
;
2517 construct_input(_("Set Clock"), NULL
, 1, 1,
2518 _ ("Format: [W | B] [+]T[+I] | ++I | M/T [M/T [...] [SD/T]] [+I]\n" \
2519 "T = time (hms), I = increment, M = moves per, SD = sudden death\ne.g., 30m or 4m+12s or 35/90m SD/30m"),
2520 NULL
, NULL
, 0, in
, INPUT_HIST_CLOCK
, -1);
2523 void do_play_toggle_human()
2525 struct userdata_s
*d
= gp
->data
;
2527 TOGGLE_FLAG(d
->flags
, CF_HUMAN
);
2529 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && pgn_history_total(gp
->hp
)) {
2530 if (init_chess_engine(gp
))
2534 CLEAR_FLAG(d
->flags
, CF_ENGINE_LOOP
);
2537 d
->engine
->status
= ENGINE_READY
;
2540 void do_play_toggle_engine()
2542 struct userdata_s
*d
= gp
->data
;
2544 TOGGLE_FLAG(d
->flags
, CF_ENGINE_LOOP
);
2545 CLEAR_FLAG(d
->flags
, CF_HUMAN
);
2547 if (d
->engine
&& TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
)) {
2548 char *fen
= pgn_game_to_fen (gp
, d
->b
);
2550 pgn_board_update(gp
, d
->b
,
2551 pgn_history_total(gp
->hp
));
2552 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
2558 * This will send a command to the engine skipping the command queue.
2560 void do_play_send_command()
2562 struct userdata_s
*d
= gp
->data
;
2563 struct input_data_s
*in
;
2565 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
) {
2566 if (init_chess_engine(gp
))
2570 in
= Calloc(1, sizeof(struct input_data_s
));
2571 in
->efunc
= do_engine_command_finalize
;
2572 construct_input(_("Engine Command"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
, INPUT_HIST_ENGINE
, -1);
2575 void do_play_switch_turn()
2577 struct userdata_s *d = gp->data;
2579 pgn_switch_side(gp);
2580 pgn_switch_turn(gp);
2582 if (!TEST_FLAG(d->flags, CF_HUMAN))
2583 add_engine_command(gp, -1,
2584 (gp->side == WHITE) ? "white\n" : "black\n");
2586 update_status_window(gp);
2589 void do_play_toggle_eh_mode()
2591 struct userdata_s
*d
= gp
->data
;
2593 if (!TEST_FLAG(d
->flags
, CF_HUMAN
)) {
2595 pgn_switch_side(gp
, TRUE
);
2596 d
->play_mode
= (d
->play_mode
) ? PLAY_HE
: PLAY_EH
;
2597 if (gp
->side
== BLACK
)
2598 update_status_notify(gp
, _("Press 'g' to start the game"));
2600 rotate
= (rotate
) ? FALSE
: TRUE
;
2603 message(NULL
, ANY_KEY_STR
,
2604 _("You may only switch sides at the start of the \n"
2605 "game. Press ^K or ^N to begin a new game."));
2611 struct userdata_s
*d
= gp
->data
;
2613 if (!pgn_history_total(gp
->hp
))
2617 if (gp
->hindex
- keycount
< 0)
2621 gp
->hindex
-= (keycount
* 2) -1;
2623 gp
->hindex
-= keycount
* 2;
2627 if (gp
->hindex
- 2 < 0)
2637 pgn_history_free(gp
->hp
, gp
->hindex
);
2638 gp
->hindex
= pgn_history_total(gp
->hp
);
2639 pgn_board_update(gp
, d
->b
, gp
->hindex
);
2641 if (d
->engine
&& d
->engine
->status
== ENGINE_READY
) {
2642 char *fen
= pgn_game_to_fen(gp
, d
->b
);
2644 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
2646 d
->engine
->status
= ENGINE_READY
;
2649 update_history_window(gp
);
2652 pgn_switch_side(gp
, FALSE
);
2657 void do_play_toggle_pause()
2659 struct userdata_s
*d
= gp
->data
;
2661 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && gp
->turn
!=
2667 d
->paused
= (d
->paused
) ? 0 : 1;
2672 struct userdata_s
*d
= gp
->data
;
2674 if (TEST_FLAG(d
->flags
, CF_HUMAN
))
2677 if (fm_loaded_file
&& gp
->side
!= gp
->turn
) {
2678 pgn_switch_side(gp
, FALSE
);
2679 add_engine_command(gp
, ENGINE_THINKING
, "black\n");
2682 add_engine_command(gp
, ENGINE_THINKING
, "go\n");
2684 // Completa la función para que permita seguir jugando al usarla.
2685 // Complete the function to allow continue playing when using.
2686 if (gp
->side
== gp
->turn
)
2687 pgn_switch_side(gp
, FALSE
);
2692 void do_play_config_command()
2697 for (x
= 0; config
.keys
[x
]; x
++) {
2698 if (config
.keys
[x
]->c
== input_c
) {
2699 switch (config
.keys
[x
]->type
) {
2701 add_engine_command(gp
, -1, "%ls\n",
2702 config
.keys
[x
]->str
);
2708 add_engine_command(gp
, -1,
2709 "%ls %i\n", config
.keys
[x
]->str
, keycount
);
2716 for (w
= 0; w
< keycount
; w
++)
2717 add_engine_command(gp
, -1,
2718 "%ls\n", config
.keys
[x
]->str
);
2726 update_status_notify(gp
, NULL
);
2729 void do_play_cancel_selected()
2731 struct userdata_s
*d
= gp
->data
;
2733 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
2735 pgn_reset_valid_moves(d
->b
);
2736 update_status_notify(gp
, NULL
);
2739 void do_play_commit()
2741 struct userdata_s
*d
= gp
->data
;
2743 pushkey
= keycount
= 0;
2744 update_status_notify(gp
, NULL
);
2746 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) &&
2747 (!d
->engine
|| d
->engine
->status
== ENGINE_THINKING
))
2753 d
->sp
.row
= d
->c_row
;
2754 d
->sp
.col
= d
->c_col
;
2757 rotate_position(SP_POSITION
);
2758 rotate_position(SPS_POSITION
);
2763 // Completa la función para que permita seguir jugando cuando se carga un
2764 // archivo pgn (con juego no terminado) que inicie con turno del lado
2766 // Complete the function to allow continue playing when loading a file
2767 // pgn (with unfinished game) you start to turn black side.
2768 if (gp
->side
!= gp
->turn
)
2769 pgn_switch_side(gp
, FALSE
);
2771 if (rotate
&& d
->sp
.icon
)
2772 rotate_position(SPS_POSITION
);
2774 // Envia comando 'go' a Polyglot en el primer movimiento debido a que
2775 // polyglot no envia el movimiento y cboard se queda esperando.
2776 // Send command 'go' to the first movement Polyglot because cboard
2777 // waits to send polyglot movement and this does not make.
2778 if (config
.fmpolyglot
&&
2779 ((gp
->side
== WHITE
&& !gp
->hindex
) || fm_loaded_file
))
2780 add_engine_command(gp
, ENGINE_THINKING
, "go\n");
2783 fm_loaded_file
= FALSE
;
2786 void do_play_select()
2788 struct userdata_s
*d
= gp
->data
;
2790 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && (!d
->engine
||
2791 d
->engine
->status
== ENGINE_OFFLINE
)) {
2792 if (init_chess_engine(gp
))
2796 if (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
)
2800 do_play_cancel_selected ();
2803 rotate_position(CURSOR_POSITION
);
2805 d
->sp
.icon
= d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
;
2807 if (pgn_piece_to_int(d
->sp
.icon
) == OPEN_SQUARE
) {
2812 if (((islower(d
->sp
.icon
) && gp
->turn
!= BLACK
)
2813 || (isupper(d
->sp
.icon
) && gp
->turn
!= WHITE
))) {
2815 char *str
= Malloc (512);
2817 for (k
= play_keys
; *k
; k
++) {
2818 if ((*k
)->f
== do_play_toggle_eh_mode
)
2822 snprintf (str
, 512, _("It is not your turn to move. You may switch playing sides by pressing \"%lc\"."),
2823 *k
? (*k
)->c
: '?');
2824 message(NULL
, ANY_KEY_STR
, "%s", str
);
2829 if (pgn_history_total(gp
->hp
)) {
2830 message(NULL
, ANY_KEY_STR
, "%s", _("It is not your turn to move. You can switch sides "));
2835 if (pgn_tag_find(gp
->tag
, "FEN") != E_PGN_ERR
)
2838 add_engine_command(gp
, ENGINE_READY
, "black\n");
2839 pgn_switch_turn(gp
);
2841 if (gp
->side
!= BLACK
)
2842 pgn_switch_side(gp
);
2847 d
->sp
.srow
= d
->c_row
;
2848 d
->sp
.scol
= d
->c_col
;
2850 if (config
.validmoves
)
2851 pgn_find_valid_moves(gp
, d
->b
, d
->sp
.scol
, d
->sp
.srow
);
2854 rotate_position(CURSOR_POSITION
);
2855 rotate_position(SPS_POSITION
);
2858 CLEAR_FLAG(d
->flags
, CF_NEW
);
2862 static wchar_t *build_help(struct key_s
**keys
)
2864 int i
, nlen
= 1, len
, t
, n
;
2865 wchar_t *buf
= NULL
, *wc
= NULL
;
2871 for (i
= len
= t
= 0; keys
[i
]; i
++) {
2876 if (wcslen(keys
[i
]->key
) > nlen
) {
2877 nlen
= wcslen(keys
[i
]->key
);
2887 if (wcslen(keys
[i
]->d
) > len
)
2888 len
= wcslen(keys
[i
]->d
);
2896 buf
= Malloc((t
+1)*sizeof(wchar_t));
2899 for (i
= 0; keys
[i
]; i
++) {
2904 n
= wcslen(keys
[i
]->key
);
2914 wcsncat(buf
, keys
[i
]->key
, t
-1);
2915 p
= buf
+ wcslen(buf
);
2926 wcsncat(buf
, keys
[i
]->d
, t
-1);
2929 wc
= str_to_wchar ("*");
2930 wcsncat(buf
, wc
, t
-1);
2934 wc
= str_to_wchar ("\n");
2937 p
= buf
+ wcslen(buf
);
2943 void do_global_help ()
2945 wchar_t *buf
= build_help(global_keys
);
2947 construct_message(_("Global Game Keys (* = can take a repeat count)"),
2948 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
, buf
, do_more_help
,
2952 void do_main_help(WIN
*win
)
2972 static void do_more_help(WIN
*win
)
2974 if (win
->c
== KEY_F(1) || win
->c
== CTRL_KEY('g'))
2975 construct_message(_("Command Key Index"),
2976 _ ("p/h/e/g or any other key to quit"), 0, 0,
2977 NULL
, NULL
, NULL
, do_main_help
, 0, 0, "%s",
2979 "p - play mode keys\n"
2980 "h - history mode keys\n"
2981 "e - board edit mode keys\n"
2982 "g - global game keys"
2988 wchar_t *buf
= build_help(play_keys
);
2990 construct_message(_("Play Mode Keys (* = can take a repeat count)"),
2991 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
,
2992 buf
, do_more_help
, 0, 1, "%ls", buf
);
2995 void do_play_history_mode()
2997 struct userdata_s
*d
= gp
->data
;
2999 if (!pgn_history_total(gp
->hp
) ||
3000 (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
))
3003 d
->mode
= MODE_HISTORY
;
3004 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3007 void do_play_edit_mode()
3009 struct userdata_s
*d
= gp
->data
;
3011 if (pgn_history_total(gp
->hp
))
3014 pgn_board_init_fen(gp
, d
->b
, NULL
);
3016 d
->mode
= MODE_EDIT
;
3019 void do_edit_insert_finalize(WIN
*win
)
3021 struct userdata_s
*d
= win
->data
;
3023 if (pgn_piece_to_int(win
->c
) == -1)
3026 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
= win
->c
;
3029 void do_edit_select()
3031 struct userdata_s
*d
= gp
->data
;
3036 d
->sp
.icon
= d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
;
3038 if (pgn_piece_to_int(d
->sp
.icon
) == OPEN_SQUARE
) {
3043 d
->sp
.srow
= d
->c_row
;
3044 d
->sp
.scol
= d
->c_col
;
3047 void do_edit_commit()
3050 struct userdata_s
*d
= gp
->data
;
3052 pushkey
= keycount
= 0;
3053 update_status_notify(gp
, NULL
);
3058 d
->sp
.row
= d
->c_row
;
3059 d
->sp
.col
= d
->c_col
;
3060 p
= d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
;
3061 d
->b
[RANKTOBOARD(d
->sp
.row
)][FILETOBOARD(d
->sp
.col
)].icon
= p
;
3062 d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
=
3063 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3064 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3067 void do_edit_delete()
3069 struct userdata_s
*d
= gp
->data
;
3072 d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
=
3073 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3075 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
=
3076 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3078 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3081 void do_edit_cancel_selected()
3083 struct userdata_s
*d
= gp
->data
;
3085 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3087 update_status_notify(gp
, NULL
);
3090 void do_edit_switch_turn()
3092 pgn_switch_turn(gp
);
3095 void do_edit_toggle_castle()
3097 struct userdata_s
*d
= gp
->data
;
3099 castling_state(gp
, d
->b
, RANKTOBOARD(d
->c_row
),
3100 FILETOBOARD(d
->c_col
),
3101 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
, 1);
3104 void do_edit_insert()
3106 struct userdata_s
*d
= gp
->data
;
3108 construct_message(_("Insert Piece"), _("P=pawn, R=rook, N=knight, B=bishop, "), 0, 0, NULL
, NULL
,
3109 d
->b
, do_edit_insert_finalize
, 0, 0, "%s", _("Type the piece letter to insert. Lowercase "));
3112 void do_edit_enpassant()
3114 struct userdata_s
*d
= gp
->data
;
3116 if (d
->c_row
== 6 || d
->c_row
== 3) {
3117 pgn_reset_enpassant(d
->b
);
3118 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].enpassant
= 1;
3124 wchar_t *buf
= build_help(edit_keys
);
3126 construct_message(_("Edit Mode Keys (* = can take a repeat count)"),
3127 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
, buf
, do_more_help
,
3133 struct userdata_s
*d
= gp
->data
;
3134 char *fen
= pgn_game_to_fen(gp
, d
->b
);
3137 pgn_tag_add(&gp
->tag
, "FEN", fen
);
3139 pgn_tag_add(&gp
->tag
, "SetUp", "1");
3140 pgn_tag_sort(gp
->tag
);
3141 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3142 d
->mode
= MODE_PLAY
;
3145 void really_do_annotate_finalize(struct input_data_s
*in
,
3146 struct userdata_s
*d
)
3148 HISTORY
*h
= in
->data
;
3158 len
= strlen(in
->str
);
3159 h
->comment
= Realloc(h
->comment
, len
+1);
3160 strncpy(h
->comment
, in
->str
, len
);
3161 h
->comment
[len
] = 0;
3166 SET_FLAG(d
->flags
, CF_MODIFIED
);
3169 void do_annotate_finalize(WIN
*win
)
3171 struct userdata_s
*d
= gp
->data
;
3172 struct input_data_s
*in
= win
->data
;
3174 really_do_annotate_finalize(in
, d
);
3177 void do_find_move_exp_finalize(int init
, int which
)
3180 struct userdata_s
*d
= gp
->data
;
3181 static int firstrun
;
3186 if (init
|| !firstrun
) {
3190 if ((ret
= regcomp(&r
, moveexp
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
3191 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
3192 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
3199 if ((n
= find_move_exp(gp
, r
,
3200 (which
== -1) ? 0 : 1, (keycount
) ? keycount
: 1)) == -1)
3204 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3207 void do_find_move_exp(WIN
*win
)
3209 struct input_data_s
*in
= win
->data
;
3214 strncpy(moveexp
, in
->str
, sizeof(moveexp
)-1);
3215 moveexp
[sizeof(moveexp
)-1] = 0;
3216 do_find_move_exp_finalize(1, which
);
3224 void do_move_jump_finalize(int n
)
3226 struct userdata_s
*d
= gp
->data
;
3228 if (n
< 0 || n
> (pgn_history_total(gp
->hp
) / 2))
3232 update_status_notify(gp
, NULL
);
3233 gp
->hindex
= (n
) ? n
* 2 - 1 : n
* 2;
3234 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3237 void do_move_jump(WIN
*win
)
3239 struct input_data_s
*in
= win
->data
;
3241 if (!in
->str
|| !isinteger(in
->str
)) {
3249 do_move_jump_finalize(atoi(in
->str
));
3254 struct history_menu_s
{
3262 void free_history_menu_data(struct history_menu_s
**h
)
3269 for (i
= 0; h
[i
]; i
++) {
3277 void get_history_data(HISTORY
**hp
, struct history_menu_s
***menu
, int m
,
3281 int t
= pgn_history_total(hp
);
3282 char buf
[MAX_SAN_MOVE_LEN
+ 4];
3284 struct history_menu_s
**hmenu
= *menu
;
3287 for (n
= 0; hmenu
[n
]; n
++);
3291 for (i
= 0; i
< t
; i
++) {
3292 hmenu
= Realloc(hmenu
, (n
+ 2) * sizeof(struct history_menu_s
*));
3293 hmenu
[n
] = Malloc(sizeof(struct history_menu_s
));
3294 snprintf(buf
, sizeof(buf
), "%c%s%s", (turn
== WHITE
) ? 'W' : 'B',
3295 hp
[i
]->move
, (hp
[i
]->comment
|| hp
[i
]->nag
[0]) ? " !" : "");
3296 hmenu
[n
]->line
= strdup(buf
);
3297 hmenu
[n
]->hindex
= i
;
3298 hmenu
[n
]->indent
= 0;
3299 hmenu
[n
]->ravlevel
= depth
;
3300 hmenu
[n
]->move
= (n
&& depth
> hmenu
[n
-1]->ravlevel
) ? m
++ : m
;
3307 get_history_data(hp
[i
]->rav
, &hmenu
, m
, turn
);
3308 for (n
= 0; hmenu
[n
]; n
++);
3316 turn
= (turn
== WHITE
) ? BLACK
: WHITE
;
3322 void history_draw_update(struct menu_input_s
*m
)
3325 struct userdata_s
*d
= g
->data
;
3327 g
->hindex
= m
->selected
+ 1;
3328 update_cursor(g
, m
->selected
);
3329 pgn_board_update(g
, d
->b
, m
->selected
+ 1);
3332 struct menu_item_s
**get_history_items(WIN
*win
)
3334 struct menu_input_s
*m
= win
->data
;
3336 struct userdata_s
*d
= g
->data
;
3337 struct history_menu_s
**hm
= d
->data
;
3338 struct menu_item_s
**items
= m
->items
;
3342 get_history_data(g
->history
, &hm
, 0,
3343 TEST_FLAG(g
->flags
, GF_BLACK_OPENING
));
3344 m
->selected
= g
->hindex
- 1;
3346 if (m
->selected
< 0)
3349 m
->draw_exit_func
= history_draw_update
;
3355 for (i
= 0; items
[i
]; i
++)
3362 for (i
= 0; hm
[i
]; i
++) {
3363 items
= Realloc(items
, (i
+2) * sizeof(struct menu_item_s
*));
3364 items
[i
] = Malloc(sizeof(struct menu_item_s
));
3365 items
[i
]->name
= hm
[i
]->line
;
3366 items
[i
]->value
= NULL
;
3367 items
[i
]->selected
= 0;
3378 void history_menu_quit(struct menu_input_s
*m
)
3383 void history_menu_exit(WIN
*win
)
3386 struct userdata_s
*d
= g
->data
;
3387 struct history_menu_s
**hm
= d
->data
;
3393 for (i
= 0; hm
[i
]; i
++) {
3403 void history_menu_next(struct menu_input_s
*m
)
3406 struct userdata_s
*d
= g
->data
;
3407 struct history_menu_s
**hm
= d
->data
;
3410 for (t
= 0; hm
[t
]; t
++);
3412 if (m
->selected
+ 1 == t
)
3415 n
= hm
[m
->selected
+ 1]->hindex
;
3422 void history_menu_prev(struct menu_input_s
*m
)
3425 struct userdata_s
*d
= g
->data
;
3426 struct history_menu_s
**hm
= d
->data
;
3429 for (t
= 0; hm
[t
]; t
++);
3431 if (m
->selected
- 1 < 0)
3434 n
= hm
[m
->selected
- 1]->hindex
;
3440 void history_menu_help(struct menu_input_s
*m
)
3442 message(_("History Menu Help"), ANY_KEY_STR
, "%s",
3444 " UP/DOWN - previous/next menu item\n"
3445 " HOME/END - first/last menu item\n"
3446 " PGDN/PGUP - next/previous page\n"
3447 " a-zA-Z0-9 - jump to item\n"
3448 " CTRL-a - annotate the selected move\n"
3449 " ENTER - view annotation\n"
3450 " CTRL-d - toggle board details\n"
3451 " ESCAPE/M - return to move history"
3455 void do_annotate_move(HISTORY
*hp
)
3458 struct input_data_s
*in
;
3460 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Editing Annotation for"), hp
->move
);
3461 in
= Calloc(1, sizeof(struct input_data_s
));
3463 in
->efunc
= do_annotate_finalize
;
3464 construct_input(buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
3465 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
, CTRL_KEY('T'), in
, -1, -1);
3468 void history_menu_view_annotation(struct menu_input_s
*m
)
3473 view_annotation(g
->history
[m
->selected
]);
3476 void history_menu_annotate_finalize(WIN
*win
)
3478 struct input_data_s
*in
= win
->data
;
3479 GAME g
= in
->moredata
;
3480 struct userdata_s
*d
= g
->data
;
3481 struct history_menu_s
**hm
= d
->data
;
3483 really_do_annotate_finalize(in
, d
);
3484 free_history_menu_data(hm
);
3486 get_history_data(g
->history
, &hm
, 0, TEST_FLAG(g
->flags
, GF_BLACK_OPENING
));
3488 pushkey
= REFRESH_MENU
;
3491 void history_menu_annotate(struct menu_input_s
*m
)
3495 struct input_data_s
*in
;
3496 HISTORY
*hp
= g
->history
[m
->selected
]; // FIXME RAV
3498 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Editing Annotation for"), hp
->move
);
3499 in
= Calloc(1, sizeof(struct input_data_s
));
3501 in
->moredata
= m
->data
;
3502 in
->efunc
= history_menu_annotate_finalize
;
3503 construct_input(buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
3504 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
, CTRL_KEY('T'), in
, -1, -1);
3507 void history_menu_details(struct menu_input_s
*m
)
3513 void history_menu_print(WIN
*win
)
3515 struct menu_input_s
*m
= win
->data
;
3517 struct userdata_s
*d
= g
->data
;
3518 struct history_menu_s
**hm
= d
->data
;
3519 struct history_menu_s
*h
= hm
[m
->top
];
3521 char *p
= m
->item
->name
;
3522 int line
= m
->print_line
- 2;
3524 * Solaris 5.9 doesn't have wattr_get() or any function that requires an
3531 for (total
= 0; hm
[total
]; total
++);
3532 wattr_get(win
->w
, &attrs
, &pair
, NULL
);
3533 wattroff(win
->w
, COLOR_PAIR(pair
));
3534 mvwaddch(win
->w
, m
->print_line
, 1,
3535 *p
== 'W' ? *p
| mix_cp(CP_BOARD_WHITE
, CP_HISTORY_WINDOW
, ATTRS(CP_BOARD_WHITE
), A_FG_B_BG
) : *p
| mix_cp(CP_BOARD_BLACK
, CP_HISTORY_WINDOW
, ATTRS(CP_BOARD_BLACK
), A_FG_B_BG
));
3538 if (h
->hindex
== 0 && line
== 0)
3539 waddch(win
->w
, ACS_ULCORNER
| CP_HISTORY_MENU_LG
);
3540 else if ((!hm
[h
->hindex
+ (win
->rows
- 5) + 1] && line
== win
->rows
- 5) ||
3541 (m
->top
+ line
== total
- 1))
3542 waddch(win
->w
, ACS_LLCORNER
| CP_HISTORY_MENU_LG
);
3543 else if (hm
[m
->top
+ 1]->ravlevel
!= h
->ravlevel
|| !h
->ravlevel
)
3544 waddch(win
->w
, ACS_LTEE
| CP_HISTORY_MENU_LG
);
3546 waddch(win
->w
, ACS_VLINE
| CP_HISTORY_MENU_LG
);
3548 wattron(win
->w
, COLOR_PAIR(pair
) | attrs
);
3550 for (i
= 2; *p
; p
++, i
++)
3551 waddch(win
->w
, (*p
== '!') ? *p
| A_BOLD
: *p
);
3553 while (i
++ < win
->cols
- 2)
3554 waddch(win
->w
, ' ');
3557 void history_menu(GAME g
)
3559 struct menu_key_s
**keys
= NULL
;
3561 add_menu_key(&keys
, KEY_ESCAPE
, history_menu_quit
);
3562 add_menu_key(&keys
, 'M', history_menu_quit
);
3563 add_menu_key(&keys
, KEY_UP
, history_menu_prev
);
3564 add_menu_key(&keys
, KEY_DOWN
, history_menu_next
);
3565 add_menu_key(&keys
, KEY_F(1), history_menu_help
);
3566 add_menu_key(&keys
, CTRL_KEY('a'), history_menu_annotate
);
3567 add_menu_key(&keys
, CTRL_KEY('d'), history_menu_details
);
3568 add_menu_key(&keys
, '\n', history_menu_view_annotation
);
3569 construct_menu(MEGA_BOARD
? LINES
- HISTORY_HEIGHT_MB
: LINES
,
3570 TAG_WIDTH
, 0, config
.boardleft
? BOARD_WIDTH
: 0,
3571 _("Move History Tree"), 1, get_history_items
, keys
, g
,
3572 history_menu_print
, history_menu_exit
);
3575 void do_history_menu()
3580 void do_history_half_move_toggle()
3582 movestep
= (movestep
== 1) ? 2 : 1;
3583 update_history_window(gp
);
3586 void do_history_rotate_board()
3588 rotate
= (rotate
) ? FALSE
: TRUE
;
3591 void do_history_jump_next()
3593 struct userdata_s
*d
= gp
->data
;
3595 pgn_history_next(gp
, d
->b
, (keycount
> 0) ?
3596 config
.jumpcount
* keycount
* movestep
:
3597 config
.jumpcount
* movestep
);
3600 void do_history_jump_prev()
3602 struct userdata_s
*d
= gp
->data
;
3604 pgn_history_prev(gp
, d
->b
, (keycount
) ?
3605 config
.jumpcount
* keycount
* movestep
:
3606 config
.jumpcount
* movestep
);
3609 void do_history_prev()
3611 struct userdata_s
*d
= gp
->data
;
3613 pgn_history_prev(gp
, d
->b
,
3614 (keycount
) ? keycount
* movestep
: movestep
);
3617 void do_history_next()
3619 struct userdata_s
*d
= gp
->data
;
3621 pgn_history_next(gp
, d
->b
, (keycount
) ?
3622 keycount
* movestep
: movestep
);
3625 void do_history_mode_finalize(struct userdata_s
*d
)
3628 d
->mode
= MODE_PLAY
;
3631 void do_history_mode_confirm(WIN
*win
)
3633 struct userdata_s
*d
= gp
->data
;
3634 wchar_t str
[] = { win
->c
, 0 };
3636 if (!wcscmp (str
, resume_wchar
)) {
3637 pgn_history_free(gp
->hp
, gp
->hindex
);
3638 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3643 if (pgn_history_rav_new(gp
, d
->b
,
3644 gp
->hindex
) != E_PGN_OK
)
3652 if (!TEST_FLAG(d
->flags
, CF_HUMAN
)) {
3653 char *fen
= pgn_game_to_fen(gp
, d
->b
);
3655 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
3659 do_history_mode_finalize(d
);
3662 void do_history_toggle()
3664 struct userdata_s
*d
= gp
->data
;
3666 // FIXME Resuming from previous history could append to a RAV.
3667 if (gp
->hindex
!= pgn_history_total(gp
->hp
)) {
3669 construct_message(NULL
, _ ("What would you like to do?"), 0, 1,
3670 NULL
, NULL
, NULL
, do_history_mode_confirm
, 0, 0,
3671 _("The current move is not the final move of this round. Press \"%ls\" to resume a game from the current move and discard future moves or any other key to cancel."),
3676 if (TEST_FLAG(gp
->flags
, GF_GAMEOVER
))
3680 if (gp
->side
!= gp
->turn
) {
3681 d
->play_mode
= PLAY_EH
;
3684 d
->play_mode
= PLAY_HE
;
3688 do_history_mode_finalize(d
);
3691 void do_history_annotate()
3695 if (n
&& gp
->hp
[n
- 1]->move
)
3700 do_annotate_move(gp
->hp
[n
]);
3703 void do_history_help()
3705 wchar_t *buf
= build_help(history_keys
);
3707 construct_message(_("History Mode Keys (* = can take a repeat count)"),
3708 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
,
3709 buf
, do_more_help
, 0, 1, "%ls", buf
);
3712 void do_history_find(int which
)
3714 struct input_data_s
*in
;
3717 if (pgn_history_total(gp
->hp
) < 2)
3720 in
= Calloc(1, sizeof(struct input_data_s
));
3721 p
= Malloc(sizeof(int));
3724 in
->efunc
= do_find_move_exp
;
3726 if (!*moveexp
|| which
== 0) {
3727 construct_input(_("Find Move Text Expression"), NULL
, 1, 0, NULL
, NULL
, NULL
,
3728 0, in
, INPUT_HIST_MOVE_EXP
, -1);
3732 do_find_move_exp_finalize(0, which
);
3735 void do_history_find_new()
3740 void do_history_find_prev()
3742 do_history_find(-1);
3745 void do_history_find_next()
3750 void do_history_rav(int which
)
3752 struct userdata_s
*d
= gp
->data
;
3754 rav_next_prev(gp
, d
->b
, which
);
3757 void do_history_rav_next()
3762 void do_history_rav_prev()
3767 void do_history_jump()
3769 struct input_data_s
*in
;
3771 if (pgn_history_total(gp
->hp
) < 2)
3775 in
= Calloc(1, sizeof(struct input_data_s
));
3776 in
->efunc
= do_move_jump
;
3778 construct_input(_("Jump to Move Number"), NULL
, 1, 1, NULL
,
3779 NULL
, NULL
, 0, in
, -1, 0);
3783 do_move_jump_finalize(keycount
);
3786 static void free_userdata_once(GAME g
)
3788 struct userdata_s
*d
= g
->data
;
3796 if (d
->engine
->enginebuf
) {
3799 for (n
= 0; d
->engine
->enginebuf
[n
]; n
++)
3800 free(d
->engine
->enginebuf
[n
]);
3802 free(d
->engine
->enginebuf
);
3805 if (d
->engine
->queue
) {
3808 for (q
= d
->engine
->queue
; *q
; q
++)
3811 free(d
->engine
->queue
);
3829 static void free_userdata()
3833 for (i
= 0; i
< gtotal
; i
++) {
3834 free_userdata_once(game
[i
]);
3835 game
[i
]->data
= NULL
;
3839 void update_loading_window(int n
)
3844 loadingw
= newwin(3, COLS
/ 2, CALCPOSY(3), CALCPOSX(COLS
/ 2));
3845 loadingp
= new_panel(loadingw
);
3846 wbkgd(loadingw
, CP_MESSAGE_WINDOW
);
3849 wmove(loadingw
, 0, 0);
3850 wclrtobot(loadingw
);
3851 wattron(loadingw
, CP_MESSAGE_BORDER
);
3852 box(loadingw
, ACS_VLINE
, ACS_HLINE
);
3853 wattroff(loadingw
, CP_MESSAGE_BORDER
);
3854 mvwprintw(loadingw
, 1, CENTER_INT((COLS
/ 2), 11 +
3855 strlen(itoa(gtotal
, buf
))),
3856 _("Loading... %i%% (%i games)"), n
, gtotal
);
3861 static void init_userdata_once(GAME g
, int n
)
3863 struct userdata_s
*d
= NULL
;
3865 d
= Calloc(1, sizeof(struct userdata_s
));
3867 d
->c_row
= 2, d
->c_col
= 5;
3868 SET_FLAG(d
->flags
, CF_NEW
);
3871 if (pgn_board_init_fen(g
, d
->b
, NULL
) != E_PGN_OK
)
3872 pgn_board_init(d
->b
);
3875 void init_userdata()
3879 for (i
= 0; i
< gtotal
; i
++)
3880 init_userdata_once(game
[i
], i
);
3883 void fix_marks(int *start
, int *end
)
3887 *start
= (*start
< 0) ? 0 : *start
;
3888 *end
= (*end
< 0) ? 0 : *end
;
3890 if (*start
> *end
) {
3896 *end
= (*end
> gtotal
) ? gtotal
: *end
;
3899 void do_new_game_finalize(GAME g
)
3901 struct userdata_s
*d
= g
->data
;
3903 d
->mode
= MODE_PLAY
;
3904 update_status_notify(g
, NULL
);
3907 void do_new_game_from_scratch(WIN
*win
)
3909 wchar_t str
[] = { win
->c
, 0 };
3911 if (wcscmp (str
, yes_wchar
))
3918 add_custom_tags(&gp
->tag
);
3921 do_new_game_finalize(gp
);
3929 add_custom_tags(&gp
->tag
);
3930 init_userdata_once(gp
, gindex
);
3931 do_new_game_finalize(gp
);
3935 void do_game_delete_finalize(int n
)
3937 struct userdata_s
*d
;
3939 delete_game((!n
) ? gindex
: -1);
3941 if (d
->mode
!= MODE_EDIT
)
3942 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3945 void do_game_delete_confirm(WIN
*win
)
3948 wchar_t str
[] = { win
->c
, 0 };
3950 if (wcscmp (str
, yes_wchar
)) {
3955 n
= (int *)win
->data
;
3956 do_game_delete_finalize(*n
);
3960 void do_game_delete()
3964 struct userdata_s
*d
;
3968 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
3974 for (i
= n
= 0; i
< gtotal
; i
++) {
3977 if (TEST_FLAG(d
->flags
, CF_DELETE
))
3982 tmp
= _("Delete the current game?");
3985 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
3989 tmp
= _("Delete all games marked for deletion?");
3992 if (config
.deleteprompt
) {
3993 p
= Malloc(sizeof(int));
3995 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, p
,
3996 do_game_delete_confirm
, 0, 0, tmp
);
4000 do_game_delete_finalize(n
);
4003 void do_find_game_exp_finalize(int which
)
4005 struct userdata_s
*d
= gp
->data
;
4008 if ((n
= find_game_exp(gameexp
, (which
== -1) ? 0 : 1,
4009 (keycount
) ? keycount
: 1)) == -1) {
4010 update_status_notify(gp
, "%s", _("No matches found"));
4017 if (pgn_history_total(gp
->hp
))
4018 d
->mode
= MODE_HISTORY
;
4020 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4023 void do_find_game_exp(WIN
*win
)
4025 struct input_data_s
*in
= win
->data
;
4030 strncpy(gameexp
, in
->str
, sizeof(gameexp
));
4031 gameexp
[sizeof(gameexp
)-1] = 0;
4036 do_find_game_exp_finalize(c
);
4044 void do_game_jump_finalize(int n
)
4046 struct userdata_s
*d
;
4048 if (--n
> gtotal
- 1 || n
< 0)
4054 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4055 update_status_notify(gp
, NULL
);
4058 void do_game_jump(WIN
*win
)
4060 struct input_data_s
*in
= win
->data
;
4062 if (!in
->str
|| !isinteger(in
->str
)) {
4070 do_game_jump_finalize(atoi(in
->str
));
4075 void do_load_file(WIN
*win
)
4077 struct input_data_s
*in
= win
->data
;
4078 char *tmp
= in
->str
;
4079 struct userdata_s
*d
;
4080 PGN_FILE
*pgn
= NULL
;
4088 if ((tmp
= pathfix(tmp
)) == NULL
)
4091 n
= pgn_open(tmp
, "r", &pgn
);
4093 if (n
== E_PGN_ERR
) {
4094 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
, strerror(errno
));
4097 else if (n
== E_PGN_INVALID
) {
4098 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
, _("Not a regular file"));
4104 if (pgn_parse(pgn
) == E_PGN_ERR
) {
4105 del_panel(loadingp
);
4113 del_panel(loadingp
);
4118 strncpy(loadfile
, tmp
, sizeof(loadfile
));
4119 loadfile
[sizeof(loadfile
)-1] = 0;
4123 if (pgn_history_total(gp
->hp
))
4124 d
->mode
= MODE_HISTORY
;
4126 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4128 fm_loaded_file
= TRUE
;
4139 void do_game_save(WIN
*win
)
4141 struct input_data_s
*in
= win
->data
;
4144 char *tmp
= in
->str
;
4145 char tfile
[FILENAME_MAX
];
4148 struct userdata_s
*d
;
4150 if (!tmp
|| (tmp
= pathfix(tmp
)) == NULL
)
4153 if (pgn_is_compressed(tmp
) == E_PGN_ERR
) {
4154 p
= tmp
+ strlen(tmp
) - 1;
4156 if (*p
!= 'n' || *(p
-1) != 'g' || *(p
-2) != 'p' ||
4158 snprintf(tfile
, sizeof(tfile
), "%s.pgn", tmp
);
4164 * When in edit mode, update the FEN tag.
4167 for (i
= 0; i
< gtotal
; i
++) {
4170 if (d
->mode
== MODE_EDIT
) {
4171 char *fen
= pgn_game_to_fen(game
[i
], d
->b
);
4173 pgn_tag_add(&game
[i
]->tag
, "FEN", fen
);
4181 if (d
->mode
== MODE_EDIT
) {
4182 char *fen
= pgn_game_to_fen(game
[n
], d
->b
);
4184 pgn_tag_add(&game
[n
]->tag
, "FEN", fen
);
4199 void do_get_game_save_input(int n
)
4201 struct input_data_s
*in
= Calloc(1, sizeof(struct input_data_s
));
4202 int *p
= Malloc(sizeof(int));
4204 in
->efunc
= do_game_save
;
4208 construct_input(_("Save Game Filename"), loadfile
, 1, 1, _("Type TAB for file browser"),
4209 file_browser
, NULL
, '\t', in
, INPUT_HIST_FILE
, -1);
4212 void do_game_save_multi_confirm(WIN
*win
)
4215 wchar_t str
[] = { win
->c
, 0 };
4217 if (!wcscmp (str
, current_wchar
))
4219 else if (!wcscmp (str
, all_wchar
))
4222 update_status_notify(gp
, "%s", _("Save game aborted."));
4226 do_get_game_save_input(i
);
4229 void do_global_about()
4231 cmessage(_("ABOUT"), ANY_KEY_STR
,
4232 _("%s\nUsing %s with %i colors and %i color pairs\n%s"),
4233 PACKAGE_STRING
, curses_version(), COLORS
, COLOR_PAIRS
,
4237 void global_game_next_prev(int which
)
4239 struct userdata_s
*d
;
4241 game_next_prev(gp
, (which
== 1) ? 1 : 0,
4242 (keycount
) ? keycount
: 1);
4247 markend
= markstart
+ delete_count
;
4251 markend
= markstart
- delete_count
+ 1;
4252 delete_count
= -1; // to fix gindex in the other direction
4255 fix_marks(&markstart
, &markend
);
4256 do_global_toggle_delete();
4259 if (d
->mode
== MODE_HISTORY
)
4260 pgn_board_update(gp
, d
->b
, gp
->hindex
);
4261 else if (d
->mode
== MODE_PLAY
)
4262 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4265 void do_global_next_game()
4267 global_game_next_prev(1);
4270 void do_global_prev_game()
4272 global_game_next_prev(0);
4275 void global_find(int which
)
4277 struct input_data_s
*in
;
4283 in
= Calloc(1, sizeof(struct input_data_s
));
4284 p
= Malloc(sizeof(int));
4287 in
->efunc
= do_find_game_exp
;
4289 if (!*gameexp
|| which
== 0) {
4290 construct_input(_("Find Game by Tag Expression"), NULL
, 1, 0,
4291 _("[name expression:]value expression"), NULL
, NULL
, 0, in
,
4292 INPUT_HIST_GAME_EXP
, -1);
4296 do_find_game_exp_finalize(which
);
4299 void do_global_find_new()
4304 void do_global_find_next()
4309 void do_global_find_prev()
4314 void do_global_game_jump()
4320 struct input_data_s
*in
;
4322 in
= Calloc(1, sizeof(struct input_data_s
));
4323 in
->efunc
= do_game_jump
;
4324 construct_input(_("Jump to Game Number"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
,
4329 do_game_jump_finalize(keycount
);
4332 void do_global_toggle_delete()
4341 if (keycount
&& delete_count
== 0) {
4343 delete_count
= keycount
;
4344 update_status_notify(gp
, "%s (delete)", status
.notify
);
4348 if (markstart
>= 0 && markend
>= 0) {
4349 for (i
= markstart
; i
< markend
; i
++) {
4350 if (toggle_delete_flag(i
)) {
4355 gindex
= (delete_count
< 0) ? markstart
: i
- 1;
4358 if (toggle_delete_flag(gindex
))
4362 markstart
= markend
= -1;
4364 update_status_window(gp
);
4367 void do_global_delete_game()
4372 void do_global_tag_edit()
4374 struct userdata_s
*d
= gp
->data
;
4376 edit_tags(gp
, d
->b
, 1);
4379 void do_global_tag_view()
4381 struct userdata_s
*d
= gp
->data
;
4383 edit_tags(gp
, d
->b
, 0);
4386 void do_global_resume_game()
4388 struct input_data_s
*in
;
4390 in
= Calloc(1, sizeof(struct input_data_s
));
4391 in
->efunc
= do_load_file
;
4392 construct_input(_("Load Filename"), NULL
, 1, 1, _("Type TAB for file browser"), file_browser
,
4393 NULL
, '\t', in
, INPUT_HIST_FILE
, -1);
4396 void do_global_save_game()
4399 construct_message(NULL
, _("What would you like to do?"), 0, 1,
4400 NULL
, NULL
, NULL
, do_game_save_multi_confirm
, 0, 0,
4401 _("There is more than one game loaded. Press \"%ls\" to save the current game, \"%ls\" to save all games or any other key to cancel."),
4402 current_wchar
, all_wchar
);
4406 do_get_game_save_input(-1);
4409 void do_global_new_game()
4414 void do_global_copy_game()
4418 struct userdata_s
*d
;
4420 do_global_new_game();
4422 n
= pgn_tag_total(game
[g
]->tag
);
4424 for (i
= 0; i
< n
; i
++)
4425 pgn_tag_add(&gp
->tag
, game
[g
]->tag
[i
]->name
,
4426 game
[g
]->tag
[i
]->value
);
4428 pgn_board_init_fen (gp
, d
->b
, NULL
);
4429 n
= pgn_history_total(game
[g
]->history
);
4432 for (i
= 0; i
< n
; i
++) {
4434 char *move
= strdup (game
[g
]->history
[i
]->move
);
4436 if (pgn_parse_move(gp
, d
->b
, &move
, &frfr
) != E_PGN_OK
) {
4438 SET_FLAG(gp
->flags
, GF_PERROR
);
4442 pgn_history_add(gp
, d
->b
, move
);
4445 pgn_switch_turn(gp
);
4448 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4451 void do_global_new_all()
4453 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
4454 do_new_game_from_scratch
, 0, 0, "%s", _("Really start a new game from scratch?"));
4457 void do_quit(WIN
*win
)
4459 wchar_t str
[] = { win
->c
, 0 };
4461 if (wcscmp (str
, yes_wchar
))
4467 void do_global_quit()
4469 if (config
.exitdialogbox
)
4470 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
4471 do_quit
, 0, 0, _("Want to Quit?"));
4476 void do_global_toggle_engine_window()
4479 enginew
= newwin(LINES
, COLS
, 0, 0);
4480 enginep
= new_panel(enginew
);
4481 window_draw_title(enginew
, _("Engine IO Window"), COLS
, CP_MESSAGE_TITLE
,
4483 hide_panel(enginep
);
4486 if (panel_hidden(enginep
)) {
4487 update_engine_window(gp
);
4491 hide_panel(enginep
);
4495 void do_global_toggle_board_details()
4500 void do_global_toggle_strict_castling()
4502 do_toggle_strict_castling();
4505 // Global and other keys.
4506 static int globalkeys()
4508 struct userdata_s
*d
= gp
->data
;
4512 * These cannot be modified and other game mode keys cannot conflict with
4517 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
4518 markend
= markstart
= 0;
4522 update_status_notify(gp
, NULL
);
4525 if (config
.validmoves
)
4526 pgn_reset_valid_moves(d
->b
);
4533 keycount
= keycount
* 10 + i
;
4537 update_status_notify(gp
, _("Repeat %i"), keycount
);
4540 if (d
->mode
== MODE_HISTORY
)
4544 d
->c_row
+= keycount
;
4553 if (d
->mode
== MODE_HISTORY
)
4557 d
->c_row
-= keycount
;
4558 update_status_notify(gp
, NULL
);
4568 if (d
->mode
== MODE_HISTORY
)
4572 d
->c_col
-= keycount
;
4581 if (d
->mode
== MODE_HISTORY
)
4585 d
->c_col
+= keycount
;
4597 for (i
= 0; global_keys
[i
]; i
++) {
4598 if (input_c
== global_keys
[i
]->c
&& global_keys
[i
]->f
) {
4599 (*global_keys
[i
]->f
)();
4610 static void perl_error(const char *fmt
, ...)
4616 vasprintf(&buf
, fmt
, ap
);
4619 message(ERROR_STR
, ANY_KEY_STR
, "%s", buf
);
4623 static void do_perl_finalize(WIN
*win
)
4625 struct input_data_s
*in
= win
->data
;
4627 struct userdata_s
*d
= g
->data
;
4629 char *result
= NULL
;
4633 asprintf(&filename
, "%s/perl.pl", config
.datadir
);
4638 if (perl_init_file(filename
, perl_error
))
4641 arg
= pgn_game_to_fen(g
, d
->b
);
4643 if (perl_call_sub(trim(in
->str
), arg
, &result
))
4646 d
->perlfen
= pgn_game_to_fen(g
, d
->b
);
4647 d
->perlflags
= g
->flags
;
4649 if (pgn_board_init_fen(g
, d
->b
, result
) != E_PGN_OK
) {
4650 message(ERROR_STR
, ANY_KEY_STR
, "%s", _("FEN parse error."));
4651 pgn_board_init_fen(g
, d
->b
, d
->perlfen
);
4652 g
->flags
= d
->perlflags
;
4658 SET_FLAG(d
->flags
, CF_PERL
);
4659 n
= pgn_tag_find(g
->tag
, "FEN");
4662 d
->oldfen
= strdup(g
->tag
[n
]->value
);
4664 pgn_tag_add(&g
->tag
, "FEN", result
);
4665 update_status_notify(g
, "%s", ANY_KEY_STR
);
4676 void do_global_perl()
4678 struct input_data_s
*in
;
4680 in
= Calloc(1, sizeof(struct input_data_s
));
4682 in
->efunc
= do_perl_finalize
;
4683 construct_input(_("PERL Subroutine Filter"), NULL
, 1, 0, NULL
, NULL
, NULL
, 0, in
, INPUT_HIST_PERL
, -1);
4688 * A macro may contain a key that belongs to another macro so macro_match will
4689 * need to be updated to the new index of the matching macro.
4691 static void find_macro(struct userdata_s
*d
)
4696 * Macros can't contain macros when in a window.
4702 for (i
= 0; macros
[i
]; i
++) {
4703 if ((macros
[i
]->mode
== -1 || macros
[i
]->mode
== d
->mode
) &&
4704 input_c
== macros
[i
]->c
) {
4705 input_c
= macros
[i
]->keys
[macros
[i
]->n
++];
4707 if (!macro_depth_n
&& macro_match
> -1) {
4708 macro_depth
= realloc(macro_depth
, (macro_depth_n
+ 1) * sizeof(int));
4709 macro_depth
[macro_depth_n
++] = macro_match
;
4712 macro_depth
= realloc(macro_depth
, (macro_depth_n
+ 1) * sizeof(int));
4713 macro_depth
[macro_depth_n
++] = i
;
4721 * Resets the position in each macro to the first key.
4723 static void reset_macros()
4726 struct userdata_s
*d
= gp
->data
;
4729 if (macro_depth_n
> 0) {
4731 macro_match
= macro_depth
[macro_depth_n
];
4733 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
4736 input_c
= macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
4741 for (i
= 0; macros
[i
]; i
++)
4752 struct userdata_s
*d
;
4755 gindex
= gtotal
- 1;
4759 if (pgn_history_total(gp
->hp
))
4760 d
->mode
= MODE_HISTORY
;
4762 d
->mode
= MODE_PLAY
;
4763 d
->play_mode
= PLAY_HE
;
4766 if (d
->mode
== MODE_HISTORY
)
4767 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4769 update_status_notify(gp
, "%s", _("Type F1 for help"));
4773 wtimeout(boardw
, WINDOW_TIMEOUT
);
4777 char fdbuf
[8192] = {0};
4779 struct timeval tv
= {0, 0};
4787 for (i
= 0; i
< gtotal
; i
++) {
4790 if (d
->engine
&& d
->engine
->pid
!= -1) {
4791 if (d
->engine
->fd
[ENGINE_IN_FD
] > 2) {
4792 if (d
->engine
->fd
[ENGINE_IN_FD
] > n
)
4793 n
= d
->engine
->fd
[ENGINE_IN_FD
];
4795 FD_SET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
);
4798 if (d
->engine
->fd
[ENGINE_OUT_FD
] > 2) {
4799 if (d
->engine
->fd
[ENGINE_OUT_FD
] > n
)
4800 n
= d
->engine
->fd
[ENGINE_OUT_FD
];
4802 FD_SET(d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
);
4808 if ((n
= select(n
+ 1, &rfds
, &wfds
, NULL
, &tv
)) > 0) {
4809 for (i
= 0; i
< gtotal
; i
++) {
4812 if (d
->engine
&& d
->engine
->pid
!= -1) {
4813 if (FD_ISSET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
)) {
4814 len
= read(d
->engine
->fd
[ENGINE_IN_FD
], fdbuf
,
4818 if (d
->engine
->iobuf
)
4819 d
->engine
->iobuf
= Realloc(d
->engine
->iobuf
, d
->engine
->len
+ len
+ 1);
4821 d
->engine
->iobuf
= Calloc(1, len
+ 1);
4823 memcpy(&(d
->engine
->iobuf
[d
->engine
->len
]), &fdbuf
, len
);
4824 d
->engine
->len
+= len
;
4825 d
->engine
->iobuf
[d
->engine
->len
] = 0;
4828 * The fdbuf is full or no newline
4829 * was found. So we'll append the next
4830 * read() to this games buffer.
4832 if (d
->engine
->iobuf
[d
->engine
->len
- 1] != '\n')
4835 parse_engine_output(game
[i
], d
->engine
->iobuf
);
4836 free(d
->engine
->iobuf
);
4837 d
->engine
->iobuf
= NULL
;
4840 else if (len
== -1) {
4841 if (errno
!= EAGAIN
) {
4842 cmessage(ERROR_STR
, ANY_KEY_STR
, "Engine read(): %s",
4844 waitpid(d
->engine
->pid
, &n
, 0);
4852 if (FD_ISSET(d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
)) {
4853 if (d
->engine
->queue
)
4854 send_engine_command(game
[i
]);
4861 cmessage(ERROR_STR
, ANY_KEY_STR
, "select(): %s", strerror(errno
));
4870 * This is needed to detect terminal resizing.
4873 if (LINES
!= LINES_OLD
|| COLS
!= COLS_OLD
) {
4880 * Finds the top level window in the window stack so we know what
4881 * window the wget_wch()'ed key belongs to.
4884 for (i
= 0; wins
[i
]; i
++);
4887 wtimeout(wp
, WINDOW_TIMEOUT
);
4896 if (macros
&& macro_match
>= 0) {
4897 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
4900 input_c
= macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
4905 if (wget_wch(wp
, &input_c
) == ERR
|| input_c
== KEY_RESIZE
)
4916 * Run the function associated with the window. When the
4917 * function returns 0 win->efunc is ran (if not NULL) with
4918 * win as the one and only parameter. Then the window is
4921 * The exit function may create another window which will
4922 * mess up the window stack when window_destroy() is called.
4923 * So don't destory the window until the top window is
4924 * destroyable. See window_destroy().
4926 if ((*win
->func
)(win
) == 0) {
4931 window_destroy(win
);
4939 if (!keycount
&& status
.notify
)
4940 update_status_notify(gp
, NULL
);
4943 if (TEST_FLAG(d
->flags
, CF_PERL
)) {
4944 CLEAR_FLAG(d
->flags
, CF_PERL
);
4945 pgn_board_init_fen(gp
, d
->b
, d
->perlfen
);
4946 gp
->flags
= d
->perlflags
;
4948 pgn_tag_add(&gp
->tag
, "FEN", d
->oldfen
);
4950 d
->perlfen
= d
->oldfen
= NULL
;
4956 if (macros
&& macro_match
< 0)
4959 if ((n
= globalkeys()) == 1) {
4960 if (macro_match
== -1)
4970 for (i
= 0; edit_keys
[i
]; i
++) {
4971 if (input_c
== edit_keys
[i
]->c
) {
4972 (*edit_keys
[i
]->f
)();
4978 for (i
= 0; play_keys
[i
]; i
++) {
4979 if (input_c
== play_keys
[i
]->c
) {
4980 (*play_keys
[i
]->f
)();
4985 do_play_config_command();
4988 for (i
= 0; history_keys
[i
]; i
++) {
4989 if (input_c
== history_keys
[i
]->c
) {
4990 (*history_keys
[i
]->f
)();
5001 update_status_notify(gp
, NULL
);
5010 void usage(const char *pn
, int ret
)
5012 fprintf((ret
) ? stderr
: stdout
, "%s%s",
5015 "Usage: cboard [-hvCD] [-u [N]] [-p [-VtRSE] <file>]\n"
5016 " -D Dump libchess debugging info to \"libchess.debug\" (stderr)\n"),
5019 "Usage: cboard [-hvC] [-u [N]] [-p [-VtRSE] <file>]\n"),
5022 " -p Load PGN file.\n"
5023 " -V Validate a game file.\n"
5024 " -S Validate and output a PGN formatted game.\n"
5025 " -R Like -S but write a reduced PGN formatted game.\n"
5026 " -t Also write custom PGN tags from config file.\n"
5027 " -E Stop processing on file parsing error (overrides config).\n"
5028 " -C Enable strict castling (overrides config).\n"
5029 " -u Enable/disable UTF-8 pieces (1=enable, 0=disable, overrides config).\n"
5030 " -v Version information.\n"
5031 " -h This help text.\n"));
5043 free(config
.engine_cmd
);
5044 free(config
.pattern
);
5045 free(config
.ccfile
);
5046 free(config
.nagfile
);
5047 free(config
.configfile
);
5050 for (i
= 0; config
.keys
[i
]; i
++) {
5051 free(config
.keys
[i
]->str
);
5052 free(config
.keys
[i
]);
5059 for (i
= 0; config
.einit
[i
]; i
++)
5060 free(config
.einit
[i
]);
5066 pgn_tag_free(config
.tag
);
5068 free(config
.datadir
);
5070 if (curses_initialized
) {
5072 del_panel(historyp
);
5093 static void signal_save_pgn(int sig
)
5097 char *p
= config
.savedirectory
? config
.savedirectory
: config
.datadir
;
5100 asprintf(&buf
, "%s/signal-%i-%li.pgn", p
, sig
, now
);
5102 if (do_game_write(buf
, "w", 0, gtotal
)) {
5103 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", p
, strerror(errno
));
5104 update_status_notify(gp
, "%s", _("Save game failed."));
5111 void catch_signal(int which
)
5118 if (which
== SIGPIPE
&& quit
)
5121 if (which
== SIGPIPE
)
5122 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Broken pipe. Quitting."));
5132 do_window_resize ();
5133 keypad(boardw
, TRUE
);
5139 signal_save_pgn(which
);
5146 void loading_progress(long total
, long offset
)
5148 int n
= (100 * (offset
/ 100) / (total
/ 100));
5150 if (curses_initialized
)
5151 update_loading_window(n
);
5153 fprintf(stderr
, _("Loading... %i%% (%i games)%c"), n
, gtotal
, '\r');
5158 static void set_defaults()
5160 set_config_defaults();
5162 filetype
= FILE_NONE
;
5163 pgn_config_set(PGN_PROGRESS
, 1024);
5164 pgn_config_set(PGN_PROGRESS_FUNC
, loading_progress
);
5167 int main(int argc
, char *argv
[])
5171 char buf
[FILENAME_MAX
];
5172 char datadir
[FILENAME_MAX
];
5173 int ret
= EXIT_SUCCESS
;
5174 int validate_only
= 0, validate_and_write
= 0;
5175 int write_custom_tags
= 0;
5178 int utf8_pieces
= -1;
5180 setlocale (LC_ALL
, "");
5181 bindtextdomain ("cboard", LOCALE_DIR
);
5182 textdomain ("cboard");
5185 #ifndef HAVE_PROGNAME
5186 __progname
= argv
[0];
5189 if ((config
.pwd
= getpwuid(getuid())) == NULL
)
5190 err(EXIT_FAILURE
, "getpwuid()");
5192 snprintf(datadir
, sizeof(datadir
), "%s/.cboard", config
.pwd
->pw_dir
);
5193 config
.datadir
= strdup(datadir
);
5194 snprintf(buf
, sizeof(buf
), "%s/cc.data", datadir
);
5195 config
.ccfile
= strdup(buf
);
5196 snprintf(buf
, sizeof(buf
), "%s/nag.data", datadir
);
5197 config
.nagfile
= strdup(buf
);
5198 snprintf(buf
, sizeof(buf
), "%s/config", datadir
);
5199 config
.configfile
= strdup(buf
);
5201 if (stat(datadir
, &st
) == -1) {
5202 if (errno
== ENOENT
) {
5203 if (mkdir(datadir
, 0755) == -1)
5204 err(EXIT_FAILURE
, "%s", datadir
);
5207 err(EXIT_FAILURE
, "%s", datadir
);
5212 if (!S_ISDIR(st
.st_mode
))
5213 errx(EXIT_FAILURE
, "%s: %s", datadir
, _("Not a directory."));
5218 while ((opt
= getopt(argc
, argv
, "DCEVtSRhp:vu::")) != -1) {
5220 while ((opt
= getopt(argc
, argv
, "ECVtSRhp:vu::")) != -1) {
5225 unlink("libchess.debug");
5226 pgn_config_set(PGN_DEBUG
, 1);
5230 pgn_config_set(PGN_STRICT_CASTLING
, 1);
5233 write_custom_tags
= 1;
5239 pgn_config_set(PGN_REDUCED
, 1);
5241 validate_and_write
= 1;
5246 printf("%s (%s)\n%s\n", PACKAGE_STRING
, curses_version(),
5250 filetype
= FILE_PGN
;
5251 strncpy(loadfile
, optarg
, sizeof(loadfile
));
5252 loadfile
[sizeof(loadfile
)-1] = 0;
5255 utf8_pieces
= optarg
? atoi (optarg
): 1;
5259 usage(argv
[0], EXIT_SUCCESS
);
5263 if ((validate_only
|| validate_and_write
) && !*loadfile
)
5264 usage(argv
[0], EXIT_FAILURE
);
5266 if (access(config
.configfile
, R_OK
) == 0)
5267 parse_rcfile(config
.configfile
);
5270 pgn_config_set(PGN_STOP_ON_ERROR
, 1);
5272 signal(SIGPIPE
, catch_signal
);
5273 signal(SIGCONT
, catch_signal
);
5274 signal(SIGSTOP
, catch_signal
);
5275 signal(SIGINT
, catch_signal
);
5276 signal(SIGALRM
, catch_signal
);
5277 signal(SIGTERM
, catch_signal
);
5283 if (pgn_open(loadfile
, "r", &pgn
) != E_PGN_OK
)
5284 err(EXIT_FAILURE
, "%s", loadfile
);
5286 ret
= pgn_parse(pgn
);
5290 //ret = parse_fen_file(loadfile);
5292 case FILE_EPD
: // Not implemented.
5295 // No file specified. Empty game.
5296 ret
= pgn_parse(NULL
);
5298 add_custom_tags(&gp
->tag
);
5302 if (validate_only
|| validate_and_write
) {
5303 if (validate_and_write
) {
5304 if (pgn_open("-", "r", &pgn
) != E_PGN_OK
)
5305 err(EXIT_FAILURE
, "pgn_open()");
5307 for (i
= 0; i
< gtotal
; i
++) {
5308 if (write_custom_tags
)
5309 add_custom_tags(&game
[i
]->tag
);
5311 pgn_write(pgn
, game
[i
]);
5316 fm_loaded_file
= TRUE
;
5322 else if (ret
== E_PGN_ERR
)
5325 if (utf8_pieces
!= -1)
5326 config
.utf8_pieces
= utf8_pieces
;
5328 init_wchar_pieces ();
5329 yes_wchar
= str_to_wchar (_("y"));
5330 all_wchar
= str_to_wchar (_("a"));
5331 overwrite_wchar
= str_to_wchar (_("o"));
5332 resume_wchar
= str_to_wchar (_("r"));
5333 current_wchar
= str_to_wchar (_("c"));
5334 append_wchar
= str_to_wchar (_("a"));
5335 translatable_tag_names
[0] = _("Event");
5336 translatable_tag_names
[1] = _("Site");
5337 translatable_tag_names
[2] = _("Date");
5338 translatable_tag_names
[3] = _("Round");
5339 translatable_tag_names
[4] = _("White");
5340 translatable_tag_names
[5] = _("Black");
5341 translatable_tag_names
[6] = _("Result");
5345 * This fixes window resizing in an xterm.
5347 if (getenv("DISPLAY") != NULL
) {
5352 if (initscr() == NULL
)
5353 errx(EXIT_FAILURE
, "%s", _("Could not initialize curses."));
5355 curses_initialized
= 1;
5357 if (LINES
< 23 || COLS
< 74) {
5359 errx(EXIT_FAILURE
, _("Need at least an 74x23 terminal."));
5365 if (has_colors() == TRUE
&& start_color() == OK
)
5368 boardw
= newwin(BOARD_HEIGHT
, BOARD_WIDTH
, 0, COLS
- BOARD_WIDTH
);
5369 boardp
= new_panel(boardw
);
5370 historyw
= newwin(HISTORY_HEIGHT
, HISTORY_WIDTH
, LINES
- HISTORY_HEIGHT
,
5371 COLS
- HISTORY_WIDTH
);
5372 historyp
= new_panel(historyw
);
5373 statusw
= newwin(STATUS_HEIGHT
, STATUS_WIDTH
, 0, 0);
5374 statusp
= new_panel(statusw
);
5375 tagw
= newwin(TAG_HEIGHT
, TAG_WIDTH
, STATUS_HEIGHT
+ 1, 0);
5376 tagp
= new_panel(tagw
);
5377 keypad(boardw
, TRUE
);
5378 // leaveok(boardw, TRUE);
5379 leaveok(tagw
, TRUE
);
5380 leaveok(statusw
, TRUE
);
5381 leaveok(historyw
, TRUE
);
5385 draw_window_decor();
5388 free (w_pawn_wchar
);
5389 free (w_rook_wchar
);
5390 free (w_bishop_wchar
);
5391 free (w_knight_wchar
);
5392 free (w_queen_wchar
);
5393 free (w_king_wchar
);
5394 free (b_pawn_wchar
);
5395 free (b_rook_wchar
);
5396 free (b_bishop_wchar
);
5397 free (b_knight_wchar
);
5398 free (b_queen_wchar
);
5399 free (b_king_wchar
);
5401 free (enpassant_wchar
);
5404 free (overwrite_wchar
);
5405 free (resume_wchar
);
5406 free (current_wchar
);
5407 free (append_wchar
);
5408 free (status
.notify
);