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
;
136 static int COLS_OLD
, LINES_OLD
;
140 wchar_t *notify
; // The status window notification line buffer.
143 static int curses_initialized
;
145 // When in history mode a full step is to the next move of the same playing
146 // side. Half stepping is alternating sides.
149 static wchar_t *w_pawn_wchar
;
150 static wchar_t *w_rook_wchar
;
151 static wchar_t *w_bishop_wchar
;
152 static wchar_t *w_knight_wchar
;
153 static wchar_t *w_queen_wchar
;
154 static wchar_t *w_king_wchar
;
155 static wchar_t *b_pawn_wchar
;
156 static wchar_t *b_rook_wchar
;
157 static wchar_t *b_bishop_wchar
;
158 static wchar_t *b_knight_wchar
;
159 static wchar_t *b_queen_wchar
;
160 static wchar_t *b_king_wchar
;
161 static wchar_t *empty_wchar
;
162 static wchar_t *enpassant_wchar
;
164 static wchar_t *yes_wchar
;
165 static wchar_t *all_wchar
; // do_save_game_overwrite_confirm()
166 static wchar_t *overwrite_wchar
; // do_save_game_overwrite_confirm()
167 static wchar_t *resume_wchar
; // do_history_mode_confirm()
168 static wchar_t *current_wchar
; // do_game_save_multi_confirm()
169 static wchar_t *append_wchar
; // save_pgn()
171 static const char piece_chars
[] = "PpRrNnBbQqKkxx";
172 static char *translatable_tag_names
[7];
173 static const char *f_pieces
[] = {
197 static const bool cb
[8][8] = {
208 static void free_userdata_once(GAME g
);
209 static void do_more_help(WIN
*);
211 void coordofmove(GAME g
, char *move
, char *prow
, char *pcol
)
213 char l
= strlen(move
);
216 *prow
= (g
->turn
== WHITE
) ? 8 : 1;
217 *pcol
= (l
<= 4) ? 7 : 3;
223 while (!isdigit(*move
))
226 *prow
= RANKTOINT(*move
--);
227 *pcol
= FILETOINT(*move
);
230 #define INV_INT(x) (9 - x)
231 #define INV_INT0(x) (7 - x)
233 // Posición por rotación de tablero.
234 // Rotation board position.
235 void rotate_position(char *prow
, char *pcol
)
237 *prow
= INV_INT(*prow
);
238 *pcol
= INV_INT(*pcol
);
241 void update_cursor(GAME g
, int idx
)
243 int t
= pgn_history_total(g
->hp
);
244 struct userdata_s
*d
= g
->data
;
247 * If not deincremented then r and c would be the next move.
251 if (idx
> t
|| idx
< 0 || !t
|| !g
->hp
[idx
]->move
)
252 d
->c_row
= 2, d
->c_col
= 5;
254 coordofmove(g
, g
->hp
[idx
]->move
, &d
->c_row
, &d
->c_col
);
256 if (d
->mode
== MODE_HISTORY
&& d
->rotate
)
257 rotate_position(&d
->c_row
, &d
->c_col
);
260 static int init_nag()
266 if ((fp
= fopen(config
.nagfile
, "r")) == NULL
) {
267 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.nagfile
, strerror(errno
));
271 nags
= Realloc(nags
, (i
+2) * sizeof(char *));
272 nags
[i
++] = strdup(_("none"));
276 if (fscanf(fp
, " %[^\n] ", line
) == 1) {
277 nags
= Realloc(nags
, (i
+ 2) * sizeof(char *));
278 nags
[i
++] = strdup(line
);
287 void edit_nag_toggle_item(struct menu_input_s
*m
)
289 struct input_s
*in
= m
->data
;
290 struct input_data_s
*id
= in
->data
;
291 HISTORY
*h
= id
->data
;
294 if (m
->selected
== 0) {
295 for (i
= 0; i
< MAX_PGN_NAG
; i
++)
298 for (i
= 0; m
->items
[i
]; i
++)
299 m
->items
[i
]->selected
= 0;
304 for (i
= 0; i
< MAX_PGN_NAG
; i
++) {
305 if (h
->nag
[i
] == m
->selected
)
306 h
->nag
[i
] = m
->selected
= 0;
309 h
->nag
[i
] = m
->selected
;
316 void edit_nag_save(struct menu_input_s
*m
)
321 void edit_nag_help(struct menu_input_s
*m
)
323 message(_("NAG Menu Keys"), ANY_KEY_STR
, "%s",
325 " UP/DOWN - previous/next menu item\n"
326 " HOME/END - first/last menu item\n"
327 " PGDN/PGUP - next/previous page\n"
328 " a-zA-Z0-9 - jump to item\n"
329 " SPACE - toggle selected item\n"
330 " CTRL-X - quit with changes"
334 struct menu_item_s
**get_nag_items(WIN
*win
)
337 struct menu_input_s
*m
= win
->data
;
338 struct input_s
*in
= m
->data
;
339 struct input_data_s
*id
= in
->data
;
340 struct menu_item_s
**items
= m
->items
;
341 HISTORY
*h
= id
->data
;
344 for (i
= 0; items
[i
]; i
++)
348 for (i
= 0; nags
[i
]; i
++) {
349 items
= Realloc(items
, (i
+2) * sizeof(struct menu_item_s
*));
350 items
[i
] = Malloc(sizeof(struct menu_item_s
));
351 items
[i
]->name
= nags
[i
];
352 items
[i
]->value
= NULL
;
354 for (n
= 0; n
< MAX_PGN_NAG
; n
++) {
355 if (h
->nag
[n
] == i
) {
356 items
[i
]->selected
= 1;
363 items
[i
]->selected
= 0;
372 void nag_print(WIN
*win
)
374 struct menu_input_s
*m
= win
->data
;
376 mvwprintw(win
->w
, m
->print_line
, 1, "%-*s", win
->cols
- 2, m
->item
->name
);
379 void edit_nag(void *arg
)
381 struct menu_key_s
**keys
= NULL
;
388 add_menu_key(&keys
, ' ', edit_nag_toggle_item
);
389 add_menu_key(&keys
, CTRL_KEY('x'), edit_nag_save
);
390 add_menu_key(&keys
, KEY_F(1), edit_nag_help
);
391 construct_menu(0, 0, -1, -1, _("Numeric Annotation Glyphs"), 1, get_nag_items
, keys
, arg
,
396 static void *view_nag(void *arg
)
398 HISTORY
*h
= (HISTORY
*)arg
;
400 char line
[LINE_MAX
] = {0};
403 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Viewing NAG for"), h
->move
);
410 for (i
= 0; i
< MAX_PGN_NAG
; i
++) {
416 if (h
->nag
[i
] >= nag_total
)
417 strncat(line
, itoa(h
->nag
[i
], buf2
), sizeof(line
)-1);
419 strncat(line
, nags
[h
->nag
[i
]], sizeof(line
)-1);
421 strncat(line
, "\n", sizeof(line
)-1);
424 line
[strlen(line
) - 1] = 0;
425 message(buf
, ANY_KEY_STR
, "%s", line
);
429 void view_annotation(HISTORY
*h
)
431 char buf
[MAX_SAN_MOVE_LEN
+ strlen(_("Viewing Annotation for")) + 4];
432 int nag
= 0, comment
= 0;
437 if (h
->comment
&& h
->comment
[0])
443 if (!nag
&& !comment
)
446 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Viewing Annotation for"), h
->move
);
449 construct_message(buf
, (nag
) ? _("Any other key to continue") : ANY_KEY_STR
, 0, 1,
450 (nag
) ? _("Press 'n' to view NAG") : NULL
,
451 (nag
) ? view_nag
: NULL
, (nag
) ? h
: NULL
, NULL
,
452 (nag
) ? 'n' : 0, 0, "%s", h
->comment
);
454 construct_message(buf
, _("Any other key to continue"), 0, 1, _("Press 'n' to view NAG"), view_nag
, h
, NULL
,
455 'n', 0, "%s", _("No comment text for this move"));
458 int do_game_write(char *filename
, char *mode
, int start
, int end
)
461 struct userdata_s
*d
;
464 i
= pgn_open(filename
, mode
, &pgn
);
466 if (i
== E_PGN_ERR
) {
467 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", filename
, strerror(errno
));
470 else if (i
== E_PGN_INVALID
) {
471 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", filename
, _("Not a regular file"));
475 for (i
= (start
== -1) ? 0 : start
; i
< end
; i
++) {
477 pgn_write(pgn
, game
[i
]);
478 CLEAR_FLAG(d
->flags
, CF_MODIFIED
);
481 if (pgn_close(pgn
) != E_PGN_OK
)
482 message(ERROR_STR
, ANY_KEY_STR
, "%s", strerror(errno
));
485 strncpy(loadfile
, filename
, sizeof(loadfile
));
486 loadfile
[sizeof(loadfile
)-1] = 0;
499 void do_save_game_overwrite_confirm(WIN
*win
)
502 struct save_game_s
*s
= win
->data
;
503 wchar_t str
[] = { win
->c
, 0 };
505 if (!wcscmp (str
, append_wchar
))
507 else if (!wcscmp (str
, overwrite_wchar
))
512 if (do_game_write(s
->filename
, mode
, s
->start
, s
->end
))
513 update_status_notify(gp
, "%s", _("Save game failed."));
515 update_status_notify(gp
, "%s", _("Game saved."));
522 /* If the saveindex argument is -1, all games will be saved. Otherwise it's a
525 void save_pgn(char *filename
, int saveindex
)
527 char buf
[FILENAME_MAX
];
529 int end
= (saveindex
== -1) ? gtotal
: saveindex
+ 1;
530 struct save_game_s
*s
;
532 if (filename
[0] != '/' && config
.savedirectory
) {
533 if (stat(config
.savedirectory
, &st
) == -1) {
534 if (errno
== ENOENT
) {
535 if (mkdir(config
.savedirectory
, 0755) == -1) {
536 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
,
542 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
,
548 stat(config
.savedirectory
, &st
);
550 if (!S_ISDIR(st
.st_mode
)) {
551 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
, _("Not a directory."));
555 snprintf(buf
, sizeof(buf
), "%s/%s", config
.savedirectory
, filename
);
559 if (access(filename
, W_OK
) == 0) {
560 s
= Malloc(sizeof(struct save_game_s
));
561 s
->filename
= strdup(filename
);
562 s
->start
= saveindex
;
564 construct_message(NULL
, _("What would you like to do?"), 0, 1, NULL
,
565 NULL
, s
, do_save_game_overwrite_confirm
, 0, 0,
566 "%s \"%s\"\nPress \"%ls\" to append to this file, \"%ls\" to overwrite or any other key to cancel.",
567 _("File exists:"), filename
, append_wchar
,
572 if (do_game_write(filename
, "a", saveindex
, end
))
573 update_status_notify(gp
, "%s", _("Save game failed."));
575 update_status_notify(gp
, "%s", _("Game saved."));
578 static int castling_state(GAME g
, BOARD b
, int row
, int col
, int piece
, int mod
)
580 if (pgn_piece_to_int(piece
) == ROOK
&& col
== 7
582 (TEST_FLAG(g
->flags
, GF_WK_CASTLE
) || mod
) &&
583 pgn_piece_to_int(b
[7][4].icon
) == KING
&& isupper(piece
)) {
585 TOGGLE_FLAG(g
->flags
, GF_WK_CASTLE
);
588 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 0
590 (TEST_FLAG(g
->flags
, GF_WQ_CASTLE
) || mod
) &&
591 pgn_piece_to_int(b
[7][4].icon
) == KING
&& isupper(piece
)) {
593 TOGGLE_FLAG(g
->flags
, GF_WQ_CASTLE
);
596 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 7
598 (TEST_FLAG(g
->flags
, GF_BK_CASTLE
) || mod
) &&
599 pgn_piece_to_int(b
[0][4].icon
) == KING
&& islower(piece
)) {
601 TOGGLE_FLAG(g
->flags
, GF_BK_CASTLE
);
604 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 0
606 (TEST_FLAG(g
->flags
, GF_BQ_CASTLE
) || mod
) &&
607 pgn_piece_to_int(b
[0][4].icon
) == KING
&& islower(piece
)) {
609 TOGGLE_FLAG(g
->flags
, GF_BQ_CASTLE
);
612 else if (pgn_piece_to_int(piece
) == KING
&& col
== 4
614 (mod
|| (pgn_piece_to_int(b
[7][7].icon
) == ROOK
&&
615 TEST_FLAG(g
->flags
, GF_WK_CASTLE
))
617 (pgn_piece_to_int(b
[7][0].icon
) == ROOK
&&
618 TEST_FLAG(g
->flags
, GF_WQ_CASTLE
))) && isupper(piece
)) {
620 if (TEST_FLAG(g
->flags
, GF_WK_CASTLE
) ||
621 TEST_FLAG(g
->flags
, GF_WQ_CASTLE
))
622 CLEAR_FLAG(g
->flags
, GF_WK_CASTLE
|GF_WQ_CASTLE
);
624 SET_FLAG(g
->flags
, GF_WK_CASTLE
|GF_WQ_CASTLE
);
628 else if (pgn_piece_to_int(piece
) == KING
&& col
== 4
630 (mod
|| (pgn_piece_to_int(b
[0][7].icon
) == ROOK
&&
631 TEST_FLAG(g
->flags
, GF_BK_CASTLE
))
633 (pgn_piece_to_int(b
[0][0].icon
) == ROOK
&&
634 TEST_FLAG(g
->flags
, GF_BQ_CASTLE
))) && islower(piece
)) {
636 if (TEST_FLAG(g
->flags
, GF_BK_CASTLE
) ||
637 TEST_FLAG(g
->flags
, GF_BQ_CASTLE
))
638 CLEAR_FLAG(g
->flags
, GF_BK_CASTLE
|GF_BQ_CASTLE
);
640 SET_FLAG(g
->flags
, GF_BK_CASTLE
|GF_BQ_CASTLE
);
648 #define IS_ENPASSANT(c) (c == 'x') ? CP_BOARD_ENPASSANT : isupper(c) ? CP_BOARD_WHITE : CP_BOARD_BLACK
649 #define ATTRS(cp) (cp & (A_BOLD|A_STANDOUT|A_BLINK|A_DIM|A_UNDERLINE|A_INVIS|A_REVERSE))
654 w_pawn_wchar
= str_to_wchar (config
.utf8_pieces
? "♙" : "P");
655 w_rook_wchar
= str_to_wchar (config
.utf8_pieces
? "♖" : "R");
656 w_bishop_wchar
= str_to_wchar (config
.utf8_pieces
? "♗" : "B");
657 w_knight_wchar
= str_to_wchar (config
.utf8_pieces
? "♘" : "N");
658 w_queen_wchar
= str_to_wchar (config
.utf8_pieces
? "♕" : "Q");
659 w_king_wchar
= str_to_wchar (config
.utf8_pieces
? "♔" : "K");
660 b_pawn_wchar
= str_to_wchar (config
.utf8_pieces
? "♟" : "p");
661 b_rook_wchar
= str_to_wchar (config
.utf8_pieces
? "♜" : "r");
662 b_bishop_wchar
= str_to_wchar (config
.utf8_pieces
? "♝" : "b");
663 b_knight_wchar
= str_to_wchar (config
.utf8_pieces
? "♞" : "n");
664 b_queen_wchar
= str_to_wchar (config
.utf8_pieces
? "♛" : "q");
665 b_king_wchar
= str_to_wchar (config
.utf8_pieces
? "♚" : "k");
666 empty_wchar
= str_to_wchar (" ");
667 enpassant_wchar
= str_to_wchar ("x");
671 piece_to_wchar (unsigned char p
)
684 return w_bishop_wchar
;
686 return b_bishop_wchar
;
688 return w_knight_wchar
;
690 return b_knight_wchar
;
692 return w_queen_wchar
;
694 return b_queen_wchar
;
700 return enpassant_wchar
;
706 static int piece_can_attack (GAME g
, int rank
, int file
)
708 struct userdata_s
*d
= g
->data
;
709 char *m
, *frfr
= NULL
;
711 int row
, col
, p
, v
, pi
, cpi
;
714 rotate_position(&d
->c_row
, &d
->c_col
);
715 rotate_position(&d
->sp
.srow
, &d
->sp
.scol
);
718 v
= d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].valid
;
719 pi
= pgn_piece_to_int (d
->b
[RANKTOBOARD (rank
)][FILETOBOARD (file
)].icon
);
721 ? pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
)
722 : pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].icon
);
724 if (pi
== OPEN_SQUARE
|| cpi
== OPEN_SQUARE
|| !VALIDFILE (file
)
725 || !VALIDRANK (rank
)) {
727 rotate_position(&d
->c_row
, &d
->c_col
);
728 rotate_position(&d
->sp
.srow
, &d
->sp
.scol
);
735 col
= v
? d
->c_col
: d
->sp
.scol
;
736 row
= v
? d
->c_row
: d
->sp
.srow
;
743 m
= malloc(MAX_SAN_MOVE_LEN
+1);
744 m
[0] = INTTOFILE (file
);
745 m
[1] = INTTORANK (rank
);
746 m
[2] = INTTOFILE (col
);
747 m
[3] = INTTORANK (row
);
750 if (d
->sp
.icon
&& v
) {
753 memcpy (b
, d
->b
, sizeof(BOARD
));
754 p
= b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
;
755 b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
756 pgn_int_to_piece (WHITE
, OPEN_SQUARE
);
757 b
[RANKTOBOARD (row
)][FILETOBOARD (col
)].icon
= p
;
759 e
= pgn_validate_move (g
, b
, &m
, &frfr
);
764 if (e
!= E_PGN_OK
&& pgn_piece_to_int (d
->sp
.icon
) == PAWN
) {
765 int n
= (d
->sp
.srow
== 7 && rank
== 5) ? 6 :
766 (d
->sp
.srow
== 2 && rank
== 4) ? 3 : 0;
768 if (n
&& (file
== d
->c_col
-1 || file
== d
->c_col
+1)) {
769 memcpy (b
, d
->b
, sizeof(BOARD
));
770 p
= b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
;
771 b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
772 pgn_int_to_piece (WHITE
, OPEN_SQUARE
);
773 b
[RANKTOBOARD (row
)][FILETOBOARD (col
)].icon
= p
;
774 b
[RANKTOBOARD (n
)][FILETOBOARD (d
->sp
.scol
)].enpassant
= 1;
775 m
= malloc(MAX_SAN_MOVE_LEN
+1);
776 m
[0] = INTTOFILE (file
);
777 m
[1] = INTTORANK (rank
);
778 m
[2] = INTTOFILE (col
);
779 m
[3] = INTTORANK (n
);
782 SET_FLAG (g
->flags
, GF_ENPASSANT
);
783 e
= pgn_validate_move (g
, b
, &m
, &frfr
);
784 CLEAR_FLAG (g
->flags
, GF_ENPASSANT
);
795 e
= pgn_validate_move (g
, d
->b
, &m
, &frfr
);
798 if (!strcmp (m
, "O-O") || !strcmp (m
, "O-O-O"))
802 int sf
= FILETOINT (frfr
[0]), sr
= RANKTOINT (frfr
[1]);
803 int df
= FILETOINT (frfr
[2]);
805 pi
= d
->b
[RANKTOBOARD (sr
)][FILETOBOARD (sf
)].icon
;
806 pi
= pgn_piece_to_int (pi
);
807 if (pi
== PAWN
&& sf
== df
)
816 rotate_position(&d
->c_row
, &d
->c_col
);
817 rotate_position(&d
->sp
.srow
, &d
->sp
.scol
);
820 return e
== E_PGN_OK
? 1 : 0;
823 void print_piece(WINDOW
*w
, int l
, int c
, char p
)
827 for (i
= 0; i
< 13; i
+= 2) {
828 if (p
== piece_chars
[i
] || p
== piece_chars
[i
+ 1]) {
829 for (y
= 0; y
< 3; y
++)
830 mvwprintw(w
, l
+ y
, c
, "%s", f_pieces
[i
+ ff
+ y
]);
837 for (y
= 0; y
< 3; y
++)
838 mvwprintw(w
, l
+ y
, c
, f_pieces
[0]);
841 void board_prev_move_play (GAME g
)
843 struct userdata_s
*d
= g
->data
;
845 char l
= strlen(d
->pm_frfr
);
847 char q
= (l
> 4) ? 2 : 1;
849 d
->pm_row
= RANKTOINT(d
->pm_frfr
[l
- q
++]);
850 d
->pm_col
= FILETOINT(d
->pm_frfr
[l
- q
++]);
851 d
->ospm_row
= RANKTOINT(d
->pm_frfr
[l
- q
++]);
852 d
->ospm_col
= FILETOINT(d
->pm_frfr
[l
- q
]);
854 rotate_position(&d
->pm_row
, &d
->pm_col
);
855 rotate_position(&d
->ospm_row
, &d
->ospm_col
);
866 void board_prev_move_history (GAME g
)
868 struct userdata_s
*d
= g
->data
;
871 char *move
= g
->hp
[g
->hindex
- 1]->move
;
874 if (d
->mode
== MODE_PLAY
)
875 coordofmove(g
, move
, &d
->pm_row
, &d
->pm_col
);
882 d
->ospm_row
= g
->turn
== WHITE
? 8 : 1;
891 HISTORY
*h
= pgn_history_by_n(g
->hp
, g
->hindex
- 2);
893 pgn_board_init_fen(g
, ob
, h
->fen
);
897 for (f
= 0; f
< 8; f
++) {
898 for (r
= 0; r
< 8; r
++) {
899 if (ob
[f
][r
].icon
!= '.' && d
->b
[f
][r
].icon
== '.') {
900 d
->ospm_row
= INV_INT0(f
) + 1;
907 if (d
->mode
== MODE_PLAY
)
908 rotate_position(&d
->pm_row
, &d
->pm_col
);
909 rotate_position(&d
->ospm_row
, &d
->ospm_col
);
922 static int is_the_square (int brow
, int bcol
, int row
, int col
,
926 row
== ROWTOMATRIX(prow
) && col
== COLTOMATRIX(pcol
))
928 brow
+ 1 == INV_INT(prow
) && bcol
+ 1 == pcol
))
934 static int is_prev_move (struct userdata_s
*d
, int brow
, int bcol
,
937 if (is_the_square (brow
, bcol
, row
, col
, d
->pm_row
, d
->pm_col
))
943 void update_board_window(GAME g
)
946 int bcol
= 0, brow
= 0;
947 int l
= config
.coordsyleft
;
948 int maxy
= BOARD_HEIGHT
, maxx
= BOARD_WIDTH
;
949 int ncols
= 0, offset
= 1;
950 int rowr
= (MEGA_BOARD
) ? 6 : (BIG_BOARD
) ? 4 : 2;
951 int colr
= (MEGA_BOARD
) ? 12 : (BIG_BOARD
) ? 8 : 4;
952 unsigned coords_y
= 8, cxgc
= 0;
954 struct userdata_s
*d
= g
->data
;
956 if (config
.bprevmove
&& d
->mode
!= MODE_EDIT
)
957 if (!d
->pm_undo
&& d
->mode
== MODE_PLAY
)
958 board_prev_move_play(g
);
960 board_prev_move_history(g
);
968 if (d
->mode
!= MODE_PLAY
&& d
->mode
!= MODE_EDIT
)
969 update_cursor(g
, g
->hindex
);
986 for (row
= 0; row
< maxy
; row
++) {
1000 for (col
= 0; col
< maxx
; col
++) {
1002 chtype attrs
= 0, old_attrs
= 0;
1007 if (row
== 0 || row
== maxy
- 2) {
1009 mvwaddch(boardw
, row
, col
+ l
,
1011 ? ACS_LLCORNER
| CP_BOARD_GRAPHICS
1012 : ACS_ULCORNER
| CP_BOARD_GRAPHICS
));
1013 else if (col
== maxx
- 2)
1014 mvwaddch(boardw
, row
, col
+ l
,
1016 ? ACS_LRCORNER
| CP_BOARD_GRAPHICS
1017 : ACS_URCORNER
| CP_BOARD_GRAPHICS
));
1018 else if (!(col
% colr
))
1019 mvwaddch(boardw
, row
, col
+ l
,
1021 ? ACS_BTEE
| CP_BOARD_GRAPHICS
1022 : ACS_TTEE
| CP_BOARD_GRAPHICS
));
1024 if (col
!= maxx
- 1)
1025 mvwaddch(boardw
, row
, col
+ l
,
1026 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
1032 if ((row
% 2) && col
== maxx
- 1 &&
1033 (coords_y
> 0 && coords_y
< 9)) {
1034 wattron(boardw
, CP_BOARD_COORDS
);
1036 (BIG_BOARD
) ? row
* ((MEGA_BOARD
) ? 3 : 2)
1037 : row
, (l
) ? 0 : col
, "%d",
1038 (d
->rotate
) ? coords_y
++ : coords_y
--);
1039 wattroff(boardw
, CP_BOARD_COORDS
);
1043 if ((col
== 0 || col
== maxx
- 2) && row
!= maxy
- 1) {
1045 mvwaddch(boardw
, row
, col
+ l
,
1046 LINE_GRAPHIC((col
) ?
1047 ACS_RTEE
| CP_BOARD_GRAPHICS
:
1048 ACS_LTEE
| CP_BOARD_GRAPHICS
));
1050 mvwaddch(boardw
, row
, col
+ l
,
1051 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
1056 if ((row
% rowr
) && !(col
% colr
) && row
!= maxy
- 1) {
1057 mvwaddch(boardw
, row
, col
+ l
,
1058 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
1062 if (!(col
% colr
) && row
!= maxy
- 1) {
1063 mvwaddch(boardw
, row
, col
+ l
,
1064 LINE_GRAPHIC(ACS_PLUS
| CP_BOARD_GRAPHICS
));
1071 attrwhich
= (cb
[brow
][bcol
]) ? WHITE
: BLACK
;
1078 if (((ncols
% 2) && !(offset
% 2))
1079 || (!(ncols
% 2) && (offset
% 2)))
1085 if (BIG_BOARD
&& d
->rotate
) {
1086 brow
= INV_INT0(brow
);
1087 bcol
= INV_INT0(bcol
);
1091 p
= d
->b
[brow
][bcol
].icon
;
1093 p
= d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].icon
;
1095 int pi
= pgn_piece_to_int(p
);
1097 if (config
.details
&&
1098 ((!BIG_BOARD
&& d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].enpassant
)
1099 || (BIG_BOARD
&& d
->b
[brow
][bcol
].enpassant
))) {
1101 attrs
= mix_cp(CP_BOARD_ENPASSANT
,
1102 (attrwhich
== WHITE
) ? CP_BOARD_WHITE
: CP_BOARD_BLACK
,
1103 ATTRS(CP_BOARD_ENPASSANT
), A_FG_B_BG
);
1106 if (config
.showattacks
&& config
.details
1107 && piece_can_attack (g
,
1108 BIG_BOARD
? INV_INT0(brow
)+1 : brow
,
1109 BIG_BOARD
? bcol
+1 : bcol
)) {
1110 attrs
= CP_BOARD_ATTACK
;
1115 if (config
.validmoves
&&
1116 ((!BIG_BOARD
&& d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].valid
)
1117 || (BIG_BOARD
&& d
->b
[brow
][bcol
].valid
))) {
1121 if (attrwhich
== WHITE
)
1122 attrs
= mix_cp(CP_BOARD_MOVES_WHITE
,
1124 ATTRS(CP_BOARD_MOVES_WHITE
),
1127 attrs
= mix_cp(CP_BOARD_MOVES_BLACK
,
1129 ATTRS(CP_BOARD_MOVES_BLACK
),
1132 else if (p
!= 'x' && !can_attack
)
1133 attrs
= (attrwhich
== WHITE
) ? CP_BOARD_WHITE
: CP_BOARD_BLACK
;
1135 if (BIG_BOARD
&& d
->rotate
) {
1136 brow
= INV_INT0(brow
);
1137 bcol
= INV_INT0(bcol
);
1140 if (is_the_square (brow
, bcol
, row
, col
, d
->c_row
,
1142 attrs
= mix_cp(CP_BOARD_CURSOR
, IS_ENPASSANT(p
),
1143 ATTRS(CP_BOARD_CURSOR
), B_FG_A_BG
);
1146 else if (is_the_square (brow
, bcol
, row
, col
, d
->sp
.srow
,
1148 attrs
= mix_cp(CP_BOARD_SELECTED
, IS_ENPASSANT(p
),
1149 ATTRS(CP_BOARD_SELECTED
), B_FG_A_BG
);
1152 else if ((is_prev_move (d
, brow
, bcol
, row
, col
) && !valid
)
1153 || (is_the_square (brow
, bcol
, row
, col
, d
->ospm_row
,
1155 attrs
= mix_cp(CP_BOARD_PREVMOVE
, IS_ENPASSANT(p
),
1156 ATTRS(CP_BOARD_PREVMOVE
), B_FG_A_BG
);
1160 if (row
== maxy
- 1)
1164 int n
= is_prev_move (d
, brow
, bcol
, row
, col
);
1165 chtype a
= n
&& !valid
1166 ? CP_BOARD_PREVMOVE
: attrwhich
== WHITE
? valid
? CP_BOARD_MOVES_WHITE
: CP_BOARD_WHITE
: valid
? CP_BOARD_MOVES_BLACK
: CP_BOARD_BLACK
;
1167 attrs
= mix_cp(CP_BOARD_ATTACK
, a
,
1168 ATTRS (CP_BOARD_ATTACK
), A_FG_B_BG
);
1173 wmove(boardw
, row
, col
+ ((MEGA_BOARD
) ? 5 : 3) + l
);
1175 mvwaddch(boardw
, row
, col
+ l
, ' ' | attrs
);
1177 if (row
== maxy
- 1 && cxgc
< 8) {
1178 waddch(boardw
, "abcdefgh"[(BIG_BOARD
) ? bcol
: bcol
- 1] | CP_BOARD_COORDS
);
1182 if (old_attrs
== -1) {
1189 if (pi
!= OPEN_SQUARE
&& p
!= 'x' && !can_attack
) {
1190 if (attrwhich
== WHITE
) {
1192 attrs
= CP_BOARD_W_W
;
1194 attrs
= CP_BOARD_W_B
;
1198 attrs
= CP_BOARD_B_W
;
1200 attrs
= CP_BOARD_B_B
;
1206 if (config
.details
&& !can_attack
1207 && castling_state(g
, d
->b
,
1208 (d
->rotate
) ? INV_INT(brow
+ 1) - 1: brow
,
1209 (d
->rotate
) ? INV_INT(bcol
+ 1) - 1: bcol
, p
, 0))
1210 attrs
= mix_cp(CP_BOARD_CASTLING
, attrs
,
1211 ATTRS(CP_BOARD_CASTLING
),
1215 if (config
.details
&& !can_attack
1216 && castling_state(g
, d
->b
, RANKTOBOARD (brow
),
1217 FILETOBOARD (bcol
), p
, 0)) {
1218 attrs
= mix_cp(CP_BOARD_CASTLING
, attrs
,
1219 ATTRS(CP_BOARD_CASTLING
),
1225 // FIXME: Reimpresión de piezas(+4).
1227 wattron (boardw
, attrs
);
1229 for (i
= 0; i
< 5; i
++)
1230 mvwprintw(boardw
, i
+ brow
* 6 + 1,
1233 if (pi
!= OPEN_SQUARE
)
1234 print_piece(boardw
, brow
* 6 + 2,
1235 bcol
* 12 + 3 + l
, p
);
1238 print_piece(boardw
, brow
* 4 + 1,
1240 (pi
!= OPEN_SQUARE
) ? p
: 0);
1243 wattroff (boardw
, attrs
);
1248 wattron (boardw
, attrs
);
1249 waddwstr (boardw
, piece_to_wchar (pi
!= OPEN_SQUARE
? p
: 0));
1250 wattroff (boardw
, attrs
);
1257 col
+= (MEGA_BOARD
) ? 10 : 6;
1259 waddch(boardw
, ' ' | attrs
);
1277 if (col
!= maxx
- 1)
1278 mvwaddch(boardw
, row
, col
+ l
,
1279 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
1299 void invalid_move(int n
, int e
, const char *m
)
1301 if (curses_initialized
)
1302 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s \"%s\" (round #%i)", (e
== E_PGN_AMBIGUOUS
)
1303 ? _("Ambiguous move") : _("Invalid move"), m
, n
);
1305 warnx("%s: %s \"%s\" (round #%i)", loadfile
, (e
== E_PGN_AMBIGUOUS
)
1306 ? _("Ambiguous move") : _("Invalid move"), m
, n
);
1309 void gameover(GAME g
)
1311 struct userdata_s
*d
= g
->data
;
1313 SET_FLAG(g
->flags
, GF_GAMEOVER
);
1314 d
->mode
= MODE_HISTORY
;
1318 static void update_clock(GAME g
, struct itimerval it
)
1320 struct userdata_s
*d
= g
->data
;
1322 if (TEST_FLAG(d
->flags
, CF_CLOCK
) && g
->turn
== WHITE
) {
1323 d
->wclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1324 d
->wclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1326 if (d
->wclock
.elapsed
.tv_usec
> 1000000 - 1) {
1327 d
->wclock
.elapsed
.tv_sec
+= d
->wclock
.elapsed
.tv_usec
/ 1000000;
1328 d
->wclock
.elapsed
.tv_usec
= d
->wclock
.elapsed
.tv_usec
% 1000000;
1331 if (d
->wclock
.tc
[d
->wclock
.tcn
][1] &&
1332 d
->wclock
.elapsed
.tv_sec
>= d
->wclock
.tc
[d
->wclock
.tcn
][1]) {
1333 pgn_tag_add(&g
->tag
, "Result", "0-1");
1337 else if (TEST_FLAG(d
->flags
, CF_CLOCK
) && g
->turn
== BLACK
) {
1338 d
->bclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1339 d
->bclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1341 if (d
->bclock
.elapsed
.tv_usec
> 1000000 - 1) {
1342 d
->bclock
.elapsed
.tv_sec
+= d
->bclock
.elapsed
.tv_usec
/ 1000000;
1343 d
->bclock
.elapsed
.tv_usec
= d
->bclock
.elapsed
.tv_usec
% 1000000;
1346 if (d
->bclock
.tc
[d
->bclock
.tcn
][1] &&
1347 d
->bclock
.elapsed
.tv_sec
>= d
->bclock
.tc
[d
->bclock
.tcn
][1]) {
1348 pgn_tag_add(&g
->tag
, "Result", "1-0");
1353 d
->elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1354 d
->elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1356 if (d
->elapsed
.tv_usec
> 1000000 - 1) {
1357 d
->elapsed
.tv_sec
+= d
->elapsed
.tv_usec
/ 1000000;
1358 d
->elapsed
.tv_usec
= d
->elapsed
.tv_usec
% 1000000;
1362 static void update_time_control(GAME g
)
1364 struct userdata_s
*d
= g
->data
;
1365 struct clock_s
*clk
= (g
->turn
== WHITE
) ? &d
->wclock
: &d
->bclock
;
1368 clk
->tc
[clk
->tcn
][1] += clk
->incr
;
1370 if (!clk
->tc
[clk
->tcn
][1])
1375 if (!clk
->tc
[clk
->tcn
][0] || clk
->move
>= clk
->tc
[clk
->tcn
][0]) {
1377 clk
->tc
[clk
->tcn
+ 1][1] += abs(clk
->elapsed
.tv_sec
- clk
->tc
[clk
->tcn
][1]);
1378 memset(&clk
->elapsed
, 0, sizeof(clk
->elapsed
));
1383 void update_history_window(GAME g
)
1385 char buf
[HISTORY_WIDTH
- 1];
1388 int t
= pgn_history_total(g
->hp
);
1390 n
= (g
->hindex
+ 1) / 2;
1393 total
= (t
+ 1) / 2;
1398 snprintf(buf
, sizeof(buf
), "%u %s %u%s", n
, _("of"), total
,
1399 (movestep
== 1) ? _(" (ply)") : "");
1401 strncpy(buf
, _("not available"), sizeof(buf
)-1);
1403 buf
[sizeof(buf
)-1] = 0;
1404 mvwprintw(historyw
, 2, 1, "%*s %-*s", 10, _("Move:"),
1405 HISTORY_WIDTH
- 14, buf
);
1407 h
= pgn_history_by_n(g
->hp
, g
->hindex
);
1408 snprintf(buf
, sizeof(buf
), "%s",
1409 (h
&& h
->move
) ? h
->move
1410 : (LINES
< 24) ? _("empty") : _("not available"));
1413 if (h
&& ((h
->comment
) || h
->nag
[0])) {
1414 strncat(buf
, _(" (Annotated"), sizeof(buf
)-1);
1419 strncat(buf
, (n
) ? ",+" : " (+", sizeof(buf
)-1);
1424 strncat(buf
, (n
) ? ",-" : " (-", sizeof(buf
)-1);
1429 strncat(buf
, ")", sizeof(buf
)-1);
1431 mvwprintw(historyw
, 3, ((LINES
< 24) ? 17 : 1), "%s %-*s",
1432 (LINES
< 24) ? _("Next:") :_("Next move:"),
1433 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1435 h
= pgn_history_by_n(g
->hp
, g
->hindex
- 1);
1436 snprintf(buf
, sizeof(buf
), "%s",
1437 (h
&& h
->move
) ? h
->move
1438 : (LINES
< 24) ? _("empty") : _("not available"));
1441 if (h
&& ((h
->comment
) || h
->nag
[0])) {
1442 strncat(buf
, _(" (Annotated"), sizeof(buf
)-1);
1447 strncat(buf
, (n
) ? ",+" : " (+", sizeof(buf
)-1);
1452 strncat(buf
, (n
) ? ",-" : " (-", sizeof(buf
)-1);
1457 strncat(buf
, ")", sizeof(buf
)-1);
1459 mvwprintw(historyw
, ((LINES
< 24) ? 3 : 4), 1, "%s %-*s",
1460 (LINES
< 24) ? _("Prev.:") : _("Prev move:"),
1461 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1464 void do_validate_move(char **move
)
1466 struct userdata_s
*d
= gp
->data
;
1470 if (TEST_FLAG(d
->flags
, CF_HUMAN
)) {
1471 if ((n
= pgn_parse_move(gp
, d
->b
, move
, &frfr
)) != E_PGN_OK
) {
1472 invalid_move(d
->n
+ 1, n
, *move
);
1476 strcpy(d
->pm_frfr
, frfr
);
1477 update_time_control(gp
);
1478 pgn_history_add(gp
, d
->b
, *move
);
1479 pgn_switch_turn(gp
);
1482 if ((n
= pgn_validate_move(gp
, d
->b
, move
, &frfr
)) != E_PGN_OK
) {
1483 invalid_move(d
->n
+ 1, n
, *move
);
1487 add_engine_command(gp
, ENGINE_THINKING
, "%s\n",
1488 (config
.engine_protocol
== 1) ? frfr
: *move
);
1491 d
->sp
.srow
= d
->sp
.scol
= d
->sp
.icon
= 0;
1493 if (config
.validmoves
)
1494 pgn_reset_valid_moves(d
->b
);
1496 if (TEST_FLAG(gp
->flags
, GF_GAMEOVER
))
1497 d
->mode
= MODE_HISTORY
;
1499 SET_FLAG(d
->flags
, CF_MODIFIED
);
1503 update_history_window(gp
);
1504 update_board_window(gp
);
1508 void do_promotion_piece_finalize(WIN
*win
)
1510 char *p
, *str
= win
->data
;
1512 if (pgn_piece_to_int(win
->c
) == -1)
1515 p
= str
+ strlen(str
);
1516 *p
++ = toupper(win
->c
);
1518 do_validate_move(&str
);
1523 static void move_to_engine(GAME g
)
1525 struct userdata_s
*d
= g
->data
;
1529 if (config
.validmoves
&&
1530 !d
->b
[RANKTOBOARD(d
->sp
.row
)][FILETOBOARD(d
->sp
.col
)].valid
)
1533 str
= Malloc(MAX_SAN_MOVE_LEN
+ 1);
1534 snprintf(str
, MAX_SAN_MOVE_LEN
+ 1, "%c%i%c%i",
1535 _("abcdefgh")[d
->sp
.scol
- 1],
1536 d
->sp
.srow
, _("abcdefgh")[d
->sp
.col
- 1], d
->sp
.row
);
1538 piece
= pgn_piece_to_int(d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
);
1540 if (piece
== PAWN
&& (d
->sp
.row
== 8 || d
->sp
.row
== 1)) {
1541 construct_message(_("Select Pawn Promotion Piece"), _ ("R/N/B/Q"), 1, 1, NULL
, NULL
,
1542 str
, do_promotion_piece_finalize
, 0, 0, "%s", _("R = Rook, N = Knight, B = Bishop, Q = Queen"));
1546 do_validate_move(&str
);
1550 static char *clock_to_char(long n
)
1552 static char buf
[16];
1553 int h
= 0, m
= 0, s
= 0;
1556 m
= (n
% 3600) / 60;
1557 s
= (n
% 3600) % 60;
1558 snprintf(buf
, sizeof(buf
), "%.2i:%.2i:%.2i", h
, m
, s
);
1562 static char *timeval_to_char(struct timeval t
, long limit
)
1565 int h
= 0, m
= 0, s
= 0;
1566 int n
= limit
? abs(limit
- t
.tv_sec
) : 0;
1569 m
= (n
% 3600) / 60;
1570 s
= (n
% 3600) % 60;
1571 snprintf(buf
, sizeof(buf
), "%.2i:%.2i:%.2i", h
, m
, s
);
1575 static char *time_control_status(struct clock_s
*clk
)
1577 static char buf
[80] = {0};
1581 if (clk
->tc
[clk
->tcn
][0] && clk
->tc
[clk
->tcn
+ 1][1])
1582 snprintf(buf
, sizeof(buf
), " M%.2i/%s", abs(clk
->tc
[clk
->tcn
][0] - clk
->move
),
1583 clock_to_char(clk
->tc
[clk
->tcn
+ 1][1]));
1584 else if (!clk
->incr
)
1589 strncat(buf
, " I", sizeof(buf
)-1);
1590 strncat(buf
, itoa(clk
->incr
, buf
), sizeof(buf
)-1);
1596 void update_status_window(GAME g
)
1600 char tmp
[15] = {0}, *engine
, *mode
;
1606 struct userdata_s
*d
= g
->data
;
1610 if (!curses_initialized
)
1613 getmaxyx(statusw
, maxy
, maxx
);
1620 wchar_t *loadfilew
= loadfile
[0] ? str_etc (loadfile
, w
, 1) : str_to_wchar (_ ("not available"));
1621 mvwprintw(statusw
, y
++, 1, "%*s %-*ls", 7, _("File:"), w
, loadfilew
);
1623 snprintf(buf
, len
, "%i %s %i", gindex
+ 1, _("of"), gtotal
);
1624 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Game:"), w
, buf
);
1629 if (config
.details
) {
1634 if (TEST_FLAG(d
->flags
, CF_DELETE
)) {
1642 if (TEST_FLAG(g
->flags
, GF_PERROR
)) {
1650 if (TEST_FLAG(d
->flags
, CF_MODIFIED
)) {
1658 pgn_config_get(PGN_STRICT_CASTLING
, &n
);
1668 if (TEST_FLAG(d
->flags
, CF_PERL
)) {
1678 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Flags:"), w
, (tmp
[0]) ? tmp
: "-");
1682 mode
= _("move history");
1691 mode
= _("(empty value)");
1695 snprintf(buf
, len
- 1, "%*s %s", 7, _("Mode:"), mode
);
1697 if (d
->mode
== MODE_PLAY
) {
1698 if (TEST_FLAG(d
->flags
, CF_HUMAN
))
1699 strncat(buf
, _(" (human/human)"), len
- 1);
1700 else if (TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
))
1701 strncat(buf
, _(" (engine/engine)"), len
- 1);
1703 strncat(buf
, (d
->play_mode
== PLAY_EH
) ?
1704 _(" (engine/human)") : _(" (human/engine)"), len
- 1);
1708 mvwprintw(statusw
, y
++, 1, "%-*s", len
, buf
);
1712 switch (d
->engine
->status
) {
1713 case ENGINE_THINKING
:
1714 engine
= _("pondering...");
1717 engine
= _("ready");
1719 case ENGINE_INITIALIZING
:
1720 engine
= _("initializing...");
1722 case ENGINE_OFFLINE
:
1723 engine
= _("offline");
1726 engine
= _("(empty value)");
1731 engine
= _("offline");
1733 mvwprintw(statusw
, y
, 1, "%*s %-*s", 7, _("Engine:"), w
, " ");
1734 wattron(statusw
, CP_STATUS_ENGINE
);
1735 mvwaddstr(statusw
, y
++, 9, engine
);
1736 wattroff(statusw
, CP_STATUS_ENGINE
);
1738 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Turn:"), w
,
1739 (g
->turn
== WHITE
) ? _("white") : _("black"));
1741 strncpy(tmp
, _("white"), sizeof(tmp
)-1);
1742 tmp
[0] = toupper(tmp
[0]);
1743 snprintf(t
, sizeof(t
), "%s%s",
1744 timeval_to_char(d
->wclock
.elapsed
, d
->wclock
.tc
[d
->wclock
.tcn
][1]),
1745 time_control_status(&d
->wclock
));
1746 mvwprintw(statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1748 strncpy(tmp
, _("black"), sizeof(tmp
)-1);
1749 tmp
[0] = toupper(tmp
[0]);
1750 snprintf(t
, sizeof(t
), "%s%s",
1751 timeval_to_char(d
->bclock
.elapsed
, d
->bclock
.tc
[d
->bclock
.tcn
][1]),
1752 time_control_status(&d
->bclock
));
1753 mvwprintw(statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1755 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Total:"), w
,
1756 clock_to_char(d
->elapsed
.tv_sec
));
1758 // for (i = 0; i < STATUS_WIDTH; i++)
1759 // mvwprintw(stdscr, STATUS_HEIGHT, i, " ");
1762 status
.notify
= str_to_wchar(_("Type F1 for help"));
1764 wattron(stdscr
, CP_STATUS_NOTIFY
);
1765 for (i
= (config
.boardleft
) ? BOARD_WIDTH
: 0;
1766 i
< ((config
.boardleft
) ? COLS
: STATUS_WIDTH
); i
++)
1767 mvwprintw(stdscr
, STATUS_HEIGHT
, i
, " ");
1768 mvwprintw(stdscr
, STATUS_HEIGHT
, CENTERX(STATUS_WIDTH
, status
.notify
)
1769 + ((config
.boardleft
) ? BOARD_WIDTH
: 0),
1770 "%ls", status
.notify
);
1771 wattroff(stdscr
, CP_STATUS_NOTIFY
);
1774 wchar_t *translate_tag_name(const char *tag
)
1776 if (!strcmp (tag
, "Event"))
1777 return str_to_wchar (translatable_tag_names
[0]);
1778 else if (!strcmp (tag
, "Site"))
1779 return str_to_wchar (translatable_tag_names
[1]);
1780 else if (!strcmp (tag
, "Date"))
1781 return str_to_wchar (translatable_tag_names
[2]);
1782 else if (!strcmp (tag
, "Round"))
1783 return str_to_wchar (translatable_tag_names
[3]);
1784 else if (!strcmp (tag
, "White"))
1785 return str_to_wchar (translatable_tag_names
[4]);
1786 else if (!strcmp (tag
, "Black"))
1787 return str_to_wchar (translatable_tag_names
[5]);
1788 else if (!strcmp (tag
, "Result"))
1789 return str_to_wchar (translatable_tag_names
[6]);
1791 return str_to_wchar (tag
);
1794 void update_tag_window(TAG
**t
)
1799 for (i
= 0; t
[i
]; i
++) {
1800 wchar_t *namewc
= translate_tag_name(t
[i
]->name
);
1808 w
= TAG_WIDTH
- namel
- 4;
1810 for (i
= 0; t
[i
] && i
< TAG_HEIGHT
- 3; i
++) {
1811 wchar_t *namewc
= translate_tag_name(t
[i
]->name
);
1812 wchar_t *valuewc
= str_etc(t
[i
]->value
, w
, 0);
1814 mvwprintw(tagw
, (i
+ 2), 1, "%*ls: %-*ls", namel
, namewc
, w
, valuewc
);
1819 for (; i
< TAG_HEIGHT
- 3; i
++)
1820 mvwprintw(tagw
, (i
+ 2), 1, "%*s", namel
+ w
+ 2, " ");
1823 void append_enginebuf(GAME g
, char *line
)
1826 struct userdata_s
*d
= g
->data
;
1828 if (d
->engine
->enginebuf
)
1829 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++);
1831 if (i
>= LINES
- 3) {
1832 free(d
->engine
->enginebuf
[0]);
1834 for (i
= 0; d
->engine
->enginebuf
[i
+1]; i
++)
1835 d
->engine
->enginebuf
[i
] = d
->engine
->enginebuf
[i
+1];
1837 d
->engine
->enginebuf
[i
] = strdup(line
);
1840 d
->engine
->enginebuf
= Realloc(d
->engine
->enginebuf
, (i
+ 2) * sizeof(char *));
1841 d
->engine
->enginebuf
[i
++] = strdup(line
);
1842 d
->engine
->enginebuf
[i
] = NULL
;
1846 void update_engine_window(GAME g
)
1849 struct userdata_s
*d
= g
->data
;
1851 wmove(enginew
, 0, 0);
1854 if (d
->engine
&& d
->engine
->enginebuf
) {
1855 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++)
1856 mvwprintw(enginew
, i
+ 2, 1, "%s", d
->engine
->enginebuf
[i
]);
1859 window_draw_title(enginew
, _("Engine IO Window"), COLS
, CP_MESSAGE_TITLE
,
1863 void update_all(GAME g
)
1865 struct userdata_s
*d
= g
->data
;
1868 * In the middle of a macro. Don't update the screen.
1870 if (macro_match
!= -1)
1873 wmove(boardw
, ROWTOMATRIX(d
->c_row
), COLTOMATRIX(d
->c_col
));
1874 update_board_window(g
);
1875 update_status_window(g
);
1876 update_history_window(g
);
1877 update_tag_window(g
->tag
);
1878 update_engine_window(g
);
1883 static void game_next_prev(GAME g
, int n
, int count
)
1889 if (gindex
+ count
> gtotal
- 1) {
1891 gindex
= gtotal
- 1;
1899 if (gindex
- count
< 0) {
1903 gindex
= gtotal
- 1;
1912 static void delete_game(int which
)
1915 struct userdata_s
*d
;
1917 for (i
= 0; i
< gtotal
; i
++) {
1920 if (i
== w
|| TEST_FLAG(d
->flags
, CF_DELETE
)) {
1923 free_userdata_once(game
[i
]);
1926 for (n
= i
; n
+1 < gtotal
; n
++)
1927 game
[n
] = game
[n
+1];
1936 if (which
+ 1 >= gtotal
)
1937 gindex
= gtotal
- 1;
1942 gindex
= gtotal
- 1;
1945 gp
->hp
= gp
->history
;
1949 * FIXME find across multiple games.
1951 static int find_move_exp(GAME g
, regex_t r
, int which
, int count
)
1959 incr
= (which
== 0) ? -1 : 1;
1961 for (i
= g
->hindex
+ incr
- 1, found
= 0; ; i
+= incr
) {
1962 if (i
== g
->hindex
- 1)
1965 if (i
>= pgn_history_total(g
->hp
))
1968 i
= pgn_history_total(g
->hp
) - 1;
1971 ret
= regexec(&r
, g
->hp
[i
]->move
, 0, 0, 0);
1974 if (count
== ++found
) {
1979 if (ret
!= REG_NOMATCH
) {
1980 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
1981 cmessage(_("Error Matching Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
1990 static int toggle_delete_flag(int n
)
1993 struct userdata_s
*d
= game
[n
]->data
;
1995 TOGGLE_FLAG(d
->flags
, CF_DELETE
);
1998 for (i
= x
= 0; i
< gtotal
; i
++) {
2001 if (TEST_FLAG(d
->flags
, CF_DELETE
))
2006 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
2008 CLEAR_FLAG(d
->flags
, CF_DELETE
);
2015 static int find_game_exp(char *str
, int which
, int count
)
2017 char *nstr
= NULL
, *exp
= NULL
;
2021 char buf
[255] = {0}, *tmp
;
2024 int incr
= (which
== 0) ? -(1) : 1;
2026 strncpy(buf
, str
, sizeof(buf
)-1);
2029 if (strstr(tmp
, ":") != NULL
) {
2030 nstr
= strsep(&tmp
, ":");
2032 if ((ret
= regcomp(&nexp
, nstr
,
2033 REG_ICASE
|REG_EXTENDED
|REG_NOSUB
)) != 0) {
2034 regerror(ret
, &nexp
, errbuf
, sizeof(errbuf
));
2035 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
2043 while (*exp
&& isspace(*exp
))
2049 if ((ret
= regcomp(&vexp
, exp
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
2050 regerror(ret
, &vexp
, errbuf
, sizeof(errbuf
));
2051 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
2058 for (g
= gindex
+ incr
, found
= 0; ; g
+= incr
) {
2069 for (t
= 0; game
[g
]->tag
[t
]; t
++) {
2071 if (regexec(&nexp
, game
[g
]->tag
[t
]->name
, 0, 0, 0) == 0) {
2072 if (regexec(&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0) {
2073 if (count
== ++found
) {
2081 if (regexec(&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0) {
2082 if (count
== ++found
) {
2104 * Updates the notification line in the status window then refreshes the
2107 void update_status_notify(GAME g
, char *fmt
, ...)
2110 #ifdef HAVE_VASPRINTF
2116 free(status
.notify
);
2117 status
.notify
= NULL
;
2123 #ifdef HAVE_VASPRINTF
2124 vasprintf(&line
, fmt
, ap
);
2126 vsnprintf(line
, sizeof(line
), fmt
, ap
);
2130 status
.notify
= str_to_wchar(line
);
2132 #ifdef HAVE_VASPRINTF
2137 int rav_next_prev(GAME g
, BOARD b
, int n
)
2141 if ((!g
->ravlevel
&& g
->hindex
&& g
->hp
[g
->hindex
- 1]->rav
== NULL
) ||
2142 (!g
->ravlevel
&& !g
->hindex
&& g
->hp
[g
->hindex
]->rav
== NULL
) ||
2143 (g
->ravlevel
&& g
->hp
[g
->hindex
]->rav
== NULL
))
2146 g
->rav
= Realloc(g
->rav
, (g
->ravlevel
+ 1) * sizeof(RAV
));
2147 g
->rav
[g
->ravlevel
].hp
= g
->hp
;
2148 g
->rav
[g
->ravlevel
].flags
= g
->flags
;
2149 g
->rav
[g
->ravlevel
].fen
= pgn_game_to_fen(g
, b
);
2150 g
->rav
[g
->ravlevel
].hindex
= g
->hindex
;
2151 g
->hp
= (!g
->ravlevel
) ? (g
->hindex
) ? g
->hp
[g
->hindex
- 1]->rav
: g
->hp
[g
->hindex
]->rav
: g
->hp
[g
->hindex
]->rav
;
2154 pgn_board_update(g
, b
, g
->hindex
+ 1);
2158 if (g
->ravlevel
- 1 < 0)
2163 pgn_board_init_fen(g
, b
, g
->rav
[g
->ravlevel
].fen
);
2164 free(g
->rav
[g
->ravlevel
].fen
);
2165 g
->hp
= g
->rav
[g
->ravlevel
].hp
;
2166 g
->flags
= g
->rav
[g
->ravlevel
].flags
;
2167 g
->hindex
= g
->rav
[g
->ravlevel
].hindex
;
2171 static void draw_window_decor()
2173 move_panel(boardp
, 0,
2174 (config
.boardleft
) ? 0 : COLS
- BOARD_WIDTH
);
2175 move_panel(historyp
, LINES
- HISTORY_HEIGHT
,
2176 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
: 0 :
2177 (MEGA_BOARD
) ? 0 : COLS
- HISTORY_WIDTH
);
2178 move_panel(statusp
, 0,
2179 (config
.boardleft
) ? BOARD_WIDTH
: 0);
2180 move_panel(tagp
, STATUS_HEIGHT
+ 1,
2181 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
:
2184 wbkgd(boardw
, CP_BOARD_WINDOW
);
2185 wbkgd(statusw
, CP_STATUS_WINDOW
);
2186 window_draw_title(statusw
, _("Game Status"), STATUS_WIDTH
,
2187 CP_STATUS_TITLE
, CP_STATUS_BORDER
);
2188 wbkgd(tagw
, CP_TAG_WINDOW
);
2189 window_draw_title(tagw
, _("Roster Tags"), TAG_WIDTH
, CP_TAG_TITLE
,
2191 wbkgd(historyw
, CP_HISTORY_WINDOW
);
2192 window_draw_title(historyw
, _("Move History"), HISTORY_WIDTH
,
2193 CP_HISTORY_TITLE
, CP_HISTORY_BORDER
);
2196 void do_window_resize()
2198 if (LINES
< 23 || COLS
< 74)
2201 resizeterm(LINES
, COLS
);
2203 wresize(boardw
, BOARD_HEIGHT
, BOARD_WIDTH
);
2204 wresize(historyw
, HISTORY_HEIGHT
, HISTORY_WIDTH
);
2205 wresize(statusw
, STATUS_HEIGHT
, STATUS_WIDTH
);
2206 wresize(tagw
, TAG_HEIGHT
, TAG_WIDTH
);
2214 draw_window_decor();
2216 keypad(boardw
, TRUE
);
2222 void do_global_redraw ()
2224 do_window_resize ();
2229 memset(&clock_timer
, 0, sizeof(struct itimerval
));
2230 setitimer(ITIMER_REAL
, &clock_timer
, NULL
);
2233 void start_clock(GAME g
)
2235 struct userdata_s
*d
= g
->data
;
2237 if (clock_timer
.it_interval
.tv_usec
)
2240 memset(&d
->elapsed
, 0, sizeof(struct timeval
));
2241 clock_timer
.it_value
.tv_sec
= 0;
2242 clock_timer
.it_value
.tv_usec
= 100000;
2243 clock_timer
.it_interval
.tv_sec
= 0;
2244 clock_timer
.it_interval
.tv_usec
= 100000;
2245 setitimer(ITIMER_REAL
, &clock_timer
, NULL
);
2248 static void update_clocks()
2251 struct userdata_s
*d
;
2252 struct itimerval it
;
2255 getitimer(ITIMER_REAL
, &it
);
2257 for (i
= 0; i
< gtotal
; i
++) {
2260 if (d
&& d
->mode
== MODE_PLAY
) {
2261 if (d
->paused
== 1 || TEST_FLAG(d
->flags
, CF_NEW
))
2263 else if (d
->paused
== -1) {
2264 if (game
[i
]->side
== game
[i
]->turn
) {
2270 update_clock(game
[i
], it
);
2278 update_status_window(gp
);
2284 #define SKIP_SPACE(str) { while (isspace(*str)) str++; }
2286 static int parse_clock_time(char **str
)
2341 static int parse_clock_input(struct clock_s
*clk
, char *str
, int *incr
)
2363 memset(clk
, 0, sizeof(struct clock_s
));
2367 if (strncasecmp(p
, "SD", 2) == 0) {
2373 n
= parse_clock_time(&p
);
2387 /* Sudden death without a previous time control. */
2393 n
= parse_clock_time(&p
);
2399 message(ERROR_STR
, ANY_KEY_STR
, "%s (%i)", _("Maximum number of time controls reached"), MAX_TC
);
2404 clk
->tc
[tc
++][1] = n
;
2419 clk
->tc
[clk
->tcn
][1] = (n
<= clk
->elapsed
.tv_sec
) ? clk
->elapsed
.tv_sec
+ n
: n
;
2427 n
= parse_clock_time(&p
);
2447 static int parse_which_clock(struct clock_s
*clk
, char *str
)
2452 memcpy(&tmp
, clk
, sizeof(struct clock_s
));
2454 if (parse_clock_input(&tmp
, str
, &incr
)) {
2455 cmessage(ERROR_STR
, ANY_KEY_STR
, _("Invalid clock specification"));
2459 memcpy(clk
, &tmp
, sizeof(struct clock_s
));
2460 clk
->tc
[clk
->tcn
][1] += incr
;
2464 void do_clock_input_finalize(WIN
*win
)
2466 struct userdata_s
*d
= gp
->data
;
2467 struct input_data_s
*in
= win
->data
;
2477 if (tolower(*p
) == 'w') {
2480 if (parse_which_clock(&d
->wclock
, p
))
2483 else if (tolower(*p
) == 'b') {
2486 if (parse_which_clock(&d
->bclock
, p
))
2490 if (parse_which_clock(&d
->wclock
, p
))
2493 if (parse_which_clock(&d
->bclock
, p
))
2497 if (!d
->wclock
.tc
[0][1] && !d
->bclock
.tc
[0][1])
2498 CLEAR_FLAG(d
->flags
, CF_CLOCK
);
2500 SET_FLAG(d
->flags
, CF_CLOCK
);
2507 void do_engine_command_finalize(WIN
*win
)
2509 struct userdata_s
*d
= gp
->data
;
2510 struct input_data_s
*in
= win
->data
;
2521 x
= d
->engine
->status
;
2522 send_to_engine(gp
, -1, "%s\n", in
->str
);
2523 d
->engine
->status
= x
;
2530 void do_board_details()
2532 config
.details
= (config
.details
) ? 0 : 1;
2535 void do_toggle_strict_castling()
2539 pgn_config_get(PGN_STRICT_CASTLING
, &n
);
2542 pgn_config_set(PGN_STRICT_CASTLING
, 1);
2544 pgn_config_set(PGN_STRICT_CASTLING
, 0);
2547 void do_play_set_clock()
2549 struct input_data_s
*in
;
2551 in
= Calloc(1, sizeof(struct input_data_s
));
2552 in
->efunc
= do_clock_input_finalize
;
2553 construct_input(_("Set Clock"), NULL
, 1, 1,
2554 _ ("Format: [W | B] [+]T[+I] | ++I | M/T [M/T [...] [SD/T]] [+I]\n" \
2555 "T = time (hms), I = increment, M = moves per, SD = sudden death\ne.g., 30m or 4m+12s or 35/90m SD/30m"),
2556 NULL
, NULL
, 0, in
, INPUT_HIST_CLOCK
, -1);
2559 void do_play_toggle_human()
2561 struct userdata_s
*d
= gp
->data
;
2563 TOGGLE_FLAG(d
->flags
, CF_HUMAN
);
2565 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && pgn_history_total(gp
->hp
)) {
2566 if (init_chess_engine(gp
))
2570 CLEAR_FLAG(d
->flags
, CF_ENGINE_LOOP
);
2573 d
->engine
->status
= ENGINE_READY
;
2576 void do_play_toggle_engine()
2578 struct userdata_s
*d
= gp
->data
;
2580 TOGGLE_FLAG(d
->flags
, CF_ENGINE_LOOP
);
2581 CLEAR_FLAG(d
->flags
, CF_HUMAN
);
2583 if (d
->engine
&& TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
)) {
2584 char *fen
= pgn_game_to_fen (gp
, d
->b
);
2586 pgn_board_update(gp
, d
->b
,
2587 pgn_history_total(gp
->hp
));
2588 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
2594 * This will send a command to the engine skipping the command queue.
2596 void do_play_send_command()
2598 struct userdata_s
*d
= gp
->data
;
2599 struct input_data_s
*in
;
2601 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
) {
2602 if (init_chess_engine(gp
))
2606 in
= Calloc(1, sizeof(struct input_data_s
));
2607 in
->efunc
= do_engine_command_finalize
;
2608 construct_input(_("Engine Command"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
, INPUT_HIST_ENGINE
, -1);
2611 void do_play_switch_turn()
2613 struct userdata_s *d = gp->data;
2615 pgn_switch_side(gp);
2616 pgn_switch_turn(gp);
2618 if (!TEST_FLAG(d->flags, CF_HUMAN))
2619 add_engine_command(gp, -1,
2620 (gp->side == WHITE) ? "white\n" : "black\n");
2622 update_status_window(gp);
2625 void do_play_toggle_eh_mode()
2627 struct userdata_s
*d
= gp
->data
;
2629 if (!TEST_FLAG(d
->flags
, CF_HUMAN
)) {
2631 pgn_switch_side(gp
, TRUE
);
2632 d
->play_mode
= (d
->play_mode
) ? PLAY_HE
: PLAY_EH
;
2633 if (gp
->side
== BLACK
)
2634 update_status_notify(gp
, _("Press 'g' to start the game"));
2636 d
->rotate
= !d
->rotate
;
2639 message(NULL
, ANY_KEY_STR
,
2640 _("You may only switch sides at the start of the \n"
2641 "game. Press ^K or ^N to begin a new game."));
2647 struct userdata_s
*d
= gp
->data
;
2649 if (!pgn_history_total(gp
->hp
))
2653 if (gp
->hindex
- keycount
< 0)
2657 gp
->hindex
-= (keycount
* 2) -1;
2659 gp
->hindex
-= keycount
* 2;
2663 if (gp
->hindex
- 2 < 0)
2673 pgn_history_free(gp
->hp
, gp
->hindex
);
2674 gp
->hindex
= pgn_history_total(gp
->hp
);
2675 pgn_board_update(gp
, d
->b
, gp
->hindex
);
2677 if (d
->engine
&& d
->engine
->status
== ENGINE_READY
) {
2678 char *fen
= pgn_game_to_fen(gp
, d
->b
);
2680 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
2682 d
->engine
->status
= ENGINE_READY
;
2685 update_history_window(gp
);
2688 pgn_switch_side(gp
, FALSE
);
2694 void do_play_toggle_pause()
2696 struct userdata_s
*d
= gp
->data
;
2698 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && gp
->turn
!=
2704 d
->paused
= (d
->paused
) ? 0 : 1;
2709 struct userdata_s
*d
= gp
->data
;
2711 if (TEST_FLAG(d
->flags
, CF_HUMAN
))
2714 if (fm_loaded_file
&& gp
->side
!= gp
->turn
) {
2715 pgn_switch_side(gp
, FALSE
);
2716 add_engine_command(gp
, ENGINE_THINKING
, "black\n");
2719 add_engine_command(gp
, ENGINE_THINKING
, "go\n");
2721 // Completa la función para que permita seguir jugando al usarla.
2722 // Complete the function to allow continue playing when using.
2723 if (gp
->side
== gp
->turn
)
2724 pgn_switch_side(gp
, FALSE
);
2729 void do_play_config_command()
2734 for (x
= 0; config
.keys
[x
]; x
++) {
2735 if (config
.keys
[x
]->c
== input_c
) {
2736 switch (config
.keys
[x
]->type
) {
2738 add_engine_command(gp
, -1, "%ls\n",
2739 config
.keys
[x
]->str
);
2745 add_engine_command(gp
, -1,
2746 "%ls %i\n", config
.keys
[x
]->str
, keycount
);
2753 for (w
= 0; w
< keycount
; w
++)
2754 add_engine_command(gp
, -1,
2755 "%ls\n", config
.keys
[x
]->str
);
2763 update_status_notify(gp
, NULL
);
2766 void do_play_cancel_selected()
2768 struct userdata_s
*d
= gp
->data
;
2770 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
2772 pgn_reset_valid_moves(d
->b
);
2773 update_status_notify(gp
, NULL
);
2776 void do_play_commit()
2778 struct userdata_s
*d
= gp
->data
;
2780 pushkey
= keycount
= 0;
2781 update_status_notify(gp
, NULL
);
2783 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) &&
2784 (!d
->engine
|| d
->engine
->status
== ENGINE_THINKING
))
2790 d
->sp
.row
= d
->c_row
;
2791 d
->sp
.col
= d
->c_col
;
2794 rotate_position(&d
->sp
.row
, &d
->sp
.col
);
2795 rotate_position(&d
->sp
.srow
, &d
->sp
.scol
);
2800 // Completa la función para que permita seguir jugando cuando se carga un
2801 // archivo pgn (con juego no terminado) que inicie con turno del lado
2803 // Complete the function to allow continue playing when loading a file
2804 // pgn (with unfinished game) you start to turn black side.
2805 if (gp
->side
!= gp
->turn
)
2806 pgn_switch_side(gp
, FALSE
);
2808 if (d
->rotate
&& d
->sp
.icon
)
2809 rotate_position(&d
->sp
.srow
, &d
->sp
.scol
);
2811 // Envia comando 'go' a Polyglot en el primer movimiento debido a que
2812 // polyglot no envia el movimiento y cboard se queda esperando.
2813 // Send command 'go' to the first movement Polyglot because cboard
2814 // waits to send polyglot movement and this does not make.
2815 if (config
.fmpolyglot
&&
2816 ((gp
->side
== WHITE
&& !gp
->hindex
) || fm_loaded_file
))
2817 add_engine_command(gp
, ENGINE_THINKING
, "go\n");
2820 fm_loaded_file
= FALSE
;
2824 void do_play_select()
2826 struct userdata_s
*d
= gp
->data
;
2828 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && (!d
->engine
||
2829 d
->engine
->status
== ENGINE_OFFLINE
)) {
2830 if (init_chess_engine(gp
))
2834 if (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
)
2838 do_play_cancel_selected ();
2841 rotate_position(&d
->c_row
, &d
->c_col
);
2843 d
->sp
.icon
= d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
;
2845 if (pgn_piece_to_int(d
->sp
.icon
) == OPEN_SQUARE
) {
2850 if (((islower(d
->sp
.icon
) && gp
->turn
!= BLACK
)
2851 || (isupper(d
->sp
.icon
) && gp
->turn
!= WHITE
))) {
2853 char *str
= Malloc (512);
2855 for (k
= play_keys
; *k
; k
++) {
2856 if ((*k
)->f
== do_play_toggle_eh_mode
)
2860 snprintf (str
, 512, _("It is not your turn to move. You may switch playing sides by pressing \"%lc\"."),
2861 *k
? (*k
)->c
: '?');
2862 message(NULL
, ANY_KEY_STR
, "%s", str
);
2867 if (pgn_history_total(gp
->hp
)) {
2868 message(NULL
, ANY_KEY_STR
, "%s", _("It is not your turn to move. You can switch sides "));
2873 if (pgn_tag_find(gp
->tag
, "FEN") != E_PGN_ERR
)
2876 add_engine_command(gp
, ENGINE_READY
, "black\n");
2877 pgn_switch_turn(gp
);
2879 if (gp
->side
!= BLACK
)
2880 pgn_switch_side(gp
);
2885 d
->sp
.srow
= d
->c_row
;
2886 d
->sp
.scol
= d
->c_col
;
2888 if (config
.validmoves
)
2889 pgn_find_valid_moves(gp
, d
->b
, d
->sp
.scol
, d
->sp
.srow
);
2892 rotate_position(&d
->c_row
, &d
->c_col
);
2893 rotate_position(&d
->sp
.srow
, &d
->sp
.scol
);
2896 CLEAR_FLAG(d
->flags
, CF_NEW
);
2900 static wchar_t *build_help(struct key_s
**keys
)
2902 int i
, nlen
= 1, len
, t
, n
;
2903 wchar_t *buf
= NULL
, *wc
= NULL
;
2909 for (i
= len
= t
= 0; keys
[i
]; i
++) {
2914 if (wcslen(keys
[i
]->key
) > nlen
) {
2915 nlen
= wcslen(keys
[i
]->key
);
2925 if (wcslen(keys
[i
]->d
) > len
)
2926 len
= wcslen(keys
[i
]->d
);
2934 buf
= Malloc((t
+1)*sizeof(wchar_t));
2937 for (i
= 0; keys
[i
]; i
++) {
2942 n
= wcslen(keys
[i
]->key
);
2952 wcsncat(buf
, keys
[i
]->key
, t
-1);
2953 p
= buf
+ wcslen(buf
);
2964 wcsncat(buf
, keys
[i
]->d
, t
-1);
2967 wc
= str_to_wchar ("*");
2968 wcsncat(buf
, wc
, t
-1);
2972 wc
= str_to_wchar ("\n");
2975 p
= buf
+ wcslen(buf
);
2981 void do_global_help ()
2983 wchar_t *buf
= build_help(global_keys
);
2985 construct_message(_("Global Game Keys (* = can take a repeat count)"),
2986 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
, buf
, do_more_help
,
2990 void do_main_help(WIN
*win
)
3010 static void do_more_help(WIN
*win
)
3012 if (win
->c
== KEY_F(1) || win
->c
== CTRL_KEY('g'))
3013 construct_message(_("Command Key Index"),
3014 _ ("p/h/e/g or any other key to quit"), 0, 0,
3015 NULL
, NULL
, NULL
, do_main_help
, 0, 0, "%s",
3017 "p - play mode keys\n"
3018 "h - history mode keys\n"
3019 "e - board edit mode keys\n"
3020 "g - global game keys"
3026 wchar_t *buf
= build_help(play_keys
);
3028 construct_message(_("Play Mode Keys (* = can take a repeat count)"),
3029 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
,
3030 buf
, do_more_help
, 0, 1, "%ls", buf
);
3033 void do_play_history_mode()
3035 struct userdata_s
*d
= gp
->data
;
3037 if (!pgn_history_total(gp
->hp
) ||
3038 (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
))
3041 d
->mode
= MODE_HISTORY
;
3042 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3045 void do_play_edit_mode()
3047 struct userdata_s
*d
= gp
->data
;
3049 if (pgn_history_total(gp
->hp
))
3052 pgn_board_init_fen(gp
, d
->b
, NULL
);
3054 d
->mode
= MODE_EDIT
;
3057 void do_edit_insert_finalize(WIN
*win
)
3059 struct userdata_s
*d
= win
->data
;
3061 if (pgn_piece_to_int(win
->c
) == -1)
3064 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
= win
->c
;
3067 void do_edit_select()
3069 struct userdata_s
*d
= gp
->data
;
3074 d
->sp
.icon
= d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
;
3076 if (pgn_piece_to_int(d
->sp
.icon
) == OPEN_SQUARE
) {
3081 d
->sp
.srow
= d
->c_row
;
3082 d
->sp
.scol
= d
->c_col
;
3085 void do_edit_commit()
3088 struct userdata_s
*d
= gp
->data
;
3090 pushkey
= keycount
= 0;
3091 update_status_notify(gp
, NULL
);
3096 d
->sp
.row
= d
->c_row
;
3097 d
->sp
.col
= d
->c_col
;
3098 p
= d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
;
3099 d
->b
[RANKTOBOARD(d
->sp
.row
)][FILETOBOARD(d
->sp
.col
)].icon
= p
;
3100 d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
=
3101 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3102 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3105 void do_edit_delete()
3107 struct userdata_s
*d
= gp
->data
;
3110 d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
=
3111 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3113 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
=
3114 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3116 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3119 void do_edit_cancel_selected()
3121 struct userdata_s
*d
= gp
->data
;
3123 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3125 update_status_notify(gp
, NULL
);
3128 void do_edit_switch_turn()
3130 pgn_switch_turn(gp
);
3133 void do_edit_toggle_castle()
3135 struct userdata_s
*d
= gp
->data
;
3137 castling_state(gp
, d
->b
, RANKTOBOARD(d
->c_row
),
3138 FILETOBOARD(d
->c_col
),
3139 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
, 1);
3142 void do_edit_insert()
3144 struct userdata_s
*d
= gp
->data
;
3146 construct_message(_("Insert Piece"), _("P=pawn, R=rook, N=knight, B=bishop, "), 0, 0, NULL
, NULL
,
3147 d
->b
, do_edit_insert_finalize
, 0, 0, "%s", _("Type the piece letter to insert. Lowercase "));
3150 void do_edit_enpassant()
3152 struct userdata_s
*d
= gp
->data
;
3154 if (d
->c_row
== 6 || d
->c_row
== 3) {
3155 pgn_reset_enpassant(d
->b
);
3156 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].enpassant
= 1;
3162 wchar_t *buf
= build_help(edit_keys
);
3164 construct_message(_("Edit Mode Keys (* = can take a repeat count)"),
3165 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
, buf
, do_more_help
,
3171 struct userdata_s
*d
= gp
->data
;
3172 char *fen
= pgn_game_to_fen(gp
, d
->b
);
3175 pgn_tag_add(&gp
->tag
, "FEN", fen
);
3177 pgn_tag_add(&gp
->tag
, "SetUp", "1");
3178 pgn_tag_sort(gp
->tag
);
3179 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3180 d
->mode
= MODE_PLAY
;
3183 void really_do_annotate_finalize(struct input_data_s
*in
,
3184 struct userdata_s
*d
)
3186 HISTORY
*h
= in
->data
;
3196 len
= strlen(in
->str
);
3197 h
->comment
= Realloc(h
->comment
, len
+1);
3198 strncpy(h
->comment
, in
->str
, len
);
3199 h
->comment
[len
] = 0;
3204 SET_FLAG(d
->flags
, CF_MODIFIED
);
3207 void do_annotate_finalize(WIN
*win
)
3209 struct userdata_s
*d
= gp
->data
;
3210 struct input_data_s
*in
= win
->data
;
3212 really_do_annotate_finalize(in
, d
);
3215 void do_find_move_exp_finalize(int init
, int which
)
3218 struct userdata_s
*d
= gp
->data
;
3219 static int firstrun
;
3224 if (init
|| !firstrun
) {
3228 if ((ret
= regcomp(&r
, moveexp
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
3229 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
3230 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
3237 if ((n
= find_move_exp(gp
, r
,
3238 (which
== -1) ? 0 : 1, (keycount
) ? keycount
: 1)) == -1)
3242 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3245 void do_find_move_exp(WIN
*win
)
3247 struct input_data_s
*in
= win
->data
;
3252 strncpy(moveexp
, in
->str
, sizeof(moveexp
)-1);
3253 moveexp
[sizeof(moveexp
)-1] = 0;
3254 do_find_move_exp_finalize(1, which
);
3262 void do_move_jump_finalize(int n
)
3264 struct userdata_s
*d
= gp
->data
;
3266 if (n
< 0 || n
> (pgn_history_total(gp
->hp
) / 2))
3270 update_status_notify(gp
, NULL
);
3271 gp
->hindex
= (n
) ? n
* 2 - 1 : n
* 2;
3272 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3275 void do_move_jump(WIN
*win
)
3277 struct input_data_s
*in
= win
->data
;
3279 if (!in
->str
|| !isinteger(in
->str
)) {
3287 do_move_jump_finalize(atoi(in
->str
));
3292 struct history_menu_s
{
3300 void free_history_menu_data(struct history_menu_s
**h
)
3307 for (i
= 0; h
[i
]; i
++) {
3315 void get_history_data(HISTORY
**hp
, struct history_menu_s
***menu
, int m
,
3319 int t
= pgn_history_total(hp
);
3320 char buf
[MAX_SAN_MOVE_LEN
+ 4];
3322 struct history_menu_s
**hmenu
= *menu
;
3325 for (n
= 0; hmenu
[n
]; n
++);
3329 for (i
= 0; i
< t
; i
++) {
3330 hmenu
= Realloc(hmenu
, (n
+ 2) * sizeof(struct history_menu_s
*));
3331 hmenu
[n
] = Malloc(sizeof(struct history_menu_s
));
3332 snprintf(buf
, sizeof(buf
), "%c%s%s", (turn
== WHITE
) ? 'W' : 'B',
3333 hp
[i
]->move
, (hp
[i
]->comment
|| hp
[i
]->nag
[0]) ? " !" : "");
3334 hmenu
[n
]->line
= strdup(buf
);
3335 hmenu
[n
]->hindex
= i
;
3336 hmenu
[n
]->indent
= 0;
3337 hmenu
[n
]->ravlevel
= depth
;
3338 hmenu
[n
]->move
= (n
&& depth
> hmenu
[n
-1]->ravlevel
) ? m
++ : m
;
3345 get_history_data(hp
[i
]->rav
, &hmenu
, m
, turn
);
3346 for (n
= 0; hmenu
[n
]; n
++);
3354 turn
= (turn
== WHITE
) ? BLACK
: WHITE
;
3360 void history_draw_update(struct menu_input_s
*m
)
3363 struct userdata_s
*d
= g
->data
;
3365 g
->hindex
= m
->selected
+ 1;
3366 update_cursor(g
, m
->selected
);
3367 pgn_board_update(g
, d
->b
, m
->selected
+ 1);
3370 struct menu_item_s
**get_history_items(WIN
*win
)
3372 struct menu_input_s
*m
= win
->data
;
3374 struct userdata_s
*d
= g
->data
;
3375 struct history_menu_s
**hm
= d
->data
;
3376 struct menu_item_s
**items
= m
->items
;
3380 get_history_data(g
->history
, &hm
, 0,
3381 TEST_FLAG(g
->flags
, GF_BLACK_OPENING
));
3382 m
->selected
= g
->hindex
- 1;
3384 if (m
->selected
< 0)
3387 m
->draw_exit_func
= history_draw_update
;
3393 for (i
= 0; items
[i
]; i
++)
3400 for (i
= 0; hm
[i
]; i
++) {
3401 items
= Realloc(items
, (i
+2) * sizeof(struct menu_item_s
*));
3402 items
[i
] = Malloc(sizeof(struct menu_item_s
));
3403 items
[i
]->name
= hm
[i
]->line
;
3404 items
[i
]->value
= NULL
;
3405 items
[i
]->selected
= 0;
3416 void history_menu_quit(struct menu_input_s
*m
)
3421 void history_menu_exit(WIN
*win
)
3424 struct userdata_s
*d
= g
->data
;
3425 struct history_menu_s
**hm
= d
->data
;
3431 for (i
= 0; hm
[i
]; i
++) {
3441 void history_menu_next(struct menu_input_s
*m
)
3444 struct userdata_s
*d
= g
->data
;
3445 struct history_menu_s
**hm
= d
->data
;
3448 for (t
= 0; hm
[t
]; t
++);
3450 if (m
->selected
+ 1 == t
)
3453 n
= hm
[m
->selected
+ 1]->hindex
;
3460 void history_menu_prev(struct menu_input_s
*m
)
3463 struct userdata_s
*d
= g
->data
;
3464 struct history_menu_s
**hm
= d
->data
;
3467 for (t
= 0; hm
[t
]; t
++);
3469 if (m
->selected
- 1 < 0)
3472 n
= hm
[m
->selected
- 1]->hindex
;
3478 void history_menu_help(struct menu_input_s
*m
)
3480 message(_("History Menu Help"), ANY_KEY_STR
, "%s",
3482 " UP/DOWN - previous/next menu item\n"
3483 " HOME/END - first/last menu item\n"
3484 " PGDN/PGUP - next/previous page\n"
3485 " a-zA-Z0-9 - jump to item\n"
3486 " CTRL-a - annotate the selected move\n"
3487 " ENTER - view annotation\n"
3488 " CTRL-d - toggle board details\n"
3489 " ESCAPE/M - return to move history"
3493 void do_annotate_move(HISTORY
*hp
)
3496 struct input_data_s
*in
;
3498 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Editing Annotation for"), hp
->move
);
3499 in
= Calloc(1, sizeof(struct input_data_s
));
3501 in
->efunc
= do_annotate_finalize
;
3502 construct_input(buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
3503 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
, CTRL_KEY('T'), in
, -1, -1);
3506 void history_menu_view_annotation(struct menu_input_s
*m
)
3511 view_annotation(g
->history
[m
->selected
]);
3514 void history_menu_annotate_finalize(WIN
*win
)
3516 struct input_data_s
*in
= win
->data
;
3517 GAME g
= in
->moredata
;
3518 struct userdata_s
*d
= g
->data
;
3519 struct history_menu_s
**hm
= d
->data
;
3521 really_do_annotate_finalize(in
, d
);
3522 free_history_menu_data(hm
);
3524 get_history_data(g
->history
, &hm
, 0, TEST_FLAG(g
->flags
, GF_BLACK_OPENING
));
3526 pushkey
= REFRESH_MENU
;
3529 void history_menu_annotate(struct menu_input_s
*m
)
3533 struct input_data_s
*in
;
3534 HISTORY
*hp
= g
->history
[m
->selected
]; // FIXME RAV
3536 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Editing Annotation for"), hp
->move
);
3537 in
= Calloc(1, sizeof(struct input_data_s
));
3539 in
->moredata
= m
->data
;
3540 in
->efunc
= history_menu_annotate_finalize
;
3541 construct_input(buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
3542 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
, CTRL_KEY('T'), in
, -1, -1);
3545 void history_menu_details(struct menu_input_s
*m
)
3551 void history_menu_print(WIN
*win
)
3553 struct menu_input_s
*m
= win
->data
;
3555 struct userdata_s
*d
= g
->data
;
3556 struct history_menu_s
**hm
= d
->data
;
3557 struct history_menu_s
*h
= hm
[m
->top
];
3559 char *p
= m
->item
->name
;
3560 int line
= m
->print_line
- 2;
3562 * Solaris 5.9 doesn't have wattr_get() or any function that requires an
3569 for (total
= 0; hm
[total
]; total
++);
3570 wattr_get(win
->w
, &attrs
, &pair
, NULL
);
3571 wattroff(win
->w
, COLOR_PAIR(pair
));
3572 mvwaddch(win
->w
, m
->print_line
, 1,
3573 *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
));
3576 if (h
->hindex
== 0 && line
== 0)
3577 waddch(win
->w
, ACS_ULCORNER
| CP_HISTORY_MENU_LG
);
3578 else if ((!hm
[h
->hindex
+ (win
->rows
- 5) + 1] && line
== win
->rows
- 5) ||
3579 (m
->top
+ line
== total
- 1))
3580 waddch(win
->w
, ACS_LLCORNER
| CP_HISTORY_MENU_LG
);
3581 else if (hm
[m
->top
+ 1]->ravlevel
!= h
->ravlevel
|| !h
->ravlevel
)
3582 waddch(win
->w
, ACS_LTEE
| CP_HISTORY_MENU_LG
);
3584 waddch(win
->w
, ACS_VLINE
| CP_HISTORY_MENU_LG
);
3586 wattron(win
->w
, COLOR_PAIR(pair
) | attrs
);
3588 for (i
= 2; *p
; p
++, i
++)
3589 waddch(win
->w
, (*p
== '!') ? *p
| A_BOLD
: *p
);
3591 while (i
++ < win
->cols
- 2)
3592 waddch(win
->w
, ' ');
3595 void history_menu(GAME g
)
3597 struct menu_key_s
**keys
= NULL
;
3599 add_menu_key(&keys
, KEY_ESCAPE
, history_menu_quit
);
3600 add_menu_key(&keys
, 'M', history_menu_quit
);
3601 add_menu_key(&keys
, KEY_UP
, history_menu_prev
);
3602 add_menu_key(&keys
, KEY_DOWN
, history_menu_next
);
3603 add_menu_key(&keys
, KEY_F(1), history_menu_help
);
3604 add_menu_key(&keys
, CTRL_KEY('a'), history_menu_annotate
);
3605 add_menu_key(&keys
, CTRL_KEY('d'), history_menu_details
);
3606 add_menu_key(&keys
, '\n', history_menu_view_annotation
);
3607 construct_menu(MEGA_BOARD
? LINES
- HISTORY_HEIGHT_MB
: LINES
,
3608 TAG_WIDTH
, 0, config
.boardleft
? BOARD_WIDTH
: 0,
3609 _("Move History Tree"), 1, get_history_items
, keys
, g
,
3610 history_menu_print
, history_menu_exit
);
3613 void do_history_menu()
3618 void do_history_half_move_toggle()
3620 movestep
= (movestep
== 1) ? 2 : 1;
3621 update_history_window(gp
);
3624 void do_history_rotate_board()
3626 struct userdata_s
*d
= gp
->data
;
3627 d
->rotate
= !d
->rotate
;
3630 void do_history_jump_next()
3632 struct userdata_s
*d
= gp
->data
;
3634 pgn_history_next(gp
, d
->b
, (keycount
> 0) ?
3635 config
.jumpcount
* keycount
* movestep
:
3636 config
.jumpcount
* movestep
);
3639 void do_history_jump_prev()
3641 struct userdata_s
*d
= gp
->data
;
3643 pgn_history_prev(gp
, d
->b
, (keycount
) ?
3644 config
.jumpcount
* keycount
* movestep
:
3645 config
.jumpcount
* movestep
);
3648 void do_history_prev()
3650 struct userdata_s
*d
= gp
->data
;
3652 pgn_history_prev(gp
, d
->b
,
3653 (keycount
) ? keycount
* movestep
: movestep
);
3656 void do_history_next()
3658 struct userdata_s
*d
= gp
->data
;
3660 pgn_history_next(gp
, d
->b
, (keycount
) ?
3661 keycount
* movestep
: movestep
);
3664 void do_history_mode_finalize(struct userdata_s
*d
)
3667 d
->mode
= MODE_PLAY
;
3670 void do_history_mode_confirm(WIN
*win
)
3672 struct userdata_s
*d
= gp
->data
;
3673 wchar_t str
[] = { win
->c
, 0 };
3675 if (!wcscmp (str
, resume_wchar
)) {
3676 pgn_history_free(gp
->hp
, gp
->hindex
);
3677 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3682 if (pgn_history_rav_new(gp
, d
->b
,
3683 gp
->hindex
) != E_PGN_OK
)
3691 if (!TEST_FLAG(d
->flags
, CF_HUMAN
)) {
3692 char *fen
= pgn_game_to_fen(gp
, d
->b
);
3694 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
3698 do_history_mode_finalize(d
);
3701 void do_history_toggle()
3703 struct userdata_s
*d
= gp
->data
;
3705 // FIXME Resuming from previous history could append to a RAV.
3706 if (gp
->hindex
!= pgn_history_total(gp
->hp
)) {
3708 construct_message(NULL
, _ ("What would you like to do?"), 0, 1,
3709 NULL
, NULL
, NULL
, do_history_mode_confirm
, 0, 0,
3710 _("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."),
3715 if (TEST_FLAG(gp
->flags
, GF_GAMEOVER
))
3719 if (gp
->side
!= gp
->turn
) {
3720 d
->play_mode
= PLAY_EH
;
3723 d
->play_mode
= PLAY_HE
;
3727 do_history_mode_finalize(d
);
3730 void do_history_annotate()
3734 if (n
&& gp
->hp
[n
- 1]->move
)
3739 do_annotate_move(gp
->hp
[n
]);
3742 void do_history_help()
3744 wchar_t *buf
= build_help(history_keys
);
3746 construct_message(_("History Mode Keys (* = can take a repeat count)"),
3747 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
,
3748 buf
, do_more_help
, 0, 1, "%ls", buf
);
3751 void do_history_find(int which
)
3753 struct input_data_s
*in
;
3756 if (pgn_history_total(gp
->hp
) < 2)
3759 in
= Calloc(1, sizeof(struct input_data_s
));
3760 p
= Malloc(sizeof(int));
3763 in
->efunc
= do_find_move_exp
;
3765 if (!*moveexp
|| which
== 0) {
3766 construct_input(_("Find Move Text Expression"), NULL
, 1, 0, NULL
, NULL
, NULL
,
3767 0, in
, INPUT_HIST_MOVE_EXP
, -1);
3771 do_find_move_exp_finalize(0, which
);
3774 void do_history_find_new()
3779 void do_history_find_prev()
3781 do_history_find(-1);
3784 void do_history_find_next()
3789 void do_history_rav(int which
)
3791 struct userdata_s
*d
= gp
->data
;
3793 rav_next_prev(gp
, d
->b
, which
);
3796 void do_history_rav_next()
3801 void do_history_rav_prev()
3806 void do_history_jump()
3808 struct input_data_s
*in
;
3810 if (pgn_history_total(gp
->hp
) < 2)
3814 in
= Calloc(1, sizeof(struct input_data_s
));
3815 in
->efunc
= do_move_jump
;
3817 construct_input(_("Jump to Move Number"), NULL
, 1, 1, NULL
,
3818 NULL
, NULL
, 0, in
, -1, 0);
3822 do_move_jump_finalize(keycount
);
3825 static void free_userdata_once(GAME g
)
3827 struct userdata_s
*d
= g
->data
;
3835 if (d
->engine
->enginebuf
) {
3838 for (n
= 0; d
->engine
->enginebuf
[n
]; n
++)
3839 free(d
->engine
->enginebuf
[n
]);
3841 free(d
->engine
->enginebuf
);
3844 if (d
->engine
->queue
) {
3847 for (q
= d
->engine
->queue
; *q
; q
++)
3850 free(d
->engine
->queue
);
3868 static void free_userdata()
3872 for (i
= 0; i
< gtotal
; i
++) {
3873 free_userdata_once(game
[i
]);
3874 game
[i
]->data
= NULL
;
3878 void update_loading_window(int n
)
3883 loadingw
= newwin(3, COLS
/ 2, CALCPOSY(3), CALCPOSX(COLS
/ 2));
3884 loadingp
= new_panel(loadingw
);
3885 wbkgd(loadingw
, CP_MESSAGE_WINDOW
);
3888 wmove(loadingw
, 0, 0);
3889 wclrtobot(loadingw
);
3890 wattron(loadingw
, CP_MESSAGE_BORDER
);
3891 box(loadingw
, ACS_VLINE
, ACS_HLINE
);
3892 wattroff(loadingw
, CP_MESSAGE_BORDER
);
3893 mvwprintw(loadingw
, 1, CENTER_INT((COLS
/ 2), 11 +
3894 strlen(itoa(gtotal
, buf
))),
3895 _("Loading... %i%% (%i games)"), n
, gtotal
);
3900 static void init_userdata_once(GAME g
, int n
)
3902 struct userdata_s
*d
= NULL
;
3904 d
= Calloc(1, sizeof(struct userdata_s
));
3906 d
->c_row
= 2, d
->c_col
= 5;
3907 SET_FLAG(d
->flags
, CF_NEW
);
3910 if (pgn_board_init_fen(g
, d
->b
, NULL
) != E_PGN_OK
)
3911 pgn_board_init(d
->b
);
3914 void init_userdata()
3918 for (i
= 0; i
< gtotal
; i
++)
3919 init_userdata_once(game
[i
], i
);
3922 void fix_marks(int *start
, int *end
)
3926 *start
= (*start
< 0) ? 0 : *start
;
3927 *end
= (*end
< 0) ? 0 : *end
;
3929 if (*start
> *end
) {
3935 *end
= (*end
> gtotal
) ? gtotal
: *end
;
3938 void do_new_game_finalize(GAME g
)
3940 struct userdata_s
*d
= g
->data
;
3942 d
->mode
= MODE_PLAY
;
3943 update_status_notify(g
, NULL
);
3948 void do_new_game_from_scratch(WIN
*win
)
3950 wchar_t str
[] = { win
->c
, 0 };
3952 if (wcscmp (str
, yes_wchar
))
3959 add_custom_tags(&gp
->tag
);
3962 do_new_game_finalize(gp
);
3969 add_custom_tags(&gp
->tag
);
3970 init_userdata_once(gp
, gindex
);
3971 do_new_game_finalize(gp
);
3974 void do_game_delete_finalize(int n
)
3976 struct userdata_s
*d
;
3978 delete_game((!n
) ? gindex
: -1);
3980 if (d
->mode
!= MODE_EDIT
)
3981 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3984 void do_game_delete_confirm(WIN
*win
)
3987 wchar_t str
[] = { win
->c
, 0 };
3989 if (wcscmp (str
, yes_wchar
)) {
3994 n
= (int *)win
->data
;
3995 do_game_delete_finalize(*n
);
3999 void do_game_delete()
4003 struct userdata_s
*d
;
4007 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
4013 for (i
= n
= 0; i
< gtotal
; i
++) {
4016 if (TEST_FLAG(d
->flags
, CF_DELETE
))
4021 tmp
= _("Delete the current game?");
4024 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
4028 tmp
= _("Delete all games marked for deletion?");
4031 if (config
.deleteprompt
) {
4032 p
= Malloc(sizeof(int));
4034 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, p
,
4035 do_game_delete_confirm
, 0, 0, tmp
);
4039 do_game_delete_finalize(n
);
4042 void do_find_game_exp_finalize(int which
)
4044 struct userdata_s
*d
= gp
->data
;
4047 if ((n
= find_game_exp(gameexp
, (which
== -1) ? 0 : 1,
4048 (keycount
) ? keycount
: 1)) == -1) {
4049 update_status_notify(gp
, "%s", _("No matches found"));
4056 if (pgn_history_total(gp
->hp
))
4057 d
->mode
= MODE_HISTORY
;
4059 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4062 void do_find_game_exp(WIN
*win
)
4064 struct input_data_s
*in
= win
->data
;
4069 strncpy(gameexp
, in
->str
, sizeof(gameexp
));
4070 gameexp
[sizeof(gameexp
)-1] = 0;
4075 do_find_game_exp_finalize(c
);
4083 void do_game_jump_finalize(int n
)
4085 struct userdata_s
*d
;
4087 if (--n
> gtotal
- 1 || n
< 0)
4093 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4094 update_status_notify(gp
, NULL
);
4097 void do_game_jump(WIN
*win
)
4099 struct input_data_s
*in
= win
->data
;
4101 if (!in
->str
|| !isinteger(in
->str
)) {
4109 do_game_jump_finalize(atoi(in
->str
));
4114 void do_load_file(WIN
*win
)
4116 struct input_data_s
*in
= win
->data
;
4117 char *tmp
= in
->str
;
4118 struct userdata_s
*d
;
4119 PGN_FILE
*pgn
= NULL
;
4127 if ((tmp
= pathfix(tmp
)) == NULL
)
4130 n
= pgn_open(tmp
, "r", &pgn
);
4132 if (n
== E_PGN_ERR
) {
4133 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
, strerror(errno
));
4136 else if (n
== E_PGN_INVALID
) {
4137 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
, _("Not a regular file"));
4143 if (pgn_parse(pgn
) == E_PGN_ERR
) {
4144 del_panel(loadingp
);
4152 del_panel(loadingp
);
4157 strncpy(loadfile
, tmp
, sizeof(loadfile
));
4158 loadfile
[sizeof(loadfile
)-1] = 0;
4162 if (pgn_history_total(gp
->hp
))
4163 d
->mode
= MODE_HISTORY
;
4165 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4167 fm_loaded_file
= TRUE
;
4179 void do_game_save(WIN
*win
)
4181 struct input_data_s
*in
= win
->data
;
4184 char *tmp
= in
->str
;
4185 char tfile
[FILENAME_MAX
];
4188 struct userdata_s
*d
;
4190 if (!tmp
|| (tmp
= pathfix(tmp
)) == NULL
)
4193 if (pgn_is_compressed(tmp
) == E_PGN_ERR
) {
4194 p
= tmp
+ strlen(tmp
) - 1;
4196 if (*p
!= 'n' || *(p
-1) != 'g' || *(p
-2) != 'p' ||
4198 snprintf(tfile
, sizeof(tfile
), "%s.pgn", tmp
);
4204 * When in edit mode, update the FEN tag.
4207 for (i
= 0; i
< gtotal
; i
++) {
4210 if (d
->mode
== MODE_EDIT
) {
4211 char *fen
= pgn_game_to_fen(game
[i
], d
->b
);
4213 pgn_tag_add(&game
[i
]->tag
, "FEN", fen
);
4221 if (d
->mode
== MODE_EDIT
) {
4222 char *fen
= pgn_game_to_fen(game
[n
], d
->b
);
4224 pgn_tag_add(&game
[n
]->tag
, "FEN", fen
);
4239 void do_get_game_save_input(int n
)
4241 struct input_data_s
*in
= Calloc(1, sizeof(struct input_data_s
));
4242 int *p
= Malloc(sizeof(int));
4244 in
->efunc
= do_game_save
;
4248 construct_input(_("Save Game Filename"), loadfile
, 1, 1, _("Type TAB for file browser"),
4249 file_browser
, NULL
, '\t', in
, INPUT_HIST_FILE
, -1);
4252 void do_game_save_multi_confirm(WIN
*win
)
4255 wchar_t str
[] = { win
->c
, 0 };
4257 if (!wcscmp (str
, current_wchar
))
4259 else if (!wcscmp (str
, all_wchar
))
4262 update_status_notify(gp
, "%s", _("Save game aborted."));
4266 do_get_game_save_input(i
);
4269 void do_global_about()
4271 cmessage(_("ABOUT"), ANY_KEY_STR
,
4272 _("%s\nUsing %s with %i colors and %i color pairs\n%s"),
4273 PACKAGE_STRING
, curses_version(), COLORS
, COLOR_PAIRS
,
4277 void global_game_next_prev(int which
)
4279 struct userdata_s
*d
;
4281 game_next_prev(gp
, (which
== 1) ? 1 : 0,
4282 (keycount
) ? keycount
: 1);
4287 markend
= markstart
+ delete_count
;
4291 markend
= markstart
- delete_count
+ 1;
4292 delete_count
= -1; // to fix gindex in the other direction
4295 fix_marks(&markstart
, &markend
);
4296 do_global_toggle_delete();
4299 if (d
->mode
== MODE_HISTORY
)
4300 pgn_board_update(gp
, d
->b
, gp
->hindex
);
4301 else if (d
->mode
== MODE_PLAY
)
4302 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4305 void do_global_next_game()
4307 global_game_next_prev(1);
4310 void do_global_prev_game()
4312 global_game_next_prev(0);
4315 void global_find(int which
)
4317 struct input_data_s
*in
;
4323 in
= Calloc(1, sizeof(struct input_data_s
));
4324 p
= Malloc(sizeof(int));
4327 in
->efunc
= do_find_game_exp
;
4329 if (!*gameexp
|| which
== 0) {
4330 construct_input(_("Find Game by Tag Expression"), NULL
, 1, 0,
4331 _("[name expression:]value expression"), NULL
, NULL
, 0, in
,
4332 INPUT_HIST_GAME_EXP
, -1);
4336 do_find_game_exp_finalize(which
);
4339 void do_global_find_new()
4344 void do_global_find_next()
4349 void do_global_find_prev()
4354 void do_global_game_jump()
4360 struct input_data_s
*in
;
4362 in
= Calloc(1, sizeof(struct input_data_s
));
4363 in
->efunc
= do_game_jump
;
4364 construct_input(_("Jump to Game Number"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
,
4369 do_game_jump_finalize(keycount
);
4372 void do_global_toggle_delete()
4381 if (keycount
&& delete_count
== 0) {
4383 delete_count
= keycount
;
4384 update_status_notify(gp
, "%s (delete)", status
.notify
);
4388 if (markstart
>= 0 && markend
>= 0) {
4389 for (i
= markstart
; i
< markend
; i
++) {
4390 if (toggle_delete_flag(i
)) {
4395 gindex
= (delete_count
< 0) ? markstart
: i
- 1;
4398 if (toggle_delete_flag(gindex
))
4402 markstart
= markend
= -1;
4404 update_status_window(gp
);
4407 void do_global_delete_game()
4412 void do_global_tag_edit()
4414 struct userdata_s
*d
= gp
->data
;
4416 edit_tags(gp
, d
->b
, 1);
4419 void do_global_tag_view()
4421 struct userdata_s
*d
= gp
->data
;
4423 edit_tags(gp
, d
->b
, 0);
4426 void do_global_resume_game()
4428 struct input_data_s
*in
;
4430 in
= Calloc(1, sizeof(struct input_data_s
));
4431 in
->efunc
= do_load_file
;
4432 construct_input(_("Load Filename"), NULL
, 1, 1, _("Type TAB for file browser"), file_browser
,
4433 NULL
, '\t', in
, INPUT_HIST_FILE
, -1);
4436 void do_global_save_game()
4439 construct_message(NULL
, _("What would you like to do?"), 0, 1,
4440 NULL
, NULL
, NULL
, do_game_save_multi_confirm
, 0, 0,
4441 _("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."),
4442 current_wchar
, all_wchar
);
4446 do_get_game_save_input(-1);
4449 void do_global_new_game()
4454 void do_global_copy_game()
4458 struct userdata_s
*d
;
4460 do_global_new_game();
4462 n
= pgn_tag_total(game
[g
]->tag
);
4464 for (i
= 0; i
< n
; i
++)
4465 pgn_tag_add(&gp
->tag
, game
[g
]->tag
[i
]->name
,
4466 game
[g
]->tag
[i
]->value
);
4468 pgn_board_init_fen (gp
, d
->b
, NULL
);
4469 n
= pgn_history_total(game
[g
]->history
);
4472 for (i
= 0; i
< n
; i
++) {
4474 char *move
= strdup (game
[g
]->history
[i
]->move
);
4476 if (pgn_parse_move(gp
, d
->b
, &move
, &frfr
) != E_PGN_OK
) {
4478 SET_FLAG(gp
->flags
, GF_PERROR
);
4482 pgn_history_add(gp
, d
->b
, move
);
4485 pgn_switch_turn(gp
);
4488 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4491 void do_global_new_all()
4493 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
4494 do_new_game_from_scratch
, 0, 0, "%s", _("Really start a new game from scratch?"));
4497 void do_quit(WIN
*win
)
4499 wchar_t str
[] = { win
->c
, 0 };
4501 if (wcscmp (str
, yes_wchar
))
4507 void do_global_quit()
4509 if (config
.exitdialogbox
)
4510 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
4511 do_quit
, 0, 0, _("Want to Quit?"));
4516 void do_global_toggle_engine_window()
4519 enginew
= newwin(LINES
, COLS
, 0, 0);
4520 enginep
= new_panel(enginew
);
4521 window_draw_title(enginew
, _("Engine IO Window"), COLS
, CP_MESSAGE_TITLE
,
4523 hide_panel(enginep
);
4526 if (panel_hidden(enginep
)) {
4527 update_engine_window(gp
);
4531 hide_panel(enginep
);
4535 void do_global_toggle_board_details()
4540 void do_global_toggle_strict_castling()
4542 do_toggle_strict_castling();
4545 // Global and other keys.
4546 static int globalkeys()
4548 struct userdata_s
*d
= gp
->data
;
4552 * These cannot be modified and other game mode keys cannot conflict with
4557 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
4558 markend
= markstart
= 0;
4562 update_status_notify(gp
, NULL
);
4565 if (config
.validmoves
)
4566 pgn_reset_valid_moves(d
->b
);
4573 keycount
= keycount
* 10 + i
;
4577 update_status_notify(gp
, _("Repeat %i"), keycount
);
4580 if (d
->mode
== MODE_HISTORY
)
4584 d
->c_row
+= keycount
;
4593 if (d
->mode
== MODE_HISTORY
)
4597 d
->c_row
-= keycount
;
4598 update_status_notify(gp
, NULL
);
4608 if (d
->mode
== MODE_HISTORY
)
4612 d
->c_col
-= keycount
;
4621 if (d
->mode
== MODE_HISTORY
)
4625 d
->c_col
+= keycount
;
4637 for (i
= 0; global_keys
[i
]; i
++) {
4638 if (input_c
== global_keys
[i
]->c
&& global_keys
[i
]->f
) {
4639 (*global_keys
[i
]->f
)();
4650 static void perl_error(const char *fmt
, ...)
4656 vasprintf(&buf
, fmt
, ap
);
4659 message(ERROR_STR
, ANY_KEY_STR
, "%s", buf
);
4663 static void do_perl_finalize(WIN
*win
)
4665 struct input_data_s
*in
= win
->data
;
4667 struct userdata_s
*d
= g
->data
;
4669 char *result
= NULL
;
4673 asprintf(&filename
, "%s/perl.pl", config
.datadir
);
4678 if (perl_init_file(filename
, perl_error
))
4681 arg
= pgn_game_to_fen(g
, d
->b
);
4683 if (perl_call_sub(trim(in
->str
), arg
, &result
))
4686 d
->perlfen
= pgn_game_to_fen(g
, d
->b
);
4687 d
->perlflags
= g
->flags
;
4689 if (pgn_board_init_fen(g
, d
->b
, result
) != E_PGN_OK
) {
4690 message(ERROR_STR
, ANY_KEY_STR
, "%s", _("FEN parse error."));
4691 pgn_board_init_fen(g
, d
->b
, d
->perlfen
);
4692 g
->flags
= d
->perlflags
;
4698 SET_FLAG(d
->flags
, CF_PERL
);
4699 n
= pgn_tag_find(g
->tag
, "FEN");
4702 d
->oldfen
= strdup(g
->tag
[n
]->value
);
4704 pgn_tag_add(&g
->tag
, "FEN", result
);
4705 update_status_notify(g
, "%s", ANY_KEY_STR
);
4716 void do_global_perl()
4718 struct input_data_s
*in
;
4720 in
= Calloc(1, sizeof(struct input_data_s
));
4722 in
->efunc
= do_perl_finalize
;
4723 construct_input(_("PERL Subroutine Filter"), NULL
, 1, 0, NULL
, NULL
, NULL
, 0, in
, INPUT_HIST_PERL
, -1);
4728 * A macro may contain a key that belongs to another macro so macro_match will
4729 * need to be updated to the new index of the matching macro.
4731 static void find_macro(struct userdata_s
*d
)
4736 * Macros can't contain macros when in a window.
4742 for (i
= 0; macros
[i
]; i
++) {
4743 if ((macros
[i
]->mode
== -1 || macros
[i
]->mode
== d
->mode
) &&
4744 input_c
== macros
[i
]->c
) {
4745 input_c
= macros
[i
]->keys
[macros
[i
]->n
++];
4747 if (!macro_depth_n
&& macro_match
> -1) {
4748 macro_depth
= realloc(macro_depth
, (macro_depth_n
+ 1) * sizeof(int));
4749 macro_depth
[macro_depth_n
++] = macro_match
;
4752 macro_depth
= realloc(macro_depth
, (macro_depth_n
+ 1) * sizeof(int));
4753 macro_depth
[macro_depth_n
++] = i
;
4761 * Resets the position in each macro to the first key.
4763 static void reset_macros()
4766 struct userdata_s
*d
= gp
->data
;
4769 if (macro_depth_n
> 0) {
4771 macro_match
= macro_depth
[macro_depth_n
];
4773 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
4776 input_c
= macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
4781 for (i
= 0; macros
[i
]; i
++)
4792 struct userdata_s
*d
;
4795 gindex
= gtotal
- 1;
4799 if (pgn_history_total(gp
->hp
))
4800 d
->mode
= MODE_HISTORY
;
4802 d
->mode
= MODE_PLAY
;
4803 d
->play_mode
= PLAY_HE
;
4809 d
->pm_frfr
[0] = '\0';
4811 if (d
->mode
== MODE_HISTORY
)
4812 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4814 update_status_notify(gp
, "%s", _("Type F1 for help"));
4818 wtimeout(boardw
, WINDOW_TIMEOUT
);
4822 char fdbuf
[8192] = {0};
4824 struct timeval tv
= {0, 0};
4832 for (i
= 0; i
< gtotal
; i
++) {
4835 if (d
->engine
&& d
->engine
->pid
!= -1) {
4836 if (d
->engine
->fd
[ENGINE_IN_FD
] > 2) {
4837 if (d
->engine
->fd
[ENGINE_IN_FD
] > n
)
4838 n
= d
->engine
->fd
[ENGINE_IN_FD
];
4840 FD_SET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
);
4843 if (d
->engine
->fd
[ENGINE_OUT_FD
] > 2) {
4844 if (d
->engine
->fd
[ENGINE_OUT_FD
] > n
)
4845 n
= d
->engine
->fd
[ENGINE_OUT_FD
];
4847 FD_SET(d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
);
4853 if ((n
= select(n
+ 1, &rfds
, &wfds
, NULL
, &tv
)) > 0) {
4854 for (i
= 0; i
< gtotal
; i
++) {
4857 if (d
->engine
&& d
->engine
->pid
!= -1) {
4858 if (FD_ISSET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
)) {
4859 len
= read(d
->engine
->fd
[ENGINE_IN_FD
], fdbuf
,
4863 if (d
->engine
->iobuf
)
4864 d
->engine
->iobuf
= Realloc(d
->engine
->iobuf
, d
->engine
->len
+ len
+ 1);
4866 d
->engine
->iobuf
= Calloc(1, len
+ 1);
4868 memcpy(&(d
->engine
->iobuf
[d
->engine
->len
]), &fdbuf
, len
);
4869 d
->engine
->len
+= len
;
4870 d
->engine
->iobuf
[d
->engine
->len
] = 0;
4873 * The fdbuf is full or no newline
4874 * was found. So we'll append the next
4875 * read() to this games buffer.
4877 if (d
->engine
->iobuf
[d
->engine
->len
- 1] != '\n')
4880 parse_engine_output(game
[i
], d
->engine
->iobuf
);
4881 free(d
->engine
->iobuf
);
4882 d
->engine
->iobuf
= NULL
;
4885 else if (len
== -1) {
4886 if (errno
!= EAGAIN
) {
4887 cmessage(ERROR_STR
, ANY_KEY_STR
, "Engine read(): %s",
4889 waitpid(d
->engine
->pid
, &n
, 0);
4897 if (FD_ISSET(d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
)) {
4898 if (d
->engine
->queue
)
4899 send_engine_command(game
[i
]);
4906 cmessage(ERROR_STR
, ANY_KEY_STR
, "select(): %s", strerror(errno
));
4915 * This is needed to detect terminal resizing.
4918 if (LINES
!= LINES_OLD
|| COLS
!= COLS_OLD
) {
4925 * Finds the top level window in the window stack so we know what
4926 * window the wget_wch()'ed key belongs to.
4929 for (i
= 0; wins
[i
]; i
++);
4932 wtimeout(wp
, WINDOW_TIMEOUT
);
4941 if (macros
&& macro_match
>= 0) {
4942 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
4945 input_c
= macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
4950 if (wget_wch(wp
, &input_c
) == ERR
|| input_c
== KEY_RESIZE
)
4961 * Run the function associated with the window. When the
4962 * function returns 0 win->efunc is ran (if not NULL) with
4963 * win as the one and only parameter. Then the window is
4966 * The exit function may create another window which will
4967 * mess up the window stack when window_destroy() is called.
4968 * So don't destory the window until the top window is
4969 * destroyable. See window_destroy().
4971 if ((*win
->func
)(win
) == 0) {
4976 window_destroy(win
);
4984 if (!keycount
&& status
.notify
)
4985 update_status_notify(gp
, NULL
);
4988 if (TEST_FLAG(d
->flags
, CF_PERL
)) {
4989 CLEAR_FLAG(d
->flags
, CF_PERL
);
4990 pgn_board_init_fen(gp
, d
->b
, d
->perlfen
);
4991 gp
->flags
= d
->perlflags
;
4993 pgn_tag_add(&gp
->tag
, "FEN", d
->oldfen
);
4995 d
->perlfen
= d
->oldfen
= NULL
;
5001 if (macros
&& macro_match
< 0)
5004 if ((n
= globalkeys()) == 1) {
5005 if (macro_match
== -1)
5015 for (i
= 0; edit_keys
[i
]; i
++) {
5016 if (input_c
== edit_keys
[i
]->c
) {
5017 (*edit_keys
[i
]->f
)();
5023 for (i
= 0; play_keys
[i
]; i
++) {
5024 if (input_c
== play_keys
[i
]->c
) {
5025 (*play_keys
[i
]->f
)();
5030 do_play_config_command();
5033 for (i
= 0; history_keys
[i
]; i
++) {
5034 if (input_c
== history_keys
[i
]->c
) {
5035 (*history_keys
[i
]->f
)();
5046 update_status_notify(gp
, NULL
);
5055 void usage(const char *pn
, int ret
)
5057 fprintf((ret
) ? stderr
: stdout
, "%s%s",
5060 "Usage: cboard [-hvCD] [-u [N]] [-p [-VtRSE] <file>]\n"
5061 " -D Dump libchess debugging info to \"libchess.debug\" (stderr)\n"),
5064 "Usage: cboard [-hvC] [-u [N]] [-p [-VtRSE] <file>]\n"),
5067 " -p Load PGN file.\n"
5068 " -V Validate a game file.\n"
5069 " -S Validate and output a PGN formatted game.\n"
5070 " -R Like -S but write a reduced PGN formatted game.\n"
5071 " -t Also write custom PGN tags from config file.\n"
5072 " -E Stop processing on file parsing error (overrides config).\n"
5073 " -C Enable strict castling (overrides config).\n"
5074 " -u Enable/disable UTF-8 pieces (1=enable, 0=disable, overrides config).\n"
5075 " -v Version information.\n"
5076 " -h This help text.\n"));
5088 free(config
.engine_cmd
);
5089 free(config
.pattern
);
5090 free(config
.ccfile
);
5091 free(config
.nagfile
);
5092 free(config
.configfile
);
5095 for (i
= 0; config
.keys
[i
]; i
++) {
5096 free(config
.keys
[i
]->str
);
5097 free(config
.keys
[i
]);
5104 for (i
= 0; config
.einit
[i
]; i
++)
5105 free(config
.einit
[i
]);
5111 pgn_tag_free(config
.tag
);
5113 free(config
.datadir
);
5115 if (curses_initialized
) {
5117 del_panel(historyp
);
5138 static void signal_save_pgn(int sig
)
5142 char *p
= config
.savedirectory
? config
.savedirectory
: config
.datadir
;
5145 asprintf(&buf
, "%s/signal-%i-%li.pgn", p
, sig
, now
);
5147 if (do_game_write(buf
, "w", 0, gtotal
)) {
5148 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", p
, strerror(errno
));
5149 update_status_notify(gp
, "%s", _("Save game failed."));
5156 void catch_signal(int which
)
5163 if (which
== SIGPIPE
&& quit
)
5166 if (which
== SIGPIPE
)
5167 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Broken pipe. Quitting."));
5177 do_window_resize ();
5178 keypad(boardw
, TRUE
);
5184 signal_save_pgn(which
);
5191 void loading_progress(long total
, long offset
)
5193 int n
= (100 * (offset
/ 100) / (total
/ 100));
5195 if (curses_initialized
)
5196 update_loading_window(n
);
5198 fprintf(stderr
, _("Loading... %i%% (%i games)%c"), n
, gtotal
, '\r');
5203 static void set_defaults()
5205 set_config_defaults();
5207 filetype
= FILE_NONE
;
5208 pgn_config_set(PGN_PROGRESS
, 1024);
5209 pgn_config_set(PGN_PROGRESS_FUNC
, loading_progress
);
5212 int main(int argc
, char *argv
[])
5216 char buf
[FILENAME_MAX
];
5217 char datadir
[FILENAME_MAX
];
5218 int ret
= EXIT_SUCCESS
;
5219 int validate_only
= 0, validate_and_write
= 0;
5220 int write_custom_tags
= 0;
5223 int utf8_pieces
= -1;
5225 setlocale (LC_ALL
, "");
5226 bindtextdomain ("cboard", LOCALE_DIR
);
5227 textdomain ("cboard");
5230 #ifndef HAVE_PROGNAME
5231 __progname
= argv
[0];
5234 if ((config
.pwd
= getpwuid(getuid())) == NULL
)
5235 err(EXIT_FAILURE
, "getpwuid()");
5237 snprintf(datadir
, sizeof(datadir
), "%s/.cboard", config
.pwd
->pw_dir
);
5238 config
.datadir
= strdup(datadir
);
5239 snprintf(buf
, sizeof(buf
), "%s/cc.data", datadir
);
5240 config
.ccfile
= strdup(buf
);
5241 snprintf(buf
, sizeof(buf
), "%s/nag.data", datadir
);
5242 config
.nagfile
= strdup(buf
);
5243 snprintf(buf
, sizeof(buf
), "%s/config", datadir
);
5244 config
.configfile
= strdup(buf
);
5246 if (stat(datadir
, &st
) == -1) {
5247 if (errno
== ENOENT
) {
5248 if (mkdir(datadir
, 0755) == -1)
5249 err(EXIT_FAILURE
, "%s", datadir
);
5252 err(EXIT_FAILURE
, "%s", datadir
);
5257 if (!S_ISDIR(st
.st_mode
))
5258 errx(EXIT_FAILURE
, "%s: %s", datadir
, _("Not a directory."));
5263 while ((opt
= getopt(argc
, argv
, "DCEVtSRhp:vu::")) != -1) {
5265 while ((opt
= getopt(argc
, argv
, "ECVtSRhp:vu::")) != -1) {
5270 unlink("libchess.debug");
5271 pgn_config_set(PGN_DEBUG
, 1);
5275 pgn_config_set(PGN_STRICT_CASTLING
, 1);
5278 write_custom_tags
= 1;
5284 pgn_config_set(PGN_REDUCED
, 1);
5286 validate_and_write
= 1;
5291 printf("%s (%s)\n%s\n", PACKAGE_STRING
, curses_version(),
5295 filetype
= FILE_PGN
;
5296 strncpy(loadfile
, optarg
, sizeof(loadfile
));
5297 loadfile
[sizeof(loadfile
)-1] = 0;
5300 utf8_pieces
= optarg
? atoi (optarg
): 1;
5304 usage(argv
[0], EXIT_SUCCESS
);
5308 if ((validate_only
|| validate_and_write
) && !*loadfile
)
5309 usage(argv
[0], EXIT_FAILURE
);
5311 if (access(config
.configfile
, R_OK
) == 0)
5312 parse_rcfile(config
.configfile
);
5315 pgn_config_set(PGN_STOP_ON_ERROR
, 1);
5317 signal(SIGPIPE
, catch_signal
);
5318 signal(SIGCONT
, catch_signal
);
5319 signal(SIGSTOP
, catch_signal
);
5320 signal(SIGINT
, catch_signal
);
5321 signal(SIGALRM
, catch_signal
);
5322 signal(SIGTERM
, catch_signal
);
5328 if (pgn_open(loadfile
, "r", &pgn
) != E_PGN_OK
)
5329 err(EXIT_FAILURE
, "%s", loadfile
);
5331 ret
= pgn_parse(pgn
);
5335 //ret = parse_fen_file(loadfile);
5337 case FILE_EPD
: // Not implemented.
5340 // No file specified. Empty game.
5341 ret
= pgn_parse(NULL
);
5343 add_custom_tags(&gp
->tag
);
5347 if (validate_only
|| validate_and_write
) {
5348 if (validate_and_write
) {
5349 if (pgn_open("-", "r", &pgn
) != E_PGN_OK
)
5350 err(EXIT_FAILURE
, "pgn_open()");
5352 for (i
= 0; i
< gtotal
; i
++) {
5353 if (write_custom_tags
)
5354 add_custom_tags(&game
[i
]->tag
);
5356 pgn_write(pgn
, game
[i
]);
5361 fm_loaded_file
= TRUE
;
5367 else if (ret
== E_PGN_ERR
)
5370 if (utf8_pieces
!= -1)
5371 config
.utf8_pieces
= utf8_pieces
;
5373 init_wchar_pieces ();
5374 yes_wchar
= str_to_wchar (_("y"));
5375 all_wchar
= str_to_wchar (_("a"));
5376 overwrite_wchar
= str_to_wchar (_("o"));
5377 resume_wchar
= str_to_wchar (_("r"));
5378 current_wchar
= str_to_wchar (_("c"));
5379 append_wchar
= str_to_wchar (_("a"));
5380 translatable_tag_names
[0] = _("Event");
5381 translatable_tag_names
[1] = _("Site");
5382 translatable_tag_names
[2] = _("Date");
5383 translatable_tag_names
[3] = _("Round");
5384 translatable_tag_names
[4] = _("White");
5385 translatable_tag_names
[5] = _("Black");
5386 translatable_tag_names
[6] = _("Result");
5390 * This fixes window resizing in an xterm.
5392 if (getenv("DISPLAY") != NULL
) {
5397 if (initscr() == NULL
)
5398 errx(EXIT_FAILURE
, "%s", _("Could not initialize curses."));
5400 curses_initialized
= 1;
5402 if (LINES
< 23 || COLS
< 74) {
5404 errx(EXIT_FAILURE
, _("Need at least an 74x23 terminal."));
5410 if (has_colors() == TRUE
&& start_color() == OK
)
5413 boardw
= newwin(BOARD_HEIGHT
, BOARD_WIDTH
, 0, COLS
- BOARD_WIDTH
);
5414 boardp
= new_panel(boardw
);
5415 historyw
= newwin(HISTORY_HEIGHT
, HISTORY_WIDTH
, LINES
- HISTORY_HEIGHT
,
5416 COLS
- HISTORY_WIDTH
);
5417 historyp
= new_panel(historyw
);
5418 statusw
= newwin(STATUS_HEIGHT
, STATUS_WIDTH
, 0, 0);
5419 statusp
= new_panel(statusw
);
5420 tagw
= newwin(TAG_HEIGHT
, TAG_WIDTH
, STATUS_HEIGHT
+ 1, 0);
5421 tagp
= new_panel(tagw
);
5422 keypad(boardw
, TRUE
);
5423 // leaveok(boardw, TRUE);
5424 leaveok(tagw
, TRUE
);
5425 leaveok(statusw
, TRUE
);
5426 leaveok(historyw
, TRUE
);
5430 draw_window_decor();
5433 free (w_pawn_wchar
);
5434 free (w_rook_wchar
);
5435 free (w_bishop_wchar
);
5436 free (w_knight_wchar
);
5437 free (w_queen_wchar
);
5438 free (w_king_wchar
);
5439 free (b_pawn_wchar
);
5440 free (b_rook_wchar
);
5441 free (b_bishop_wchar
);
5442 free (b_knight_wchar
);
5443 free (b_queen_wchar
);
5444 free (b_king_wchar
);
5446 free (enpassant_wchar
);
5449 free (overwrite_wchar
);
5450 free (resume_wchar
);
5451 free (current_wchar
);
5452 free (append_wchar
);
5453 free (status
.notify
);