1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2013 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
27 #include <sys/socket.h>
42 #ifdef HAVE_SYS_WAIT_H
51 #include "perl-plugin.h"
66 #include "filebrowser.h"
72 #define COPYRIGHT "Copyright (C) 2002-2013 " PACKAGE_BUGREPORT
73 #define LINE_GRAPHIC(c) ((!config.linegraphics) ? ' ' : c)
74 #define ROWTOMATRIX(r) ((8 - r) * 2 + 2 - 1)
75 #define COLTOMATRIX(c) ((c == 1) ? 1 : c * 4 - 3)
76 #define STATUS_HEIGHT 12
77 #define MEGA_BOARD (LINES >= 50 && COLS >= 144)
78 #define BOARD_HEIGHT_MB 50
79 #define BOARD_WIDTH_MB 98
80 #define STATUS_WIDTH_MB (COLS - BOARD_WIDTH_MB)
81 #define TAG_HEIGHT_MB 31
82 #define TAG_WIDTH_MB (COLS - BOARD_WIDTH_MB)
83 #define HISTORY_HEIGHT_MB (LINES - (STATUS_HEIGHT + TAG_HEIGHT_MB + 1))
84 #define HISTORY_WIDTH_MB (COLS - BOARD_WIDTH_MB)
85 #define BIG_BOARD (LINES >= 40 && COLS >= 112)
86 #define BOARD_HEIGHT ((MEGA_BOARD) ? BOARD_HEIGHT_MB : (BIG_BOARD) ? 34 : 18)
87 #define BOARD_WIDTH ((MEGA_BOARD) ? BOARD_WIDTH_MB : (BIG_BOARD) ? 66 : 34)
88 #define STATUS_WIDTH ((MEGA_BOARD) ? STATUS_WIDTH_MB : COLS - BOARD_WIDTH)
89 #define TAG_HEIGHT ((MEGA_BOARD) ? TAG_HEIGHT_MB : LINES - STATUS_HEIGHT - 1)
90 #define TAG_WIDTH ((MEGA_BOARD) ? TAG_WIDTH_MB : COLS - BOARD_WIDTH)
91 #define HISTORY_HEIGHT ((MEGA_BOARD) ? HISTORY_HEIGHT_MB : LINES - BOARD_HEIGHT)
92 #define HISTORY_WIDTH ((MEGA_BOARD) ? HISTORY_WIDTH_MB : COLS - STATUS_WIDTH)
93 #define MAX_VALUE_WIDTH (COLS - 8)
99 static WINDOW
*boardw
;
100 static PANEL
*boardp
;
103 static WINDOW
*statusw
;
104 static PANEL
*statusp
;
105 static WINDOW
*historyw
;
106 static PANEL
*historyp
;
107 static WINDOW
*loadingw
;
108 static PANEL
*loadingp
;
109 static WINDOW
*enginew
;
110 static PANEL
*enginep
;
112 static char gameexp
[255];
113 static char moveexp
[255];
114 static struct itimerval clock_timer
;
115 static int delete_count
= 0;
116 static int markstart
= -1, markend
= -1;
118 static char loadfile
[FILENAME_MAX
];
120 static wint_t input_c
;
122 // Loaded filename from the command line or from the file input dialog.
125 FILE_NONE
, FILE_PGN
, FILE_FEN
, FILE_EPD
129 static int nag_total
;
130 static int macro_match
;
132 // Primer movimiento de juego cargado
133 // First move loaded game
134 static char fm_loaded_file
= FALSE
;
135 // Movimiento resultado de la función 'do_play_go'
136 // Movement function result 'do_play_go'
137 static int go_move
= 0;
138 // Controla rotación de tablero
139 // Rotation control board
140 static int rotate
= FALSE
;
142 static int COLS_OLD
, LINES_OLD
;
146 wchar_t *notify
; // The status window notification line buffer.
149 static int curses_initialized
;
151 // When in history mode a full step is to the next move of the same playing
152 // side. Half stepping is alternating sides.
155 static wchar_t *w_pawn_wchar
;
156 static wchar_t *w_rook_wchar
;
157 static wchar_t *w_bishop_wchar
;
158 static wchar_t *w_knight_wchar
;
159 static wchar_t *w_queen_wchar
;
160 static wchar_t *w_king_wchar
;
161 static wchar_t *b_pawn_wchar
;
162 static wchar_t *b_rook_wchar
;
163 static wchar_t *b_bishop_wchar
;
164 static wchar_t *b_knight_wchar
;
165 static wchar_t *b_queen_wchar
;
166 static wchar_t *b_king_wchar
;
167 static wchar_t *empty_wchar
;
168 static wchar_t *enpassant_wchar
;
170 static wchar_t *yes_wchar
;
171 static wchar_t *all_wchar
; // do_save_game_overwrite_confirm()
172 static wchar_t *overwrite_wchar
; // do_save_game_overwrite_confirm()
173 static wchar_t *resume_wchar
; // do_history_mode_confirm()
174 static wchar_t *current_wchar
; // do_game_save_multi_confirm()
175 static wchar_t *append_wchar
; // save_pgn()
177 static const char piece_chars
[] = "PpRrNnBbQqKkxx";
178 static char *translatable_tag_names
[7];
179 static const char *f_pieces
[] = {
203 static const bool cb
[8][8] = {
214 static void free_userdata_once(GAME g
);
216 // Posición por rotación de tablero.
217 // Rotation board position.
218 static void rotate_position(int p
)
220 struct userdata_s
*d
= gp
->data
;
222 char fr2
= 8, fc2
= 8;
224 for (fr
= 1; fr
< 9; fr
++) {
227 for (fc
= 1; fc
< 9; fc
++){
230 if (p
== CURSOR_POSITION
&&
231 d
->c_row
== fr
&& d
->c_col
== fc
) {
236 else if (p
== SP_POSITION
&&
237 d
->sp
.row
== fr
&& d
->sp
.col
== fc
) {
242 else if (p
== SPS_POSITION
&&
243 d
->sp
.srow
== fr
&& d
->sp
.scol
== fc
) {
254 void update_cursor(GAME g
, int idx
)
258 int t
= pgn_history_total(g
->hp
);
259 struct userdata_s
*d
= g
->data
;
262 * If not deincremented then r and c would be the next move.
266 if (idx
> t
|| idx
< 0 || !t
|| !g
->hp
[idx
]->move
) {
267 d
->c_row
= 2, d
->c_col
= 5;
272 p
= g
->hp
[idx
]->move
;
277 d
->c_col
= rotate
? 3 : 7;
279 d
->c_col
= rotate
? 6 : 3;
282 d
->c_row
= (g
->turn
== WHITE
) ? 1 : 8;
284 d
->c_row
= (g
->turn
== WHITE
) ? 8 : 1;
294 d
->c_row
= RANKTOINT(*p
--);
295 d
->c_col
= FILETOINT(*p
);
298 if (d
->mode
== MODE_HISTORY
&& rotate
)
299 rotate_position(CURSOR_POSITION
);
302 static int init_nag()
308 if ((fp
= fopen(config
.nagfile
, "r")) == NULL
) {
309 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.nagfile
, strerror(errno
));
313 nags
= Realloc(nags
, (i
+2) * sizeof(char *));
314 nags
[i
++] = strdup(_("none"));
318 if (fscanf(fp
, " %[^\n] ", line
) == 1) {
319 nags
= Realloc(nags
, (i
+ 2) * sizeof(char *));
320 nags
[i
++] = strdup(line
);
329 void edit_nag_toggle_item(struct menu_input_s
*m
)
331 struct input_s
*in
= m
->data
;
332 struct input_data_s
*id
= in
->data
;
333 HISTORY
*h
= id
->data
;
336 if (m
->selected
== 0) {
337 for (i
= 0; i
< MAX_PGN_NAG
; i
++)
340 for (i
= 0; m
->items
[i
]; i
++)
341 m
->items
[i
]->selected
= 0;
346 for (i
= 0; i
< MAX_PGN_NAG
; i
++) {
347 if (h
->nag
[i
] == m
->selected
)
348 h
->nag
[i
] = m
->selected
= 0;
351 h
->nag
[i
] = m
->selected
;
358 void edit_nag_save(struct menu_input_s
*m
)
363 void edit_nag_help(struct menu_input_s
*m
)
365 message(_("NAG Menu Keys"), ANY_KEY_STR
, "%s",
367 " UP/DOWN - previous/next menu item\n"
368 " HOME/END - first/last menu item\n"
369 " PGDN/PGUP - next/previous page\n"
370 " a-zA-Z0-9 - jump to item\n"
371 " SPACE - toggle selected item\n"
372 " CTRL-X - quit with changes"
376 struct menu_item_s
**get_nag_items(WIN
*win
)
379 struct menu_input_s
*m
= win
->data
;
380 struct input_s
*in
= m
->data
;
381 struct input_data_s
*id
= in
->data
;
382 struct menu_item_s
**items
= m
->items
;
383 HISTORY
*h
= id
->data
;
386 for (i
= 0; items
[i
]; i
++)
390 for (i
= 0; nags
[i
]; i
++) {
391 items
= Realloc(items
, (i
+2) * sizeof(struct menu_item_s
*));
392 items
[i
] = Malloc(sizeof(struct menu_item_s
));
393 items
[i
]->name
= nags
[i
];
394 items
[i
]->value
= NULL
;
396 for (n
= 0; n
< MAX_PGN_NAG
; n
++) {
397 if (h
->nag
[n
] == i
) {
398 items
[i
]->selected
= 1;
405 items
[i
]->selected
= 0;
414 void nag_print(WIN
*win
)
416 struct menu_input_s
*m
= win
->data
;
418 mvwprintw(win
->w
, m
->print_line
, 1, "%-*s", win
->cols
- 2, m
->item
->name
);
421 void edit_nag(void *arg
)
423 struct menu_key_s
**keys
= NULL
;
430 add_menu_key(&keys
, ' ', edit_nag_toggle_item
);
431 add_menu_key(&keys
, CTRL_KEY('x'), edit_nag_save
);
432 add_menu_key(&keys
, KEY_F(1), edit_nag_help
);
433 construct_menu(0, 0, -1, -1, _("Numeric Annotation Glyphs"), 1, get_nag_items
, keys
, arg
,
438 static void *view_nag(void *arg
)
440 HISTORY
*h
= (HISTORY
*)arg
;
442 char line
[LINE_MAX
] = {0};
445 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Viewing NAG for"), h
->move
);
452 for (i
= 0; i
< MAX_PGN_NAG
; i
++) {
458 if (h
->nag
[i
] >= nag_total
)
459 strncat(line
, itoa(h
->nag
[i
], buf2
), sizeof(line
)-1);
461 strncat(line
, nags
[h
->nag
[i
]], sizeof(line
)-1);
463 strncat(line
, "\n", sizeof(line
)-1);
466 line
[strlen(line
) - 1] = 0;
467 message(buf
, ANY_KEY_STR
, "%s", line
);
471 void view_annotation(HISTORY
*h
)
473 char buf
[MAX_SAN_MOVE_LEN
+ strlen(_("Viewing Annotation for")) + 4];
474 int nag
= 0, comment
= 0;
479 if (h
->comment
&& h
->comment
[0])
485 if (!nag
&& !comment
)
488 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Viewing Annotation for"), h
->move
);
491 construct_message(buf
, (nag
) ? _("Any other key to continue") : ANY_KEY_STR
, 0, 1,
492 (nag
) ? _("Press 'n' to view NAG") : NULL
,
493 (nag
) ? view_nag
: NULL
, (nag
) ? h
: NULL
, NULL
,
494 (nag
) ? 'n' : 0, 0, "%s", h
->comment
);
496 construct_message(buf
, _("Any other key to continue"), 0, 1, _("Press 'n' to view NAG"), view_nag
, h
, NULL
,
497 'n', 0, "%s", _("No comment text for this move"));
500 int do_game_write(char *filename
, char *mode
, int start
, int end
)
503 struct userdata_s
*d
;
506 i
= pgn_open(filename
, mode
, &pgn
);
508 if (i
== E_PGN_ERR
) {
509 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", filename
, strerror(errno
));
512 else if (i
== E_PGN_INVALID
) {
513 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", filename
, _("Not a regular file"));
517 for (i
= (start
== -1) ? 0 : start
; i
< end
; i
++) {
519 pgn_write(pgn
, game
[i
]);
520 CLEAR_FLAG(d
->flags
, CF_MODIFIED
);
523 if (pgn_close(pgn
) != E_PGN_OK
)
524 message(ERROR_STR
, ANY_KEY_STR
, "%s", strerror(errno
));
527 strncpy(loadfile
, filename
, sizeof(loadfile
));
528 loadfile
[sizeof(loadfile
)-1] = 0;
541 void do_save_game_overwrite_confirm(WIN
*win
)
544 struct save_game_s
*s
= win
->data
;
545 wchar_t str
[] = { win
->c
, 0 };
547 if (!wcscmp (str
, append_wchar
))
549 else if (!wcscmp (str
, overwrite_wchar
))
554 if (do_game_write(s
->filename
, mode
, s
->start
, s
->end
))
555 update_status_notify(gp
, "%s", _("Save game failed."));
557 update_status_notify(gp
, "%s", _("Game saved."));
564 /* If the saveindex argument is -1, all games will be saved. Otherwise it's a
567 void save_pgn(char *filename
, int saveindex
)
569 char buf
[FILENAME_MAX
];
571 int end
= (saveindex
== -1) ? gtotal
: saveindex
+ 1;
572 struct save_game_s
*s
;
574 if (filename
[0] != '/' && config
.savedirectory
) {
575 if (stat(config
.savedirectory
, &st
) == -1) {
576 if (errno
== ENOENT
) {
577 if (mkdir(config
.savedirectory
, 0755) == -1) {
578 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
,
584 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
,
590 stat(config
.savedirectory
, &st
);
592 if (!S_ISDIR(st
.st_mode
)) {
593 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
, _("Not a directory."));
597 snprintf(buf
, sizeof(buf
), "%s/%s", config
.savedirectory
, filename
);
601 if (access(filename
, W_OK
) == 0) {
602 s
= Malloc(sizeof(struct save_game_s
));
603 s
->filename
= strdup(filename
);
604 s
->start
= saveindex
;
606 construct_message(NULL
, _("What would you like to do?"), 0, 1, NULL
,
607 NULL
, s
, do_save_game_overwrite_confirm
, 0, 0,
608 "%s \"%s\"\nPress \"%ls\" to append to this file, \"%ls\" to overwrite or any other key to cancel.",
609 _("File exists:"), filename
, append_wchar
,
614 if (do_game_write(filename
, "a", saveindex
, end
))
615 update_status_notify(gp
, "%s", _("Save game failed."));
617 update_status_notify(gp
, "%s", _("Game saved."));
620 static int castling_state(GAME g
, BOARD b
, int row
, int col
, int piece
, int mod
)
622 if (pgn_piece_to_int(piece
) == ROOK
&& col
== 7
624 (TEST_FLAG(g
->flags
, GF_WK_CASTLE
) || mod
) &&
625 pgn_piece_to_int(b
[7][4].icon
) == KING
&& isupper(piece
)) {
627 TOGGLE_FLAG(g
->flags
, GF_WK_CASTLE
);
630 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 0
632 (TEST_FLAG(g
->flags
, GF_WQ_CASTLE
) || mod
) &&
633 pgn_piece_to_int(b
[7][4].icon
) == KING
&& isupper(piece
)) {
635 TOGGLE_FLAG(g
->flags
, GF_WQ_CASTLE
);
638 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 7
640 (TEST_FLAG(g
->flags
, GF_BK_CASTLE
) || mod
) &&
641 pgn_piece_to_int(b
[0][4].icon
) == KING
&& islower(piece
)) {
643 TOGGLE_FLAG(g
->flags
, GF_BK_CASTLE
);
646 else if (pgn_piece_to_int(piece
) == ROOK
&& col
== 0
648 (TEST_FLAG(g
->flags
, GF_BQ_CASTLE
) || mod
) &&
649 pgn_piece_to_int(b
[0][4].icon
) == KING
&& islower(piece
)) {
651 TOGGLE_FLAG(g
->flags
, GF_BQ_CASTLE
);
654 else if (pgn_piece_to_int(piece
) == KING
&& col
== 4
656 (mod
|| (pgn_piece_to_int(b
[7][7].icon
) == ROOK
&&
657 TEST_FLAG(g
->flags
, GF_WK_CASTLE
))
659 (pgn_piece_to_int(b
[7][0].icon
) == ROOK
&&
660 TEST_FLAG(g
->flags
, GF_WQ_CASTLE
))) && isupper(piece
)) {
662 if (TEST_FLAG(g
->flags
, GF_WK_CASTLE
) ||
663 TEST_FLAG(g
->flags
, GF_WQ_CASTLE
))
664 CLEAR_FLAG(g
->flags
, GF_WK_CASTLE
|GF_WQ_CASTLE
);
666 SET_FLAG(g
->flags
, GF_WK_CASTLE
|GF_WQ_CASTLE
);
670 else if (pgn_piece_to_int(piece
) == KING
&& col
== 4
672 (mod
|| (pgn_piece_to_int(b
[0][7].icon
) == ROOK
&&
673 TEST_FLAG(g
->flags
, GF_BK_CASTLE
))
675 (pgn_piece_to_int(b
[0][0].icon
) == ROOK
&&
676 TEST_FLAG(g
->flags
, GF_BQ_CASTLE
))) && islower(piece
)) {
678 if (TEST_FLAG(g
->flags
, GF_BK_CASTLE
) ||
679 TEST_FLAG(g
->flags
, GF_BQ_CASTLE
))
680 CLEAR_FLAG(g
->flags
, GF_BK_CASTLE
|GF_BQ_CASTLE
);
682 SET_FLAG(g
->flags
, GF_BK_CASTLE
|GF_BQ_CASTLE
);
690 #define IS_ENPASSANT(c) (c == 'x') ? CP_BOARD_ENPASSANT : isupper(c) ? CP_BOARD_WHITE : CP_BOARD_BLACK
691 #define ATTRS(cp) (cp & (A_BOLD|A_STANDOUT|A_BLINK|A_DIM|A_UNDERLINE|A_INVIS|A_REVERSE))
696 w_pawn_wchar
= str_to_wchar (config
.utf8_pieces
? "♙" : "P");
697 w_rook_wchar
= str_to_wchar (config
.utf8_pieces
? "♖" : "R");
698 w_bishop_wchar
= str_to_wchar (config
.utf8_pieces
? "♗" : "B");
699 w_knight_wchar
= str_to_wchar (config
.utf8_pieces
? "♘" : "N");
700 w_queen_wchar
= str_to_wchar (config
.utf8_pieces
? "♕" : "Q");
701 w_king_wchar
= str_to_wchar (config
.utf8_pieces
? "♔" : "K");
702 b_pawn_wchar
= str_to_wchar (config
.utf8_pieces
? "♟" : "p");
703 b_rook_wchar
= str_to_wchar (config
.utf8_pieces
? "♜" : "r");
704 b_bishop_wchar
= str_to_wchar (config
.utf8_pieces
? "♝" : "b");
705 b_knight_wchar
= str_to_wchar (config
.utf8_pieces
? "♞" : "n");
706 b_queen_wchar
= str_to_wchar (config
.utf8_pieces
? "♛" : "q");
707 b_king_wchar
= str_to_wchar (config
.utf8_pieces
? "♚" : "k");
708 empty_wchar
= str_to_wchar (" ");
709 enpassant_wchar
= str_to_wchar ("x");
713 piece_to_wchar (unsigned char p
)
726 return w_bishop_wchar
;
728 return b_bishop_wchar
;
730 return w_knight_wchar
;
732 return b_knight_wchar
;
734 return w_queen_wchar
;
736 return b_queen_wchar
;
742 return enpassant_wchar
;
748 static int piece_can_attack (GAME g
, int rank
, int file
)
750 struct userdata_s
*d
= g
->data
;
751 char *m
, *frfr
= NULL
;
753 int row
, col
, p
, v
, pi
, cpi
;
756 rotate_position(CURSOR_POSITION
);
757 rotate_position(SPS_POSITION
);
760 v
= d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].valid
;
761 pi
= pgn_piece_to_int (d
->b
[RANKTOBOARD (rank
)][FILETOBOARD (file
)].icon
);
763 ? pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
)
764 : pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].icon
);
766 if (pi
== OPEN_SQUARE
|| cpi
== OPEN_SQUARE
|| !VALIDFILE (file
)
767 || !VALIDRANK (rank
)) {
769 rotate_position(CURSOR_POSITION
);
770 rotate_position(SPS_POSITION
);
777 col
= v
? d
->c_col
: d
->sp
.scol
;
778 row
= v
? d
->c_row
: d
->sp
.srow
;
785 m
= malloc(MAX_SAN_MOVE_LEN
+1);
786 m
[0] = INTTOFILE (file
);
787 m
[1] = INTTORANK (rank
);
788 m
[2] = INTTOFILE (col
);
789 m
[3] = INTTORANK (row
);
792 if (d
->sp
.icon
&& v
) {
795 memcpy (b
, d
->b
, sizeof(BOARD
));
796 p
= b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
;
797 b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
798 pgn_int_to_piece (WHITE
, OPEN_SQUARE
);
799 b
[RANKTOBOARD (row
)][FILETOBOARD (col
)].icon
= p
;
801 e
= pgn_validate_move (g
, b
, &m
, &frfr
);
806 if (e
!= E_PGN_OK
&& pgn_piece_to_int (d
->sp
.icon
) == PAWN
) {
807 int n
= (d
->sp
.srow
== 7 && rank
== 5) ? 6 :
808 (d
->sp
.srow
== 2 && rank
== 4) ? 3 : 0;
810 if (n
&& (file
== d
->c_col
-1 || file
== d
->c_col
+1)) {
811 unsigned short flags
= g
->flags
;
813 memcpy (b
, d
->b
, sizeof(BOARD
));
814 p
= b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
;
815 b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
816 pgn_int_to_piece (WHITE
, OPEN_SQUARE
);
817 b
[RANKTOBOARD (row
)][FILETOBOARD (col
)].icon
= p
;
818 b
[RANKTOBOARD (n
)][FILETOBOARD (d
->sp
.scol
)].enpassant
= 1;
819 m
= malloc(MAX_SAN_MOVE_LEN
+1);
820 m
[0] = INTTOFILE (file
);
821 m
[1] = INTTORANK (rank
);
822 m
[2] = INTTOFILE (col
);
823 m
[3] = INTTORANK (n
);
826 SET_FLAG (g
->flags
, GF_ENPASSANT
);
827 e
= pgn_validate_move (g
, b
, &m
, &frfr
);
836 rotate_position(CURSOR_POSITION
);
837 rotate_position(SPS_POSITION
);
840 return e
== E_PGN_OK
? 1 : 0;
844 e
= pgn_validate_move (g
, d
->b
, &m
, &frfr
);
847 if (!strcmp (m
, "O-O") || !strcmp (m
, "O-O-O"))
851 int sf
= FILETOINT (frfr
[0]), sr
= RANKTOINT (frfr
[1]);
852 int df
= FILETOINT (frfr
[2]);
854 pi
= d
->b
[RANKTOBOARD (sr
)][FILETOBOARD (sf
)].icon
;
855 pi
= pgn_piece_to_int (pi
);
856 if (pi
== PAWN
&& sf
== df
)
864 rotate_position(CURSOR_POSITION
);
865 rotate_position(SPS_POSITION
);
868 return e
== E_PGN_OK
? 1 : 0;
871 void print_piece(WINDOW
*w
, int l
, int c
, char p
)
875 for (i
= 0; i
< 13; i
+= 2) {
876 if (p
== piece_chars
[i
] || p
== piece_chars
[i
+ 1]) {
877 for (y
= 0; y
< 3; y
++)
878 mvwprintw(w
, l
+ y
, c
, "%s", f_pieces
[i
+ ff
+ y
]);
885 for (y
= 0; y
< 3; y
++)
886 mvwprintw(w
, l
+ y
, c
, f_pieces
[0]);
893 for (fx
= 1; fx
< 9; fx
++) {
902 int coordofmove(GAME g
, char *move
, int yorx
)
906 if (!strcmp (move
, "O-O")) {
908 return g
->turn
== WHITE
? 8 : 1;
912 else if (!strcmp (move
, "O-O-O")) {
914 return g
->turn
== WHITE
? 8 : 1;
920 if (strchr(move
, '+') && strchr(move
, '=')) {
924 else if (!strchr(move
, '+') && strchr(move
, '=')) {
928 else if (strchr(move
, '+') && !strchr(move
, '=')) {
938 return move
[l
- y
] - 48;
940 for (i
= 0; i
< 8; i
++) {
941 if (move
[l
- x
] == "abcdefgh"[i
])
949 static int is_prev_move (struct userdata_s
*d
, int brow
, int bcol
,
953 row
== ROWTOMATRIX(((rotate
) ? inv_int(d
->pm_row
) : d
->pm_row
)) &&
954 col
== COLTOMATRIX(((rotate
) ? inv_int(d
->pm_col
) : d
->pm_col
)))
956 brow
+ 1 == ((rotate
) ? d
->pm_row
: inv_int(d
->pm_row
)) &&
957 bcol
+ 1 == ((rotate
) ? inv_int(d
->pm_col
) : d
->pm_col
)))
963 void update_board_window(GAME g
)
966 int bcol
= 0, brow
= 0;
967 int l
= config
.coordsyleft
;
968 int maxy
= BOARD_HEIGHT
, maxx
= BOARD_WIDTH
;
969 int ncols
= 0, offset
= 1;
970 int rowr
= (MEGA_BOARD
) ? 6 : (BIG_BOARD
) ? 4 : 2;
971 int colr
= (MEGA_BOARD
) ? 12 : (BIG_BOARD
) ? 8 : 4;
972 unsigned coords_y
= 8, cxgc
= 0;
974 struct userdata_s
*d
= g
->data
;
977 h
= pgn_history_by_n(g
->hp
, g
->hindex
- 1);
978 d
->pm_row
= (h
&& h
->move
&& d
->mode
== MODE_PLAY
)
979 ? coordofmove(g
, h
->move
, 1) : 0;
980 d
->pm_col
= (h
&& h
->move
&& d
->mode
== MODE_PLAY
)
981 ? coordofmove(g
, h
->move
, 0) : 0;
983 if (d
->mode
!= MODE_PLAY
&& d
->mode
!= MODE_EDIT
)
984 update_cursor(g
, g
->hindex
);
1001 for (row
= 0; row
< maxy
; row
++) {
1015 for (col
= 0; col
< maxx
; col
++) {
1017 chtype attrs
= 0, old_attrs
= 0;
1022 if (row
== 0 || row
== maxy
- 2) {
1024 mvwaddch(boardw
, row
, col
+ l
,
1026 ? ACS_LLCORNER
| CP_BOARD_GRAPHICS
1027 : ACS_ULCORNER
| CP_BOARD_GRAPHICS
));
1028 else if (col
== maxx
- 2)
1029 mvwaddch(boardw
, row
, col
+ l
,
1031 ? ACS_LRCORNER
| CP_BOARD_GRAPHICS
1032 : ACS_URCORNER
| CP_BOARD_GRAPHICS
));
1033 else if (!(col
% colr
))
1034 mvwaddch(boardw
, row
, col
+ l
,
1036 ? ACS_BTEE
| CP_BOARD_GRAPHICS
1037 : ACS_TTEE
| CP_BOARD_GRAPHICS
));
1039 if (col
!= maxx
- 1)
1040 mvwaddch(boardw
, row
, col
+ l
,
1041 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
1047 if ((row
% 2) && col
== maxx
- 1 &&
1048 (coords_y
> 0 && coords_y
< 9)) {
1049 wattron(boardw
, CP_BOARD_COORDS
);
1051 (BIG_BOARD
) ? row
* ((MEGA_BOARD
) ? 3 : 2)
1052 : row
, (l
) ? 0 : col
, "%d",
1053 (rotate
) ? coords_y
++ : coords_y
--);
1054 wattroff(boardw
, CP_BOARD_COORDS
);
1058 if ((col
== 0 || col
== maxx
- 2) && row
!= maxy
- 1) {
1060 mvwaddch(boardw
, row
, col
+ l
,
1061 LINE_GRAPHIC((col
) ?
1062 ACS_RTEE
| CP_BOARD_GRAPHICS
:
1063 ACS_LTEE
| CP_BOARD_GRAPHICS
));
1065 mvwaddch(boardw
, row
, col
+ l
,
1066 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
1071 if ((row
% rowr
) && !(col
% colr
) && row
!= maxy
- 1) {
1072 mvwaddch(boardw
, row
, col
+ l
,
1073 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
1077 if (!(col
% colr
) && row
!= maxy
- 1) {
1078 mvwaddch(boardw
, row
, col
+ l
,
1079 LINE_GRAPHIC(ACS_PLUS
| CP_BOARD_GRAPHICS
));
1086 attrwhich
= (cb
[brow
][bcol
]) ? WHITE
: BLACK
;
1093 if (((ncols
% 2) && !(offset
% 2))
1094 || (!(ncols
% 2) && (offset
% 2)))
1100 if (BIG_BOARD
&& rotate
) {
1101 brow
= inv_int(brow
+ 1) - 1;
1102 bcol
= inv_int(bcol
+ 1) - 1;
1106 p
= d
->b
[brow
][bcol
].icon
;
1108 p
= d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].icon
;
1110 int pi
= pgn_piece_to_int(p
);
1112 if (config
.details
&&
1113 ((!BIG_BOARD
&& d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].enpassant
)
1114 || (BIG_BOARD
&& d
->b
[brow
][bcol
].enpassant
))) {
1116 attrs
= mix_cp(CP_BOARD_ENPASSANT
,
1117 (attrwhich
== WHITE
) ? CP_BOARD_WHITE
: CP_BOARD_BLACK
,
1118 ATTRS(CP_BOARD_ENPASSANT
), A_FG_B_BG
);
1121 if (config
.showattacks
&& config
.details
1122 && piece_can_attack (g
,
1123 BIG_BOARD
? inv_int (brow
+1) : brow
,
1124 BIG_BOARD
? bcol
+1 : bcol
)) {
1125 attrs
= CP_BOARD_ATTACK
;
1130 if (config
.validmoves
&&
1131 ((!BIG_BOARD
&& d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].valid
)
1132 || (BIG_BOARD
&& d
->b
[brow
][bcol
].valid
))) {
1136 if (attrwhich
== WHITE
)
1137 attrs
= mix_cp(CP_BOARD_MOVES_WHITE
,
1139 ATTRS(CP_BOARD_MOVES_WHITE
),
1142 attrs
= mix_cp(CP_BOARD_MOVES_BLACK
,
1144 ATTRS(CP_BOARD_MOVES_BLACK
),
1147 else if (p
!= 'x' && !can_attack
)
1148 attrs
= (attrwhich
== WHITE
) ? CP_BOARD_WHITE
: CP_BOARD_BLACK
;
1150 if (BIG_BOARD
&& rotate
) {
1151 brow
= inv_int(brow
+ 1) - 1;
1152 bcol
= inv_int(bcol
+ 1) - 1;
1155 if ((!BIG_BOARD
&& row
== ROWTOMATRIX(d
->c_row
)
1156 && col
== COLTOMATRIX(d
->c_col
))
1157 || (BIG_BOARD
&& brow
+ 1 == inv_int(d
->c_row
)
1158 && bcol
+ 1 == d
->c_col
)) {
1159 attrs
= mix_cp(CP_BOARD_CURSOR
, IS_ENPASSANT(p
),
1160 ATTRS(CP_BOARD_CURSOR
), B_FG_A_BG
);
1163 else if ((!BIG_BOARD
&& row
== ROWTOMATRIX(d
->sp
.srow
) &&
1164 col
== COLTOMATRIX(d
->sp
.scol
)) ||
1165 (BIG_BOARD
&& brow
+ 1 == inv_int(d
->sp
.srow
) &&
1166 bcol
+ 1 == d
->sp
.scol
)) {
1167 attrs
= mix_cp(CP_BOARD_SELECTED
, IS_ENPASSANT(p
),
1168 ATTRS(CP_BOARD_SELECTED
), B_FG_A_BG
);
1171 else if (is_prev_move (d
, brow
, bcol
, row
, col
) && !valid
)
1173 attrs
= mix_cp(CP_BOARD_PREVMOVE
, IS_ENPASSANT(p
),
1174 ATTRS(CP_BOARD_PREVMOVE
), B_FG_A_BG
);
1178 if (row
== maxy
- 1)
1182 int n
= is_prev_move (d
, brow
, bcol
, row
, col
);
1183 chtype a
= n
&& !valid
1184 ? CP_BOARD_PREVMOVE
: attrwhich
== WHITE
? valid
? CP_BOARD_MOVES_WHITE
: CP_BOARD_WHITE
: valid
? CP_BOARD_MOVES_BLACK
: CP_BOARD_BLACK
;
1185 attrs
= mix_cp(CP_BOARD_ATTACK
, a
,
1186 ATTRS (CP_BOARD_ATTACK
), A_FG_B_BG
);
1191 wmove(boardw
, row
, col
+ ((MEGA_BOARD
) ? 5 : 3) + l
);
1193 mvwaddch(boardw
, row
, col
+ l
, ' ' | attrs
);
1195 if (row
== maxy
- 1 && cxgc
< 8) {
1196 waddch(boardw
, "abcdefgh"[(BIG_BOARD
) ? bcol
: bcol
- 1] | CP_BOARD_COORDS
);
1200 if (old_attrs
== -1) {
1207 if (pi
!= OPEN_SQUARE
&& p
!= 'x' && !can_attack
) {
1208 if (attrwhich
== WHITE
) {
1210 attrs
= CP_BOARD_W_W
;
1212 attrs
= CP_BOARD_W_B
;
1216 attrs
= CP_BOARD_B_W
;
1218 attrs
= CP_BOARD_B_B
;
1224 if (config
.details
&& !can_attack
1225 && castling_state(g
, d
->b
,
1226 (rotate
) ? inv_int(brow
+ 1) - 1: brow
,
1227 (rotate
) ? inv_int(bcol
+ 1) - 1: bcol
, p
, 0))
1228 attrs
= mix_cp(CP_BOARD_CASTLING
, attrs
,
1229 ATTRS(CP_BOARD_CASTLING
),
1233 if (config
.details
&& !can_attack
1234 && castling_state(g
, d
->b
, RANKTOBOARD (brow
),
1235 FILETOBOARD (bcol
), p
, 0)) {
1236 attrs
= mix_cp(CP_BOARD_CASTLING
, attrs
,
1237 ATTRS(CP_BOARD_CASTLING
),
1243 // FIXME: Reimpresión de piezas(+4).
1245 wattron (boardw
, attrs
);
1247 for (i
= 0; i
< 5; i
++)
1248 mvwprintw(boardw
, i
+ brow
* 6 + 1,
1251 if (pi
!= OPEN_SQUARE
)
1252 print_piece(boardw
, brow
* 6 + 2,
1253 bcol
* 12 + 3 + l
, p
);
1256 print_piece(boardw
, brow
* 4 + 1,
1258 (pi
!= OPEN_SQUARE
) ? p
: 0);
1261 wattroff (boardw
, attrs
);
1266 wattron (boardw
, attrs
);
1267 waddwstr (boardw
, piece_to_wchar (pi
!= OPEN_SQUARE
? p
: 0));
1268 wattroff (boardw
, attrs
);
1275 col
+= (MEGA_BOARD
) ? 10 : 6;
1277 waddch(boardw
, ' ' | attrs
);
1295 if (col
!= maxx
- 1)
1296 mvwaddch(boardw
, row
, col
+ l
,
1297 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
1317 void invalid_move(int n
, int e
, const char *m
)
1319 if (curses_initialized
)
1320 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s \"%s\" (round #%i)", (e
== E_PGN_AMBIGUOUS
)
1321 ? _("Ambiguous move") : _("Invalid move"), m
, n
);
1323 warnx("%s: %s \"%s\" (round #%i)", loadfile
, (e
== E_PGN_AMBIGUOUS
)
1324 ? _("Ambiguous move") : _("Invalid move"), m
, n
);
1327 void gameover(GAME g
)
1329 struct userdata_s
*d
= g
->data
;
1331 SET_FLAG(g
->flags
, GF_GAMEOVER
);
1332 d
->mode
= MODE_HISTORY
;
1336 static void update_clock(GAME g
, struct itimerval it
)
1338 struct userdata_s
*d
= g
->data
;
1340 if (TEST_FLAG(d
->flags
, CF_CLOCK
) && g
->turn
== WHITE
) {
1341 d
->wclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1342 d
->wclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1344 if (d
->wclock
.elapsed
.tv_usec
> 1000000 - 1) {
1345 d
->wclock
.elapsed
.tv_sec
+= d
->wclock
.elapsed
.tv_usec
/ 1000000;
1346 d
->wclock
.elapsed
.tv_usec
= d
->wclock
.elapsed
.tv_usec
% 1000000;
1349 if (d
->wclock
.tc
[d
->wclock
.tcn
][1] &&
1350 d
->wclock
.elapsed
.tv_sec
>= d
->wclock
.tc
[d
->wclock
.tcn
][1]) {
1351 pgn_tag_add(&g
->tag
, "Result", "0-1");
1355 else if (TEST_FLAG(d
->flags
, CF_CLOCK
) && g
->turn
== BLACK
) {
1356 d
->bclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1357 d
->bclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1359 if (d
->bclock
.elapsed
.tv_usec
> 1000000 - 1) {
1360 d
->bclock
.elapsed
.tv_sec
+= d
->bclock
.elapsed
.tv_usec
/ 1000000;
1361 d
->bclock
.elapsed
.tv_usec
= d
->bclock
.elapsed
.tv_usec
% 1000000;
1364 if (d
->bclock
.tc
[d
->bclock
.tcn
][1] &&
1365 d
->bclock
.elapsed
.tv_sec
>= d
->bclock
.tc
[d
->bclock
.tcn
][1]) {
1366 pgn_tag_add(&g
->tag
, "Result", "1-0");
1371 d
->elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1372 d
->elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1374 if (d
->elapsed
.tv_usec
> 1000000 - 1) {
1375 d
->elapsed
.tv_sec
+= d
->elapsed
.tv_usec
/ 1000000;
1376 d
->elapsed
.tv_usec
= d
->elapsed
.tv_usec
% 1000000;
1380 static void update_time_control(GAME g
)
1382 struct userdata_s
*d
= g
->data
;
1383 struct clock_s
*clk
= (g
->turn
== WHITE
) ? &d
->wclock
: &d
->bclock
;
1386 clk
->tc
[clk
->tcn
][1] += clk
->incr
;
1388 if (!clk
->tc
[clk
->tcn
][1])
1393 if (!clk
->tc
[clk
->tcn
][0] || clk
->move
>= clk
->tc
[clk
->tcn
][0]) {
1395 clk
->tc
[clk
->tcn
+ 1][1] += abs(clk
->elapsed
.tv_sec
- clk
->tc
[clk
->tcn
][1]);
1396 memset(&clk
->elapsed
, 0, sizeof(clk
->elapsed
));
1401 void update_history_window(GAME g
)
1403 char buf
[HISTORY_WIDTH
- 1];
1406 int t
= pgn_history_total(g
->hp
);
1408 n
= (g
->hindex
+ 1) / 2;
1411 total
= (t
+ 1) / 2;
1416 snprintf(buf
, sizeof(buf
), "%u %s %u%s", n
, _("of"), total
,
1417 (movestep
== 1) ? _(" (ply)") : "");
1419 strncpy(buf
, _("not available"), sizeof(buf
)-1);
1421 buf
[sizeof(buf
)-1] = 0;
1422 mvwprintw(historyw
, 2, 1, "%*s %-*s", 10, _("Move:"),
1423 HISTORY_WIDTH
- 14, buf
);
1425 h
= pgn_history_by_n(g
->hp
, g
->hindex
);
1426 snprintf(buf
, sizeof(buf
), "%s",
1427 (h
&& h
->move
) ? h
->move
1428 : (LINES
< 24) ? _("empty") : _("not available"));
1431 if (h
&& ((h
->comment
) || h
->nag
[0])) {
1432 strncat(buf
, _(" (Annotated"), sizeof(buf
)-1);
1437 strncat(buf
, (n
) ? ",+" : " (+", sizeof(buf
)-1);
1442 strncat(buf
, (n
) ? ",-" : " (-", sizeof(buf
)-1);
1447 strncat(buf
, ")", sizeof(buf
)-1);
1449 mvwprintw(historyw
, 3, ((LINES
< 24) ? 17 : 1), "%s %-*s",
1450 (LINES
< 24) ? _("Next:") :_("Next move:"),
1451 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1453 h
= pgn_history_by_n(g
->hp
, g
->hindex
- 1);
1454 snprintf(buf
, sizeof(buf
), "%s",
1455 (h
&& h
->move
) ? h
->move
1456 : (LINES
< 24) ? _("empty") : _("not available"));
1459 if (h
&& ((h
->comment
) || h
->nag
[0])) {
1460 strncat(buf
, _(" (Annotated"), sizeof(buf
)-1);
1465 strncat(buf
, (n
) ? ",+" : " (+", sizeof(buf
)-1);
1470 strncat(buf
, (n
) ? ",-" : " (-", sizeof(buf
)-1);
1475 strncat(buf
, ")", sizeof(buf
)-1);
1477 mvwprintw(historyw
, ((LINES
< 24) ? 3 : 4), 1, "%s %-*s",
1478 (LINES
< 24) ? _("Prev.:") : _("Prev move:"),
1479 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1482 void do_validate_move(char *move
)
1484 struct userdata_s
*d
= gp
->data
;
1488 if (TEST_FLAG(d
->flags
, CF_HUMAN
)) {
1489 if ((n
= pgn_parse_move(gp
, d
->b
, &move
, &frfr
)) != E_PGN_OK
) {
1490 invalid_move(d
->n
+ 1, n
, move
);
1494 update_time_control(gp
);
1495 pgn_history_add(gp
, d
->b
, move
);
1496 pgn_switch_turn(gp
);
1499 if ((n
= pgn_validate_move(gp
, d
->b
, &move
, &frfr
)) != E_PGN_OK
) {
1500 invalid_move(d
->n
+ 1, n
, move
);
1504 add_engine_command(gp
, ENGINE_THINKING
, "%s\n",
1505 (config
.engine_protocol
== 1) ? frfr
: move
);
1508 d
->sp
.srow
= d
->sp
.scol
= d
->sp
.icon
= 0;
1510 if (config
.validmoves
)
1511 pgn_reset_valid_moves(d
->b
);
1513 if (TEST_FLAG(gp
->flags
, GF_GAMEOVER
))
1514 d
->mode
= MODE_HISTORY
;
1516 SET_FLAG(d
->flags
, CF_MODIFIED
);
1520 update_history_window(gp
);
1521 update_board_window(gp
);
1525 void do_promotion_piece_finalize(WIN
*win
)
1527 char *p
, *str
= win
->data
;
1529 if (pgn_piece_to_int(win
->c
) == -1)
1532 p
= str
+ strlen(str
);
1533 *p
++ = toupper(win
->c
);
1535 do_validate_move(str
);
1540 static void move_to_engine(GAME g
)
1542 struct userdata_s
*d
= g
->data
;
1546 if (config
.validmoves
&&
1547 !d
->b
[RANKTOBOARD(d
->sp
.row
)][FILETOBOARD(d
->sp
.col
)].valid
)
1550 str
= Malloc(MAX_SAN_MOVE_LEN
+ 1);
1551 snprintf(str
, MAX_SAN_MOVE_LEN
+ 1, "%c%i%c%i",
1552 _("abcdefgh")[d
->sp
.scol
- 1],
1553 d
->sp
.srow
, _("abcdefgh")[d
->sp
.col
- 1], d
->sp
.row
);
1555 piece
= pgn_piece_to_int(d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
);
1557 if (piece
== PAWN
&& (d
->sp
.row
== 8 || d
->sp
.row
== 1)) {
1558 construct_message(_("Select Pawn Promotion Piece"), _ ("R/N/B/Q"), 1, 1, NULL
, NULL
,
1559 str
, do_promotion_piece_finalize
, 0, 0, "%s", _("R = Rook, N = Knight, B = Bishop, Q = Queen"));
1563 do_validate_move(str
);
1567 static char *clock_to_char(long n
)
1569 static char buf
[16];
1570 int h
= 0, m
= 0, s
= 0;
1573 m
= (n
% 3600) / 60;
1574 s
= (n
% 3600) % 60;
1575 snprintf(buf
, sizeof(buf
), "%.2i:%.2i:%.2i", h
, m
, s
);
1579 static char *timeval_to_char(struct timeval t
, long limit
)
1582 int h
= 0, m
= 0, s
= 0;
1583 int n
= limit
? abs(limit
- t
.tv_sec
) : 0;
1586 m
= (n
% 3600) / 60;
1587 s
= (n
% 3600) % 60;
1588 snprintf(buf
, sizeof(buf
), "%.2i:%.2i:%.2i", h
, m
, s
);
1592 static char *time_control_status(struct clock_s
*clk
)
1594 static char buf
[80] = {0};
1598 if (clk
->tc
[clk
->tcn
][0] && clk
->tc
[clk
->tcn
+ 1][1])
1599 snprintf(buf
, sizeof(buf
), " M%.2i/%s", abs(clk
->tc
[clk
->tcn
][0] - clk
->move
),
1600 clock_to_char(clk
->tc
[clk
->tcn
+ 1][1]));
1601 else if (!clk
->incr
)
1606 strncat(buf
, " I", sizeof(buf
)-1);
1607 strncat(buf
, itoa(clk
->incr
, buf
), sizeof(buf
)-1);
1613 void update_status_window(GAME g
)
1617 char tmp
[15] = {0}, *engine
, *mode
;
1623 struct userdata_s
*d
= g
->data
;
1627 if (!curses_initialized
)
1630 getmaxyx(statusw
, maxy
, maxx
);
1637 wchar_t *loadfilew
= loadfile
[0] ? str_etc (loadfile
, w
, 1) : str_to_wchar (_ ("not available"));
1638 mvwprintw(statusw
, y
++, 1, "%*s %-*ls", 7, _("File:"), w
, loadfilew
);
1640 snprintf(buf
, len
, "%i %s %i", gindex
+ 1, _("of"), gtotal
);
1641 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Game:"), w
, buf
);
1646 if (config
.details
) {
1651 if (TEST_FLAG(d
->flags
, CF_DELETE
)) {
1659 if (TEST_FLAG(g
->flags
, GF_PERROR
)) {
1667 if (TEST_FLAG(d
->flags
, CF_MODIFIED
)) {
1675 pgn_config_get(PGN_STRICT_CASTLING
, &n
);
1685 if (TEST_FLAG(d
->flags
, CF_PERL
)) {
1695 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Flags:"), w
, (tmp
[0]) ? tmp
: "-");
1699 mode
= _("move history");
1708 mode
= _("(empty value)");
1712 snprintf(buf
, len
- 1, "%*s %s", 7, _("Mode:"), mode
);
1714 if (d
->mode
== MODE_PLAY
) {
1715 if (TEST_FLAG(d
->flags
, CF_HUMAN
))
1716 strncat(buf
, _(" (human/human)"), len
- 1);
1717 else if (TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
))
1718 strncat(buf
, _(" (engine/engine)"), len
- 1);
1720 strncat(buf
, (d
->play_mode
== PLAY_EH
) ?
1721 _(" (engine/human)") : _(" (human/engine)"), len
- 1);
1725 mvwprintw(statusw
, y
++, 1, "%-*s", len
, buf
);
1729 switch (d
->engine
->status
) {
1730 case ENGINE_THINKING
:
1731 engine
= _("pondering...");
1734 engine
= _("ready");
1736 case ENGINE_INITIALIZING
:
1737 engine
= _("initializing...");
1739 case ENGINE_OFFLINE
:
1740 engine
= _("offline");
1743 engine
= _("(empty value)");
1748 engine
= _("offline");
1750 mvwprintw(statusw
, y
, 1, "%*s %-*s", 7, _("Engine:"), w
, " ");
1751 wattron(statusw
, CP_STATUS_ENGINE
);
1752 mvwaddstr(statusw
, y
++, 9, engine
);
1753 wattroff(statusw
, CP_STATUS_ENGINE
);
1755 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Turn:"), w
,
1756 (g
->turn
== WHITE
) ? _("white") : _("black"));
1758 strncpy(tmp
, _("white"), sizeof(tmp
)-1);
1759 tmp
[0] = toupper(tmp
[0]);
1760 snprintf(t
, sizeof(t
), "%s%s",
1761 timeval_to_char(d
->wclock
.elapsed
, d
->wclock
.tc
[d
->wclock
.tcn
][1]),
1762 time_control_status(&d
->wclock
));
1763 mvwprintw(statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1765 strncpy(tmp
, _("black"), sizeof(tmp
)-1);
1766 tmp
[0] = toupper(tmp
[0]);
1767 snprintf(t
, sizeof(t
), "%s%s",
1768 timeval_to_char(d
->bclock
.elapsed
, d
->bclock
.tc
[d
->bclock
.tcn
][1]),
1769 time_control_status(&d
->bclock
));
1770 mvwprintw(statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1772 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Total:"), w
,
1773 clock_to_char(d
->elapsed
.tv_sec
));
1775 // for (i = 0; i < STATUS_WIDTH; i++)
1776 // mvwprintw(stdscr, STATUS_HEIGHT, i, " ");
1779 status
.notify
= str_to_wchar(_("Type F1 for help"));
1781 wattron(stdscr
, CP_STATUS_NOTIFY
);
1782 for (i
= (config
.boardleft
) ? BOARD_WIDTH
: 0;
1783 i
< ((config
.boardleft
) ? COLS
: STATUS_WIDTH
); i
++)
1784 mvwprintw(stdscr
, STATUS_HEIGHT
, i
, " ");
1785 mvwprintw(stdscr
, STATUS_HEIGHT
, CENTERX(STATUS_WIDTH
, status
.notify
)
1786 + ((config
.boardleft
) ? BOARD_WIDTH
: 0),
1787 "%ls", status
.notify
);
1788 wattroff(stdscr
, CP_STATUS_NOTIFY
);
1791 wchar_t *translate_tag_name(const char *tag
)
1793 if (!strcmp (tag
, "Event"))
1794 return str_to_wchar (translatable_tag_names
[0]);
1795 else if (!strcmp (tag
, "Site"))
1796 return str_to_wchar (translatable_tag_names
[1]);
1797 else if (!strcmp (tag
, "Date"))
1798 return str_to_wchar (translatable_tag_names
[2]);
1799 else if (!strcmp (tag
, "Round"))
1800 return str_to_wchar (translatable_tag_names
[3]);
1801 else if (!strcmp (tag
, "White"))
1802 return str_to_wchar (translatable_tag_names
[4]);
1803 else if (!strcmp (tag
, "Black"))
1804 return str_to_wchar (translatable_tag_names
[5]);
1805 else if (!strcmp (tag
, "Result"))
1806 return str_to_wchar (translatable_tag_names
[6]);
1808 return str_to_wchar (tag
);
1811 void update_tag_window(TAG
**t
)
1816 for (i
= 0; t
[i
]; i
++) {
1817 wchar_t *namewc
= translate_tag_name(t
[i
]->name
);
1825 w
= TAG_WIDTH
- namel
- 4;
1827 for (i
= 0; t
[i
] && i
< TAG_HEIGHT
- 3; i
++) {
1828 wchar_t *namewc
= translate_tag_name(t
[i
]->name
);
1829 wchar_t *valuewc
= str_etc(t
[i
]->value
, w
, 0);
1831 mvwprintw(tagw
, (i
+ 2), 1, "%*ls: %-*ls", namel
, namewc
, w
, valuewc
);
1836 for (; i
< TAG_HEIGHT
- 3; i
++)
1837 mvwprintw(tagw
, (i
+ 2), 1, "%*s", namel
+ w
+ 2, " ");
1840 void append_enginebuf(GAME g
, char *line
)
1843 struct userdata_s
*d
= g
->data
;
1845 if (d
->engine
->enginebuf
)
1846 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++);
1848 if (i
>= LINES
- 3) {
1849 free(d
->engine
->enginebuf
[0]);
1851 for (i
= 0; d
->engine
->enginebuf
[i
+1]; i
++)
1852 d
->engine
->enginebuf
[i
] = d
->engine
->enginebuf
[i
+1];
1854 d
->engine
->enginebuf
[i
] = strdup(line
);
1857 d
->engine
->enginebuf
= Realloc(d
->engine
->enginebuf
, (i
+ 2) * sizeof(char *));
1858 d
->engine
->enginebuf
[i
++] = strdup(line
);
1859 d
->engine
->enginebuf
[i
] = NULL
;
1863 void update_engine_window(GAME g
)
1866 struct userdata_s
*d
= g
->data
;
1868 wmove(enginew
, 0, 0);
1871 if (d
->engine
&& d
->engine
->enginebuf
) {
1872 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++)
1873 mvwprintw(enginew
, i
+ 2, 1, "%s", d
->engine
->enginebuf
[i
]);
1876 window_draw_title(enginew
, _("Engine IO Window"), COLS
, CP_MESSAGE_TITLE
,
1880 void update_all(GAME g
)
1882 struct userdata_s
*d
= g
->data
;
1885 * In the middle of a macro. Don't update the screen.
1887 if (macro_match
!= -1)
1890 wmove(boardw
, ROWTOMATRIX(d
->c_row
), COLTOMATRIX(d
->c_col
));
1891 update_board_window(g
);
1892 update_status_window(g
);
1893 update_history_window(g
);
1894 update_tag_window(g
->tag
);
1895 update_engine_window(g
);
1900 static void game_next_prev(GAME g
, int n
, int count
)
1906 if (gindex
+ count
> gtotal
- 1) {
1908 gindex
= gtotal
- 1;
1916 if (gindex
- count
< 0) {
1920 gindex
= gtotal
- 1;
1929 static void delete_game(int which
)
1932 struct userdata_s
*d
;
1934 for (i
= 0; i
< gtotal
; i
++) {
1937 if (i
== which
|| TEST_FLAG(d
->flags
, CF_DELETE
)) {
1940 free_userdata_once(game
[i
]);
1943 for (n
= i
; n
+1 < gtotal
; n
++)
1944 game
[n
] = game
[n
+1];
1953 if (which
+ 1 >= gtotal
)
1954 gindex
= gtotal
- 1;
1959 gindex
= gtotal
- 1;
1962 gp
->hp
= gp
->history
;
1966 * FIXME find across multiple games.
1968 static int find_move_exp(GAME g
, regex_t r
, int which
, int count
)
1976 incr
= (which
== 0) ? -1 : 1;
1978 for (i
= g
->hindex
+ incr
- 1, found
= 0; ; i
+= incr
) {
1979 if (i
== g
->hindex
- 1)
1982 if (i
>= pgn_history_total(g
->hp
))
1985 i
= pgn_history_total(g
->hp
) - 1;
1988 ret
= regexec(&r
, g
->hp
[i
]->move
, 0, 0, 0);
1991 if (count
== ++found
) {
1996 if (ret
!= REG_NOMATCH
) {
1997 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
1998 cmessage(_("Error Matching Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
2007 static int toggle_delete_flag(int n
)
2010 struct userdata_s
*d
= game
[n
]->data
;
2012 TOGGLE_FLAG(d
->flags
, CF_DELETE
);
2015 for (i
= x
= 0; i
< gtotal
; i
++) {
2018 if (TEST_FLAG(d
->flags
, CF_DELETE
))
2023 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
2025 CLEAR_FLAG(d
->flags
, CF_DELETE
);
2032 static int find_game_exp(char *str
, int which
, int count
)
2034 char *nstr
= NULL
, *exp
= NULL
;
2038 char buf
[255] = {0}, *tmp
;
2041 int incr
= (which
== 0) ? -(1) : 1;
2043 strncpy(buf
, str
, sizeof(buf
)-1);
2046 if (strstr(tmp
, ":") != NULL
) {
2047 nstr
= strsep(&tmp
, ":");
2049 if ((ret
= regcomp(&nexp
, nstr
,
2050 REG_ICASE
|REG_EXTENDED
|REG_NOSUB
)) != 0) {
2051 regerror(ret
, &nexp
, errbuf
, sizeof(errbuf
));
2052 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
2060 while (*exp
&& isspace(*exp
))
2066 if ((ret
= regcomp(&vexp
, exp
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
2067 regerror(ret
, &vexp
, errbuf
, sizeof(errbuf
));
2068 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
2075 for (g
= gindex
+ incr
, found
= 0; ; g
+= incr
) {
2086 for (t
= 0; game
[g
]->tag
[t
]; t
++) {
2088 if (regexec(&nexp
, game
[g
]->tag
[t
]->name
, 0, 0, 0) == 0) {
2089 if (regexec(&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0) {
2090 if (count
== ++found
) {
2098 if (regexec(&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0) {
2099 if (count
== ++found
) {
2121 * Updates the notification line in the status window then refreshes the
2124 void update_status_notify(GAME g
, char *fmt
, ...)
2127 #ifdef HAVE_VASPRINTF
2134 if (status
.notify
) {
2135 free(status
.notify
);
2136 status
.notify
= NULL
;
2143 #ifdef HAVE_VASPRINTF
2144 vasprintf(&line
, fmt
, ap
);
2146 vsnprintf(line
, sizeof(line
), fmt
, ap
);
2151 free(status
.notify
);
2153 status
.notify
= str_to_wchar(line
);
2155 #ifdef HAVE_VASPRINTF
2160 int rav_next_prev(GAME g
, BOARD b
, int n
)
2164 if ((!g
->ravlevel
&& g
->hindex
&& g
->hp
[g
->hindex
- 1]->rav
== NULL
) ||
2165 (!g
->ravlevel
&& !g
->hindex
&& g
->hp
[g
->hindex
]->rav
== NULL
) ||
2166 (g
->ravlevel
&& g
->hp
[g
->hindex
]->rav
== NULL
))
2169 g
->rav
= Realloc(g
->rav
, (g
->ravlevel
+ 1) * sizeof(RAV
));
2170 g
->rav
[g
->ravlevel
].hp
= g
->hp
;
2171 g
->rav
[g
->ravlevel
].flags
= g
->flags
;
2172 g
->rav
[g
->ravlevel
].fen
= pgn_game_to_fen(g
, b
);
2173 g
->rav
[g
->ravlevel
].hindex
= g
->hindex
;
2174 g
->hp
= (!g
->ravlevel
) ? (g
->hindex
) ? g
->hp
[g
->hindex
- 1]->rav
: g
->hp
[g
->hindex
]->rav
: g
->hp
[g
->hindex
]->rav
;
2177 pgn_board_update(g
, b
, g
->hindex
+ 1);
2181 if (g
->ravlevel
- 1 < 0)
2186 pgn_board_init_fen(g
, b
, g
->rav
[g
->ravlevel
].fen
);
2187 free(g
->rav
[g
->ravlevel
].fen
);
2188 g
->hp
= g
->rav
[g
->ravlevel
].hp
;
2189 g
->flags
= g
->rav
[g
->ravlevel
].flags
;
2190 g
->hindex
= g
->rav
[g
->ravlevel
].hindex
;
2194 static void draw_window_decor()
2196 move_panel(boardp
, 0,
2197 (config
.boardleft
) ? 0 : COLS
- BOARD_WIDTH
);
2198 move_panel(historyp
, LINES
- HISTORY_HEIGHT
,
2199 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
: 0 :
2200 (MEGA_BOARD
) ? 0 : COLS
- HISTORY_WIDTH
);
2201 move_panel(statusp
, 0,
2202 (config
.boardleft
) ? BOARD_WIDTH
: 0);
2203 move_panel(tagp
, STATUS_HEIGHT
+ 1,
2204 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
:
2207 wbkgd(boardw
, CP_BOARD_WINDOW
);
2208 wbkgd(statusw
, CP_STATUS_WINDOW
);
2209 window_draw_title(statusw
, _("Game Status"), STATUS_WIDTH
,
2210 CP_STATUS_TITLE
, CP_STATUS_BORDER
);
2211 wbkgd(tagw
, CP_TAG_WINDOW
);
2212 window_draw_title(tagw
, _("Roster Tags"), TAG_WIDTH
, CP_TAG_TITLE
,
2214 wbkgd(historyw
, CP_HISTORY_WINDOW
);
2215 window_draw_title(historyw
, _("Move History"), HISTORY_WIDTH
,
2216 CP_HISTORY_TITLE
, CP_HISTORY_BORDER
);
2219 void do_window_resize()
2221 if (LINES
< 23 || COLS
< 74)
2224 resizeterm(LINES
, COLS
);
2226 wresize(boardw
, BOARD_HEIGHT
, BOARD_WIDTH
);
2227 wresize(historyw
, HISTORY_HEIGHT
, HISTORY_WIDTH
);
2228 wresize(statusw
, STATUS_HEIGHT
, STATUS_WIDTH
);
2229 wresize(tagw
, TAG_HEIGHT
, TAG_WIDTH
);
2237 draw_window_decor();
2239 keypad(boardw
, TRUE
);
2245 void do_global_redraw ()
2247 do_window_resize ();
2252 memset(&clock_timer
, 0, sizeof(struct itimerval
));
2253 setitimer(ITIMER_REAL
, &clock_timer
, NULL
);
2256 void start_clock(GAME g
)
2258 struct userdata_s
*d
= g
->data
;
2260 if (clock_timer
.it_interval
.tv_usec
)
2263 memset(&d
->elapsed
, 0, sizeof(struct timeval
));
2264 clock_timer
.it_value
.tv_sec
= 0;
2265 clock_timer
.it_value
.tv_usec
= 100000;
2266 clock_timer
.it_interval
.tv_sec
= 0;
2267 clock_timer
.it_interval
.tv_usec
= 100000;
2268 setitimer(ITIMER_REAL
, &clock_timer
, NULL
);
2271 static void update_clocks()
2274 struct userdata_s
*d
;
2275 struct itimerval it
;
2278 getitimer(ITIMER_REAL
, &it
);
2280 for (i
= 0; i
< gtotal
; i
++) {
2283 if (d
&& d
->mode
== MODE_PLAY
) {
2284 if (d
->paused
== 1 || TEST_FLAG(d
->flags
, CF_NEW
))
2286 else if (d
->paused
== -1) {
2287 if (game
[i
]->side
== game
[i
]->turn
) {
2293 update_clock(game
[i
], it
);
2301 update_status_window(gp
);
2307 #define SKIP_SPACE(str) { while (isspace(*str)) str++; }
2309 static int parse_clock_time(char **str
)
2364 static int parse_clock_input(struct clock_s
*clk
, char *str
, int *incr
)
2386 memset(clk
, 0, sizeof(struct clock_s
));
2390 if (strncasecmp(p
, "SD", 2) == 0) {
2396 n
= parse_clock_time(&p
);
2410 /* Sudden death without a previous time control. */
2416 n
= parse_clock_time(&p
);
2422 message(ERROR_STR
, ANY_KEY_STR
, "%s (%i)", _("Maximum number of time controls reached"), MAX_TC
);
2427 clk
->tc
[tc
++][1] = n
;
2442 clk
->tc
[clk
->tcn
][1] = (n
<= clk
->elapsed
.tv_sec
) ? clk
->elapsed
.tv_sec
+ n
: n
;
2450 n
= parse_clock_time(&p
);
2470 static int parse_which_clock(struct clock_s
*clk
, char *str
)
2475 memcpy(&tmp
, clk
, sizeof(struct clock_s
));
2477 if (parse_clock_input(&tmp
, str
, &incr
)) {
2478 cmessage(ERROR_STR
, ANY_KEY_STR
, _("Invalid clock specification"));
2482 memcpy(clk
, &tmp
, sizeof(struct clock_s
));
2483 clk
->tc
[clk
->tcn
][1] += incr
;
2487 void do_clock_input_finalize(WIN
*win
)
2489 struct userdata_s
*d
= gp
->data
;
2490 struct input_data_s
*in
= win
->data
;
2500 if (tolower(*p
) == 'w') {
2503 if (parse_which_clock(&d
->wclock
, p
))
2506 else if (tolower(*p
) == 'b') {
2509 if (parse_which_clock(&d
->bclock
, p
))
2513 if (parse_which_clock(&d
->wclock
, p
))
2516 if (parse_which_clock(&d
->bclock
, p
))
2520 if (!d
->wclock
.tc
[0][1] && !d
->bclock
.tc
[0][1])
2521 CLEAR_FLAG(d
->flags
, CF_CLOCK
);
2523 SET_FLAG(d
->flags
, CF_CLOCK
);
2530 void do_engine_command_finalize(WIN
*win
)
2532 struct userdata_s
*d
= gp
->data
;
2533 struct input_data_s
*in
= win
->data
;
2544 x
= d
->engine
->status
;
2545 send_to_engine(gp
, -1, "%s\n", in
->str
);
2546 d
->engine
->status
= x
;
2553 void do_board_details()
2555 config
.details
= (config
.details
) ? 0 : 1;
2558 void do_toggle_strict_castling()
2562 pgn_config_get(PGN_STRICT_CASTLING
, &n
);
2565 pgn_config_set(PGN_STRICT_CASTLING
, 1);
2567 pgn_config_set(PGN_STRICT_CASTLING
, 0);
2570 void do_play_set_clock()
2572 struct input_data_s
*in
;
2574 in
= Calloc(1, sizeof(struct input_data_s
));
2575 in
->efunc
= do_clock_input_finalize
;
2576 construct_input(_("Set Clock"), NULL
, 1, 1,
2577 _ ("Format: [W | B] [+]T[+I] | ++I | M/T [M/T [...] [SD/T]] [+I]\n" \
2578 "T = time (hms), I = increment, M = moves per, SD = sudden death\ne.g., 30m or 4m+12s or 35/90m SD/30m"),
2579 NULL
, NULL
, 0, in
, INPUT_HIST_CLOCK
, -1);
2582 void do_play_toggle_human()
2584 struct userdata_s
*d
= gp
->data
;
2586 TOGGLE_FLAG(d
->flags
, CF_HUMAN
);
2588 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && pgn_history_total(gp
->hp
)) {
2589 if (init_chess_engine(gp
))
2593 CLEAR_FLAG(d
->flags
, CF_ENGINE_LOOP
);
2596 d
->engine
->status
= ENGINE_READY
;
2599 void do_play_toggle_engine()
2601 struct userdata_s
*d
= gp
->data
;
2603 TOGGLE_FLAG(d
->flags
, CF_ENGINE_LOOP
);
2604 CLEAR_FLAG(d
->flags
, CF_HUMAN
);
2606 if (d
->engine
&& TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
)) {
2607 char *fen
= pgn_game_to_fen (gp
, d
->b
);
2609 pgn_board_update(gp
, d
->b
,
2610 pgn_history_total(gp
->hp
));
2611 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
2617 * This will send a command to the engine skipping the command queue.
2619 void do_play_send_command()
2621 struct userdata_s
*d
= gp
->data
;
2622 struct input_data_s
*in
;
2624 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
) {
2625 if (init_chess_engine(gp
))
2629 in
= Calloc(1, sizeof(struct input_data_s
));
2630 in
->efunc
= do_engine_command_finalize
;
2631 construct_input(_("Engine Command"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
, INPUT_HIST_ENGINE
, -1);
2634 void do_play_switch_turn()
2636 struct userdata_s *d = gp->data;
2638 pgn_switch_side(gp);
2639 pgn_switch_turn(gp);
2641 if (!TEST_FLAG(d->flags, CF_HUMAN))
2642 add_engine_command(gp, -1,
2643 (gp->side == WHITE) ? "white\n" : "black\n");
2645 update_status_window(gp);
2648 void do_play_toggle_eh_mode()
2650 struct userdata_s
*d
= gp
->data
;
2652 if (!TEST_FLAG(d
->flags
, CF_HUMAN
)) {
2654 pgn_switch_side(gp
, TRUE
);
2655 d
->play_mode
= (d
->play_mode
) ? PLAY_HE
: PLAY_EH
;
2656 if (gp
->side
== BLACK
)
2657 update_status_notify(gp
, _("Press 'g' to start the game"));
2659 rotate
= (rotate
) ? FALSE
: TRUE
;
2662 message(NULL
, ANY_KEY_STR
,
2663 _("You may only switch sides at the start of the \n"
2664 "game. Press ^K or ^N to begin a new game."));
2670 struct userdata_s
*d
= gp
->data
;
2672 if (!pgn_history_total(gp
->hp
))
2676 if (gp
->hindex
- keycount
< 0)
2680 gp
->hindex
-= (keycount
* 2) -1;
2682 gp
->hindex
-= keycount
* 2;
2686 if (gp
->hindex
- 2 < 0)
2696 pgn_history_free(gp
->hp
, gp
->hindex
);
2697 gp
->hindex
= pgn_history_total(gp
->hp
);
2698 pgn_board_update(gp
, d
->b
, gp
->hindex
);
2700 if (d
->engine
&& d
->engine
->status
== ENGINE_READY
) {
2701 char *fen
= pgn_game_to_fen(gp
, d
->b
);
2703 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
2705 d
->engine
->status
= ENGINE_READY
;
2708 update_history_window(gp
);
2711 pgn_switch_side(gp
, FALSE
);
2716 void do_play_toggle_pause()
2718 struct userdata_s
*d
= gp
->data
;
2720 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && gp
->turn
!=
2726 d
->paused
= (d
->paused
) ? 0 : 1;
2731 struct userdata_s
*d
= gp
->data
;
2733 if (TEST_FLAG(d
->flags
, CF_HUMAN
))
2736 if (fm_loaded_file
&& gp
->side
!= gp
->turn
) {
2737 pgn_switch_side(gp
, FALSE
);
2738 add_engine_command(gp
, ENGINE_THINKING
, "black\n");
2741 add_engine_command(gp
, ENGINE_THINKING
, "go\n");
2743 // Completa la función para que permita seguir jugando al usarla.
2744 // Complete the function to allow continue playing when using.
2745 if (gp
->side
== gp
->turn
)
2746 pgn_switch_side(gp
, FALSE
);
2751 void do_play_config_command()
2756 for (x
= 0; config
.keys
[x
]; x
++) {
2757 if (config
.keys
[x
]->c
== input_c
) {
2758 switch (config
.keys
[x
]->type
) {
2760 add_engine_command(gp
, -1, "%ls\n",
2761 config
.keys
[x
]->str
);
2767 add_engine_command(gp
, -1,
2768 "%ls %i\n", config
.keys
[x
]->str
, keycount
);
2775 for (w
= 0; w
< keycount
; w
++)
2776 add_engine_command(gp
, -1,
2777 "%ls\n", config
.keys
[x
]->str
);
2785 update_status_notify(gp
, NULL
);
2788 void do_play_cancel_selected()
2790 struct userdata_s
*d
= gp
->data
;
2792 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
2794 pgn_reset_valid_moves(d
->b
);
2795 update_status_notify(gp
, NULL
);
2798 void do_play_commit()
2800 struct userdata_s
*d
= gp
->data
;
2802 pushkey
= keycount
= 0;
2803 update_status_notify(gp
, NULL
);
2805 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) &&
2806 (!d
->engine
|| d
->engine
->status
== ENGINE_THINKING
))
2812 d
->sp
.row
= d
->c_row
;
2813 d
->sp
.col
= d
->c_col
;
2816 rotate_position(SP_POSITION
);
2817 rotate_position(SPS_POSITION
);
2822 // Completa la función para que permita seguir jugando cuando se carga un
2823 // archivo pgn (con juego no terminado) que inicie con turno del lado
2825 // Complete the function to allow continue playing when loading a file
2826 // pgn (with unfinished game) you start to turn black side.
2827 if (gp
->side
!= gp
->turn
)
2828 pgn_switch_side(gp
, FALSE
);
2830 if (rotate
&& d
->sp
.icon
)
2831 rotate_position(SPS_POSITION
);
2833 // Envia comando 'go' a Polyglot en el primer movimiento debido a que
2834 // polyglot no envia el movimiento y cboard se queda esperando.
2835 // Send command 'go' to the first movement Polyglot because cboard
2836 // waits to send polyglot movement and this does not make.
2837 if (config
.fmpolyglot
&&
2838 ((gp
->side
== WHITE
&& !gp
->hindex
) || fm_loaded_file
))
2839 add_engine_command(gp
, ENGINE_THINKING
, "go\n");
2842 fm_loaded_file
= FALSE
;
2845 void do_play_select()
2847 struct userdata_s
*d
= gp
->data
;
2849 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && (!d
->engine
||
2850 d
->engine
->status
== ENGINE_OFFLINE
)) {
2851 if (init_chess_engine(gp
))
2855 if (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
)
2859 do_play_cancel_selected ();
2862 rotate_position(CURSOR_POSITION
);
2864 d
->sp
.icon
= d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
;
2866 if (pgn_piece_to_int(d
->sp
.icon
) == OPEN_SQUARE
) {
2871 if (((islower(d
->sp
.icon
) && gp
->turn
!= BLACK
)
2872 || (isupper(d
->sp
.icon
) && gp
->turn
!= WHITE
))) {
2874 char *str
= Malloc (512);
2876 for (k
= play_keys
; *k
; k
++) {
2877 if ((*k
)->f
== do_play_toggle_eh_mode
)
2881 snprintf (str
, 512, _("It is not your turn to move. You may switch playing sides by pressing \"%lc\"."),
2882 *k
? (*k
)->c
: '?');
2883 message(NULL
, ANY_KEY_STR
, "%s", str
);
2888 if (pgn_history_total(gp
->hp
)) {
2889 message(NULL
, ANY_KEY_STR
, "%s", _("It is not your turn to move. You can switch sides "));
2894 if (pgn_tag_find(gp
->tag
, "FEN") != E_PGN_ERR
)
2897 add_engine_command(gp
, ENGINE_READY
, "black\n");
2898 pgn_switch_turn(gp
);
2900 if (gp
->side
!= BLACK
)
2901 pgn_switch_side(gp
);
2906 d
->sp
.srow
= d
->c_row
;
2907 d
->sp
.scol
= d
->c_col
;
2909 if (config
.validmoves
)
2910 pgn_find_valid_moves(gp
, d
->b
, d
->sp
.scol
, d
->sp
.srow
);
2913 rotate_position(CURSOR_POSITION
);
2914 rotate_position(SPS_POSITION
);
2917 CLEAR_FLAG(d
->flags
, CF_NEW
);
2921 /* FIXME: keys with the same function should comma deliminated. */
2922 static wchar_t *build_help(struct key_s
**keys
)
2924 int i
, nlen
= 1, len
, t
, n
;
2925 wchar_t *buf
= NULL
, *wc
= NULL
;
2931 for (i
= len
= t
= 0; keys
[i
]; i
++) {
2936 if (wcslen(keys
[i
]->key
) > nlen
) {
2937 nlen
= wcslen(keys
[i
]->key
);
2945 if (wcslen(keys
[i
]->d
) > len
)
2946 len
= wcslen(keys
[i
]->d
);
2954 buf
= Malloc(t
*sizeof(wchar_t));
2957 for (i
= 0; keys
[i
]; i
++) {
2962 n
= wcslen(keys
[i
]->key
);
2972 wcscat(buf
, keys
[i
]->key
);
2973 p
= buf
+ wcslen(buf
);
2984 wcscat(buf
, keys
[i
]->d
);
2987 wc
= str_to_wchar ("*");
2992 wc
= str_to_wchar ("\n");
2995 p
= buf
+ wcslen(buf
);
3001 void do_more_help(WIN
*);
3002 void do_main_help(WIN
*win
)
3008 buf
= build_help(play_keys
);
3009 construct_message(_("Play Mode Keys (* = can take a repeat count)"), ANY_KEY_SCROLL_STR
, 0, 0,
3010 NULL
, NULL
, buf
, do_more_help
, 0, 1, "%ls", buf
);
3013 buf
= build_help(history_keys
);
3014 construct_message(_("History Mode Keys (* = can take a repeat count)"), ANY_KEY_SCROLL_STR
, 0, 0,
3015 NULL
, NULL
, buf
, do_more_help
, 0, 1, "%ls", buf
);
3018 buf
= build_help(edit_keys
);
3019 construct_message(_("Edit Mode Keys (* = can take a repeat count)"), ANY_KEY_SCROLL_STR
, 0, 0,
3020 NULL
, NULL
, buf
, do_more_help
, 0, 1, "%ls", buf
);
3023 buf
= build_help(global_keys
);
3024 construct_message(_("Global Game Keys (* = can take a repeat count)"), ANY_KEY_SCROLL_STR
, 0, 0,
3025 NULL
, NULL
, buf
, do_more_help
, 0, 1, "%ls", buf
);
3032 void do_more_help(WIN
*win
)
3034 if (win
->c
== KEY_F(1) || win
->c
== CTRL_KEY('g'))
3035 construct_message(_("Command Key Index"),
3036 _ ("p/h/e/g or any other key to quit"), 0, 0,
3037 NULL
, NULL
, NULL
, do_main_help
, 0, 0, "%s",
3039 "p - play mode keys\n"
3040 "h - history mode keys\n"
3041 "e - board edit mode keys\n"
3042 "g - global game keys"
3048 wchar_t *buf
= build_help(play_keys
);
3050 construct_message(_("Play Mode Keys (* = can take a repeat count)"),
3051 ANY_KEY_STR
, 0, 0, NULL
, NULL
,
3052 buf
, do_more_help
, 0, 1, "%ls", buf
);
3055 void do_play_history_mode()
3057 struct userdata_s
*d
= gp
->data
;
3059 if (!pgn_history_total(gp
->hp
) ||
3060 (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
))
3063 d
->mode
= MODE_HISTORY
;
3064 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3067 void do_play_edit_mode()
3069 struct userdata_s
*d
= gp
->data
;
3071 if (pgn_history_total(gp
->hp
))
3074 pgn_board_init_fen(gp
, d
->b
, NULL
);
3076 d
->mode
= MODE_EDIT
;
3079 void do_edit_insert_finalize(WIN
*win
)
3081 struct userdata_s
*d
= win
->data
;
3083 if (pgn_piece_to_int(win
->c
) == -1)
3086 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
= win
->c
;
3089 void do_edit_select()
3091 struct userdata_s
*d
= gp
->data
;
3096 d
->sp
.icon
= d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
;
3098 if (pgn_piece_to_int(d
->sp
.icon
) == OPEN_SQUARE
) {
3103 d
->sp
.srow
= d
->c_row
;
3104 d
->sp
.scol
= d
->c_col
;
3107 void do_edit_commit()
3110 struct userdata_s
*d
= gp
->data
;
3112 pushkey
= keycount
= 0;
3113 update_status_notify(gp
, NULL
);
3118 d
->sp
.row
= d
->c_row
;
3119 d
->sp
.col
= d
->c_col
;
3120 p
= d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
;
3121 d
->b
[RANKTOBOARD(d
->sp
.row
)][FILETOBOARD(d
->sp
.col
)].icon
= p
;
3122 d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
=
3123 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3124 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3127 void do_edit_delete()
3129 struct userdata_s
*d
= gp
->data
;
3132 d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
=
3133 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3135 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
=
3136 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3138 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3141 void do_edit_cancel_selected()
3143 struct userdata_s
*d
= gp
->data
;
3145 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3147 update_status_notify(gp
, NULL
);
3150 void do_edit_switch_turn()
3152 pgn_switch_turn(gp
);
3155 void do_edit_toggle_castle()
3157 struct userdata_s
*d
= gp
->data
;
3159 castling_state(gp
, d
->b
, RANKTOBOARD(d
->c_row
),
3160 FILETOBOARD(d
->c_col
),
3161 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
, 1);
3164 void do_edit_insert()
3166 struct userdata_s
*d
= gp
->data
;
3168 construct_message(_("Insert Piece"), _("P=pawn, R=rook, N=knight, B=bishop, "), 0, 0, NULL
, NULL
,
3169 d
->b
, do_edit_insert_finalize
, 0, 0, "%s", _("Type the piece letter to insert. Lowercase "));
3172 void do_edit_enpassant()
3174 struct userdata_s
*d
= gp
->data
;
3176 if (d
->c_row
== 6 || d
->c_row
== 3) {
3177 pgn_reset_enpassant(d
->b
);
3178 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].enpassant
= 1;
3184 wchar_t *buf
= build_help(edit_keys
);
3186 construct_message(_("Edit Mode Keys (* = can take a repeat count)"),
3187 ANY_KEY_STR
, 0, 0, NULL
, NULL
,
3188 buf
, do_more_help
, 0, 1, "%ls", buf
);
3193 struct userdata_s
*d
= gp
->data
;
3194 char *fen
= pgn_game_to_fen(gp
, d
->b
);
3197 pgn_tag_add(&gp
->tag
, "FEN", fen
);
3199 pgn_tag_add(&gp
->tag
, "SetUp", "1");
3200 pgn_tag_sort(gp
->tag
);
3201 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3202 d
->mode
= MODE_PLAY
;
3205 void really_do_annotate_finalize(struct input_data_s
*in
,
3206 struct userdata_s
*d
)
3208 HISTORY
*h
= in
->data
;
3218 len
= strlen(in
->str
);
3219 h
->comment
= Realloc(h
->comment
, len
+1);
3220 strncpy(h
->comment
, in
->str
, len
);
3221 h
->comment
[len
] = 0;
3226 SET_FLAG(d
->flags
, CF_MODIFIED
);
3229 void do_annotate_finalize(WIN
*win
)
3231 struct userdata_s
*d
= gp
->data
;
3232 struct input_data_s
*in
= win
->data
;
3234 really_do_annotate_finalize(in
, d
);
3237 void do_find_move_exp_finalize(int init
, int which
)
3240 struct userdata_s
*d
= gp
->data
;
3241 static int firstrun
;
3246 if (init
|| !firstrun
) {
3250 if ((ret
= regcomp(&r
, moveexp
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
3251 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
3252 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
3259 if ((n
= find_move_exp(gp
, r
,
3260 (which
== -1) ? 0 : 1, (keycount
) ? keycount
: 1)) == -1)
3264 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3267 void do_find_move_exp(WIN
*win
)
3269 struct input_data_s
*in
= win
->data
;
3274 strncpy(moveexp
, in
->str
, sizeof(moveexp
)-1);
3275 moveexp
[sizeof(moveexp
)-1] = 0;
3276 do_find_move_exp_finalize(1, which
);
3284 void do_move_jump_finalize(int n
)
3286 struct userdata_s
*d
= gp
->data
;
3288 if (n
< 0 || n
> (pgn_history_total(gp
->hp
) / 2))
3292 update_status_notify(gp
, NULL
);
3293 gp
->hindex
= (n
) ? n
* 2 - 1 : n
* 2;
3294 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3297 void do_move_jump(WIN
*win
)
3299 struct input_data_s
*in
= win
->data
;
3301 if (!in
->str
|| !isinteger(in
->str
)) {
3309 do_move_jump_finalize(atoi(in
->str
));
3314 struct history_menu_s
{
3322 void free_history_menu_data(struct history_menu_s
**h
)
3329 for (i
= 0; h
[i
]; i
++) {
3337 void get_history_data(HISTORY
**hp
, struct history_menu_s
***menu
, int m
,
3341 int t
= pgn_history_total(hp
);
3342 char buf
[MAX_SAN_MOVE_LEN
+ 4];
3344 struct history_menu_s
**hmenu
= *menu
;
3347 for (n
= 0; hmenu
[n
]; n
++);
3351 for (i
= 0; i
< t
; i
++) {
3352 hmenu
= Realloc(hmenu
, (n
+ 2) * sizeof(struct history_menu_s
*));
3353 hmenu
[n
] = Malloc(sizeof(struct history_menu_s
));
3354 snprintf(buf
, sizeof(buf
), "%c%s%s", (turn
== WHITE
) ? 'W' : 'B',
3355 hp
[i
]->move
, (hp
[i
]->comment
|| hp
[i
]->nag
[0]) ? " !" : "");
3356 hmenu
[n
]->line
= strdup(buf
);
3357 hmenu
[n
]->hindex
= i
;
3358 hmenu
[n
]->indent
= 0;
3359 hmenu
[n
]->ravlevel
= depth
;
3360 hmenu
[n
]->move
= (n
&& depth
> hmenu
[n
-1]->ravlevel
) ? m
++ : m
;
3367 get_history_data(hp
[i
]->rav
, &hmenu
, m
, turn
);
3368 for (n
= 0; hmenu
[n
]; n
++);
3376 turn
= (turn
== WHITE
) ? BLACK
: WHITE
;
3382 void history_draw_update(struct menu_input_s
*m
)
3385 struct userdata_s
*d
= g
->data
;
3387 g
->hindex
= m
->selected
+ 1;
3388 update_cursor(g
, m
->selected
);
3389 pgn_board_update(g
, d
->b
, m
->selected
+ 1);
3392 struct menu_item_s
**get_history_items(WIN
*win
)
3394 struct menu_input_s
*m
= win
->data
;
3396 struct userdata_s
*d
= g
->data
;
3397 struct history_menu_s
**hm
= d
->data
;
3398 struct menu_item_s
**items
= m
->items
;
3402 get_history_data(g
->history
, &hm
, 0,
3403 TEST_FLAG(g
->flags
, GF_BLACK_OPENING
));
3404 m
->selected
= g
->hindex
- 1;
3406 if (m
->selected
< 0)
3409 m
->draw_exit_func
= history_draw_update
;
3415 for (i
= 0; items
[i
]; i
++)
3422 for (i
= 0; hm
[i
]; i
++) {
3423 items
= Realloc(items
, (i
+2) * sizeof(struct menu_item_s
*));
3424 items
[i
] = Malloc(sizeof(struct menu_item_s
));
3425 items
[i
]->name
= hm
[i
]->line
;
3426 items
[i
]->value
= NULL
;
3427 items
[i
]->selected
= 0;
3438 void history_menu_quit(struct menu_input_s
*m
)
3443 void history_menu_exit(WIN
*win
)
3446 struct userdata_s
*d
= g
->data
;
3447 struct history_menu_s
**hm
= d
->data
;
3453 for (i
= 0; hm
[i
]; i
++) {
3463 void history_menu_next(struct menu_input_s
*m
)
3466 struct userdata_s
*d
= g
->data
;
3467 struct history_menu_s
**hm
= d
->data
;
3470 for (t
= 0; hm
[t
]; t
++);
3472 if (m
->selected
+ 1 == t
)
3475 n
= hm
[m
->selected
+ 1]->hindex
;
3482 void history_menu_prev(struct menu_input_s
*m
)
3485 struct userdata_s
*d
= g
->data
;
3486 struct history_menu_s
**hm
= d
->data
;
3489 for (t
= 0; hm
[t
]; t
++);
3491 if (m
->selected
- 1 < 0)
3494 n
= hm
[m
->selected
- 1]->hindex
;
3500 void history_menu_help(struct menu_input_s
*m
)
3502 message(_("History Menu Help"), ANY_KEY_STR
, "%s",
3504 " UP/DOWN - previous/next menu item\n"
3505 " HOME/END - first/last menu item\n"
3506 " PGDN/PGUP - next/previous page\n"
3507 " a-zA-Z0-9 - jump to item\n"
3508 " CTRL-a - annotate the selected move\n"
3509 " ENTER - view annotation\n"
3510 " CTRL-d - toggle board details\n"
3511 " ESCAPE/M - return to move history"
3515 void do_annotate_move(HISTORY
*hp
)
3518 struct input_data_s
*in
;
3520 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Editing Annotation for"), hp
->move
);
3521 in
= Calloc(1, sizeof(struct input_data_s
));
3523 in
->efunc
= do_annotate_finalize
;
3524 construct_input(buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
3525 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
, CTRL_KEY('T'), in
, -1, -1);
3528 void history_menu_view_annotation(struct menu_input_s
*m
)
3533 view_annotation(g
->history
[m
->selected
]);
3536 void history_menu_annotate_finalize(WIN
*win
)
3538 struct input_data_s
*in
= win
->data
;
3539 GAME g
= in
->moredata
;
3540 struct userdata_s
*d
= g
->data
;
3541 struct history_menu_s
**hm
= d
->data
;
3543 really_do_annotate_finalize(in
, d
);
3544 free_history_menu_data(hm
);
3546 get_history_data(g
->history
, &hm
, 0, TEST_FLAG(g
->flags
, GF_BLACK_OPENING
));
3548 pushkey
= REFRESH_MENU
;
3551 void history_menu_annotate(struct menu_input_s
*m
)
3555 struct input_data_s
*in
;
3556 HISTORY
*hp
= g
->history
[m
->selected
]; // FIXME RAV
3558 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Editing Annotation for"), hp
->move
);
3559 in
= Calloc(1, sizeof(struct input_data_s
));
3561 in
->moredata
= m
->data
;
3562 in
->efunc
= history_menu_annotate_finalize
;
3563 construct_input(buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
3564 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
, CTRL_KEY('T'), in
, -1, -1);
3567 void history_menu_details(struct menu_input_s
*m
)
3573 void history_menu_print(WIN
*win
)
3575 struct menu_input_s
*m
= win
->data
;
3577 struct userdata_s
*d
= g
->data
;
3578 struct history_menu_s
**hm
= d
->data
;
3579 struct history_menu_s
*h
= hm
[m
->top
];
3581 char *p
= m
->item
->name
;
3582 int line
= m
->print_line
- 2;
3584 * Solaris 5.9 doesn't have wattr_get() or any function that requires an
3591 for (total
= 0; hm
[total
]; total
++);
3592 wattr_get(win
->w
, &attrs
, &pair
, NULL
);
3593 wattroff(win
->w
, COLOR_PAIR(pair
));
3594 mvwaddch(win
->w
, m
->print_line
, 1,
3595 *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
));
3598 if (h
->hindex
== 0 && line
== 0)
3599 waddch(win
->w
, ACS_ULCORNER
| CP_HISTORY_MENU_LG
);
3600 else if ((!hm
[h
->hindex
+ (win
->rows
- 5) + 1] && line
== win
->rows
- 5) ||
3601 (m
->top
+ line
== total
- 1))
3602 waddch(win
->w
, ACS_LLCORNER
| CP_HISTORY_MENU_LG
);
3603 else if (hm
[m
->top
+ 1]->ravlevel
!= h
->ravlevel
|| !h
->ravlevel
)
3604 waddch(win
->w
, ACS_LTEE
| CP_HISTORY_MENU_LG
);
3606 waddch(win
->w
, ACS_VLINE
| CP_HISTORY_MENU_LG
);
3608 wattron(win
->w
, COLOR_PAIR(pair
) | attrs
);
3610 for (i
= 2; *p
; p
++, i
++)
3611 waddch(win
->w
, (*p
== '!') ? *p
| A_BOLD
: *p
);
3613 while (i
++ < win
->cols
- 2)
3614 waddch(win
->w
, ' ');
3617 void history_menu(GAME g
)
3619 struct menu_key_s
**keys
= NULL
;
3621 add_menu_key(&keys
, KEY_ESCAPE
, history_menu_quit
);
3622 add_menu_key(&keys
, 'M', history_menu_quit
);
3623 add_menu_key(&keys
, KEY_UP
, history_menu_prev
);
3624 add_menu_key(&keys
, KEY_DOWN
, history_menu_next
);
3625 add_menu_key(&keys
, KEY_F(1), history_menu_help
);
3626 add_menu_key(&keys
, CTRL_KEY('a'), history_menu_annotate
);
3627 add_menu_key(&keys
, CTRL_KEY('d'), history_menu_details
);
3628 add_menu_key(&keys
, '\n', history_menu_view_annotation
);
3629 construct_menu(MEGA_BOARD
? LINES
- HISTORY_HEIGHT_MB
: LINES
,
3630 TAG_WIDTH
, 0, config
.boardleft
? BOARD_WIDTH
: 0,
3631 _("Move History Tree"), 1, get_history_items
, keys
, g
,
3632 history_menu_print
, history_menu_exit
);
3635 void do_history_menu()
3640 void do_history_half_move_toggle()
3642 movestep
= (movestep
== 1) ? 2 : 1;
3643 update_history_window(gp
);
3646 void do_history_rotate_board()
3648 rotate
= (rotate
) ? FALSE
: TRUE
;
3651 void do_history_jump_next()
3653 struct userdata_s
*d
= gp
->data
;
3655 pgn_history_next(gp
, d
->b
, (keycount
> 0) ?
3656 config
.jumpcount
* keycount
* movestep
:
3657 config
.jumpcount
* movestep
);
3660 void do_history_jump_prev()
3662 struct userdata_s
*d
= gp
->data
;
3664 pgn_history_prev(gp
, d
->b
, (keycount
) ?
3665 config
.jumpcount
* keycount
* movestep
:
3666 config
.jumpcount
* movestep
);
3669 void do_history_prev()
3671 struct userdata_s
*d
= gp
->data
;
3673 pgn_history_prev(gp
, d
->b
,
3674 (keycount
) ? keycount
* movestep
: movestep
);
3677 void do_history_next()
3679 struct userdata_s
*d
= gp
->data
;
3681 pgn_history_next(gp
, d
->b
, (keycount
) ?
3682 keycount
* movestep
: movestep
);
3685 void do_history_mode_finalize(struct userdata_s
*d
)
3688 d
->mode
= MODE_PLAY
;
3691 void do_history_mode_confirm(WIN
*win
)
3693 struct userdata_s
*d
= gp
->data
;
3694 wchar_t str
[] = { win
->c
, 0 };
3696 if (!wcscmp (str
, resume_wchar
)) {
3697 pgn_history_free(gp
->hp
, gp
->hindex
);
3698 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3703 if (pgn_history_rav_new(gp
, d
->b
,
3704 gp
->hindex
) != E_PGN_OK
)
3712 if (!TEST_FLAG(d
->flags
, CF_HUMAN
)) {
3713 char *fen
= pgn_game_to_fen(gp
, d
->b
);
3715 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
3719 do_history_mode_finalize(d
);
3722 void do_history_toggle()
3724 struct userdata_s
*d
= gp
->data
;
3726 // FIXME Resuming from previous history could append to a RAV.
3727 if (gp
->hindex
!= pgn_history_total(gp
->hp
)) {
3729 construct_message(NULL
, _ ("What would you like to do?"), 0, 1,
3730 NULL
, NULL
, NULL
, do_history_mode_confirm
, 0, 0,
3731 _("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."),
3736 if (TEST_FLAG(gp
->flags
, GF_GAMEOVER
))
3740 if (gp
->side
!= gp
->turn
) {
3741 d
->play_mode
= PLAY_EH
;
3744 d
->play_mode
= PLAY_HE
;
3748 do_history_mode_finalize(d
);
3751 void do_history_annotate()
3755 if (n
&& gp
->hp
[n
- 1]->move
)
3760 do_annotate_move(gp
->hp
[n
]);
3763 void do_history_help()
3765 wchar_t *buf
= build_help(history_keys
);
3767 construct_message(_("History Mode Keys (* = can take a repeat count)"),
3768 ANY_KEY_STR
, 0, 0, NULL
, NULL
,
3769 buf
, do_more_help
, 0, 1, "%ls", buf
);
3772 void do_history_find(int which
)
3774 struct input_data_s
*in
;
3777 if (pgn_history_total(gp
->hp
) < 2)
3780 in
= Calloc(1, sizeof(struct input_data_s
));
3781 p
= Malloc(sizeof(int));
3784 in
->efunc
= do_find_move_exp
;
3786 if (!*moveexp
|| which
== 0) {
3787 construct_input(_("Find Move Text Expression"), NULL
, 1, 0, NULL
, NULL
, NULL
,
3788 0, in
, INPUT_HIST_MOVE_EXP
, -1);
3792 do_find_move_exp_finalize(0, which
);
3795 void do_history_find_new()
3800 void do_history_find_prev()
3802 do_history_find(-1);
3805 void do_history_find_next()
3810 void do_history_rav(int which
)
3812 struct userdata_s
*d
= gp
->data
;
3814 rav_next_prev(gp
, d
->b
, which
);
3817 void do_history_rav_next()
3822 void do_history_rav_prev()
3827 void do_history_jump()
3829 struct input_data_s
*in
;
3831 if (pgn_history_total(gp
->hp
) < 2)
3835 in
= Calloc(1, sizeof(struct input_data_s
));
3836 in
->efunc
= do_move_jump
;
3838 construct_input(_("Jump to Move Number"), NULL
, 1, 1, NULL
,
3839 NULL
, NULL
, 0, in
, -1, 0);
3843 do_move_jump_finalize(keycount
);
3846 static void free_userdata_once(GAME g
)
3848 struct userdata_s
*d
= g
->data
;
3856 if (d
->engine
->enginebuf
) {
3859 for (n
= 0; d
->engine
->enginebuf
[n
]; n
++)
3860 free(d
->engine
->enginebuf
[n
]);
3862 free(d
->engine
->enginebuf
);
3865 if (d
->engine
->queue
) {
3868 for (q
= d
->engine
->queue
; *q
; q
++)
3871 free(d
->engine
->queue
);
3889 static void free_userdata()
3893 for (i
= 0; i
< gtotal
; i
++) {
3894 free_userdata_once(game
[i
]);
3895 game
[i
]->data
= NULL
;
3899 void update_loading_window(int n
)
3904 loadingw
= newwin(3, COLS
/ 2, CALCPOSY(3), CALCPOSX(COLS
/ 2));
3905 loadingp
= new_panel(loadingw
);
3906 wbkgd(loadingw
, CP_MESSAGE_WINDOW
);
3909 wmove(loadingw
, 0, 0);
3910 wclrtobot(loadingw
);
3911 wattron(loadingw
, CP_MESSAGE_BORDER
);
3912 box(loadingw
, ACS_VLINE
, ACS_HLINE
);
3913 wattroff(loadingw
, CP_MESSAGE_BORDER
);
3914 mvwprintw(loadingw
, 1, CENTER_INT((COLS
/ 2), 11 +
3915 strlen(itoa(gtotal
, buf
))),
3916 _("Loading... %i%% (%i games)"), n
, gtotal
);
3921 static void init_userdata_once(GAME g
, int n
)
3923 struct userdata_s
*d
= NULL
;
3925 d
= Calloc(1, sizeof(struct userdata_s
));
3927 d
->c_row
= 2, d
->c_col
= 5;
3928 SET_FLAG(d
->flags
, CF_NEW
);
3931 if (pgn_board_init_fen(g
, d
->b
, NULL
) != E_PGN_OK
)
3932 pgn_board_init(d
->b
);
3935 void init_userdata()
3939 for (i
= 0; i
< gtotal
; i
++)
3940 init_userdata_once(game
[i
], i
);
3943 void fix_marks(int *start
, int *end
)
3947 *start
= (*start
< 0) ? 0 : *start
;
3948 *end
= (*end
< 0) ? 0 : *end
;
3950 if (*start
> *end
) {
3956 *end
= (*end
> gtotal
) ? gtotal
: *end
;
3959 void do_new_game_finalize(GAME g
)
3961 struct userdata_s
*d
= g
->data
;
3963 d
->mode
= MODE_PLAY
;
3964 update_status_notify(g
, NULL
);
3967 void do_new_game_from_scratch(WIN
*win
)
3969 wchar_t str
[] = { win
->c
, 0 };
3971 if (wcscmp (str
, yes_wchar
))
3978 add_custom_tags(&gp
->tag
);
3981 do_new_game_finalize(gp
);
3989 add_custom_tags(&gp
->tag
);
3990 init_userdata_once(gp
, gindex
);
3991 do_new_game_finalize(gp
);
3995 void do_game_delete_finalize(int n
)
3997 struct userdata_s
*d
;
3999 delete_game((!n
) ? gindex
: -1);
4001 if (d
->mode
!= MODE_EDIT
)
4002 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4005 void do_game_delete_confirm(WIN
*win
)
4008 wchar_t str
[] = { win
->c
, 0 };
4010 if (wcscmp (str
, yes_wchar
)) {
4015 n
= (int *)win
->data
;
4016 do_game_delete_finalize(*n
);
4020 void do_game_delete()
4024 struct userdata_s
*d
;
4028 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
4034 for (i
= n
= 0; i
< gtotal
; i
++) {
4037 if (TEST_FLAG(d
->flags
, CF_DELETE
))
4042 tmp
= _("Delete the current game?");
4045 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
4049 tmp
= _("Delete all games marked for deletion?");
4052 if (config
.deleteprompt
) {
4053 p
= Malloc(sizeof(int));
4055 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, p
,
4056 do_game_delete_confirm
, 0, 0, tmp
);
4060 do_game_delete_finalize(n
);
4063 void do_find_game_exp_finalize(int which
)
4065 struct userdata_s
*d
= gp
->data
;
4068 if ((n
= find_game_exp(gameexp
, (which
== -1) ? 0 : 1,
4069 (keycount
) ? keycount
: 1)) == -1) {
4070 update_status_notify(gp
, "%s", _("No matches found"));
4077 if (pgn_history_total(gp
->hp
))
4078 d
->mode
= MODE_HISTORY
;
4080 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4083 void do_find_game_exp(WIN
*win
)
4085 struct input_data_s
*in
= win
->data
;
4090 strncpy(gameexp
, in
->str
, sizeof(gameexp
));
4091 gameexp
[sizeof(gameexp
)-1] = 0;
4096 do_find_game_exp_finalize(c
);
4104 void do_game_jump_finalize(int n
)
4106 struct userdata_s
*d
;
4108 if (--n
> gtotal
- 1 || n
< 0)
4114 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4115 update_status_notify(gp
, NULL
);
4118 void do_game_jump(WIN
*win
)
4120 struct input_data_s
*in
= win
->data
;
4122 if (!in
->str
|| !isinteger(in
->str
)) {
4130 do_game_jump_finalize(atoi(in
->str
));
4135 void do_load_file(WIN
*win
)
4137 struct input_data_s
*in
= win
->data
;
4138 char *tmp
= in
->str
;
4139 struct userdata_s
*d
;
4140 PGN_FILE
*pgn
= NULL
;
4148 if ((tmp
= pathfix(tmp
)) == NULL
)
4151 n
= pgn_open(tmp
, "r", &pgn
);
4153 if (n
== E_PGN_ERR
) {
4154 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
, strerror(errno
));
4157 else if (n
== E_PGN_INVALID
) {
4158 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
, _("Not a regular file"));
4164 if (pgn_parse(pgn
) == E_PGN_ERR
) {
4165 del_panel(loadingp
);
4173 del_panel(loadingp
);
4178 strncpy(loadfile
, tmp
, sizeof(loadfile
));
4179 loadfile
[sizeof(loadfile
)-1] = 0;
4183 if (pgn_history_total(gp
->hp
))
4184 d
->mode
= MODE_HISTORY
;
4186 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4188 fm_loaded_file
= TRUE
;
4199 void do_game_save(WIN
*win
)
4201 struct input_data_s
*in
= win
->data
;
4204 char *tmp
= in
->str
;
4205 char tfile
[FILENAME_MAX
];
4208 struct userdata_s
*d
;
4210 if (!tmp
|| (tmp
= pathfix(tmp
)) == NULL
)
4213 if (pgn_is_compressed(tmp
) == E_PGN_ERR
) {
4214 p
= tmp
+ strlen(tmp
) - 1;
4216 if (*p
!= 'n' || *(p
-1) != 'g' || *(p
-2) != 'p' ||
4218 snprintf(tfile
, sizeof(tfile
), "%s.pgn", tmp
);
4224 * When in edit mode, update the FEN tag.
4227 for (i
= 0; i
< gtotal
; i
++) {
4230 if (d
->mode
== MODE_EDIT
) {
4231 char *fen
= pgn_game_to_fen(game
[i
], d
->b
);
4233 pgn_tag_add(&game
[i
]->tag
, "FEN", fen
);
4241 if (d
->mode
== MODE_EDIT
) {
4242 char *fen
= pgn_game_to_fen(game
[n
], d
->b
);
4244 pgn_tag_add(&game
[n
]->tag
, "FEN", fen
);
4259 void do_get_game_save_input(int n
)
4261 struct input_data_s
*in
= Calloc(1, sizeof(struct input_data_s
));
4262 int *p
= Malloc(sizeof(int));
4264 in
->efunc
= do_game_save
;
4268 construct_input(_("Save Game Filename"), loadfile
, 1, 1, _("Type TAB for file browser"),
4269 file_browser
, NULL
, '\t', in
, INPUT_HIST_FILE
, -1);
4272 void do_game_save_multi_confirm(WIN
*win
)
4275 wchar_t str
[] = { win
->c
, 0 };
4277 if (!wcscmp (str
, current_wchar
))
4279 else if (!wcscmp (str
, all_wchar
))
4282 update_status_notify(gp
, "%s", _("Save game aborted."));
4286 do_get_game_save_input(i
);
4289 void do_global_about()
4291 cmessage(_("ABOUT"), ANY_KEY_STR
,
4292 _("%s\nUsing %s with %i colors and %i color pairs\n%s"),
4293 PACKAGE_STRING
, curses_version(), COLORS
, COLOR_PAIRS
,
4297 void global_game_next_prev(int which
)
4299 struct userdata_s
*d
;
4301 game_next_prev(gp
, (which
== 1) ? 1 : 0,
4302 (keycount
) ? keycount
: 1);
4307 markend
= markstart
+ delete_count
;
4311 markend
= markstart
- delete_count
+ 1;
4312 delete_count
= -1; // to fix gindex in the other direction
4315 fix_marks(&markstart
, &markend
);
4316 do_global_toggle_delete();
4319 if (d
->mode
== MODE_HISTORY
)
4320 pgn_board_update(gp
, d
->b
, gp
->hindex
);
4321 else if (d
->mode
== MODE_PLAY
)
4322 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4325 void do_global_next_game()
4327 global_game_next_prev(1);
4330 void do_global_prev_game()
4332 global_game_next_prev(0);
4335 void global_find(int which
)
4337 struct input_data_s
*in
;
4343 in
= Calloc(1, sizeof(struct input_data_s
));
4344 p
= Malloc(sizeof(int));
4347 in
->efunc
= do_find_game_exp
;
4349 if (!*gameexp
|| which
== 0) {
4350 construct_input(_("Find Game by Tag Expression"), NULL
, 1, 0,
4351 _("[name expression:]value expression"), NULL
, NULL
, 0, in
,
4352 INPUT_HIST_GAME_EXP
, -1);
4356 do_find_game_exp_finalize(which
);
4359 void do_global_find_new()
4364 void do_global_find_next()
4369 void do_global_find_prev()
4374 void do_global_game_jump()
4380 struct input_data_s
*in
;
4382 in
= Calloc(1, sizeof(struct input_data_s
));
4383 in
->efunc
= do_game_jump
;
4384 construct_input(_("Jump to Game Number"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
,
4389 do_game_jump_finalize(keycount
);
4392 void do_global_toggle_delete()
4401 if (keycount
&& delete_count
== 0) {
4403 delete_count
= keycount
;
4404 update_status_notify(gp
, "%s (delete)", status
.notify
);
4408 if (markstart
>= 0 && markend
>= 0) {
4409 for (i
= markstart
; i
< markend
; i
++) {
4410 if (toggle_delete_flag(i
)) {
4415 gindex
= (delete_count
< 0) ? markstart
: i
- 1;
4418 if (toggle_delete_flag(gindex
))
4422 markstart
= markend
= -1;
4424 update_status_window(gp
);
4427 void do_global_delete_game()
4432 void do_global_tag_edit()
4434 struct userdata_s
*d
= gp
->data
;
4436 edit_tags(gp
, d
->b
, 1);
4439 void do_global_tag_view()
4441 struct userdata_s
*d
= gp
->data
;
4443 edit_tags(gp
, d
->b
, 0);
4446 void do_global_resume_game()
4448 struct input_data_s
*in
;
4450 in
= Calloc(1, sizeof(struct input_data_s
));
4451 in
->efunc
= do_load_file
;
4452 construct_input(_("Load Filename"), NULL
, 1, 1, _("Type TAB for file browser"), file_browser
,
4453 NULL
, '\t', in
, INPUT_HIST_FILE
, -1);
4456 void do_global_save_game()
4459 construct_message(NULL
, _("What would you like to do?"), 0, 1,
4460 NULL
, NULL
, NULL
, do_game_save_multi_confirm
, 0, 0,
4461 _("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."),
4462 current_wchar
, all_wchar
);
4466 do_get_game_save_input(-1);
4469 void do_global_new_game()
4474 void do_global_copy_game()
4478 struct userdata_s
*d
;
4480 do_global_new_game();
4482 n
= pgn_tag_total(game
[g
]->tag
);
4484 for (i
= 0; i
< n
; i
++)
4485 pgn_tag_add(&gp
->tag
, game
[g
]->tag
[i
]->name
,
4486 game
[g
]->tag
[i
]->value
);
4488 pgn_board_init_fen (gp
, d
->b
, NULL
);
4489 n
= pgn_history_total(game
[g
]->history
);
4492 for (i
= 0; i
< n
; i
++) {
4494 char move
[MAX_SAN_MOVE_LEN
+1] = {0}, *m
= move
;
4496 strcpy (move
, game
[g
]->history
[i
]->move
);
4497 if (pgn_parse_move(gp
, d
->b
, &m
, &frfr
) != E_PGN_OK
) {
4498 SET_FLAG(gp
->flags
, GF_PERROR
);
4502 pgn_history_add(gp
, d
->b
, move
);
4504 pgn_switch_turn(gp
);
4507 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4510 void do_global_new_all()
4512 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
4513 do_new_game_from_scratch
, 0, 0, "%s", _("Really start a new game from scratch?"));
4516 void do_quit(WIN
*win
)
4518 wchar_t str
[] = { win
->c
, 0 };
4520 if (wcscmp (str
, yes_wchar
))
4526 void do_global_quit()
4528 if (config
.exitdialogbox
)
4529 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
4530 do_quit
, 0, 0, _("Want to Quit?"));
4535 void do_global_toggle_engine_window()
4538 enginew
= newwin(LINES
, COLS
, 0, 0);
4539 enginep
= new_panel(enginew
);
4540 window_draw_title(enginew
, _("Engine IO Window"), COLS
, CP_MESSAGE_TITLE
,
4542 hide_panel(enginep
);
4545 if (panel_hidden(enginep
)) {
4546 update_engine_window(gp
);
4550 hide_panel(enginep
);
4554 void do_global_toggle_board_details()
4559 void do_global_toggle_strict_castling()
4561 do_toggle_strict_castling();
4564 // Global and other keys.
4565 static int globalkeys()
4567 struct userdata_s
*d
= gp
->data
;
4571 * These cannot be modified and other game mode keys cannot conflict with
4576 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
4577 markend
= markstart
= 0;
4581 update_status_notify(gp
, NULL
);
4584 if (config
.validmoves
)
4585 pgn_reset_valid_moves(d
->b
);
4592 keycount
= keycount
* 10 + i
;
4596 update_status_notify(gp
, _("Repeat %i"), keycount
);
4599 if (d
->mode
== MODE_HISTORY
)
4603 d
->c_row
+= keycount
;
4612 if (d
->mode
== MODE_HISTORY
)
4616 d
->c_row
-= keycount
;
4617 update_status_notify(gp
, NULL
);
4627 if (d
->mode
== MODE_HISTORY
)
4631 d
->c_col
-= keycount
;
4640 if (d
->mode
== MODE_HISTORY
)
4644 d
->c_col
+= keycount
;
4656 for (i
= 0; global_keys
[i
]; i
++) {
4657 if (input_c
== global_keys
[i
]->c
) {
4658 (*global_keys
[i
]->f
)();
4669 static void perl_error(const char *fmt
, ...)
4675 vasprintf(&buf
, fmt
, ap
);
4678 message(ERROR_STR
, ANY_KEY_STR
, "%s", buf
);
4682 static void do_perl_finalize(WIN
*win
)
4684 struct input_data_s
*in
= win
->data
;
4686 struct userdata_s
*d
= g
->data
;
4688 char *result
= NULL
;
4692 asprintf(&filename
, "%s/perl.pl", config
.datadir
);
4697 if (perl_init_file(filename
, perl_error
))
4700 arg
= pgn_game_to_fen(g
, d
->b
);
4702 if (perl_call_sub(trim(in
->str
), arg
, &result
))
4705 d
->perlfen
= pgn_game_to_fen(g
, d
->b
);
4706 d
->perlflags
= g
->flags
;
4708 if (pgn_board_init_fen(g
, d
->b
, result
) != E_PGN_OK
) {
4709 message(ERROR_STR
, ANY_KEY_STR
, "%s", _("FEN parse error."));
4710 pgn_board_init_fen(g
, d
->b
, d
->perlfen
);
4711 g
->flags
= d
->perlflags
;
4717 SET_FLAG(d
->flags
, CF_PERL
);
4718 n
= pgn_tag_find(g
->tag
, "FEN");
4721 d
->oldfen
= strdup(g
->tag
[n
]->value
);
4723 pgn_tag_add(&g
->tag
, "FEN", result
);
4724 update_status_notify(g
, "%s", ANY_KEY_STR
);
4735 void do_global_perl()
4737 struct input_data_s
*in
;
4739 in
= Calloc(1, sizeof(struct input_data_s
));
4741 in
->efunc
= do_perl_finalize
;
4742 construct_input(_("PERL Subroutine Filter"), NULL
, 1, 0, NULL
, NULL
, NULL
, 0, in
, INPUT_HIST_PERL
, -1);
4747 * A macro may contain a key that belongs to another macro so macro_match will
4748 * need to be updated to the new index of the matching macro.
4750 static void find_macro(struct userdata_s
*d
)
4755 * Macros can't contain macros when in a window.
4761 for (i
= 0; macros
[i
]; i
++) {
4762 if ((macros
[i
]->mode
== -1 || macros
[i
]->mode
== d
->mode
) &&
4763 input_c
== macros
[i
]->c
) {
4764 input_c
= macros
[i
]->keys
[macros
[i
]->n
++];
4766 if (!macro_depth_n
&& macro_match
> -1) {
4767 macro_depth
= realloc(macro_depth
, (macro_depth_n
+ 1) * sizeof(int));
4768 macro_depth
[macro_depth_n
++] = macro_match
;
4771 macro_depth
= realloc(macro_depth
, (macro_depth_n
+ 1) * sizeof(int));
4772 macro_depth
[macro_depth_n
++] = i
;
4780 * Resets the position in each macro to the first key.
4782 static void reset_macros()
4785 struct userdata_s
*d
= gp
->data
;
4788 if (macro_depth_n
> 0) {
4790 macro_match
= macro_depth
[macro_depth_n
];
4792 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
4795 input_c
= macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
4800 for (i
= 0; macros
[i
]; i
++)
4811 struct userdata_s
*d
;
4814 gindex
= gtotal
- 1;
4818 if (pgn_history_total(gp
->hp
))
4819 d
->mode
= MODE_HISTORY
;
4821 d
->mode
= MODE_PLAY
;
4822 d
->play_mode
= PLAY_HE
;
4825 if (d
->mode
== MODE_HISTORY
)
4826 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4828 update_status_notify(gp
, "%s", _("Type F1 for help"));
4832 wtimeout(boardw
, WINDOW_TIMEOUT
);
4836 char fdbuf
[8192] = {0};
4838 struct timeval tv
= {0, 0};
4846 for (i
= 0; i
< gtotal
; i
++) {
4849 if (d
->engine
&& d
->engine
->pid
!= -1) {
4850 if (d
->engine
->fd
[ENGINE_IN_FD
] > 2) {
4851 if (d
->engine
->fd
[ENGINE_IN_FD
] > n
)
4852 n
= d
->engine
->fd
[ENGINE_IN_FD
];
4854 FD_SET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
);
4857 if (d
->engine
->fd
[ENGINE_OUT_FD
] > 2) {
4858 if (d
->engine
->fd
[ENGINE_OUT_FD
] > n
)
4859 n
= d
->engine
->fd
[ENGINE_OUT_FD
];
4861 FD_SET(d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
);
4867 if ((n
= select(n
+ 1, &rfds
, &wfds
, NULL
, &tv
)) > 0) {
4868 for (i
= 0; i
< gtotal
; i
++) {
4871 if (d
->engine
&& d
->engine
->pid
!= -1) {
4872 if (FD_ISSET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
)) {
4873 len
= read(d
->engine
->fd
[ENGINE_IN_FD
], fdbuf
,
4877 if (d
->engine
->iobuf
)
4878 d
->engine
->iobuf
= Realloc(d
->engine
->iobuf
, d
->engine
->len
+ len
+ 1);
4880 d
->engine
->iobuf
= Calloc(1, len
+ 1);
4882 memcpy(&(d
->engine
->iobuf
[d
->engine
->len
]), &fdbuf
, len
);
4883 d
->engine
->len
+= len
;
4884 d
->engine
->iobuf
[d
->engine
->len
] = 0;
4887 * The fdbuf is full or no newline
4888 * was found. So we'll append the next
4889 * read() to this games buffer.
4891 if (d
->engine
->iobuf
[d
->engine
->len
- 1] != '\n')
4894 parse_engine_output(game
[i
], d
->engine
->iobuf
);
4895 free(d
->engine
->iobuf
);
4896 d
->engine
->iobuf
= NULL
;
4899 else if (len
== -1) {
4900 if (errno
!= EAGAIN
) {
4901 cmessage(ERROR_STR
, ANY_KEY_STR
, "Engine read(): %s",
4903 waitpid(d
->engine
->pid
, &n
, 0);
4911 if (FD_ISSET(d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
)) {
4912 if (d
->engine
->queue
)
4913 send_engine_command(game
[i
]);
4920 cmessage(ERROR_STR
, ANY_KEY_STR
, "select(): %s", strerror(errno
));
4929 * This is needed to detect terminal resizing.
4932 if (LINES
!= LINES_OLD
|| COLS
!= COLS_OLD
) {
4939 * Finds the top level window in the window stack so we know what
4940 * window the wget_wch()'ed key belongs to.
4943 for (i
= 0; wins
[i
]; i
++);
4946 wtimeout(wp
, WINDOW_TIMEOUT
);
4955 if (macros
&& macro_match
>= 0) {
4956 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
4959 input_c
= macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
4964 if (wget_wch(wp
, &input_c
) == ERR
|| input_c
== KEY_RESIZE
)
4974 do_window_resize ();
4981 * Run the function associated with the window. When the
4982 * function returns 0 win->efunc is ran (if not NULL) with
4983 * win as the one and only parameter. Then the window is
4986 * The exit function may create another window which will
4987 * mess up the window stack when window_destroy() is called.
4988 * So don't destory the window until the top window is
4989 * destroyable. See window_destroy().
4991 if ((*win
->func
)(win
) == 0) {
4996 window_destroy(win
);
5004 if (!keycount
&& status
.notify
)
5005 update_status_notify(gp
, NULL
);
5008 if (TEST_FLAG(d
->flags
, CF_PERL
)) {
5009 CLEAR_FLAG(d
->flags
, CF_PERL
);
5010 pgn_board_init_fen(gp
, d
->b
, d
->perlfen
);
5011 gp
->flags
= d
->perlflags
;
5013 pgn_tag_add(&gp
->tag
, "FEN", d
->oldfen
);
5015 d
->perlfen
= d
->oldfen
= NULL
;
5021 if (macros
&& macro_match
< 0)
5024 if ((n
= globalkeys()) == 1) {
5025 if (macro_match
== -1)
5035 for (i
= 0; edit_keys
[i
]; i
++) {
5036 if (input_c
== edit_keys
[i
]->c
) {
5037 (*edit_keys
[i
]->f
)();
5043 for (i
= 0; play_keys
[i
]; i
++) {
5044 if (input_c
== play_keys
[i
]->c
) {
5045 (*play_keys
[i
]->f
)();
5050 do_play_config_command();
5053 for (i
= 0; history_keys
[i
]; i
++) {
5054 if (input_c
== history_keys
[i
]->c
) {
5055 (*history_keys
[i
]->f
)();
5066 update_status_notify(gp
, NULL
);
5075 void usage(const char *pn
, int ret
)
5077 fprintf((ret
) ? stderr
: stdout
, "%s%s",
5080 "Usage: cboard [-hvCD] [-u [N]] [-p [-VtRSE] <file>]\n"
5081 " -D Dump libchess debugging info to \"libchess.debug\" (stderr)\n"),
5084 "Usage: cboard [-hvC] [-u [N]] [-p [-VtRSE] <file>]\n"),
5087 " -p Load PGN file.\n"
5088 " -V Validate a game file.\n"
5089 " -S Validate and output a PGN formatted game.\n"
5090 " -R Like -S but write a reduced PGN formatted game.\n"
5091 " -t Also write custom PGN tags from config file.\n"
5092 " -E Stop processing on file parsing error (overrides config).\n"
5093 " -C Enable strict castling (overrides config).\n"
5094 " -u Enable/disable UTF-8 pieces (1=enable, 0=disable, overrides config).\n"
5095 " -v Version information.\n"
5096 " -h This help text.\n"));
5108 free(config
.engine_cmd
);
5109 free(config
.pattern
);
5110 free(config
.ccfile
);
5111 free(config
.nagfile
);
5112 free(config
.configfile
);
5115 for (i
= 0; config
.keys
[i
]; i
++) {
5116 free(config
.keys
[i
]->str
);
5117 free(config
.keys
[i
]);
5124 for (i
= 0; config
.einit
[i
]; i
++)
5125 free(config
.einit
[i
]);
5131 pgn_tag_free(config
.tag
);
5133 free(config
.datadir
);
5135 if (curses_initialized
) {
5137 del_panel(historyp
);
5158 static void signal_save_pgn(int sig
)
5162 char *p
= config
.savedirectory
? config
.savedirectory
: config
.datadir
;
5165 asprintf(&buf
, "%s/signal-%i-%li.pgn", p
, sig
, now
);
5167 if (do_game_write(buf
, "w", 0, gtotal
)) {
5168 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", p
, strerror(errno
));
5169 update_status_notify(gp
, "%s", _("Save game failed."));
5176 void catch_signal(int which
)
5183 if (which
== SIGPIPE
&& quit
)
5186 if (which
== SIGPIPE
)
5187 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Broken pipe. Quitting."));
5197 do_window_resize ();
5198 keypad(boardw
, TRUE
);
5204 signal_save_pgn(which
);
5211 void loading_progress(long total
, long offset
)
5213 int n
= (100 * (offset
/ 100) / (total
/ 100));
5215 if (curses_initialized
)
5216 update_loading_window(n
);
5218 fprintf(stderr
, _("Loading... %i%% (%i games)%c"), n
, gtotal
, '\r');
5223 static void set_defaults()
5225 set_config_defaults();
5227 filetype
= FILE_NONE
;
5228 pgn_config_set(PGN_PROGRESS
, 1024);
5229 pgn_config_set(PGN_PROGRESS_FUNC
, loading_progress
);
5232 int main(int argc
, char *argv
[])
5236 char buf
[FILENAME_MAX
];
5237 char datadir
[FILENAME_MAX
];
5238 int ret
= EXIT_SUCCESS
;
5239 int validate_only
= 0, validate_and_write
= 0;
5240 int write_custom_tags
= 0;
5243 int utf8_pieces
= -1;
5245 setlocale (LC_ALL
, "");
5246 bindtextdomain ("cboard", LOCALE_DIR
);
5247 textdomain ("cboard");
5250 #ifndef HAVE_PROGNAME
5251 __progname
= argv
[0];
5254 if ((config
.pwd
= getpwuid(getuid())) == NULL
)
5255 err(EXIT_FAILURE
, "getpwuid()");
5257 snprintf(datadir
, sizeof(datadir
), "%s/.cboard", config
.pwd
->pw_dir
);
5258 config
.datadir
= strdup(datadir
);
5259 snprintf(buf
, sizeof(buf
), "%s/cc.data", datadir
);
5260 config
.ccfile
= strdup(buf
);
5261 snprintf(buf
, sizeof(buf
), "%s/nag.data", datadir
);
5262 config
.nagfile
= strdup(buf
);
5263 snprintf(buf
, sizeof(buf
), "%s/config", datadir
);
5264 config
.configfile
= strdup(buf
);
5266 if (stat(datadir
, &st
) == -1) {
5267 if (errno
== ENOENT
) {
5268 if (mkdir(datadir
, 0755) == -1)
5269 err(EXIT_FAILURE
, "%s", datadir
);
5272 err(EXIT_FAILURE
, "%s", datadir
);
5277 if (!S_ISDIR(st
.st_mode
))
5278 errx(EXIT_FAILURE
, "%s: %s", datadir
, _("Not a directory."));
5283 while ((opt
= getopt(argc
, argv
, "DCEVtSRhp:vu::")) != -1) {
5285 while ((opt
= getopt(argc
, argv
, "ECVtSRhp:vu::")) != -1) {
5290 unlink("libchess.debug");
5291 pgn_config_set(PGN_DEBUG
, 1);
5295 pgn_config_set(PGN_STRICT_CASTLING
, 1);
5298 write_custom_tags
= 1;
5304 pgn_config_set(PGN_REDUCED
, 1);
5306 validate_and_write
= 1;
5311 printf("%s (%s)\n%s\n", PACKAGE_STRING
, curses_version(),
5315 filetype
= FILE_PGN
;
5316 strncpy(loadfile
, optarg
, sizeof(loadfile
));
5317 loadfile
[sizeof(loadfile
)-1] = 0;
5320 utf8_pieces
= optarg
? atoi (optarg
): 1;
5324 usage(argv
[0], EXIT_SUCCESS
);
5328 if ((validate_only
|| validate_and_write
) && !*loadfile
)
5329 usage(argv
[0], EXIT_FAILURE
);
5331 if (access(config
.configfile
, R_OK
) == 0)
5332 parse_rcfile(config
.configfile
);
5335 pgn_config_set(PGN_STOP_ON_ERROR
, 1);
5337 signal(SIGPIPE
, catch_signal
);
5338 signal(SIGCONT
, catch_signal
);
5339 signal(SIGSTOP
, catch_signal
);
5340 signal(SIGINT
, catch_signal
);
5341 signal(SIGALRM
, catch_signal
);
5342 signal(SIGTERM
, catch_signal
);
5348 if (pgn_open(loadfile
, "r", &pgn
) != E_PGN_OK
)
5349 err(EXIT_FAILURE
, "%s", loadfile
);
5351 ret
= pgn_parse(pgn
);
5355 //ret = parse_fen_file(loadfile);
5357 case FILE_EPD
: // Not implemented.
5360 // No file specified. Empty game.
5361 ret
= pgn_parse(NULL
);
5363 add_custom_tags(&gp
->tag
);
5367 if (validate_only
|| validate_and_write
) {
5368 if (validate_and_write
) {
5369 if (pgn_open("-", "r", &pgn
) != E_PGN_OK
)
5370 err(EXIT_FAILURE
, "pgn_open()");
5372 for (i
= 0; i
< gtotal
; i
++) {
5373 if (write_custom_tags
)
5374 add_custom_tags(&game
[i
]->tag
);
5376 pgn_write(pgn
, game
[i
]);
5381 fm_loaded_file
= TRUE
;
5387 else if (ret
== E_PGN_ERR
)
5390 if (utf8_pieces
!= -1)
5391 config
.utf8_pieces
= utf8_pieces
;
5393 init_wchar_pieces ();
5394 yes_wchar
= str_to_wchar (_("y"));
5395 all_wchar
= str_to_wchar (_("a"));
5396 overwrite_wchar
= str_to_wchar (_("o"));
5397 resume_wchar
= str_to_wchar (_("r"));
5398 current_wchar
= str_to_wchar (_("c"));
5399 append_wchar
= str_to_wchar (_("a"));
5400 translatable_tag_names
[0] = _("Event");
5401 translatable_tag_names
[1] = _("Site");
5402 translatable_tag_names
[2] = _("Date");
5403 translatable_tag_names
[3] = _("Round");
5404 translatable_tag_names
[4] = _("White");
5405 translatable_tag_names
[5] = _("Black");
5406 translatable_tag_names
[6] = _("Result");
5410 * This fixes window resizing in an xterm.
5412 if (getenv("DISPLAY") != NULL
) {
5417 if (initscr() == NULL
)
5418 errx(EXIT_FAILURE
, "%s", _("Could not initialize curses."));
5420 curses_initialized
= 1;
5422 if (LINES
< 23 || COLS
< 74) {
5424 errx(EXIT_FAILURE
, _("Need at least an 74x23 terminal."));
5430 if (has_colors() == TRUE
&& start_color() == OK
)
5433 boardw
= newwin(BOARD_HEIGHT
, BOARD_WIDTH
, 0, COLS
- BOARD_WIDTH
);
5434 boardp
= new_panel(boardw
);
5435 historyw
= newwin(HISTORY_HEIGHT
, HISTORY_WIDTH
, LINES
- HISTORY_HEIGHT
,
5436 COLS
- HISTORY_WIDTH
);
5437 historyp
= new_panel(historyw
);
5438 statusw
= newwin(STATUS_HEIGHT
, STATUS_WIDTH
, 0, 0);
5439 statusp
= new_panel(statusw
);
5440 tagw
= newwin(TAG_HEIGHT
, TAG_WIDTH
, STATUS_HEIGHT
+ 1, 0);
5441 tagp
= new_panel(tagw
);
5442 keypad(boardw
, TRUE
);
5443 // leaveok(boardw, TRUE);
5444 leaveok(tagw
, TRUE
);
5445 leaveok(statusw
, TRUE
);
5446 leaveok(historyw
, TRUE
);
5450 draw_window_decor();
5453 free (w_pawn_wchar
);
5454 free (w_rook_wchar
);
5455 free (w_bishop_wchar
);
5456 free (w_knight_wchar
);
5457 free (w_queen_wchar
);
5458 free (w_king_wchar
);
5459 free (b_pawn_wchar
);
5460 free (b_rook_wchar
);
5461 free (b_bishop_wchar
);
5462 free (b_knight_wchar
);
5463 free (b_queen_wchar
);
5464 free (b_king_wchar
);
5466 free (enpassant_wchar
);
5469 free (overwrite_wchar
);
5470 free (resume_wchar
);
5471 free (current_wchar
);
5472 free (append_wchar
);