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
;
754 int v
= d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].valid
;
755 int pi
= pgn_piece_to_int (d
->b
[RANKTOBOARD (rank
)][FILETOBOARD (file
)].icon
);
757 ? pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
)
758 : pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].icon
);
760 if (pi
== OPEN_SQUARE
|| cpi
== OPEN_SQUARE
|| !VALIDFILE (file
)
761 || !VALIDRANK (rank
))
765 col
= v
? d
->c_col
: d
->sp
.scol
;
766 row
= v
? d
->c_row
: d
->sp
.srow
;
773 m
= malloc(MAX_SAN_MOVE_LEN
+1);
774 m
[0] = INTTOFILE (file
);
775 m
[1] = INTTORANK (rank
);
776 m
[2] = INTTOFILE (col
);
777 m
[3] = INTTORANK (row
);
780 if (d
->sp
.icon
&& v
) {
783 memcpy (b
, d
->b
, sizeof(BOARD
));
784 p
= b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
;
785 b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
786 pgn_int_to_piece (WHITE
, OPEN_SQUARE
);
787 b
[RANKTOBOARD (row
)][FILETOBOARD (col
)].icon
= p
;
789 e
= pgn_validate_move (g
, b
, &m
, &frfr
);
793 return e
== E_PGN_OK
? 1 : 0;
797 e
= pgn_validate_move (g
, d
->b
, &m
, &frfr
);
800 if (!strcmp (m
, "O-O") || !strcmp (m
, "O-O-O"))
804 int sf
= FILETOINT (frfr
[0]), sr
= RANKTOINT (frfr
[1]);
805 int df
= FILETOINT (frfr
[2]);
807 pi
= d
->b
[RANKTOBOARD (sr
)][FILETOBOARD (sf
)].icon
;
808 pi
= pgn_piece_to_int (pi
);
809 if (pi
== PAWN
&& sf
== df
)
815 return e
== E_PGN_OK
? 1 : 0;
818 void print_piece(WINDOW
*w
, int l
, int c
, char p
)
822 for (i
= 0; i
< 13; i
+= 2) {
823 if (p
== piece_chars
[i
] || p
== piece_chars
[i
+ 1]) {
824 for (y
= 0; y
< 3; y
++)
825 mvwprintw(w
, l
+ y
, c
, "%s", f_pieces
[i
+ ff
+ y
]);
832 for (y
= 0; y
< 3; y
++)
833 mvwprintw(w
, l
+ y
, c
, f_pieces
[0]);
840 for (fx
= 1; fx
< 9; fx
++) {
849 int coordofmove(char *move
, int yorx
)
854 if (!strcmp(move
, "O-O"))
855 return (yorx
) ? 8 : 7;
856 else if (!strcmp(move
, "O-O-O"))
857 return (yorx
) ? 8 : 3;
860 if (!strcmp(move
, "O-O"))
861 return (yorx
) ? 1 : 7;
862 else if (!strcmp(move
, "O-O-O"))
863 return (yorx
) ? 1 : 3;
869 return move
[l
- 1] - 48;
871 for (i
= 0; i
< 8; i
++) {
872 if (move
[l
- 2] == "abcdefgh"[i
])
880 static int is_prev_move (struct userdata_s
*d
, int brow
, int bcol
,
884 row
== ROWTOMATRIX(((rotate
) ? inv_int(d
->pm_row
) : d
->pm_row
)) &&
885 col
== COLTOMATRIX(((rotate
) ? inv_int(d
->pm_col
) : d
->pm_col
)))
887 brow
+ 1 == ((rotate
) ? d
->pm_row
: inv_int(d
->pm_row
)) &&
888 bcol
+ 1 == ((rotate
) ? inv_int(d
->pm_col
) : d
->pm_col
)))
894 // FIXME: BIG_BOARD - start in dwm monocle layout.
895 // Example: lxterminal -e cboard (dmenu, geany, etc.)
896 void update_board_window(GAME g
)
899 int bcol
= 0, brow
= 0;
900 int l
= config
.coordsyleft
;
901 int maxy
= BOARD_HEIGHT
, maxx
= BOARD_WIDTH
;
902 int ncols
= 0, offset
= 1;
903 int rowr
= (MEGA_BOARD
) ? 6 : (BIG_BOARD
) ? 4 : 2;
904 int colr
= (MEGA_BOARD
) ? 12 : (BIG_BOARD
) ? 8 : 4;
905 unsigned coords_y
= 8, cxgc
= 0;
907 struct userdata_s
*d
= g
->data
;
910 h
= pgn_history_by_n(g
->hp
, g
->hindex
- 1);
911 d
->pm_row
= (h
&& h
->move
&& d
->mode
== MODE_PLAY
)
912 ? coordofmove(h
->move
, 1) : 0;
913 d
->pm_col
= (h
&& h
->move
&& d
->mode
== MODE_PLAY
)
914 ? coordofmove(h
->move
, 0) : 0;
916 if (d
->mode
!= MODE_PLAY
&& d
->mode
!= MODE_EDIT
)
917 update_cursor(g
, g
->hindex
);
934 for (row
= 0; row
< maxy
; row
++) {
948 for (col
= 0; col
< maxx
; col
++) {
950 chtype attrs
= 0, old_attrs
= 0;
955 if (row
== 0 || row
== maxy
- 2) {
957 mvwaddch(boardw
, row
, col
+ l
,
959 ? ACS_LLCORNER
| CP_BOARD_GRAPHICS
960 : ACS_ULCORNER
| CP_BOARD_GRAPHICS
));
961 else if (col
== maxx
- 2)
962 mvwaddch(boardw
, row
, col
+ l
,
964 ? ACS_LRCORNER
| CP_BOARD_GRAPHICS
965 : ACS_URCORNER
| CP_BOARD_GRAPHICS
));
966 else if (!(col
% colr
))
967 mvwaddch(boardw
, row
, col
+ l
,
969 ? ACS_BTEE
| CP_BOARD_GRAPHICS
970 : ACS_TTEE
| CP_BOARD_GRAPHICS
));
973 mvwaddch(boardw
, row
, col
+ l
,
974 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
980 if ((row
% 2) && col
== maxx
- 1 &&
981 (coords_y
> 0 && coords_y
< 9)) {
982 wattron(boardw
, CP_BOARD_COORDS
);
984 (BIG_BOARD
) ? row
* ((MEGA_BOARD
) ? 3 : 2)
985 : row
, (l
) ? 0 : col
, "%d",
986 (rotate
) ? coords_y
++ : coords_y
--);
987 wattroff(boardw
, CP_BOARD_COORDS
);
991 if ((col
== 0 || col
== maxx
- 2) && row
!= maxy
- 1) {
993 mvwaddch(boardw
, row
, col
+ l
,
995 ACS_RTEE
| CP_BOARD_GRAPHICS
:
996 ACS_LTEE
| CP_BOARD_GRAPHICS
));
998 mvwaddch(boardw
, row
, col
+ l
,
999 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
1004 if ((row
% rowr
) && !(col
% colr
) && row
!= maxy
- 1) {
1005 mvwaddch(boardw
, row
, col
+ l
,
1006 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
1010 if (!(col
% colr
) && row
!= maxy
- 1) {
1011 mvwaddch(boardw
, row
, col
+ l
,
1012 LINE_GRAPHIC(ACS_PLUS
| CP_BOARD_GRAPHICS
));
1019 attrwhich
= (cb
[brow
][bcol
]) ? WHITE
: BLACK
;
1026 if (((ncols
% 2) && !(offset
% 2))
1027 || (!(ncols
% 2) && (offset
% 2)))
1033 if (BIG_BOARD
&& rotate
) {
1034 brow
= inv_int(brow
+ 1) - 1;
1035 bcol
= inv_int(bcol
+ 1) - 1;
1039 p
= d
->b
[brow
][bcol
].icon
;
1041 p
= d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].icon
;
1043 int pi
= pgn_piece_to_int(p
);
1045 if (config
.details
&&
1046 ((!BIG_BOARD
&& d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].enpassant
)
1047 || (BIG_BOARD
&& d
->b
[brow
][bcol
].enpassant
))) {
1049 attrs
= mix_cp(CP_BOARD_ENPASSANT
,
1050 (attrwhich
== WHITE
) ? CP_BOARD_WHITE
: CP_BOARD_BLACK
,
1051 ATTRS(CP_BOARD_ENPASSANT
), A_FG_B_BG
);
1054 // FIXME: showattacks - rotated board.
1055 if (config
.showattacks
&& config
.details
1056 && piece_can_attack (g
,
1057 BIG_BOARD
? inv_int (brow
+1) : brow
,
1058 BIG_BOARD
? bcol
+1 : bcol
)) {
1059 attrs
= CP_BOARD_ATTACK
;
1064 if (config
.validmoves
&&
1065 ((!BIG_BOARD
&& d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].valid
)
1066 || (BIG_BOARD
&& d
->b
[brow
][bcol
].valid
))) {
1070 if (attrwhich
== WHITE
)
1071 attrs
= mix_cp(CP_BOARD_MOVES_WHITE
,
1073 ATTRS(CP_BOARD_MOVES_WHITE
),
1076 attrs
= mix_cp(CP_BOARD_MOVES_BLACK
,
1078 ATTRS(CP_BOARD_MOVES_BLACK
),
1081 else if (p
!= 'x' && !can_attack
)
1082 attrs
= (attrwhich
== WHITE
) ? CP_BOARD_WHITE
: CP_BOARD_BLACK
;
1084 if (BIG_BOARD
&& rotate
) {
1085 brow
= inv_int(brow
+ 1) - 1;
1086 bcol
= inv_int(bcol
+ 1) - 1;
1089 if ((!BIG_BOARD
&& row
== ROWTOMATRIX(d
->c_row
)
1090 && col
== COLTOMATRIX(d
->c_col
))
1091 || (BIG_BOARD
&& brow
+ 1 == inv_int(d
->c_row
)
1092 && bcol
+ 1 == d
->c_col
)) {
1093 attrs
= mix_cp(CP_BOARD_CURSOR
, IS_ENPASSANT(p
),
1094 ATTRS(CP_BOARD_CURSOR
), B_FG_A_BG
);
1097 else if ((!BIG_BOARD
&& row
== ROWTOMATRIX(d
->sp
.srow
) &&
1098 col
== COLTOMATRIX(d
->sp
.scol
)) ||
1099 (BIG_BOARD
&& brow
+ 1 == inv_int(d
->sp
.srow
) &&
1100 bcol
+ 1 == d
->sp
.scol
)) {
1101 attrs
= mix_cp(CP_BOARD_SELECTED
, IS_ENPASSANT(p
),
1102 ATTRS(CP_BOARD_SELECTED
), B_FG_A_BG
);
1105 else if (is_prev_move (d
, brow
, bcol
, row
, col
) && !valid
)
1107 attrs
= mix_cp(CP_BOARD_PREVMOVE
, IS_ENPASSANT(p
),
1108 ATTRS(CP_BOARD_PREVMOVE
), B_FG_A_BG
);
1112 if (row
== maxy
- 1)
1116 int n
= is_prev_move (d
, brow
, bcol
, row
, col
);
1117 chtype a
= n
&& !valid
1118 ? CP_BOARD_PREVMOVE
: attrwhich
== WHITE
? valid
? CP_BOARD_MOVES_WHITE
: CP_BOARD_WHITE
: valid
? CP_BOARD_MOVES_BLACK
: CP_BOARD_BLACK
;
1119 attrs
= mix_cp(CP_BOARD_ATTACK
, a
,
1120 ATTRS (CP_BOARD_ATTACK
), A_FG_B_BG
);
1125 wmove(boardw
, row
, col
+ ((MEGA_BOARD
) ? 5 : 3) + l
);
1127 mvwaddch(boardw
, row
, col
+ l
, ' ' | attrs
);
1129 if (row
== maxy
- 1 && cxgc
< 8) {
1130 waddch(boardw
, "abcdefgh"[(BIG_BOARD
) ? bcol
: bcol
- 1] | CP_BOARD_COORDS
);
1134 if (old_attrs
== -1) {
1141 if (pi
!= OPEN_SQUARE
&& p
!= 'x' && !can_attack
) {
1142 if (attrwhich
== WHITE
) {
1144 attrs
= CP_BOARD_W_W
;
1146 attrs
= CP_BOARD_W_B
;
1150 attrs
= CP_BOARD_B_W
;
1152 attrs
= CP_BOARD_B_B
;
1158 if (config
.details
&& !can_attack
1159 && castling_state(g
, d
->b
,
1160 (rotate
) ? inv_int(brow
+ 1) - 1: brow
,
1161 (rotate
) ? inv_int(bcol
+ 1) - 1: bcol
, p
, 0))
1162 attrs
= mix_cp(CP_BOARD_CASTLING
, attrs
,
1163 ATTRS(CP_BOARD_CASTLING
),
1167 if (config
.details
&& !can_attack
1168 && castling_state(g
, d
->b
, RANKTOBOARD (brow
),
1169 FILETOBOARD (bcol
), p
, 0)) {
1170 attrs
= mix_cp(CP_BOARD_CASTLING
, attrs
,
1171 ATTRS(CP_BOARD_CASTLING
),
1177 // FIXME: Reimpresión de piezas(+3).
1179 wattron (boardw
, attrs
);
1181 for (i
= 0; i
< 5; i
++)
1182 mvwprintw(boardw
, i
+ brow
* 6 + 1,
1185 if (pi
!= OPEN_SQUARE
)
1186 print_piece(boardw
, brow
* 6 + 2,
1187 bcol
* 12 + 3 + l
, p
);
1190 print_piece(boardw
, brow
* 4 + 1,
1192 (pi
!= OPEN_SQUARE
) ? p
: 0);
1195 wattroff (boardw
, attrs
);
1200 wattron (boardw
, attrs
);
1201 waddwstr (boardw
, piece_to_wchar (pi
!= OPEN_SQUARE
? p
: 0));
1202 wattroff (boardw
, attrs
);
1209 col
+= (MEGA_BOARD
) ? 10 : 6;
1211 waddch(boardw
, ' ' | attrs
);
1229 if (col
!= maxx
- 1)
1230 mvwaddch(boardw
, row
, col
+ l
,
1231 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
1251 void invalid_move(int n
, int e
, const char *m
)
1253 if (curses_initialized
)
1254 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s \"%s\" (round #%i)", (e
== E_PGN_AMBIGUOUS
)
1255 ? _("Ambiguous move") : _("Invalid move"), m
, n
);
1257 warnx("%s: %s \"%s\" (round #%i)", loadfile
, (e
== E_PGN_AMBIGUOUS
)
1258 ? _("Ambiguous move") : _("Invalid move"), m
, n
);
1261 void gameover(GAME g
)
1263 struct userdata_s
*d
= g
->data
;
1265 SET_FLAG(g
->flags
, GF_GAMEOVER
);
1266 d
->mode
= MODE_HISTORY
;
1270 static void update_clock(GAME g
, struct itimerval it
)
1272 struct userdata_s
*d
= g
->data
;
1274 if (TEST_FLAG(d
->flags
, CF_CLOCK
) && g
->turn
== WHITE
) {
1275 d
->wclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1276 d
->wclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1278 if (d
->wclock
.elapsed
.tv_usec
> 1000000 - 1) {
1279 d
->wclock
.elapsed
.tv_sec
+= d
->wclock
.elapsed
.tv_usec
/ 1000000;
1280 d
->wclock
.elapsed
.tv_usec
= d
->wclock
.elapsed
.tv_usec
% 1000000;
1283 if (d
->wclock
.tc
[d
->wclock
.tcn
][1] &&
1284 d
->wclock
.elapsed
.tv_sec
>= d
->wclock
.tc
[d
->wclock
.tcn
][1]) {
1285 pgn_tag_add(&g
->tag
, "Result", "0-1");
1289 else if (TEST_FLAG(d
->flags
, CF_CLOCK
) && g
->turn
== BLACK
) {
1290 d
->bclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1291 d
->bclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1293 if (d
->bclock
.elapsed
.tv_usec
> 1000000 - 1) {
1294 d
->bclock
.elapsed
.tv_sec
+= d
->bclock
.elapsed
.tv_usec
/ 1000000;
1295 d
->bclock
.elapsed
.tv_usec
= d
->bclock
.elapsed
.tv_usec
% 1000000;
1298 if (d
->bclock
.tc
[d
->bclock
.tcn
][1] &&
1299 d
->bclock
.elapsed
.tv_sec
>= d
->bclock
.tc
[d
->bclock
.tcn
][1]) {
1300 pgn_tag_add(&g
->tag
, "Result", "1-0");
1305 d
->elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1306 d
->elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1308 if (d
->elapsed
.tv_usec
> 1000000 - 1) {
1309 d
->elapsed
.tv_sec
+= d
->elapsed
.tv_usec
/ 1000000;
1310 d
->elapsed
.tv_usec
= d
->elapsed
.tv_usec
% 1000000;
1314 static void update_time_control(GAME g
)
1316 struct userdata_s
*d
= g
->data
;
1317 struct clock_s
*clk
= (g
->turn
== WHITE
) ? &d
->wclock
: &d
->bclock
;
1320 clk
->tc
[clk
->tcn
][1] += clk
->incr
;
1322 if (!clk
->tc
[clk
->tcn
][1])
1327 if (!clk
->tc
[clk
->tcn
][0] || clk
->move
>= clk
->tc
[clk
->tcn
][0]) {
1329 clk
->tc
[clk
->tcn
+ 1][1] += abs(clk
->elapsed
.tv_sec
- clk
->tc
[clk
->tcn
][1]);
1330 memset(&clk
->elapsed
, 0, sizeof(clk
->elapsed
));
1335 void update_history_window(GAME g
)
1337 char buf
[HISTORY_WIDTH
- 1];
1340 int t
= pgn_history_total(g
->hp
);
1342 n
= (g
->hindex
+ 1) / 2;
1345 total
= (t
+ 1) / 2;
1350 snprintf(buf
, sizeof(buf
), "%u %s %u%s", n
, _("of"), total
,
1351 (movestep
== 1) ? _(" (ply)") : "");
1353 strncpy(buf
, _("not available"), sizeof(buf
)-1);
1355 buf
[sizeof(buf
)-1] = 0;
1356 mvwprintw(historyw
, 2, 1, "%*s %-*s", 10, _("Move:"),
1357 HISTORY_WIDTH
- 14, buf
);
1359 h
= pgn_history_by_n(g
->hp
, g
->hindex
);
1360 snprintf(buf
, sizeof(buf
), "%s",
1361 (h
&& h
->move
) ? h
->move
1362 : (LINES
< 24) ? _("empty") : _("not available"));
1365 if (h
&& ((h
->comment
) || h
->nag
[0])) {
1366 strncat(buf
, _(" (Annotated"), sizeof(buf
)-1);
1371 strncat(buf
, (n
) ? ",+" : " (+", sizeof(buf
)-1);
1376 strncat(buf
, (n
) ? ",-" : " (-", sizeof(buf
)-1);
1381 strncat(buf
, ")", sizeof(buf
)-1);
1383 mvwprintw(historyw
, 3, ((LINES
< 24) ? 17 : 1), "%s %-*s",
1384 (LINES
< 24) ? _("Next:") :_("Next move:"),
1385 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1387 h
= pgn_history_by_n(g
->hp
, g
->hindex
- 1);
1388 snprintf(buf
, sizeof(buf
), "%s",
1389 (h
&& h
->move
) ? h
->move
1390 : (LINES
< 24) ? _("empty") : _("not available"));
1393 if (h
&& ((h
->comment
) || h
->nag
[0])) {
1394 strncat(buf
, _(" (Annotated"), sizeof(buf
)-1);
1399 strncat(buf
, (n
) ? ",+" : " (+", sizeof(buf
)-1);
1404 strncat(buf
, (n
) ? ",-" : " (-", sizeof(buf
)-1);
1409 strncat(buf
, ")", sizeof(buf
)-1);
1411 mvwprintw(historyw
, ((LINES
< 24) ? 3 : 4), 1, "%s %-*s",
1412 (LINES
< 24) ? _("Prev.:") : _("Prev move:"),
1413 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1416 void do_validate_move(char *move
)
1418 struct userdata_s
*d
= gp
->data
;
1422 if (TEST_FLAG(d
->flags
, CF_HUMAN
)) {
1423 if ((n
= pgn_parse_move(gp
, d
->b
, &move
, &frfr
)) != E_PGN_OK
) {
1424 invalid_move(d
->n
+ 1, n
, move
);
1428 update_time_control(gp
);
1429 pgn_history_add(gp
, d
->b
, move
);
1430 pgn_switch_turn(gp
);
1433 if ((n
= pgn_validate_move(gp
, d
->b
, &move
, &frfr
)) != E_PGN_OK
) {
1434 invalid_move(d
->n
+ 1, n
, move
);
1438 add_engine_command(gp
, ENGINE_THINKING
, "%s\n",
1439 (config
.engine_protocol
== 1) ? frfr
: move
);
1442 d
->sp
.srow
= d
->sp
.scol
= d
->sp
.icon
= 0;
1444 if (config
.validmoves
)
1445 pgn_reset_valid_moves(d
->b
);
1447 if (TEST_FLAG(gp
->flags
, GF_GAMEOVER
))
1448 d
->mode
= MODE_HISTORY
;
1450 SET_FLAG(d
->flags
, CF_MODIFIED
);
1454 update_history_window(gp
);
1455 update_board_window(gp
);
1459 void do_promotion_piece_finalize(WIN
*win
)
1461 char *p
, *str
= win
->data
;
1463 if (pgn_piece_to_int(win
->c
) == -1)
1466 p
= str
+ strlen(str
);
1467 *p
++ = toupper(win
->c
);
1469 do_validate_move(str
);
1474 static void move_to_engine(GAME g
)
1476 struct userdata_s
*d
= g
->data
;
1480 if (config
.validmoves
&&
1481 !d
->b
[RANKTOBOARD(d
->sp
.row
)][FILETOBOARD(d
->sp
.col
)].valid
)
1484 str
= Malloc(MAX_SAN_MOVE_LEN
+ 1);
1485 snprintf(str
, MAX_SAN_MOVE_LEN
+ 1, "%c%i%c%i",
1486 _("abcdefgh")[d
->sp
.scol
- 1],
1487 d
->sp
.srow
, _("abcdefgh")[d
->sp
.col
- 1], d
->sp
.row
);
1489 piece
= pgn_piece_to_int(d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
);
1491 if (piece
== PAWN
&& (d
->sp
.row
== 8 || d
->sp
.row
== 1)) {
1492 construct_message(_("Select Pawn Promotion Piece"), _ ("R/N/B/Q"), 1, 1, NULL
, NULL
,
1493 str
, do_promotion_piece_finalize
, 0, 0, "%s", _("R = Rook, N = Knight, B = Bishop, Q = Queen"));
1497 do_validate_move(str
);
1501 static char *clock_to_char(long n
)
1503 static char buf
[16];
1504 int h
= 0, m
= 0, s
= 0;
1507 m
= (n
% 3600) / 60;
1508 s
= (n
% 3600) % 60;
1509 snprintf(buf
, sizeof(buf
), "%.2i:%.2i:%.2i", h
, m
, s
);
1513 static char *timeval_to_char(struct timeval t
, long limit
)
1516 int h
= 0, m
= 0, s
= 0;
1517 int n
= limit
? abs(limit
- t
.tv_sec
) : 0;
1520 m
= (n
% 3600) / 60;
1521 s
= (n
% 3600) % 60;
1522 snprintf(buf
, sizeof(buf
), "%.2i:%.2i:%.2i", h
, m
, s
);
1526 static char *time_control_status(struct clock_s
*clk
)
1528 static char buf
[80] = {0};
1532 if (clk
->tc
[clk
->tcn
][0] && clk
->tc
[clk
->tcn
+ 1][1])
1533 snprintf(buf
, sizeof(buf
), " M%.2i/%s", abs(clk
->tc
[clk
->tcn
][0] - clk
->move
),
1534 clock_to_char(clk
->tc
[clk
->tcn
+ 1][1]));
1535 else if (!clk
->incr
)
1540 strncat(buf
, " I", sizeof(buf
)-1);
1541 strncat(buf
, itoa(clk
->incr
, buf
), sizeof(buf
)-1);
1547 void update_status_window(GAME g
)
1551 char tmp
[15] = {0}, *engine
, *mode
;
1557 struct userdata_s
*d
= g
->data
;
1561 if (!curses_initialized
)
1564 getmaxyx(statusw
, maxy
, maxx
);
1571 wchar_t *loadfilew
= loadfile
[0] ? str_etc (loadfile
, w
, 1) : str_to_wchar (_ ("not available"));
1572 mvwprintw(statusw
, y
++, 1, "%*s %-*ls", 7, _("File:"), w
, loadfilew
);
1574 snprintf(buf
, len
, "%i %s %i", gindex
+ 1, _("of"), gtotal
);
1575 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Game:"), w
, buf
);
1580 if (config
.details
) {
1585 if (TEST_FLAG(d
->flags
, CF_DELETE
)) {
1593 if (TEST_FLAG(g
->flags
, GF_PERROR
)) {
1601 if (TEST_FLAG(d
->flags
, CF_MODIFIED
)) {
1609 pgn_config_get(PGN_STRICT_CASTLING
, &n
);
1619 if (TEST_FLAG(d
->flags
, CF_PERL
)) {
1629 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Flags:"), w
, (tmp
[0]) ? tmp
: "-");
1633 mode
= _("move history");
1642 mode
= _("(empty value)");
1646 snprintf(buf
, len
- 1, "%*s %s", 7, _("Mode:"), mode
);
1648 if (d
->mode
== MODE_PLAY
) {
1649 if (TEST_FLAG(d
->flags
, CF_HUMAN
))
1650 strncat(buf
, _(" (human/human)"), len
- 1);
1651 else if (TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
))
1652 strncat(buf
, _(" (engine/engine)"), len
- 1);
1654 strncat(buf
, (d
->play_mode
== PLAY_EH
) ?
1655 _(" (engine/human)") : _(" (human/engine)"), len
- 1);
1659 mvwprintw(statusw
, y
++, 1, "%-*s", len
, buf
);
1663 switch (d
->engine
->status
) {
1664 case ENGINE_THINKING
:
1665 engine
= _("pondering...");
1668 engine
= _("ready");
1670 case ENGINE_INITIALIZING
:
1671 engine
= _("initializing...");
1673 case ENGINE_OFFLINE
:
1674 engine
= _("offline");
1677 engine
= _("(empty value)");
1682 engine
= _("offline");
1684 mvwprintw(statusw
, y
, 1, "%*s %-*s", 7, _("Engine:"), w
, " ");
1685 wattron(statusw
, CP_STATUS_ENGINE
);
1686 mvwaddstr(statusw
, y
++, 9, engine
);
1687 wattroff(statusw
, CP_STATUS_ENGINE
);
1689 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Turn:"), w
,
1690 (g
->turn
== WHITE
) ? _("white") : _("black"));
1692 strncpy(tmp
, _("white"), sizeof(tmp
)-1);
1693 tmp
[0] = toupper(tmp
[0]);
1694 snprintf(t
, sizeof(t
), "%s%s",
1695 timeval_to_char(d
->wclock
.elapsed
, d
->wclock
.tc
[d
->wclock
.tcn
][1]),
1696 time_control_status(&d
->wclock
));
1697 mvwprintw(statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1699 strncpy(tmp
, _("black"), sizeof(tmp
)-1);
1700 tmp
[0] = toupper(tmp
[0]);
1701 snprintf(t
, sizeof(t
), "%s%s",
1702 timeval_to_char(d
->bclock
.elapsed
, d
->bclock
.tc
[d
->bclock
.tcn
][1]),
1703 time_control_status(&d
->bclock
));
1704 mvwprintw(statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1706 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Total:"), w
,
1707 clock_to_char(d
->elapsed
.tv_sec
));
1709 // for (i = 0; i < STATUS_WIDTH; i++)
1710 // mvwprintw(stdscr, STATUS_HEIGHT, i, " ");
1713 status
.notify
= str_to_wchar(_("Type F1 for help"));
1715 wattron(stdscr
, CP_STATUS_NOTIFY
);
1716 for (i
= (config
.boardleft
) ? BOARD_WIDTH
: 0;
1717 i
< ((config
.boardleft
) ? COLS
: STATUS_WIDTH
); i
++)
1718 mvwprintw(stdscr
, STATUS_HEIGHT
, i
, " ");
1719 mvwprintw(stdscr
, STATUS_HEIGHT
, CENTERX(STATUS_WIDTH
, status
.notify
)
1720 + ((config
.boardleft
) ? BOARD_WIDTH
: 0),
1721 "%ls", status
.notify
);
1722 wattroff(stdscr
, CP_STATUS_NOTIFY
);
1725 wchar_t *translate_tag_name(const char *tag
)
1727 if (!strcmp (tag
, "Event"))
1728 return str_to_wchar (translatable_tag_names
[0]);
1729 else if (!strcmp (tag
, "Site"))
1730 return str_to_wchar (translatable_tag_names
[1]);
1731 else if (!strcmp (tag
, "Date"))
1732 return str_to_wchar (translatable_tag_names
[2]);
1733 else if (!strcmp (tag
, "Round"))
1734 return str_to_wchar (translatable_tag_names
[3]);
1735 else if (!strcmp (tag
, "White"))
1736 return str_to_wchar (translatable_tag_names
[4]);
1737 else if (!strcmp (tag
, "Black"))
1738 return str_to_wchar (translatable_tag_names
[5]);
1739 else if (!strcmp (tag
, "Result"))
1740 return str_to_wchar (translatable_tag_names
[6]);
1742 return str_to_wchar (tag
);
1745 void update_tag_window(TAG
**t
)
1750 for (i
= 0; t
[i
]; i
++) {
1751 wchar_t *namewc
= translate_tag_name(t
[i
]->name
);
1759 w
= TAG_WIDTH
- namel
- 4;
1761 for (i
= 0; t
[i
] && i
< TAG_HEIGHT
- 3; i
++) {
1762 wchar_t *namewc
= translate_tag_name(t
[i
]->name
);
1763 wchar_t *valuewc
= str_etc(t
[i
]->value
, w
, 0);
1765 mvwprintw(tagw
, (i
+ 2), 1, "%*ls: %-*ls", namel
, namewc
, w
, valuewc
);
1770 for (; i
< TAG_HEIGHT
- 3; i
++)
1771 mvwprintw(tagw
, (i
+ 2), 1, "%*s", namel
+ w
+ 2, " ");
1774 void append_enginebuf(GAME g
, char *line
)
1777 struct userdata_s
*d
= g
->data
;
1779 if (d
->engine
->enginebuf
)
1780 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++);
1782 if (i
>= LINES
- 3) {
1783 free(d
->engine
->enginebuf
[0]);
1785 for (i
= 0; d
->engine
->enginebuf
[i
+1]; i
++)
1786 d
->engine
->enginebuf
[i
] = d
->engine
->enginebuf
[i
+1];
1788 d
->engine
->enginebuf
[i
] = strdup(line
);
1791 d
->engine
->enginebuf
= Realloc(d
->engine
->enginebuf
, (i
+ 2) * sizeof(char *));
1792 d
->engine
->enginebuf
[i
++] = strdup(line
);
1793 d
->engine
->enginebuf
[i
] = NULL
;
1797 void update_engine_window(GAME g
)
1800 struct userdata_s
*d
= g
->data
;
1802 wmove(enginew
, 0, 0);
1805 if (d
->engine
&& d
->engine
->enginebuf
) {
1806 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++)
1807 mvwprintw(enginew
, i
+ 2, 1, "%s", d
->engine
->enginebuf
[i
]);
1810 window_draw_title(enginew
, _("Engine IO Window"), COLS
, CP_MESSAGE_TITLE
,
1814 void update_all(GAME g
)
1816 struct userdata_s
*d
= g
->data
;
1819 * In the middle of a macro. Don't update the screen.
1821 if (macro_match
!= -1)
1824 wmove(boardw
, ROWTOMATRIX(d
->c_row
), COLTOMATRIX(d
->c_col
));
1825 update_board_window(g
);
1826 update_status_window(g
);
1827 update_history_window(g
);
1828 update_tag_window(g
->tag
);
1829 update_engine_window(g
);
1834 static void game_next_prev(GAME g
, int n
, int count
)
1840 if (gindex
+ count
> gtotal
- 1) {
1842 gindex
= gtotal
- 1;
1850 if (gindex
- count
< 0) {
1854 gindex
= gtotal
- 1;
1863 static void delete_game(int which
)
1868 struct userdata_s
*d
;
1870 for (i
= 0; i
< gtotal
; i
++) {
1873 if (i
== which
|| TEST_FLAG(d
->flags
, CF_DELETE
)) {
1874 free_userdata_once(game
[i
]);
1879 g
= Realloc(g
, (gi
+ 1) * sizeof(GAME
*));
1880 g
[gi
] = Calloc(1, sizeof(struct game_s
));
1881 memcpy(g
[gi
], game
[i
], sizeof(struct game_s
));
1882 g
[gi
]->tag
= game
[i
]->tag
;
1883 g
[gi
]->history
= game
[i
]->history
;
1884 g
[gi
]->hp
= game
[i
]->hp
;
1892 if (which
+ 1 >= gtotal
)
1893 gindex
= gtotal
- 1;
1898 gindex
= gtotal
- 1;
1901 gp
->hp
= gp
->history
;
1905 * FIXME find across multiple games.
1907 static int find_move_exp(GAME g
, regex_t r
, int which
, int count
)
1915 incr
= (which
== 0) ? -1 : 1;
1917 for (i
= g
->hindex
+ incr
- 1, found
= 0; ; i
+= incr
) {
1918 if (i
== g
->hindex
- 1)
1921 if (i
>= pgn_history_total(g
->hp
))
1924 i
= pgn_history_total(g
->hp
) - 1;
1927 ret
= regexec(&r
, g
->hp
[i
]->move
, 0, 0, 0);
1930 if (count
== ++found
) {
1935 if (ret
!= REG_NOMATCH
) {
1936 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
1937 cmessage(_("Error Matching Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
1946 static int toggle_delete_flag(int n
)
1949 struct userdata_s
*d
= game
[n
]->data
;
1951 TOGGLE_FLAG(d
->flags
, CF_DELETE
);
1954 for (i
= x
= 0; i
< gtotal
; i
++) {
1957 if (TEST_FLAG(d
->flags
, CF_DELETE
))
1962 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
1964 CLEAR_FLAG(d
->flags
, CF_DELETE
);
1971 static int find_game_exp(char *str
, int which
, int count
)
1973 char *nstr
= NULL
, *exp
= NULL
;
1977 char buf
[255] = {0}, *tmp
;
1980 int incr
= (which
== 0) ? -(1) : 1;
1982 strncpy(buf
, str
, sizeof(buf
)-1);
1985 if (strstr(tmp
, ":") != NULL
) {
1986 nstr
= strsep(&tmp
, ":");
1988 if ((ret
= regcomp(&nexp
, nstr
,
1989 REG_ICASE
|REG_EXTENDED
|REG_NOSUB
)) != 0) {
1990 regerror(ret
, &nexp
, errbuf
, sizeof(errbuf
));
1991 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
1999 while (*exp
&& isspace(*exp
))
2005 if ((ret
= regcomp(&vexp
, exp
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
2006 regerror(ret
, &vexp
, errbuf
, sizeof(errbuf
));
2007 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
2014 for (g
= gindex
+ incr
, found
= 0; ; g
+= incr
) {
2025 for (t
= 0; game
[g
]->tag
[t
]; t
++) {
2027 if (regexec(&nexp
, game
[g
]->tag
[t
]->name
, 0, 0, 0) == 0) {
2028 if (regexec(&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0) {
2029 if (count
== ++found
) {
2037 if (regexec(&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0) {
2038 if (count
== ++found
) {
2060 * Updates the notification line in the status window then refreshes the
2063 void update_status_notify(GAME g
, char *fmt
, ...)
2066 #ifdef HAVE_VASPRINTF
2073 if (status
.notify
) {
2074 free(status
.notify
);
2075 status
.notify
= NULL
;
2082 #ifdef HAVE_VASPRINTF
2083 vasprintf(&line
, fmt
, ap
);
2085 vsnprintf(line
, sizeof(line
), fmt
, ap
);
2090 free(status
.notify
);
2092 status
.notify
= str_to_wchar(line
);
2094 #ifdef HAVE_VASPRINTF
2099 int rav_next_prev(GAME g
, BOARD b
, int n
)
2103 if ((!g
->ravlevel
&& g
->hindex
&& g
->hp
[g
->hindex
- 1]->rav
== NULL
) ||
2104 (!g
->ravlevel
&& !g
->hindex
&& g
->hp
[g
->hindex
]->rav
== NULL
) ||
2105 (g
->ravlevel
&& g
->hp
[g
->hindex
]->rav
== NULL
))
2108 g
->rav
= Realloc(g
->rav
, (g
->ravlevel
+ 1) * sizeof(RAV
));
2109 g
->rav
[g
->ravlevel
].hp
= g
->hp
;
2110 g
->rav
[g
->ravlevel
].flags
= g
->flags
;
2111 g
->rav
[g
->ravlevel
].fen
= pgn_game_to_fen(g
, b
);
2112 g
->rav
[g
->ravlevel
].hindex
= g
->hindex
;
2113 g
->hp
= (!g
->ravlevel
) ? (g
->hindex
) ? g
->hp
[g
->hindex
- 1]->rav
: g
->hp
[g
->hindex
]->rav
: g
->hp
[g
->hindex
]->rav
;
2116 pgn_board_update(g
, b
, g
->hindex
+ 1);
2120 if (g
->ravlevel
- 1 < 0)
2125 pgn_board_init_fen(g
, b
, g
->rav
[g
->ravlevel
].fen
);
2126 free(g
->rav
[g
->ravlevel
].fen
);
2127 g
->hp
= g
->rav
[g
->ravlevel
].hp
;
2128 g
->flags
= g
->rav
[g
->ravlevel
].flags
;
2129 g
->hindex
= g
->rav
[g
->ravlevel
].hindex
;
2133 static void draw_window_decor()
2135 move_panel(boardp
, 0,
2136 (config
.boardleft
) ? 0 : COLS
- BOARD_WIDTH
);
2137 move_panel(historyp
, LINES
- HISTORY_HEIGHT
,
2138 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
: 0 :
2139 (MEGA_BOARD
) ? 0 : COLS
- HISTORY_WIDTH
);
2140 move_panel(statusp
, 0,
2141 (config
.boardleft
) ? BOARD_WIDTH
: 0);
2142 move_panel(tagp
, STATUS_HEIGHT
+ 1,
2143 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
:
2146 wbkgd(boardw
, CP_BOARD_WINDOW
);
2147 wbkgd(statusw
, CP_STATUS_WINDOW
);
2148 window_draw_title(statusw
, _("Game Status"), STATUS_WIDTH
,
2149 CP_STATUS_TITLE
, CP_STATUS_BORDER
);
2150 wbkgd(tagw
, CP_TAG_WINDOW
);
2151 window_draw_title(tagw
, _("Roster Tags"), TAG_WIDTH
, CP_TAG_TITLE
,
2153 wbkgd(historyw
, CP_HISTORY_WINDOW
);
2154 window_draw_title(historyw
, _("Move History"), HISTORY_WIDTH
,
2155 CP_HISTORY_TITLE
, CP_HISTORY_BORDER
);
2158 static void do_window_resize()
2160 if (LINES
< 23 || COLS
< 74)
2163 resizeterm(LINES
, COLS
);
2165 wresize(boardw
, BOARD_HEIGHT
, BOARD_WIDTH
);
2166 wresize(historyw
, HISTORY_HEIGHT
, HISTORY_WIDTH
);
2167 wresize(statusw
, STATUS_HEIGHT
, STATUS_WIDTH
);
2168 wresize(tagw
, TAG_HEIGHT
, TAG_WIDTH
);
2176 draw_window_decor();
2178 keypad(boardw
, TRUE
);
2186 memset(&clock_timer
, 0, sizeof(struct itimerval
));
2187 setitimer(ITIMER_REAL
, &clock_timer
, NULL
);
2190 void start_clock(GAME g
)
2192 struct userdata_s
*d
= g
->data
;
2194 if (clock_timer
.it_interval
.tv_usec
)
2197 memset(&d
->elapsed
, 0, sizeof(struct timeval
));
2198 clock_timer
.it_value
.tv_sec
= 0;
2199 clock_timer
.it_value
.tv_usec
= 100000;
2200 clock_timer
.it_interval
.tv_sec
= 0;
2201 clock_timer
.it_interval
.tv_usec
= 100000;
2202 setitimer(ITIMER_REAL
, &clock_timer
, NULL
);
2205 static void update_clocks()
2208 struct userdata_s
*d
;
2209 struct itimerval it
;
2212 getitimer(ITIMER_REAL
, &it
);
2214 for (i
= 0; i
< gtotal
; i
++) {
2217 if (d
&& d
->mode
== MODE_PLAY
) {
2218 if (d
->paused
== 1 || TEST_FLAG(d
->flags
, CF_NEW
))
2220 else if (d
->paused
== -1) {
2221 if (game
[i
]->side
== game
[i
]->turn
) {
2227 update_clock(game
[i
], it
);
2235 update_status_window(gp
);
2241 #define SKIP_SPACE(str) { while (isspace(*str)) str++; }
2243 static int parse_clock_time(char **str
)
2298 static int parse_clock_input(struct clock_s
*clk
, char *str
, int *incr
)
2320 memset(clk
, 0, sizeof(struct clock_s
));
2324 if (strncasecmp(p
, "SD", 2) == 0) {
2330 n
= parse_clock_time(&p
);
2344 /* Sudden death without a previous time control. */
2350 n
= parse_clock_time(&p
);
2356 message(ERROR_STR
, ANY_KEY_STR
, "%s (%i)", _("Maximum number of time controls reached"), MAX_TC
);
2361 clk
->tc
[tc
++][1] = n
;
2376 clk
->tc
[clk
->tcn
][1] = (n
<= clk
->elapsed
.tv_sec
) ? clk
->elapsed
.tv_sec
+ n
: n
;
2384 n
= parse_clock_time(&p
);
2404 static int parse_which_clock(struct clock_s
*clk
, char *str
)
2409 memcpy(&tmp
, clk
, sizeof(struct clock_s
));
2411 if (parse_clock_input(&tmp
, str
, &incr
)) {
2412 cmessage(ERROR_STR
, ANY_KEY_STR
, _("Invalid clock specification"));
2416 memcpy(clk
, &tmp
, sizeof(struct clock_s
));
2417 clk
->tc
[clk
->tcn
][1] += incr
;
2421 void do_clock_input_finalize(WIN
*win
)
2423 struct userdata_s
*d
= gp
->data
;
2424 struct input_data_s
*in
= win
->data
;
2434 if (tolower(*p
) == 'w') {
2437 if (parse_which_clock(&d
->wclock
, p
))
2440 else if (tolower(*p
) == 'b') {
2443 if (parse_which_clock(&d
->bclock
, p
))
2447 if (parse_which_clock(&d
->wclock
, p
))
2450 if (parse_which_clock(&d
->bclock
, p
))
2454 if (!d
->wclock
.tc
[0][1] && !d
->bclock
.tc
[0][1])
2455 CLEAR_FLAG(d
->flags
, CF_CLOCK
);
2457 SET_FLAG(d
->flags
, CF_CLOCK
);
2464 void do_engine_command_finalize(WIN
*win
)
2466 struct userdata_s
*d
= gp
->data
;
2467 struct input_data_s
*in
= win
->data
;
2478 x
= d
->engine
->status
;
2479 send_to_engine(gp
, -1, "%s\n", in
->str
);
2480 d
->engine
->status
= x
;
2487 void do_board_details()
2489 config
.details
= (config
.details
) ? 0 : 1;
2492 void do_toggle_strict_castling()
2496 pgn_config_get(PGN_STRICT_CASTLING
, &n
);
2499 pgn_config_set(PGN_STRICT_CASTLING
, 1);
2501 pgn_config_set(PGN_STRICT_CASTLING
, 0);
2504 void do_play_set_clock()
2506 struct input_data_s
*in
;
2508 in
= Calloc(1, sizeof(struct input_data_s
));
2509 in
->efunc
= do_clock_input_finalize
;
2510 construct_input(_("Set Clock"), NULL
, 1, 1,
2511 _ ("Format: [W | B] [+]T[+I] | ++I | M/T [M/T [...] [SD/T]] [+I]\n" \
2512 "T = time (hms), I = increment, M = moves per, SD = sudden death\ne.g., 30m or 4m+12s or 35/90m SD/30m"),
2513 NULL
, NULL
, 0, in
, INPUT_HIST_CLOCK
, -1);
2516 void do_play_toggle_human()
2518 struct userdata_s
*d
= gp
->data
;
2520 TOGGLE_FLAG(d
->flags
, CF_HUMAN
);
2522 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && pgn_history_total(gp
->hp
)) {
2523 if (init_chess_engine(gp
))
2527 CLEAR_FLAG(d
->flags
, CF_ENGINE_LOOP
);
2530 d
->engine
->status
= ENGINE_READY
;
2533 void do_play_toggle_engine()
2535 struct userdata_s
*d
= gp
->data
;
2537 TOGGLE_FLAG(d
->flags
, CF_ENGINE_LOOP
);
2538 CLEAR_FLAG(d
->flags
, CF_HUMAN
);
2540 if (d
->engine
&& TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
)) {
2541 char *fen
= pgn_game_to_fen (gp
, d
->b
);
2543 pgn_board_update(gp
, d
->b
,
2544 pgn_history_total(gp
->hp
));
2545 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
2551 * This will send a command to the engine skipping the command queue.
2553 void do_play_send_command()
2555 struct userdata_s
*d
= gp
->data
;
2556 struct input_data_s
*in
;
2558 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
) {
2559 if (init_chess_engine(gp
))
2563 in
= Calloc(1, sizeof(struct input_data_s
));
2564 in
->efunc
= do_engine_command_finalize
;
2565 construct_input(_("Engine Command"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
, INPUT_HIST_ENGINE
, -1);
2568 void do_play_switch_turn()
2570 struct userdata_s *d = gp->data;
2572 pgn_switch_side(gp);
2573 pgn_switch_turn(gp);
2575 if (!TEST_FLAG(d->flags, CF_HUMAN))
2576 add_engine_command(gp, -1,
2577 (gp->side == WHITE) ? "white\n" : "black\n");
2579 update_status_window(gp);
2582 void do_play_toggle_eh_mode()
2584 struct userdata_s
*d
= gp
->data
;
2586 if (!TEST_FLAG(d
->flags
, CF_HUMAN
)) {
2588 pgn_switch_side(gp
, TRUE
);
2589 d
->play_mode
= (d
->play_mode
) ? PLAY_HE
: PLAY_EH
;
2590 if (gp
->side
== BLACK
)
2591 update_status_notify(gp
, _("Press 'g' to start the game"));
2593 rotate
= (rotate
) ? FALSE
: TRUE
;
2596 message(NULL
, ANY_KEY_STR
,
2597 _("You may only switch sides at the start of the \n"
2598 "game. Press ^K or ^N to begin a new game."));
2604 struct userdata_s
*d
= gp
->data
;
2606 if (!pgn_history_total(gp
->hp
))
2610 if (gp
->hindex
- keycount
< 0)
2614 gp
->hindex
-= (keycount
* 2) -1;
2616 gp
->hindex
-= keycount
* 2;
2620 if (gp
->hindex
- 2 < 0)
2630 pgn_history_free(gp
->hp
, gp
->hindex
);
2631 gp
->hindex
= pgn_history_total(gp
->hp
);
2632 pgn_board_update(gp
, d
->b
, gp
->hindex
);
2634 if (d
->engine
&& d
->engine
->status
== ENGINE_READY
) {
2635 char *fen
= pgn_game_to_fen(gp
, d
->b
);
2637 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
2639 d
->engine
->status
= ENGINE_READY
;
2642 update_history_window(gp
);
2645 pgn_switch_side(gp
, FALSE
);
2650 void do_play_toggle_pause()
2652 struct userdata_s
*d
= gp
->data
;
2654 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && gp
->turn
!=
2660 d
->paused
= (d
->paused
) ? 0 : 1;
2665 struct userdata_s
*d
= gp
->data
;
2667 if (TEST_FLAG(d
->flags
, CF_HUMAN
))
2670 if (fm_loaded_file
&& gp
->side
!= gp
->turn
) {
2671 pgn_switch_side(gp
, FALSE
);
2672 add_engine_command(gp
, ENGINE_THINKING
, "black\n");
2675 add_engine_command(gp
, ENGINE_THINKING
, "go\n");
2677 // Completa la función para que permita seguir jugando al usarla.
2678 // Complete the function to allow continue playing when using.
2679 if (gp
->side
== gp
->turn
)
2680 pgn_switch_side(gp
, FALSE
);
2685 void do_play_config_command()
2690 for (x
= 0; config
.keys
[x
]; x
++) {
2691 if (config
.keys
[x
]->c
== input_c
) {
2692 switch (config
.keys
[x
]->type
) {
2694 add_engine_command(gp
, -1, "%ls\n",
2695 config
.keys
[x
]->str
);
2701 add_engine_command(gp
, -1,
2702 "%ls %i\n", config
.keys
[x
]->str
, keycount
);
2709 for (w
= 0; w
< keycount
; w
++)
2710 add_engine_command(gp
, -1,
2711 "%ls\n", config
.keys
[x
]->str
);
2719 update_status_notify(gp
, NULL
);
2722 void do_play_cancel_selected()
2724 struct userdata_s
*d
= gp
->data
;
2726 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
2728 pgn_reset_valid_moves(d
->b
);
2729 update_status_notify(gp
, NULL
);
2732 void do_play_commit()
2734 struct userdata_s
*d
= gp
->data
;
2736 pushkey
= keycount
= 0;
2737 update_status_notify(gp
, NULL
);
2739 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) &&
2740 (!d
->engine
|| d
->engine
->status
== ENGINE_THINKING
))
2746 d
->sp
.row
= d
->c_row
;
2747 d
->sp
.col
= d
->c_col
;
2750 rotate_position(SP_POSITION
);
2751 rotate_position(SPS_POSITION
);
2756 // Completa la función para que permita seguir jugando cuando se carga un
2757 // archivo pgn (con juego no terminado) que inicie con turno del lado
2759 // Complete the function to allow continue playing when loading a file
2760 // pgn (with unfinished game) you start to turn black side.
2761 if (gp
->side
!= gp
->turn
)
2762 pgn_switch_side(gp
, FALSE
);
2764 if (rotate
&& d
->sp
.icon
)
2765 rotate_position(SPS_POSITION
);
2767 // Envia comando 'go' a Polyglot en el primer movimiento debido a que
2768 // polyglot no envia el movimiento y cboard se queda esperando.
2769 // Send command 'go' to the first movement Polyglot because cboard
2770 // waits to send polyglot movement and this does not make.
2771 if (config
.fmpolyglot
&&
2772 ((gp
->side
== WHITE
&& !gp
->hindex
) || fm_loaded_file
))
2773 add_engine_command(gp
, ENGINE_THINKING
, "go\n");
2776 fm_loaded_file
= FALSE
;
2779 void do_play_select()
2781 struct userdata_s
*d
= gp
->data
;
2783 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && (!d
->engine
||
2784 d
->engine
->status
== ENGINE_OFFLINE
)) {
2785 if (init_chess_engine(gp
))
2789 if (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
)
2793 do_play_cancel_selected ();
2796 rotate_position(CURSOR_POSITION
);
2798 d
->sp
.icon
= d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
;
2800 if (pgn_piece_to_int(d
->sp
.icon
) == OPEN_SQUARE
) {
2805 if (((islower(d
->sp
.icon
) && gp
->turn
!= BLACK
)
2806 || (isupper(d
->sp
.icon
) && gp
->turn
!= WHITE
))) {
2808 char *str
= Malloc (512);
2810 for (k
= play_keys
; *k
; k
++) {
2811 if ((*k
)->f
== do_play_toggle_eh_mode
)
2815 snprintf (str
, 512, _("It is not your turn to move. You may switch playing sides by pressing \"%lc\"."),
2816 *k
? (*k
)->c
: '?');
2817 message(NULL
, ANY_KEY_STR
, "%s", str
);
2822 if (pgn_history_total(gp
->hp
)) {
2823 message(NULL
, ANY_KEY_STR
, "%s", _("It is not your turn to move. You can switch sides "));
2828 if (pgn_tag_find(gp
->tag
, "FEN") != E_PGN_ERR
)
2831 add_engine_command(gp
, ENGINE_READY
, "black\n");
2832 pgn_switch_turn(gp
);
2834 if (gp
->side
!= BLACK
)
2835 pgn_switch_side(gp
);
2840 d
->sp
.srow
= d
->c_row
;
2841 d
->sp
.scol
= d
->c_col
;
2843 if (config
.validmoves
)
2844 pgn_find_valid_moves(gp
, d
->b
, d
->sp
.scol
, d
->sp
.srow
);
2847 rotate_position(CURSOR_POSITION
);
2848 rotate_position(SPS_POSITION
);
2851 CLEAR_FLAG(d
->flags
, CF_NEW
);
2855 /* FIXME: keys with the same function should comma deliminated. */
2856 static wchar_t *build_help(struct key_s
**keys
)
2858 int i
, nlen
= 1, len
, t
, n
;
2859 wchar_t *buf
= NULL
, *wc
= NULL
;
2865 for (i
= len
= t
= 0; keys
[i
]; i
++) {
2870 if (wcslen(keys
[i
]->key
) > nlen
) {
2871 nlen
= wcslen(keys
[i
]->key
);
2879 if (wcslen(keys
[i
]->d
) > len
)
2880 len
= wcslen(keys
[i
]->d
);
2888 buf
= Malloc(t
*sizeof(wchar_t));
2891 for (i
= 0; keys
[i
]; i
++) {
2896 n
= wcslen(keys
[i
]->key
);
2906 wcscat(buf
, keys
[i
]->key
);
2907 p
= buf
+ wcslen(buf
);
2918 wcscat(buf
, keys
[i
]->d
);
2921 wc
= str_to_wchar ("*");
2926 wc
= str_to_wchar ("\n");
2929 p
= buf
+ wcslen(buf
);
2935 void do_more_help(WIN
*);
2936 void do_main_help(WIN
*win
)
2942 buf
= build_help(play_keys
);
2943 construct_message(_("Play Mode Keys (* = can take a repeat count)"), ANY_KEY_STR
, 0, 0,
2944 NULL
, NULL
, buf
, do_more_help
, 0, 1, "%ls", buf
);
2947 buf
= build_help(history_keys
);
2948 construct_message(_("History Mode Keys (* = can take a repeat count)"), ANY_KEY_STR
, 0, 0,
2949 NULL
, NULL
, buf
, do_more_help
, 0, 1, "%ls", buf
);
2952 buf
= build_help(edit_keys
);
2953 construct_message(_("Edit Mode Keys (* = can take a repeat count)"), ANY_KEY_STR
, 0, 0,
2954 NULL
, NULL
, buf
, do_more_help
, 0, 1, "%ls", buf
);
2957 buf
= build_help(global_keys
);
2958 construct_message(_("Global Game Keys (* = can take a repeat count)"), ANY_KEY_STR
, 0, 0,
2959 NULL
, NULL
, buf
, do_more_help
, 0, 1, "%ls", buf
);
2966 void do_more_help(WIN
*win
)
2968 if (win
->c
== KEY_F(1) || win
->c
== CTRL_KEY('g'))
2969 construct_message(_("Command Key Index"),
2970 _ ("p/h/e/g or any other key to quit"), 0, 0,
2971 NULL
, NULL
, NULL
, do_main_help
, 0, 0, "%s",
2973 "p - play mode keys\n"
2974 "h - history mode keys\n"
2975 "e - board edit mode keys\n"
2976 "g - global game keys"
2982 wchar_t *buf
= build_help(play_keys
);
2984 construct_message(_("Play Mode Keys (* = can take a repeat count)"),
2985 ANY_KEY_STR
, 0, 0, NULL
, NULL
,
2986 buf
, do_more_help
, 0, 1, "%ls", buf
);
2989 void do_play_history_mode()
2991 struct userdata_s
*d
= gp
->data
;
2993 if (!pgn_history_total(gp
->hp
) ||
2994 (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
))
2997 d
->mode
= MODE_HISTORY
;
2998 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3001 void do_play_edit_mode()
3003 struct userdata_s
*d
= gp
->data
;
3005 if (pgn_history_total(gp
->hp
))
3008 pgn_board_init_fen(gp
, d
->b
, NULL
);
3010 d
->mode
= MODE_EDIT
;
3013 void do_edit_insert_finalize(WIN
*win
)
3015 struct userdata_s
*d
= win
->data
;
3017 if (pgn_piece_to_int(win
->c
) == -1)
3020 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
= win
->c
;
3023 void do_edit_select()
3025 struct userdata_s
*d
= gp
->data
;
3030 d
->sp
.icon
= d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
;
3032 if (pgn_piece_to_int(d
->sp
.icon
) == OPEN_SQUARE
) {
3037 d
->sp
.srow
= d
->c_row
;
3038 d
->sp
.scol
= d
->c_col
;
3041 void do_edit_commit()
3044 struct userdata_s
*d
= gp
->data
;
3046 pushkey
= keycount
= 0;
3047 update_status_notify(gp
, NULL
);
3052 d
->sp
.row
= d
->c_row
;
3053 d
->sp
.col
= d
->c_col
;
3054 p
= d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
;
3055 d
->b
[RANKTOBOARD(d
->sp
.row
)][FILETOBOARD(d
->sp
.col
)].icon
= p
;
3056 d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
=
3057 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3058 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3061 void do_edit_delete()
3063 struct userdata_s
*d
= gp
->data
;
3066 d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
=
3067 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3069 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
=
3070 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3072 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3075 void do_edit_cancel_selected()
3077 struct userdata_s
*d
= gp
->data
;
3079 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3081 update_status_notify(gp
, NULL
);
3084 void do_edit_switch_turn()
3086 pgn_switch_turn(gp
);
3089 void do_edit_toggle_castle()
3091 struct userdata_s
*d
= gp
->data
;
3093 castling_state(gp
, d
->b
, RANKTOBOARD(d
->c_row
),
3094 FILETOBOARD(d
->c_col
),
3095 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
, 1);
3098 void do_edit_insert()
3100 struct userdata_s
*d
= gp
->data
;
3102 construct_message(_("Insert Piece"), _("P=pawn, R=rook, N=knight, B=bishop, "), 0, 0, NULL
, NULL
,
3103 d
->b
, do_edit_insert_finalize
, 0, 0, "%s", _("Type the piece letter to insert. Lowercase "));
3106 void do_edit_enpassant()
3108 struct userdata_s
*d
= gp
->data
;
3110 if (d
->c_row
== 6 || d
->c_row
== 3) {
3111 pgn_reset_enpassant(d
->b
);
3112 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].enpassant
= 1;
3118 wchar_t *buf
= build_help(edit_keys
);
3120 construct_message(_("Edit Mode Keys (* = can take a repeat count)"),
3121 ANY_KEY_STR
, 0, 0, NULL
, NULL
,
3122 buf
, do_more_help
, 0, 1, "%ls", buf
);
3127 struct userdata_s
*d
= gp
->data
;
3128 char *fen
= pgn_game_to_fen(gp
, d
->b
);
3131 pgn_tag_add(&gp
->tag
, "FEN", fen
);
3133 pgn_tag_add(&gp
->tag
, "SetUp", "1");
3134 pgn_tag_sort(gp
->tag
);
3135 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3136 d
->mode
= MODE_PLAY
;
3139 void really_do_annotate_finalize(struct input_data_s
*in
,
3140 struct userdata_s
*d
)
3142 HISTORY
*h
= in
->data
;
3152 len
= strlen(in
->str
);
3153 h
->comment
= Realloc(h
->comment
, len
+1);
3154 strncpy(h
->comment
, in
->str
, len
);
3155 h
->comment
[len
] = 0;
3160 SET_FLAG(d
->flags
, CF_MODIFIED
);
3163 void do_annotate_finalize(WIN
*win
)
3165 struct userdata_s
*d
= gp
->data
;
3166 struct input_data_s
*in
= win
->data
;
3168 really_do_annotate_finalize(in
, d
);
3171 void do_find_move_exp_finalize(int init
, int which
)
3174 struct userdata_s
*d
= gp
->data
;
3175 static int firstrun
;
3180 if (init
|| !firstrun
) {
3184 if ((ret
= regcomp(&r
, moveexp
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
3185 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
3186 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
3193 if ((n
= find_move_exp(gp
, r
,
3194 (which
== -1) ? 0 : 1, (keycount
) ? keycount
: 1)) == -1)
3198 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3201 void do_find_move_exp(WIN
*win
)
3203 struct input_data_s
*in
= win
->data
;
3208 strncpy(moveexp
, in
->str
, sizeof(moveexp
)-1);
3209 moveexp
[sizeof(moveexp
)-1] = 0;
3210 do_find_move_exp_finalize(1, which
);
3218 void do_move_jump_finalize(int n
)
3220 struct userdata_s
*d
= gp
->data
;
3222 if (n
< 0 || n
> (pgn_history_total(gp
->hp
) / 2))
3226 update_status_notify(gp
, NULL
);
3227 gp
->hindex
= (n
) ? n
* 2 - 1 : n
* 2;
3228 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3231 void do_move_jump(WIN
*win
)
3233 struct input_data_s
*in
= win
->data
;
3235 if (!in
->str
|| !isinteger(in
->str
)) {
3243 do_move_jump_finalize(atoi(in
->str
));
3248 struct history_menu_s
{
3256 void free_history_menu_data(struct history_menu_s
**h
)
3263 for (i
= 0; h
[i
]; i
++) {
3271 void get_history_data(HISTORY
**hp
, struct history_menu_s
***menu
, int m
,
3275 int t
= pgn_history_total(hp
);
3276 char buf
[MAX_SAN_MOVE_LEN
+ 4];
3278 struct history_menu_s
**hmenu
= *menu
;
3281 for (n
= 0; hmenu
[n
]; n
++);
3285 for (i
= 0; i
< t
; i
++) {
3286 hmenu
= Realloc(hmenu
, (n
+ 2) * sizeof(struct history_menu_s
*));
3287 hmenu
[n
] = Malloc(sizeof(struct history_menu_s
));
3288 snprintf(buf
, sizeof(buf
), "%c%s%s", (turn
== WHITE
) ? 'W' : 'B',
3289 hp
[i
]->move
, (hp
[i
]->comment
|| hp
[i
]->nag
[0]) ? " !" : "");
3290 hmenu
[n
]->line
= strdup(buf
);
3291 hmenu
[n
]->hindex
= i
;
3292 hmenu
[n
]->indent
= 0;
3293 hmenu
[n
]->ravlevel
= depth
;
3294 hmenu
[n
]->move
= (n
&& depth
> hmenu
[n
-1]->ravlevel
) ? m
++ : m
;
3301 get_history_data(hp
[i
]->rav
, &hmenu
, m
, turn
);
3302 for (n
= 0; hmenu
[n
]; n
++);
3310 turn
= (turn
== WHITE
) ? BLACK
: WHITE
;
3316 void history_draw_update(struct menu_input_s
*m
)
3319 struct userdata_s
*d
= g
->data
;
3321 g
->hindex
= m
->selected
+ 1;
3322 update_cursor(g
, m
->selected
);
3323 pgn_board_update(g
, d
->b
, m
->selected
+ 1);
3326 struct menu_item_s
**get_history_items(WIN
*win
)
3328 struct menu_input_s
*m
= win
->data
;
3330 struct userdata_s
*d
= g
->data
;
3331 struct history_menu_s
**hm
= d
->data
;
3332 struct menu_item_s
**items
= m
->items
;
3336 get_history_data(g
->history
, &hm
, 0,
3337 TEST_FLAG(g
->flags
, GF_BLACK_OPENING
));
3338 m
->selected
= g
->hindex
- 1;
3340 if (m
->selected
< 0)
3343 m
->draw_exit_func
= history_draw_update
;
3349 for (i
= 0; items
[i
]; i
++)
3356 for (i
= 0; hm
[i
]; i
++) {
3357 items
= Realloc(items
, (i
+2) * sizeof(struct menu_item_s
*));
3358 items
[i
] = Malloc(sizeof(struct menu_item_s
));
3359 items
[i
]->name
= hm
[i
]->line
;
3360 items
[i
]->value
= NULL
;
3361 items
[i
]->selected
= 0;
3372 void history_menu_quit(struct menu_input_s
*m
)
3377 void history_menu_exit(WIN
*win
)
3380 struct userdata_s
*d
= g
->data
;
3381 struct history_menu_s
**hm
= d
->data
;
3387 for (i
= 0; hm
[i
]; i
++) {
3397 void history_menu_next(struct menu_input_s
*m
)
3400 struct userdata_s
*d
= g
->data
;
3401 struct history_menu_s
**hm
= d
->data
;
3404 for (t
= 0; hm
[t
]; t
++);
3406 if (m
->selected
+ 1 == t
)
3409 n
= hm
[m
->selected
+ 1]->hindex
;
3416 void history_menu_prev(struct menu_input_s
*m
)
3419 struct userdata_s
*d
= g
->data
;
3420 struct history_menu_s
**hm
= d
->data
;
3423 for (t
= 0; hm
[t
]; t
++);
3425 if (m
->selected
- 1 < 0)
3428 n
= hm
[m
->selected
- 1]->hindex
;
3434 void history_menu_help(struct menu_input_s
*m
)
3436 message(_("History Menu Help"), ANY_KEY_STR
, "%s",
3438 " UP/DOWN - previous/next menu item\n"
3439 " HOME/END - first/last menu item\n"
3440 " PGDN/PGUP - next/previous page\n"
3441 " a-zA-Z0-9 - jump to item\n"
3442 " CTRL-a - annotate the selected move\n"
3443 " ENTER - view annotation\n"
3444 " CTRL-d - toggle board details\n"
3445 " ESCAPE/M - return to move history"
3449 void do_annotate_move(HISTORY
*hp
)
3452 struct input_data_s
*in
;
3454 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Editing Annotation for"), hp
->move
);
3455 in
= Calloc(1, sizeof(struct input_data_s
));
3457 in
->efunc
= do_annotate_finalize
;
3458 construct_input(buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
3459 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
, CTRL_KEY('T'), in
, -1, -1);
3462 void history_menu_view_annotation(struct menu_input_s
*m
)
3467 view_annotation(g
->history
[m
->selected
]);
3470 void history_menu_annotate_finalize(WIN
*win
)
3472 struct input_data_s
*in
= win
->data
;
3473 GAME g
= in
->moredata
;
3474 struct userdata_s
*d
= g
->data
;
3475 struct history_menu_s
**hm
= d
->data
;
3477 really_do_annotate_finalize(in
, d
);
3478 free_history_menu_data(hm
);
3480 get_history_data(g
->history
, &hm
, 0, TEST_FLAG(g
->flags
, GF_BLACK_OPENING
));
3482 pushkey
= REFRESH_MENU
;
3485 void history_menu_annotate(struct menu_input_s
*m
)
3489 struct input_data_s
*in
;
3490 HISTORY
*hp
= g
->history
[m
->selected
]; // FIXME RAV
3492 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Editing Annotation for"), hp
->move
);
3493 in
= Calloc(1, sizeof(struct input_data_s
));
3495 in
->moredata
= m
->data
;
3496 in
->efunc
= history_menu_annotate_finalize
;
3497 construct_input(buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
3498 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
, CTRL_KEY('T'), in
, -1, -1);
3501 void history_menu_details(struct menu_input_s
*m
)
3507 void history_menu_print(WIN
*win
)
3509 struct menu_input_s
*m
= win
->data
;
3511 struct userdata_s
*d
= g
->data
;
3512 struct history_menu_s
**hm
= d
->data
;
3513 struct history_menu_s
*h
= hm
[m
->top
];
3515 char *p
= m
->item
->name
;
3516 int line
= m
->print_line
- 2;
3518 * Solaris 5.9 doesn't have wattr_get() or any function that requires an
3525 for (total
= 0; hm
[total
]; total
++);
3526 wattr_get(win
->w
, &attrs
, &pair
, NULL
);
3527 wattroff(win
->w
, COLOR_PAIR(pair
));
3528 mvwaddch(win
->w
, m
->print_line
, 1,
3529 *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
));
3532 if (h
->hindex
== 0 && line
== 0)
3533 waddch(win
->w
, ACS_ULCORNER
| CP_HISTORY_MENU_LG
);
3534 else if ((!hm
[h
->hindex
+ (win
->rows
- 5) + 1] && line
== win
->rows
- 5) ||
3535 (m
->top
+ line
== total
- 1))
3536 waddch(win
->w
, ACS_LLCORNER
| CP_HISTORY_MENU_LG
);
3537 else if (hm
[m
->top
+ 1]->ravlevel
!= h
->ravlevel
|| !h
->ravlevel
)
3538 waddch(win
->w
, ACS_LTEE
| CP_HISTORY_MENU_LG
);
3540 waddch(win
->w
, ACS_VLINE
| CP_HISTORY_MENU_LG
);
3542 wattron(win
->w
, COLOR_PAIR(pair
) | attrs
);
3544 for (i
= 2; *p
; p
++, i
++)
3545 waddch(win
->w
, (*p
== '!') ? *p
| A_BOLD
: *p
);
3547 while (i
++ < win
->cols
- 2)
3548 waddch(win
->w
, ' ');
3551 void history_menu(GAME g
)
3553 struct menu_key_s
**keys
= NULL
;
3555 add_menu_key(&keys
, KEY_ESCAPE
, history_menu_quit
);
3556 add_menu_key(&keys
, 'M', history_menu_quit
);
3557 add_menu_key(&keys
, KEY_UP
, history_menu_prev
);
3558 add_menu_key(&keys
, KEY_DOWN
, history_menu_next
);
3559 add_menu_key(&keys
, KEY_F(1), history_menu_help
);
3560 add_menu_key(&keys
, CTRL_KEY('a'), history_menu_annotate
);
3561 add_menu_key(&keys
, CTRL_KEY('d'), history_menu_details
);
3562 add_menu_key(&keys
, '\n', history_menu_view_annotation
);
3563 construct_menu(MEGA_BOARD
? LINES
- HISTORY_HEIGHT_MB
: LINES
,
3564 TAG_WIDTH
, 0, config
.boardleft
? BOARD_WIDTH
: 0,
3565 _("Move History Tree"), 1, get_history_items
, keys
, g
,
3566 history_menu_print
, history_menu_exit
);
3569 void do_history_menu()
3574 void do_history_half_move_toggle()
3576 movestep
= (movestep
== 1) ? 2 : 1;
3577 update_history_window(gp
);
3580 void do_history_rotate_board()
3582 rotate
= (rotate
) ? FALSE
: TRUE
;
3585 void do_history_jump_next()
3587 struct userdata_s
*d
= gp
->data
;
3589 pgn_history_next(gp
, d
->b
, (keycount
> 0) ?
3590 config
.jumpcount
* keycount
* movestep
:
3591 config
.jumpcount
* movestep
);
3594 void do_history_jump_prev()
3596 struct userdata_s
*d
= gp
->data
;
3598 pgn_history_prev(gp
, d
->b
, (keycount
) ?
3599 config
.jumpcount
* keycount
* movestep
:
3600 config
.jumpcount
* movestep
);
3603 void do_history_prev()
3605 struct userdata_s
*d
= gp
->data
;
3607 pgn_history_prev(gp
, d
->b
,
3608 (keycount
) ? keycount
* movestep
: movestep
);
3611 void do_history_next()
3613 struct userdata_s
*d
= gp
->data
;
3615 pgn_history_next(gp
, d
->b
, (keycount
) ?
3616 keycount
* movestep
: movestep
);
3619 void do_history_mode_finalize(struct userdata_s
*d
)
3622 d
->mode
= MODE_PLAY
;
3625 void do_history_mode_confirm(WIN
*win
)
3627 struct userdata_s
*d
= gp
->data
;
3628 wchar_t str
[] = { win
->c
, 0 };
3630 if (!wcscmp (str
, resume_wchar
)) {
3631 pgn_history_free(gp
->hp
, gp
->hindex
);
3632 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3637 if (pgn_history_rav_new(gp
, d
->b
,
3638 gp
->hindex
) != E_PGN_OK
)
3646 if (!TEST_FLAG(d
->flags
, CF_HUMAN
)) {
3647 char *fen
= pgn_game_to_fen(gp
, d
->b
);
3649 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
3653 do_history_mode_finalize(d
);
3656 void do_history_toggle()
3658 struct userdata_s
*d
= gp
->data
;
3660 // FIXME Resuming from previous history could append to a RAV.
3661 if (gp
->hindex
!= pgn_history_total(gp
->hp
)) {
3663 construct_message(NULL
, _ ("What would you like to do?"), 0, 1,
3664 NULL
, NULL
, NULL
, do_history_mode_confirm
, 0, 0,
3665 _("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."),
3670 if (TEST_FLAG(gp
->flags
, GF_GAMEOVER
))
3674 if (gp
->side
!= gp
->turn
) {
3675 d
->play_mode
= PLAY_EH
;
3678 d
->play_mode
= PLAY_HE
;
3682 do_history_mode_finalize(d
);
3685 void do_history_annotate()
3689 if (n
&& gp
->hp
[n
- 1]->move
)
3694 do_annotate_move(gp
->hp
[n
]);
3697 void do_history_help()
3699 wchar_t *buf
= build_help(history_keys
);
3701 construct_message(_("History Mode Keys (* = can take a repeat count)"),
3702 ANY_KEY_STR
, 0, 0, NULL
, NULL
,
3703 buf
, do_more_help
, 0, 1, "%ls", buf
);
3706 void do_history_find(int which
)
3708 struct input_data_s
*in
;
3711 if (pgn_history_total(gp
->hp
) < 2)
3714 in
= Calloc(1, sizeof(struct input_data_s
));
3715 p
= Malloc(sizeof(int));
3718 in
->efunc
= do_find_move_exp
;
3720 if (!*moveexp
|| which
== 0) {
3721 construct_input(_("Find Move Text Expression"), NULL
, 1, 0, NULL
, NULL
, NULL
,
3722 0, in
, INPUT_HIST_MOVE_EXP
, -1);
3726 do_find_move_exp_finalize(0, which
);
3729 void do_history_find_new()
3734 void do_history_find_prev()
3736 do_history_find(-1);
3739 void do_history_find_next()
3744 void do_history_rav(int which
)
3746 struct userdata_s
*d
= gp
->data
;
3748 rav_next_prev(gp
, d
->b
, which
);
3751 void do_history_rav_next()
3756 void do_history_rav_prev()
3761 void do_history_jump()
3763 struct input_data_s
*in
;
3765 if (pgn_history_total(gp
->hp
) < 2)
3769 in
= Calloc(1, sizeof(struct input_data_s
));
3770 in
->efunc
= do_move_jump
;
3772 construct_input(_("Jump to Move Number"), NULL
, 1, 1, NULL
,
3773 NULL
, NULL
, 0, in
, -1, 0);
3777 do_move_jump_finalize(keycount
);
3780 static void free_userdata_once(GAME g
)
3782 struct userdata_s
*d
= g
->data
;
3790 if (d
->engine
->enginebuf
) {
3793 for (n
= 0; d
->engine
->enginebuf
[n
]; n
++)
3794 free(d
->engine
->enginebuf
[n
]);
3796 free(d
->engine
->enginebuf
);
3799 if (d
->engine
->queue
) {
3802 for (q
= d
->engine
->queue
; *q
; q
++)
3805 free(d
->engine
->queue
);
3823 static void free_userdata()
3827 for (i
= 0; i
< gtotal
; i
++) {
3828 free_userdata_once(game
[i
]);
3829 game
[i
]->data
= NULL
;
3833 void update_loading_window(int n
)
3838 loadingw
= newwin(3, COLS
/ 2, CALCPOSY(3), CALCPOSX(COLS
/ 2));
3839 loadingp
= new_panel(loadingw
);
3840 wbkgd(loadingw
, CP_MESSAGE_WINDOW
);
3843 wmove(loadingw
, 0, 0);
3844 wclrtobot(loadingw
);
3845 wattron(loadingw
, CP_MESSAGE_BORDER
);
3846 box(loadingw
, ACS_VLINE
, ACS_HLINE
);
3847 wattroff(loadingw
, CP_MESSAGE_BORDER
);
3848 mvwprintw(loadingw
, 1, CENTER_INT((COLS
/ 2), 11 +
3849 strlen(itoa(gtotal
, buf
))),
3850 _("Loading... %i%% (%i games)"), n
, gtotal
);
3855 static void init_userdata_once(GAME g
, int n
)
3857 struct userdata_s
*d
= NULL
;
3859 d
= Calloc(1, sizeof(struct userdata_s
));
3861 d
->c_row
= 2, d
->c_col
= 5;
3862 SET_FLAG(d
->flags
, CF_NEW
);
3865 if (pgn_board_init_fen(g
, d
->b
, NULL
) != E_PGN_OK
)
3866 pgn_board_init(d
->b
);
3869 void init_userdata()
3873 for (i
= 0; i
< gtotal
; i
++)
3874 init_userdata_once(game
[i
], i
);
3877 void fix_marks(int *start
, int *end
)
3881 *start
= (*start
< 0) ? 0 : *start
;
3882 *end
= (*end
< 0) ? 0 : *end
;
3884 if (*start
> *end
) {
3890 *end
= (*end
> gtotal
) ? gtotal
: *end
;
3893 void do_new_game_finalize(GAME g
)
3895 struct userdata_s
*d
= g
->data
;
3897 d
->mode
= MODE_PLAY
;
3898 update_status_notify(g
, NULL
);
3901 void do_new_game_from_scratch(WIN
*win
)
3903 wchar_t str
[] = { win
->c
, 0 };
3905 if (wcscmp (str
, yes_wchar
))
3912 add_custom_tags(&gp
->tag
);
3915 do_new_game_finalize(gp
);
3923 add_custom_tags(&gp
->tag
);
3924 init_userdata_once(gp
, gindex
);
3925 do_new_game_finalize(gp
);
3929 void do_game_delete_finalize(int n
)
3931 struct userdata_s
*d
;
3933 delete_game((!n
) ? gindex
: -1);
3935 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3938 void do_game_delete_confirm(WIN
*win
)
3941 wchar_t str
[] = { win
->c
, 0 };
3943 if (wcscmp (str
, yes_wchar
)) {
3948 n
= (int *)win
->data
;
3949 do_game_delete_finalize(*n
);
3953 void do_game_delete()
3957 struct userdata_s
*d
;
3961 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
3967 for (i
= n
= 0; i
< gtotal
; i
++) {
3970 if (TEST_FLAG(d
->flags
, CF_DELETE
))
3975 tmp
= _("Delete the current game?");
3978 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
3982 tmp
= _("Delete all games marked for deletion?");
3985 if (config
.deleteprompt
) {
3986 p
= Malloc(sizeof(int));
3988 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, p
,
3989 do_game_delete_confirm
, 0, 0, tmp
);
3993 do_game_delete_finalize(n
);
3996 void do_find_game_exp_finalize(int which
)
3998 struct userdata_s
*d
= gp
->data
;
4001 if ((n
= find_game_exp(gameexp
, (which
== -1) ? 0 : 1,
4002 (keycount
) ? keycount
: 1)) == -1) {
4003 update_status_notify(gp
, "%s", _("No matches found"));
4010 if (pgn_history_total(gp
->hp
))
4011 d
->mode
= MODE_HISTORY
;
4013 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4016 void do_find_game_exp(WIN
*win
)
4018 struct input_data_s
*in
= win
->data
;
4023 strncpy(gameexp
, in
->str
, sizeof(gameexp
));
4024 gameexp
[sizeof(gameexp
)-1] = 0;
4029 do_find_game_exp_finalize(c
);
4037 void do_game_jump_finalize(int n
)
4039 struct userdata_s
*d
;
4041 if (--n
> gtotal
- 1 || n
< 0)
4047 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4048 update_status_notify(gp
, NULL
);
4051 void do_game_jump(WIN
*win
)
4053 struct input_data_s
*in
= win
->data
;
4055 if (!in
->str
|| !isinteger(in
->str
)) {
4063 do_game_jump_finalize(atoi(in
->str
));
4068 void do_load_file(WIN
*win
)
4070 struct input_data_s
*in
= win
->data
;
4071 char *tmp
= in
->str
;
4072 struct userdata_s
*d
;
4073 PGN_FILE
*pgn
= NULL
;
4081 if ((tmp
= pathfix(tmp
)) == NULL
)
4084 n
= pgn_open(tmp
, "r", &pgn
);
4086 if (n
== E_PGN_ERR
) {
4087 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
, strerror(errno
));
4090 else if (n
== E_PGN_INVALID
) {
4091 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
, _("Not a regular file"));
4097 if (pgn_parse(pgn
) == E_PGN_ERR
) {
4098 del_panel(loadingp
);
4106 del_panel(loadingp
);
4111 strncpy(loadfile
, tmp
, sizeof(loadfile
));
4112 loadfile
[sizeof(loadfile
)-1] = 0;
4116 if (pgn_history_total(gp
->hp
))
4117 d
->mode
= MODE_HISTORY
;
4119 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4121 fm_loaded_file
= TRUE
;
4132 void do_game_save(WIN
*win
)
4134 struct input_data_s
*in
= win
->data
;
4137 char *tmp
= in
->str
;
4138 char tfile
[FILENAME_MAX
];
4141 struct userdata_s
*d
;
4143 if (!tmp
|| (tmp
= pathfix(tmp
)) == NULL
)
4146 if (pgn_is_compressed(tmp
) == E_PGN_ERR
) {
4147 p
= tmp
+ strlen(tmp
) - 1;
4149 if (*p
!= 'n' || *(p
-1) != 'g' || *(p
-2) != 'p' ||
4151 snprintf(tfile
, sizeof(tfile
), "%s.pgn", tmp
);
4157 * When in edit mode, update the FEN tag.
4160 for (i
= 0; i
< gtotal
; i
++) {
4163 if (d
->mode
== MODE_EDIT
) {
4164 char *fen
= pgn_game_to_fen(game
[i
], d
->b
);
4166 pgn_tag_add(&game
[i
]->tag
, "FEN", fen
);
4174 if (d
->mode
== MODE_EDIT
) {
4175 char *fen
= pgn_game_to_fen(game
[n
], d
->b
);
4177 pgn_tag_add(&game
[n
]->tag
, "FEN", fen
);
4192 void do_get_game_save_input(int n
)
4194 struct input_data_s
*in
= Calloc(1, sizeof(struct input_data_s
));
4195 int *p
= Malloc(sizeof(int));
4197 in
->efunc
= do_game_save
;
4201 construct_input(_("Save Game Filename"), loadfile
, 1, 1, _("Type TAB for file browser"),
4202 file_browser
, NULL
, '\t', in
, INPUT_HIST_FILE
, -1);
4205 void do_game_save_multi_confirm(WIN
*win
)
4208 wchar_t str
[] = { win
->c
, 0 };
4210 if (!wcscmp (str
, current_wchar
))
4212 else if (!wcscmp (str
, all_wchar
))
4215 update_status_notify(gp
, "%s", _("Save game aborted."));
4219 do_get_game_save_input(i
);
4222 void do_global_about()
4224 cmessage("ABOUT", ANY_KEY_STR
, "%s\nUsing %s with %i colors "
4225 "and %i color pairs\n%s",
4226 PACKAGE_STRING
, curses_version(), COLORS
, COLOR_PAIRS
,
4230 void global_game_next_prev(int which
)
4232 struct userdata_s
*d
;
4234 game_next_prev(gp
, (which
== 1) ? 1 : 0,
4235 (keycount
) ? keycount
: 1);
4240 markend
= markstart
+ delete_count
;
4244 markend
= markstart
- delete_count
+ 1;
4245 delete_count
= -1; // to fix gindex in the other direction
4248 fix_marks(&markstart
, &markend
);
4249 do_global_toggle_delete();
4252 if (d
->mode
== MODE_HISTORY
)
4253 pgn_board_update(gp
, d
->b
, gp
->hindex
);
4254 else if (d
->mode
== MODE_PLAY
)
4255 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4258 void do_global_next_game()
4260 global_game_next_prev(1);
4263 void do_global_prev_game()
4265 global_game_next_prev(0);
4268 void global_find(int which
)
4270 struct input_data_s
*in
;
4276 in
= Calloc(1, sizeof(struct input_data_s
));
4277 p
= Malloc(sizeof(int));
4280 in
->efunc
= do_find_game_exp
;
4282 if (!*gameexp
|| which
== 0) {
4283 construct_input(_("Find Game by Tag Expression"), NULL
, 1, 0,
4284 _("[name expression:]value expression"), NULL
, NULL
, 0, in
,
4285 INPUT_HIST_GAME_EXP
, -1);
4289 do_find_game_exp_finalize(which
);
4292 void do_global_find_new()
4297 void do_global_find_next()
4302 void do_global_find_prev()
4307 void do_global_game_jump()
4313 struct input_data_s
*in
;
4315 in
= Calloc(1, sizeof(struct input_data_s
));
4316 in
->efunc
= do_game_jump
;
4317 construct_input(_("Jump to Game Number"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
,
4322 do_game_jump_finalize(keycount
);
4325 void do_global_toggle_delete()
4334 if (keycount
&& delete_count
== 0) {
4336 delete_count
= keycount
;
4337 update_status_notify(gp
, "%s (delete)", status
.notify
);
4341 if (markstart
>= 0 && markend
>= 0) {
4342 for (i
= markstart
; i
< markend
; i
++) {
4343 if (toggle_delete_flag(i
)) {
4348 gindex
= (delete_count
< 0) ? markstart
: i
- 1;
4351 if (toggle_delete_flag(gindex
))
4355 markstart
= markend
= -1;
4357 update_status_window(gp
);
4360 void do_global_delete_game()
4365 void do_global_tag_edit()
4367 struct userdata_s
*d
= gp
->data
;
4369 edit_tags(gp
, d
->b
, 1);
4372 void do_global_tag_view()
4374 struct userdata_s
*d
= gp
->data
;
4376 edit_tags(gp
, d
->b
, 0);
4379 void do_global_resume_game()
4381 struct input_data_s
*in
;
4383 in
= Calloc(1, sizeof(struct input_data_s
));
4384 in
->efunc
= do_load_file
;
4385 construct_input(_("Load Filename"), NULL
, 1, 1, _("Type TAB for file browser"), file_browser
,
4386 NULL
, '\t', in
, INPUT_HIST_FILE
, -1);
4389 void do_global_save_game()
4392 construct_message(NULL
, _("What would you like to do?"), 0, 1,
4393 NULL
, NULL
, NULL
, do_game_save_multi_confirm
, 0, 0,
4394 _("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."),
4395 current_wchar
, all_wchar
);
4399 do_get_game_save_input(-1);
4402 void do_global_new_game()
4407 void do_global_copy_game()
4411 struct userdata_s
*d
;
4413 do_global_new_game();
4415 n
= pgn_tag_total(game
[g
]->tag
);
4417 for (i
= 0; i
< n
; i
++)
4418 pgn_tag_add(&gp
->tag
, game
[g
]->tag
[i
]->name
,
4419 game
[g
]->tag
[i
]->value
);
4421 pgn_board_init_fen (gp
, d
->b
, NULL
);
4422 n
= pgn_history_total(game
[g
]->history
);
4425 for (i
= 0; i
< n
; i
++) {
4427 char move
[MAX_SAN_MOVE_LEN
+1] = {0}, *m
= move
;
4429 strcpy (move
, game
[g
]->history
[i
]->move
);
4430 if (pgn_parse_move(gp
, d
->b
, &m
, &frfr
) != E_PGN_OK
) {
4431 SET_FLAG(gp
->flags
, GF_PERROR
);
4435 pgn_history_add(gp
, d
->b
, move
);
4437 pgn_switch_turn(gp
);
4440 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4443 void do_global_new_all()
4445 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
4446 do_new_game_from_scratch
, 0, 0, "%s", _("Really start a new game from scratch?"));
4449 void do_quit(WIN
*win
)
4451 wchar_t str
[] = { win
->c
, 0 };
4453 if (wcscmp (str
, yes_wchar
))
4459 void do_global_quit()
4461 if (config
.exitdialogbox
)
4462 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
4463 do_quit
, 0, 0, _("Want to Quit?"));
4468 void do_global_toggle_engine_window()
4471 enginew
= newwin(LINES
, COLS
, 0, 0);
4472 enginep
= new_panel(enginew
);
4473 window_draw_title(enginew
, _("Engine IO Window"), COLS
, CP_MESSAGE_TITLE
,
4475 hide_panel(enginep
);
4478 if (panel_hidden(enginep
)) {
4479 update_engine_window(gp
);
4483 hide_panel(enginep
);
4487 void do_global_toggle_board_details()
4492 void do_global_toggle_strict_castling()
4494 do_toggle_strict_castling();
4497 // Global and other keys.
4498 static int globalkeys()
4500 struct userdata_s
*d
= gp
->data
;
4504 * These cannot be modified and other game mode keys cannot conflict with
4509 do_window_resize ();
4512 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
4513 markend
= markstart
= 0;
4517 update_status_notify(gp
, NULL
);
4520 if (config
.validmoves
)
4521 pgn_reset_valid_moves(d
->b
);
4528 keycount
= keycount
* 10 + i
;
4532 update_status_notify(gp
, _("Repeat %i"), keycount
);
4535 if (d
->mode
== MODE_HISTORY
)
4539 d
->c_row
+= keycount
;
4548 if (d
->mode
== MODE_HISTORY
)
4552 d
->c_row
-= keycount
;
4553 update_status_notify(gp
, NULL
);
4563 if (d
->mode
== MODE_HISTORY
)
4567 d
->c_col
-= keycount
;
4576 if (d
->mode
== MODE_HISTORY
)
4580 d
->c_col
+= keycount
;
4592 for (i
= 0; global_keys
[i
]; i
++) {
4593 if (input_c
== global_keys
[i
]->c
) {
4594 (*global_keys
[i
]->f
)();
4605 static void perl_error(const char *fmt
, ...)
4611 vasprintf(&buf
, fmt
, ap
);
4614 message(ERROR_STR
, ANY_KEY_STR
, "%s", buf
);
4618 static void do_perl_finalize(WIN
*win
)
4620 struct input_data_s
*in
= win
->data
;
4622 struct userdata_s
*d
= g
->data
;
4624 char *result
= NULL
;
4628 asprintf(&filename
, "%s/perl.pl", config
.datadir
);
4633 if (perl_init_file(filename
, perl_error
))
4636 arg
= pgn_game_to_fen(g
, d
->b
);
4638 if (perl_call_sub(trim(in
->str
), arg
, &result
))
4641 d
->perlfen
= pgn_game_to_fen(g
, d
->b
);
4642 d
->perlflags
= g
->flags
;
4644 if (pgn_board_init_fen(g
, d
->b
, result
) != E_PGN_OK
) {
4645 message(ERROR_STR
, ANY_KEY_STR
, "%s", _("FEN parse error."));
4646 pgn_board_init_fen(g
, d
->b
, d
->perlfen
);
4647 g
->flags
= d
->perlflags
;
4653 SET_FLAG(d
->flags
, CF_PERL
);
4654 n
= pgn_tag_find(g
->tag
, "FEN");
4657 d
->oldfen
= strdup(g
->tag
[n
]->value
);
4659 pgn_tag_add(&g
->tag
, "FEN", result
);
4660 update_status_notify(g
, "%s", ANY_KEY_STR
);
4671 void do_global_perl()
4673 struct input_data_s
*in
;
4675 in
= Calloc(1, sizeof(struct input_data_s
));
4677 in
->efunc
= do_perl_finalize
;
4678 construct_input(_("PERL Subroutine Filter"), NULL
, 1, 0, NULL
, NULL
, NULL
, 0, in
, INPUT_HIST_PERL
, -1);
4683 * A macro may contain a key that belongs to another macro so macro_match will
4684 * need to be updated to the new index of the matching macro.
4686 static void find_macro(struct userdata_s
*d
)
4691 * Macros can't contain macros when in a window.
4697 for (i
= 0; macros
[i
]; i
++) {
4698 if ((macros
[i
]->mode
== -1 || macros
[i
]->mode
== d
->mode
) &&
4699 input_c
== macros
[i
]->c
) {
4700 input_c
= macros
[i
]->keys
[macros
[i
]->n
++];
4702 if (!macro_depth_n
&& macro_match
> -1) {
4703 macro_depth
= realloc(macro_depth
, (macro_depth_n
+ 1) * sizeof(int));
4704 macro_depth
[macro_depth_n
++] = macro_match
;
4707 macro_depth
= realloc(macro_depth
, (macro_depth_n
+ 1) * sizeof(int));
4708 macro_depth
[macro_depth_n
++] = i
;
4716 * Resets the position in each macro to the first key.
4718 static void reset_macros()
4721 struct userdata_s
*d
= gp
->data
;
4724 if (macro_depth_n
> 0) {
4726 macro_match
= macro_depth
[macro_depth_n
];
4728 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
4731 input_c
= macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
4736 for (i
= 0; macros
[i
]; i
++)
4747 struct userdata_s
*d
;
4750 gindex
= gtotal
- 1;
4754 if (pgn_history_total(gp
->hp
))
4755 d
->mode
= MODE_HISTORY
;
4757 d
->mode
= MODE_PLAY
;
4758 d
->play_mode
= PLAY_HE
;
4761 if (d
->mode
== MODE_HISTORY
)
4762 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4764 update_status_notify(gp
, "%s", _("Type F1 for help"));
4768 wtimeout(boardw
, WINDOW_TIMEOUT
);
4772 char fdbuf
[8192] = {0};
4774 struct timeval tv
= {0, 0};
4782 for (i
= 0; i
< gtotal
; i
++) {
4785 if (d
->engine
&& d
->engine
->pid
!= -1) {
4786 if (d
->engine
->fd
[ENGINE_IN_FD
] > 2) {
4787 if (d
->engine
->fd
[ENGINE_IN_FD
] > n
)
4788 n
= d
->engine
->fd
[ENGINE_IN_FD
];
4790 FD_SET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
);
4793 if (d
->engine
->fd
[ENGINE_OUT_FD
] > 2) {
4794 if (d
->engine
->fd
[ENGINE_OUT_FD
] > n
)
4795 n
= d
->engine
->fd
[ENGINE_OUT_FD
];
4797 FD_SET(d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
);
4803 if ((n
= select(n
+ 1, &rfds
, &wfds
, NULL
, &tv
)) > 0) {
4804 for (i
= 0; i
< gtotal
; i
++) {
4807 if (d
->engine
&& d
->engine
->pid
!= -1) {
4808 if (FD_ISSET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
)) {
4809 len
= read(d
->engine
->fd
[ENGINE_IN_FD
], fdbuf
,
4813 if (d
->engine
->iobuf
)
4814 d
->engine
->iobuf
= Realloc(d
->engine
->iobuf
, d
->engine
->len
+ len
+ 1);
4816 d
->engine
->iobuf
= Calloc(1, len
+ 1);
4818 memcpy(&(d
->engine
->iobuf
[d
->engine
->len
]), &fdbuf
, len
);
4819 d
->engine
->len
+= len
;
4820 d
->engine
->iobuf
[d
->engine
->len
] = 0;
4823 * The fdbuf is full or no newline
4824 * was found. So we'll append the next
4825 * read() to this games buffer.
4827 if (d
->engine
->iobuf
[d
->engine
->len
- 1] != '\n')
4830 parse_engine_output(game
[i
], d
->engine
->iobuf
);
4831 free(d
->engine
->iobuf
);
4832 d
->engine
->iobuf
= NULL
;
4835 else if (len
== -1) {
4836 if (errno
!= EAGAIN
) {
4837 cmessage(ERROR_STR
, ANY_KEY_STR
, "Engine read(): %s",
4839 waitpid(d
->engine
->pid
, &n
, 0);
4847 if (FD_ISSET(d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
)) {
4848 if (d
->engine
->queue
)
4849 send_engine_command(game
[i
]);
4856 cmessage(ERROR_STR
, ANY_KEY_STR
, "select(): %s", strerror(errno
));
4865 * This is needed to detect terminal resizing.
4868 if (LINES
!= LINES_OLD
|| COLS
!= COLS_OLD
) {
4875 * Finds the top level window in the window stack so we know what
4876 * window the wget_wch()'ed key belongs to.
4879 for (i
= 0; wins
[i
]; i
++);
4882 wtimeout(wp
, WINDOW_TIMEOUT
);
4891 if (macros
&& macro_match
>= 0) {
4892 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
4895 input_c
= macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
4900 if (wget_wch(wp
, &input_c
) == ERR
|| input_c
== KEY_RESIZE
)
4910 do_window_resize ();
4917 * Run the function associated with the window. When the
4918 * function returns 0 win->efunc is ran (if not NULL) with
4919 * win as the one and only parameter. Then the window is
4922 * The exit function may create another window which will
4923 * mess up the window stack when window_destroy() is called.
4924 * So don't destory the window until the top window is
4925 * destroyable. See window_destroy().
4927 if ((*win
->func
)(win
) == 0) {
4932 window_destroy(win
);
4940 if (!keycount
&& status
.notify
)
4941 update_status_notify(gp
, NULL
);
4944 if (TEST_FLAG(d
->flags
, CF_PERL
)) {
4945 CLEAR_FLAG(d
->flags
, CF_PERL
);
4946 pgn_board_init_fen(gp
, d
->b
, d
->perlfen
);
4947 gp
->flags
= d
->perlflags
;
4949 pgn_tag_add(&gp
->tag
, "FEN", d
->oldfen
);
4951 d
->perlfen
= d
->oldfen
= NULL
;
4957 if (macros
&& macro_match
< 0)
4960 if ((n
= globalkeys()) == 1) {
4961 if (macro_match
== -1)
4971 for (i
= 0; edit_keys
[i
]; i
++) {
4972 if (input_c
== edit_keys
[i
]->c
) {
4973 (*edit_keys
[i
]->f
)();
4979 for (i
= 0; play_keys
[i
]; i
++) {
4980 if (input_c
== play_keys
[i
]->c
) {
4981 (*play_keys
[i
]->f
)();
4986 do_play_config_command();
4989 for (i
= 0; history_keys
[i
]; i
++) {
4990 if (input_c
== history_keys
[i
]->c
) {
4991 (*history_keys
[i
]->f
)();
5002 update_status_notify(gp
, NULL
);
5011 void usage(const char *pn
, int ret
)
5013 fprintf((ret
) ? stderr
: stdout
, "%s%s",
5016 "Usage: cboard [-hvCD] [-u [N]] [-p [-VtRSE] <file>]\n"
5017 " -D Dump libchess debugging info to \"libchess.debug\" (stderr)\n"),
5020 "Usage: cboard [-hvC] [-u [N]] [-p [-VtRSE] <file>]\n"),
5023 " -p Load PGN file.\n"
5024 " -V Validate a game file.\n"
5025 " -S Validate and output a PGN formatted game.\n"
5026 " -R Like -S but write a reduced PGN formatted game.\n"
5027 " -t Also write custom PGN tags from config file.\n"
5028 " -E Stop processing on file parsing error (overrides config).\n"
5029 " -C Enable strict castling (overrides config).\n"
5030 " -u Enable/disable UTF-8 pieces (1=enable, 0=disable, overrides config).\n"
5031 " -v Version information.\n"
5032 " -h This help text.\n"));
5044 free(config
.engine_cmd
);
5045 free(config
.pattern
);
5046 free(config
.ccfile
);
5047 free(config
.nagfile
);
5048 free(config
.configfile
);
5051 for (i
= 0; config
.keys
[i
]; i
++) {
5052 free(config
.keys
[i
]->str
);
5053 free(config
.keys
[i
]);
5060 for (i
= 0; config
.einit
[i
]; i
++)
5061 free(config
.einit
[i
]);
5067 pgn_tag_free(config
.tag
);
5069 free(config
.datadir
);
5071 if (curses_initialized
) {
5073 del_panel(historyp
);
5094 static void signal_save_pgn(int sig
)
5098 char *p
= config
.savedirectory
? config
.savedirectory
: config
.datadir
;
5101 asprintf(&buf
, "%s/signal-%i-%li.pgn", p
, sig
, now
);
5103 if (do_game_write(buf
, "w", 0, gtotal
)) {
5104 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", p
, strerror(errno
));
5105 update_status_notify(gp
, "%s", _("Save game failed."));
5112 void catch_signal(int which
)
5119 if (which
== SIGPIPE
&& quit
)
5122 if (which
== SIGPIPE
)
5123 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Broken pipe. Quitting."));
5133 do_window_resize ();
5134 keypad(boardw
, TRUE
);
5140 signal_save_pgn(which
);
5147 void loading_progress(long total
, long offset
)
5149 int n
= (100 * (offset
/ 100) / (total
/ 100));
5151 if (curses_initialized
)
5152 update_loading_window(n
);
5154 fprintf(stderr
, _("Loading... %i%% (%i games)%c"), n
, gtotal
, '\r');
5159 static void set_defaults()
5161 set_config_defaults();
5163 filetype
= FILE_NONE
;
5164 pgn_config_set(PGN_PROGRESS
, 1024);
5165 pgn_config_set(PGN_PROGRESS_FUNC
, loading_progress
);
5168 int main(int argc
, char *argv
[])
5172 char buf
[FILENAME_MAX
];
5173 char datadir
[FILENAME_MAX
];
5174 int ret
= EXIT_SUCCESS
;
5175 int validate_only
= 0, validate_and_write
= 0;
5176 int write_custom_tags
= 0;
5179 int utf8_pieces
= -1;
5181 setlocale (LC_ALL
, "");
5182 bindtextdomain ("cboard", LOCALE_DIR
);
5183 textdomain ("cboard");
5186 #ifndef HAVE_PROGNAME
5187 __progname
= argv
[0];
5190 if ((config
.pwd
= getpwuid(getuid())) == NULL
)
5191 err(EXIT_FAILURE
, "getpwuid()");
5193 snprintf(datadir
, sizeof(datadir
), "%s/.cboard", config
.pwd
->pw_dir
);
5194 config
.datadir
= strdup(datadir
);
5195 snprintf(buf
, sizeof(buf
), "%s/cc.data", datadir
);
5196 config
.ccfile
= strdup(buf
);
5197 snprintf(buf
, sizeof(buf
), "%s/nag.data", datadir
);
5198 config
.nagfile
= strdup(buf
);
5199 snprintf(buf
, sizeof(buf
), "%s/config", datadir
);
5200 config
.configfile
= strdup(buf
);
5202 if (stat(datadir
, &st
) == -1) {
5203 if (errno
== ENOENT
) {
5204 if (mkdir(datadir
, 0755) == -1)
5205 err(EXIT_FAILURE
, "%s", datadir
);
5208 err(EXIT_FAILURE
, "%s", datadir
);
5213 if (!S_ISDIR(st
.st_mode
))
5214 errx(EXIT_FAILURE
, "%s: %s", datadir
, _("Not a directory."));
5219 while ((opt
= getopt(argc
, argv
, "DCEVtSRhp:vu::")) != -1) {
5221 while ((opt
= getopt(argc
, argv
, "ECVtSRhp:vu::")) != -1) {
5226 unlink("libchess.debug");
5227 pgn_config_set(PGN_DEBUG
, 1);
5231 pgn_config_set(PGN_STRICT_CASTLING
, 1);
5234 write_custom_tags
= 1;
5240 pgn_config_set(PGN_REDUCED
, 1);
5242 validate_and_write
= 1;
5247 printf("%s (%s)\n%s\n", PACKAGE_STRING
, curses_version(),
5251 filetype
= FILE_PGN
;
5252 strncpy(loadfile
, optarg
, sizeof(loadfile
));
5253 loadfile
[sizeof(loadfile
)-1] = 0;
5256 utf8_pieces
= optarg
? atoi (optarg
): 1;
5260 usage(argv
[0], EXIT_SUCCESS
);
5264 if ((validate_only
|| validate_and_write
) && !*loadfile
)
5265 usage(argv
[0], EXIT_FAILURE
);
5267 if (access(config
.configfile
, R_OK
) == 0)
5268 parse_rcfile(config
.configfile
);
5271 pgn_config_set(PGN_STOP_ON_ERROR
, 1);
5273 signal(SIGPIPE
, catch_signal
);
5274 signal(SIGCONT
, catch_signal
);
5275 signal(SIGSTOP
, catch_signal
);
5276 signal(SIGINT
, catch_signal
);
5277 signal(SIGALRM
, catch_signal
);
5278 signal(SIGTERM
, catch_signal
);
5284 if (pgn_open(loadfile
, "r", &pgn
) != E_PGN_OK
)
5285 err(EXIT_FAILURE
, "%s", loadfile
);
5287 ret
= pgn_parse(pgn
);
5291 //ret = parse_fen_file(loadfile);
5293 case FILE_EPD
: // Not implemented.
5296 // No file specified. Empty game.
5297 ret
= pgn_parse(NULL
);
5299 add_custom_tags(&gp
->tag
);
5303 if (validate_only
|| validate_and_write
) {
5304 if (validate_and_write
) {
5305 if (pgn_open("-", "r", &pgn
) != E_PGN_OK
)
5306 err(EXIT_FAILURE
, "pgn_open()");
5308 for (i
= 0; i
< gtotal
; i
++) {
5309 if (write_custom_tags
)
5310 add_custom_tags(&game
[i
]->tag
);
5312 pgn_write(pgn
, game
[i
]);
5317 fm_loaded_file
= TRUE
;
5323 else if (ret
== E_PGN_ERR
)
5326 if (utf8_pieces
!= -1)
5327 config
.utf8_pieces
= utf8_pieces
;
5329 init_wchar_pieces ();
5330 yes_wchar
= str_to_wchar (_("y"));
5331 all_wchar
= str_to_wchar (_("a"));
5332 overwrite_wchar
= str_to_wchar (_("o"));
5333 resume_wchar
= str_to_wchar (_("r"));
5334 current_wchar
= str_to_wchar (_("c"));
5335 append_wchar
= str_to_wchar (_("a"));
5336 translatable_tag_names
[0] = _("Event");
5337 translatable_tag_names
[1] = _("Site");
5338 translatable_tag_names
[2] = _("Date");
5339 translatable_tag_names
[3] = _("Round");
5340 translatable_tag_names
[4] = _("White");
5341 translatable_tag_names
[5] = _("Black");
5342 translatable_tag_names
[6] = _("Result");
5346 * This fixes window resizing in an xterm.
5348 if (getenv("DISPLAY") != NULL
) {
5353 if (initscr() == NULL
)
5354 errx(EXIT_FAILURE
, "%s", _("Could not initialize curses."));
5356 curses_initialized
= 1;
5358 if (LINES
< 23 || COLS
< 74) {
5360 errx(EXIT_FAILURE
, _("Need at least an 74x23 terminal."));
5366 if (has_colors() == TRUE
&& start_color() == OK
)
5369 boardw
= newwin(BOARD_HEIGHT
, BOARD_WIDTH
, 0, COLS
- BOARD_WIDTH
);
5370 boardp
= new_panel(boardw
);
5371 historyw
= newwin(HISTORY_HEIGHT
, HISTORY_WIDTH
, LINES
- HISTORY_HEIGHT
,
5372 COLS
- HISTORY_WIDTH
);
5373 historyp
= new_panel(historyw
);
5374 statusw
= newwin(STATUS_HEIGHT
, STATUS_WIDTH
, 0, 0);
5375 statusp
= new_panel(statusw
);
5376 tagw
= newwin(TAG_HEIGHT
, TAG_WIDTH
, STATUS_HEIGHT
+ 1, 0);
5377 tagp
= new_panel(tagw
);
5378 keypad(boardw
, TRUE
);
5379 // leaveok(boardw, TRUE);
5380 leaveok(tagw
, TRUE
);
5381 leaveok(statusw
, TRUE
);
5382 leaveok(historyw
, TRUE
);
5386 draw_window_decor();
5389 free (w_pawn_wchar
);
5390 free (w_rook_wchar
);
5391 free (w_bishop_wchar
);
5392 free (w_knight_wchar
);
5393 free (w_queen_wchar
);
5394 free (w_king_wchar
);
5395 free (b_pawn_wchar
);
5396 free (b_rook_wchar
);
5397 free (b_bishop_wchar
);
5398 free (b_knight_wchar
);
5399 free (b_queen_wchar
);
5400 free (b_king_wchar
);
5402 free (enpassant_wchar
);
5405 free (overwrite_wchar
);
5406 free (resume_wchar
);
5407 free (current_wchar
);
5408 free (append_wchar
);