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 (20)
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 // FIXME: BIG_BOARD - start in dwm monocle layout.
850 // Example: lxterminal -e cboard (dmenu, geany, etc.)
851 void update_board_window(GAME g
)
854 int bcol
= 0, brow
= 0;
855 int l
= config
.coordsyleft
;
856 int maxy
= BOARD_HEIGHT
, maxx
= BOARD_WIDTH
;
857 int ncols
= 0, offset
= 1;
858 int rowr
= (MEGA_BOARD
) ? 6 : (BIG_BOARD
) ? 4 : 2;
859 int colr
= (MEGA_BOARD
) ? 12 : (BIG_BOARD
) ? 8 : 4;
860 unsigned coords_y
= 8, cxgc
= 0;
862 struct userdata_s
*d
= g
->data
;
864 if (d
->mode
!= MODE_PLAY
&& d
->mode
!= MODE_EDIT
)
865 update_cursor(g
, g
->hindex
);
882 for (row
= 0; row
< maxy
; row
++) {
896 for (col
= 0; col
< maxx
; col
++) {
898 chtype attrs
= 0, old_attrs
= 0;
903 if (row
== 0 || row
== maxy
- 2) {
905 mvwaddch(boardw
, row
, col
+ l
,
907 ? ACS_LLCORNER
| CP_BOARD_GRAPHICS
908 : ACS_ULCORNER
| CP_BOARD_GRAPHICS
));
909 else if (col
== maxx
- 2)
910 mvwaddch(boardw
, row
, col
+ l
,
912 ? ACS_LRCORNER
| CP_BOARD_GRAPHICS
913 : ACS_URCORNER
| CP_BOARD_GRAPHICS
));
914 else if (!(col
% colr
))
915 mvwaddch(boardw
, row
, col
+ l
,
917 ? ACS_BTEE
| CP_BOARD_GRAPHICS
918 : ACS_TTEE
| CP_BOARD_GRAPHICS
));
921 mvwaddch(boardw
, row
, col
+ l
,
922 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
928 if ((row
% 2) && col
== maxx
- 1 &&
929 (coords_y
> 0 && coords_y
< 9)) {
930 wattron(boardw
, CP_BOARD_COORDS
);
932 (BIG_BOARD
) ? row
* ((MEGA_BOARD
) ? 3 : 2)
933 : row
, (l
) ? 0 : col
, "%d",
934 (rotate
) ? coords_y
++ : coords_y
--);
935 wattroff(boardw
, CP_BOARD_COORDS
);
939 if ((col
== 0 || col
== maxx
- 2) && row
!= maxy
- 1) {
941 mvwaddch(boardw
, row
, col
+ l
,
943 ACS_RTEE
| CP_BOARD_GRAPHICS
:
944 ACS_LTEE
| CP_BOARD_GRAPHICS
));
946 mvwaddch(boardw
, row
, col
+ l
,
947 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
952 if ((row
% rowr
) && !(col
% colr
) && row
!= maxy
- 1) {
953 mvwaddch(boardw
, row
, col
+ l
,
954 LINE_GRAPHIC(ACS_VLINE
| CP_BOARD_GRAPHICS
));
958 if (!(col
% colr
) && row
!= maxy
- 1) {
959 mvwaddch(boardw
, row
, col
+ l
,
960 LINE_GRAPHIC(ACS_PLUS
| CP_BOARD_GRAPHICS
));
967 attrwhich
= (cb
[brow
][bcol
]) ? WHITE
: BLACK
;
974 if (((ncols
% 2) && !(offset
% 2))
975 || (!(ncols
% 2) && (offset
% 2)))
981 if (BIG_BOARD
&& rotate
) {
982 brow
= inv_int(brow
+ 1) - 1;
983 bcol
= inv_int(bcol
+ 1) - 1;
987 p
= d
->b
[brow
][bcol
].icon
;
989 p
= d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].icon
;
991 int pi
= pgn_piece_to_int(p
);
993 if (config
.details
&&
994 ((!BIG_BOARD
&& d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].enpassant
)
995 || (BIG_BOARD
&& d
->b
[brow
][bcol
].enpassant
))) {
997 attrs
= mix_cp(CP_BOARD_ENPASSANT
,
998 (attrwhich
== WHITE
) ? CP_BOARD_WHITE
: CP_BOARD_BLACK
,
999 ATTRS(CP_BOARD_ENPASSANT
), A_FG_B_BG
);
1002 // FIXME: showattacks - rotated board.
1003 if (config
.showattacks
&& config
.details
1004 && piece_can_attack (g
,
1005 BIG_BOARD
? inv_int (brow
+1) : brow
,
1006 BIG_BOARD
? bcol
+1 : bcol
)) {
1007 attrs
= CP_BOARD_ATTACK
;
1012 if (config
.validmoves
&&
1013 ((!BIG_BOARD
&& d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].valid
)
1014 || (BIG_BOARD
&& d
->b
[brow
][bcol
].valid
))) {
1018 if (attrwhich
== WHITE
)
1019 attrs
= mix_cp(CP_BOARD_MOVES_WHITE
,
1021 ATTRS(CP_BOARD_MOVES_WHITE
),
1024 attrs
= mix_cp(CP_BOARD_MOVES_BLACK
,
1026 ATTRS(CP_BOARD_MOVES_BLACK
),
1029 else if (p
!= 'x' && !can_attack
)
1030 attrs
= (attrwhich
== WHITE
) ? CP_BOARD_WHITE
: CP_BOARD_BLACK
;
1032 if (BIG_BOARD
&& rotate
) {
1033 brow
= inv_int(brow
+ 1) - 1;
1034 bcol
= inv_int(bcol
+ 1) - 1;
1037 if ((!BIG_BOARD
&& row
== ROWTOMATRIX(d
->c_row
)
1038 && col
== COLTOMATRIX(d
->c_col
))
1039 || (BIG_BOARD
&& brow
+ 1 == inv_int(d
->c_row
)
1040 && bcol
+ 1 == d
->c_col
)) {
1041 attrs
= mix_cp(CP_BOARD_CURSOR
, IS_ENPASSANT(p
),
1042 ATTRS(CP_BOARD_CURSOR
), B_FG_A_BG
);
1045 else if ((!BIG_BOARD
&& row
== ROWTOMATRIX(d
->sp
.srow
) &&
1046 col
== COLTOMATRIX(d
->sp
.scol
)) ||
1047 (BIG_BOARD
&& brow
+ 1 == inv_int(d
->sp
.srow
) &&
1048 bcol
+ 1 == d
->sp
.scol
)) {
1049 attrs
= mix_cp(CP_BOARD_SELECTED
, IS_ENPASSANT(p
),
1050 ATTRS(CP_BOARD_SELECTED
), B_FG_A_BG
);
1054 if (row
== maxy
- 1)
1058 attrs
= mix_cp(CP_BOARD_ATTACK
,
1060 (attrwhich
== WHITE
) ? CP_BOARD_MOVES_WHITE
: CP_BOARD_MOVES_BLACK
:
1061 (attrwhich
== WHITE
) ? CP_BOARD_WHITE
: CP_BOARD_BLACK
,
1062 ATTRS (CP_BOARD_ATTACK
), A_FG_B_BG
);
1066 wmove(boardw
, row
, col
+ ((MEGA_BOARD
) ? 5 : 3) + l
);
1068 mvwaddch(boardw
, row
, col
+ l
, ' ' | attrs
);
1070 if (row
== maxy
- 1 && cxgc
< 8) {
1071 waddch(boardw
, "abcdefgh"[(BIG_BOARD
) ? bcol
: bcol
- 1] | CP_BOARD_COORDS
);
1075 if (old_attrs
== -1) {
1082 if (pi
!= OPEN_SQUARE
&& p
!= 'x' && !can_attack
) {
1083 if (attrwhich
== WHITE
) {
1085 attrs
= CP_BOARD_W_W
;
1087 attrs
= CP_BOARD_W_B
;
1091 attrs
= CP_BOARD_B_W
;
1093 attrs
= CP_BOARD_B_B
;
1099 if (config
.details
&& !can_attack
1100 && castling_state(g
, d
->b
,
1101 (rotate
) ? inv_int(brow
+ 1) - 1: brow
,
1102 (rotate
) ? inv_int(bcol
+ 1) - 1: bcol
, p
, 0))
1103 attrs
= mix_cp(CP_BOARD_CASTLING
, attrs
,
1104 ATTRS(CP_BOARD_CASTLING
),
1108 if (config
.details
&& !can_attack
1109 && castling_state(g
, d
->b
, RANKTOBOARD (brow
),
1110 FILETOBOARD (bcol
), p
, 0)) {
1111 attrs
= mix_cp(CP_BOARD_CASTLING
, attrs
,
1112 ATTRS(CP_BOARD_CASTLING
),
1118 // FIXME: Reimpresión de piezas(+3).
1120 wattron (boardw
, attrs
);
1122 for (i
= 0; i
< 5; i
++)
1123 mvwprintw(boardw
, i
+ brow
* 6 + 1,
1126 if (pi
!= OPEN_SQUARE
)
1127 print_piece(boardw
, brow
* 6 + 2,
1128 bcol
* 12 + 3 + l
, p
);
1131 print_piece(boardw
, brow
* 4 + 1,
1133 (pi
!= OPEN_SQUARE
) ? p
: 0);
1136 wattroff (boardw
, attrs
);
1141 wattron (boardw
, attrs
);
1142 waddwstr (boardw
, piece_to_wchar (pi
!= OPEN_SQUARE
? p
: 0));
1143 wattroff (boardw
, attrs
);
1150 col
+= (MEGA_BOARD
) ? 10 : 6;
1152 waddch(boardw
, ' ' | attrs
);
1170 if (col
!= maxx
- 1)
1171 mvwaddch(boardw
, row
, col
+ l
,
1172 LINE_GRAPHIC(ACS_HLINE
| CP_BOARD_GRAPHICS
));
1192 void invalid_move(int n
, int e
, const char *m
)
1194 if (curses_initialized
)
1195 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s \"%s\" (round #%i)", (e
== E_PGN_AMBIGUOUS
)
1196 ? _("Ambiguous move") : _("Invalid move"), m
, n
);
1198 warnx("%s: %s \"%s\" (round #%i)", loadfile
, (e
== E_PGN_AMBIGUOUS
)
1199 ? _("Ambiguous move") : _("Invalid move"), m
, n
);
1202 void gameover(GAME g
)
1204 struct userdata_s
*d
= g
->data
;
1206 SET_FLAG(g
->flags
, GF_GAMEOVER
);
1207 d
->mode
= MODE_HISTORY
;
1211 static void update_clock(GAME g
, struct itimerval it
)
1213 struct userdata_s
*d
= g
->data
;
1215 if (TEST_FLAG(d
->flags
, CF_CLOCK
) && g
->turn
== WHITE
) {
1216 d
->wclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1217 d
->wclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1219 if (d
->wclock
.elapsed
.tv_usec
> 1000000 - 1) {
1220 d
->wclock
.elapsed
.tv_sec
+= d
->wclock
.elapsed
.tv_usec
/ 1000000;
1221 d
->wclock
.elapsed
.tv_usec
= d
->wclock
.elapsed
.tv_usec
% 1000000;
1224 if (d
->wclock
.tc
[d
->wclock
.tcn
][1] &&
1225 d
->wclock
.elapsed
.tv_sec
>= d
->wclock
.tc
[d
->wclock
.tcn
][1]) {
1226 pgn_tag_add(&g
->tag
, "Result", "0-1");
1230 else if (TEST_FLAG(d
->flags
, CF_CLOCK
) && g
->turn
== BLACK
) {
1231 d
->bclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1232 d
->bclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1234 if (d
->bclock
.elapsed
.tv_usec
> 1000000 - 1) {
1235 d
->bclock
.elapsed
.tv_sec
+= d
->bclock
.elapsed
.tv_usec
/ 1000000;
1236 d
->bclock
.elapsed
.tv_usec
= d
->bclock
.elapsed
.tv_usec
% 1000000;
1239 if (d
->bclock
.tc
[d
->bclock
.tcn
][1] &&
1240 d
->bclock
.elapsed
.tv_sec
>= d
->bclock
.tc
[d
->bclock
.tcn
][1]) {
1241 pgn_tag_add(&g
->tag
, "Result", "1-0");
1246 d
->elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1247 d
->elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1249 if (d
->elapsed
.tv_usec
> 1000000 - 1) {
1250 d
->elapsed
.tv_sec
+= d
->elapsed
.tv_usec
/ 1000000;
1251 d
->elapsed
.tv_usec
= d
->elapsed
.tv_usec
% 1000000;
1255 static void update_time_control(GAME g
)
1257 struct userdata_s
*d
= g
->data
;
1258 struct clock_s
*clk
= (g
->turn
== WHITE
) ? &d
->wclock
: &d
->bclock
;
1261 clk
->tc
[clk
->tcn
][1] += clk
->incr
;
1263 if (!clk
->tc
[clk
->tcn
][1])
1268 if (!clk
->tc
[clk
->tcn
][0] || clk
->move
>= clk
->tc
[clk
->tcn
][0]) {
1270 clk
->tc
[clk
->tcn
+ 1][1] += abs(clk
->elapsed
.tv_sec
- clk
->tc
[clk
->tcn
][1]);
1271 memset(&clk
->elapsed
, 0, sizeof(clk
->elapsed
));
1276 void update_history_window(GAME g
)
1278 char buf
[HISTORY_WIDTH
- 1];
1281 int t
= pgn_history_total(g
->hp
);
1283 n
= (g
->hindex
+ 1) / 2;
1286 total
= (t
+ 1) / 2;
1291 snprintf(buf
, sizeof(buf
), "%u %s %u%s", n
, _("of"), total
,
1292 (movestep
== 1) ? _(" (ply)") : "");
1294 strncpy(buf
, _("not available"), sizeof(buf
)-1);
1296 buf
[sizeof(buf
)-1] = 0;
1297 mvwprintw(historyw
, 2, 1, "%*s %-*s", 10, _("Move:"),
1298 HISTORY_WIDTH
- 14, buf
);
1300 h
= pgn_history_by_n(g
->hp
, g
->hindex
);
1301 snprintf(buf
, sizeof(buf
), "%s",
1302 (h
&& h
->move
) ? h
->move
1303 : (LINES
< 24) ? _("empty") : _("not available"));
1306 if (h
&& ((h
->comment
) || h
->nag
[0])) {
1307 strncat(buf
, _(" (Annotated"), sizeof(buf
)-1);
1312 strncat(buf
, (n
) ? ",+" : " (+", sizeof(buf
)-1);
1317 strncat(buf
, (n
) ? ",-" : " (-", sizeof(buf
)-1);
1322 strncat(buf
, ")", sizeof(buf
)-1);
1324 mvwprintw(historyw
, 3, ((LINES
< 24) ? 17 : 1), "%s %-*s",
1325 (LINES
< 24) ? _("Next:") :_("Next move:"),
1326 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1328 h
= pgn_history_by_n(g
->hp
, g
->hindex
- 1);
1329 snprintf(buf
, sizeof(buf
), "%s",
1330 (h
&& h
->move
) ? h
->move
1331 : (LINES
< 24) ? _("empty") : _("not available"));
1334 if (h
&& ((h
->comment
) || h
->nag
[0])) {
1335 strncat(buf
, _(" (Annotated"), sizeof(buf
)-1);
1340 strncat(buf
, (n
) ? ",+" : " (+", sizeof(buf
)-1);
1345 strncat(buf
, (n
) ? ",-" : " (-", sizeof(buf
)-1);
1350 strncat(buf
, ")", sizeof(buf
)-1);
1352 mvwprintw(historyw
, ((LINES
< 24) ? 3 : 4), 1, "%s %-*s",
1353 (LINES
< 24) ? _("Prev.:") : _("Prev move:"),
1354 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1357 void do_validate_move(char *move
)
1359 struct userdata_s
*d
= gp
->data
;
1363 if (TEST_FLAG(d
->flags
, CF_HUMAN
)) {
1364 if ((n
= pgn_parse_move(gp
, d
->b
, &move
, &frfr
)) != E_PGN_OK
) {
1365 invalid_move(d
->n
+ 1, n
, move
);
1369 update_time_control(gp
);
1370 pgn_history_add(gp
, d
->b
, move
);
1371 pgn_switch_turn(gp
);
1374 if ((n
= pgn_validate_move(gp
, d
->b
, &move
, &frfr
)) != E_PGN_OK
) {
1375 invalid_move(d
->n
+ 1, n
, move
);
1379 add_engine_command(gp
, ENGINE_THINKING
, "%s\n",
1380 (config
.engine_protocol
== 1) ? frfr
: move
);
1383 d
->sp
.srow
= d
->sp
.scol
= d
->sp
.icon
= 0;
1385 if (config
.validmoves
)
1386 pgn_reset_valid_moves(d
->b
);
1388 if (TEST_FLAG(gp
->flags
, GF_GAMEOVER
))
1389 d
->mode
= MODE_HISTORY
;
1391 SET_FLAG(d
->flags
, CF_MODIFIED
);
1395 update_history_window(gp
);
1396 update_board_window(gp
);
1400 void do_promotion_piece_finalize(WIN
*win
)
1402 char *p
, *str
= win
->data
;
1404 if (pgn_piece_to_int(win
->c
) == -1)
1407 p
= str
+ strlen(str
);
1408 *p
++ = toupper(win
->c
);
1410 do_validate_move(str
);
1415 static void move_to_engine(GAME g
)
1417 struct userdata_s
*d
= g
->data
;
1421 if (config
.validmoves
&&
1422 !d
->b
[RANKTOBOARD(d
->sp
.row
)][FILETOBOARD(d
->sp
.col
)].valid
)
1425 str
= Malloc(MAX_SAN_MOVE_LEN
+ 1);
1426 snprintf(str
, MAX_SAN_MOVE_LEN
+ 1, "%c%i%c%i",
1427 _("abcdefgh")[d
->sp
.scol
- 1],
1428 d
->sp
.srow
, _("abcdefgh")[d
->sp
.col
- 1], d
->sp
.row
);
1430 piece
= pgn_piece_to_int(d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
);
1432 if (piece
== PAWN
&& (d
->sp
.row
== 8 || d
->sp
.row
== 1)) {
1433 construct_message(_("Select Pawn Promotion Piece"), _ ("R/N/B/Q"), 1, 1, NULL
, NULL
,
1434 str
, do_promotion_piece_finalize
, 0, 0, "%s", _("R = Rook, N = Knight, B = Bishop, Q = Queen"));
1438 do_validate_move(str
);
1442 static char *clock_to_char(long n
)
1444 static char buf
[16];
1445 int h
= 0, m
= 0, s
= 0;
1448 m
= (n
% 3600) / 60;
1449 s
= (n
% 3600) % 60;
1450 snprintf(buf
, sizeof(buf
), "%.2i:%.2i:%.2i", h
, m
, s
);
1454 static char *timeval_to_char(struct timeval t
, long limit
)
1457 int h
= 0, m
= 0, s
= 0;
1458 int n
= limit
? abs(limit
- t
.tv_sec
) : 0;
1461 m
= (n
% 3600) / 60;
1462 s
= (n
% 3600) % 60;
1463 snprintf(buf
, sizeof(buf
), "%.2i:%.2i:%.2i", h
, m
, s
);
1467 static char *time_control_status(struct clock_s
*clk
)
1469 static char buf
[80] = {0};
1473 if (clk
->tc
[clk
->tcn
][0] && clk
->tc
[clk
->tcn
+ 1][1])
1474 snprintf(buf
, sizeof(buf
), " M%.2i/%s", abs(clk
->tc
[clk
->tcn
][0] - clk
->move
),
1475 clock_to_char(clk
->tc
[clk
->tcn
+ 1][1]));
1476 else if (!clk
->incr
)
1481 strncat(buf
, " I", sizeof(buf
)-1);
1482 strncat(buf
, itoa(clk
->incr
, buf
), sizeof(buf
)-1);
1488 void update_status_window(GAME g
)
1492 char tmp
[15] = {0}, *engine
, *mode
;
1498 struct userdata_s
*d
= g
->data
;
1502 if (!curses_initialized
)
1505 getmaxyx(statusw
, maxy
, maxx
);
1512 wchar_t *loadfilew
= loadfile
[0] ? str_etc (loadfile
, w
, 1) : str_to_wchar (_ ("not available"));
1513 mvwprintw(statusw
, y
++, 1, "%*s %-*ls", 7, _("File:"), w
, loadfilew
);
1515 snprintf(buf
, len
, "%i %s %i", gindex
+ 1, _("of"), gtotal
);
1516 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Game:"), w
, buf
);
1521 if (config
.details
) {
1526 if (TEST_FLAG(d
->flags
, CF_DELETE
)) {
1534 if (TEST_FLAG(g
->flags
, GF_PERROR
)) {
1542 if (TEST_FLAG(d
->flags
, CF_MODIFIED
)) {
1550 pgn_config_get(PGN_STRICT_CASTLING
, &n
);
1560 if (TEST_FLAG(d
->flags
, CF_PERL
)) {
1570 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Flags:"), w
, (tmp
[0]) ? tmp
: "-");
1574 mode
= _("move history");
1583 mode
= _("(empty value)");
1587 snprintf(buf
, len
- 1, "%*s %s", 7, _("Mode:"), mode
);
1589 if (d
->mode
== MODE_PLAY
) {
1590 if (TEST_FLAG(d
->flags
, CF_HUMAN
))
1591 strncat(buf
, _(" (human/human)"), len
- 1);
1592 else if (TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
))
1593 strncat(buf
, _(" (engine/engine)"), len
- 1);
1595 strncat(buf
, (d
->play_mode
== PLAY_EH
) ?
1596 _(" (engine/human)") : _(" (human/engine)"), len
- 1);
1600 mvwprintw(statusw
, y
++, 1, "%-*s", len
, buf
);
1604 switch (d
->engine
->status
) {
1605 case ENGINE_THINKING
:
1606 engine
= _("pondering...");
1609 engine
= _("ready");
1611 case ENGINE_INITIALIZING
:
1612 engine
= _("initializing...");
1614 case ENGINE_OFFLINE
:
1615 engine
= _("offline");
1618 engine
= _("(empty value)");
1623 engine
= _("offline");
1625 mvwprintw(statusw
, y
, 1, "%*s %-*s", 7, _("Engine:"), w
, " ");
1626 wattron(statusw
, CP_STATUS_ENGINE
);
1627 mvwaddstr(statusw
, y
++, 9, engine
);
1628 wattroff(statusw
, CP_STATUS_ENGINE
);
1630 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Turn:"), w
,
1631 (g
->turn
== WHITE
) ? _("white") : _("black"));
1633 strncpy(tmp
, _("white"), sizeof(tmp
)-1);
1634 tmp
[0] = toupper(tmp
[0]);
1635 snprintf(t
, sizeof(t
), "%s%s",
1636 timeval_to_char(d
->wclock
.elapsed
, d
->wclock
.tc
[d
->wclock
.tcn
][1]),
1637 time_control_status(&d
->wclock
));
1638 mvwprintw(statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1640 strncpy(tmp
, _("black"), sizeof(tmp
)-1);
1641 tmp
[0] = toupper(tmp
[0]);
1642 snprintf(t
, sizeof(t
), "%s%s",
1643 timeval_to_char(d
->bclock
.elapsed
, d
->bclock
.tc
[d
->bclock
.tcn
][1]),
1644 time_control_status(&d
->bclock
));
1645 mvwprintw(statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1647 mvwprintw(statusw
, y
++, 1, "%*s %-*s", 7, _("Total:"), w
,
1648 clock_to_char(d
->elapsed
.tv_sec
));
1650 // for (i = 0; i < STATUS_WIDTH; i++)
1651 // mvwprintw(stdscr, STATUS_HEIGHT, i, " ");
1654 status
.notify
= str_to_wchar(_("Type F1 for help"));
1656 wattron(stdscr
, CP_STATUS_NOTIFY
);
1657 for (i
= (config
.boardleft
) ? BOARD_WIDTH
: 0;
1658 i
< ((config
.boardleft
) ? COLS
: STATUS_WIDTH
); i
++)
1659 mvwprintw(stdscr
, STATUS_HEIGHT
, i
, " ");
1660 mvwprintw(stdscr
, STATUS_HEIGHT
, CENTERX(STATUS_WIDTH
, status
.notify
)
1661 + ((config
.boardleft
) ? BOARD_WIDTH
: 0),
1662 "%ls", status
.notify
);
1663 wattroff(stdscr
, CP_STATUS_NOTIFY
);
1666 wchar_t *translate_tag_name(const char *tag
)
1668 if (!strcmp (tag
, "Event"))
1669 return str_to_wchar (translatable_tag_names
[0]);
1670 else if (!strcmp (tag
, "Site"))
1671 return str_to_wchar (translatable_tag_names
[1]);
1672 else if (!strcmp (tag
, "Date"))
1673 return str_to_wchar (translatable_tag_names
[2]);
1674 else if (!strcmp (tag
, "Round"))
1675 return str_to_wchar (translatable_tag_names
[3]);
1676 else if (!strcmp (tag
, "White"))
1677 return str_to_wchar (translatable_tag_names
[4]);
1678 else if (!strcmp (tag
, "Black"))
1679 return str_to_wchar (translatable_tag_names
[5]);
1680 else if (!strcmp (tag
, "Result"))
1681 return str_to_wchar (translatable_tag_names
[6]);
1683 return str_to_wchar (tag
);
1686 void update_tag_window(TAG
**t
)
1691 for (i
= 0; t
[i
]; i
++) {
1692 wchar_t *namewc
= translate_tag_name(t
[i
]->name
);
1700 w
= TAG_WIDTH
- namel
- 4;
1702 for (i
= 0; t
[i
] && i
< TAG_HEIGHT
- 3; i
++) {
1703 wchar_t *namewc
= translate_tag_name(t
[i
]->name
);
1704 wchar_t *valuewc
= str_etc(t
[i
]->value
, w
, 0);
1706 mvwprintw(tagw
, (i
+ 2), 1, "%*ls: %-*ls", namel
, namewc
, w
, valuewc
);
1711 for (; i
< TAG_HEIGHT
- 3; i
++)
1712 mvwprintw(tagw
, (i
+ 2), 1, "%*s", namel
+ w
+ 2, " ");
1715 void append_enginebuf(GAME g
, char *line
)
1718 struct userdata_s
*d
= g
->data
;
1720 if (d
->engine
->enginebuf
)
1721 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++);
1723 if (i
>= LINES
- 3) {
1724 free(d
->engine
->enginebuf
[0]);
1726 for (i
= 0; d
->engine
->enginebuf
[i
+1]; i
++)
1727 d
->engine
->enginebuf
[i
] = d
->engine
->enginebuf
[i
+1];
1729 d
->engine
->enginebuf
[i
] = strdup(line
);
1732 d
->engine
->enginebuf
= Realloc(d
->engine
->enginebuf
, (i
+ 2) * sizeof(char *));
1733 d
->engine
->enginebuf
[i
++] = strdup(line
);
1734 d
->engine
->enginebuf
[i
] = NULL
;
1738 void update_engine_window(GAME g
)
1741 struct userdata_s
*d
= g
->data
;
1743 if (!d
->engine
|| !d
->engine
->enginebuf
)
1746 wmove(enginew
, 0, 0);
1749 if (d
->engine
->enginebuf
) {
1750 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++)
1751 mvwprintw(enginew
, i
+ 2, 1, "%s", d
->engine
->enginebuf
[i
]);
1754 window_draw_title(enginew
, _("Engine IO Window"), COLS
, CP_MESSAGE_TITLE
,
1758 void update_all(GAME g
)
1760 struct userdata_s
*d
= g
->data
;
1763 * In the middle of a macro. Don't update the screen.
1765 if (macro_match
!= -1)
1769 * No need to update when the engine window is being shown.
1771 if (enginep
&& panel_hidden(enginep
) == ERR
) {
1777 wmove(boardw
, ROWTOMATRIX(d
->c_row
), COLTOMATRIX(d
->c_col
));
1778 update_board_window(g
);
1779 update_status_window(g
);
1780 update_history_window(g
);
1781 update_tag_window(g
->tag
);
1782 update_engine_window(g
);
1787 static void game_next_prev(GAME g
, int n
, int count
)
1793 if (gindex
+ count
> gtotal
- 1) {
1795 gindex
= gtotal
- 1;
1803 if (gindex
- count
< 0) {
1807 gindex
= gtotal
- 1;
1816 static void delete_game(int which
)
1821 struct userdata_s
*d
;
1823 for (i
= 0; i
< gtotal
; i
++) {
1826 if (i
== which
|| TEST_FLAG(d
->flags
, CF_DELETE
)) {
1827 free_userdata_once(game
[i
]);
1832 g
= Realloc(g
, (gi
+ 1) * sizeof(GAME
*));
1833 g
[gi
] = Calloc(1, sizeof(struct game_s
));
1834 memcpy(g
[gi
], game
[i
], sizeof(struct game_s
));
1835 g
[gi
]->tag
= game
[i
]->tag
;
1836 g
[gi
]->history
= game
[i
]->history
;
1837 g
[gi
]->hp
= game
[i
]->hp
;
1845 if (which
+ 1 >= gtotal
)
1846 gindex
= gtotal
- 1;
1851 gindex
= gtotal
- 1;
1854 gp
->hp
= gp
->history
;
1858 * FIXME find across multiple games.
1860 static int find_move_exp(GAME g
, regex_t r
, int which
, int count
)
1868 incr
= (which
== 0) ? -1 : 1;
1870 for (i
= g
->hindex
+ incr
- 1, found
= 0; ; i
+= incr
) {
1871 if (i
== g
->hindex
- 1)
1874 if (i
>= pgn_history_total(g
->hp
))
1877 i
= pgn_history_total(g
->hp
) - 1;
1880 ret
= regexec(&r
, g
->hp
[i
]->move
, 0, 0, 0);
1883 if (count
== ++found
) {
1888 if (ret
!= REG_NOMATCH
) {
1889 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
1890 cmessage(_("Error Matching Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
1899 static int toggle_delete_flag(int n
)
1902 struct userdata_s
*d
= game
[n
]->data
;
1904 TOGGLE_FLAG(d
->flags
, CF_DELETE
);
1907 for (i
= x
= 0; i
< gtotal
; i
++) {
1910 if (TEST_FLAG(d
->flags
, CF_DELETE
))
1915 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
1917 CLEAR_FLAG(d
->flags
, CF_DELETE
);
1924 static int find_game_exp(char *str
, int which
, int count
)
1926 char *nstr
= NULL
, *exp
= NULL
;
1930 char buf
[255] = {0}, *tmp
;
1933 int incr
= (which
== 0) ? -(1) : 1;
1935 strncpy(buf
, str
, sizeof(buf
)-1);
1938 if (strstr(tmp
, ":") != NULL
) {
1939 nstr
= strsep(&tmp
, ":");
1941 if ((ret
= regcomp(&nexp
, nstr
,
1942 REG_ICASE
|REG_EXTENDED
|REG_NOSUB
)) != 0) {
1943 regerror(ret
, &nexp
, errbuf
, sizeof(errbuf
));
1944 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
1952 while (*exp
&& isspace(*exp
))
1958 if ((ret
= regcomp(&vexp
, exp
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
1959 regerror(ret
, &vexp
, errbuf
, sizeof(errbuf
));
1960 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
1967 for (g
= gindex
+ incr
, found
= 0; ; g
+= incr
) {
1978 for (t
= 0; game
[g
]->tag
[t
]; t
++) {
1980 if (regexec(&nexp
, game
[g
]->tag
[t
]->name
, 0, 0, 0) == 0) {
1981 if (regexec(&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0) {
1982 if (count
== ++found
) {
1990 if (regexec(&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0) {
1991 if (count
== ++found
) {
2013 * Updates the notification line in the status window then refreshes the
2016 void update_status_notify(GAME g
, char *fmt
, ...)
2019 #ifdef HAVE_VASPRINTF
2026 if (status
.notify
) {
2027 free(status
.notify
);
2028 status
.notify
= NULL
;
2035 #ifdef HAVE_VASPRINTF
2036 vasprintf(&line
, fmt
, ap
);
2038 vsnprintf(line
, sizeof(line
), fmt
, ap
);
2043 free(status
.notify
);
2045 status
.notify
= str_to_wchar(line
);
2047 #ifdef HAVE_VASPRINTF
2052 int rav_next_prev(GAME g
, BOARD b
, int n
)
2056 if ((!g
->ravlevel
&& g
->hindex
&& g
->hp
[g
->hindex
- 1]->rav
== NULL
) ||
2057 (!g
->ravlevel
&& !g
->hindex
&& g
->hp
[g
->hindex
]->rav
== NULL
) ||
2058 (g
->ravlevel
&& g
->hp
[g
->hindex
]->rav
== NULL
))
2061 g
->rav
= Realloc(g
->rav
, (g
->ravlevel
+ 1) * sizeof(RAV
));
2062 g
->rav
[g
->ravlevel
].hp
= g
->hp
;
2063 g
->rav
[g
->ravlevel
].flags
= g
->flags
;
2064 g
->rav
[g
->ravlevel
].fen
= pgn_game_to_fen(g
, b
);
2065 g
->rav
[g
->ravlevel
].hindex
= g
->hindex
;
2066 g
->hp
= (!g
->ravlevel
) ? (g
->hindex
) ? g
->hp
[g
->hindex
- 1]->rav
: g
->hp
[g
->hindex
]->rav
: g
->hp
[g
->hindex
]->rav
;
2069 pgn_board_update(g
, b
, g
->hindex
+ 1);
2073 if (g
->ravlevel
- 1 < 0)
2078 pgn_board_init_fen(g
, b
, g
->rav
[g
->ravlevel
].fen
);
2079 free(g
->rav
[g
->ravlevel
].fen
);
2080 g
->hp
= g
->rav
[g
->ravlevel
].hp
;
2081 g
->flags
= g
->rav
[g
->ravlevel
].flags
;
2082 g
->hindex
= g
->rav
[g
->ravlevel
].hindex
;
2086 static void draw_window_decor()
2088 move_panel(boardp
, 0,
2089 (config
.boardleft
) ? 0 : COLS
- BOARD_WIDTH
);
2090 move_panel(historyp
, LINES
- HISTORY_HEIGHT
,
2091 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
: 0 :
2092 (MEGA_BOARD
) ? 0 : COLS
- HISTORY_WIDTH
);
2093 move_panel(statusp
, 0,
2094 (config
.boardleft
) ? BOARD_WIDTH
: 0);
2095 move_panel(tagp
, STATUS_HEIGHT
+ 1,
2096 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
:
2099 wbkgd(boardw
, CP_BOARD_WINDOW
);
2100 wbkgd(statusw
, CP_STATUS_WINDOW
);
2101 window_draw_title(statusw
, _("Game Status"), STATUS_WIDTH
,
2102 CP_STATUS_TITLE
, CP_STATUS_BORDER
);
2103 wbkgd(tagw
, CP_TAG_WINDOW
);
2104 window_draw_title(tagw
, _("Roster Tags"), TAG_WIDTH
, CP_TAG_TITLE
,
2106 wbkgd(historyw
, CP_HISTORY_WINDOW
);
2107 window_draw_title(historyw
, _("Move History"), HISTORY_WIDTH
,
2108 CP_HISTORY_TITLE
, CP_HISTORY_BORDER
);
2111 static void do_window_resize()
2113 if (LINES
< 23 || COLS
< 74)
2116 resizeterm(LINES
, COLS
);
2117 wresize(boardw
, BOARD_HEIGHT
, BOARD_WIDTH
);
2118 wresize(historyw
, HISTORY_HEIGHT
, HISTORY_WIDTH
);
2119 wresize(statusw
, STATUS_HEIGHT
, STATUS_WIDTH
);
2120 wresize(tagw
, TAG_HEIGHT
, TAG_WIDTH
);
2121 wmove(boardw
, 0, 0);
2123 wmove(historyw
, 0, 0);
2124 wclrtobot(historyw
);
2127 wmove(statusw
, 0, 0);
2129 draw_window_decor();
2135 memset(&clock_timer
, 0, sizeof(struct itimerval
));
2136 setitimer(ITIMER_REAL
, &clock_timer
, NULL
);
2139 void start_clock(GAME g
)
2141 struct userdata_s
*d
= g
->data
;
2143 if (clock_timer
.it_interval
.tv_usec
)
2146 memset(&d
->elapsed
, 0, sizeof(struct timeval
));
2147 clock_timer
.it_value
.tv_sec
= 0;
2148 clock_timer
.it_value
.tv_usec
= 100000;
2149 clock_timer
.it_interval
.tv_sec
= 0;
2150 clock_timer
.it_interval
.tv_usec
= 100000;
2151 setitimer(ITIMER_REAL
, &clock_timer
, NULL
);
2154 static void update_clocks()
2157 struct userdata_s
*d
;
2158 struct itimerval it
;
2161 getitimer(ITIMER_REAL
, &it
);
2163 for (i
= 0; i
< gtotal
; i
++) {
2166 if (d
&& d
->mode
== MODE_PLAY
) {
2167 if (d
->paused
== 1 || TEST_FLAG(d
->flags
, CF_NEW
))
2169 else if (d
->paused
== -1) {
2170 if (game
[i
]->side
== game
[i
]->turn
) {
2176 update_clock(game
[i
], it
);
2184 update_status_window(gp
);
2190 #define SKIP_SPACE(str) { while (isspace(*str)) str++; }
2192 static int parse_clock_time(char **str
)
2247 static int parse_clock_input(struct clock_s
*clk
, char *str
, int *incr
)
2269 memset(clk
, 0, sizeof(struct clock_s
));
2273 if (strncasecmp(p
, "SD", 2) == 0) {
2279 n
= parse_clock_time(&p
);
2293 /* Sudden death without a previous time control. */
2299 n
= parse_clock_time(&p
);
2305 message(ERROR_STR
, ANY_KEY_STR
, "%s (%i)", _("Maximum number of time controls reached"), MAX_TC
);
2310 clk
->tc
[tc
++][1] = n
;
2325 clk
->tc
[clk
->tcn
][1] = (n
<= clk
->elapsed
.tv_sec
) ? clk
->elapsed
.tv_sec
+ n
: n
;
2333 n
= parse_clock_time(&p
);
2353 static int parse_which_clock(struct clock_s
*clk
, char *str
)
2358 memcpy(&tmp
, clk
, sizeof(struct clock_s
));
2360 if (parse_clock_input(&tmp
, str
, &incr
)) {
2361 cmessage(ERROR_STR
, ANY_KEY_STR
, _("Invalid clock specification"));
2365 memcpy(clk
, &tmp
, sizeof(struct clock_s
));
2366 clk
->tc
[clk
->tcn
][1] += incr
;
2370 void do_clock_input_finalize(WIN
*win
)
2372 struct userdata_s
*d
= gp
->data
;
2373 struct input_data_s
*in
= win
->data
;
2383 if (tolower(*p
) == 'w') {
2386 if (parse_which_clock(&d
->wclock
, p
))
2389 else if (tolower(*p
) == 'b') {
2392 if (parse_which_clock(&d
->bclock
, p
))
2396 if (parse_which_clock(&d
->wclock
, p
))
2399 if (parse_which_clock(&d
->bclock
, p
))
2403 if (!d
->wclock
.tc
[0][1] && !d
->bclock
.tc
[0][1])
2404 CLEAR_FLAG(d
->flags
, CF_CLOCK
);
2406 SET_FLAG(d
->flags
, CF_CLOCK
);
2413 void do_engine_command_finalize(WIN
*win
)
2415 struct userdata_s
*d
= gp
->data
;
2416 struct input_data_s
*in
= win
->data
;
2427 x
= d
->engine
->status
;
2428 send_to_engine(gp
, -1, "%s\n", in
->str
);
2429 d
->engine
->status
= x
;
2436 void do_board_details()
2438 config
.details
= (config
.details
) ? 0 : 1;
2441 void do_toggle_strict_castling()
2445 pgn_config_get(PGN_STRICT_CASTLING
, &n
);
2448 pgn_config_set(PGN_STRICT_CASTLING
, 1);
2450 pgn_config_set(PGN_STRICT_CASTLING
, 0);
2453 void do_play_set_clock()
2455 struct input_data_s
*in
;
2457 in
= Calloc(1, sizeof(struct input_data_s
));
2458 in
->efunc
= do_clock_input_finalize
;
2459 construct_input(_("Set Clock"), NULL
, 1, 1,
2460 _ ("Format: [W | B] [+]T[+I] | ++I | M/T [M/T [...] [SD/T]] [+I]\n" \
2461 "T = time (hms), I = increment, M = moves per, SD = sudden death\ne.g., 30m or 4m+12s or 35/90m SD/30m"),
2462 NULL
, NULL
, 0, in
, INPUT_HIST_CLOCK
, -1);
2465 void do_play_toggle_human()
2467 struct userdata_s
*d
= gp
->data
;
2469 TOGGLE_FLAG(d
->flags
, CF_HUMAN
);
2471 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && pgn_history_total(gp
->hp
)) {
2472 if (init_chess_engine(gp
))
2476 CLEAR_FLAG(d
->flags
, CF_ENGINE_LOOP
);
2479 d
->engine
->status
= ENGINE_READY
;
2482 void do_play_toggle_engine()
2484 struct userdata_s
*d
= gp
->data
;
2486 TOGGLE_FLAG(d
->flags
, CF_ENGINE_LOOP
);
2487 CLEAR_FLAG(d
->flags
, CF_HUMAN
);
2489 if (d
->engine
&& TEST_FLAG(d
->flags
, CF_ENGINE_LOOP
)) {
2490 char *fen
= pgn_game_to_fen (gp
, d
->b
);
2492 pgn_board_update(gp
, d
->b
,
2493 pgn_history_total(gp
->hp
));
2494 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
2500 * This will send a command to the engine skipping the command queue.
2502 void do_play_send_command()
2504 struct userdata_s
*d
= gp
->data
;
2505 struct input_data_s
*in
;
2507 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
) {
2508 if (init_chess_engine(gp
))
2512 in
= Calloc(1, sizeof(struct input_data_s
));
2513 in
->efunc
= do_engine_command_finalize
;
2514 construct_input(_("Engine Command"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
, INPUT_HIST_ENGINE
, -1);
2517 void do_play_switch_turn()
2519 struct userdata_s *d = gp->data;
2521 pgn_switch_side(gp);
2522 pgn_switch_turn(gp);
2524 if (!TEST_FLAG(d->flags, CF_HUMAN))
2525 add_engine_command(gp, -1,
2526 (gp->side == WHITE) ? "white\n" : "black\n");
2528 update_status_window(gp);
2531 void do_play_toggle_eh_mode()
2533 struct userdata_s
*d
= gp
->data
;
2535 if (!TEST_FLAG(d
->flags
, CF_HUMAN
)) {
2537 pgn_switch_side(gp
, TRUE
);
2538 d
->play_mode
= (d
->play_mode
) ? PLAY_HE
: PLAY_EH
;
2539 if (gp
->side
== BLACK
)
2540 update_status_notify(gp
, _("Press 'g' to start the game"));
2542 rotate
= (rotate
) ? FALSE
: TRUE
;
2545 message(NULL
, ANY_KEY_STR
,
2546 _("You may only switch sides at the start of the \n"
2547 "game. Press ^K or ^N to begin a new game."));
2553 struct userdata_s
*d
= gp
->data
;
2555 if (!pgn_history_total(gp
->hp
))
2559 if (gp
->hindex
- keycount
< 0)
2563 gp
->hindex
-= (keycount
* 2) -1;
2565 gp
->hindex
-= keycount
* 2;
2569 if (gp
->hindex
- 2 < 0)
2579 pgn_history_free(gp
->hp
, gp
->hindex
);
2580 gp
->hindex
= pgn_history_total(gp
->hp
);
2581 pgn_board_update(gp
, d
->b
, gp
->hindex
);
2583 if (d
->engine
&& d
->engine
->status
== ENGINE_READY
) {
2584 char *fen
= pgn_game_to_fen(gp
, d
->b
);
2586 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
2588 d
->engine
->status
= ENGINE_READY
;
2591 update_history_window(gp
);
2594 pgn_switch_side(gp
, FALSE
);
2599 void do_play_toggle_pause()
2601 struct userdata_s
*d
= gp
->data
;
2603 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && gp
->turn
!=
2609 d
->paused
= (d
->paused
) ? 0 : 1;
2614 struct userdata_s
*d
= gp
->data
;
2616 if (TEST_FLAG(d
->flags
, CF_HUMAN
))
2619 if (fm_loaded_file
&& gp
->side
!= gp
->turn
) {
2620 pgn_switch_side(gp
, FALSE
);
2621 add_engine_command(gp
, ENGINE_THINKING
, "black\n");
2624 add_engine_command(gp
, ENGINE_THINKING
, "go\n");
2626 // Completa la función para que permita seguir jugando al usarla.
2627 // Complete the function to allow continue playing when using.
2628 if (gp
->side
== gp
->turn
)
2629 pgn_switch_side(gp
, FALSE
);
2634 void do_play_config_command()
2639 for (x
= 0; config
.keys
[x
]; x
++) {
2640 if (config
.keys
[x
]->c
== input_c
) {
2641 switch (config
.keys
[x
]->type
) {
2643 add_engine_command(gp
, -1, "%s\n",
2644 config
.keys
[x
]->str
);
2650 add_engine_command(gp
, -1,
2651 "%s %i\n", config
.keys
[x
]->str
, keycount
);
2658 for (w
= 0; w
< keycount
; w
++)
2659 add_engine_command(gp
, -1,
2660 "%s\n", config
.keys
[x
]->str
);
2668 update_status_notify(gp
, NULL
);
2671 void do_play_cancel_selected()
2673 struct userdata_s
*d
= gp
->data
;
2675 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
2677 pgn_reset_valid_moves(d
->b
);
2678 update_status_notify(gp
, NULL
);
2681 void do_play_commit()
2683 struct userdata_s
*d
= gp
->data
;
2685 pushkey
= keycount
= 0;
2686 update_status_notify(gp
, NULL
);
2688 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) &&
2689 (!d
->engine
|| d
->engine
->status
== ENGINE_THINKING
))
2695 d
->sp
.row
= d
->c_row
;
2696 d
->sp
.col
= d
->c_col
;
2699 rotate_position(SP_POSITION
);
2700 rotate_position(SPS_POSITION
);
2705 // Completa la función para que permita seguir jugando cuando se carga un
2706 // archivo pgn (con juego no terminado) que inicie con turno del lado
2708 // Complete the function to allow continue playing when loading a file
2709 // pgn (with unfinished game) you start to turn black side.
2710 if (gp
->side
!= gp
->turn
)
2711 pgn_switch_side(gp
, FALSE
);
2713 if (rotate
&& d
->sp
.icon
)
2714 rotate_position(SPS_POSITION
);
2716 // Envia comando 'go' a Polyglot en el primer movimiento debido a que
2717 // polyglot no envia el movimiento y cboard se queda esperando.
2718 // Send command 'go' to the first movement Polyglot because cboard
2719 // waits to send polyglot movement and this does not make.
2720 if (config
.fmpolyglot
&&
2721 ((gp
->side
== WHITE
&& !gp
->hindex
) || fm_loaded_file
))
2722 add_engine_command(gp
, ENGINE_THINKING
, "go\n");
2725 fm_loaded_file
= FALSE
;
2728 void do_play_select()
2730 struct userdata_s
*d
= gp
->data
;
2732 if (!TEST_FLAG(d
->flags
, CF_HUMAN
) && (!d
->engine
||
2733 d
->engine
->status
== ENGINE_OFFLINE
)) {
2734 if (init_chess_engine(gp
))
2738 if (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
)
2742 do_play_cancel_selected ();
2745 rotate_position(CURSOR_POSITION
);
2747 d
->sp
.icon
= d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
;
2749 if (pgn_piece_to_int(d
->sp
.icon
) == OPEN_SQUARE
) {
2754 if (((islower(d
->sp
.icon
) && gp
->turn
!= BLACK
)
2755 || (isupper(d
->sp
.icon
) && gp
->turn
!= WHITE
))) {
2757 char *str
= Malloc (512);
2759 for (k
= play_keys
; *k
; k
++) {
2760 if ((*k
)->f
== do_play_toggle_eh_mode
)
2764 snprintf (str
, 512, _("It is not your turn to move. You may switch playing sides by pressing \"%lc\"."),
2765 *k
? (*k
)->c
: '?');
2766 message(NULL
, ANY_KEY_STR
, "%s", str
);
2771 if (pgn_history_total(gp
->hp
)) {
2772 message(NULL
, ANY_KEY_STR
, "%s", _("It is not your turn to move. You can switch sides "));
2777 if (pgn_tag_find(gp
->tag
, "FEN") != E_PGN_ERR
)
2780 add_engine_command(gp
, ENGINE_READY
, "black\n");
2781 pgn_switch_turn(gp
);
2783 if (gp
->side
!= BLACK
)
2784 pgn_switch_side(gp
);
2789 d
->sp
.srow
= d
->c_row
;
2790 d
->sp
.scol
= d
->c_col
;
2792 if (config
.validmoves
)
2793 pgn_find_valid_moves(gp
, d
->b
, d
->sp
.scol
, d
->sp
.srow
);
2796 rotate_position(CURSOR_POSITION
);
2797 rotate_position(SPS_POSITION
);
2800 CLEAR_FLAG(d
->flags
, CF_NEW
);
2804 /* FIXME: keys with the same function should comma deliminated. */
2805 static wchar_t *build_help(struct key_s
**keys
)
2807 int i
, nlen
= 1, len
, t
, n
;
2808 wchar_t *buf
= NULL
, *wc
= NULL
;
2814 for (i
= len
= t
= 0; keys
[i
]; i
++) {
2819 if (wcslen(keys
[i
]->key
) > nlen
) {
2820 nlen
= wcslen(keys
[i
]->key
);
2828 if (wcslen(keys
[i
]->d
) > len
)
2829 len
= wcslen(keys
[i
]->d
);
2837 buf
= Malloc(t
*sizeof(wchar_t));
2840 for (i
= 0; keys
[i
]; i
++) {
2845 n
= wcslen(keys
[i
]->key
);
2855 wcscat(buf
, keys
[i
]->key
);
2856 p
= buf
+ wcslen(buf
);
2867 wcscat(buf
, keys
[i
]->d
);
2870 wc
= str_to_wchar ("*");
2875 wc
= str_to_wchar ("\n");
2878 p
= buf
+ wcslen(buf
);
2884 void do_more_help(WIN
*);
2885 void do_main_help(WIN
*win
)
2891 buf
= build_help(play_keys
);
2892 construct_message(_("Play Mode Keys (* = can take a repeat count)"), ANY_KEY_STR
, 0, 0,
2893 NULL
, NULL
, buf
, do_more_help
, 0, 1, "%ls", buf
);
2896 buf
= build_help(history_keys
);
2897 construct_message(_("History Mode Keys (* = can take a repeat count)"), ANY_KEY_STR
, 0, 0,
2898 NULL
, NULL
, buf
, do_more_help
, 0, 1, "%ls", buf
);
2901 buf
= build_help(edit_keys
);
2902 construct_message(_("Edit Mode Keys (* = can take a repeat count)"), ANY_KEY_STR
, 0, 0,
2903 NULL
, NULL
, buf
, do_more_help
, 0, 1, "%ls", buf
);
2906 buf
= build_help(global_keys
);
2907 construct_message(_("Global Game Keys (* = can take a repeat count)"), ANY_KEY_STR
, 0, 0,
2908 NULL
, NULL
, buf
, do_more_help
, 0, 1, "%ls", buf
);
2915 void do_more_help(WIN
*win
)
2917 if (win
->c
== KEY_F(1) || win
->c
== CTRL_KEY('g'))
2918 construct_message(_("Command Key Index"),
2919 _ ("p/h/e/g or any other key to quit"), 0, 0,
2920 NULL
, NULL
, NULL
, do_main_help
, 0, 0, "%s",
2922 "p - play mode keys\n"
2923 "h - history mode keys\n"
2924 "e - board edit mode keys\n"
2925 "g - global game keys"
2931 wchar_t *buf
= build_help(play_keys
);
2933 construct_message(_("Play Mode Keys (* = can take a repeat count)"),
2934 ANY_KEY_STR
, 0, 0, NULL
, NULL
,
2935 buf
, do_more_help
, 0, 1, "%ls", buf
);
2938 void do_play_history_mode()
2940 struct userdata_s
*d
= gp
->data
;
2942 if (!pgn_history_total(gp
->hp
) ||
2943 (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
))
2946 d
->mode
= MODE_HISTORY
;
2947 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
2950 void do_play_edit_mode()
2952 struct userdata_s
*d
= gp
->data
;
2954 if (pgn_history_total(gp
->hp
))
2957 pgn_board_init_fen(gp
, d
->b
, NULL
);
2959 d
->mode
= MODE_EDIT
;
2962 void do_edit_insert_finalize(WIN
*win
)
2964 struct userdata_s
*d
= win
->data
;
2966 if (pgn_piece_to_int(win
->c
) == -1)
2969 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
= win
->c
;
2972 void do_edit_select()
2974 struct userdata_s
*d
= gp
->data
;
2979 d
->sp
.icon
= d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
;
2981 if (pgn_piece_to_int(d
->sp
.icon
) == OPEN_SQUARE
) {
2986 d
->sp
.srow
= d
->c_row
;
2987 d
->sp
.scol
= d
->c_col
;
2990 void do_edit_commit()
2993 struct userdata_s
*d
= gp
->data
;
2995 pushkey
= keycount
= 0;
2996 update_status_notify(gp
, NULL
);
3001 d
->sp
.row
= d
->c_row
;
3002 d
->sp
.col
= d
->c_col
;
3003 p
= d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
;
3004 d
->b
[RANKTOBOARD(d
->sp
.row
)][FILETOBOARD(d
->sp
.col
)].icon
= p
;
3005 d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
=
3006 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3007 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3010 void do_edit_delete()
3012 struct userdata_s
*d
= gp
->data
;
3015 d
->b
[RANKTOBOARD(d
->sp
.srow
)][FILETOBOARD(d
->sp
.scol
)].icon
=
3016 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3018 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
=
3019 pgn_int_to_piece(gp
->turn
, OPEN_SQUARE
);
3021 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3024 void do_edit_cancel_selected()
3026 struct userdata_s
*d
= gp
->data
;
3028 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3030 update_status_notify(gp
, NULL
);
3033 void do_edit_switch_turn()
3035 pgn_switch_turn(gp
);
3038 void do_edit_toggle_castle()
3040 struct userdata_s
*d
= gp
->data
;
3042 castling_state(gp
, d
->b
, RANKTOBOARD(d
->c_row
),
3043 FILETOBOARD(d
->c_col
),
3044 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].icon
, 1);
3047 void do_edit_insert()
3049 struct userdata_s
*d
= gp
->data
;
3051 construct_message(_("Insert Piece"), _("P=pawn, R=rook, N=knight, B=bishop, "), 0, 0, NULL
, NULL
,
3052 d
->b
, do_edit_insert_finalize
, 0, 0, "%s", _("Type the piece letter to insert. Lowercase "));
3055 void do_edit_enpassant()
3057 struct userdata_s
*d
= gp
->data
;
3059 if (d
->c_row
== 6 || d
->c_row
== 3) {
3060 pgn_reset_enpassant(d
->b
);
3061 d
->b
[RANKTOBOARD(d
->c_row
)][FILETOBOARD(d
->c_col
)].enpassant
= 1;
3067 wchar_t *buf
= build_help(edit_keys
);
3069 construct_message(_("Edit Mode Keys (* = can take a repeat count)"),
3070 ANY_KEY_STR
, 0, 0, NULL
, NULL
,
3071 buf
, do_more_help
, 0, 1, "%ls", buf
);
3076 struct userdata_s
*d
= gp
->data
;
3077 char *fen
= pgn_game_to_fen(gp
, d
->b
);
3080 pgn_tag_add(&gp
->tag
, "FEN", fen
);
3082 pgn_tag_add(&gp
->tag
, "SetUp", "1");
3083 pgn_tag_sort(gp
->tag
);
3084 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3085 d
->mode
= MODE_PLAY
;
3088 void really_do_annotate_finalize(struct input_data_s
*in
,
3089 struct userdata_s
*d
)
3091 HISTORY
*h
= in
->data
;
3101 len
= strlen(in
->str
);
3102 h
->comment
= Realloc(h
->comment
, len
+1);
3103 strncpy(h
->comment
, in
->str
, len
);
3104 h
->comment
[len
] = 0;
3109 SET_FLAG(d
->flags
, CF_MODIFIED
);
3112 void do_annotate_finalize(WIN
*win
)
3114 struct userdata_s
*d
= gp
->data
;
3115 struct input_data_s
*in
= win
->data
;
3117 really_do_annotate_finalize(in
, d
);
3120 void do_find_move_exp_finalize(int init
, int which
)
3123 struct userdata_s
*d
= gp
->data
;
3124 static int firstrun
;
3129 if (init
|| !firstrun
) {
3133 if ((ret
= regcomp(&r
, moveexp
, REG_EXTENDED
|REG_NOSUB
)) != 0) {
3134 regerror(ret
, &r
, errbuf
, sizeof(errbuf
));
3135 cmessage(_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s", errbuf
);
3142 if ((n
= find_move_exp(gp
, r
,
3143 (which
== -1) ? 0 : 1, (keycount
) ? keycount
: 1)) == -1)
3147 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3150 void do_find_move_exp(WIN
*win
)
3152 struct input_data_s
*in
= win
->data
;
3157 strncpy(moveexp
, in
->str
, sizeof(moveexp
)-1);
3158 moveexp
[sizeof(moveexp
)-1] = 0;
3159 do_find_move_exp_finalize(1, which
);
3167 void do_move_jump_finalize(int n
)
3169 struct userdata_s
*d
= gp
->data
;
3171 if (n
< 0 || n
> (pgn_history_total(gp
->hp
) / 2))
3175 update_status_notify(gp
, NULL
);
3176 gp
->hindex
= (n
) ? n
* 2 - 1 : n
* 2;
3177 pgn_board_update(gp
, d
->b
, gp
->hindex
);
3180 void do_move_jump(WIN
*win
)
3182 struct input_data_s
*in
= win
->data
;
3184 if (!in
->str
|| !isinteger(in
->str
)) {
3192 do_move_jump_finalize(atoi(in
->str
));
3197 struct history_menu_s
{
3205 void free_history_menu_data(struct history_menu_s
**h
)
3212 for (i
= 0; h
[i
]; i
++) {
3220 void get_history_data(HISTORY
**hp
, struct history_menu_s
***menu
, int m
,
3224 int t
= pgn_history_total(hp
);
3225 char buf
[MAX_SAN_MOVE_LEN
+ 4];
3227 struct history_menu_s
**hmenu
= *menu
;
3230 for (n
= 0; hmenu
[n
]; n
++);
3234 for (i
= 0; i
< t
; i
++) {
3235 hmenu
= Realloc(hmenu
, (n
+ 2) * sizeof(struct history_menu_s
*));
3236 hmenu
[n
] = Malloc(sizeof(struct history_menu_s
));
3237 snprintf(buf
, sizeof(buf
), "%c%s%s", (turn
== WHITE
) ? 'W' : 'B',
3238 hp
[i
]->move
, (hp
[i
]->comment
|| hp
[i
]->nag
[0]) ? " !" : "");
3239 hmenu
[n
]->line
= strdup(buf
);
3240 hmenu
[n
]->hindex
= i
;
3241 hmenu
[n
]->indent
= 0;
3242 hmenu
[n
]->ravlevel
= depth
;
3243 hmenu
[n
]->move
= (n
&& depth
> hmenu
[n
-1]->ravlevel
) ? m
++ : m
;
3250 get_history_data(hp
[i
]->rav
, &hmenu
, m
, turn
);
3251 for (n
= 0; hmenu
[n
]; n
++);
3259 turn
= (turn
== WHITE
) ? BLACK
: WHITE
;
3265 void history_draw_update(struct menu_input_s
*m
)
3268 struct userdata_s
*d
= g
->data
;
3270 g
->hindex
= m
->selected
+ 1;
3271 update_cursor(g
, m
->selected
);
3272 pgn_board_update(g
, d
->b
, m
->selected
+ 1);
3275 struct menu_item_s
**get_history_items(WIN
*win
)
3277 struct menu_input_s
*m
= win
->data
;
3279 struct userdata_s
*d
= g
->data
;
3280 struct history_menu_s
**hm
= d
->data
;
3281 struct menu_item_s
**items
= m
->items
;
3285 get_history_data(g
->history
, &hm
, 0,
3286 TEST_FLAG(g
->flags
, GF_BLACK_OPENING
));
3287 m
->selected
= g
->hindex
- 1;
3289 if (m
->selected
< 0)
3292 m
->draw_exit_func
= history_draw_update
;
3298 for (i
= 0; items
[i
]; i
++)
3305 for (i
= 0; hm
[i
]; i
++) {
3306 items
= Realloc(items
, (i
+2) * sizeof(struct menu_item_s
*));
3307 items
[i
] = Malloc(sizeof(struct menu_item_s
));
3308 items
[i
]->name
= hm
[i
]->line
;
3309 items
[i
]->value
= NULL
;
3310 items
[i
]->selected
= 0;
3321 void history_menu_quit(struct menu_input_s
*m
)
3326 void history_menu_exit(WIN
*win
)
3329 struct userdata_s
*d
= g
->data
;
3330 struct history_menu_s
**hm
= d
->data
;
3336 for (i
= 0; hm
[i
]; i
++) {
3346 void history_menu_next(struct menu_input_s
*m
)
3349 struct userdata_s
*d
= g
->data
;
3350 struct history_menu_s
**hm
= d
->data
;
3353 for (t
= 0; hm
[t
]; t
++);
3355 if (m
->selected
+ 1 == t
)
3358 n
= hm
[m
->selected
+ 1]->hindex
;
3365 void history_menu_prev(struct menu_input_s
*m
)
3368 struct userdata_s
*d
= g
->data
;
3369 struct history_menu_s
**hm
= d
->data
;
3372 for (t
= 0; hm
[t
]; t
++);
3374 if (m
->selected
- 1 < 0)
3377 n
= hm
[m
->selected
- 1]->hindex
;
3383 void history_menu_help(struct menu_input_s
*m
)
3385 message("History Menu Help", ANY_KEY_STR
, "%s",
3387 " UP/DOWN - previous/next menu item\n"
3388 " HOME/END - first/last menu item\n"
3389 " PGDN/PGUP - next/previous page\n"
3390 " a-zA-Z0-9 - jump to item\n"
3391 " CTRL-a - annotate the selected move\n"
3392 " ENTER - view annotation\n"
3393 " CTRL-d - toggle board details\n"
3394 " ESCAPE/M - return to move history"
3398 void do_annotate_move(HISTORY
*hp
)
3401 struct input_data_s
*in
;
3403 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Editing Annotation for"), hp
->move
);
3404 in
= Calloc(1, sizeof(struct input_data_s
));
3406 in
->efunc
= do_annotate_finalize
;
3407 construct_input(buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
3408 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
, CTRL_KEY('T'), in
, -1, -1);
3411 void history_menu_view_annotation(struct menu_input_s
*m
)
3416 view_annotation(g
->history
[m
->selected
]);
3419 void history_menu_annotate_finalize(WIN
*win
)
3421 struct input_data_s
*in
= win
->data
;
3422 GAME g
= in
->moredata
;
3423 struct userdata_s
*d
= g
->data
;
3424 struct history_menu_s
**hm
= d
->data
;
3426 really_do_annotate_finalize(in
, d
);
3427 free_history_menu_data(hm
);
3429 get_history_data(g
->history
, &hm
, 0, TEST_FLAG(g
->flags
, GF_BLACK_OPENING
));
3431 pushkey
= REFRESH_MENU
;
3434 void history_menu_annotate(struct menu_input_s
*m
)
3438 struct input_data_s
*in
;
3439 HISTORY
*hp
= g
->history
[m
->selected
]; // FIXME RAV
3441 snprintf(buf
, sizeof(buf
), "%s \"%s\"", _("Editing Annotation for"), hp
->move
);
3442 in
= Calloc(1, sizeof(struct input_data_s
));
3444 in
->moredata
= m
->data
;
3445 in
->efunc
= history_menu_annotate_finalize
;
3446 construct_input(buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
3447 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
, CTRL_KEY('T'), in
, -1, -1);
3450 void history_menu_details(struct menu_input_s
*m
)
3456 void history_menu_print(WIN
*win
)
3458 struct menu_input_s
*m
= win
->data
;
3460 struct userdata_s
*d
= g
->data
;
3461 struct history_menu_s
**hm
= d
->data
;
3462 struct history_menu_s
*h
= hm
[m
->top
];
3464 char *p
= m
->item
->name
;
3465 int line
= m
->print_line
- 2;
3467 * Solaris 5.9 doesn't have wattr_get() or any function that requires an
3476 for (total
= 0; hm
[total
]; total
++);
3478 wattr_get(win
->w
, &attrs
, &pair
, NULL
);
3479 wattroff(win
->w
, COLOR_PAIR(pair
));
3481 mvwaddch(win
->w
, m
->print_line
, 1,
3482 *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
));
3485 if (h
->hindex
== 0 && line
== 0)
3486 waddch(win
->w
, ACS_ULCORNER
| CP_HISTORY_MENU_LG
);
3487 else if ((!hm
[h
->hindex
+ (win
->rows
- 5) + 1] && line
== win
->rows
- 5) ||
3488 (m
->top
+ line
== total
- 1))
3489 waddch(win
->w
, ACS_LLCORNER
| CP_HISTORY_MENU_LG
);
3490 else if (hm
[m
->top
+ 1]->ravlevel
!= h
->ravlevel
|| !h
->ravlevel
)
3491 waddch(win
->w
, ACS_LTEE
| CP_HISTORY_MENU_LG
);
3493 waddch(win
->w
, ACS_VLINE
| CP_HISTORY_MENU_LG
);
3496 wattron(win
->w
, COLOR_PAIR(pair
) | attrs
);
3499 for (i
= 2; *p
; p
++, i
++)
3500 waddch(win
->w
, (*p
== '!') ? *p
| A_BOLD
: *p
);
3502 while (i
++ < win
->cols
- 2)
3503 waddch(win
->w
, ' ');
3506 void history_menu(GAME g
)
3508 struct menu_key_s
**keys
= NULL
;
3510 add_menu_key(&keys
, KEY_ESCAPE
, history_menu_quit
);
3511 add_menu_key(&keys
, 'M', history_menu_quit
);
3512 add_menu_key(&keys
, KEY_UP
, history_menu_prev
);
3513 add_menu_key(&keys
, KEY_DOWN
, history_menu_next
);
3514 add_menu_key(&keys
, KEY_F(1), history_menu_help
);
3515 add_menu_key(&keys
, CTRL_KEY('a'), history_menu_annotate
);
3516 add_menu_key(&keys
, CTRL_KEY('d'), history_menu_details
);
3517 add_menu_key(&keys
, '\n', history_menu_view_annotation
);
3518 construct_menu(LINES
, TAG_WIDTH
, 0, config
.boardleft
? BOARD_WIDTH
: 0,
3519 _("Move History Tree"), 1, get_history_items
, keys
, g
,
3520 history_menu_print
, history_menu_exit
);
3523 void do_history_menu()
3528 void do_history_half_move_toggle()
3530 movestep
= (movestep
== 1) ? 2 : 1;
3531 update_history_window(gp
);
3534 void do_history_rotate_board()
3536 rotate
= (rotate
) ? FALSE
: TRUE
;
3539 void do_history_jump_next()
3541 struct userdata_s
*d
= gp
->data
;
3543 pgn_history_next(gp
, d
->b
, (keycount
> 0) ?
3544 config
.jumpcount
* keycount
* movestep
:
3545 config
.jumpcount
* movestep
);
3548 void do_history_jump_prev()
3550 struct userdata_s
*d
= gp
->data
;
3552 pgn_history_prev(gp
, d
->b
, (keycount
) ?
3553 config
.jumpcount
* keycount
* movestep
:
3554 config
.jumpcount
* movestep
);
3557 void do_history_prev()
3559 struct userdata_s
*d
= gp
->data
;
3561 pgn_history_prev(gp
, d
->b
,
3562 (keycount
) ? keycount
* movestep
: movestep
);
3565 void do_history_next()
3567 struct userdata_s
*d
= gp
->data
;
3569 pgn_history_next(gp
, d
->b
, (keycount
) ?
3570 keycount
* movestep
: movestep
);
3573 void do_history_mode_finalize(struct userdata_s
*d
)
3576 d
->mode
= MODE_PLAY
;
3579 void do_history_mode_confirm(WIN
*win
)
3581 struct userdata_s
*d
= gp
->data
;
3582 wchar_t str
[] = { win
->c
, 0 };
3584 if (!wcscmp (str
, resume_wchar
)) {
3585 pgn_history_free(gp
->hp
, gp
->hindex
);
3586 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3591 if (pgn_history_rav_new(gp
, d
->b
,
3592 gp
->hindex
) != E_PGN_OK
)
3600 if (!TEST_FLAG(d
->flags
, CF_HUMAN
)) {
3601 char *fen
= pgn_game_to_fen(gp
, d
->b
);
3603 add_engine_command(gp
, ENGINE_READY
, "setboard %s\n", fen
);
3607 do_history_mode_finalize(d
);
3610 void do_history_toggle()
3612 struct userdata_s
*d
= gp
->data
;
3614 // FIXME Resuming from previous history could append to a RAV.
3615 if (gp
->hindex
!= pgn_history_total(gp
->hp
)) {
3617 construct_message(NULL
, _ ("What would you like to do?"), 0, 1,
3618 NULL
, NULL
, NULL
, do_history_mode_confirm
, 0, 0,
3619 _("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."),
3624 if (TEST_FLAG(gp
->flags
, GF_GAMEOVER
))
3628 if (gp
->side
!= gp
->turn
) {
3629 d
->play_mode
= PLAY_EH
;
3632 d
->play_mode
= PLAY_HE
;
3636 do_history_mode_finalize(d
);
3639 void do_history_annotate()
3643 if (n
&& gp
->hp
[n
- 1]->move
)
3648 do_annotate_move(gp
->hp
[n
]);
3651 void do_history_help()
3653 wchar_t *buf
= build_help(history_keys
);
3655 construct_message(_("History Mode Keys (* = can take a repeat count)"),
3656 ANY_KEY_STR
, 0, 0, NULL
, NULL
,
3657 buf
, do_more_help
, 0, 1, "%ls", buf
);
3660 void do_history_find(int which
)
3662 struct input_data_s
*in
;
3665 if (pgn_history_total(gp
->hp
) < 2)
3668 in
= Calloc(1, sizeof(struct input_data_s
));
3669 p
= Malloc(sizeof(int));
3672 in
->efunc
= do_find_move_exp
;
3674 if (!*moveexp
|| which
== 0) {
3675 construct_input(_("Find Move Text Expression"), NULL
, 1, 0, NULL
, NULL
, NULL
,
3676 0, in
, INPUT_HIST_MOVE_EXP
, -1);
3680 do_find_move_exp_finalize(0, which
);
3683 void do_history_find_new()
3688 void do_history_find_prev()
3690 do_history_find(-1);
3693 void do_history_find_next()
3698 void do_history_rav(int which
)
3700 struct userdata_s
*d
= gp
->data
;
3702 rav_next_prev(gp
, d
->b
, which
);
3705 void do_history_rav_next()
3710 void do_history_rav_prev()
3715 void do_history_jump()
3717 struct input_data_s
*in
;
3719 if (pgn_history_total(gp
->hp
) < 2)
3723 in
= Calloc(1, sizeof(struct input_data_s
));
3724 in
->efunc
= do_move_jump
;
3726 construct_input(_("Jump to Move Number"), NULL
, 1, 1, NULL
,
3727 NULL
, NULL
, 0, in
, -1, 0);
3731 do_move_jump_finalize(keycount
);
3734 static void free_userdata_once(GAME g
)
3736 struct userdata_s
*d
= g
->data
;
3744 if (d
->engine
->enginebuf
) {
3747 for (n
= 0; d
->engine
->enginebuf
[n
]; n
++)
3748 free(d
->engine
->enginebuf
[n
]);
3750 free(d
->engine
->enginebuf
);
3753 if (d
->engine
->queue
) {
3756 for (q
= d
->engine
->queue
; *q
; q
++)
3759 free(d
->engine
->queue
);
3777 static void free_userdata()
3781 for (i
= 0; i
< gtotal
; i
++) {
3782 free_userdata_once(game
[i
]);
3783 game
[i
]->data
= NULL
;
3787 void update_loading_window(int n
)
3792 loadingw
= newwin(3, COLS
/ 2, CALCPOSY(3), CALCPOSX(COLS
/ 2));
3793 loadingp
= new_panel(loadingw
);
3794 wbkgd(loadingw
, CP_MESSAGE_WINDOW
);
3797 wmove(loadingw
, 0, 0);
3798 wclrtobot(loadingw
);
3799 wattron(loadingw
, CP_MESSAGE_BORDER
);
3800 box(loadingw
, ACS_VLINE
, ACS_HLINE
);
3801 wattroff(loadingw
, CP_MESSAGE_BORDER
);
3802 mvwprintw(loadingw
, 1, CENTER_INT((COLS
/ 2), 11 +
3803 strlen(itoa(gtotal
, buf
))),
3804 _("Loading... %i%% (%i games)"), n
, gtotal
);
3809 static void init_userdata_once(GAME g
, int n
)
3811 struct userdata_s
*d
= NULL
;
3813 d
= Calloc(1, sizeof(struct userdata_s
));
3815 d
->c_row
= 2, d
->c_col
= 5;
3816 SET_FLAG(d
->flags
, CF_NEW
);
3819 if (pgn_board_init_fen(g
, d
->b
, NULL
) != E_PGN_OK
)
3820 pgn_board_init(d
->b
);
3823 void init_userdata()
3827 for (i
= 0; i
< gtotal
; i
++)
3828 init_userdata_once(game
[i
], i
);
3831 void fix_marks(int *start
, int *end
)
3835 *start
= (*start
< 0) ? 0 : *start
;
3836 *end
= (*end
< 0) ? 0 : *end
;
3838 if (*start
> *end
) {
3844 *end
= (*end
> gtotal
) ? gtotal
: *end
;
3847 void do_new_game_finalize(GAME g
)
3849 struct userdata_s
*d
= g
->data
;
3851 d
->mode
= MODE_PLAY
;
3852 update_status_notify(g
, NULL
);
3855 void do_new_game_from_scratch(WIN
*win
)
3857 wchar_t str
[] = { win
->c
, 0 };
3859 if (wcscmp (str
, yes_wchar
))
3866 add_custom_tags(&gp
->tag
);
3869 do_new_game_finalize(gp
);
3877 add_custom_tags(&gp
->tag
);
3878 init_userdata_once(gp
, gindex
);
3879 do_new_game_finalize(gp
);
3883 void do_game_delete_finalize(int n
)
3885 struct userdata_s
*d
;
3887 delete_game((!n
) ? gindex
: -1);
3889 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3892 void do_game_delete_confirm(WIN
*win
)
3895 wchar_t str
[] = { win
->c
, 0 };
3897 if (wcscmp (str
, yes_wchar
)) {
3902 n
= (int *)win
->data
;
3903 do_game_delete_finalize(*n
);
3907 void do_game_delete()
3911 struct userdata_s
*d
;
3915 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
3921 for (i
= n
= 0; i
< gtotal
; i
++) {
3924 if (TEST_FLAG(d
->flags
, CF_DELETE
))
3929 tmp
= _("Delete the current game?");
3932 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
3936 tmp
= _("Delete all games marked for deletion?");
3939 if (config
.deleteprompt
) {
3940 p
= Malloc(sizeof(int));
3942 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, p
,
3943 do_game_delete_confirm
, 0, 0, tmp
);
3947 do_game_delete_finalize(n
);
3950 void do_find_game_exp_finalize(int which
)
3952 struct userdata_s
*d
= gp
->data
;
3955 if ((n
= find_game_exp(gameexp
, (which
== -1) ? 0 : 1,
3956 (keycount
) ? keycount
: 1)) == -1) {
3957 update_status_notify(gp
, "%s", _("No matches found"));
3964 if (pgn_history_total(gp
->hp
))
3965 d
->mode
= MODE_HISTORY
;
3967 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
3970 void do_find_game_exp(WIN
*win
)
3972 struct input_data_s
*in
= win
->data
;
3977 strncpy(gameexp
, in
->str
, sizeof(gameexp
));
3978 gameexp
[sizeof(gameexp
)-1] = 0;
3983 do_find_game_exp_finalize(c
);
3991 void do_game_jump_finalize(int n
)
3993 struct userdata_s
*d
;
3995 if (--n
> gtotal
- 1 || n
< 0)
4001 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4002 update_status_notify(gp
, NULL
);
4005 void do_game_jump(WIN
*win
)
4007 struct input_data_s
*in
= win
->data
;
4009 if (!in
->str
|| !isinteger(in
->str
)) {
4017 do_game_jump_finalize(atoi(in
->str
));
4022 void do_load_file(WIN
*win
)
4024 struct input_data_s
*in
= win
->data
;
4025 char *tmp
= in
->str
;
4026 struct userdata_s
*d
;
4027 PGN_FILE
*pgn
= NULL
;
4035 if ((tmp
= pathfix(tmp
)) == NULL
)
4038 n
= pgn_open(tmp
, "r", &pgn
);
4040 if (n
== E_PGN_ERR
) {
4041 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
, strerror(errno
));
4044 else if (n
== E_PGN_INVALID
) {
4045 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
, _("Not a regular file"));
4051 if (pgn_parse(pgn
) == E_PGN_ERR
) {
4052 del_panel(loadingp
);
4060 del_panel(loadingp
);
4065 strncpy(loadfile
, tmp
, sizeof(loadfile
));
4066 loadfile
[sizeof(loadfile
)-1] = 0;
4070 if (pgn_history_total(gp
->hp
))
4071 d
->mode
= MODE_HISTORY
;
4073 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4075 fm_loaded_file
= TRUE
;
4086 void do_game_save(WIN
*win
)
4088 struct input_data_s
*in
= win
->data
;
4091 char *tmp
= in
->str
;
4092 char tfile
[FILENAME_MAX
];
4095 struct userdata_s
*d
;
4097 if (!tmp
|| (tmp
= pathfix(tmp
)) == NULL
)
4100 if (pgn_is_compressed(tmp
) == E_PGN_ERR
) {
4101 p
= tmp
+ strlen(tmp
) - 1;
4103 if (*p
!= 'n' || *(p
-1) != 'g' || *(p
-2) != 'p' ||
4105 snprintf(tfile
, sizeof(tfile
), "%s.pgn", tmp
);
4111 * When in edit mode, update the FEN tag.
4114 for (i
= 0; i
< gtotal
; i
++) {
4117 if (d
->mode
== MODE_EDIT
) {
4118 char *fen
= pgn_game_to_fen(game
[i
], d
->b
);
4120 pgn_tag_add(&game
[i
]->tag
, "FEN", fen
);
4128 if (d
->mode
== MODE_EDIT
) {
4129 char *fen
= pgn_game_to_fen(game
[n
], d
->b
);
4131 pgn_tag_add(&game
[n
]->tag
, "FEN", fen
);
4146 void do_get_game_save_input(int n
)
4148 struct input_data_s
*in
= Calloc(1, sizeof(struct input_data_s
));
4149 int *p
= Malloc(sizeof(int));
4151 in
->efunc
= do_game_save
;
4155 construct_input(_("Save Game Filename"), loadfile
, 1, 1, _("Type TAB for file browser"),
4156 file_browser
, NULL
, '\t', in
, INPUT_HIST_FILE
, -1);
4159 void do_game_save_multi_confirm(WIN
*win
)
4162 wchar_t str
[] = { win
->c
, 0 };
4164 if (!wcscmp (str
, current_wchar
))
4166 else if (!wcscmp (str
, all_wchar
))
4169 update_status_notify(gp
, "%s", _("Save game aborted."));
4173 do_get_game_save_input(i
);
4176 void do_global_about()
4178 cmessage("ABOUT", ANY_KEY_STR
, "%s\nUsing %s with %i colors "
4179 "and %i color pairs\n%s",
4180 PACKAGE_STRING
, curses_version(), COLORS
, COLOR_PAIRS
,
4184 void global_game_next_prev(int which
)
4186 struct userdata_s
*d
;
4188 game_next_prev(gp
, (which
== 1) ? 1 : 0,
4189 (keycount
) ? keycount
: 1);
4194 markend
= markstart
+ delete_count
;
4198 markend
= markstart
- delete_count
+ 1;
4199 delete_count
= -1; // to fix gindex in the other direction
4202 fix_marks(&markstart
, &markend
);
4203 do_global_toggle_delete();
4206 if (d
->mode
== MODE_HISTORY
)
4207 pgn_board_update(gp
, d
->b
, gp
->hindex
);
4208 else if (d
->mode
== MODE_PLAY
)
4209 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4212 void do_global_next_game()
4214 global_game_next_prev(1);
4217 void do_global_prev_game()
4219 global_game_next_prev(0);
4222 void global_find(int which
)
4224 struct input_data_s
*in
;
4230 in
= Calloc(1, sizeof(struct input_data_s
));
4231 p
= Malloc(sizeof(int));
4234 in
->efunc
= do_find_game_exp
;
4236 if (!*gameexp
|| which
== 0) {
4237 construct_input(_("Find Game by Tag Expression"), NULL
, 1, 0,
4238 _("[name expression:]value expression"), NULL
, NULL
, 0, in
,
4239 INPUT_HIST_GAME_EXP
, -1);
4243 do_find_game_exp_finalize(which
);
4246 void do_global_find_new()
4251 void do_global_find_next()
4256 void do_global_find_prev()
4261 void do_global_game_jump()
4267 struct input_data_s
*in
;
4269 in
= Calloc(1, sizeof(struct input_data_s
));
4270 in
->efunc
= do_game_jump
;
4271 construct_input(_("Jump to Game Number"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
,
4276 do_game_jump_finalize(keycount
);
4279 void do_global_toggle_delete()
4288 if (keycount
&& delete_count
== 0) {
4290 delete_count
= keycount
;
4291 update_status_notify(gp
, "%s (delete)", status
.notify
);
4295 if (markstart
>= 0 && markend
>= 0) {
4296 for (i
= markstart
; i
< markend
; i
++) {
4297 if (toggle_delete_flag(i
)) {
4302 gindex
= (delete_count
< 0) ? markstart
: i
- 1;
4305 if (toggle_delete_flag(gindex
))
4309 markstart
= markend
= -1;
4311 update_status_window(gp
);
4314 void do_global_delete_game()
4319 void do_global_tag_edit()
4321 struct userdata_s
*d
= gp
->data
;
4323 edit_tags(gp
, d
->b
, 1);
4326 void do_global_tag_view()
4328 struct userdata_s
*d
= gp
->data
;
4330 edit_tags(gp
, d
->b
, 0);
4333 void do_global_resume_game()
4335 struct input_data_s
*in
;
4337 in
= Calloc(1, sizeof(struct input_data_s
));
4338 in
->efunc
= do_load_file
;
4339 construct_input(_("Load Filename"), NULL
, 1, 1, _("Type TAB for file browser"), file_browser
,
4340 NULL
, '\t', in
, INPUT_HIST_FILE
, -1);
4343 void do_global_save_game()
4346 construct_message(NULL
, _("What would you like to do?"), 0, 1,
4347 NULL
, NULL
, NULL
, do_game_save_multi_confirm
, 0, 0,
4348 _("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."),
4349 current_wchar
, all_wchar
);
4353 do_get_game_save_input(-1);
4356 void do_global_new_game()
4361 void do_global_copy_game()
4365 struct userdata_s
*d
;
4367 do_global_new_game();
4369 n
= pgn_tag_total(game
[g
]->tag
);
4371 for (i
= 0; i
< n
; i
++)
4372 pgn_tag_add(&gp
->tag
, game
[g
]->tag
[i
]->name
,
4373 game
[g
]->tag
[i
]->value
);
4375 pgn_board_init_fen (gp
, d
->b
, NULL
);
4376 n
= pgn_history_total(game
[g
]->history
);
4379 for (i
= 0; i
< n
; i
++) {
4381 char move
[MAX_SAN_MOVE_LEN
+1] = {0}, *m
= move
;
4383 strcpy (move
, game
[g
]->history
[i
]->move
);
4384 if (pgn_parse_move(gp
, d
->b
, &m
, &frfr
) != E_PGN_OK
) {
4385 SET_FLAG(gp
->flags
, GF_PERROR
);
4389 pgn_history_add(gp
, d
->b
, move
);
4391 pgn_switch_turn(gp
);
4394 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4397 void do_global_new_all()
4399 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
4400 do_new_game_from_scratch
, 0, 0, "%s", _("Really start a new game from scratch?"));
4403 void do_quit(WIN
*win
)
4405 wchar_t str
[] = { win
->c
, 0 };
4407 if (wcscmp (str
, yes_wchar
))
4413 void do_global_quit()
4415 if (config
.exitdialogbox
)
4416 construct_message(NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
4417 do_quit
, 0, 0, _("Want to Quit?"));
4422 void do_global_toggle_engine_window()
4425 enginew
= newwin(LINES
, COLS
, 0, 0);
4426 enginep
= new_panel(enginew
);
4427 window_draw_title(enginew
, _("Engine IO Window"), COLS
, CP_MESSAGE_TITLE
,
4429 hide_panel(enginep
);
4432 if (panel_hidden(enginep
)) {
4433 update_engine_window(gp
);
4437 hide_panel(enginep
);
4441 void do_global_toggle_board_details()
4446 void do_global_toggle_strict_castling()
4448 do_toggle_strict_castling();
4451 // Global and other keys.
4452 static int globalkeys()
4454 struct userdata_s
*d
= gp
->data
;
4458 * These cannot be modified and other game mode keys cannot conflict with
4464 keypad(boardw
, TRUE
);
4465 wmove(stdscr
, 0, 0);
4469 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
4470 markend
= markstart
= 0;
4474 update_status_notify(gp
, NULL
);
4477 if (config
.validmoves
)
4478 pgn_reset_valid_moves(d
->b
);
4485 keycount
= keycount
* 10 + i
;
4489 update_status_notify(gp
, _("Repeat %i"), keycount
);
4492 if (d
->mode
== MODE_HISTORY
)
4496 d
->c_row
+= keycount
;
4505 if (d
->mode
== MODE_HISTORY
)
4509 d
->c_row
-= keycount
;
4510 update_status_notify(gp
, NULL
);
4520 if (d
->mode
== MODE_HISTORY
)
4524 d
->c_col
-= keycount
;
4533 if (d
->mode
== MODE_HISTORY
)
4537 d
->c_col
+= keycount
;
4549 for (i
= 0; global_keys
[i
]; i
++) {
4550 if (input_c
== global_keys
[i
]->c
) {
4551 (*global_keys
[i
]->f
)();
4562 static void perl_error(const char *fmt
, ...)
4568 vasprintf(&buf
, fmt
, ap
);
4571 message(ERROR_STR
, ANY_KEY_STR
, "%s", buf
);
4575 static void do_perl_finalize(WIN
*win
)
4577 struct input_data_s
*in
= win
->data
;
4579 struct userdata_s
*d
= g
->data
;
4581 char *result
= NULL
;
4585 asprintf(&filename
, "%s/perl.pl", config
.datadir
);
4590 if (perl_init_file(filename
, perl_error
))
4593 arg
= pgn_game_to_fen(g
, d
->b
);
4595 if (perl_call_sub(trim(in
->str
), arg
, &result
))
4598 d
->perlfen
= pgn_game_to_fen(g
, d
->b
);
4599 d
->perlflags
= g
->flags
;
4601 if (pgn_board_init_fen(g
, d
->b
, result
) != E_PGN_OK
) {
4602 message(ERROR_STR
, ANY_KEY_STR
, "%s", _("FEN parse error."));
4603 pgn_board_init_fen(g
, d
->b
, d
->perlfen
);
4604 g
->flags
= d
->perlflags
;
4610 SET_FLAG(d
->flags
, CF_PERL
);
4611 n
= pgn_tag_find(g
->tag
, "FEN");
4614 d
->oldfen
= strdup(g
->tag
[n
]->value
);
4616 pgn_tag_add(&g
->tag
, "FEN", result
);
4617 update_status_notify(g
, "%s", ANY_KEY_STR
);
4628 void do_global_perl()
4630 struct input_data_s
*in
;
4632 in
= Calloc(1, sizeof(struct input_data_s
));
4634 in
->efunc
= do_perl_finalize
;
4635 construct_input(_("PERL Subroutine Filter"), NULL
, 1, 0, NULL
, NULL
, NULL
, 0, in
, INPUT_HIST_PERL
, -1);
4640 * A macro may contain a key that belongs to another macro so macro_match will
4641 * need to be updated to the new index of the matching macro.
4643 static void find_macro(struct userdata_s
*d
)
4648 * Macros can't contain macros when in a window.
4654 for (i
= 0; macros
[i
]; i
++) {
4655 if ((macros
[i
]->mode
== -1 || macros
[i
]->mode
== d
->mode
) &&
4656 input_c
== macros
[i
]->c
) {
4657 input_c
= macros
[i
]->keys
[macros
[i
]->n
++];
4659 if (!macro_depth_n
&& macro_match
> -1) {
4660 macro_depth
= realloc(macro_depth
, (macro_depth_n
+ 1) * sizeof(int));
4661 macro_depth
[macro_depth_n
++] = macro_match
;
4664 macro_depth
= realloc(macro_depth
, (macro_depth_n
+ 1) * sizeof(int));
4665 macro_depth
[macro_depth_n
++] = i
;
4673 * Resets the position in each macro to the first key.
4675 static void reset_macros()
4678 struct userdata_s
*d
= gp
->data
;
4681 if (macro_depth_n
> 0) {
4683 macro_match
= macro_depth
[macro_depth_n
];
4685 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
4688 input_c
= macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
4693 for (i
= 0; macros
[i
]; i
++)
4704 struct userdata_s
*d
;
4707 gindex
= gtotal
- 1;
4711 if (pgn_history_total(gp
->hp
))
4712 d
->mode
= MODE_HISTORY
;
4714 d
->mode
= MODE_PLAY
;
4715 d
->play_mode
= PLAY_HE
;
4718 if (d
->mode
== MODE_HISTORY
)
4719 pgn_board_update(gp
, d
->b
, pgn_history_total(gp
->hp
));
4721 update_status_notify(gp
, "%s", _("Type F1 for help"));
4725 wtimeout(boardw
, WINDOW_TIMEOUT
);
4729 char fdbuf
[8192] = {0};
4731 struct timeval tv
= {0, 0};
4739 for (i
= 0; i
< gtotal
; i
++) {
4742 if (d
->engine
&& d
->engine
->pid
!= -1) {
4743 if (d
->engine
->fd
[ENGINE_IN_FD
] > 2) {
4744 if (d
->engine
->fd
[ENGINE_IN_FD
] > n
)
4745 n
= d
->engine
->fd
[ENGINE_IN_FD
];
4747 FD_SET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
);
4750 if (d
->engine
->fd
[ENGINE_OUT_FD
] > 2) {
4751 if (d
->engine
->fd
[ENGINE_OUT_FD
] > n
)
4752 n
= d
->engine
->fd
[ENGINE_OUT_FD
];
4754 FD_SET(d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
);
4760 if ((n
= select(n
+ 1, &rfds
, &wfds
, NULL
, &tv
)) > 0) {
4761 for (i
= 0; i
< gtotal
; i
++) {
4764 if (d
->engine
&& d
->engine
->pid
!= -1) {
4765 if (FD_ISSET(d
->engine
->fd
[ENGINE_IN_FD
], &rfds
)) {
4766 len
= read(d
->engine
->fd
[ENGINE_IN_FD
], fdbuf
,
4770 if (d
->engine
->iobuf
)
4771 d
->engine
->iobuf
= Realloc(d
->engine
->iobuf
, d
->engine
->len
+ len
+ 1);
4773 d
->engine
->iobuf
= Calloc(1, len
+ 1);
4775 memcpy(&(d
->engine
->iobuf
[d
->engine
->len
]), &fdbuf
, len
);
4776 d
->engine
->len
+= len
;
4777 d
->engine
->iobuf
[d
->engine
->len
] = 0;
4780 * The fdbuf is full or no newline
4781 * was found. So we'll append the next
4782 * read() to this games buffer.
4784 if (d
->engine
->iobuf
[d
->engine
->len
- 1] != '\n')
4787 parse_engine_output(game
[i
], d
->engine
->iobuf
);
4788 free(d
->engine
->iobuf
);
4789 d
->engine
->iobuf
= NULL
;
4792 else if (len
== -1) {
4793 if (errno
!= EAGAIN
) {
4794 cmessage(ERROR_STR
, ANY_KEY_STR
, "Engine read(): %s",
4796 waitpid(d
->engine
->pid
, &n
, 0);
4804 if (FD_ISSET(d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
)) {
4805 if (d
->engine
->queue
)
4806 send_engine_command(game
[i
]);
4813 cmessage(ERROR_STR
, ANY_KEY_STR
, "select(): %s", strerror(errno
));
4822 * This is needed to detect terminal resizing.
4825 if (LINES
!= LINES_OLD
|| COLS
!= COLS_OLD
) {
4832 * Finds the top level window in the window stack so we know what
4833 * window the wget_wch()'ed key belongs to.
4836 for (i
= 0; wins
[i
]; i
++);
4839 wtimeout(wp
, WINDOW_TIMEOUT
);
4848 if (macros
&& macro_match
>= 0) {
4849 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
4852 input_c
= macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
4857 if (wget_wch(wp
, &input_c
) == ERR
|| input_c
== KEY_RESIZE
)
4868 keypad(boardw
, TRUE
);
4869 wmove(stdscr
, 0, 0);
4877 * Run the function associated with the window. When the
4878 * function returns 0 win->efunc is ran (if not NULL) with
4879 * win as the one and only parameter. Then the window is
4882 * The exit function may create another window which will
4883 * mess up the window stack when window_destroy() is called.
4884 * So don't destory the window until the top window is
4885 * destroyable. See window_destroy().
4887 if ((*win
->func
)(win
) == 0) {
4892 window_destroy(win
);
4900 if (!keycount
&& status
.notify
)
4901 update_status_notify(gp
, NULL
);
4904 if (TEST_FLAG(d
->flags
, CF_PERL
)) {
4905 CLEAR_FLAG(d
->flags
, CF_PERL
);
4906 pgn_board_init_fen(gp
, d
->b
, d
->perlfen
);
4907 gp
->flags
= d
->perlflags
;
4909 pgn_tag_add(&gp
->tag
, "FEN", d
->oldfen
);
4911 d
->perlfen
= d
->oldfen
= NULL
;
4917 if (macros
&& macro_match
< 0)
4920 if ((n
= globalkeys()) == 1) {
4921 if (macro_match
== -1)
4931 for (i
= 0; edit_keys
[i
]; i
++) {
4932 if (input_c
== edit_keys
[i
]->c
) {
4933 (*edit_keys
[i
]->f
)();
4939 for (i
= 0; play_keys
[i
]; i
++) {
4940 if (input_c
== play_keys
[i
]->c
) {
4941 (*play_keys
[i
]->f
)();
4946 do_play_config_command();
4949 for (i
= 0; history_keys
[i
]; i
++) {
4950 if (input_c
== history_keys
[i
]->c
) {
4951 (*history_keys
[i
]->f
)();
4962 update_status_notify(gp
, NULL
);
4971 void usage(const char *pn
, int ret
)
4973 fprintf((ret
) ? stderr
: stdout
, "%s", _(
4975 "Usage: cboard [-hvCD] [-u [N]] [-p [-VtRSE] <file>]\n"
4976 " -D Dump libchess debugging info to \"libchess.debug\" (stderr)\n"
4978 "Usage: cboard [-hvC] [-u [N]] [-p [-VtRSE] <file>]\n"
4980 " -p Load PGN file.\n"
4981 " -V Validate a game file.\n"
4982 " -S Validate and output a PGN formatted game.\n"
4983 " -R Like -S but write a reduced PGN formatted game.\n"
4984 " -t Also write custom PGN tags from config file.\n"
4985 " -E Stop processing on file parsing error (overrides config).\n"
4986 " -C Enable strict castling (overrides config).\n"
4987 " -u Enable/disable UTF-8 pieces (1=enable, 0=disable, moverrides config).\n"
4988 " -v Version information.\n"
4989 " -h This help text.\n"));
5001 free(config
.engine_cmd
);
5002 free(config
.pattern
);
5003 free(config
.ccfile
);
5004 free(config
.nagfile
);
5005 free(config
.configfile
);
5008 for (i
= 0; config
.keys
[i
]; i
++) {
5009 free(config
.keys
[i
]->str
);
5010 free(config
.keys
[i
]);
5017 for (i
= 0; config
.einit
[i
]; i
++)
5018 free(config
.einit
[i
]);
5024 pgn_tag_free(config
.tag
);
5026 free(config
.datadir
);
5028 if (curses_initialized
) {
5030 del_panel(historyp
);
5051 static void signal_save_pgn(int sig
)
5055 char *p
= config
.savedirectory
? config
.savedirectory
: config
.datadir
;
5058 asprintf(&buf
, "%s/signal-%i-%li.pgn", p
, sig
, now
);
5060 if (do_game_write(buf
, "w", 0, gtotal
)) {
5061 cmessage(ERROR_STR
, ANY_KEY_STR
, "%s: %s", p
, strerror(errno
));
5062 update_status_notify(gp
, "%s", _("Save game failed."));
5069 void catch_signal(int which
)
5076 if (which
== SIGPIPE
&& quit
)
5079 if (which
== SIGPIPE
)
5080 cmessage(NULL
, ANY_KEY_STR
, "%s", _("Broken pipe. Quitting."));
5090 keypad(boardw
, TRUE
);
5099 signal_save_pgn(which
);
5106 void loading_progress(long total
, long offset
)
5108 int n
= (100 * (offset
/ 100) / (total
/ 100));
5110 if (curses_initialized
)
5111 update_loading_window(n
);
5113 fprintf(stderr
, _("Loading... %i%% (%i games)%c"), n
, gtotal
, '\r');
5118 static void set_defaults()
5120 set_config_defaults();
5122 filetype
= FILE_NONE
;
5123 pgn_config_set(PGN_PROGRESS
, 1024);
5124 pgn_config_set(PGN_PROGRESS_FUNC
, loading_progress
);
5127 int main(int argc
, char *argv
[])
5131 char buf
[FILENAME_MAX
];
5132 char datadir
[FILENAME_MAX
];
5133 int ret
= EXIT_SUCCESS
;
5134 int validate_only
= 0, validate_and_write
= 0;
5135 int write_custom_tags
= 0;
5138 int utf8_pieces
= -1;
5140 setlocale (LC_ALL
, "");
5141 bindtextdomain ("cboard", LOCALE_DIR
);
5142 textdomain ("cboard");
5145 #ifndef HAVE_PROGNAME
5146 __progname
= argv
[0];
5149 if ((config
.pwd
= getpwuid(getuid())) == NULL
)
5150 err(EXIT_FAILURE
, "getpwuid()");
5152 snprintf(datadir
, sizeof(datadir
), "%s/.cboard", config
.pwd
->pw_dir
);
5153 config
.datadir
= strdup(datadir
);
5154 snprintf(buf
, sizeof(buf
), "%s/cc.data", datadir
);
5155 config
.ccfile
= strdup(buf
);
5156 snprintf(buf
, sizeof(buf
), "%s/nag.data", datadir
);
5157 config
.nagfile
= strdup(buf
);
5158 snprintf(buf
, sizeof(buf
), "%s/config", datadir
);
5159 config
.configfile
= strdup(buf
);
5161 if (stat(datadir
, &st
) == -1) {
5162 if (errno
== ENOENT
) {
5163 if (mkdir(datadir
, 0755) == -1)
5164 err(EXIT_FAILURE
, "%s", datadir
);
5167 err(EXIT_FAILURE
, "%s", datadir
);
5172 if (!S_ISDIR(st
.st_mode
))
5173 errx(EXIT_FAILURE
, "%s: %s", datadir
, _("Not a directory."));
5178 while ((opt
= getopt(argc
, argv
, "DCEVtSRhp:vu::")) != -1) {
5180 while ((opt
= getopt(argc
, argv
, "ECVtSRhp:vu::")) != -1) {
5185 unlink("libchess.debug");
5186 pgn_config_set(PGN_DEBUG
, 1);
5190 pgn_config_set(PGN_STRICT_CASTLING
, 1);
5193 write_custom_tags
= 1;
5199 pgn_config_set(PGN_REDUCED
, 1);
5201 validate_and_write
= 1;
5206 printf("%s (%s)\n%s\n", PACKAGE_STRING
, curses_version(),
5210 filetype
= FILE_PGN
;
5211 strncpy(loadfile
, optarg
, sizeof(loadfile
));
5212 loadfile
[sizeof(loadfile
)-1] = 0;
5215 utf8_pieces
= optarg
? atoi (optarg
): 1;
5219 usage(argv
[0], EXIT_SUCCESS
);
5223 if ((validate_only
|| validate_and_write
) && !*loadfile
)
5224 usage(argv
[0], EXIT_FAILURE
);
5226 if (access(config
.configfile
, R_OK
) == 0)
5227 parse_rcfile(config
.configfile
);
5230 pgn_config_set(PGN_STOP_ON_ERROR
, 1);
5232 signal(SIGPIPE
, catch_signal
);
5233 signal(SIGCONT
, catch_signal
);
5234 signal(SIGSTOP
, catch_signal
);
5235 signal(SIGINT
, catch_signal
);
5236 signal(SIGALRM
, catch_signal
);
5237 signal(SIGTERM
, catch_signal
);
5243 if (pgn_open(loadfile
, "r", &pgn
) != E_PGN_OK
)
5244 err(EXIT_FAILURE
, "%s", loadfile
);
5246 ret
= pgn_parse(pgn
);
5250 //ret = parse_fen_file(loadfile);
5252 case FILE_EPD
: // Not implemented.
5255 // No file specified. Empty game.
5256 ret
= pgn_parse(NULL
);
5258 add_custom_tags(&gp
->tag
);
5262 if (validate_only
|| validate_and_write
) {
5263 if (validate_and_write
) {
5264 if (pgn_open("-", "r", &pgn
) != E_PGN_OK
)
5265 err(EXIT_FAILURE
, "pgn_open()");
5267 for (i
= 0; i
< gtotal
; i
++) {
5268 if (write_custom_tags
)
5269 add_custom_tags(&game
[i
]->tag
);
5271 pgn_write(pgn
, game
[i
]);
5276 fm_loaded_file
= TRUE
;
5282 else if (ret
== E_PGN_ERR
)
5285 if (utf8_pieces
!= -1)
5286 config
.utf8_pieces
= utf8_pieces
;
5288 init_wchar_pieces ();
5289 yes_wchar
= str_to_wchar (_("y"));
5290 all_wchar
= str_to_wchar (_("a"));
5291 overwrite_wchar
= str_to_wchar (_("o"));
5292 resume_wchar
= str_to_wchar (_("r"));
5293 current_wchar
= str_to_wchar (_("c"));
5294 append_wchar
= str_to_wchar (_("a"));
5295 translatable_tag_names
[0] = _("Event");
5296 translatable_tag_names
[1] = _("Site");
5297 translatable_tag_names
[2] = _("Date");
5298 translatable_tag_names
[3] = _("Round");
5299 translatable_tag_names
[4] = _("White");
5300 translatable_tag_names
[5] = _("Black");
5301 translatable_tag_names
[6] = _("Result");
5305 * This fixes window resizing in an xterm.
5307 if (getenv("DISPLAY") != NULL
) {
5312 if (initscr() == NULL
)
5313 errx(EXIT_FAILURE
, "%s", _("Could not initialize curses."));
5315 curses_initialized
= 1;
5317 if (LINES
< 23 || COLS
< 74) {
5319 errx(EXIT_FAILURE
, _("Need at least an 74x23 terminal."));
5325 if (has_colors() == TRUE
&& start_color() == OK
)
5328 boardw
= newwin(BOARD_HEIGHT
, BOARD_WIDTH
, 0, COLS
- BOARD_WIDTH
);
5329 boardp
= new_panel(boardw
);
5330 historyw
= newwin(HISTORY_HEIGHT
, HISTORY_WIDTH
, LINES
- HISTORY_HEIGHT
,
5331 COLS
- HISTORY_WIDTH
);
5332 historyp
= new_panel(historyw
);
5333 statusw
= newwin(STATUS_HEIGHT
, STATUS_WIDTH
, 0, 0);
5334 statusp
= new_panel(statusw
);
5335 tagw
= newwin(TAG_HEIGHT
, TAG_WIDTH
, STATUS_HEIGHT
+ 1, 0);
5336 tagp
= new_panel(tagw
);
5337 keypad(boardw
, TRUE
);
5338 // leaveok(boardw, TRUE);
5339 leaveok(tagw
, TRUE
);
5340 leaveok(statusw
, TRUE
);
5341 leaveok(historyw
, TRUE
);
5345 draw_window_decor();
5348 free (w_pawn_wchar
);
5349 free (w_rook_wchar
);
5350 free (w_bishop_wchar
);
5351 free (w_knight_wchar
);
5352 free (w_queen_wchar
);
5353 free (w_king_wchar
);
5354 free (b_pawn_wchar
);
5355 free (b_rook_wchar
);
5356 free (b_bishop_wchar
);
5357 free (b_knight_wchar
);
5358 free (b_queen_wchar
);
5359 free (b_king_wchar
);
5361 free (enpassant_wchar
);
5364 free (overwrite_wchar
);
5365 free (resume_wchar
);
5366 free (current_wchar
);
5367 free (append_wchar
);