1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2015 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-2015 " 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
);
288 void edit_nag_toggle_item(struct menu_input_s
*m
)
290 struct input_s
*in
= m
->data
;
291 struct input_data_s
*id
= in
->data
;
292 HISTORY
*h
= id
->data
;
295 if (m
->selected
== 0) {
296 for (i
= 0; i
< MAX_PGN_NAG
; i
++)
299 for (i
= 0; m
->items
[i
]; i
++)
300 m
->items
[i
]->selected
= 0;
305 for (i
= 0; i
< MAX_PGN_NAG
; i
++) {
306 if (h
->nag
[i
] == m
->selected
)
307 h
->nag
[i
] = m
->selected
= 0;
310 h
->nag
[i
] = m
->selected
;
317 void edit_nag_save(struct menu_input_s
*m
)
322 void edit_nag_help(struct menu_input_s
*m
)
324 message(_("NAG Menu Keys"), ANY_KEY_STR
, "%s",
326 " UP/DOWN - previous/next menu item\n"
327 " HOME/END - first/last menu item\n"
328 " PGDN/PGUP - next/previous page\n"
329 " a-zA-Z0-9 - jump to item\n"
330 " SPACE - toggle selected item\n"
331 " CTRL-X - quit with changes"
335 struct menu_item_s
**get_nag_items(WIN
*win
)
338 struct menu_input_s
*m
= win
->data
;
339 struct input_s
*in
= m
->data
;
340 struct input_data_s
*id
= in
->data
;
341 struct menu_item_s
**items
= m
->items
;
342 HISTORY
*h
= id
->data
;
345 for (i
= 0; items
[i
]; i
++)
349 for (i
= 0; nags
[i
]; i
++) {
350 items
= Realloc(items
, (i
+2) * sizeof(struct menu_item_s
*));
351 items
[i
] = Malloc(sizeof(struct menu_item_s
));
352 items
[i
]->name
= nags
[i
];
353 items
[i
]->value
= NULL
;
355 for (n
= 0; n
< MAX_PGN_NAG
; n
++) {
356 if (h
->nag
[n
] == i
) {
357 items
[i
]->selected
= 1;
364 items
[i
]->selected
= 0;
373 void nag_print(WIN
*win
)
375 struct menu_input_s
*m
= win
->data
;
377 mvwprintw(win
->w
, m
->print_line
, 1, "%-*s", win
->cols
- 2, m
->item
->name
);
380 void edit_nag(void *arg
)
382 struct menu_key_s
**keys
= NULL
;
389 add_menu_key(&keys
, ' ', edit_nag_toggle_item
);
390 add_menu_key(&keys
, CTRL_KEY('x'), edit_nag_save
);
391 add_menu_key(&keys
, KEY_F(1), edit_nag_help
);
392 construct_menu(0, 0, -1, -1, _("Numeric Annotation Glyphs"), 1, get_nag_items
, keys
, arg
,
397 static void *view_nag(void *arg
)
399 HISTORY
*h
= (HISTORY
*)arg
;
401 char line
[LINE_MAX
] = {0};
404 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Viewing NAG for"), h
->move
);
411 for (i
= 0; i
< MAX_PGN_NAG
; i
++) {
417 if (h
->nag
[i
] >= nag_total
)
418 strncat(line
, itoa(h
->nag
[i
], buf2
), sizeof(line
)-1);
420 strncat(line
, nags
[h
->nag
[i
]], sizeof(line
)-1);
422 strncat(line
, "\n", sizeof(line
)-1);
425 line
[strlen(line
) - 1] = 0;
426 message(buf
, ANY_KEY_STR
, "%s", line
);
430 void view_annotation(HISTORY
*h
)
432 char buf
[MAX_SAN_MOVE_LEN
+ strlen(_("Viewing Annotation for")) + 4];
433 int nag
= 0, comment
= 0;
438 if (h
->comment
&& h
->comment
[0])
444 if (!nag
&& !comment
)
447 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Viewing Annotation for"), h
->move
);
450 construct_message(buf
, (nag
) ? _("Any other key to continue") : ANY_KEY_STR
, 0, 1,
451 (nag
) ? _("Press 'n' to view NAG") : NULL
,
452 (nag
) ? view_nag
: NULL
, (nag
) ? h
: NULL
, NULL
,
453 (nag
) ? 'n' : 0, 0, "%s", h
->comment
);
455 construct_message(buf
, _("Any other key to continue"), 0, 1, _("Press 'n' to view NAG"), view_nag
, h
, NULL
,
456 'n', 0, "%s", _("No comment text for this move"));
459 int do_game_write(char *filename
, char *mode
, int start
, int end
)
462 struct userdata_s
*d
;
465 i
= pgn_open(filename
, mode
, &pgn
);
467 if (i
== E_PGN_ERR
) {
468 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", filename
, strerror(errno
));
471 else if (i
== E_PGN_INVALID
) {
472 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", filename
, _("Not a regular file"));
476 for (i
= (start
== -1) ? 0 : start
; i
< end
; i
++) {
478 pgn_write(pgn
, game
[i
]);
479 CLEAR_FLAG(d
->flags
, CF_MODIFIED
);
482 if (pgn_close(pgn
) != E_PGN_OK
)
483 message(ERROR_STR
, ANY_KEY_STR
, "%s", strerror(errno
));
486 strncpy(loadfile
, filename
, sizeof(loadfile
));
487 loadfile
[sizeof(loadfile
)-1] = 0;
500 void do_save_game_overwrite_confirm(WIN
*win
)
503 struct save_game_s
*s
= win
->data
;
504 wchar_t str
[] = { win
->c
, 0 };
506 if (!wcscmp (str
, append_wchar
))
508 else if (!wcscmp (str
, overwrite_wchar
))
513 if (do_game_write(s
->filename
, mode
, s
->start
, s
->end
))
514 update_status_notify(gp
, "%s", _("Save game failed."));
516 update_status_notify(gp
, "%s", _("Game saved."));
523 /* If the saveindex argument is -1, all games will be saved. Otherwise it's a
526 void save_pgn(char *filename
, int saveindex
)
528 char buf
[FILENAME_MAX
];
530 int end
= (saveindex
== -1) ? gtotal
: saveindex
+ 1;
531 struct save_game_s
*s
;
533 if (filename
[0] != '/' && config
.savedirectory
) {
534 if (stat(config
.savedirectory
, &st
) == -1) {
535 if (errno
== ENOENT
) {
536 if (mkdir(config
.savedirectory
, 0755) == -1) {
537 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
,
543 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
,
549 if (stat(config
.savedirectory
, &st
) == -1) {
550 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
,
555 if (!S_ISDIR(st
.st_mode
)) {
556 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
, _("Not a directory."));
560 snprintf(buf
, sizeof(buf
), "%s/%s", config
.savedirectory
, filename
);
564 if (access(filename
, W_OK
) == 0) {
565 s
= Malloc(sizeof(struct save_game_s
));
566 s
->filename
= strdup(filename
);
567 s
->start
= saveindex
;
569 construct_message(NULL
, _("What would you like to do?"), 0, 1, NULL
,
570 NULL
, s
, do_save_game_overwrite_confirm
, 0, 0,
571 "%s \"%s\"\nPress \"%ls\" to append to this file, \"%ls\" to overwrite or any other key to cancel.",
572 _("File exists:"), filename
, append_wchar
,
577 if (do_game_write(filename
, "a", saveindex
, end
))
578 update_status_notify(gp
, "%s", _("Save game failed."));
580 update_status_notify(gp
, "%s", _("Game saved."));
583 static int castling_state(GAME g
, BOARD b
, int row
, int col
, int piece
, int mod
)
585 if (pgn_piece_to_int(piece
) == ROOK
&& col
== 7
587 (TEST_FLAG(g
->flags
, GF_WK_CASTLE
) || mod
) &&
588 pgn_piece_to_int(b
[7][4].icon
) == KING
&& isupper(piece
)) {
590 TOGGLE_FLAG(g
->flags
, GF_WK_CASTLE
);
593 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 0
595 (TEST_FLAG(g
->flags
, GF_WQ_CASTLE
) || mod
) &&
596 pgn_piece_to_int(b
[7][4].icon
) == KING
&& isupper(piece
)) {
598 TOGGLE_FLAG(g
->flags
, GF_WQ_CASTLE
);
601 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 7
603 (TEST_FLAG(g
->flags
, GF_BK_CASTLE
) || mod
) &&
604 pgn_piece_to_int(b
[0][4].icon
) == KING
&& islower(piece
)) {
606 TOGGLE_FLAG(g
->flags
, GF_BK_CASTLE
);
609 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 0
611 (TEST_FLAG(g
->flags
, GF_BQ_CASTLE
) || mod
) &&
612 pgn_piece_to_int(b
[0][4].icon
) == KING
&& islower(piece
)) {
614 TOGGLE_FLAG(g
->flags
, GF_BQ_CASTLE
);
617 else if (pgn_piece_to_int(piece
) == KING
&& col
== 4
619 (mod
|| (pgn_piece_to_int(b
[7][7].icon
) == ROOK
&&
620 TEST_FLAG(g
->flags
, GF_WK_CASTLE
))
622 (pgn_piece_to_int(b
[7][0].icon
) == ROOK
&&
623 TEST_FLAG(g
->flags
, GF_WQ_CASTLE
))) && isupper(piece
)) {
625 if (TEST_FLAG(g
->flags
, GF_WK_CASTLE
) ||
626 TEST_FLAG(g
->flags
, GF_WQ_CASTLE
))
627 CLEAR_FLAG(g
->flags
, GF_WK_CASTLE
|GF_WQ_CASTLE
);
629 SET_FLAG(g
->flags
, GF_WK_CASTLE
|GF_WQ_CASTLE
);
633 else if (pgn_piece_to_int(piece
) == KING
&& col
== 4
635 (mod
|| (pgn_piece_to_int(b
[0][7].icon
) == ROOK
&&
636 TEST_FLAG(g
->flags
, GF_BK_CASTLE
))
638 (pgn_piece_to_int(b
[0][0].icon
) == ROOK
&&
639 TEST_FLAG(g
->flags
, GF_BQ_CASTLE
))) && islower(piece
)) {
641 if (TEST_FLAG(g
->flags
, GF_BK_CASTLE
) ||
642 TEST_FLAG(g
->flags
, GF_BQ_CASTLE
))
643 CLEAR_FLAG(g
->flags
, GF_BK_CASTLE
|GF_BQ_CASTLE
);
645 SET_FLAG(g
->flags
, GF_BK_CASTLE
|GF_BQ_CASTLE
);
653 #define IS_ENPASSANT(c) (c == 'x') ? CP_BOARD_ENPASSANT : isupper(c) ? CP_BOARD_WHITE : CP_BOARD_BLACK
654 #define ATTRS(cp) (cp & (A_BOLD|A_STANDOUT|A_BLINK|A_DIM|A_UNDERLINE|A_INVIS|A_REVERSE))
659 w_pawn_wchar
= str_to_wchar (config
.utf8_pieces
? "♙" : "P");
660 w_rook_wchar
= str_to_wchar (config
.utf8_pieces
? "♖" : "R");
661 w_bishop_wchar
= str_to_wchar (config
.utf8_pieces
? "♗" : "B");
662 w_knight_wchar
= str_to_wchar (config
.utf8_pieces
? "♘" : "N");
663 w_queen_wchar
= str_to_wchar (config
.utf8_pieces
? "♕" : "Q");
664 w_king_wchar
= str_to_wchar (config
.utf8_pieces
? "♔" : "K");
665 b_pawn_wchar
= str_to_wchar (config
.utf8_pieces
? "♟" : "p");
666 b_rook_wchar
= str_to_wchar (config
.utf8_pieces
? "♜" : "r");
667 b_bishop_wchar
= str_to_wchar (config
.utf8_pieces
? "♝" : "b");
668 b_knight_wchar
= str_to_wchar (config
.utf8_pieces
? "♞" : "n");
669 b_queen_wchar
= str_to_wchar (config
.utf8_pieces
? "♛" : "q");
670 b_king_wchar
= str_to_wchar (config
.utf8_pieces
? "♚" : "k");
671 empty_wchar
= str_to_wchar (" ");
672 enpassant_wchar
= str_to_wchar ("x");
676 piece_to_wchar (unsigned char p
)
689 return w_bishop_wchar
;
691 return b_bishop_wchar
;
693 return w_knight_wchar
;
695 return b_knight_wchar
;
697 return w_queen_wchar
;
699 return b_queen_wchar
;
705 return enpassant_wchar
;
711 static int piece_can_attack (GAME g
, int rank
, int file
)
713 struct userdata_s
*d
= g
->data
;
714 char *m
, *frfr
= NULL
;
716 int row
, col
, p
, v
, pi
, cpi
;
719 rotate_position(&d
->c_row
, &d
->c_col
);
720 rotate_position(&d
->sp
.srow
, &d
->sp
.scol
);
723 v
= d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].valid
;
724 pi
= pgn_piece_to_int (d
->b
[RANKTOBOARD (rank
)][FILETOBOARD (file
)].icon
);
726 ? pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
)
727 : pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].icon
);
729 if (pi
== OPEN_SQUARE
|| cpi
== OPEN_SQUARE
|| !VALIDFILE (file
)
730 || !VALIDRANK (rank
)) {
732 rotate_position(&d
->c_row
, &d
->c_col
);
733 rotate_position(&d
->sp
.srow
, &d
->sp
.scol
);
740 col
= v
? d
->c_col
: d
->sp
.scol
;
741 row
= v
? d
->c_row
: d
->sp
.srow
;
748 m
= malloc(MAX_SAN_MOVE_LEN
+1);
749 m
[0] = INTTOFILE (file
);
750 m
[1] = INTTORANK (rank
);
751 m
[2] = INTTOFILE (col
);
752 m
[3] = INTTORANK (row
);
755 if (d
->sp
.icon
&& v
) {
758 memcpy (b
, d
->b
, sizeof(BOARD
));
759 p
= b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
;
760 b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
761 pgn_int_to_piece (WHITE
, OPEN_SQUARE
);
762 b
[RANKTOBOARD (row
)][FILETOBOARD (col
)].icon
= p
;
764 e
= pgn_validate_move (g
, b
, &m
, &frfr
);
769 if (e
!= E_PGN_OK
&& pgn_piece_to_int (d
->sp
.icon
) == PAWN
) {
770 int n
= (d
->sp
.srow
== 7 && rank
== 5) ? 6 :
771 (d
->sp
.srow
== 2 && rank
== 4) ? 3 : 0;
773 if (n
&& (file
== d
->c_col
-1 || file
== d
->c_col
+1)) {
774 memcpy (b
, d
->b
, sizeof(BOARD
));
775 p
= b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
;
776 b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
777 pgn_int_to_piece (WHITE
, OPEN_SQUARE
);
778 b
[RANKTOBOARD (row
)][FILETOBOARD (col
)].icon
= p
;
779 b
[RANKTOBOARD (n
)][FILETOBOARD (d
->sp
.scol
)].enpassant
= 1;
780 m
= malloc(MAX_SAN_MOVE_LEN
+1);
781 m
[0] = INTTOFILE (file
);
782 m
[1] = INTTORANK (rank
);
783 m
[2] = INTTOFILE (col
);
784 m
[3] = INTTORANK (n
);
787 SET_FLAG (g
->flags
, GF_ENPASSANT
);
788 e
= pgn_validate_move (g
, b
, &m
, &frfr
);
789 CLEAR_FLAG (g
->flags
, GF_ENPASSANT
);
800 e
= pgn_validate_move (g
, d
->b
, &m
, &frfr
);
803 if (!strcmp (m
, "O-O") || !strcmp (m
, "O-O-O"))
807 int sf
= FILETOINT (frfr
[0]), sr
= RANKTOINT (frfr
[1]);
808 int df
= FILETOINT (frfr
[2]);
810 pi
= d
->b
[RANKTOBOARD (sr
)][FILETOBOARD (sf
)].icon
;
811 pi
= pgn_piece_to_int (pi
);
812 if (pi
== PAWN
&& sf
== df
)
821 rotate_position(&d
->c_row
, &d
->c_col
);
822 rotate_position(&d
->sp
.srow
, &d
->sp
.scol
);
825 return e
== E_PGN_OK
? 1 : 0;
828 void print_piece(WINDOW
*w
, int l
, int c
, char p
)
832 for (i
= 0; i
< 13; i
+= 2) {
833 if (p
== piece_chars
[i
] || p
== piece_chars
[i
+ 1]) {
834 for (y
= 0; y
< 3; y
++)
835 mvwprintw(w
, l
+ y
, c
, "%s", f_pieces
[i
+ ff
+ y
]);
842 for (y
= 0; y
< 3; y
++)
843 mvwprintw(w
, l
+ y
, c
, f_pieces
[0]);
846 void board_prev_move_play (GAME g
)
848 struct userdata_s
*d
= g
->data
;
849 char l
= strlen(d
->pm_frfr
);
852 char q
= (l
> 4) ? 2 : 1;
854 d
->pm_row
= RANKTOINT(d
->pm_frfr
[l
- q
++]);
855 d
->pm_col
= FILETOINT(d
->pm_frfr
[l
- q
++]);
856 d
->ospm_row
= RANKTOINT(d
->pm_frfr
[l
- q
++]);
857 d
->ospm_col
= FILETOINT(d
->pm_frfr
[l
- q
]);
859 rotate_position(&d
->pm_row
, &d
->pm_col
);
860 rotate_position(&d
->ospm_row
, &d
->ospm_col
);
871 void board_prev_move_history (GAME g
)
873 struct userdata_s
*d
= g
->data
;
876 char *move
= g
->hp
[g
->hindex
- 1]->move
;
879 if (d
->mode
== MODE_PLAY
)
880 coordofmove(g
, move
, &d
->pm_row
, &d
->pm_col
);
887 d
->ospm_row
= g
->turn
== WHITE
? 8 : 1;
898 HISTORY
*h
= pgn_history_by_n(g
->hp
, g
->hindex
- 2);
900 pgn_board_init_fen(g
, ob
, h
->fen
);
905 for (f
= 0; f
< 8; f
++) {
906 for (r
= 0; r
< 8; r
++) {
907 if (ob
[f
][r
].icon
!= '.' && d
->b
[f
][r
].icon
== '.') {
908 d
->ospm_row
= INV_INT0(f
) + 1;
916 if (d
->mode
== MODE_PLAY
)
917 rotate_position(&d
->pm_row
, &d
->pm_col
);
919 rotate_position(&d
->ospm_row
, &d
->ospm_col
);
933 static int is_the_square (int brow
, int bcol
, int row
, int col
,
936 if ((!BIG_BOARD
&& row
== ROWTOMATRIX(prow
) && col
== COLTOMATRIX(pcol
))
937 || (BIG_BOARD
&& brow
+ 1 == INV_INT(prow
) && bcol
+ 1 == pcol
))
943 static int is_prev_move (struct userdata_s
*d
, int brow
, int bcol
,
946 if (is_the_square (brow
, bcol
, row
, col
, d
->pm_row
, d
->pm_col
))
952 void update_board_window(GAME g
)
955 int bcol
= 0, brow
= 0;
956 int l
= config
.coordsyleft
;
957 int maxy
= BOARD_HEIGHT
, maxx
= BOARD_WIDTH
;
958 int ncols
= 0, offset
= 1;
959 int rowr
= (MEGA_BOARD
) ? 6 : (BIG_BOARD
) ? 4 : 2;
960 int colr
= (MEGA_BOARD
) ? 12 : (BIG_BOARD
) ? 8 : 4;
961 unsigned coords_y
= 8, cxgc
= 0;
963 struct userdata_s
*d
= g
->data
;
965 if (config
.bprevmove
&& d
->mode
!= MODE_EDIT
) {
966 if (!d
->pm_undo
&& d
->mode
== MODE_PLAY
)
967 board_prev_move_play(g
);
969 board_prev_move_history(g
);
978 if (d
->mode
!= MODE_PLAY
&& d
->mode
!= MODE_EDIT
)
979 update_cursor(g
, g
->hindex
);
996 for (row
= 0; row
< maxy
; row
++) {
1010 for (col
= 0; col
< maxx
; col
++) {
1012 chtype attrs
= 0, old_attrs
= 0;
1017 if (row
== 0 || row
== maxy
- 2) {
1019 mvwaddch(boardw
, row
, col
+ l
,
1021 ? ACS_LLCORNER
| CP_BOARD_GRAPHICS
1022 : ACS_ULCORNER
| CP_BOARD_GRAPHICS
));
1023 else if (col
== maxx
- 2)
1024 mvwaddch(boardw
, row
, col
+ l
,
1026 ? ACS_LRCORNER
| CP_BOARD_GRAPHICS
1027 : ACS_URCORNER
| CP_BOARD_GRAPHICS
));
1028 else if (!(col
% colr
))
1029 mvwaddch(boardw
, row
, col
+ l
,
1031 ? ACS_BTEE
| CP_BOARD_GRAPHICS
1032 : ACS_TTEE
| CP_BOARD_GRAPHICS
));
1034 if (col
!= maxx
- 1)
1035 mvwaddch(boardw
, row
, col
+ l
,
1036 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
1042 if ((row
% 2) && col
== maxx
- 1 &&
1043 (coords_y
> 0 && coords_y
< 9)) {
1044 wattron(boardw
, CP_BOARD_COORDS
);
1046 (BIG_BOARD
) ? row
* ((MEGA_BOARD
) ? 3 : 2)
1047 : row
, (l
) ? 0 : col
, "%d",
1048 (d
->rotate
) ? coords_y
++ : coords_y
--);
1049 wattroff(boardw
, CP_BOARD_COORDS
);
1053 if ((col
== 0 || col
== maxx
- 2) && row
!= maxy
- 1) {
1055 mvwaddch(boardw
, row
, col
+ l
,
1056 LINE_GRAPHIC((col
) ?
1057 ACS_RTEE
| CP_BOARD_GRAPHICS
:
1058 ACS_LTEE
| CP_BOARD_GRAPHICS
));
1060 mvwaddch(boardw
, row
, col
+ l
,
1061 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
1066 if ((row
% rowr
) && !(col
% colr
) && row
!= maxy
- 1) {
1067 mvwaddch(boardw
, row
, col
+ l
,
1068 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
1072 if (!(col
% colr
) && row
!= maxy
- 1) {
1073 mvwaddch(boardw
, row
, col
+ l
,
1074 LINE_GRAPHIC(ACS_PLUS
| CP_BOARD_GRAPHICS
));
1081 attrwhich
= (cb
[brow
][bcol
]) ? WHITE
: BLACK
;
1088 if (((ncols
% 2) && !(offset
% 2))
1089 || (!(ncols
% 2) && (offset
% 2)))
1095 if (BIG_BOARD
&& d
->rotate
) {
1096 brow
= INV_INT0(brow
);
1097 bcol
= INV_INT0(bcol
);
1101 p
= d
->b
[brow
][bcol
].icon
;
1103 p
= d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].icon
;
1105 int pi
= pgn_piece_to_int(p
);
1107 if (config
.details
&&
1108 ((!BIG_BOARD
&& d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].enpassant
)
1109 || (BIG_BOARD
&& d
->b
[brow
][bcol
].enpassant
))) {
1111 attrs
= mix_cp(CP_BOARD_ENPASSANT
,
1112 (attrwhich
== WHITE
) ? CP_BOARD_WHITE
: CP_BOARD_BLACK
,
1113 ATTRS(CP_BOARD_ENPASSANT
), A_FG_B_BG
);
1116 if (config
.showattacks
&& config
.details
1117 && piece_can_attack (g
,
1118 BIG_BOARD
? INV_INT0(brow
)+1 : brow
,
1119 BIG_BOARD
? bcol
+1 : bcol
)) {
1120 attrs
= CP_BOARD_ATTACK
;
1125 if (config
.validmoves
&&
1126 ((!BIG_BOARD
&& d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].valid
)
1127 || (BIG_BOARD
&& d
->b
[brow
][bcol
].valid
))) {
1131 if (attrwhich
== WHITE
)
1132 attrs
= mix_cp(CP_BOARD_MOVES_WHITE
,
1134 ATTRS(CP_BOARD_MOVES_WHITE
),
1137 attrs
= mix_cp(CP_BOARD_MOVES_BLACK
,
1139 ATTRS(CP_BOARD_MOVES_BLACK
),
1142 else if (p
!= 'x' && !can_attack
)
1143 attrs
= (attrwhich
== WHITE
) ? CP_BOARD_WHITE
: CP_BOARD_BLACK
;
1145 if (BIG_BOARD
&& d
->rotate
) {
1146 brow
= INV_INT0(brow
);
1147 bcol
= INV_INT0(bcol
);
1150 if (is_the_square (brow
, bcol
, row
, col
, d
->c_row
,
1152 attrs
= mix_cp(CP_BOARD_CURSOR
, IS_ENPASSANT(p
),
1153 ATTRS(CP_BOARD_CURSOR
), B_FG_A_BG
);
1156 else if (is_the_square (brow
, bcol
, row
, col
,
1157 d
->sp
.srow
, d
->sp
.scol
)) {
1158 attrs
= mix_cp(CP_BOARD_SELECTED
, IS_ENPASSANT(p
),
1159 ATTRS(CP_BOARD_SELECTED
), B_FG_A_BG
);
1162 else if ((is_prev_move (d
, brow
, bcol
, row
, col
) && !valid
)
1163 || (is_the_square (brow
, bcol
, row
, col
,
1164 d
->ospm_row
, d
->ospm_col
))) {
1165 attrs
= mix_cp(CP_BOARD_PREVMOVE
, IS_ENPASSANT(p
),
1166 ATTRS(CP_BOARD_PREVMOVE
), B_FG_A_BG
);
1170 if (row
== maxy
- 1)
1174 int n
= is_prev_move (d
, brow
, bcol
, row
, col
);
1175 chtype a
= n
&& !valid
1176 ? CP_BOARD_PREVMOVE
: attrwhich
== WHITE
? valid
? CP_BOARD_MOVES_WHITE
: CP_BOARD_WHITE
: valid
? CP_BOARD_MOVES_BLACK
: CP_BOARD_BLACK
;
1177 attrs
= mix_cp(CP_BOARD_ATTACK
, a
,
1178 ATTRS (CP_BOARD_ATTACK
), A_FG_B_BG
);
1183 wmove(boardw
, row
, col
+ ((MEGA_BOARD
) ? 5 : 3) + l
);
1185 mvwaddch(boardw
, row
, col
+ l
, ' ' | attrs
);
1187 if (row
== maxy
- 1 && cxgc
< 8) {
1188 waddch(boardw
, "abcdefgh"[(BIG_BOARD
) ? bcol
: bcol
- 1] | CP_BOARD_COORDS
);
1192 if (old_attrs
== -1) {
1199 if (pi
!= OPEN_SQUARE
&& p
!= 'x' && !can_attack
) {
1200 if (attrwhich
== WHITE
) {
1202 attrs
= CP_BOARD_W_W
;
1204 attrs
= CP_BOARD_W_B
;
1208 attrs
= CP_BOARD_B_W
;
1210 attrs
= CP_BOARD_B_B
;
1216 if (config
.details
&& !can_attack
1217 && castling_state(g
, d
->b
,
1218 (d
->rotate
) ? INV_INT(brow
+ 1) - 1: brow
,
1219 (d
->rotate
) ? INV_INT(bcol
+ 1) - 1: bcol
, p
, 0))
1220 attrs
= mix_cp(CP_BOARD_CASTLING
, attrs
,
1221 ATTRS(CP_BOARD_CASTLING
),
1225 if (config
.details
&& !can_attack
1226 && castling_state(g
, d
->b
, RANKTOBOARD (brow
),
1227 FILETOBOARD (bcol
), p
, 0)) {
1228 attrs
= mix_cp(CP_BOARD_CASTLING
, attrs
,
1229 ATTRS(CP_BOARD_CASTLING
),
1235 // FIXME: Reimpresión de piezas(+4).
1237 wattron (boardw
, attrs
);
1239 for (i
= 0; i
< 5; i
++)
1240 mvwprintw(boardw
, i
+ brow
* 6 + 1,
1243 if (pi
!= OPEN_SQUARE
)
1244 print_piece(boardw
, brow
* 6 + 2,
1245 bcol
* 12 + 3 + l
, p
);
1248 print_piece(boardw
, brow
* 4 + 1,
1250 (pi
!= OPEN_SQUARE
) ? p
: 0);
1253 wattroff (boardw
, attrs
);
1258 wattron (boardw
, attrs
);
1259 waddwstr (boardw
, piece_to_wchar (pi
!= OPEN_SQUARE
? p
: 0));
1260 wattroff (boardw
, attrs
);
1267 col
+= (MEGA_BOARD
) ? 10 : 6;
1269 waddch(boardw
, ' ' | attrs
);
1287 if (col
!= maxx
- 1)
1288 mvwaddch(boardw
, row
, col
+ l
,
1289 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
1309 void invalid_move(int n
, int e
, const char *m
)
1311 if (curses_initialized
)
1312 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s \"%s\" (round #%i)", (e
== E_PGN_AMBIGUOUS
)
1313 ? _("Ambiguous move") : _("Invalid move"), m
, n
);
1315 warnx("%s: %s \"%s\" (round #%i)", loadfile
, (e
== E_PGN_AMBIGUOUS
)
1316 ? _("Ambiguous move") : _("Invalid move"), m
, n
);
1319 void gameover(GAME g
)
1321 struct userdata_s
*d
= g
->data
;
1323 SET_FLAG(g
->flags
, GF_GAMEOVER
);
1324 d
->mode
= MODE_HISTORY
;
1328 static void update_clock(GAME g
, struct itimerval it
)
1330 struct userdata_s
*d
= g
->data
;
1332 if (TEST_FLAG(d
->flags
, CF_CLOCK
) && g
->turn
== WHITE
) {
1333 d
->wclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1334 d
->wclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1336 if (d
->wclock
.elapsed
.tv_usec
> 1000000 - 1) {
1337 d
->wclock
.elapsed
.tv_sec
+= d
->wclock
.elapsed
.tv_usec
/ 1000000;
1338 d
->wclock
.elapsed
.tv_usec
= d
->wclock
.elapsed
.tv_usec
% 1000000;
1341 if (d
->wclock
.tc
[d
->wclock
.tcn
][1] &&
1342 d
->wclock
.elapsed
.tv_sec
>= d
->wclock
.tc
[d
->wclock
.tcn
][1]) {
1343 pgn_tag_add(&g
->tag
, "Result", "0-1");
1347 else if (TEST_FLAG(d
->flags
, CF_CLOCK
) && g
->turn
== BLACK
) {
1348 d
->bclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1349 d
->bclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1351 if (d
->bclock
.elapsed
.tv_usec
> 1000000 - 1) {
1352 d
->bclock
.elapsed
.tv_sec
+= d
->bclock
.elapsed
.tv_usec
/ 1000000;
1353 d
->bclock
.elapsed
.tv_usec
= d
->bclock
.elapsed
.tv_usec
% 1000000;
1356 if (d
->bclock
.tc
[d
->bclock
.tcn
][1] &&
1357 d
->bclock
.elapsed
.tv_sec
>= d
->bclock
.tc
[d
->bclock
.tcn
][1]) {
1358 pgn_tag_add(&g
->tag
, "Result", "1-0");
1363 d
->elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1364 d
->elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1366 if (d
->elapsed
.tv_usec
> 1000000 - 1) {
1367 d
->elapsed
.tv_sec
+= d
->elapsed
.tv_usec
/ 1000000;
1368 d
->elapsed
.tv_usec
= d
->elapsed
.tv_usec
% 1000000;
1372 static void update_time_control(GAME g
)
1374 struct userdata_s
*d
= g
->data
;
1375 struct clock_s
*clk
= (g
->turn
== WHITE
) ? &d
->wclock
: &d
->bclock
;
1378 clk
->tc
[clk
->tcn
][1] += clk
->incr
;
1380 if (!clk
->tc
[clk
->tcn
][1])
1385 if (!clk
->tc
[clk
->tcn
][0] || clk
->move
>= clk
->tc
[clk
->tcn
][0]) {
1387 clk
->tc
[clk
->tcn
+ 1][1] += abs(clk
->elapsed
.tv_sec
- clk
->tc
[clk
->tcn
][1]);
1388 memset(&clk
->elapsed
, 0, sizeof(clk
->elapsed
));
1393 void update_history_window(GAME g
)
1395 char buf
[HISTORY_WIDTH
- 1];
1398 int t
= pgn_history_total(g
->hp
);
1400 n
= (g
->hindex
+ 1) / 2;
1403 total
= (t
+ 1) / 2;
1408 snprintf(buf
, sizeof(buf
), "%u %s %u%s", n
, _("of"), total
,
1409 (movestep
== 1) ? _(" (ply)") : "");
1411 strncpy(buf
, _("not available"), sizeof(buf
)-1);
1413 buf
[sizeof(buf
)-1] = 0;
1414 mvwprintw(historyw
, 2, 1, "%*s %-*s", 10, _("Move:"),
1415 HISTORY_WIDTH
- 14, buf
);
1417 h
= pgn_history_by_n(g
->hp
, g
->hindex
);
1418 snprintf(buf
, sizeof(buf
), "%s",
1419 (h
&& h
->move
) ? h
->move
1420 : (LINES
< 24) ? _("empty") : _("not available"));
1423 if (h
&& ((h
->comment
) || h
->nag
[0])) {
1424 strncat(buf
, _(" (Annotated"), sizeof(buf
)-1);
1429 strncat(buf
, (n
) ? ",+" : " (+", sizeof(buf
)-1);
1434 strncat(buf
, (n
) ? ",-" : " (-", sizeof(buf
)-1);
1439 strncat(buf
, ")", sizeof(buf
)-1);
1441 mvwprintw(historyw
, 3, ((LINES
< 24) ? 17 : 1), "%s %-*s",
1442 (LINES
< 24) ? _("Next:") :_("Next move:"),
1443 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1445 h
= pgn_history_by_n(g
->hp
, g
->hindex
- 1);
1446 snprintf(buf
, sizeof(buf
), "%s",
1447 (h
&& h
->move
) ? h
->move
1448 : (LINES
< 24) ? _("empty") : _("not available"));
1451 if (h
&& ((h
->comment
) || h
->nag
[0])) {
1452 strncat(buf
, _(" (Annotated"), sizeof(buf
)-1);
1457 strncat(buf
, (n
) ? ",+" : " (+", sizeof(buf
)-1);
1462 strncat(buf
, (n
) ? ",-" : " (-", sizeof(buf
)-1);
1467 strncat(buf
, ")", sizeof(buf
)-1);
1469 mvwprintw(historyw
, ((LINES
< 24) ? 3 : 4), 1, "%s %-*s",
1470 (LINES
< 24) ? _("Prev.:") : _("Prev move:"),
1471 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1474 void do_validate_move(char **move
)
1476 struct userdata_s
*d
= gp
->data
;
1480 if (TEST_FLAG(d
->flags
, CF_HUMAN
)) {
1481 if ((n
= pgn_parse_move(gp
, d
->b
, move
, &frfr
)) != E_PGN_OK
) {
1482 invalid_move(d
->n
+ 1, n
, *move
);
1486 strcpy(d
->pm_frfr
, frfr
);
1487 update_time_control(gp
);
1488 pgn_history_add(gp
, d
->b
, *move
);
1489 pgn_switch_turn(gp
);
1492 if ((n
= pgn_validate_move(gp
, d
->b
, move
, &frfr
)) != E_PGN_OK
) {
1493 invalid_move(d
->n
+ 1, n
, *move
);
1497 add_engine_command(gp
, ENGINE_THINKING
, "%s\n",
1498 (config
.engine_protocol
== 1) ? frfr
: *move
);
1501 d
->sp
.srow
= d
->sp
.scol
= d
->sp
.icon
= 0;
1503 if (config
.validmoves
)
1504 pgn_reset_valid_moves(d
->b
);
1506 if (TEST_FLAG(gp
->flags
, GF_GAMEOVER
))
1507 d
->mode
= MODE_HISTORY
;
1509 SET_FLAG(d
->flags
, CF_MODIFIED
);
1513 update_history_window(gp
);
1514 update_board_window(gp
);
1518 void do_promotion_piece_finalize(WIN
*win
)
1520 char *p
, *str
= win
->data
;
1522 if (pgn_piece_to_int(win
->c
) == -1)
1525 p
= str
+ strlen(str
);
1526 *p
++ = toupper(win
->c
);
1528 do_validate_move(&str
);
1533 static void move_to_engine(GAME g
)
1535 struct userdata_s
*d
= g
->data
;
1539 if (config
.validmoves
&&
1540 !d
->b
[RANKTOBOARD(d
->sp
.row
)][FILETOBOARD(d
->sp
.col
)].valid
)
1543 str
= Malloc(MAX_SAN_MOVE_LEN
+ 1);
1544 snprintf(str
, MAX_SAN_MOVE_LEN
+ 1, "%c%i%c%i",
1545 _("abcdefgh")[d
->sp
.scol
- 1],
1546 d
->sp
.srow
, _("abcdefgh")[d
->sp
.col
- 1], d
->sp
.row
);
1548 piece
= pgn_piece_to_int(d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
);
1550 if (piece
== PAWN
&& (d
->sp
.row
== 8 || d
->sp
.row
== 1)) {
1551 construct_message(_("Select Pawn Promotion Piece"), _ ("R/N/B/Q"), 1, 1, NULL
, NULL
,
1552 str
, do_promotion_piece_finalize
, 0, 0, "%s", _("R = Rook, N = Knight, B = Bishop, Q = Queen"));
1556 do_validate_move(&str
);
1560 static char *clock_to_char(long n
)
1562 static char buf
[16];
1563 int h
= 0, m
= 0, s
= 0;
1566 m
= (n
% 3600) / 60;
1567 s
= (n
% 3600) % 60;
1568 snprintf(buf
, sizeof(buf
), "%.2i:%.2i:%.2i", h
, m
, s
);
1572 static char *timeval_to_char(struct timeval t
, long limit
)
1575 int h
= 0, m
= 0, s
= 0;
1576 int n
= limit
? abs(limit
- t
.tv_sec
) : 0;
1579 m
= (n
% 3600) / 60;
1580 s
= (n
% 3600) % 60;
1581 snprintf(buf
, sizeof(buf
), "%.2i:%.2i:%.2i", h
, m
, s
);
1585 static char *time_control_status(struct clock_s
*clk
)
1587 static char buf
[80] = {0};
1591 if (clk
->tc
[clk
->tcn
][0] && clk
->tc
[clk
->tcn
+ 1][1])
1592 snprintf(buf
, sizeof(buf
), " M%.2i/%s", abs(clk
->tc
[clk
->tcn
][0] - clk
->move
),
1593 clock_to_char(clk
->tc
[clk
->tcn
+ 1][1]));
1594 else if (!clk
->incr
)
1599 strncat(buf
, " I", sizeof(buf
)-1);
1600 strncat(buf
, itoa(clk
->incr
, buf
), sizeof(buf
)-1);
1606 void update_status_window(GAME g
)
1610 char tmp
[15] = {0}, *engine
, *mode
;
1616 struct userdata_s
*d
= g
->data
;
1620 if (!curses_initialized
)
1623 getmaxyx(statusw
, maxy
, maxx
);
1630 wchar_t *loadfilew
= loadfile
[0] ? str_etc (loadfile
, w
, 1) : str_to_wchar (_ ("not available"));
1631 mvwprintw(statusw
, y
++, 1, "%*s %-*ls", 7, _("File:"), w
, loadfilew
);
1633 snprintf(buf
, len
, "%i %s %i", gindex
+ 1, _("of"), gtotal
);
1634 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Game:"), w
, buf
);
1639 if (config
.details
) {
1644 if (TEST_FLAG(d
->flags
, CF_DELETE
)) {
1652 if (TEST_FLAG(g
->flags
, GF_PERROR
)) {
1660 if (TEST_FLAG(d
->flags
, CF_MODIFIED
)) {
1668 pgn_config_get(PGN_STRICT_CASTLING
, &n
);
1678 if (TEST_FLAG(d
->flags
, CF_PERL
)) {
1688 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Flags:"), w
, (tmp
[0]) ? tmp
: "-");
1692 mode
= _("move history");
1701 mode
= _("(empty value)");
1705 snprintf(buf
, len
- 1, "%*s %s", 7, _("Mode:"), mode
);
1707 if (d
->mode
== MODE_PLAY
) {
1708 if (TEST_FLAG(d
->flags
, CF_HUMAN
))
1709 strncat(buf
, _(" (human/human)"), len
- 1);
1710 else if (TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
))
1711 strncat(buf
, _(" (engine/engine)"), len
- 1);
1713 strncat(buf
, (d
->play_mode
== PLAY_EH
) ?
1714 _(" (engine/human)") : _(" (human/engine)"), len
- 1);
1718 mvwprintw(statusw
, y
++, 1, "%-*s", len
, buf
);
1722 switch (d
->engine
->status
) {
1723 case ENGINE_THINKING
:
1724 engine
= _("pondering...");
1727 engine
= _("ready");
1729 case ENGINE_INITIALIZING
:
1730 engine
= _("initializing...");
1732 case ENGINE_OFFLINE
:
1733 engine
= _("offline");
1736 engine
= _("(empty value)");
1741 engine
= _("offline");
1743 mvwprintw(statusw
, y
, 1, "%*s %-*s", 7, _("Engine:"), w
, " ");
1744 wattron(statusw
, CP_STATUS_ENGINE
);
1745 mvwaddstr(statusw
, y
++, 9, engine
);
1746 wattroff(statusw
, CP_STATUS_ENGINE
);
1748 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Turn:"), w
,
1749 (g
->turn
== WHITE
) ? _("white") : _("black"));
1751 strncpy(tmp
, _("white"), sizeof(tmp
)-1);
1752 tmp
[0] = toupper(tmp
[0]);
1753 snprintf(t
, sizeof(t
), "%s%s",
1754 timeval_to_char(d
->wclock
.elapsed
, d
->wclock
.tc
[d
->wclock
.tcn
][1]),
1755 time_control_status(&d
->wclock
));
1756 mvwprintw(statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1758 strncpy(tmp
, _("black"), sizeof(tmp
)-1);
1759 tmp
[0] = toupper(tmp
[0]);
1760 snprintf(t
, sizeof(t
), "%s%s",
1761 timeval_to_char(d
->bclock
.elapsed
, d
->bclock
.tc
[d
->bclock
.tcn
][1]),
1762 time_control_status(&d
->bclock
));
1763 mvwprintw(statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1765 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Total:"), w
,
1766 clock_to_char(d
->elapsed
.tv_sec
));
1768 // for (i = 0; i < STATUS_WIDTH; i++)
1769 // mvwprintw(stdscr, STATUS_HEIGHT, i, " ");
1772 status
.notify
= str_to_wchar(_("Type F1 for help"));
1774 wattron(stdscr
, CP_STATUS_NOTIFY
);
1775 for (i
= (config
.boardleft
) ? BOARD_WIDTH
: 0;
1776 i
< ((config
.boardleft
) ? COLS
: STATUS_WIDTH
); i
++)
1777 mvwprintw(stdscr
, STATUS_HEIGHT
, i
, " ");
1778 mvwprintw(stdscr
, STATUS_HEIGHT
, CENTERX(STATUS_WIDTH
, status
.notify
)
1779 + ((config
.boardleft
) ? BOARD_WIDTH
: 0),
1780 "%ls", status
.notify
);
1781 wattroff(stdscr
, CP_STATUS_NOTIFY
);
1784 wchar_t *translate_tag_name(const char *tag
)
1786 if (!strcmp (tag
, "Event"))
1787 return str_to_wchar (translatable_tag_names
[0]);
1788 else if (!strcmp (tag
, "Site"))
1789 return str_to_wchar (translatable_tag_names
[1]);
1790 else if (!strcmp (tag
, "Date"))
1791 return str_to_wchar (translatable_tag_names
[2]);
1792 else if (!strcmp (tag
, "Round"))
1793 return str_to_wchar (translatable_tag_names
[3]);
1794 else if (!strcmp (tag
, "White"))
1795 return str_to_wchar (translatable_tag_names
[4]);
1796 else if (!strcmp (tag
, "Black"))
1797 return str_to_wchar (translatable_tag_names
[5]);
1798 else if (!strcmp (tag
, "Result"))
1799 return str_to_wchar (translatable_tag_names
[6]);
1801 return str_to_wchar (tag
);
1804 void update_tag_window(TAG
**t
)
1809 for (i
= 0; t
[i
]; i
++) {
1810 wchar_t *namewc
= translate_tag_name(t
[i
]->name
);
1818 w
= TAG_WIDTH
- namel
- 4;
1820 for (i
= 0; t
[i
] && i
< TAG_HEIGHT
- 3; i
++) {
1821 wchar_t *namewc
= translate_tag_name(t
[i
]->name
);
1822 wchar_t *valuewc
= str_etc(t
[i
]->value
, w
, 0);
1824 mvwprintw(tagw
, (i
+ 2), 1, "%*ls: %-*ls", namel
, namewc
, w
, valuewc
);
1829 for (; i
< TAG_HEIGHT
- 3; i
++)
1830 mvwprintw(tagw
, (i
+ 2), 1, "%*s", namel
+ w
+ 2, " ");
1833 void append_enginebuf(GAME g
, char *line
)
1836 struct userdata_s
*d
= g
->data
;
1838 if (d
->engine
->enginebuf
)
1839 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++);
1841 if (i
>= LINES
- 3) {
1842 free(d
->engine
->enginebuf
[0]);
1844 for (i
= 0; d
->engine
->enginebuf
[i
+1]; i
++)
1845 d
->engine
->enginebuf
[i
] = d
->engine
->enginebuf
[i
+1];
1847 d
->engine
->enginebuf
[i
] = strdup(line
);
1850 d
->engine
->enginebuf
= Realloc(d
->engine
->enginebuf
, (i
+ 2) * sizeof(char *));
1851 d
->engine
->enginebuf
[i
++] = strdup(line
);
1852 d
->engine
->enginebuf
[i
] = NULL
;
1856 void update_engine_window(GAME g
)
1859 struct userdata_s
*d
= g
->data
;
1861 wmove(enginew
, 0, 0);
1864 if (d
->engine
&& d
->engine
->enginebuf
) {
1865 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++)
1866 mvwprintw(enginew
, i
+ 2, 1, "%s", d
->engine
->enginebuf
[i
]);
1869 window_draw_title(enginew
, _("Engine IO Window"), COLS
, CP_MESSAGE_TITLE
,
1873 void update_all(GAME g
)
1875 struct userdata_s
*d
= g
->data
;
1878 * In the middle of a macro. Don't update the screen.
1880 if (macro_match
!= -1)
1883 wmove(boardw
, ROWTOMATRIX(d
->c_row
), COLTOMATRIX(d
->c_col
));
1884 update_board_window(g
);
1885 update_status_window(g
);
1886 update_history_window(g
);
1887 update_tag_window(g
->tag
);
1888 update_engine_window(g
);
1893 static void game_next_prev(GAME g
, int n
, int count
)
1899 if (gindex
+ count
> gtotal
- 1) {
1901 gindex
= gtotal
- 1;
1909 if (gindex
- count
< 0) {
1913 gindex
= gtotal
- 1;
1922 static void delete_game(int which
)
1925 struct userdata_s
*d
;
1927 for (i
= 0; i
< gtotal
; i
++) {
1930 if (i
== w
|| TEST_FLAG(d
->flags
, CF_DELETE
)) {
1933 free_userdata_once(game
[i
]);
1936 for (n
= i
; n
+1 < gtotal
; n
++)
1937 game
[n
] = game
[n
+1];
1946 if (which
+ 1 >= gtotal
)
1947 gindex
= gtotal
- 1;
1952 gindex
= gtotal
- 1;
1955 gp
->hp
= gp
->history
;
1959 * FIXME find across multiple games.
1961 static int find_move_exp(GAME g
, regex_t r
, int which
, int count
)
1969 incr
= (which
== 0) ? -1 : 1;
1971 for (i
= g
->hindex
+ incr
- 1, found
= 0; ; i
+= incr
) {
1972 if (i
== g
->hindex
- 1)
1975 if (i
>= pgn_history_total(g
->hp
))
1978 i
= pgn_history_total(g
->hp
) - 1;
1981 ret
= regexec(&r
, g
->hp
[i
]->move
, 0, 0, 0);
1984 if (count
== ++found
) {
1989 if (ret
!= REG_NOMATCH
) {
1990 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
1991 cmessage(_("Error Matching Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
2000 static int toggle_delete_flag(int n
)
2003 struct userdata_s
*d
= game
[n
]->data
;
2005 TOGGLE_FLAG(d
->flags
, CF_DELETE
);
2008 for (i
= x
= 0; i
< gtotal
; i
++) {
2011 if (TEST_FLAG(d
->flags
, CF_DELETE
))
2016 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
2018 CLEAR_FLAG(d
->flags
, CF_DELETE
);
2025 static int find_game_exp(char *str
, int which
, int count
)
2027 char *nstr
= NULL
, *exp
= NULL
;
2031 char buf
[255] = {0}, *tmp
;
2034 int incr
= (which
== 0) ? -(1) : 1;
2036 strncpy(buf
, str
, sizeof(buf
)-1);
2039 if (strstr(tmp
, ":") != NULL
) {
2040 nstr
= strsep(&tmp
, ":");
2042 if ((ret
= regcomp(&nexp
, nstr
,
2043 REG_ICASE
|REG_EXTENDED
|REG_NOSUB
)) != 0) {
2044 regerror(ret
, &nexp
, errbuf
, sizeof(errbuf
));
2045 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
2053 while (exp
&& *exp
&& isspace(*exp
))
2059 if ((ret
= regcomp(&vexp
, exp
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
2060 regerror(ret
, &vexp
, errbuf
, sizeof(errbuf
));
2061 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
2068 for (g
= gindex
+ incr
, found
= 0; ; g
+= incr
) {
2079 for (t
= 0; game
[g
]->tag
[t
]; t
++) {
2081 if (regexec(&nexp
, game
[g
]->tag
[t
]->name
, 0, 0, 0) == 0) {
2082 if (regexec(&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0) {
2083 if (count
== ++found
) {
2091 if (regexec(&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0) {
2092 if (count
== ++found
) {
2114 * Updates the notification line in the status window then refreshes the
2117 void update_status_notify(GAME g
, char *fmt
, ...)
2120 #ifdef HAVE_VASPRINTF
2126 free(status
.notify
);
2127 status
.notify
= NULL
;
2133 #ifdef HAVE_VASPRINTF
2134 vasprintf(&line
, fmt
, ap
);
2136 vsnprintf(line
, sizeof(line
), fmt
, ap
);
2140 status
.notify
= str_to_wchar(line
);
2142 #ifdef HAVE_VASPRINTF
2147 int rav_next_prev(GAME g
, BOARD b
, int n
)
2151 if ((!g
->ravlevel
&& g
->hindex
&& g
->hp
[g
->hindex
- 1]->rav
== NULL
) ||
2152 (!g
->ravlevel
&& !g
->hindex
&& g
->hp
[g
->hindex
]->rav
== NULL
) ||
2153 (g
->ravlevel
&& g
->hp
[g
->hindex
]->rav
== NULL
))
2156 g
->rav
= Realloc(g
->rav
, (g
->ravlevel
+ 1) * sizeof(RAV
));
2157 g
->rav
[g
->ravlevel
].hp
= g
->hp
;
2158 g
->rav
[g
->ravlevel
].flags
= g
->flags
;
2159 g
->rav
[g
->ravlevel
].fen
= pgn_game_to_fen(g
, b
);
2160 g
->rav
[g
->ravlevel
].hindex
= g
->hindex
;
2161 g
->hp
= (!g
->ravlevel
) ? (g
->hindex
) ? g
->hp
[g
->hindex
- 1]->rav
: g
->hp
[g
->hindex
]->rav
: g
->hp
[g
->hindex
]->rav
;
2164 pgn_board_update(g
, b
, g
->hindex
+ 1);
2168 if (g
->ravlevel
- 1 < 0)
2173 pgn_board_init_fen(g
, b
, g
->rav
[g
->ravlevel
].fen
);
2174 free(g
->rav
[g
->ravlevel
].fen
);
2175 g
->hp
= g
->rav
[g
->ravlevel
].hp
;
2176 g
->flags
= g
->rav
[g
->ravlevel
].flags
;
2177 g
->hindex
= g
->rav
[g
->ravlevel
].hindex
;
2181 static void draw_window_decor()
2183 move_panel(boardp
, 0,
2184 (config
.boardleft
) ? 0 : COLS
- BOARD_WIDTH
);
2185 move_panel(historyp
, LINES
- HISTORY_HEIGHT
,
2186 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
: 0 :
2187 (MEGA_BOARD
) ? 0 : COLS
- HISTORY_WIDTH
);
2188 move_panel(statusp
, 0,
2189 (config
.boardleft
) ? BOARD_WIDTH
: 0);
2190 move_panel(tagp
, STATUS_HEIGHT
+ 1,
2191 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
:
2194 wbkgd(boardw
, CP_BOARD_WINDOW
);
2195 wbkgd(statusw
, CP_STATUS_WINDOW
);
2196 window_draw_title(statusw
, _("Game Status"), STATUS_WIDTH
,
2197 CP_STATUS_TITLE
, CP_STATUS_BORDER
);
2198 wbkgd(tagw
, CP_TAG_WINDOW
);
2199 window_draw_title(tagw
, _("Roster Tags"), TAG_WIDTH
, CP_TAG_TITLE
,
2201 wbkgd(historyw
, CP_HISTORY_WINDOW
);
2202 window_draw_title(historyw
, _("Move History"), HISTORY_WIDTH
,
2203 CP_HISTORY_TITLE
, CP_HISTORY_BORDER
);
2206 void do_window_resize()
2208 if (LINES
< 23 || COLS
< 74)
2211 resizeterm(LINES
, COLS
);
2213 wresize(boardw
, BOARD_HEIGHT
, BOARD_WIDTH
);
2214 wresize(historyw
, HISTORY_HEIGHT
, HISTORY_WIDTH
);
2215 wresize(statusw
, STATUS_HEIGHT
, STATUS_WIDTH
);
2216 wresize(tagw
, TAG_HEIGHT
, TAG_WIDTH
);
2224 draw_window_decor();
2226 keypad(boardw
, TRUE
);
2232 void do_global_redraw ()
2234 do_window_resize ();
2239 memset(&clock_timer
, 0, sizeof(struct itimerval
));
2240 setitimer(ITIMER_REAL
, &clock_timer
, NULL
);
2243 void start_clock(GAME g
)
2245 struct userdata_s
*d
= g
->data
;
2247 if (clock_timer
.it_interval
.tv_usec
)
2250 memset(&d
->elapsed
, 0, sizeof(struct timeval
));
2251 clock_timer
.it_value
.tv_sec
= 0;
2252 clock_timer
.it_value
.tv_usec
= 100000;
2253 clock_timer
.it_interval
.tv_sec
= 0;
2254 clock_timer
.it_interval
.tv_usec
= 100000;
2255 setitimer(ITIMER_REAL
, &clock_timer
, NULL
);
2258 static void update_clocks()
2261 struct userdata_s
*d
;
2262 struct itimerval it
;
2265 getitimer(ITIMER_REAL
, &it
);
2267 for (i
= 0; i
< gtotal
; i
++) {
2270 if (d
&& d
->mode
== MODE_PLAY
) {
2271 if (d
->paused
== 1 || TEST_FLAG(d
->flags
, CF_NEW
))
2273 else if (d
->paused
== -1) {
2274 if (game
[i
]->side
== game
[i
]->turn
) {
2280 update_clock(game
[i
], it
);
2288 update_status_window(gp
);
2294 #define SKIP_SPACE(str) { while (isspace(*str)) str++; }
2296 static int parse_clock_time(char **str
)
2351 static int parse_clock_input(struct clock_s
*clk
, char *str
, int *incr
)
2373 memset(clk
, 0, sizeof(struct clock_s
));
2377 if (strncasecmp(p
, "SD", 2) == 0) {
2383 n
= parse_clock_time(&p
);
2397 /* Sudden death without a previous time control. */
2403 n
= parse_clock_time(&p
);
2409 message(ERROR_STR
, ANY_KEY_STR
, "%s (%i)", _("Maximum number of time controls reached"), MAX_TC
);
2414 clk
->tc
[tc
++][1] = n
;
2429 clk
->tc
[clk
->tcn
][1] = (n
<= clk
->elapsed
.tv_sec
) ? clk
->elapsed
.tv_sec
+ n
: n
;
2437 n
= parse_clock_time(&p
);
2457 static int parse_which_clock(struct clock_s
*clk
, char *str
)
2462 memcpy(&tmp
, clk
, sizeof(struct clock_s
));
2464 if (parse_clock_input(&tmp
, str
, &incr
)) {
2465 cmessage(ERROR_STR
, ANY_KEY_STR
, _("Invalid clock specification"));
2469 memcpy(clk
, &tmp
, sizeof(struct clock_s
));
2470 clk
->tc
[clk
->tcn
][1] += incr
;
2474 void do_clock_input_finalize(WIN
*win
)
2476 struct userdata_s
*d
= gp
->data
;
2477 struct input_data_s
*in
= win
->data
;
2487 if (tolower(*p
) == 'w') {
2490 if (parse_which_clock(&d
->wclock
, p
))
2493 else if (tolower(*p
) == 'b') {
2496 if (parse_which_clock(&d
->bclock
, p
))
2500 if (parse_which_clock(&d
->wclock
, p
))
2503 if (parse_which_clock(&d
->bclock
, p
))
2507 if (!d
->wclock
.tc
[0][1] && !d
->bclock
.tc
[0][1])
2508 CLEAR_FLAG(d
->flags
, CF_CLOCK
);
2510 SET_FLAG(d
->flags
, CF_CLOCK
);
2517 void do_engine_command_finalize(WIN
*win
)
2519 struct userdata_s
*d
= gp
->data
;
2520 struct input_data_s
*in
= win
->data
;
2531 x
= d
->engine
->status
;
2532 send_to_engine(gp
, -1, "%s\n", in
->str
);
2533 d
->engine
->status
= x
;
2540 void do_board_details()
2542 config
.details
= (config
.details
) ? 0 : 1;
2545 void do_toggle_strict_castling()
2549 pgn_config_get(PGN_STRICT_CASTLING
, &n
);
2552 pgn_config_set(PGN_STRICT_CASTLING
, 1);
2554 pgn_config_set(PGN_STRICT_CASTLING
, 0);
2557 void do_play_set_clock()
2559 struct input_data_s
*in
;
2561 in
= Calloc(1, sizeof(struct input_data_s
));
2562 in
->efunc
= do_clock_input_finalize
;
2563 construct_input(_("Set Clock"), NULL
, 1, 1,
2564 _ ("Format: [W | B] [+]T[+I] | ++I | M/T [M/T [...] [SD/T]] [+I]\n" \
2565 "T = time (hms), I = increment, M = moves per, SD = sudden death\ne.g., 30m or 4m+12s or 35/90m SD/30m"),
2566 NULL
, NULL
, 0, in
, INPUT_HIST_CLOCK
, -1);
2569 void do_play_toggle_human()
2571 struct userdata_s
*d
= gp
->data
;
2573 TOGGLE_FLAG(d
->flags
, CF_HUMAN
);
2575 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && pgn_history_total(gp
->hp
)) {
2576 if (init_chess_engine(gp
))
2580 CLEAR_FLAG(d
->flags
, CF_ENGINE_LOOP
);
2583 d
->engine
->status
= ENGINE_READY
;
2586 void do_play_toggle_engine()
2588 struct userdata_s
*d
= gp
->data
;
2590 TOGGLE_FLAG(d
->flags
, CF_ENGINE_LOOP
);
2591 CLEAR_FLAG(d
->flags
, CF_HUMAN
);
2593 if (d
->engine
&& TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
)) {
2594 char *fen
= pgn_game_to_fen (gp
, d
->b
);
2596 pgn_board_update(gp
, d
->b
,
2597 pgn_history_total(gp
->hp
));
2598 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
2604 * This will send a command to the engine skipping the command queue.
2606 void do_play_send_command()
2608 struct userdata_s
*d
= gp
->data
;
2609 struct input_data_s
*in
;
2611 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
) {
2612 if (init_chess_engine(gp
))
2616 in
= Calloc(1, sizeof(struct input_data_s
));
2617 in
->efunc
= do_engine_command_finalize
;
2618 construct_input(_("Engine Command"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
, INPUT_HIST_ENGINE
, -1);
2621 void do_play_switch_turn()
2623 struct userdata_s *d = gp->data;
2625 pgn_switch_side(gp);
2626 pgn_switch_turn(gp);
2628 if (!TEST_FLAG(d->flags, CF_HUMAN))
2629 add_engine_command(gp, -1,
2630 (gp->side == WHITE) ? "white\n" : "black\n");
2632 update_status_window(gp);
2635 void do_play_toggle_eh_mode()
2637 struct userdata_s
*d
= gp
->data
;
2639 if (!TEST_FLAG(d
->flags
, CF_HUMAN
)) {
2641 pgn_switch_side(gp
, TRUE
);
2642 d
->play_mode
= (d
->play_mode
) ? PLAY_HE
: PLAY_EH
;
2643 if (gp
->side
== BLACK
)
2644 update_status_notify(gp
, _("Press 'g' to start the game"));
2646 d
->rotate
= !d
->rotate
;
2649 message(NULL
, ANY_KEY_STR
,
2650 _("You may only switch sides at the start of the \n"
2651 "game. Press ^K or ^N to begin a new game."));
2657 struct userdata_s
*d
= gp
->data
;
2659 if (!pgn_history_total(gp
->hp
))
2663 if (gp
->hindex
- keycount
< 0)
2667 gp
->hindex
-= (keycount
* 2) -1;
2669 gp
->hindex
-= keycount
* 2;
2673 if (gp
->hindex
- 2 < 0)
2683 pgn_history_free(gp
->hp
, gp
->hindex
);
2684 gp
->hindex
= pgn_history_total(gp
->hp
);
2685 pgn_board_update(gp
, d
->b
, gp
->hindex
);
2687 if (d
->engine
&& d
->engine
->status
== ENGINE_READY
) {
2688 char *fen
= pgn_game_to_fen(gp
, d
->b
);
2690 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
2692 d
->engine
->status
= ENGINE_READY
;
2695 update_history_window(gp
);
2698 pgn_switch_side(gp
, FALSE
);
2705 void do_play_toggle_pause()
2707 struct userdata_s
*d
= gp
->data
;
2709 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && gp
->turn
!=
2715 d
->paused
= (d
->paused
) ? 0 : 1;
2720 struct userdata_s
*d
= gp
->data
;
2722 if (TEST_FLAG(d
->flags
, CF_HUMAN
))
2725 if (fm_loaded_file
&& gp
->side
!= gp
->turn
) {
2726 pgn_switch_side(gp
, FALSE
);
2727 add_engine_command(gp
, ENGINE_THINKING
, "black\n");
2730 add_engine_command(gp
, ENGINE_THINKING
, "go\n");
2732 // Completa la función para que permita seguir jugando al usarla.
2733 // Complete the function to allow continue playing when using.
2734 if (gp
->side
== gp
->turn
)
2735 pgn_switch_side(gp
, FALSE
);
2740 void do_play_config_command()
2745 for (x
= 0; config
.keys
[x
]; x
++) {
2746 if (config
.keys
[x
]->c
== input_c
) {
2747 switch (config
.keys
[x
]->type
) {
2749 add_engine_command(gp
, -1, "%ls\n",
2750 config
.keys
[x
]->str
);
2756 add_engine_command(gp
, -1,
2757 "%ls %i\n", config
.keys
[x
]->str
, keycount
);
2764 for (w
= 0; w
< keycount
; w
++)
2765 add_engine_command(gp
, -1,
2766 "%ls\n", config
.keys
[x
]->str
);
2774 update_status_notify(gp
, NULL
);
2777 void do_play_cancel_selected()
2779 struct userdata_s
*d
= gp
->data
;
2781 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
2783 pgn_reset_valid_moves(d
->b
);
2784 update_status_notify(gp
, NULL
);
2787 void do_play_commit()
2789 struct userdata_s
*d
= gp
->data
;
2791 pushkey
= keycount
= 0;
2792 update_status_notify(gp
, NULL
);
2794 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) &&
2795 (!d
->engine
|| d
->engine
->status
== ENGINE_THINKING
))
2801 d
->sp
.row
= d
->c_row
;
2802 d
->sp
.col
= d
->c_col
;
2805 rotate_position(&d
->sp
.row
, &d
->sp
.col
);
2806 rotate_position(&d
->sp
.srow
, &d
->sp
.scol
);
2811 // Completa la función para que permita seguir jugando cuando se carga un
2812 // archivo pgn (con juego no terminado) que inicie con turno del lado
2814 // Complete the function to allow continue playing when loading a file
2815 // pgn (with unfinished game) you start to turn black side.
2816 if (gp
->side
!= gp
->turn
)
2817 pgn_switch_side(gp
, FALSE
);
2819 if (d
->rotate
&& d
->sp
.icon
)
2820 rotate_position(&d
->sp
.srow
, &d
->sp
.scol
);
2822 // Envia comando 'go' a Polyglot en el primer movimiento debido a que
2823 // polyglot no envia el movimiento y cboard se queda esperando.
2824 // Send command 'go' to the first movement Polyglot because cboard
2825 // waits to send polyglot movement and this does not make.
2826 if (config
.fmpolyglot
&&
2827 ((gp
->side
== WHITE
&& !gp
->hindex
) || fm_loaded_file
))
2828 add_engine_command(gp
, ENGINE_THINKING
, "go\n");
2831 fm_loaded_file
= FALSE
;
2835 void do_play_select()
2837 struct userdata_s
*d
= gp
->data
;
2839 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && (!d
->engine
||
2840 d
->engine
->status
== ENGINE_OFFLINE
)) {
2841 if (init_chess_engine(gp
))
2845 if (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
)
2849 do_play_cancel_selected ();
2852 rotate_position(&d
->c_row
, &d
->c_col
);
2854 d
->sp
.icon
= d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
;
2856 if (pgn_piece_to_int(d
->sp
.icon
) == OPEN_SQUARE
) {
2861 if (((islower(d
->sp
.icon
) && gp
->turn
!= BLACK
)
2862 || (isupper(d
->sp
.icon
) && gp
->turn
!= WHITE
))) {
2864 char *str
= Malloc (512);
2866 for (k
= play_keys
; *k
; k
++) {
2867 if ((*k
)->f
== do_play_toggle_eh_mode
)
2871 snprintf (str
, 512, _("It is not your turn to move. You may switch playing sides by pressing \"%lc\"."),
2872 *k
? (*k
)->c
: '?');
2873 message(NULL
, ANY_KEY_STR
, "%s", str
);
2878 if (pgn_history_total(gp
->hp
)) {
2879 message(NULL
, ANY_KEY_STR
, "%s", _("It is not your turn to move. You can switch sides "));
2884 if (pgn_tag_find(gp
->tag
, "FEN") != E_PGN_ERR
)
2887 add_engine_command(gp
, ENGINE_READY
, "black\n");
2888 pgn_switch_turn(gp
);
2890 if (gp
->side
!= BLACK
)
2891 pgn_switch_side(gp
);
2896 d
->sp
.srow
= d
->c_row
;
2897 d
->sp
.scol
= d
->c_col
;
2899 if (config
.validmoves
)
2900 pgn_find_valid_moves(gp
, d
->b
, d
->sp
.scol
, d
->sp
.srow
);
2903 rotate_position(&d
->c_row
, &d
->c_col
);
2904 rotate_position(&d
->sp
.srow
, &d
->sp
.scol
);
2907 CLEAR_FLAG(d
->flags
, CF_NEW
);
2911 static wchar_t *build_help(struct key_s
**keys
)
2913 int i
, nlen
= 1, len
, t
, n
;
2914 wchar_t *buf
= NULL
, *wc
= NULL
;
2920 for (i
= len
= t
= 0; keys
[i
]; i
++) {
2925 if (wcslen(keys
[i
]->key
) > nlen
) {
2926 nlen
= wcslen(keys
[i
]->key
);
2936 if (wcslen(keys
[i
]->d
) > len
)
2937 len
= wcslen(keys
[i
]->d
);
2945 buf
= Malloc((t
+1)*sizeof(wchar_t));
2948 for (i
= 0; keys
[i
]; i
++) {
2953 n
= wcslen(keys
[i
]->key
);
2963 wcsncat(buf
, keys
[i
]->key
, t
-1);
2964 p
= buf
+ wcslen(buf
);
2975 wcsncat(buf
, keys
[i
]->d
, t
-1);
2978 wc
= str_to_wchar ("*");
2979 wcsncat(buf
, wc
, t
-1);
2983 wc
= str_to_wchar ("\n");
2986 p
= buf
+ wcslen(buf
);
2992 void do_global_help ()
2994 wchar_t *buf
= build_help(global_keys
);
2996 construct_message(_("Global Game Keys (* = can take a repeat count)"),
2997 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
, buf
, do_more_help
,
3001 void do_main_help(WIN
*win
)
3021 static void do_more_help(WIN
*win
)
3023 if (win
->c
== KEY_F(1) || win
->c
== CTRL_KEY('g'))
3024 construct_message(_("Command Key Index"),
3025 _ ("p/h/e/g or any other key to quit"), 0, 0,
3026 NULL
, NULL
, NULL
, do_main_help
, 0, 0, "%s",
3028 "p - play mode keys\n"
3029 "h - history mode keys\n"
3030 "e - board edit mode keys\n"
3031 "g - global game keys"
3037 wchar_t *buf
= build_help(play_keys
);
3039 construct_message(_("Play Mode Keys (* = can take a repeat count)"),
3040 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
,
3041 buf
, do_more_help
, 0, 1, "%ls", buf
);
3044 void do_play_history_mode()
3046 struct userdata_s
*d
= gp
->data
;
3048 if (!pgn_history_total(gp
->hp
) ||
3049 (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
))
3052 d
->mode
= MODE_HISTORY
;
3053 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3056 void do_play_edit_mode()
3058 struct userdata_s
*d
= gp
->data
;
3060 if (pgn_history_total(gp
->hp
))
3063 pgn_board_init_fen(gp
, d
->b
, NULL
);
3065 d
->mode
= MODE_EDIT
;
3068 void do_edit_insert_finalize(WIN
*win
)
3070 struct userdata_s
*d
= win
->data
;
3072 if (pgn_piece_to_int(win
->c
) == -1)
3075 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
= win
->c
;
3078 void do_edit_select()
3080 struct userdata_s
*d
= gp
->data
;
3085 d
->sp
.icon
= d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
;
3087 if (pgn_piece_to_int(d
->sp
.icon
) == OPEN_SQUARE
) {
3092 d
->sp
.srow
= d
->c_row
;
3093 d
->sp
.scol
= d
->c_col
;
3096 void do_edit_commit()
3099 struct userdata_s
*d
= gp
->data
;
3101 pushkey
= keycount
= 0;
3102 update_status_notify(gp
, NULL
);
3107 d
->sp
.row
= d
->c_row
;
3108 d
->sp
.col
= d
->c_col
;
3109 p
= d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
;
3110 d
->b
[RANKTOBOARD(d
->sp
.row
)][FILETOBOARD(d
->sp
.col
)].icon
= p
;
3111 d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
=
3112 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3113 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3116 void do_edit_delete()
3118 struct userdata_s
*d
= gp
->data
;
3121 d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
=
3122 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3124 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
=
3125 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3127 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3130 void do_edit_cancel_selected()
3132 struct userdata_s
*d
= gp
->data
;
3134 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3136 update_status_notify(gp
, NULL
);
3139 void do_edit_switch_turn()
3141 pgn_switch_turn(gp
);
3144 void do_edit_toggle_castle()
3146 struct userdata_s
*d
= gp
->data
;
3148 castling_state(gp
, d
->b
, RANKTOBOARD(d
->c_row
),
3149 FILETOBOARD(d
->c_col
),
3150 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
, 1);
3153 void do_edit_insert()
3155 struct userdata_s
*d
= gp
->data
;
3157 construct_message(_("Insert Piece"), _("P=pawn, R=rook, N=knight, B=bishop, "), 0, 0, NULL
, NULL
,
3158 d
->b
, do_edit_insert_finalize
, 0, 0, "%s", _("Type the piece letter to insert. Lowercase "));
3161 void do_edit_enpassant()
3163 struct userdata_s
*d
= gp
->data
;
3165 if (d
->c_row
== 6 || d
->c_row
== 3) {
3166 pgn_reset_enpassant(d
->b
);
3167 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].enpassant
= 1;
3173 wchar_t *buf
= build_help(edit_keys
);
3175 construct_message(_("Edit Mode Keys (* = can take a repeat count)"),
3176 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
, buf
, do_more_help
,
3182 struct userdata_s
*d
= gp
->data
;
3183 char *fen
= pgn_game_to_fen(gp
, d
->b
);
3186 pgn_tag_add(&gp
->tag
, "FEN", fen
);
3188 pgn_tag_add(&gp
->tag
, "SetUp", "1");
3189 pgn_tag_sort(gp
->tag
);
3190 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3191 d
->mode
= MODE_PLAY
;
3194 void really_do_annotate_finalize(struct input_data_s
*in
,
3195 struct userdata_s
*d
)
3197 HISTORY
*h
= in
->data
;
3207 len
= strlen(in
->str
);
3208 h
->comment
= Realloc(h
->comment
, len
+1);
3209 strncpy(h
->comment
, in
->str
, len
);
3210 h
->comment
[len
] = 0;
3215 SET_FLAG(d
->flags
, CF_MODIFIED
);
3218 void do_annotate_finalize(WIN
*win
)
3220 struct userdata_s
*d
= gp
->data
;
3221 struct input_data_s
*in
= win
->data
;
3223 really_do_annotate_finalize(in
, d
);
3226 void do_find_move_exp_finalize(int init
, int which
)
3229 struct userdata_s
*d
= gp
->data
;
3230 static int firstrun
;
3235 if (init
|| !firstrun
) {
3239 if ((ret
= regcomp(&r
, moveexp
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
3240 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
3241 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
3248 if ((n
= find_move_exp(gp
, r
,
3249 (which
== -1) ? 0 : 1, (keycount
) ? keycount
: 1)) == -1)
3253 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3256 void do_find_move_exp(WIN
*win
)
3258 struct input_data_s
*in
= win
->data
;
3263 strncpy(moveexp
, in
->str
, sizeof(moveexp
)-1);
3264 moveexp
[sizeof(moveexp
)-1] = 0;
3265 do_find_move_exp_finalize(1, which
);
3273 void do_move_jump_finalize(int n
)
3275 struct userdata_s
*d
= gp
->data
;
3277 if (n
< 0 || n
> (pgn_history_total(gp
->hp
) / 2))
3281 update_status_notify(gp
, NULL
);
3282 gp
->hindex
= (n
) ? n
* 2 - 1 : n
* 2;
3283 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3286 void do_move_jump(WIN
*win
)
3288 struct input_data_s
*in
= win
->data
;
3290 if (!in
->str
|| !isinteger(in
->str
)) {
3298 do_move_jump_finalize(atoi(in
->str
));
3303 struct history_menu_s
{
3311 void free_history_menu_data(struct history_menu_s
**h
)
3318 for (i
= 0; h
[i
]; i
++) {
3326 void get_history_data(HISTORY
**hp
, struct history_menu_s
***menu
, int m
,
3330 int t
= pgn_history_total(hp
);
3331 char buf
[MAX_SAN_MOVE_LEN
+ 4];
3333 struct history_menu_s
**hmenu
= *menu
;
3336 for (n
= 0; hmenu
[n
]; n
++);
3340 for (i
= 0; i
< t
; i
++) {
3341 hmenu
= Realloc(hmenu
, (n
+ 2) * sizeof(struct history_menu_s
*));
3342 hmenu
[n
] = Malloc(sizeof(struct history_menu_s
));
3343 snprintf(buf
, sizeof(buf
), "%c%s%s", (turn
== WHITE
) ? 'W' : 'B',
3344 hp
[i
]->move
, (hp
[i
]->comment
|| hp
[i
]->nag
[0]) ? " !" : "");
3345 hmenu
[n
]->line
= strdup(buf
);
3346 hmenu
[n
]->hindex
= i
;
3347 hmenu
[n
]->indent
= 0;
3348 hmenu
[n
]->ravlevel
= depth
;
3349 hmenu
[n
]->move
= (n
&& depth
> hmenu
[n
-1]->ravlevel
) ? m
++ : m
;
3356 get_history_data(hp
[i
]->rav
, &hmenu
, m
, turn
);
3357 for (n
= 0; hmenu
[n
]; n
++);
3365 turn
= (turn
== WHITE
) ? BLACK
: WHITE
;
3371 void history_draw_update(struct menu_input_s
*m
)
3374 struct userdata_s
*d
= g
->data
;
3376 g
->hindex
= m
->selected
+ 1;
3377 update_cursor(g
, m
->selected
);
3378 pgn_board_update(g
, d
->b
, m
->selected
+ 1);
3381 struct menu_item_s
**get_history_items(WIN
*win
)
3383 struct menu_input_s
*m
= win
->data
;
3385 struct userdata_s
*d
= g
->data
;
3386 struct history_menu_s
**hm
= d
->data
;
3387 struct menu_item_s
**items
= m
->items
;
3391 get_history_data(g
->history
, &hm
, 0,
3392 TEST_FLAG(g
->flags
, GF_BLACK_OPENING
));
3393 m
->selected
= g
->hindex
- 1;
3395 if (m
->selected
< 0)
3398 m
->draw_exit_func
= history_draw_update
;
3404 for (i
= 0; items
[i
]; i
++)
3411 for (i
= 0; hm
[i
]; i
++) {
3412 items
= Realloc(items
, (i
+2) * sizeof(struct menu_item_s
*));
3413 items
[i
] = Malloc(sizeof(struct menu_item_s
));
3414 items
[i
]->name
= hm
[i
]->line
;
3415 items
[i
]->value
= NULL
;
3416 items
[i
]->selected
= 0;
3427 void history_menu_quit(struct menu_input_s
*m
)
3432 void history_menu_exit(WIN
*win
)
3435 struct userdata_s
*d
= g
->data
;
3436 struct history_menu_s
**hm
= d
->data
;
3442 for (i
= 0; hm
[i
]; i
++) {
3452 void history_menu_next(struct menu_input_s
*m
)
3455 struct userdata_s
*d
= g
->data
;
3456 struct history_menu_s
**hm
= d
->data
;
3459 for (t
= 0; hm
[t
]; t
++);
3461 if (m
->selected
+ 1 == t
)
3464 n
= hm
[m
->selected
+ 1]->hindex
;
3471 void history_menu_prev(struct menu_input_s
*m
)
3474 struct userdata_s
*d
= g
->data
;
3475 struct history_menu_s
**hm
= d
->data
;
3478 for (t
= 0; hm
[t
]; t
++);
3480 if (m
->selected
- 1 < 0)
3483 n
= hm
[m
->selected
- 1]->hindex
;
3489 void history_menu_help(struct menu_input_s
*m
)
3491 message(_("History Menu Help"), ANY_KEY_STR
, "%s",
3493 " UP/DOWN - previous/next menu item\n"
3494 " HOME/END - first/last menu item\n"
3495 " PGDN/PGUP - next/previous page\n"
3496 " a-zA-Z0-9 - jump to item\n"
3497 " CTRL-a - annotate the selected move\n"
3498 " ENTER - view annotation\n"
3499 " CTRL-d - toggle board details\n"
3500 " ESCAPE/M - return to move history"
3504 void do_annotate_move(HISTORY
*hp
)
3507 struct input_data_s
*in
;
3509 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Editing Annotation for"), hp
->move
);
3510 in
= Calloc(1, sizeof(struct input_data_s
));
3512 in
->efunc
= do_annotate_finalize
;
3513 construct_input(buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
3514 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
, CTRL_KEY('T'), in
, -1, -1);
3517 void history_menu_view_annotation(struct menu_input_s
*m
)
3522 view_annotation(g
->history
[m
->selected
]);
3525 void history_menu_annotate_finalize(WIN
*win
)
3527 struct input_data_s
*in
= win
->data
;
3528 GAME g
= in
->moredata
;
3529 struct userdata_s
*d
= g
->data
;
3530 struct history_menu_s
**hm
= d
->data
;
3532 really_do_annotate_finalize(in
, d
);
3533 free_history_menu_data(hm
);
3535 get_history_data(g
->history
, &hm
, 0, TEST_FLAG(g
->flags
, GF_BLACK_OPENING
));
3537 pushkey
= REFRESH_MENU
;
3540 void history_menu_annotate(struct menu_input_s
*m
)
3544 struct input_data_s
*in
;
3545 HISTORY
*hp
= g
->history
[m
->selected
]; // FIXME RAV
3547 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Editing Annotation for"), hp
->move
);
3548 in
= Calloc(1, sizeof(struct input_data_s
));
3550 in
->moredata
= m
->data
;
3551 in
->efunc
= history_menu_annotate_finalize
;
3552 construct_input(buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
3553 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
, CTRL_KEY('T'), in
, -1, -1);
3556 void history_menu_details(struct menu_input_s
*m
)
3562 void history_menu_print(WIN
*win
)
3564 struct menu_input_s
*m
= win
->data
;
3566 struct userdata_s
*d
= g
->data
;
3567 struct history_menu_s
**hm
= d
->data
;
3568 struct history_menu_s
*h
= hm
[m
->top
];
3570 char *p
= m
->item
->name
;
3571 int line
= m
->print_line
- 2;
3573 * Solaris 5.9 doesn't have wattr_get() or any function that requires an
3580 for (total
= 0; hm
[total
]; total
++);
3581 wattr_get(win
->w
, &attrs
, &pair
, NULL
);
3582 wattroff(win
->w
, COLOR_PAIR(pair
));
3583 mvwaddch(win
->w
, m
->print_line
, 1,
3584 *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
));
3587 if (h
->hindex
== 0 && line
== 0)
3588 waddch(win
->w
, ACS_ULCORNER
| CP_HISTORY_MENU_LG
);
3589 else if ((!hm
[h
->hindex
+ (win
->rows
- 5) + 1] && line
== win
->rows
- 5) ||
3590 (m
->top
+ line
== total
- 1))
3591 waddch(win
->w
, ACS_LLCORNER
| CP_HISTORY_MENU_LG
);
3592 else if (hm
[m
->top
+ 1]->ravlevel
!= h
->ravlevel
|| !h
->ravlevel
)
3593 waddch(win
->w
, ACS_LTEE
| CP_HISTORY_MENU_LG
);
3595 waddch(win
->w
, ACS_VLINE
| CP_HISTORY_MENU_LG
);
3597 wattron(win
->w
, COLOR_PAIR(pair
) | attrs
);
3599 for (i
= 2; *p
; p
++, i
++)
3600 waddch(win
->w
, (*p
== '!') ? *p
| A_BOLD
: *p
);
3602 while (i
++ < win
->cols
- 2)
3603 waddch(win
->w
, ' ');
3606 void history_menu(GAME g
)
3608 struct menu_key_s
**keys
= NULL
;
3610 add_menu_key(&keys
, KEY_ESCAPE
, history_menu_quit
);
3611 add_menu_key(&keys
, 'M', history_menu_quit
);
3612 add_menu_key(&keys
, KEY_UP
, history_menu_prev
);
3613 add_menu_key(&keys
, KEY_DOWN
, history_menu_next
);
3614 add_menu_key(&keys
, KEY_F(1), history_menu_help
);
3615 add_menu_key(&keys
, CTRL_KEY('a'), history_menu_annotate
);
3616 add_menu_key(&keys
, CTRL_KEY('d'), history_menu_details
);
3617 add_menu_key(&keys
, '\n', history_menu_view_annotation
);
3618 construct_menu(MEGA_BOARD
? LINES
- HISTORY_HEIGHT_MB
: LINES
,
3619 TAG_WIDTH
, 0, config
.boardleft
? BOARD_WIDTH
: 0,
3620 _("Move History Tree"), 1, get_history_items
, keys
, g
,
3621 history_menu_print
, history_menu_exit
);
3624 void do_history_menu()
3629 void do_history_half_move_toggle()
3631 movestep
= (movestep
== 1) ? 2 : 1;
3632 update_history_window(gp
);
3635 void do_history_rotate_board()
3637 struct userdata_s
*d
= gp
->data
;
3638 d
->rotate
= !d
->rotate
;
3641 void do_history_jump_next()
3643 struct userdata_s
*d
= gp
->data
;
3645 pgn_history_next(gp
, d
->b
, (keycount
> 0) ?
3646 config
.jumpcount
* keycount
* movestep
:
3647 config
.jumpcount
* movestep
);
3650 void do_history_jump_prev()
3652 struct userdata_s
*d
= gp
->data
;
3654 pgn_history_prev(gp
, d
->b
, (keycount
) ?
3655 config
.jumpcount
* keycount
* movestep
:
3656 config
.jumpcount
* movestep
);
3659 void do_history_prev()
3661 struct userdata_s
*d
= gp
->data
;
3663 pgn_history_prev(gp
, d
->b
,
3664 (keycount
) ? keycount
* movestep
: movestep
);
3667 void do_history_next()
3669 struct userdata_s
*d
= gp
->data
;
3671 pgn_history_next(gp
, d
->b
, (keycount
) ?
3672 keycount
* movestep
: movestep
);
3675 void do_history_mode_finalize(struct userdata_s
*d
)
3678 d
->mode
= MODE_PLAY
;
3681 void do_history_mode_confirm(WIN
*win
)
3683 struct userdata_s
*d
= gp
->data
;
3684 wchar_t str
[] = { win
->c
, 0 };
3686 if (!wcscmp (str
, resume_wchar
)) {
3687 pgn_history_free(gp
->hp
, gp
->hindex
);
3688 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3693 if (pgn_history_rav_new(gp
, d
->b
,
3694 gp
->hindex
) != E_PGN_OK
)
3702 if (!TEST_FLAG(d
->flags
, CF_HUMAN
)) {
3703 char *fen
= pgn_game_to_fen(gp
, d
->b
);
3705 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
3709 do_history_mode_finalize(d
);
3712 void do_history_toggle()
3714 struct userdata_s
*d
= gp
->data
;
3716 // FIXME Resuming from previous history could append to a RAV.
3717 if (gp
->hindex
!= pgn_history_total(gp
->hp
)) {
3719 construct_message(NULL
, _ ("What would you like to do?"), 0, 1,
3720 NULL
, NULL
, NULL
, do_history_mode_confirm
, 0, 0,
3721 _("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."),
3726 if (TEST_FLAG(gp
->flags
, GF_GAMEOVER
))
3730 if (gp
->side
!= gp
->turn
) {
3731 d
->play_mode
= PLAY_EH
;
3734 d
->play_mode
= PLAY_HE
;
3738 do_history_mode_finalize(d
);
3741 void do_history_annotate()
3745 if (n
&& gp
->hp
[n
- 1]->move
)
3750 do_annotate_move(gp
->hp
[n
]);
3753 void do_history_help()
3755 wchar_t *buf
= build_help(history_keys
);
3757 construct_message(_("History Mode Keys (* = can take a repeat count)"),
3758 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
,
3759 buf
, do_more_help
, 0, 1, "%ls", buf
);
3762 void do_history_find(int which
)
3764 struct input_data_s
*in
;
3767 if (pgn_history_total(gp
->hp
) < 2)
3770 in
= Calloc(1, sizeof(struct input_data_s
));
3771 p
= Malloc(sizeof(int));
3774 in
->efunc
= do_find_move_exp
;
3776 if (!*moveexp
|| which
== 0) {
3777 construct_input(_("Find Move Text Expression"), NULL
, 1, 0, NULL
, NULL
, NULL
,
3778 0, in
, INPUT_HIST_MOVE_EXP
, -1);
3784 do_find_move_exp_finalize(0, which
);
3787 void do_history_find_new()
3792 void do_history_find_prev()
3794 do_history_find(-1);
3797 void do_history_find_next()
3802 void do_history_rav(int which
)
3804 struct userdata_s
*d
= gp
->data
;
3806 rav_next_prev(gp
, d
->b
, which
);
3809 void do_history_rav_next()
3814 void do_history_rav_prev()
3819 void do_history_jump()
3821 struct input_data_s
*in
;
3823 if (pgn_history_total(gp
->hp
) < 2)
3827 in
= Calloc(1, sizeof(struct input_data_s
));
3828 in
->efunc
= do_move_jump
;
3830 construct_input(_("Jump to Move Number"), NULL
, 1, 1, NULL
,
3831 NULL
, NULL
, 0, in
, -1, 0);
3835 do_move_jump_finalize(keycount
);
3838 static void free_userdata_once(GAME g
)
3840 struct userdata_s
*d
= g
->data
;
3848 if (d
->engine
->enginebuf
) {
3851 for (n
= 0; d
->engine
->enginebuf
[n
]; n
++)
3852 free(d
->engine
->enginebuf
[n
]);
3854 free(d
->engine
->enginebuf
);
3857 if (d
->engine
->queue
) {
3860 for (q
= d
->engine
->queue
; *q
; q
++)
3863 free(d
->engine
->queue
);
3881 static void free_userdata()
3885 for (i
= 0; i
< gtotal
; i
++) {
3886 free_userdata_once(game
[i
]);
3887 game
[i
]->data
= NULL
;
3891 void update_loading_window(int n
)
3896 loadingw
= newwin(3, COLS
/ 2, CALCPOSY(3), CALCPOSX(COLS
/ 2));
3897 loadingp
= new_panel(loadingw
);
3898 wbkgd(loadingw
, CP_MESSAGE_WINDOW
);
3901 wmove(loadingw
, 0, 0);
3902 wclrtobot(loadingw
);
3903 wattron(loadingw
, CP_MESSAGE_BORDER
);
3904 box(loadingw
, ACS_VLINE
, ACS_HLINE
);
3905 wattroff(loadingw
, CP_MESSAGE_BORDER
);
3906 mvwprintw(loadingw
, 1, CENTER_INT((COLS
/ 2), 11 +
3907 strlen(itoa(gtotal
, buf
))),
3908 _("Loading... %i%% (%i games)"), n
, gtotal
);
3913 static void init_userdata_once(GAME g
, int n
)
3915 struct userdata_s
*d
= NULL
;
3917 d
= Calloc(1, sizeof(struct userdata_s
));
3919 d
->c_row
= 2, d
->c_col
= 5;
3920 SET_FLAG(d
->flags
, CF_NEW
);
3923 if (pgn_board_init_fen(g
, d
->b
, NULL
) != E_PGN_OK
)
3924 pgn_board_init(d
->b
);
3927 void init_userdata()
3931 for (i
= 0; i
< gtotal
; i
++)
3932 init_userdata_once(game
[i
], i
);
3935 void fix_marks(int *start
, int *end
)
3939 *start
= (*start
< 0) ? 0 : *start
;
3940 *end
= (*end
< 0) ? 0 : *end
;
3942 if (*start
> *end
) {
3948 *end
= (*end
> gtotal
) ? gtotal
: *end
;
3951 void do_new_game_finalize(GAME g
)
3953 struct userdata_s
*d
= g
->data
;
3955 d
->mode
= MODE_PLAY
;
3956 update_status_notify(g
, NULL
);
3961 void do_new_game_from_scratch(WIN
*win
)
3963 wchar_t str
[] = { win
->c
, 0 };
3965 if (wcscmp (str
, yes_wchar
))
3972 add_custom_tags(&gp
->tag
);
3975 do_new_game_finalize(gp
);
3982 add_custom_tags(&gp
->tag
);
3983 init_userdata_once(gp
, gindex
);
3984 do_new_game_finalize(gp
);
3987 void do_game_delete_finalize(int n
)
3989 struct userdata_s
*d
;
3991 delete_game((!n
) ? gindex
: -1);
3993 if (d
->mode
!= MODE_EDIT
)
3994 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3997 void do_game_delete_confirm(WIN
*win
)
4000 wchar_t str
[] = { win
->c
, 0 };
4002 if (wcscmp (str
, yes_wchar
)) {
4007 n
= (int *)win
->data
;
4008 do_game_delete_finalize(*n
);
4012 void do_game_delete()
4016 struct userdata_s
*d
;
4020 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
4026 for (i
= n
= 0; i
< gtotal
; i
++) {
4029 if (TEST_FLAG(d
->flags
, CF_DELETE
))
4034 tmp
= _("Delete the current game?");
4037 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
4041 tmp
= _("Delete all games marked for deletion?");
4044 if (config
.deleteprompt
) {
4045 p
= Malloc(sizeof(int));
4047 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, p
,
4048 do_game_delete_confirm
, 0, 0, tmp
);
4052 do_game_delete_finalize(n
);
4055 void do_find_game_exp_finalize(int which
)
4057 struct userdata_s
*d
= gp
->data
;
4060 if ((n
= find_game_exp(gameexp
, (which
== -1) ? 0 : 1,
4061 (keycount
) ? keycount
: 1)) == -1) {
4062 update_status_notify(gp
, "%s", _("No matches found"));
4069 if (pgn_history_total(gp
->hp
))
4070 d
->mode
= MODE_HISTORY
;
4072 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4075 void do_find_game_exp(WIN
*win
)
4077 struct input_data_s
*in
= win
->data
;
4082 strncpy(gameexp
, in
->str
, sizeof(gameexp
));
4083 gameexp
[sizeof(gameexp
)-1] = 0;
4088 do_find_game_exp_finalize(c
);
4096 void do_game_jump_finalize(int n
)
4098 struct userdata_s
*d
;
4100 if (--n
> gtotal
- 1 || n
< 0)
4106 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4107 update_status_notify(gp
, NULL
);
4110 void do_game_jump(WIN
*win
)
4112 struct input_data_s
*in
= win
->data
;
4114 if (!in
->str
|| !isinteger(in
->str
)) {
4122 do_game_jump_finalize(atoi(in
->str
));
4127 void do_load_file(WIN
*win
)
4129 struct input_data_s
*in
= win
->data
;
4130 char *tmp
= in
->str
;
4131 struct userdata_s
*d
;
4132 PGN_FILE
*pgn
= NULL
;
4140 if ((tmp
= pathfix(tmp
)) == NULL
)
4143 n
= pgn_open(tmp
, "r", &pgn
);
4145 if (n
== E_PGN_ERR
) {
4146 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
, strerror(errno
));
4149 else if (n
== E_PGN_INVALID
) {
4150 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
, _("Not a regular file"));
4156 if (pgn_parse(pgn
) == E_PGN_ERR
) {
4157 del_panel(loadingp
);
4165 del_panel(loadingp
);
4170 strncpy(loadfile
, tmp
, sizeof(loadfile
));
4171 loadfile
[sizeof(loadfile
)-1] = 0;
4175 if (pgn_history_total(gp
->hp
))
4176 d
->mode
= MODE_HISTORY
;
4178 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4180 fm_loaded_file
= TRUE
;
4192 void do_game_save(WIN
*win
)
4194 struct input_data_s
*in
= win
->data
;
4197 char *tmp
= in
->str
;
4198 char tfile
[FILENAME_MAX
];
4201 struct userdata_s
*d
;
4203 if (!tmp
|| (tmp
= pathfix(tmp
)) == NULL
)
4206 if (pgn_is_compressed(tmp
) == E_PGN_ERR
) {
4207 p
= tmp
+ strlen(tmp
) - 1;
4209 if (*p
!= 'n' || *(p
-1) != 'g' || *(p
-2) != 'p' ||
4211 snprintf(tfile
, sizeof(tfile
), "%s.pgn", tmp
);
4217 * When in edit mode, update the FEN tag.
4220 for (i
= 0; i
< gtotal
; i
++) {
4223 if (d
->mode
== MODE_EDIT
) {
4224 char *fen
= pgn_game_to_fen(game
[i
], d
->b
);
4226 pgn_tag_add(&game
[i
]->tag
, "FEN", fen
);
4234 if (d
->mode
== MODE_EDIT
) {
4235 char *fen
= pgn_game_to_fen(game
[n
], d
->b
);
4237 pgn_tag_add(&game
[n
]->tag
, "FEN", fen
);
4252 void do_get_game_save_input(int n
)
4254 struct input_data_s
*in
= Calloc(1, sizeof(struct input_data_s
));
4255 int *p
= Malloc(sizeof(int));
4257 in
->efunc
= do_game_save
;
4261 construct_input(_("Save Game Filename"), loadfile
, 1, 1, _("Type TAB for file browser"),
4262 file_browser
, NULL
, '\t', in
, INPUT_HIST_FILE
, -1);
4265 void do_game_save_multi_confirm(WIN
*win
)
4268 wchar_t str
[] = { win
->c
, 0 };
4270 if (!wcscmp (str
, current_wchar
))
4272 else if (!wcscmp (str
, all_wchar
))
4275 update_status_notify(gp
, "%s", _("Save game aborted."));
4279 do_get_game_save_input(i
);
4282 void do_global_about()
4284 cmessage(_("ABOUT"), ANY_KEY_STR
,
4285 _("%s\nUsing %s with %i colors and %i color pairs\n%s"),
4286 PACKAGE_STRING
, curses_version(), COLORS
, COLOR_PAIRS
,
4290 void global_game_next_prev(int which
)
4292 struct userdata_s
*d
;
4294 game_next_prev(gp
, (which
== 1) ? 1 : 0,
4295 (keycount
) ? keycount
: 1);
4300 markend
= markstart
+ delete_count
;
4304 markend
= markstart
- delete_count
+ 1;
4305 delete_count
= -1; // to fix gindex in the other direction
4308 fix_marks(&markstart
, &markend
);
4309 do_global_toggle_delete();
4312 if (d
->mode
== MODE_HISTORY
)
4313 pgn_board_update(gp
, d
->b
, gp
->hindex
);
4314 else if (d
->mode
== MODE_PLAY
)
4315 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4318 void do_global_next_game()
4320 global_game_next_prev(1);
4323 void do_global_prev_game()
4325 global_game_next_prev(0);
4328 void global_find(int which
)
4330 struct input_data_s
*in
;
4336 in
= Calloc(1, sizeof(struct input_data_s
));
4337 p
= Malloc(sizeof(int));
4340 in
->efunc
= do_find_game_exp
;
4342 if (!*gameexp
|| which
== 0) {
4343 construct_input(_("Find Game by Tag Expression"), NULL
, 1, 0,
4344 _("[name expression:]value expression"), NULL
, NULL
, 0, in
,
4345 INPUT_HIST_GAME_EXP
, -1);
4351 do_find_game_exp_finalize(which
);
4354 void do_global_find_new()
4359 void do_global_find_next()
4364 void do_global_find_prev()
4369 void do_global_game_jump()
4375 struct input_data_s
*in
;
4377 in
= Calloc(1, sizeof(struct input_data_s
));
4378 in
->efunc
= do_game_jump
;
4379 construct_input(_("Jump to Game Number"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
,
4384 do_game_jump_finalize(keycount
);
4387 void do_global_toggle_delete()
4396 if (keycount
&& delete_count
== 0) {
4398 delete_count
= keycount
;
4399 update_status_notify(gp
, "%s (delete)", status
.notify
);
4403 if (markstart
>= 0 && markend
>= 0) {
4404 for (i
= markstart
; i
< markend
; i
++) {
4405 if (toggle_delete_flag(i
)) {
4410 gindex
= (delete_count
< 0) ? markstart
: i
- 1;
4413 if (toggle_delete_flag(gindex
))
4417 markstart
= markend
= -1;
4419 update_status_window(gp
);
4422 void do_global_delete_game()
4427 void do_global_tag_edit()
4429 struct userdata_s
*d
= gp
->data
;
4431 edit_tags(gp
, d
->b
, 1);
4434 void do_global_tag_view()
4436 struct userdata_s
*d
= gp
->data
;
4438 edit_tags(gp
, d
->b
, 0);
4441 void do_global_resume_game()
4443 struct input_data_s
*in
;
4445 in
= Calloc(1, sizeof(struct input_data_s
));
4446 in
->efunc
= do_load_file
;
4447 construct_input(_("Load Filename"), NULL
, 1, 1, _("Type TAB for file browser"), file_browser
,
4448 NULL
, '\t', in
, INPUT_HIST_FILE
, -1);
4451 void do_global_save_game()
4454 construct_message(NULL
, _("What would you like to do?"), 0, 1,
4455 NULL
, NULL
, NULL
, do_game_save_multi_confirm
, 0, 0,
4456 _("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."),
4457 current_wchar
, all_wchar
);
4461 do_get_game_save_input(-1);
4464 void do_global_new_game()
4469 void do_global_copy_game()
4473 struct userdata_s
*d
;
4475 do_global_new_game();
4477 n
= pgn_tag_total(game
[g
]->tag
);
4479 for (i
= 0; i
< n
; i
++)
4480 pgn_tag_add(&gp
->tag
, game
[g
]->tag
[i
]->name
,
4481 game
[g
]->tag
[i
]->value
);
4483 pgn_board_init_fen (gp
, d
->b
, NULL
);
4484 n
= pgn_history_total(game
[g
]->history
);
4487 for (i
= 0; i
< n
; i
++) {
4489 char *move
= strdup (game
[g
]->history
[i
]->move
);
4491 if (pgn_parse_move(gp
, d
->b
, &move
, &frfr
) != E_PGN_OK
) {
4493 SET_FLAG(gp
->flags
, GF_PERROR
);
4497 pgn_history_add(gp
, d
->b
, move
);
4500 pgn_switch_turn(gp
);
4503 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4506 void do_global_new_all()
4508 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
4509 do_new_game_from_scratch
, 0, 0, "%s", _("Really start a new game from scratch?"));
4512 void do_quit(WIN
*win
)
4514 wchar_t str
[] = { win
->c
, 0 };
4516 if (wcscmp (str
, yes_wchar
))
4522 void do_global_quit()
4524 if (config
.exitdialogbox
)
4525 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
4526 do_quit
, 0, 0, _("Want to Quit?"));
4531 void do_global_toggle_engine_window()
4534 enginew
= newwin(LINES
, COLS
, 0, 0);
4535 enginep
= new_panel(enginew
);
4536 window_draw_title(enginew
, _("Engine IO Window"), COLS
, CP_MESSAGE_TITLE
,
4538 hide_panel(enginep
);
4541 if (panel_hidden(enginep
)) {
4542 update_engine_window(gp
);
4546 hide_panel(enginep
);
4550 void do_global_toggle_board_details()
4555 void do_global_toggle_strict_castling()
4557 do_toggle_strict_castling();
4560 // Global and other keys.
4561 static int globalkeys()
4563 struct userdata_s
*d
= gp
->data
;
4567 * These cannot be modified and other game mode keys cannot conflict with
4572 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
4573 markend
= markstart
= 0;
4577 update_status_notify(gp
, NULL
);
4580 if (config
.validmoves
)
4581 pgn_reset_valid_moves(d
->b
);
4588 keycount
= keycount
* 10 + i
;
4592 update_status_notify(gp
, _("Repeat %i"), keycount
);
4595 if (d
->mode
== MODE_HISTORY
)
4599 d
->c_row
+= keycount
;
4608 if (d
->mode
== MODE_HISTORY
)
4612 d
->c_row
-= keycount
;
4613 update_status_notify(gp
, NULL
);
4623 if (d
->mode
== MODE_HISTORY
)
4627 d
->c_col
-= keycount
;
4636 if (d
->mode
== MODE_HISTORY
)
4640 d
->c_col
+= keycount
;
4652 for (i
= 0; global_keys
[i
]; i
++) {
4653 if (input_c
== global_keys
[i
]->c
&& global_keys
[i
]->f
) {
4654 (*global_keys
[i
]->f
)();
4665 static void perl_error(const char *fmt
, ...)
4671 vasprintf(&buf
, fmt
, ap
);
4674 message(ERROR_STR
, ANY_KEY_STR
, "%s", buf
);
4678 static void do_perl_finalize(WIN
*win
)
4680 struct input_data_s
*in
= win
->data
;
4682 struct userdata_s
*d
= g
->data
;
4684 char *result
= NULL
;
4688 asprintf(&filename
, "%s/perl.pl", config
.datadir
);
4693 if (perl_init_file(filename
, perl_error
))
4696 arg
= pgn_game_to_fen(g
, d
->b
);
4698 if (perl_call_sub(trim(in
->str
), arg
, &result
))
4701 d
->perlfen
= pgn_game_to_fen(g
, d
->b
);
4702 d
->perlflags
= g
->flags
;
4704 if (pgn_board_init_fen(g
, d
->b
, result
) != E_PGN_OK
) {
4705 message(ERROR_STR
, ANY_KEY_STR
, "%s", _("FEN parse error."));
4706 pgn_board_init_fen(g
, d
->b
, d
->perlfen
);
4707 g
->flags
= d
->perlflags
;
4713 SET_FLAG(d
->flags
, CF_PERL
);
4714 n
= pgn_tag_find(g
->tag
, "FEN");
4717 d
->oldfen
= strdup(g
->tag
[n
]->value
);
4719 pgn_tag_add(&g
->tag
, "FEN", result
);
4720 update_status_notify(g
, "%s", ANY_KEY_STR
);
4731 void do_global_perl()
4733 struct input_data_s
*in
;
4735 in
= Calloc(1, sizeof(struct input_data_s
));
4737 in
->efunc
= do_perl_finalize
;
4738 construct_input(_("PERL Subroutine Filter"), NULL
, 1, 0, NULL
, NULL
, NULL
, 0, in
, INPUT_HIST_PERL
, -1);
4743 * A macro may contain a key that belongs to another macro so macro_match will
4744 * need to be updated to the new index of the matching macro.
4746 static void find_macro(struct userdata_s
*d
)
4751 * Macros can't contain macros when in a window.
4757 for (i
= 0; macros
[i
]; i
++) {
4758 if ((macros
[i
]->mode
== -1 || macros
[i
]->mode
== d
->mode
) &&
4759 input_c
== macros
[i
]->c
) {
4760 input_c
= macros
[i
]->keys
[macros
[i
]->n
++];
4762 if (!macro_depth_n
&& macro_match
> -1) {
4763 macro_depth
= realloc(macro_depth
, (macro_depth_n
+ 1) * sizeof(int));
4764 macro_depth
[macro_depth_n
++] = macro_match
;
4767 macro_depth
= realloc(macro_depth
, (macro_depth_n
+ 1) * sizeof(int));
4768 macro_depth
[macro_depth_n
++] = i
;
4776 * Resets the position in each macro to the first key.
4778 static void reset_macros()
4781 struct userdata_s
*d
= gp
->data
;
4784 if (macro_depth_n
> 0) {
4786 macro_match
= macro_depth
[macro_depth_n
];
4788 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
4791 input_c
= macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
4796 for (i
= 0; macros
[i
]; i
++)
4807 struct userdata_s
*d
;
4810 gindex
= gtotal
- 1;
4814 if (pgn_history_total(gp
->hp
))
4815 d
->mode
= MODE_HISTORY
;
4817 d
->mode
= MODE_PLAY
;
4818 d
->play_mode
= PLAY_HE
;
4824 d
->pm_frfr
[0] = '\0';
4826 if (d
->mode
== MODE_HISTORY
)
4827 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4829 update_status_notify(gp
, "%s", _("Type F1 for help"));
4833 wtimeout(boardw
, WINDOW_TIMEOUT
);
4837 char fdbuf
[8192] = {0};
4839 struct timeval tv
= {0, 0};
4847 for (i
= 0; i
< gtotal
; i
++) {
4850 if (d
->engine
&& d
->engine
->pid
!= -1) {
4851 if (d
->engine
->fd
[ENGINE_IN_FD
] > 2) {
4852 if (d
->engine
->fd
[ENGINE_IN_FD
] > n
)
4853 n
= d
->engine
->fd
[ENGINE_IN_FD
];
4855 FD_SET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
);
4858 if (d
->engine
->fd
[ENGINE_OUT_FD
] > 2) {
4859 if (d
->engine
->fd
[ENGINE_OUT_FD
] > n
)
4860 n
= d
->engine
->fd
[ENGINE_OUT_FD
];
4862 FD_SET(d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
);
4868 if ((n
= select(n
+ 1, &rfds
, &wfds
, NULL
, &tv
)) > 0) {
4869 for (i
= 0; i
< gtotal
; i
++) {
4872 if (d
->engine
&& d
->engine
->pid
!= -1) {
4873 if (FD_ISSET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
)) {
4874 len
= read(d
->engine
->fd
[ENGINE_IN_FD
], fdbuf
,
4878 if (d
->engine
->iobuf
)
4879 d
->engine
->iobuf
= Realloc(d
->engine
->iobuf
, d
->engine
->len
+ len
+ 1);
4881 d
->engine
->iobuf
= Calloc(1, len
+ 1);
4883 memcpy(&(d
->engine
->iobuf
[d
->engine
->len
]), &fdbuf
, len
);
4884 d
->engine
->len
+= len
;
4885 d
->engine
->iobuf
[d
->engine
->len
] = 0;
4888 * The fdbuf is full or no newline
4889 * was found. So we'll append the next
4890 * read() to this games buffer.
4892 if (d
->engine
->iobuf
[d
->engine
->len
- 1] != '\n')
4895 parse_engine_output(game
[i
], d
->engine
->iobuf
);
4896 free(d
->engine
->iobuf
);
4897 d
->engine
->iobuf
= NULL
;
4900 else if (len
== -1) {
4901 if (errno
!= EAGAIN
) {
4902 cmessage(ERROR_STR
, ANY_KEY_STR
, "Engine read(): %s",
4904 waitpid(d
->engine
->pid
, &n
, 0);
4912 if (FD_ISSET(d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
)) {
4913 if (d
->engine
->queue
)
4914 send_engine_command(game
[i
]);
4921 cmessage(ERROR_STR
, ANY_KEY_STR
, "select(): %s", strerror(errno
));
4930 * This is needed to detect terminal resizing.
4933 if (LINES
!= LINES_OLD
|| COLS
!= COLS_OLD
) {
4940 * Finds the top level window in the window stack so we know what
4941 * window the wget_wch()'ed key belongs to.
4944 for (i
= 0; wins
[i
]; i
++);
4947 wtimeout(wp
, WINDOW_TIMEOUT
);
4956 if (macros
&& macro_match
>= 0) {
4957 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
4960 input_c
= macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
4965 if (wget_wch(wp
, &input_c
) == ERR
|| input_c
== KEY_RESIZE
)
4976 * Run the function associated with the window. When the
4977 * function returns 0 win->efunc is ran (if not NULL) with
4978 * win as the one and only parameter. Then the window is
4981 * The exit function may create another window which will
4982 * mess up the window stack when window_destroy() is called.
4983 * So don't destory the window until the top window is
4984 * destroyable. See window_destroy().
4986 if ((*win
->func
)(win
) == 0) {
4991 window_destroy(win
);
4999 if (!keycount
&& status
.notify
)
5000 update_status_notify(gp
, NULL
);
5003 if (TEST_FLAG(d
->flags
, CF_PERL
)) {
5004 CLEAR_FLAG(d
->flags
, CF_PERL
);
5005 pgn_board_init_fen(gp
, d
->b
, d
->perlfen
);
5006 gp
->flags
= d
->perlflags
;
5008 pgn_tag_add(&gp
->tag
, "FEN", d
->oldfen
);
5010 d
->perlfen
= d
->oldfen
= NULL
;
5016 if (macros
&& macro_match
< 0)
5019 if ((n
= globalkeys()) == 1) {
5020 if (macro_match
== -1)
5030 for (i
= 0; edit_keys
[i
]; i
++) {
5031 if (input_c
== edit_keys
[i
]->c
) {
5032 (*edit_keys
[i
]->f
)();
5038 for (i
= 0; play_keys
[i
]; i
++) {
5039 if (input_c
== play_keys
[i
]->c
) {
5040 (*play_keys
[i
]->f
)();
5045 do_play_config_command();
5048 for (i
= 0; history_keys
[i
]; i
++) {
5049 if (input_c
== history_keys
[i
]->c
) {
5050 (*history_keys
[i
]->f
)();
5061 update_status_notify(gp
, NULL
);
5070 void usage(const char *pn
, int ret
)
5072 fprintf((ret
) ? stderr
: stdout
, "%s%s",
5075 "Usage: cboard [-hvCD] [-u [N]] [-p [-VtRSE] <file>]\n"
5076 " -D Dump libchess debugging info to \"libchess.debug\" (stderr)\n"),
5079 "Usage: cboard [-hvC] [-u [N]] [-p [-VtRSE] <file>]\n"),
5082 " -p Load PGN file.\n"
5083 " -V Validate a game file.\n"
5084 " -S Validate and output a PGN formatted game.\n"
5085 " -R Like -S but write a reduced PGN formatted game.\n"
5086 " -t Also write custom PGN tags from config file.\n"
5087 " -E Stop processing on file parsing error (overrides config).\n"
5088 " -C Enable strict castling (overrides config).\n"
5089 " -u Enable/disable UTF-8 pieces (1=enable, 0=disable, overrides config).\n"
5090 " -v Version information.\n"
5091 " -h This help text.\n"));
5103 free(config
.engine_cmd
);
5104 free(config
.pattern
);
5105 free(config
.ccfile
);
5106 free(config
.nagfile
);
5107 free(config
.configfile
);
5110 for (i
= 0; config
.keys
[i
]; i
++) {
5111 free(config
.keys
[i
]->str
);
5112 free(config
.keys
[i
]);
5119 for (i
= 0; config
.einit
[i
]; i
++)
5120 free(config
.einit
[i
]);
5126 pgn_tag_free(config
.tag
);
5128 free(config
.datadir
);
5130 if (curses_initialized
) {
5132 del_panel(historyp
);
5153 static void signal_save_pgn(int sig
)
5157 char *p
= config
.savedirectory
? config
.savedirectory
: config
.datadir
;
5160 asprintf(&buf
, "%s/signal-%i-%li.pgn", p
, sig
, now
);
5162 if (do_game_write(buf
, "w", 0, gtotal
)) {
5163 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", p
, strerror(errno
));
5164 update_status_notify(gp
, "%s", _("Save game failed."));
5171 void catch_signal(int which
)
5178 if (which
== SIGPIPE
&& quit
)
5181 if (which
== SIGPIPE
)
5182 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Broken pipe. Quitting."));
5192 do_window_resize ();
5193 keypad(boardw
, TRUE
);
5199 signal_save_pgn(which
);
5206 void loading_progress(long total
, long offset
)
5208 int n
= (100 * (offset
/ 100) / (total
/ 100));
5210 if (curses_initialized
)
5211 update_loading_window(n
);
5213 fprintf(stderr
, _("Loading... %i%% (%i games)%c"), n
, gtotal
, '\r');
5218 static void set_defaults()
5220 set_config_defaults();
5222 filetype
= FILE_NONE
;
5223 pgn_config_set(PGN_PROGRESS
, 1024);
5224 pgn_config_set(PGN_PROGRESS_FUNC
, loading_progress
);
5227 int main(int argc
, char *argv
[])
5231 char buf
[FILENAME_MAX
];
5232 char datadir
[FILENAME_MAX
];
5233 int ret
= EXIT_SUCCESS
;
5234 int validate_only
= 0, validate_and_write
= 0;
5235 int write_custom_tags
= 0;
5238 int utf8_pieces
= -1;
5240 setlocale (LC_ALL
, "");
5241 bindtextdomain ("cboard", LOCALE_DIR
);
5242 textdomain ("cboard");
5245 #ifndef HAVE_PROGNAME
5246 __progname
= argv
[0];
5249 if ((config
.pwd
= getpwuid(getuid())) == NULL
)
5250 err(EXIT_FAILURE
, "getpwuid()");
5252 snprintf(datadir
, sizeof(datadir
), "%s/.cboard", config
.pwd
->pw_dir
);
5253 config
.datadir
= strdup(datadir
);
5254 snprintf(buf
, sizeof(buf
), "%s/cc.data", datadir
);
5255 config
.ccfile
= strdup(buf
);
5256 snprintf(buf
, sizeof(buf
), "%s/nag.data", datadir
);
5257 config
.nagfile
= strdup(buf
);
5258 snprintf(buf
, sizeof(buf
), "%s/config", datadir
);
5259 config
.configfile
= strdup(buf
);
5261 if (stat(datadir
, &st
) == -1) {
5262 if (errno
== ENOENT
) {
5263 if (mkdir(datadir
, 0755) == -1)
5264 err(EXIT_FAILURE
, "%s", datadir
);
5267 err(EXIT_FAILURE
, "%s", datadir
);
5272 if (!S_ISDIR(st
.st_mode
))
5273 errx(EXIT_FAILURE
, "%s: %s", datadir
, _("Not a directory."));
5278 while ((opt
= getopt(argc
, argv
, "DCEVtSRhp:vu::")) != -1) {
5280 while ((opt
= getopt(argc
, argv
, "ECVtSRhp:vu::")) != -1) {
5285 unlink("libchess.debug");
5286 pgn_config_set(PGN_DEBUG
, 1);
5290 pgn_config_set(PGN_STRICT_CASTLING
, 1);
5293 write_custom_tags
= 1;
5299 pgn_config_set(PGN_REDUCED
, 1);
5301 validate_and_write
= 1;
5306 printf("%s (%s)\n%s\n", PACKAGE_STRING
, curses_version(),
5310 filetype
= FILE_PGN
;
5311 strncpy(loadfile
, optarg
, sizeof(loadfile
));
5312 loadfile
[sizeof(loadfile
)-1] = 0;
5315 utf8_pieces
= optarg
? atoi (optarg
): 1;
5319 usage(argv
[0], EXIT_SUCCESS
);
5323 if ((validate_only
|| validate_and_write
) && !*loadfile
)
5324 usage(argv
[0], EXIT_FAILURE
);
5326 if (access(config
.configfile
, R_OK
) == 0)
5327 parse_rcfile(config
.configfile
);
5330 pgn_config_set(PGN_STOP_ON_ERROR
, 1);
5332 signal(SIGPIPE
, catch_signal
);
5333 signal(SIGCONT
, catch_signal
);
5334 signal(SIGSTOP
, catch_signal
);
5335 signal(SIGINT
, catch_signal
);
5336 signal(SIGALRM
, catch_signal
);
5337 signal(SIGTERM
, catch_signal
);
5343 if (pgn_open(loadfile
, "r", &pgn
) != E_PGN_OK
)
5344 err(EXIT_FAILURE
, "%s", loadfile
);
5346 ret
= pgn_parse(pgn
);
5350 //ret = parse_fen_file(loadfile);
5352 case FILE_EPD
: // Not implemented.
5355 // No file specified. Empty game.
5356 ret
= pgn_parse(NULL
);
5358 add_custom_tags(&gp
->tag
);
5362 if (validate_only
|| validate_and_write
) {
5363 if (validate_and_write
) {
5364 if (pgn_open("-", "r", &pgn
) != E_PGN_OK
)
5365 err(EXIT_FAILURE
, "pgn_open()");
5367 for (i
= 0; i
< gtotal
; i
++) {
5368 if (write_custom_tags
)
5369 add_custom_tags(&game
[i
]->tag
);
5371 pgn_write(pgn
, game
[i
]);
5376 fm_loaded_file
= TRUE
;
5382 else if (ret
== E_PGN_ERR
)
5385 if (utf8_pieces
!= -1)
5386 config
.utf8_pieces
= utf8_pieces
;
5388 init_wchar_pieces ();
5389 yes_wchar
= str_to_wchar (_("y"));
5390 all_wchar
= str_to_wchar (_("a"));
5391 overwrite_wchar
= str_to_wchar (_("o"));
5392 resume_wchar
= str_to_wchar (_("r"));
5393 current_wchar
= str_to_wchar (_("c"));
5394 append_wchar
= str_to_wchar (_("a"));
5395 translatable_tag_names
[0] = _("Event");
5396 translatable_tag_names
[1] = _("Site");
5397 translatable_tag_names
[2] = _("Date");
5398 translatable_tag_names
[3] = _("Round");
5399 translatable_tag_names
[4] = _("White");
5400 translatable_tag_names
[5] = _("Black");
5401 translatable_tag_names
[6] = _("Result");
5405 * This fixes window resizing in an xterm.
5407 if (getenv("DISPLAY") != NULL
) {
5412 if (initscr() == NULL
)
5413 errx(EXIT_FAILURE
, "%s", _("Could not initialize curses."));
5415 curses_initialized
= 1;
5417 if (LINES
< 23 || COLS
< 74) {
5419 errx(EXIT_FAILURE
, _("Need at least an 74x23 terminal."));
5425 if (has_colors() == TRUE
&& start_color() == OK
)
5428 boardw
= newwin(BOARD_HEIGHT
, BOARD_WIDTH
, 0, COLS
- BOARD_WIDTH
);
5429 boardp
= new_panel(boardw
);
5430 historyw
= newwin(HISTORY_HEIGHT
, HISTORY_WIDTH
, LINES
- HISTORY_HEIGHT
,
5431 COLS
- HISTORY_WIDTH
);
5432 historyp
= new_panel(historyw
);
5433 statusw
= newwin(STATUS_HEIGHT
, STATUS_WIDTH
, 0, 0);
5434 statusp
= new_panel(statusw
);
5435 tagw
= newwin(TAG_HEIGHT
, TAG_WIDTH
, STATUS_HEIGHT
+ 1, 0);
5436 tagp
= new_panel(tagw
);
5437 keypad(boardw
, TRUE
);
5438 // leaveok(boardw, TRUE);
5439 leaveok(tagw
, TRUE
);
5440 leaveok(statusw
, TRUE
);
5441 leaveok(historyw
, TRUE
);
5445 draw_window_decor();
5448 free (w_pawn_wchar
);
5449 free (w_rook_wchar
);
5450 free (w_bishop_wchar
);
5451 free (w_knight_wchar
);
5452 free (w_queen_wchar
);
5453 free (w_king_wchar
);
5454 free (b_pawn_wchar
);
5455 free (b_rook_wchar
);
5456 free (b_bishop_wchar
);
5457 free (b_knight_wchar
);
5458 free (b_queen_wchar
);
5459 free (b_king_wchar
);
5461 free (enpassant_wchar
);
5464 free (overwrite_wchar
);
5465 free (resume_wchar
);
5466 free (current_wchar
);
5467 free (append_wchar
);
5468 free (status
.notify
);