1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2019 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 CBOARD_URL "https://gitlab.com/bjk/cboard/wikis"
73 #define COPYRIGHT "Copyright (C) 2002-2019 " PACKAGE_BUGREPORT
74 #define LINE_GRAPHIC(c) ((!config.linegraphics) ? ' ' : c)
75 #define ROWTOMATRIX(r) ((8 - r) * 2 + 2 - 1)
76 #define COLTOMATRIX(c) ((c == 1) ? 1 : c * 4 - 3)
77 #define STATUS_HEIGHT 12
78 #define MEGA_BOARD (LINES >= 50 && COLS >= 144)
79 #define BOARD_HEIGHT_MB 50
80 #define BOARD_WIDTH_MB 98
81 #define STATUS_WIDTH_MB (COLS - BOARD_WIDTH_MB)
82 #define TAG_HEIGHT_MB 31
83 #define TAG_WIDTH_MB (COLS - BOARD_WIDTH_MB)
84 #define HISTORY_HEIGHT_MB (LINES - (STATUS_HEIGHT + TAG_HEIGHT_MB + 1))
85 #define HISTORY_WIDTH_MB (COLS - BOARD_WIDTH_MB)
86 #define BIG_BOARD (LINES >= 40 && COLS >= 112)
87 #define BOARD_HEIGHT ((MEGA_BOARD) ? BOARD_HEIGHT_MB : (BIG_BOARD) ? 34 : 18)
88 #define BOARD_WIDTH ((MEGA_BOARD) ? BOARD_WIDTH_MB : (BIG_BOARD) ? 66 : 34)
89 #define STATUS_WIDTH ((MEGA_BOARD) ? STATUS_WIDTH_MB : COLS - BOARD_WIDTH)
90 #define TAG_HEIGHT ((MEGA_BOARD) ? TAG_HEIGHT_MB : LINES - STATUS_HEIGHT - 1)
91 #define TAG_WIDTH ((MEGA_BOARD) ? TAG_WIDTH_MB : COLS - BOARD_WIDTH)
92 #define HISTORY_HEIGHT ((MEGA_BOARD) ? HISTORY_HEIGHT_MB : LINES - BOARD_HEIGHT)
93 #define HISTORY_WIDTH ((MEGA_BOARD) ? HISTORY_WIDTH_MB : COLS - STATUS_WIDTH)
94 #define MAX_VALUE_WIDTH (COLS - 8)
101 static WINDOW
*boardw
;
102 static PANEL
*boardp
;
105 static WINDOW
*statusw
;
106 static PANEL
*statusp
;
107 static WINDOW
*historyw
;
108 static PANEL
*historyp
;
109 static WINDOW
*loadingw
;
110 static PANEL
*loadingp
;
111 static WINDOW
*enginew
;
112 static PANEL
*enginep
;
114 static char gameexp
[255];
115 static char moveexp
[255];
116 static struct itimerval clock_timer
;
117 static int delete_count
= 0;
118 static int markstart
= -1, markend
= -1;
120 static char loadfile
[FILENAME_MAX
];
122 static wint_t input_c
;
124 // Loaded filename from the command line or from the file input dialog.
128 FILE_NONE
, FILE_PGN
, FILE_FEN
, FILE_EPD
132 static int nag_total
;
133 static int macro_match
;
135 // Primer movimiento de juego cargado
136 // First move loaded game
137 static char fm_loaded_file
= FALSE
;
139 static int COLS_OLD
, LINES_OLD
;
144 wchar_t *notify
; // The status window notification line buffer.
147 static int curses_initialized
;
149 // When in history mode a full step is to the next move of the same playing
150 // side. Half stepping is alternating sides.
153 static wchar_t *w_pawn_wchar
;
154 static wchar_t *w_rook_wchar
;
155 static wchar_t *w_bishop_wchar
;
156 static wchar_t *w_knight_wchar
;
157 static wchar_t *w_queen_wchar
;
158 static wchar_t *w_king_wchar
;
159 static wchar_t *b_pawn_wchar
;
160 static wchar_t *b_rook_wchar
;
161 static wchar_t *b_bishop_wchar
;
162 static wchar_t *b_knight_wchar
;
163 static wchar_t *b_queen_wchar
;
164 static wchar_t *b_king_wchar
;
165 static wchar_t *empty_wchar
;
166 static wchar_t *enpassant_wchar
;
168 static wchar_t *yes_wchar
;
169 static wchar_t *all_wchar
; // do_save_game_overwrite_confirm()
170 static wchar_t *overwrite_wchar
; // do_save_game_overwrite_confirm()
171 static wchar_t *resume_wchar
; // do_history_mode_confirm()
172 static wchar_t *current_wchar
; // do_game_save_multi_confirm()
173 static wchar_t *append_wchar
; // save_pgn()
175 static const char piece_chars
[] = "PpRrNnBbQqKkxx";
176 static char *translatable_tag_names
[7];
177 static const char *f_pieces
[] = {
201 static const bool cb
[8][8] = {
202 {1, 0, 1, 0, 1, 0, 1, 0},
203 {0, 1, 0, 1, 0, 1, 0, 1},
204 {1, 0, 1, 0, 1, 0, 1, 0},
205 {0, 1, 0, 1, 0, 1, 0, 1},
206 {1, 0, 1, 0, 1, 0, 1, 0},
207 {0, 1, 0, 1, 0, 1, 0, 1},
208 {1, 0, 1, 0, 1, 0, 1, 0},
209 {0, 1, 0, 1, 0, 1, 0, 1}
212 static void free_userdata_once (GAME g
);
213 static void do_more_help (WIN
*);
214 static void do_play_help ();
215 static void do_history_help ();
216 static void do_edit_help ();
221 coordofmove (GAME g
, char *move
, char *prow
, char *pcol
)
223 char l
= strlen (move
);
227 *prow
= (g
->turn
== WHITE
) ? 8 : 1;
228 *pcol
= (l
<= 4) ? 7 : 3;
234 while (!isdigit (*move
))
237 *prow
= RANKTOINT (*move
--);
238 *pcol
= FILETOINT (*move
);
241 #define INV_INT(x) (9 - x)
242 #define INV_INT0(x) (7 - x)
244 // Posición por rotación de tablero.
245 // Rotation board position.
247 rotate_position (char *prow
, char *pcol
)
249 *prow
= INV_INT (*prow
);
250 *pcol
= INV_INT (*pcol
);
254 update_cursor (GAME g
, int idx
)
256 int t
= pgn_history_total (g
->hp
);
257 struct userdata_s
*d
= g
->data
;
260 * If not deincremented then r and c would be the next move.
264 if (idx
> t
|| idx
< 0 || !t
|| !g
->hp
[idx
]->move
)
265 d
->c_row
= 2, d
->c_col
= 5;
267 coordofmove (g
, g
->hp
[idx
]->move
, &d
->c_row
, &d
->c_col
);
269 if (d
->mode
== MODE_HISTORY
&& d
->rotate
)
270 rotate_position (&d
->c_row
, &d
->c_col
);
280 if ((fp
= fopen (config
.nagfile
, "r")) == NULL
)
282 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.nagfile
,
287 nags
= Realloc (nags
, (i
+ 2) * sizeof (char *));
288 nags
[i
++] = strdup (_("none"));
293 if (fscanf (fp
, " %[^\n] ", line
) == 1)
295 nags
= Realloc (nags
, (i
+ 2) * sizeof (char *));
296 nags
[i
++] = strdup (line
);
307 edit_nag_toggle_item (struct menu_input_s
*m
)
309 struct input_s
*in
= m
->data
;
310 struct input_data_s
*id
= in
->data
;
311 HISTORY
*h
= id
->data
;
314 if (m
->selected
== 0)
316 for (i
= 0; i
< MAX_PGN_NAG
; i
++)
319 for (i
= 0; m
->items
[i
]; i
++)
320 m
->items
[i
]->selected
= 0;
325 for (i
= 0; i
< MAX_PGN_NAG
; i
++)
327 if (h
->nag
[i
] == m
->selected
)
328 h
->nag
[i
] = m
->selected
= 0;
333 h
->nag
[i
] = m
->selected
;
341 edit_nag_save (struct menu_input_s
*m
)
347 edit_nag_help (struct menu_input_s
*m
)
349 message (_("NAG Menu Keys"), ANY_KEY_STR
, "%s",
350 _(" UP/DOWN - previous/next menu item\n"
351 " HOME/END - first/last menu item\n"
352 " PGDN/PGUP - next/previous page\n"
353 " a-zA-Z0-9 - jump to item\n"
354 " SPACE - toggle selected item\n"
355 " CTRL-X - quit with changes"));
358 struct menu_item_s
**
359 get_nag_items (WIN
* win
)
362 struct menu_input_s
*m
= win
->data
;
363 struct input_s
*in
= m
->data
;
364 struct input_data_s
*id
= in
->data
;
365 struct menu_item_s
**items
= m
->items
;
366 HISTORY
*h
= id
->data
;
370 for (i
= 0; items
[i
]; i
++)
374 for (i
= 0; nags
[i
]; i
++)
376 items
= Realloc (items
, (i
+ 2) * sizeof (struct menu_item_s
*));
377 items
[i
] = Malloc (sizeof (struct menu_item_s
));
378 items
[i
]->name
= nags
[i
];
379 items
[i
]->value
= NULL
;
381 for (n
= 0; n
< MAX_PGN_NAG
; n
++)
385 items
[i
]->selected
= 1;
392 items
[i
]->selected
= 0;
402 nag_print (WIN
* win
)
404 struct menu_input_s
*m
= win
->data
;
406 mvwprintw (win
->w
, m
->print_line
, 1, "%-*s", win
->cols
- 2, m
->item
->name
);
412 struct menu_key_s
**keys
= NULL
;
420 add_menu_key (&keys
, ' ', edit_nag_toggle_item
);
421 add_menu_key (&keys
, CTRL_KEY ('x'), edit_nag_save
);
422 add_menu_key (&keys
, keycode_lookup (global_keys
, do_global_help
),
424 construct_menu (0, 0, -1, -1, _("Numeric Annotation Glyphs"), 1,
425 get_nag_items
, keys
, arg
, nag_print
, NULL
, NULL
);
432 HISTORY
*h
= (HISTORY
*) arg
;
434 char line
[LINE_MAX
] = { 0 };
437 snprintf (buf
, sizeof (buf
), "%s \"%s\"", _("Viewing NAG for"), h
->move
);
445 for (i
= 0; i
< MAX_PGN_NAG
; i
++)
452 if (h
->nag
[i
] >= nag_total
)
453 strncat (line
, itoa (h
->nag
[i
], buf2
), sizeof (line
) - 1);
455 strncat (line
, nags
[h
->nag
[i
]], sizeof (line
) - 1);
457 strncat (line
, "\n", sizeof (line
) - 1);
460 line
[strlen (line
) - 1] = 0;
461 message (buf
, ANY_KEY_STR
, "%s", line
);
466 view_annotation (HISTORY
* h
)
468 char buf
[MAX_SAN_MOVE_LEN
+ strlen (_("Viewing Annotation for")) + 4];
469 int nag
= 0, comment
= 0;
474 if (h
->comment
&& h
->comment
[0])
480 if (!nag
&& !comment
)
483 snprintf (buf
, sizeof (buf
), "%s \"%s\"", _("Viewing Annotation for"),
487 construct_message (buf
,
488 (nag
) ? _("Any other key to continue") : ANY_KEY_STR
,
489 0, 1, (nag
) ? _("Press 'n' to view NAG") : NULL
,
490 (nag
) ? view_nag
: NULL
, (nag
) ? h
: NULL
, NULL
,
491 (nag
) ? 'n' : 0, 0, NULL
, "%s", h
->comment
);
493 construct_message (buf
, _("Any other key to continue"), 0, 1,
494 _("Press 'n' to view NAG"), view_nag
, h
, NULL
, 'n', 0,
495 NULL
, "%s", _("No comment text for this move"));
499 do_game_write (char *filename
, const char *mode
, int start
, int end
)
502 struct userdata_s
*d
;
505 i
= pgn_open (filename
, mode
, &pgn
);
509 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s\n%s", filename
, strerror (errno
));
512 else if (i
== E_PGN_INVALID
)
514 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s\n%s", filename
,
515 _("Not a regular file"));
519 for (i
= (start
== -1) ? 0 : start
; i
< end
; i
++)
522 pgn_write (pgn
, game
[i
]);
523 CLEAR_FLAG (d
->flags
, CF_MODIFIED
);
526 if (pgn_close (pgn
) != E_PGN_OK
)
527 message (ERROR_STR
, ANY_KEY_STR
, "%s", strerror (errno
));
531 strncpy (loadfile
, filename
, sizeof (loadfile
));
532 loadfile
[sizeof (loadfile
) - 1] = 0;
547 do_save_game_overwrite_confirm (WIN
* win
)
549 const char *mode
= "w";
550 struct save_game_s
*s
= win
->data
;
551 wchar_t str
[] = { win
->c
, 0 };
553 if (!wcscmp (str
, append_wchar
))
555 else if (!wcscmp (str
, overwrite_wchar
))
560 if (do_game_write (s
->filename
, mode
, s
->start
, s
->end
))
561 update_status_notify (gp
, "%s", _("Save game failed."));
563 update_status_notify (gp
, "%s", _("Game saved."));
570 /* If the saveindex argument is -1, all games will be saved. Otherwise it's a
574 save_pgn (char *filename
, int saveindex
)
576 char buf
[FILENAME_MAX
];
578 int end
= (saveindex
== -1) ? gtotal
: saveindex
+ 1;
579 struct save_game_s
*s
;
581 if (filename
[0] != '/' && config
.savedirectory
)
583 if (stat (config
.savedirectory
, &st
) == -1)
587 if (mkdir (config
.savedirectory
, 0755) == -1)
589 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s: %s",
590 config
.savedirectory
, strerror (errno
));
596 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s: %s",
597 config
.savedirectory
, strerror (errno
));
602 if (stat (config
.savedirectory
, &st
) == -1)
604 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
,
609 if (!S_ISDIR (st
.st_mode
))
611 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
,
612 _("Not a directory."));
616 snprintf (buf
, sizeof (buf
), "%s/%s", config
.savedirectory
, filename
);
620 if (access (filename
, W_OK
) == 0)
622 s
= Malloc (sizeof (struct save_game_s
));
623 s
->filename
= strdup (filename
);
624 s
->start
= saveindex
;
626 construct_message (NULL
, _("What would you like to do?"), 0, 1, NULL
,
627 NULL
, s
, do_save_game_overwrite_confirm
, 0, 0, NULL
,
628 "%s \"%s\"\nPress \"%ls\" to append to this file, \"%ls\" to overwrite or any other key to cancel.",
629 _("File exists:"), filename
, append_wchar
,
634 if (do_game_write (filename
, "a", saveindex
, end
))
635 update_status_notify (gp
, "%s", _("Save game failed."));
637 update_status_notify (gp
, "%s", _("Game saved."));
641 castling_state (GAME g
, BOARD b
, int row
, int col
, int piece
, int mod
)
643 if (pgn_piece_to_int (piece
) == ROOK
&& col
== 7
645 (TEST_FLAG (g
->flags
, GF_WK_CASTLE
) || mod
) &&
646 pgn_piece_to_int (b
[7][4].icon
) == KING
&& isupper (piece
))
649 TOGGLE_FLAG (g
->flags
, GF_WK_CASTLE
);
652 else if (pgn_piece_to_int (piece
) == ROOK
&& col
== 0
654 (TEST_FLAG (g
->flags
, GF_WQ_CASTLE
) || mod
) &&
655 pgn_piece_to_int (b
[7][4].icon
) == KING
&& isupper (piece
))
658 TOGGLE_FLAG (g
->flags
, GF_WQ_CASTLE
);
661 else if (pgn_piece_to_int (piece
) == ROOK
&& col
== 7
663 (TEST_FLAG (g
->flags
, GF_BK_CASTLE
) || mod
) &&
664 pgn_piece_to_int (b
[0][4].icon
) == KING
&& islower (piece
))
667 TOGGLE_FLAG (g
->flags
, GF_BK_CASTLE
);
670 else if (pgn_piece_to_int (piece
) == ROOK
&& col
== 0
672 (TEST_FLAG (g
->flags
, GF_BQ_CASTLE
) || mod
) &&
673 pgn_piece_to_int (b
[0][4].icon
) == KING
&& islower (piece
))
676 TOGGLE_FLAG (g
->flags
, GF_BQ_CASTLE
);
679 else if (pgn_piece_to_int (piece
) == KING
&& col
== 4
681 (mod
|| (pgn_piece_to_int (b
[7][7].icon
) == ROOK
&&
682 TEST_FLAG (g
->flags
, GF_WK_CASTLE
))
684 (pgn_piece_to_int (b
[7][0].icon
) == ROOK
&&
685 TEST_FLAG (g
->flags
, GF_WQ_CASTLE
))) && isupper (piece
))
689 if (TEST_FLAG (g
->flags
, GF_WK_CASTLE
) ||
690 TEST_FLAG (g
->flags
, GF_WQ_CASTLE
))
691 CLEAR_FLAG (g
->flags
, GF_WK_CASTLE
| GF_WQ_CASTLE
);
693 SET_FLAG (g
->flags
, GF_WK_CASTLE
| GF_WQ_CASTLE
);
697 else if (pgn_piece_to_int (piece
) == KING
&& col
== 4
699 (mod
|| (pgn_piece_to_int (b
[0][7].icon
) == ROOK
&&
700 TEST_FLAG (g
->flags
, GF_BK_CASTLE
))
702 (pgn_piece_to_int (b
[0][0].icon
) == ROOK
&&
703 TEST_FLAG (g
->flags
, GF_BQ_CASTLE
))) && islower (piece
))
707 if (TEST_FLAG (g
->flags
, GF_BK_CASTLE
) ||
708 TEST_FLAG (g
->flags
, GF_BQ_CASTLE
))
709 CLEAR_FLAG (g
->flags
, GF_BK_CASTLE
| GF_BQ_CASTLE
);
711 SET_FLAG (g
->flags
, GF_BK_CASTLE
| GF_BQ_CASTLE
);
719 #define IS_ENPASSANT(c) (c == 'x') ? CP_BOARD_ENPASSANT : isupper(c) ? CP_BOARD_WHITE : CP_BOARD_BLACK
720 #define ATTRS(cp) (cp & (A_BOLD|A_STANDOUT|A_BLINK|A_DIM|A_UNDERLINE|A_INVIS|A_REVERSE))
725 w_pawn_wchar
= str_to_wchar (config
.utf8_pieces
? "♙" : "P");
726 w_rook_wchar
= str_to_wchar (config
.utf8_pieces
? "♖" : "R");
727 w_bishop_wchar
= str_to_wchar (config
.utf8_pieces
? "♗" : "B");
728 w_knight_wchar
= str_to_wchar (config
.utf8_pieces
? "♘" : "N");
729 w_queen_wchar
= str_to_wchar (config
.utf8_pieces
? "♕" : "Q");
730 w_king_wchar
= str_to_wchar (config
.utf8_pieces
? "♔" : "K");
731 b_pawn_wchar
= str_to_wchar (config
.utf8_pieces
? "♟" : "p");
732 b_rook_wchar
= str_to_wchar (config
.utf8_pieces
? "♜" : "r");
733 b_bishop_wchar
= str_to_wchar (config
.utf8_pieces
? "♝" : "b");
734 b_knight_wchar
= str_to_wchar (config
.utf8_pieces
? "♞" : "n");
735 b_queen_wchar
= str_to_wchar (config
.utf8_pieces
? "♛" : "q");
736 b_king_wchar
= str_to_wchar (config
.utf8_pieces
? "♚" : "k");
737 empty_wchar
= str_to_wchar (" ");
738 enpassant_wchar
= str_to_wchar ("x");
742 piece_to_wchar (unsigned char p
)
755 return w_bishop_wchar
;
757 return b_bishop_wchar
;
759 return w_knight_wchar
;
761 return b_knight_wchar
;
763 return w_queen_wchar
;
765 return b_queen_wchar
;
771 return enpassant_wchar
;
778 piece_can_attack (GAME g
, int rank
, int file
)
780 struct userdata_s
*d
= g
->data
;
781 char *m
, *frfr
= NULL
;
783 int row
, col
, p
, v
, pi
, cpi
;
787 rotate_position (&d
->c_row
, &d
->c_col
);
788 rotate_position (&d
->sp
.srow
, &d
->sp
.scol
);
791 v
= d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].valid
;
792 pi
= pgn_piece_to_int (d
->b
[RANKTOBOARD (rank
)][FILETOBOARD (file
)].icon
);
795 pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->sp
.srow
)]
796 [FILETOBOARD (d
->sp
.scol
)].icon
) :
797 pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->c_row
)]
798 [FILETOBOARD (d
->c_col
)].icon
);
800 if (pi
== OPEN_SQUARE
|| cpi
== OPEN_SQUARE
|| !VALIDFILE (file
)
801 || !VALIDRANK (rank
))
805 rotate_position (&d
->c_row
, &d
->c_col
);
806 rotate_position (&d
->sp
.srow
, &d
->sp
.scol
);
814 col
= v
? d
->c_col
: d
->sp
.scol
;
815 row
= v
? d
->c_row
: d
->sp
.srow
;
823 m
= Malloc (MAX_SAN_MOVE_LEN
+ 1);
824 m
[0] = INTTOFILE (file
);
825 m
[1] = INTTORANK (rank
);
826 m
[2] = INTTOFILE (col
);
827 m
[3] = INTTORANK (row
);
834 memcpy (b
, d
->b
, sizeof (BOARD
));
835 p
= b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
;
836 b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
837 pgn_int_to_piece (WHITE
, OPEN_SQUARE
);
838 b
[RANKTOBOARD (row
)][FILETOBOARD (col
)].icon
= p
;
840 e
= pgn_validate_move (g
, b
, &m
, &frfr
);
845 if (e
!= E_PGN_OK
&& pgn_piece_to_int (d
->sp
.icon
) == PAWN
)
847 int n
= (d
->sp
.srow
== 7 && islower (d
->sp
.icon
) && rank
== 5) ? 6 :
848 (d
->sp
.srow
== 2 && isupper (d
->sp
.icon
) && rank
== 4) ? 3 : 0;
850 if (n
&& (file
== d
->c_col
- 1 || file
== d
->c_col
+ 1))
852 memcpy (b
, d
->b
, sizeof (BOARD
));
853 p
= b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
;
854 b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
855 pgn_int_to_piece (WHITE
, OPEN_SQUARE
);
856 b
[RANKTOBOARD (row
)][FILETOBOARD (col
)].icon
= p
;
857 b
[RANKTOBOARD (n
)][FILETOBOARD (d
->sp
.scol
)].enpassant
= 1;
858 m
= Malloc (MAX_SAN_MOVE_LEN
+ 1);
859 m
[0] = INTTOFILE (file
);
860 m
[1] = INTTORANK (rank
);
861 m
[2] = INTTOFILE (col
);
862 m
[3] = INTTORANK (n
);
865 SET_FLAG (g
->flags
, GF_ENPASSANT
);
866 e
= pgn_validate_move (g
, b
, &m
, &frfr
);
867 CLEAR_FLAG (g
->flags
, GF_ENPASSANT
);
878 e
= pgn_validate_move (g
, d
->b
, &m
, &frfr
);
881 if (!strcmp (m
, "O-O") || !strcmp (m
, "O-O-O"))
886 int sf
= FILETOINT (frfr
[0]), sr
= RANKTOINT (frfr
[1]);
887 int df
= FILETOINT (frfr
[2]);
889 pi
= d
->b
[RANKTOBOARD (sr
)][FILETOBOARD (sf
)].icon
;
890 pi
= pgn_piece_to_int (pi
);
891 if (pi
== PAWN
&& sf
== df
)
901 rotate_position (&d
->c_row
, &d
->c_col
);
902 rotate_position (&d
->sp
.srow
, &d
->sp
.scol
);
905 return e
== E_PGN_OK
? 1 : 0;
909 print_piece (WINDOW
* w
, int l
, int c
, char p
)
913 for (i
= 0; i
< 13; i
+= 2)
915 if (p
== piece_chars
[i
] || p
== piece_chars
[i
+ 1])
917 for (y
= 0; y
< 3; y
++)
918 mvwprintw (w
, l
+ y
, c
, "%s", f_pieces
[i
+ ff
+ y
]);
925 for (y
= 0; y
< 3; y
++)
926 mvwprintw (w
, l
+ y
, c
, "%s", f_pieces
[0]);
930 board_prev_move_play (GAME g
)
932 struct userdata_s
*d
= g
->data
;
933 char l
= strlen (d
->pm_frfr
);
937 char q
= (l
> 4) ? 2 : 1;
939 d
->pm_row
= RANKTOINT (d
->pm_frfr
[l
- q
++]);
940 d
->pm_col
= FILETOINT (d
->pm_frfr
[l
- q
++]);
941 d
->ospm_row
= RANKTOINT (d
->pm_frfr
[l
- q
++]);
942 d
->ospm_col
= FILETOINT (d
->pm_frfr
[l
- q
]);
945 rotate_position (&d
->pm_row
, &d
->pm_col
);
946 rotate_position (&d
->ospm_row
, &d
->ospm_col
);
959 board_prev_move_history (GAME g
)
961 struct userdata_s
*d
= g
->data
;
965 char *move
= g
->hp
[g
->hindex
- 1]->move
;
969 if (d
->mode
== MODE_PLAY
)
970 coordofmove (g
, move
, &d
->pm_row
, &d
->pm_col
);
979 d
->ospm_row
= g
->turn
== WHITE
? 8 : 1;
991 HISTORY
*h
= pgn_history_by_n (g
->hp
, g
->hindex
- 2);
994 pgn_board_init_fen (g
, ob
, h
->fen
);
999 for (f
= 0; f
< 8; f
++)
1001 for (r
= 0; r
< 8; r
++)
1003 if (ob
[f
][r
].icon
!= '.' && d
->b
[f
][r
].icon
== '.')
1005 d
->ospm_row
= INV_INT0 (f
) + 1;
1006 d
->ospm_col
= r
+ 1;
1014 if (d
->mode
== MODE_PLAY
)
1015 rotate_position (&d
->pm_row
, &d
->pm_col
);
1017 rotate_position (&d
->ospm_row
, &d
->ospm_col
);
1033 is_the_square (int brow
, int bcol
, int row
, int col
, int prow
, int pcol
)
1035 if ((!BIG_BOARD
&& row
== ROWTOMATRIX (prow
) && col
== COLTOMATRIX (pcol
))
1036 || (BIG_BOARD
&& brow
+ 1 == INV_INT (prow
) && bcol
+ 1 == pcol
))
1043 is_prev_move (struct userdata_s
*d
, int brow
, int bcol
, int row
, int col
)
1045 if (is_the_square (brow
, bcol
, row
, col
, d
->pm_row
, d
->pm_col
))
1052 update_board_window (GAME g
)
1055 int bcol
= 0, brow
= 0;
1056 int l
= config
.coordsyleft
;
1057 int maxy
= BOARD_HEIGHT
, maxx
= BOARD_WIDTH
;
1058 int ncols
= 0, offset
= 1;
1059 int rowr
= (MEGA_BOARD
) ? 6 : (BIG_BOARD
) ? 4 : 2;
1060 int colr
= (MEGA_BOARD
) ? 12 : (BIG_BOARD
) ? 8 : 4;
1061 unsigned coords_y
= 8, cxgc
= 0;
1062 unsigned i
, cpd
= 0;
1063 struct userdata_s
*d
= g
->data
;
1065 if (config
.bprevmove
&& d
->mode
!= MODE_EDIT
)
1067 if (!d
->pm_undo
&& d
->mode
== MODE_PLAY
)
1068 board_prev_move_play (g
);
1070 board_prev_move_history (g
);
1080 if (d
->mode
!= MODE_PLAY
&& d
->mode
!= MODE_EDIT
)
1081 update_cursor (g
, g
->hindex
);
1102 for (row
= 0; row
< maxy
; row
++)
1119 for (col
= 0; col
< maxx
; col
++)
1122 chtype attrs
= 0, old_attrs
= 0;
1127 if (row
== 0 || row
== maxy
- 2)
1130 mvwaddch (boardw
, row
, col
+ l
,
1132 ? ACS_LLCORNER
| CP_BOARD_GRAPHICS
1133 : ACS_ULCORNER
| CP_BOARD_GRAPHICS
));
1134 else if (col
== maxx
- 2)
1135 mvwaddch (boardw
, row
, col
+ l
,
1137 ? ACS_LRCORNER
| CP_BOARD_GRAPHICS
1138 : ACS_URCORNER
| CP_BOARD_GRAPHICS
));
1139 else if (!(col
% colr
))
1140 mvwaddch (boardw
, row
, col
+ l
,
1142 ? ACS_BTEE
| CP_BOARD_GRAPHICS
1143 : ACS_TTEE
| CP_BOARD_GRAPHICS
));
1146 if (col
!= maxx
- 1)
1147 mvwaddch (boardw
, row
, col
+ l
,
1148 LINE_GRAPHIC (ACS_HLINE
| CP_BOARD_GRAPHICS
));
1154 if ((row
% 2) && col
== maxx
- 1 && (coords_y
> 0 && coords_y
< 9))
1156 wattron (boardw
, CP_BOARD_COORDS
);
1158 (BIG_BOARD
) ? row
* ((MEGA_BOARD
) ? 3 : 2)
1159 : row
, (l
) ? 0 : col
, "%d",
1160 (d
->rotate
) ? coords_y
++ : coords_y
--);
1161 wattroff (boardw
, CP_BOARD_COORDS
);
1165 if ((col
== 0 || col
== maxx
- 2) && row
!= maxy
- 1)
1168 mvwaddch (boardw
, row
, col
+ l
,
1169 LINE_GRAPHIC ((col
) ?
1170 ACS_RTEE
| CP_BOARD_GRAPHICS
:
1171 ACS_LTEE
| CP_BOARD_GRAPHICS
));
1173 mvwaddch (boardw
, row
, col
+ l
,
1174 LINE_GRAPHIC (ACS_VLINE
| CP_BOARD_GRAPHICS
));
1179 if ((row
% rowr
) && !(col
% colr
) && row
!= maxy
- 1)
1181 mvwaddch (boardw
, row
, col
+ l
,
1182 LINE_GRAPHIC (ACS_VLINE
| CP_BOARD_GRAPHICS
));
1186 if (!(col
% colr
) && row
!= maxy
- 1)
1188 mvwaddch (boardw
, row
, col
+ l
,
1189 LINE_GRAPHIC (ACS_PLUS
| CP_BOARD_GRAPHICS
));
1198 attrwhich
= (cb
[brow
][bcol
]) ? WHITE
: BLACK
;
1207 if (((ncols
% 2) && !(offset
% 2))
1208 || (!(ncols
% 2) && (offset
% 2)))
1214 if (BIG_BOARD
&& d
->rotate
)
1216 brow
= INV_INT0 (brow
);
1217 bcol
= INV_INT0 (bcol
);
1221 p
= d
->b
[brow
][bcol
].icon
;
1223 p
= d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].icon
;
1225 int pi
= pgn_piece_to_int (p
);
1227 if (config
.details
&&
1230 b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].enpassant
)
1231 || (BIG_BOARD
&& d
->b
[brow
][bcol
].enpassant
)))
1234 attrs
= mix_cp (CP_BOARD_ENPASSANT
,
1236 WHITE
) ? CP_BOARD_WHITE
:
1238 ATTRS (CP_BOARD_ENPASSANT
), A_FG_B_BG
);
1241 if (config
.showattacks
&& config
.details
1242 && piece_can_attack (g
,
1243 BIG_BOARD
? INV_INT0 (brow
) +
1245 BIG_BOARD
? bcol
+ 1 : bcol
))
1247 attrs
= CP_BOARD_ATTACK
;
1252 if (config
.validmoves
&&
1254 && d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].valid
)
1255 || (BIG_BOARD
&& d
->b
[brow
][bcol
].valid
)))
1260 if (attrwhich
== WHITE
)
1261 attrs
= mix_cp (CP_BOARD_MOVES_WHITE
,
1263 ATTRS (CP_BOARD_MOVES_WHITE
),
1266 attrs
= mix_cp (CP_BOARD_MOVES_BLACK
,
1268 ATTRS (CP_BOARD_MOVES_BLACK
),
1271 else if (p
!= 'x' && !can_attack
)
1273 (attrwhich
== WHITE
) ? CP_BOARD_WHITE
: CP_BOARD_BLACK
;
1275 if (BIG_BOARD
&& d
->rotate
)
1277 brow
= INV_INT0 (brow
);
1278 bcol
= INV_INT0 (bcol
);
1281 if (is_the_square (brow
, bcol
, row
, col
, d
->c_row
,
1284 attrs
= mix_cp (CP_BOARD_CURSOR
, IS_ENPASSANT (p
),
1285 ATTRS (CP_BOARD_CURSOR
), B_FG_A_BG
);
1288 else if (is_the_square (brow
, bcol
, row
, col
,
1289 d
->sp
.srow
, d
->sp
.scol
))
1291 attrs
= mix_cp (CP_BOARD_SELECTED
, IS_ENPASSANT (p
),
1292 ATTRS (CP_BOARD_SELECTED
), B_FG_A_BG
);
1295 else if ((is_prev_move (d
, brow
, bcol
, row
, col
) && !valid
)
1296 || (is_the_square (brow
, bcol
, row
, col
,
1297 d
->ospm_row
, d
->ospm_col
)))
1299 attrs
= mix_cp (CP_BOARD_PREVMOVE
, IS_ENPASSANT (p
),
1300 ATTRS (CP_BOARD_PREVMOVE
), B_FG_A_BG
);
1304 if (row
== maxy
- 1)
1309 int n
= is_prev_move (d
, brow
, bcol
, row
, col
);
1310 chtype a
= n
&& !valid
1311 ? CP_BOARD_PREVMOVE
: attrwhich
==
1312 WHITE
? valid
? CP_BOARD_MOVES_WHITE
: CP_BOARD_WHITE
1313 : valid
? CP_BOARD_MOVES_BLACK
: CP_BOARD_BLACK
;
1315 mix_cp (CP_BOARD_ATTACK
, a
, ATTRS (CP_BOARD_ATTACK
),
1321 wmove (boardw
, row
, col
+ ((MEGA_BOARD
) ? 5 : 3) + l
);
1323 mvwaddch (boardw
, row
, col
+ l
, ' ' | attrs
);
1325 if (row
== maxy
- 1 && cxgc
< 8)
1328 "abcdefgh"[(BIG_BOARD
) ? bcol
: bcol
-
1329 1] | CP_BOARD_COORDS
);
1334 if (old_attrs
== -1)
1342 if (pi
!= OPEN_SQUARE
&& p
!= 'x' && !can_attack
)
1344 if (attrwhich
== WHITE
)
1347 attrs
= CP_BOARD_W_W
;
1349 attrs
= CP_BOARD_W_B
;
1354 attrs
= CP_BOARD_B_W
;
1356 attrs
= CP_BOARD_B_B
;
1363 if (config
.details
&& !can_attack
1364 && castling_state (g
, d
->b
,
1365 (d
->rotate
) ? INV_INT0 (brow
+ 1) - 1 : brow
,
1366 (d
->rotate
) ? INV_INT0 (bcol
+ 1) - 1 : bcol
,
1368 attrs
= mix_cp (CP_BOARD_CASTLING
, attrs
,
1369 ATTRS (CP_BOARD_CASTLING
), A_FG_B_BG
);
1373 if (config
.details
&& !can_attack
1374 && castling_state (g
, d
->b
, RANKTOBOARD (brow
),
1375 FILETOBOARD (bcol
), p
, 0))
1377 attrs
= mix_cp (CP_BOARD_CASTLING
, attrs
,
1378 ATTRS (CP_BOARD_CASTLING
),
1385 // FIXME: Reimpresión de piezas(+4).
1388 wattron (boardw
, attrs
);
1391 for (i
= 0; i
< 5; i
++)
1392 mvwprintw (boardw
, i
+ brow
* 6 + 1,
1395 if (pi
!= OPEN_SQUARE
)
1396 print_piece (boardw
, brow
* 6 + 2,
1397 bcol
* 12 + 3 + l
, p
);
1401 print_piece (boardw
, brow
* 4 + 1,
1403 (pi
!= OPEN_SQUARE
) ? p
: 0);
1406 wattroff (boardw
, attrs
);
1412 wattron (boardw
, attrs
);
1414 piece_to_wchar (pi
!=
1415 OPEN_SQUARE
? p
: 0));
1416 wattroff (boardw
, attrs
);
1423 col
+= (MEGA_BOARD
) ? 10 : 6;
1426 waddch (boardw
, ' ' | attrs
);
1446 if (col
!= maxx
- 1)
1447 mvwaddch (boardw
, row
, col
+ l
,
1448 LINE_GRAPHIC (ACS_HLINE
| CP_BOARD_GRAPHICS
));
1471 invalid_move (int n
, int e
, const char *m
)
1473 if (curses_initialized
)
1474 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s \"%s\" (round #%i)",
1476 E_PGN_AMBIGUOUS
) ? _("Ambiguous move") : _("Invalid move"), m
,
1479 warnx ("%s: %s \"%s\" (round #%i)", loadfile
, (e
== E_PGN_AMBIGUOUS
)
1480 ? _("Ambiguous move") : _("Invalid move"), m
, n
);
1486 struct userdata_s
*d
= g
->data
;
1488 SET_FLAG (g
->flags
, GF_GAMEOVER
);
1489 d
->mode
= MODE_HISTORY
;
1494 update_clock (GAME g
, struct itimerval it
)
1496 struct userdata_s
*d
= g
->data
;
1498 if (TEST_FLAG (d
->flags
, CF_CLOCK
) && g
->turn
== WHITE
)
1500 d
->wclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1501 d
->wclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1503 if (d
->wclock
.elapsed
.tv_usec
> 1000000 - 1)
1505 d
->wclock
.elapsed
.tv_sec
+= d
->wclock
.elapsed
.tv_usec
/ 1000000;
1506 d
->wclock
.elapsed
.tv_usec
= d
->wclock
.elapsed
.tv_usec
% 1000000;
1509 if (d
->wclock
.tc
[d
->wclock
.tcn
][1] &&
1510 d
->wclock
.elapsed
.tv_sec
>= d
->wclock
.tc
[d
->wclock
.tcn
][1])
1512 pgn_tag_add (&g
->tag
, (char *) "Result", (char *) "0-1");
1516 else if (TEST_FLAG (d
->flags
, CF_CLOCK
) && g
->turn
== BLACK
)
1518 d
->bclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1519 d
->bclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1521 if (d
->bclock
.elapsed
.tv_usec
> 1000000 - 1)
1523 d
->bclock
.elapsed
.tv_sec
+= d
->bclock
.elapsed
.tv_usec
/ 1000000;
1524 d
->bclock
.elapsed
.tv_usec
= d
->bclock
.elapsed
.tv_usec
% 1000000;
1527 if (d
->bclock
.tc
[d
->bclock
.tcn
][1] &&
1528 d
->bclock
.elapsed
.tv_sec
>= d
->bclock
.tc
[d
->bclock
.tcn
][1])
1530 pgn_tag_add (&g
->tag
, (char *) "Result", (char *) "1-0");
1535 d
->elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1536 d
->elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1538 if (d
->elapsed
.tv_usec
> 1000000 - 1)
1540 d
->elapsed
.tv_sec
+= d
->elapsed
.tv_usec
/ 1000000;
1541 d
->elapsed
.tv_usec
= d
->elapsed
.tv_usec
% 1000000;
1546 update_time_control (GAME g
)
1548 struct userdata_s
*d
= g
->data
;
1549 struct clock_s
*clk
= (g
->turn
== WHITE
) ? &d
->wclock
: &d
->bclock
;
1552 clk
->tc
[clk
->tcn
][1] += clk
->incr
;
1554 if (!clk
->tc
[clk
->tcn
][1])
1559 if (!clk
->tc
[clk
->tcn
][0] || clk
->move
>= clk
->tc
[clk
->tcn
][0])
1562 clk
->tc
[clk
->tcn
+ 1][1] +=
1563 labs (clk
->elapsed
.tv_sec
- clk
->tc
[clk
->tcn
][1]);
1564 memset (&clk
->elapsed
, 0, sizeof (clk
->elapsed
));
1570 update_history_window (GAME g
)
1572 char buf
[HISTORY_WIDTH
- 1];
1575 int t
= pgn_history_total (g
->hp
);
1577 n
= (g
->hindex
+ 1) / 2;
1580 total
= (t
+ 1) / 2;
1585 snprintf (buf
, sizeof (buf
), "%u %s %u%s", n
, _("of"), total
,
1586 (movestep
== 1) ? _(" (ply)") : "");
1588 strncpy (buf
, _("not available"), sizeof (buf
) - 1);
1590 buf
[sizeof (buf
) - 1] = 0;
1591 mvwprintw (historyw
, 2, 1, "%*s %-*s", 10, _("Move:"),
1592 HISTORY_WIDTH
- 14, buf
);
1594 h
= pgn_history_by_n (g
->hp
, g
->hindex
);
1595 snprintf (buf
, sizeof (buf
), "%s",
1596 (h
&& h
->move
) ? h
->move
1597 : (LINES
< 24) ? _("empty") : _("not available"));
1600 if (h
&& ((h
->comment
) || h
->nag
[0]))
1602 strncat (buf
, _(" (Annotated"), sizeof (buf
) - 1);
1608 strncat (buf
, (n
) ? ",+" : " (+", sizeof (buf
) - 1);
1614 strncat (buf
, (n
) ? ",-" : " (-", sizeof (buf
) - 1);
1619 strncat (buf
, ")", sizeof (buf
) - 1);
1621 mvwprintw (historyw
, 3, ((LINES
< 24) ? 17 : 1), "%s %-*s",
1622 (LINES
< 24) ? _("Next:") : _("Next move:"),
1623 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1625 h
= pgn_history_by_n (g
->hp
, g
->hindex
- 1);
1626 snprintf (buf
, sizeof (buf
), "%s",
1627 (h
&& h
->move
) ? h
->move
1628 : (LINES
< 24) ? _("empty") : _("not available"));
1631 if (h
&& ((h
->comment
) || h
->nag
[0]))
1633 strncat (buf
, _(" (Annotated"), sizeof (buf
) - 1);
1639 strncat (buf
, (n
) ? ",+" : " (+", sizeof (buf
) - 1);
1645 strncat (buf
, (n
) ? ",-" : " (-", sizeof (buf
) - 1);
1650 strncat (buf
, ")", sizeof (buf
) - 1);
1652 mvwprintw (historyw
, ((LINES
< 24) ? 3 : 4), 1, "%s %-*s",
1653 (LINES
< 24) ? _("Prev.:") : _("Prev move:"),
1654 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1658 do_validate_move (char **move
)
1660 struct userdata_s
*d
= gp
->data
;
1664 if (TEST_FLAG (d
->flags
, CF_HUMAN
))
1666 if ((n
= pgn_parse_move (gp
, d
->b
, move
, &frfr
)) != E_PGN_OK
)
1668 invalid_move (d
->n
+ 1, n
, *move
);
1672 strcpy (d
->pm_frfr
, frfr
);
1673 update_time_control (gp
);
1674 pgn_history_add (gp
, d
->b
, *move
);
1675 pgn_switch_turn (gp
);
1679 if ((n
= pgn_validate_move (gp
, d
->b
, move
, &frfr
)) != E_PGN_OK
)
1681 invalid_move (d
->n
+ 1, n
, *move
);
1685 add_engine_command (gp
, ENGINE_THINKING
, "%s\n",
1686 (config
.engine_protocol
== 1) ? frfr
: *move
);
1689 d
->sp
.srow
= d
->sp
.scol
= d
->sp
.icon
= 0;
1691 if (config
.validmoves
)
1692 pgn_reset_valid_moves (d
->b
);
1694 if (TEST_FLAG (gp
->flags
, GF_GAMEOVER
))
1695 d
->mode
= MODE_HISTORY
;
1697 SET_FLAG (d
->flags
, CF_MODIFIED
);
1701 update_history_window (gp
);
1702 update_board_window (gp
);
1707 do_promotion_piece_finalize (WIN
* win
)
1709 char *p
, *str
= win
->data
;
1711 if (pgn_piece_to_int (win
->c
) == -1)
1714 p
= str
+ strlen (str
);
1715 *p
++ = toupper (win
->c
);
1717 do_validate_move (&str
);
1723 move_to_engine (GAME g
)
1725 struct userdata_s
*d
= g
->data
;
1729 if (config
.validmoves
&&
1730 !d
->b
[RANKTOBOARD (d
->sp
.row
)][FILETOBOARD (d
->sp
.col
)].valid
)
1733 str
= Malloc (MAX_SAN_MOVE_LEN
+ 1);
1734 snprintf (str
, MAX_SAN_MOVE_LEN
+ 1, "%c%u%c%u",
1735 _("abcdefgh")[d
->sp
.scol
- 1],
1736 d
->sp
.srow
, _("abcdefgh")[d
->sp
.col
- 1], d
->sp
.row
);
1739 pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->sp
.srow
)]
1740 [FILETOBOARD (d
->sp
.scol
)].icon
);
1742 if (piece
== PAWN
&& (d
->sp
.row
== 8 || d
->sp
.row
== 1))
1744 construct_message (_("Select Pawn Promotion Piece"), _("R/N/B/Q"), 1, 1,
1745 NULL
, NULL
, str
, do_promotion_piece_finalize
, 0, 0,
1747 _("R = Rook, N = Knight, B = Bishop, Q = Queen"));
1751 do_validate_move (&str
);
1756 clock_to_char (long n
)
1758 static char buf
[16];
1759 int h
= 0, m
= 0, s
= 0;
1762 m
= (n
% 3600) / 60;
1763 s
= (n
% 3600) % 60;
1764 snprintf (buf
, sizeof (buf
), "%.2i:%.2i:%.2i", h
, m
, s
);
1769 timeval_to_char (struct timeval t
, long limit
)
1772 unsigned h
= 0, m
= 0, s
= 0;
1773 int n
= limit
? labs (limit
- t
.tv_sec
) : 0;
1776 m
= (n
% 3600) / 60;
1777 s
= (n
% 3600) % 60;
1778 snprintf (buf
, sizeof (buf
), "%.2u:%.2u:%.2u", h
, m
, s
);
1783 time_control_status (struct clock_s
*clk
)
1785 static char buf
[80] = { 0 };
1789 if (clk
->tc
[clk
->tcn
][0] && clk
->tc
[clk
->tcn
+ 1][1])
1790 snprintf (buf
, sizeof (buf
), " M%.2i/%s",
1791 abs (clk
->tc
[clk
->tcn
][0] - clk
->move
),
1792 clock_to_char (clk
->tc
[clk
->tcn
+ 1][1]));
1793 else if (!clk
->incr
)
1800 strncat (tbuf
, " I", sizeof (tbuf
) - 1);
1801 strncat (tbuf
, itoa (clk
->incr
, buf
), sizeof (tbuf
) - 1);
1808 update_status_window (GAME g
)
1812 char tmp
[15] = { 0 }, *engine
, *mode
;
1818 struct userdata_s
*d
= g
->data
;
1822 if (!curses_initialized
)
1825 getmaxyx (statusw
, maxy
, maxx
);
1832 wchar_t *loadfilew
= loadfile
[0]
1833 ? str_etc (loadfile
, w
, 1) : str_to_wchar (_("not available"));
1834 mvwprintw (statusw
, y
++, 1, "%*s %-*ls", 7, _("File:"), w
, loadfilew
);
1836 snprintf (buf
, len
, "%i %s %i", gindex
+ 1, _("of"), gtotal
);
1837 mvwprintw (statusw
, y
++, 1, "%*s %-*s", 7, _("Game:"), w
, buf
);
1848 if (TEST_FLAG (d
->flags
, CF_DELETE
))
1857 if (TEST_FLAG (g
->flags
, GF_PERROR
))
1866 if (TEST_FLAG (d
->flags
, CF_MODIFIED
))
1875 pgn_config_get (PGN_STRICT_CASTLING
, &n
);
1886 if (TEST_FLAG (d
->flags
, CF_PERL
))
1897 mvwprintw (statusw
, y
++, 1, "%*s %-*s", 7, _("Flags:"), w
,
1898 (tmp
[0]) ? tmp
: "-");
1903 mode
= _("move history");
1912 mode
= _("(empty value)");
1916 snprintf (buf
, len
- 1, "%*s %s", 7, _("Mode:"), mode
);
1918 if (d
->mode
== MODE_PLAY
)
1920 if (TEST_FLAG (d
->flags
, CF_HUMAN
))
1921 strncat (buf
, _(" (human/human)"), len
- 1);
1922 else if (TEST_FLAG (d
->flags
, CF_ENGINE_LOOP
))
1923 strncat (buf
, _(" (engine/engine)"), len
- 1);
1925 strncat (buf
, (d
->play_mode
== PLAY_EH
) ?
1926 _(" (engine/human)") : _(" (human/engine)"), len
- 1);
1930 mvwprintw (statusw
, y
++, 1, "%-*s", len
, buf
);
1935 switch (d
->engine
->status
)
1937 case ENGINE_THINKING
:
1938 engine
= _("pondering...");
1941 engine
= _("ready");
1943 case ENGINE_INITIALIZING
:
1944 engine
= _("initializing...");
1946 case ENGINE_OFFLINE
:
1947 engine
= _("offline");
1950 engine
= _("(empty value)");
1955 engine
= _("offline");
1957 mvwprintw (statusw
, y
, 1, "%*s %-*s", 7, _("Engine:"), w
, " ");
1958 wattron (statusw
, CP_STATUS_ENGINE
);
1959 mvwaddstr (statusw
, y
++, 9, engine
);
1960 wattroff (statusw
, CP_STATUS_ENGINE
);
1962 mvwprintw (statusw
, y
++, 1, "%*s %-*s", 7, _("Turn:"), w
,
1963 (g
->turn
== WHITE
) ? _("white") : _("black"));
1965 strncpy (tmp
, _("white"), sizeof (tmp
) - 1);
1966 tmp
[0] = toupper (tmp
[0]);
1967 snprintf (t
, sizeof (t
), "%s%s",
1968 timeval_to_char (d
->wclock
.elapsed
,
1969 d
->wclock
.tc
[d
->wclock
.tcn
][1]),
1970 time_control_status (&d
->wclock
));
1971 mvwprintw (statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1973 strncpy (tmp
, _("black"), sizeof (tmp
) - 1);
1974 tmp
[0] = toupper (tmp
[0]);
1975 snprintf (t
, sizeof (t
), "%s%s",
1976 timeval_to_char (d
->bclock
.elapsed
,
1977 d
->bclock
.tc
[d
->bclock
.tcn
][1]),
1978 time_control_status (&d
->bclock
));
1979 mvwprintw (statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1981 mvwprintw (statusw
, y
++, 1, "%*s %-*s", 7, _("Total:"), w
,
1982 clock_to_char (d
->elapsed
.tv_sec
));
1984 // for (i = 0; i < STATUS_WIDTH; i++)
1985 // mvwprintw(stdscr, STATUS_HEIGHT, i, " ");
1991 snprintf (tbuf
, sizeof (tbuf
), _("Type %ls for help"),
1992 key_lookup (global_keys
, do_global_help
));
1993 status
.notify
= str_to_wchar (tbuf
);
1996 wattron (stdscr
, CP_STATUS_NOTIFY
);
1997 for (i
= (config
.boardleft
) ? BOARD_WIDTH
: 0;
1998 i
< ((config
.boardleft
) ? COLS
: STATUS_WIDTH
); i
++)
1999 mvwprintw (stdscr
, STATUS_HEIGHT
, i
, " ");
2000 mvwprintw (stdscr
, STATUS_HEIGHT
, CENTERX (STATUS_WIDTH
, status
.notify
)
2001 + ((config
.boardleft
) ? BOARD_WIDTH
: 0), "%ls", status
.notify
);
2002 wattroff (stdscr
, CP_STATUS_NOTIFY
);
2006 translate_tag_name (const char *tag
)
2008 if (!strcmp (tag
, "Event"))
2009 return str_to_wchar (translatable_tag_names
[0]);
2010 else if (!strcmp (tag
, "Site"))
2011 return str_to_wchar (translatable_tag_names
[1]);
2012 else if (!strcmp (tag
, "Date"))
2013 return str_to_wchar (translatable_tag_names
[2]);
2014 else if (!strcmp (tag
, "Round"))
2015 return str_to_wchar (translatable_tag_names
[3]);
2016 else if (!strcmp (tag
, "White"))
2017 return str_to_wchar (translatable_tag_names
[4]);
2018 else if (!strcmp (tag
, "Black"))
2019 return str_to_wchar (translatable_tag_names
[5]);
2020 else if (!strcmp (tag
, "Result"))
2021 return str_to_wchar (translatable_tag_names
[6]);
2023 return str_to_wchar (tag
);
2027 update_tag_window (TAG
** t
)
2032 for (i
= 0; t
[i
]; i
++)
2034 wchar_t *namewc
= translate_tag_name (t
[i
]->name
);
2036 l
= wcslen (namewc
);
2042 w
= TAG_WIDTH
- namel
- 4;
2044 for (i
= 0; t
[i
] && i
< TAG_HEIGHT
- 3; i
++)
2046 wchar_t *namewc
= translate_tag_name (t
[i
]->name
);
2047 wchar_t *valuewc
= str_etc (t
[i
]->value
, w
, 0);
2049 mvwprintw (tagw
, (i
+ 2), 1, "%*ls: %-*ls", namel
, namewc
, w
, valuewc
);
2054 for (; i
< TAG_HEIGHT
- 3; i
++)
2055 mvwprintw (tagw
, (i
+ 2), 1, "%*s", namel
+ w
+ 2, " ");
2059 append_enginebuf (GAME g
, char *line
)
2062 struct userdata_s
*d
= g
->data
;
2064 if (d
->engine
->enginebuf
)
2065 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++);
2069 free (d
->engine
->enginebuf
[0]);
2071 for (i
= 0; d
->engine
->enginebuf
[i
+ 1]; i
++)
2072 d
->engine
->enginebuf
[i
] = d
->engine
->enginebuf
[i
+ 1];
2074 d
->engine
->enginebuf
[i
] = strdup (line
);
2078 d
->engine
->enginebuf
=
2079 Realloc (d
->engine
->enginebuf
, (i
+ 2) * sizeof (char *));
2080 d
->engine
->enginebuf
[i
++] = strdup (line
);
2081 d
->engine
->enginebuf
[i
] = NULL
;
2086 update_engine_window (GAME g
)
2089 struct userdata_s
*d
= g
->data
;
2091 wmove (enginew
, 0, 0);
2092 wclrtobot (enginew
);
2094 if (d
->engine
&& d
->engine
->enginebuf
)
2096 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++)
2097 mvwprintw (enginew
, i
+ 2, 1, "%s", d
->engine
->enginebuf
[i
]);
2100 window_draw_title (enginew
, _("Engine IO Window"), COLS
, CP_MESSAGE_TITLE
,
2107 struct userdata_s
*d
= g
->data
;
2110 * In the middle of a macro. Don't update the screen.
2112 if (macro_match
!= -1)
2115 wmove (boardw
, ROWTOMATRIX (d
->c_row
), COLTOMATRIX (d
->c_col
));
2116 update_board_window (g
);
2117 update_status_window (g
);
2118 update_history_window (g
);
2119 update_tag_window (g
->tag
);
2120 update_engine_window (g
);
2126 game_next_prev (GAME g
, int n
, int count
)
2133 if (gindex
+ count
> gtotal
- 1)
2136 gindex
= gtotal
- 1;
2145 if (gindex
- count
< 0)
2150 gindex
= gtotal
- 1;
2160 delete_game (int which
)
2163 struct userdata_s
*d
;
2165 for (i
= 0; i
< gtotal
; i
++)
2169 if (i
== w
|| TEST_FLAG (d
->flags
, CF_DELETE
))
2173 free_userdata_once (game
[i
]);
2176 for (n
= i
; n
+ 1 < gtotal
; n
++)
2177 game
[n
] = game
[n
+ 1];
2187 if (which
+ 1 >= gtotal
)
2188 gindex
= gtotal
- 1;
2193 gindex
= gtotal
- 1;
2196 gp
->hp
= gp
->history
;
2200 * FIXME find across multiple games.
2203 find_move_exp (GAME g
, regex_t r
, int which
, int count
)
2211 incr
= (which
== 0) ? -1 : 1;
2213 for (i
= g
->hindex
+ incr
- 1, found
= 0;; i
+= incr
)
2215 if (i
== g
->hindex
- 1)
2218 if (i
>= pgn_history_total (g
->hp
))
2221 i
= pgn_history_total (g
->hp
) - 1;
2224 ret
= regexec (&r
, g
->hp
[i
]->move
, 0, 0, 0);
2228 if (count
== ++found
)
2235 if (ret
!= REG_NOMATCH
)
2237 regerror (ret
, &r
, errbuf
, sizeof (errbuf
));
2238 cmessage (_("Error Matching Regular Expression"), ANY_KEY_STR
,
2249 toggle_delete_flag (int n
)
2252 struct userdata_s
*d
= game
[n
]->data
;
2254 TOGGLE_FLAG (d
->flags
, CF_DELETE
);
2257 for (i
= x
= 0; i
< gtotal
; i
++)
2261 if (TEST_FLAG (d
->flags
, CF_DELETE
))
2267 cmessage (NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
2269 CLEAR_FLAG (d
->flags
, CF_DELETE
);
2277 find_game_exp (char *str
, int which
, int count
)
2279 char *nstr
= NULL
, *exp
= NULL
;
2283 char buf
[255] = { 0 }, *tmp
;
2286 int incr
= (which
== 0) ? -(1) : 1;
2288 strncpy (buf
, str
, sizeof (buf
));
2289 buf
[sizeof (buf
)-1] = 0;
2292 if (strstr (tmp
, ":") != NULL
)
2294 nstr
= strsep (&tmp
, ":");
2296 if ((ret
= regcomp (&nexp
, nstr
,
2297 REG_ICASE
| REG_EXTENDED
| REG_NOSUB
)) != 0)
2299 regerror (ret
, &nexp
, errbuf
, sizeof (errbuf
));
2300 cmessage (_("Error Compiling Regular Expression"), ANY_KEY_STR
,
2309 while (exp
&& *exp
&& isspace (*exp
))
2315 if ((ret
= regcomp (&vexp
, exp
, REG_EXTENDED
| REG_NOSUB
)) != 0)
2317 regerror (ret
, &vexp
, errbuf
, sizeof (errbuf
));
2318 cmessage (_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s",
2326 for (g
= gindex
+ incr
, found
= 0;; g
+= incr
)
2338 for (t
= 0; game
[g
]->tag
[t
]; t
++)
2342 if (regexec (&nexp
, game
[g
]->tag
[t
]->name
, 0, 0, 0) == 0)
2344 if (regexec (&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0)
2346 if (count
== ++found
)
2356 if (regexec (&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0)
2358 if (count
== ++found
)
2381 * Updates the notification line in the status window then refreshes the
2385 update_status_notify (GAME g
, const char *fmt
, ...)
2388 #ifdef HAVE_VASPRINTF
2394 free (status
.notify
);
2395 status
.notify
= NULL
;
2401 #ifdef HAVE_VASPRINTF
2402 vasprintf (&line
, fmt
, ap
);
2404 vsnprintf (line
, sizeof (line
), fmt
, ap
);
2408 status
.notify
= str_to_wchar (line
);
2410 #ifdef HAVE_VASPRINTF
2416 rav_next_prev (GAME g
, BOARD b
, int n
)
2421 if ((!g
->ravlevel
&& g
->hindex
&& g
->hp
[g
->hindex
- 1]->rav
== NULL
) ||
2422 (!g
->ravlevel
&& !g
->hindex
&& g
->hp
[g
->hindex
]->rav
== NULL
) ||
2423 (g
->ravlevel
&& g
->hp
[g
->hindex
]->rav
== NULL
))
2426 g
->rav
= Realloc (g
->rav
, (g
->ravlevel
+ 1) * sizeof (RAV
));
2427 g
->rav
[g
->ravlevel
].hp
= g
->hp
;
2428 g
->rav
[g
->ravlevel
].flags
= g
->flags
;
2429 g
->rav
[g
->ravlevel
].fen
= pgn_game_to_fen (g
, b
);
2430 g
->rav
[g
->ravlevel
].hindex
= g
->hindex
;
2432 (!g
->ravlevel
) ? (g
->hindex
) ? g
->hp
[g
->hindex
-
2435 g
->hp
[g
->hindex
]->rav
;
2438 pgn_board_update (g
, b
, g
->hindex
+ 1);
2442 if (g
->ravlevel
- 1 < 0)
2447 pgn_board_init_fen (g
, b
, g
->rav
[g
->ravlevel
].fen
);
2448 free (g
->rav
[g
->ravlevel
].fen
);
2449 g
->hp
= g
->rav
[g
->ravlevel
].hp
;
2450 g
->flags
= g
->rav
[g
->ravlevel
].flags
;
2451 g
->hindex
= g
->rav
[g
->ravlevel
].hindex
;
2456 draw_window_decor ()
2458 move_panel (boardp
, 0, (config
.boardleft
) ? 0 : COLS
- BOARD_WIDTH
);
2459 move_panel (historyp
, LINES
- HISTORY_HEIGHT
,
2460 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
: 0 :
2461 (MEGA_BOARD
) ? 0 : COLS
- HISTORY_WIDTH
);
2462 move_panel (statusp
, 0, (config
.boardleft
) ? BOARD_WIDTH
: 0);
2463 move_panel (tagp
, STATUS_HEIGHT
+ 1,
2464 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
:
2467 wbkgd (boardw
, CP_BOARD_WINDOW
);
2468 wbkgd (statusw
, CP_STATUS_WINDOW
);
2469 window_draw_title (statusw
, _("Game Status"), STATUS_WIDTH
,
2470 CP_STATUS_TITLE
, CP_STATUS_BORDER
);
2471 wbkgd (tagw
, CP_TAG_WINDOW
);
2472 window_draw_title (tagw
, _("Roster Tags"), TAG_WIDTH
, CP_TAG_TITLE
,
2474 wbkgd (historyw
, CP_HISTORY_WINDOW
);
2475 window_draw_title (historyw
, _("Move History"), HISTORY_WIDTH
,
2476 CP_HISTORY_TITLE
, CP_HISTORY_BORDER
);
2480 history_menu_resize (WIN
*w
)
2482 struct menu_input_s
*m
;
2487 w
->rows
= MEGA_BOARD
? LINES
- HISTORY_HEIGHT_MB
: LINES
;
2488 w
->cols
= TAG_WIDTH
;
2490 w
->posx
= config
.boardleft
? BOARD_WIDTH
: 0;
2492 m
->ystatic
= w
->posy
;
2493 m
->xstatic
= w
->posx
;
2500 if (LINES
< 23 || COLS
< 74)
2503 resizeterm (LINES
, COLS
);
2505 wresize (boardw
, BOARD_HEIGHT
, BOARD_WIDTH
);
2506 wresize (historyw
, HISTORY_HEIGHT
, HISTORY_WIDTH
);
2507 wresize (statusw
, STATUS_HEIGHT
, STATUS_WIDTH
);
2508 wresize (tagw
, TAG_HEIGHT
, TAG_WIDTH
);
2509 //resize_history_menu ();
2518 draw_window_decor ();
2520 keypad (boardw
, TRUE
);
2529 do_window_resize ();
2535 memset (&clock_timer
, 0, sizeof (struct itimerval
));
2536 setitimer (ITIMER_REAL
, &clock_timer
, NULL
);
2540 start_clock (GAME g
)
2542 struct userdata_s
*d
= g
->data
;
2544 if (clock_timer
.it_interval
.tv_usec
)
2547 memset (&d
->elapsed
, 0, sizeof (struct timeval
));
2548 clock_timer
.it_value
.tv_sec
= 0;
2549 clock_timer
.it_value
.tv_usec
= 100000;
2550 clock_timer
.it_interval
.tv_sec
= 0;
2551 clock_timer
.it_interval
.tv_usec
= 100000;
2552 setitimer (ITIMER_REAL
, &clock_timer
, NULL
);
2559 struct userdata_s
*d
;
2560 struct itimerval it
;
2563 getitimer (ITIMER_REAL
, &it
);
2565 for (i
= 0; i
< gtotal
; i
++)
2569 if (d
&& d
->mode
== MODE_PLAY
)
2571 if (d
->paused
== 1 || TEST_FLAG (d
->flags
, CF_NEW
))
2573 else if (d
->paused
== -1)
2575 if (game
[i
]->side
== game
[i
]->turn
)
2582 update_clock (game
[i
], it
);
2591 update_status_window (gp
);
2597 #define SKIP_SPACE(str) { while (isspace(*str)) str++; }
2600 parse_clock_time (char **str
)
2616 while (isdigit (*p
))
2659 parse_clock_input (struct clock_s
*clk
, char *str
, int *incr
)
2682 memset (clk
, 0, sizeof (struct clock_s
));
2686 if (strncasecmp (p
, "SD", 2) == 0)
2693 n
= parse_clock_time (&p
);
2708 /* Sudden death without a previous time control. */
2714 n
= parse_clock_time (&p
);
2721 message (ERROR_STR
, ANY_KEY_STR
, "%s (%i)",
2722 _("Maximum number of time controls reached"), MAX_TC
);
2727 clk
->tc
[tc
++][1] = n
;
2742 clk
->tc
[clk
->tcn
][1] =
2743 (n
<= clk
->elapsed
.tv_sec
) ? clk
->elapsed
.tv_sec
+ n
: n
;
2753 n
= parse_clock_time (&p
);
2774 parse_which_clock (struct clock_s
*clk
, char *str
)
2779 memcpy (&tmp
, clk
, sizeof (struct clock_s
));
2781 if (parse_clock_input (&tmp
, str
, &incr
))
2783 cmessage (ERROR_STR
, ANY_KEY_STR
, _("Invalid clock specification"));
2787 memcpy (clk
, &tmp
, sizeof (struct clock_s
));
2788 clk
->tc
[clk
->tcn
][1] += incr
;
2793 do_clock_input_finalize (WIN
* win
)
2795 struct userdata_s
*d
= gp
->data
;
2796 struct input_data_s
*in
= win
->data
;
2807 if (tolower (*p
) == 'w')
2811 if (parse_which_clock (&d
->wclock
, p
))
2814 else if (tolower (*p
) == 'b')
2818 if (parse_which_clock (&d
->bclock
, p
))
2823 if (parse_which_clock (&d
->wclock
, p
))
2826 if (parse_which_clock (&d
->bclock
, p
))
2830 if (!d
->wclock
.tc
[0][1] && !d
->bclock
.tc
[0][1])
2831 CLEAR_FLAG (d
->flags
, CF_CLOCK
);
2833 SET_FLAG (d
->flags
, CF_CLOCK
);
2841 do_engine_command_finalize (WIN
* win
)
2843 struct userdata_s
*d
= gp
->data
;
2844 struct input_data_s
*in
= win
->data
;
2856 x
= d
->engine
->status
;
2857 send_to_engine (gp
, -1, "%s\n", in
->str
);
2858 d
->engine
->status
= x
;
2868 config
.details
= (config
.details
) ? 0 : 1;
2872 do_toggle_strict_castling ()
2876 pgn_config_get (PGN_STRICT_CASTLING
, &n
);
2879 pgn_config_set (PGN_STRICT_CASTLING
, 1);
2881 pgn_config_set (PGN_STRICT_CASTLING
, 0);
2885 do_play_set_clock ()
2887 struct input_data_s
*in
;
2889 in
= Calloc (1, sizeof (struct input_data_s
));
2890 in
->efunc
= do_clock_input_finalize
;
2891 construct_input (_("Set Clock"), NULL
, 1, 1,
2893 ("Format: [W | B] [+]T[+I] | ++I | M/T [M/T [...] [SD/T]] [+I]\n"
2894 "T = time (hms), I = increment, M = moves per, SD = sudden death\ne.g., 30m or 4m+12s or 35/90m SD/30m"),
2895 NULL
, NULL
, 0, in
, INPUT_HIST_CLOCK
, NULL
, -1);
2899 do_play_toggle_human ()
2901 struct userdata_s
*d
= gp
->data
;
2903 TOGGLE_FLAG (d
->flags
, CF_HUMAN
);
2905 if (!TEST_FLAG (d
->flags
, CF_HUMAN
) && pgn_history_total (gp
->hp
))
2907 if (init_chess_engine (gp
))
2911 CLEAR_FLAG (d
->flags
, CF_ENGINE_LOOP
);
2914 d
->engine
->status
= ENGINE_READY
;
2918 do_play_toggle_engine ()
2920 struct userdata_s
*d
= gp
->data
;
2922 TOGGLE_FLAG (d
->flags
, CF_ENGINE_LOOP
);
2923 CLEAR_FLAG (d
->flags
, CF_HUMAN
);
2925 if (d
->engine
&& TEST_FLAG (d
->flags
, CF_ENGINE_LOOP
))
2927 char *fen
= pgn_game_to_fen (gp
, d
->b
);
2929 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
2930 add_engine_command (gp
, ENGINE_READY
, "setboard %s\n", fen
);
2936 * This will send a command to the engine skipping the command queue.
2939 do_play_send_command ()
2941 struct userdata_s
*d
= gp
->data
;
2942 struct input_data_s
*in
;
2944 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
)
2946 if (init_chess_engine (gp
))
2950 in
= Calloc (1, sizeof (struct input_data_s
));
2951 in
->efunc
= do_engine_command_finalize
;
2952 construct_input (_("Engine Command"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
,
2953 INPUT_HIST_ENGINE
, NULL
, -1);
2957 void do_play_switch_turn()
2959 struct userdata_s *d = gp->data;
2961 pgn_switch_side(gp);
2962 pgn_switch_turn(gp);
2964 if (!TEST_FLAG(d->flags, CF_HUMAN))
2965 add_engine_command(gp, -1,
2966 (gp->side == WHITE) ? "white\n" : "black\n");
2968 update_status_window(gp);
2972 do_play_toggle_eh_mode ()
2974 struct userdata_s
*d
= gp
->data
;
2976 if (!TEST_FLAG (d
->flags
, CF_HUMAN
))
2980 pgn_switch_side (gp
, TRUE
);
2981 d
->play_mode
= (d
->play_mode
) ? PLAY_HE
: PLAY_EH
;
2982 if (gp
->side
== BLACK
)
2983 update_status_notify (gp
, _("Press 'g' to start the game"));
2985 d
->rotate
= !d
->rotate
;
2988 message (NULL
, ANY_KEY_STR
,
2989 _("You may only switch sides at the start of the \n"
2990 "game. Press ^K or ^N to begin a new game."));
2997 struct userdata_s
*d
= gp
->data
;
2999 if (!pgn_history_total (gp
->hp
))
3004 if (gp
->hindex
- keycount
< 0)
3009 gp
->hindex
-= (keycount
* 2) - 1;
3011 gp
->hindex
-= keycount
* 2;
3016 if (gp
->hindex
- 2 < 0)
3027 pgn_history_free (gp
->hp
, gp
->hindex
);
3028 gp
->hindex
= pgn_history_total (gp
->hp
);
3029 pgn_board_update (gp
, d
->b
, gp
->hindex
);
3031 if (d
->engine
&& d
->engine
->status
== ENGINE_READY
)
3033 char *fen
= pgn_game_to_fen (gp
, d
->b
);
3035 add_engine_command (gp
, ENGINE_READY
, "setboard %s\n", fen
);
3037 d
->engine
->status
= ENGINE_READY
;
3040 update_history_window (gp
);
3044 pgn_switch_side (gp
, FALSE
);
3052 do_play_toggle_pause ()
3054 struct userdata_s
*d
= gp
->data
;
3056 if (!TEST_FLAG (d
->flags
, CF_HUMAN
) && gp
->turn
!= gp
->side
)
3062 d
->paused
= (d
->paused
) ? 0 : 1;
3068 struct userdata_s
*d
= gp
->data
;
3070 if (TEST_FLAG (d
->flags
, CF_HUMAN
))
3073 if (fm_loaded_file
&& gp
->side
!= gp
->turn
)
3075 pgn_switch_side (gp
, FALSE
);
3076 add_engine_command (gp
, ENGINE_THINKING
, "black\n");
3079 add_engine_command (gp
, ENGINE_THINKING
, "go\n");
3081 // Completa la función para que permita seguir jugando al usarla.
3082 // Complete the function to allow continue playing when using.
3083 if (gp
->side
== gp
->turn
)
3084 pgn_switch_side (gp
, FALSE
);
3090 do_play_config_command ()
3096 for (x
= 0; config
.keys
[x
]; x
++)
3098 if (config
.keys
[x
]->c
== input_c
)
3100 switch (config
.keys
[x
]->type
)
3103 add_engine_command (gp
, -1, "%ls\n", config
.keys
[x
]->str
);
3109 add_engine_command (gp
, -1,
3110 "%ls %i\n", config
.keys
[x
]->str
,
3118 for (w
= 0; w
< keycount
; w
++)
3119 add_engine_command (gp
, -1, "%ls\n", config
.keys
[x
]->str
);
3127 update_status_notify (gp
, NULL
);
3131 do_play_cancel_selected ()
3133 struct userdata_s
*d
= gp
->data
;
3135 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3137 pgn_reset_valid_moves (d
->b
);
3138 update_status_notify (gp
, NULL
);
3144 struct userdata_s
*d
= gp
->data
;
3146 pushkey
= keycount
= 0;
3147 update_status_notify (gp
, NULL
);
3149 if (!TEST_FLAG (d
->flags
, CF_HUMAN
) &&
3150 (!d
->engine
|| d
->engine
->status
== ENGINE_THINKING
))
3156 d
->sp
.row
= d
->c_row
;
3157 d
->sp
.col
= d
->c_col
;
3161 rotate_position (&d
->sp
.row
, &d
->sp
.col
);
3162 rotate_position (&d
->sp
.srow
, &d
->sp
.scol
);
3165 move_to_engine (gp
);
3167 // Completa la función para que permita seguir jugando cuando se carga un
3168 // archivo pgn (con juego no terminado) que inicie con turno del lado
3170 // Complete the function to allow continue playing when loading a file
3171 // pgn (with unfinished game) you start to turn black side.
3172 if (gp
->side
!= gp
->turn
)
3173 pgn_switch_side (gp
, FALSE
);
3175 if (d
->rotate
&& d
->sp
.icon
)
3176 rotate_position (&d
->sp
.srow
, &d
->sp
.scol
);
3179 fm_loaded_file
= FALSE
;
3186 struct userdata_s
*d
= gp
->data
;
3188 if (!TEST_FLAG (d
->flags
, CF_HUMAN
) && (!d
->engine
||
3189 d
->engine
->status
==
3192 if (init_chess_engine (gp
))
3196 if (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
)
3200 do_play_cancel_selected ();
3203 rotate_position (&d
->c_row
, &d
->c_col
);
3205 d
->sp
.icon
= d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].icon
;
3207 if (pgn_piece_to_int (d
->sp
.icon
) == OPEN_SQUARE
)
3213 if (((islower (d
->sp
.icon
) && gp
->turn
!= BLACK
)
3214 || (isupper (d
->sp
.icon
) && gp
->turn
!= WHITE
)))
3217 char *str
= Malloc (512);
3219 for (k
= play_keys
; *k
; k
++)
3221 if ((*k
)->f
== do_play_toggle_eh_mode
)
3227 ("It is not your turn to move. You may switch playing sides by pressing \"%lc\"."),
3228 *k
? (*k
)->c
: '?');
3229 message (NULL
, ANY_KEY_STR
, "%s", str
);
3234 if (pgn_history_total (gp
->hp
))
3236 message (NULL
, ANY_KEY_STR
, "%s",
3237 _("It is not your turn to move. You can switch sides "));
3243 if (pgn_tag_find (gp
->tag
, "FEN") != E_PGN_ERR
)
3246 add_engine_command (gp
, ENGINE_READY
, "black\n");
3247 pgn_switch_turn (gp
);
3249 if (gp
->side
!= BLACK
)
3250 pgn_switch_side (gp
);
3255 d
->sp
.srow
= d
->c_row
;
3256 d
->sp
.scol
= d
->c_col
;
3258 if (config
.validmoves
)
3259 pgn_find_valid_moves (gp
, d
->b
, d
->sp
.scol
, d
->sp
.srow
);
3263 rotate_position (&d
->c_row
, &d
->c_col
);
3264 rotate_position (&d
->sp
.srow
, &d
->sp
.scol
);
3267 CLEAR_FLAG (d
->flags
, CF_NEW
);
3272 build_help_line_once (wchar_t *buf
, wchar_t **pp
, struct key_s
*k
, int t
,
3275 wchar_t *p
= *pp
, *wc
;
3279 n
= wcslen (k
->key
);
3290 wcsncat (buf
, k
->key
, t
- 1);
3291 p
= buf
+ wcslen (buf
);
3302 wcsncat (buf
, k
->d
, t
- 1);
3306 wc
= str_to_wchar ("*");
3307 wcsncat (buf
, wc
, t
- 1);
3311 wc
= str_to_wchar ("\n");
3314 *pp
= buf
+ wcslen (buf
);
3318 calc_help_len (struct key_s
*k
, int *t
, int *nlen
, int *len
)
3325 if (wcslen (k
->key
) > *nlen
)
3327 *nlen
= wcslen (k
->key
);
3338 if (wcslen (k
->d
) > *len
)
3339 *len
= wcslen (k
->d
);
3347 build_help (struct key_s
**keys
)
3349 int i
, m
= 0, nlen
= 1, len
, t
;
3350 wchar_t *buf
= NULL
;
3352 wchar_t *more_help
= str_to_wchar (_("more help"));
3353 const wchar_t *more_help_key
= key_lookup (global_keys
, do_global_help
);
3355 int mode
= MODE_ANY
;
3363 for (i
= t
= len
= 0; keys
[i
]; i
++)
3364 calc_help_len (keys
[i
], &t
, &nlen
, &len
);
3368 if (keys
== play_keys
)
3370 else if (keys
== history_keys
)
3371 mode
= MODE_HISTORY
;
3372 else if (keys
== edit_keys
)
3377 for (m
= 0; macros
[m
]; m
++)
3379 if (macros
[m
]->mode
== mode
)
3381 k
.d
= macros
[m
]->desc
;
3383 k
.key
= str_to_wchar (fancy_key_name (macros
[m
]->c
));
3384 calc_help_len (&k
, &t
, &nlen
, &len
);
3392 k
.key
= (wchar_t *)more_help_key
;
3393 calc_help_len (&k
, &t
, &nlen
, &len
);
3395 t
+= 4 + i
+ 1 + m
+ 1; // +1 for more_help
3396 buf
= Malloc ((t
+ 1) * sizeof (wchar_t));
3399 for (i
= 0; keys
[i
]; i
++)
3404 build_help_line_once (buf
, &p
, keys
[i
], t
, nlen
);
3409 for (m
= 0; macros
[m
]; m
++)
3411 if (macros
[m
]->mode
== mode
)
3413 k
.d
= macros
[m
]->desc
;
3415 k
.key
= str_to_wchar (fancy_key_name (macros
[m
]->c
));
3416 build_help_line_once (buf
, &p
, &k
, t
, nlen
);
3424 k
.key
= (wchar_t *)more_help_key
;
3425 build_help_line_once (buf
, &p
, &k
, t
, nlen
);
3433 struct userdata_s
*d
= gp
->data
;
3436 if (!d
->global_help
)
3455 buf
= build_help (global_keys
);
3456 construct_message (_("Global Game Keys (* = can take a repeat count)"),
3457 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
, buf
, do_more_help
,
3458 0, 1, NULL
, "%ls", buf
);
3462 do_main_help (WIN
* win
)
3464 struct userdata_s
*d
= gp
->data
;
3487 do_more_help (WIN
* win
)
3491 for (i
= 0; global_keys
[i
]; i
++)
3493 if (global_keys
[i
]->f
== do_global_help
3494 && global_keys
[i
]->c
== win
->c
)
3495 construct_message (_("Command Key Index"),
3496 _("p/h/e/g or any other key to quit"), 0, 0,
3497 NULL
, NULL
, NULL
, do_main_help
, 0, 0, NULL
, "%s",
3498 _(" p - play mode keys\n"
3499 " h - history mode keys\n"
3500 " e - board edit mode keys\n"
3501 " g - global game keys"));
3508 wchar_t *buf
= build_help (play_keys
);
3510 construct_message (_("Play Mode Keys (* = can take a repeat count)"),
3511 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
,
3512 buf
, do_more_help
, 0, 1, NULL
, "%ls", buf
);
3516 do_play_history_mode ()
3518 struct userdata_s
*d
= gp
->data
;
3520 if (!pgn_history_total (gp
->hp
) ||
3521 (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
))
3524 d
->mode
= MODE_HISTORY
;
3525 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
3529 do_play_edit_mode ()
3531 struct userdata_s
*d
= gp
->data
;
3533 if (pgn_history_total (gp
->hp
))
3536 pgn_board_init_fen (gp
, d
->b
, NULL
);
3538 d
->mode
= MODE_EDIT
;
3542 do_edit_insert_finalize (WIN
* win
)
3544 struct userdata_s
*d
= win
->data
;
3546 if (pgn_piece_to_int (win
->c
) == -1)
3549 d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].icon
= win
->c
;
3555 struct userdata_s
*d
= gp
->data
;
3560 d
->sp
.icon
= d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].icon
;
3562 if (pgn_piece_to_int (d
->sp
.icon
) == OPEN_SQUARE
)
3568 d
->sp
.srow
= d
->c_row
;
3569 d
->sp
.scol
= d
->c_col
;
3576 struct userdata_s
*d
= gp
->data
;
3578 pushkey
= keycount
= 0;
3579 update_status_notify (gp
, NULL
);
3584 d
->sp
.row
= d
->c_row
;
3585 d
->sp
.col
= d
->c_col
;
3586 p
= d
->b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
;
3587 d
->b
[RANKTOBOARD (d
->sp
.row
)][FILETOBOARD (d
->sp
.col
)].icon
= p
;
3588 d
->b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
3589 pgn_int_to_piece (gp
->turn
, OPEN_SQUARE
);
3590 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3596 struct userdata_s
*d
= gp
->data
;
3599 d
->b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
3600 pgn_int_to_piece (gp
->turn
, OPEN_SQUARE
);
3602 d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].icon
=
3603 pgn_int_to_piece (gp
->turn
, OPEN_SQUARE
);
3605 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3609 do_edit_cancel_selected ()
3611 struct userdata_s
*d
= gp
->data
;
3613 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3615 update_status_notify (gp
, NULL
);
3619 do_edit_switch_turn ()
3621 pgn_switch_turn (gp
);
3625 do_edit_toggle_castle ()
3627 struct userdata_s
*d
= gp
->data
;
3629 castling_state (gp
, d
->b
, RANKTOBOARD (d
->c_row
),
3630 FILETOBOARD (d
->c_col
),
3631 d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].icon
,
3638 struct userdata_s
*d
= gp
->data
;
3640 construct_message (_("Insert Piece"),
3641 _("P=pawn, R=rook, N=knight, B=bishop, Q=queen, K=king"),
3642 0, 0, NULL
, NULL
, d
->b
, do_edit_insert_finalize
, 0, 0,
3644 _("Type the piece letter to insert. Lowercase for a black piece, uppercase for a white piece."));
3648 do_edit_enpassant ()
3650 struct userdata_s
*d
= gp
->data
;
3652 if (d
->c_row
== 6 || d
->c_row
== 3)
3654 int n
= d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].enpassant
;
3656 pgn_reset_enpassant (d
->b
);
3658 d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].enpassant
= 1;
3665 wchar_t *buf
= build_help (edit_keys
);
3667 construct_message (_("Edit Mode Keys (* = can take a repeat count)"),
3668 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
, buf
, do_more_help
,
3669 0, 1, NULL
, "%ls", buf
);
3675 struct userdata_s
*d
= gp
->data
;
3676 char *fen
= pgn_game_to_fen (gp
, d
->b
);
3679 pgn_tag_add (&gp
->tag
, (char *) "FEN", fen
);
3681 pgn_tag_add (&gp
->tag
, (char *) "SetUp", (char *) "1");
3682 pgn_tag_sort (gp
->tag
);
3683 pgn_board_update (gp
, d
->b
, gp
->hindex
);
3684 d
->mode
= MODE_PLAY
;
3688 really_do_annotate_finalize (struct input_data_s
*in
, struct userdata_s
*d
)
3690 HISTORY
*h
= in
->data
;
3703 len
= strlen (in
->str
);
3704 h
->comment
= Realloc (h
->comment
, len
+ 1);
3705 strncpy (h
->comment
, in
->str
, len
);
3706 h
->comment
[len
] = 0;
3711 SET_FLAG (d
->flags
, CF_MODIFIED
);
3715 do_annotate_finalize (WIN
* win
)
3717 struct userdata_s
*d
= gp
->data
;
3718 struct input_data_s
*in
= win
->data
;
3720 really_do_annotate_finalize (in
, d
);
3724 do_find_move_exp_finalize (int init
, int which
)
3727 struct userdata_s
*d
= gp
->data
;
3728 static int firstrun
;
3733 if (init
|| !firstrun
)
3738 if ((ret
= regcomp (&r
, moveexp
, REG_EXTENDED
| REG_NOSUB
)) != 0)
3740 regerror (ret
, &r
, errbuf
, sizeof (errbuf
));
3741 cmessage (_("Error Compiling Regular Expression"), ANY_KEY_STR
,
3749 if ((n
= find_move_exp (gp
, r
,
3750 (which
== -1) ? 0 : 1,
3751 (keycount
) ? keycount
: 1)) == -1)
3755 pgn_board_update (gp
, d
->b
, gp
->hindex
);
3759 do_find_move_exp (WIN
* win
)
3761 struct input_data_s
*in
= win
->data
;
3767 strncpy (moveexp
, in
->str
, sizeof (moveexp
) - 1);
3768 moveexp
[sizeof (moveexp
) - 1] = 0;
3769 do_find_move_exp_finalize (1, which
);
3778 do_move_jump_finalize (int n
)
3780 struct userdata_s
*d
= gp
->data
;
3782 if (n
< 0 || n
> (pgn_history_total (gp
->hp
) / 2))
3786 update_status_notify (gp
, NULL
);
3787 gp
->hindex
= (n
) ? n
* 2 - 1 : n
* 2;
3788 pgn_board_update (gp
, d
->b
, gp
->hindex
);
3792 do_move_jump (WIN
* win
)
3794 struct input_data_s
*in
= win
->data
;
3796 if (!in
->str
|| !isinteger (in
->str
))
3805 do_move_jump_finalize (atoi (in
->str
));
3810 struct history_menu_s
3820 free_history_menu_data (struct history_menu_s
**h
)
3827 for (i
= 0; h
[i
]; i
++)
3837 get_history_data (HISTORY
** hp
, struct history_menu_s
***menu
, int m
,
3841 int t
= pgn_history_total (hp
);
3842 char buf
[MAX_SAN_MOVE_LEN
+ 4];
3844 struct history_menu_s
**hmenu
= *menu
;
3847 for (n
= 0; hmenu
[n
]; n
++);
3851 for (i
= 0; i
< t
; i
++)
3853 hmenu
= Realloc (hmenu
, (n
+ 2) * sizeof (struct history_menu_s
*));
3854 hmenu
[n
] = Malloc (sizeof (struct history_menu_s
));
3855 snprintf (buf
, sizeof (buf
), "%c%s%s", (turn
== WHITE
) ? 'W' : 'B',
3856 hp
[i
]->move
, (hp
[i
]->comment
|| hp
[i
]->nag
[0]) ? " !" : "");
3857 hmenu
[n
]->line
= strdup (buf
);
3858 hmenu
[n
]->hindex
= i
;
3859 hmenu
[n
]->indent
= 0;
3860 hmenu
[n
]->ravlevel
= depth
;
3861 hmenu
[n
]->move
= (n
&& depth
> hmenu
[n
- 1]->ravlevel
) ? m
++ : m
;
3869 get_history_data (hp
[i
]->rav
, &hmenu
, m
, turn
);
3870 for (n
= 0; hmenu
[n
]; n
++);
3878 turn
= (turn
== WHITE
) ? BLACK
: WHITE
;
3885 history_draw_update (struct menu_input_s
*m
)
3888 struct userdata_s
*d
= g
->data
;
3890 g
->hindex
= m
->selected
+ 1;
3891 update_cursor (g
, m
->selected
);
3892 pgn_board_update (g
, d
->b
, m
->selected
+ 1);
3895 struct menu_item_s
**
3896 get_history_items (WIN
* win
)
3898 struct menu_input_s
*m
= win
->data
;
3900 struct userdata_s
*d
= g
->data
;
3901 struct history_menu_s
**hm
= d
->data
;
3902 struct menu_item_s
**items
= m
->items
;
3907 get_history_data (g
->history
, &hm
, 0,
3908 TEST_FLAG (g
->flags
, GF_BLACK_OPENING
));
3909 m
->selected
= g
->hindex
- 1;
3911 if (m
->selected
< 0)
3914 m
->draw_exit_func
= history_draw_update
;
3921 for (i
= 0; items
[i
]; i
++)
3928 for (i
= 0; hm
[i
]; i
++)
3930 items
= Realloc (items
, (i
+ 2) * sizeof (struct menu_item_s
*));
3931 items
[i
] = Malloc (sizeof (struct menu_item_s
));
3932 items
[i
]->name
= hm
[i
]->line
;
3933 items
[i
]->value
= NULL
;
3934 items
[i
]->selected
= 0;
3946 history_menu_quit (struct menu_input_s
*m
)
3952 history_menu_exit (WIN
* win
)
3955 struct userdata_s
*d
= g
->data
;
3956 struct history_menu_s
**hm
= d
->data
;
3962 for (i
= 0; hm
[i
]; i
++)
3974 history_menu_next (struct menu_input_s
*m
)
3977 struct userdata_s
*d
= g
->data
;
3978 struct history_menu_s
**hm
= d
->data
;
3981 for (t
= 0; hm
[t
]; t
++);
3983 if (m
->selected
+ 1 == t
)
3986 n
= hm
[m
->selected
+ 1]->hindex
;
3994 history_menu_prev (struct menu_input_s
*m
)
3997 struct userdata_s
*d
= g
->data
;
3998 struct history_menu_s
**hm
= d
->data
;
4001 for (t
= 0; hm
[t
]; t
++);
4003 if (m
->selected
- 1 < 0)
4006 n
= hm
[m
->selected
- 1]->hindex
;
4013 history_menu_help (struct menu_input_s
*m
)
4015 message (_("History Menu Help"), ANY_KEY_STR
, "%s",
4016 _(" UP/DOWN - previous/next menu item\n"
4017 " HOME/END - first/last menu item\n"
4018 " PGDN/PGUP - next/previous page\n"
4019 " a-zA-Z0-9 - jump to item\n"
4020 " CTRL-a - annotate the selected move\n"
4021 " ENTER - view annotation\n"
4022 " CTRL-d - toggle board details\n"
4023 " ESCAPE/M - return to move history"));
4027 do_annotate_move (HISTORY
* hp
)
4030 struct input_data_s
*in
;
4032 snprintf (buf
, sizeof (buf
), "%s \"%s\"", _("Editing Annotation for"),
4034 in
= Calloc (1, sizeof (struct input_data_s
));
4036 in
->efunc
= do_annotate_finalize
;
4037 construct_input (buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
4038 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
,
4039 CTRL_KEY ('T'), in
, -1, NULL
, -1);
4043 history_menu_view_annotation (struct menu_input_s
*m
)
4048 view_annotation (g
->history
[m
->selected
]);
4052 history_menu_annotate_finalize (WIN
* win
)
4054 struct input_data_s
*in
= win
->data
;
4055 GAME g
= in
->moredata
;
4056 struct userdata_s
*d
= g
->data
;
4057 struct history_menu_s
**hm
= d
->data
;
4059 really_do_annotate_finalize (in
, d
);
4060 free_history_menu_data (hm
);
4062 get_history_data (g
->history
, &hm
, 0,
4063 TEST_FLAG (g
->flags
, GF_BLACK_OPENING
));
4065 pushkey
= REFRESH_MENU
;
4069 history_menu_annotate (struct menu_input_s
*m
)
4073 struct input_data_s
*in
;
4074 HISTORY
*hp
= g
->history
[m
->selected
]; // FIXME RAV
4076 snprintf (buf
, sizeof (buf
), "%s \"%s\"", _("Editing Annotation for"),
4078 in
= Calloc (1, sizeof (struct input_data_s
));
4080 in
->moredata
= m
->data
;
4081 in
->efunc
= history_menu_annotate_finalize
;
4082 construct_input (buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
4083 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
,
4084 CTRL_KEY ('T'), in
, -1, NULL
, -1);
4088 history_menu_details (struct menu_input_s
*m
)
4090 do_board_details ();
4095 history_menu_print (WIN
* win
)
4097 struct menu_input_s
*m
= win
->data
;
4099 struct userdata_s
*d
= g
->data
;
4100 struct history_menu_s
**hm
= d
->data
;
4101 struct history_menu_s
*h
= hm
[m
->top
];
4103 char *p
= m
->item
->name
;
4104 int line
= m
->print_line
- 2;
4106 * Solaris 5.9 doesn't have wattr_get() or any function that requires an
4113 for (total
= 0; hm
[total
]; total
++);
4114 wattr_get (win
->w
, &attrs
, &pair
, NULL
);
4115 wattroff (win
->w
, COLOR_PAIR (pair
));
4116 mvwaddch (win
->w
, m
->print_line
, 1,
4117 *p
== 'W' ? *p
| mix_cp (CP_BOARD_WHITE
, CP_HISTORY_WINDOW
,
4118 ATTRS (CP_BOARD_WHITE
),
4119 A_FG_B_BG
) : *p
| mix_cp (CP_BOARD_BLACK
,
4126 if (h
->hindex
== 0 && line
== 0)
4127 waddch (win
->w
, ACS_ULCORNER
| CP_HISTORY_MENU_LG
);
4128 else if ((!hm
[h
->hindex
+ (win
->rows
- 5) + 1] && line
== win
->rows
- 5) ||
4129 (m
->top
+ line
== total
- 1))
4130 waddch (win
->w
, ACS_LLCORNER
| CP_HISTORY_MENU_LG
);
4131 else if (hm
[m
->top
+ 1]->ravlevel
!= h
->ravlevel
|| !h
->ravlevel
)
4132 waddch (win
->w
, ACS_LTEE
| CP_HISTORY_MENU_LG
);
4134 waddch (win
->w
, ACS_VLINE
| CP_HISTORY_MENU_LG
);
4136 wattron (win
->w
, COLOR_PAIR (pair
) | attrs
);
4138 for (i
= 2; *p
; p
++, i
++)
4139 waddch (win
->w
, (*p
== '!') ? *p
| A_BOLD
: *p
);
4141 while (i
++ < win
->cols
- 2)
4142 waddch (win
->w
, ' ');
4146 history_menu (GAME g
)
4148 struct menu_key_s
**keys
= NULL
;
4150 add_menu_key (&keys
, KEY_ESCAPE
, history_menu_quit
);
4151 add_menu_key (&keys
, 'M', history_menu_quit
);
4152 add_menu_key (&keys
, KEY_UP
, history_menu_prev
);
4153 add_menu_key (&keys
, KEY_DOWN
, history_menu_next
);
4154 add_menu_key (&keys
, keycode_lookup (global_keys
, do_global_help
),
4156 add_menu_key (&keys
, CTRL_KEY ('a'), history_menu_annotate
);
4157 add_menu_key (&keys
, CTRL_KEY ('d'), history_menu_details
);
4158 add_menu_key (&keys
, '\n', history_menu_view_annotation
);
4159 construct_menu (MEGA_BOARD
? LINES
- HISTORY_HEIGHT_MB
: LINES
, TAG_WIDTH
,
4160 0, config
.boardleft
? BOARD_WIDTH
: 0,
4161 _("Move History Tree"), 1, get_history_items
, keys
, g
,
4162 history_menu_print
, history_menu_exit
, history_menu_resize
);
4172 do_history_half_move_toggle ()
4174 movestep
= (movestep
== 1) ? 2 : 1;
4175 update_history_window (gp
);
4179 do_history_rotate_board ()
4181 struct userdata_s
*d
= gp
->data
;
4182 d
->rotate
= !d
->rotate
;
4186 do_history_jump_next ()
4188 struct userdata_s
*d
= gp
->data
;
4190 pgn_history_next (gp
, d
->b
, (keycount
> 0) ?
4191 config
.jumpcount
* keycount
* movestep
:
4192 config
.jumpcount
* movestep
);
4196 do_history_jump_prev ()
4198 struct userdata_s
*d
= gp
->data
;
4200 pgn_history_prev (gp
, d
->b
, (keycount
) ?
4201 config
.jumpcount
* keycount
* movestep
:
4202 config
.jumpcount
* movestep
);
4208 struct userdata_s
*d
= gp
->data
;
4210 pgn_history_prev (gp
, d
->b
, (keycount
) ? keycount
* movestep
: movestep
);
4216 struct userdata_s
*d
= gp
->data
;
4218 pgn_history_next (gp
, d
->b
, (keycount
) ? keycount
* movestep
: movestep
);
4222 do_history_mode_finalize (struct userdata_s
*d
)
4225 d
->mode
= MODE_PLAY
;
4229 do_history_mode_confirm (WIN
* win
)
4231 struct userdata_s
*d
= gp
->data
;
4232 wchar_t str
[] = { win
->c
, 0 };
4234 if (!wcscmp (str
, resume_wchar
))
4236 pgn_history_free (gp
->hp
, gp
->hindex
);
4237 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
4242 if (pgn_history_rav_new (gp
, d
->b
, gp
->hindex
) != E_PGN_OK
)
4250 if (!TEST_FLAG (d
->flags
, CF_HUMAN
))
4252 char *fen
= pgn_game_to_fen (gp
, d
->b
);
4254 add_engine_command (gp
, ENGINE_READY
, "setboard %s\n", fen
);
4258 do_history_mode_finalize (d
);
4262 do_history_toggle ()
4264 struct userdata_s
*d
= gp
->data
;
4266 // FIXME Resuming from previous history could append to a RAV.
4267 if (gp
->hindex
!= pgn_history_total (gp
->hp
))
4270 construct_message (NULL
, _("What would you like to do?"), 0, 1,
4271 NULL
, NULL
, NULL
, do_history_mode_confirm
, 0, 0,
4273 ("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."),
4279 if (TEST_FLAG (gp
->flags
, GF_GAMEOVER
))
4283 if (gp
->side
!= gp
->turn
)
4285 d
->play_mode
= PLAY_EH
;
4289 d
->play_mode
= PLAY_HE
;
4293 do_history_mode_finalize (d
);
4297 do_history_annotate ()
4301 if (n
&& gp
->hp
[n
- 1]->move
)
4306 do_annotate_move (gp
->hp
[n
]);
4312 wchar_t *buf
= build_help (history_keys
);
4314 construct_message (_("History Mode Keys (* = can take a repeat count)"),
4315 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
,
4316 buf
, do_more_help
, 0, 1, NULL
, "%ls", buf
);
4320 do_history_find (int which
)
4322 struct input_data_s
*in
;
4325 if (pgn_history_total (gp
->hp
) < 2)
4328 in
= Calloc (1, sizeof (struct input_data_s
));
4329 p
= Malloc (sizeof (int));
4332 in
->efunc
= do_find_move_exp
;
4334 if (!*moveexp
|| which
== 0)
4336 construct_input (_("Find Move Text Expression"), NULL
, 1, 0, NULL
, NULL
,
4337 NULL
, 0, in
, INPUT_HIST_MOVE_EXP
, NULL
, -1);
4343 do_find_move_exp_finalize (0, which
);
4347 do_history_find_new ()
4349 do_history_find (0);
4353 do_history_find_prev ()
4355 do_history_find (-1);
4359 do_history_find_next ()
4361 do_history_find (1);
4365 do_history_rav (int which
)
4367 struct userdata_s
*d
= gp
->data
;
4369 rav_next_prev (gp
, d
->b
, which
);
4373 do_history_rav_next ()
4379 do_history_rav_prev ()
4387 struct input_data_s
*in
;
4389 if (pgn_history_total (gp
->hp
) < 2)
4394 in
= Calloc (1, sizeof (struct input_data_s
));
4395 in
->efunc
= do_move_jump
;
4397 construct_input (_("Jump to Move Number"), NULL
, 1, 1, NULL
,
4398 NULL
, NULL
, 0, in
, -1, NULL
, 0);
4402 do_move_jump_finalize (keycount
);
4406 free_userdata_once (GAME g
)
4408 struct userdata_s
*d
= g
->data
;
4417 if (d
->engine
->enginebuf
)
4421 for (n
= 0; d
->engine
->enginebuf
[n
]; n
++)
4422 free (d
->engine
->enginebuf
[n
]);
4424 free (d
->engine
->enginebuf
);
4427 if (d
->engine
->queue
)
4431 for (q
= d
->engine
->queue
; *q
; q
++)
4434 free (d
->engine
->queue
);
4457 for (i
= 0; i
< gtotal
; i
++)
4459 free_userdata_once (game
[i
]);
4460 game
[i
]->data
= NULL
;
4465 update_loading_window (int n
)
4471 loadingw
= newwin (3, COLS
/ 2, CALCPOSY (3), CALCPOSX (COLS
/ 2));
4472 loadingp
= new_panel (loadingw
);
4473 wbkgd (loadingw
, CP_MESSAGE_WINDOW
);
4476 wmove (loadingw
, 0, 0);
4477 wclrtobot (loadingw
);
4478 wattron (loadingw
, CP_MESSAGE_BORDER
);
4479 box (loadingw
, ACS_VLINE
, ACS_HLINE
);
4480 wattroff (loadingw
, CP_MESSAGE_BORDER
);
4481 mvwprintw (loadingw
, 1, CENTER_INT ((COLS
/ 2), 11 +
4482 strlen (itoa (gtotal
, buf
))),
4483 _("Loading... %i%% (%i games)"), n
, gtotal
);
4489 init_userdata_once (GAME g
, int n
)
4491 struct userdata_s
*d
= NULL
;
4493 d
= Calloc (1, sizeof (struct userdata_s
));
4495 d
->c_row
= 2, d
->c_col
= 5;
4496 SET_FLAG (d
->flags
, CF_NEW
);
4499 if (pgn_board_init_fen (g
, d
->b
, NULL
) != E_PGN_OK
)
4500 pgn_board_init (d
->b
);
4508 for (i
= 0; i
< gtotal
; i
++)
4509 init_userdata_once (game
[i
], i
);
4513 fix_marks (int *start
, int *end
)
4517 *start
= (*start
< 0) ? 0 : *start
;
4518 *end
= (*end
< 0) ? 0 : *end
;
4527 *end
= (*end
> gtotal
) ? gtotal
: *end
;
4531 do_new_game_finalize (GAME g
)
4533 struct userdata_s
*d
= g
->data
;
4535 d
->mode
= MODE_PLAY
;
4536 update_status_notify (g
, NULL
);
4542 do_new_game_from_scratch (WIN
* win
)
4544 wchar_t str
[] = { win
->c
, 0 };
4546 if (wcscmp (str
, yes_wchar
))
4553 add_custom_tags (&gp
->tag
);
4556 do_new_game_finalize (gp
);
4564 add_custom_tags (&gp
->tag
);
4565 init_userdata_once (gp
, gindex
);
4566 do_new_game_finalize (gp
);
4570 do_game_delete_finalize (int n
)
4572 struct userdata_s
*d
;
4574 delete_game ((!n
) ? gindex
: -1);
4576 if (d
->mode
!= MODE_EDIT
)
4577 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
4581 do_game_delete_confirm (WIN
* win
)
4584 wchar_t str
[] = { win
->c
, 0 };
4586 if (wcscmp (str
, yes_wchar
))
4592 n
= (int *) win
->data
;
4593 do_game_delete_finalize (*n
);
4602 struct userdata_s
*d
;
4607 cmessage (NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
4613 for (i
= n
= 0; i
< gtotal
; i
++)
4617 if (TEST_FLAG (d
->flags
, CF_DELETE
))
4622 tmp
= _("Delete the current game?");
4627 cmessage (NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
4631 tmp
= _("Delete all games marked for deletion?");
4634 if (config
.deleteprompt
)
4636 p
= Malloc (sizeof (int));
4638 construct_message (NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, p
,
4639 do_game_delete_confirm
, 0, 0, NULL
, "%s", tmp
);
4643 do_game_delete_finalize (n
);
4647 do_find_game_exp_finalize (int which
)
4649 struct userdata_s
*d
= gp
->data
;
4652 if ((n
= find_game_exp (gameexp
, (which
== -1) ? 0 : 1,
4653 (keycount
) ? keycount
: 1)) == -1)
4655 update_status_notify (gp
, "%s", _("No matches found"));
4662 if (pgn_history_total (gp
->hp
))
4663 d
->mode
= MODE_HISTORY
;
4665 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
4669 do_find_game_exp (WIN
* win
)
4671 struct input_data_s
*in
= win
->data
;
4677 strncpy (gameexp
, in
->str
, sizeof (gameexp
));
4678 gameexp
[sizeof (gameexp
) - 1] = 0;
4683 do_find_game_exp_finalize (c
);
4692 do_game_jump_finalize (int n
)
4694 struct userdata_s
*d
;
4696 if (--n
> gtotal
- 1 || n
< 0)
4702 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
4703 update_status_notify (gp
, NULL
);
4707 do_game_jump (WIN
* win
)
4709 struct input_data_s
*in
= win
->data
;
4711 if (!in
->str
|| !isinteger (in
->str
))
4720 do_game_jump_finalize (atoi (in
->str
));
4726 do_load_file (WIN
* win
)
4728 struct input_data_s
*in
= win
->data
;
4729 char *tmp
= in
->str
;
4730 struct userdata_s
*d
;
4731 PGN_FILE
*pgn
= NULL
;
4740 if ((tmp
= pathfix (tmp
)) == NULL
)
4743 n
= pgn_open (tmp
, "r", &pgn
);
4747 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
, strerror (errno
));
4750 else if (n
== E_PGN_INVALID
)
4752 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
,
4753 _("Not a regular file"));
4759 if (pgn_parse (pgn
) == E_PGN_ERR
)
4761 del_panel (loadingp
);
4769 del_panel (loadingp
);
4774 strncpy (loadfile
, tmp
, sizeof (loadfile
));
4775 loadfile
[sizeof (loadfile
) - 1] = 0;
4779 if (pgn_history_total (gp
->hp
))
4780 d
->mode
= MODE_HISTORY
;
4782 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
4784 fm_loaded_file
= TRUE
;
4797 do_game_save (WIN
* win
)
4799 struct input_data_s
*in
= win
->data
;
4802 char *tmp
= in
->str
;
4803 char tfile
[FILENAME_MAX
];
4806 struct userdata_s
*d
;
4808 if (!tmp
|| (tmp
= pathfix (tmp
)) == NULL
)
4811 if (pgn_is_compressed (tmp
) == E_PGN_ERR
)
4813 p
= tmp
+ strlen (tmp
) - 1;
4815 if (*p
!= 'n' || *(p
- 1) != 'g' || *(p
- 2) != 'p' || *(p
- 3) != '.')
4817 snprintf (tfile
, sizeof (tfile
), "%s.pgn", tmp
);
4823 * When in edit mode, update the FEN tag.
4827 for (i
= 0; i
< gtotal
; i
++)
4831 if (d
->mode
== MODE_EDIT
)
4833 char *fen
= pgn_game_to_fen (game
[i
], d
->b
);
4835 pgn_tag_add (&game
[i
]->tag
, (char *) "FEN", fen
);
4844 if (d
->mode
== MODE_EDIT
)
4846 char *fen
= pgn_game_to_fen (game
[n
], d
->b
);
4848 pgn_tag_add (&game
[n
]->tag
, (char *) "FEN", fen
);
4864 do_get_game_save_input (int n
)
4866 struct input_data_s
*in
= Calloc (1, sizeof (struct input_data_s
));
4867 int *p
= Malloc (sizeof (int));
4869 in
->efunc
= do_game_save
;
4873 construct_input (_("Save Game Filename"), loadfile
, 1, 1,
4874 _("Type TAB for file browser"), file_browser
, NULL
, '\t',
4875 in
, INPUT_HIST_FILE
, NULL
, -1);
4879 do_game_save_multi_confirm (WIN
* win
)
4882 wchar_t str
[] = { win
->c
, 0 };
4884 if (!wcscmp (str
, current_wchar
))
4886 else if (!wcscmp (str
, all_wchar
))
4890 update_status_notify (gp
, "%s", _("Save game aborted."));
4894 do_get_game_save_input (i
);
4900 cmessage (_("ABOUT"), ANY_KEY_STR
,
4901 _("%s\nUsing %s with %i colors and %i color pairs\n%s\n%s"),
4902 PACKAGE_STRING
, curses_version (), COLORS
, COLOR_PAIRS
,
4903 COPYRIGHT
, CBOARD_URL
);
4907 global_game_next_prev (int which
)
4909 struct userdata_s
*d
;
4911 game_next_prev (gp
, (which
== 1) ? 1 : 0, (keycount
) ? keycount
: 1);
4918 markend
= markstart
+ delete_count
;
4923 markend
= markstart
- delete_count
+ 1;
4924 delete_count
= -1; // to fix gindex in the other direction
4927 fix_marks (&markstart
, &markend
);
4928 do_global_toggle_delete ();
4931 if (d
->mode
== MODE_HISTORY
)
4932 pgn_board_update (gp
, d
->b
, gp
->hindex
);
4933 else if (d
->mode
== MODE_PLAY
)
4934 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
4938 do_global_next_game ()
4940 global_game_next_prev (1);
4944 do_global_prev_game ()
4946 global_game_next_prev (0);
4950 global_find (int which
)
4952 struct input_data_s
*in
;
4958 in
= Calloc (1, sizeof (struct input_data_s
));
4959 p
= Malloc (sizeof (int));
4962 in
->efunc
= do_find_game_exp
;
4964 if (!*gameexp
|| which
== 0)
4966 construct_input (_("Find Game by Tag Expression"), NULL
, 1, 0,
4967 _("[name expression:]value expression"), NULL
, NULL
, 0,
4968 in
, INPUT_HIST_GAME_EXP
, NULL
, -1);
4974 do_find_game_exp_finalize (which
);
4978 do_global_find_new ()
4984 do_global_find_next ()
4990 do_global_find_prev ()
4996 do_global_game_jump ()
5003 struct input_data_s
*in
;
5005 in
= Calloc (1, sizeof (struct input_data_s
));
5006 in
->efunc
= do_game_jump
;
5007 construct_input (_("Jump to Game Number"), NULL
, 1, 1, NULL
, NULL
, NULL
,
5008 0, in
, -1, NULL
, 0);
5012 do_game_jump_finalize (keycount
);
5016 do_global_toggle_delete ()
5025 if (keycount
&& delete_count
== 0)
5028 delete_count
= keycount
;
5029 update_status_notify (gp
, "%s (delete)", status
.notify
);
5033 if (markstart
>= 0 && markend
>= 0)
5035 for (i
= markstart
; i
< markend
; i
++)
5037 if (toggle_delete_flag (i
))
5043 gindex
= (delete_count
< 0) ? markstart
: i
- 1;
5047 if (toggle_delete_flag (gindex
))
5051 markstart
= markend
= -1;
5053 update_status_window (gp
);
5057 do_global_delete_game ()
5063 do_global_tag_edit ()
5065 struct userdata_s
*d
= gp
->data
;
5067 edit_tags (gp
, d
->b
, 1);
5071 do_global_tag_view ()
5073 struct userdata_s
*d
= gp
->data
;
5075 edit_tags (gp
, d
->b
, 0);
5079 do_global_resume_game ()
5081 struct input_data_s
*in
;
5083 in
= Calloc (1, sizeof (struct input_data_s
));
5084 in
->efunc
= do_load_file
;
5085 construct_input (_("Load Filename"), NULL
, 1, 1,
5086 _("Type TAB for file browser"), file_browser
, NULL
, '\t',
5087 in
, INPUT_HIST_FILE
, NULL
, -1);
5091 do_global_save_game ()
5095 construct_message (NULL
, _("What would you like to do?"), 0, 1,
5096 NULL
, NULL
, NULL
, do_game_save_multi_confirm
, 0, 0,
5098 ("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."),
5099 current_wchar
, all_wchar
);
5103 do_get_game_save_input (-1);
5107 do_global_new_game ()
5113 copy_game_common (int fen
)
5117 struct userdata_s
*d
= gp
->data
;
5118 char *fentag
= fen
? pgn_game_to_fen (gp
, d
->b
) : NULL
;
5120 do_global_new_game ();
5122 n
= pgn_tag_total (game
[g
]->tag
);
5124 for (i
= 0; i
< n
; i
++)
5125 pgn_tag_add (&gp
->tag
, game
[g
]->tag
[i
]->name
, game
[g
]->tag
[i
]->value
);
5129 pgn_tag_add (&gp
->tag
, (char *) "SetUp", (char *) "1");
5130 pgn_tag_add (&gp
->tag
, (char *) "FEN", fentag
);
5132 pgn_tag_sort (gp
->tag
);
5136 pgn_board_init_fen (gp
, d
->b
, NULL
);
5137 n
= pgn_history_total (game
[g
]->history
);
5140 for (i
= 0; i
< n
; i
++)
5143 char *move
= strdup (game
[g
]->history
[i
]->move
);
5145 if (pgn_parse_move (gp
, d
->b
, &move
, &frfr
) != E_PGN_OK
)
5148 SET_FLAG (gp
->flags
, GF_PERROR
);
5152 pgn_history_add (gp
, d
->b
, move
);
5155 pgn_switch_turn (gp
);
5159 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
5163 do_global_copy_game ()
5165 copy_game_common (0);
5169 do_global_copy_game_fen ()
5171 copy_game_common (1);
5175 do_global_new_all ()
5177 construct_message (NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
5178 do_new_game_from_scratch
, 0, 0, NULL
, "%s",
5179 _("Really start a new game from scratch?"));
5185 wchar_t str
[] = { win
->c
, 0 };
5186 int n
= wcscmp (str
, yes_wchar
);
5197 if (config
.exitdialogbox
)
5198 construct_message (NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
5199 do_quit
, 0, 0, NULL
, "%s", _("Want to Quit?"));
5205 do_global_toggle_engine_window ()
5209 enginew
= newwin (LINES
, COLS
, 0, 0);
5210 enginep
= new_panel (enginew
);
5211 window_draw_title (enginew
, _("Engine IO Window"), COLS
,
5212 CP_MESSAGE_TITLE
, CP_MESSAGE_BORDER
);
5213 hide_panel (enginep
);
5216 if (panel_hidden (enginep
))
5218 update_engine_window (gp
);
5219 top_panel (enginep
);
5223 hide_panel (enginep
);
5228 do_global_toggle_board_details ()
5230 do_board_details ();
5234 do_play_toggle_strict_castling ()
5236 do_toggle_strict_castling ();
5239 // Global and other keys.
5243 struct userdata_s
*d
= gp
->data
;
5247 * These cannot be modified and other game mode keys cannot conflict with
5253 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
5254 markend
= markstart
= 0;
5259 update_status_notify (gp
, NULL
);
5262 if (config
.validmoves
)
5263 pgn_reset_valid_moves (d
->b
);
5270 keycount
= keycount
* 10 + i
;
5274 update_status_notify (gp
, _("Repeat %i"), keycount
);
5277 if (d
->mode
== MODE_HISTORY
)
5281 d
->c_row
+= keycount
;
5290 if (d
->mode
== MODE_HISTORY
)
5295 d
->c_row
-= keycount
;
5296 update_status_notify (gp
, NULL
);
5306 if (d
->mode
== MODE_HISTORY
)
5310 d
->c_col
-= keycount
;
5319 if (d
->mode
== MODE_HISTORY
)
5323 d
->c_col
+= keycount
;
5335 for (i
= 0; global_keys
[i
]; i
++)
5337 if (input_c
== global_keys
[i
]->c
&& global_keys
[i
]->f
)
5339 (*global_keys
[i
]->f
) ();
5351 perl_error (const char *fmt
, ...)
5357 vasprintf (&buf
, fmt
, ap
);
5360 message (ERROR_STR
, ANY_KEY_STR
, "%s", buf
);
5365 do_perl_finalize (WIN
* win
)
5367 struct input_data_s
*in
= win
->data
;
5369 struct userdata_s
*d
= g
->data
;
5371 char *result
= NULL
;
5375 asprintf (&filename
, "%s/perl.pl", config
.datadir
);
5380 if (perl_init_file (filename
, perl_error
))
5383 arg
= pgn_game_to_fen (g
, d
->b
);
5385 if (perl_call_sub (trim (in
->str
), arg
, &result
))
5388 d
->perlfen
= pgn_game_to_fen (g
, d
->b
);
5389 d
->perlflags
= g
->flags
;
5391 if (pgn_board_init_fen (g
, d
->b
, result
) != E_PGN_OK
)
5393 message (ERROR_STR
, ANY_KEY_STR
, "%s", _("FEN parse error."));
5394 pgn_board_init_fen (g
, d
->b
, d
->perlfen
);
5395 g
->flags
= d
->perlflags
;
5401 SET_FLAG (d
->flags
, CF_PERL
);
5402 n
= pgn_tag_find (g
->tag
, "FEN");
5405 d
->oldfen
= strdup (g
->tag
[n
]->value
);
5407 pgn_tag_add (&g
->tag
, (char *) "FEN", result
);
5408 update_status_notify (g
, "%s", ANY_KEY_STR
);
5422 struct input_data_s
*in
;
5424 in
= Calloc (1, sizeof (struct input_data_s
));
5426 in
->efunc
= do_perl_finalize
;
5427 construct_input (_("PERL Subroutine Filter"), NULL
, 1, 0, NULL
, NULL
, NULL
,
5428 0, in
, INPUT_HIST_PERL
, NULL
, -1);
5433 * A macro may contain a key that belongs to another macro so macro_match will
5434 * need to be updated to the new index of the matching macro.
5437 find_macro (struct userdata_s
*d
)
5442 * Macros can't contain macros when in a window.
5448 for (i
= 0; macros
[i
]; i
++)
5450 if ((macros
[i
]->mode
== -1 || macros
[i
]->mode
== d
->mode
) &&
5451 input_c
== macros
[i
]->c
)
5453 input_c
= macros
[i
]->keys
[macros
[i
]->n
++];
5455 if (!macro_depth_n
&& macro_match
> -1)
5457 macro_depth
= Realloc (macro_depth
, (macro_depth_n
+ 1) * sizeof (int));
5458 macro_depth
[macro_depth_n
++] = macro_match
;
5461 macro_depth
= Realloc (macro_depth
, (macro_depth_n
+ 1) * sizeof (int));
5462 macro_depth
[macro_depth_n
++] = i
;
5470 * Resets the position in each macro to the first key.
5476 struct userdata_s
*d
= gp
->data
;
5479 if (macro_depth_n
> 0)
5482 macro_match
= macro_depth
[macro_depth_n
];
5484 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
5487 input_c
= macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
5492 for (i
= 0; macros
[i
]; i
++)
5504 struct userdata_s
*d
;
5507 gindex
= gtotal
- 1;
5511 if (pgn_history_total (gp
->hp
))
5512 d
->mode
= MODE_HISTORY
;
5515 d
->mode
= MODE_PLAY
;
5516 d
->play_mode
= PLAY_HE
;
5522 d
->pm_frfr
[0] = '\0';
5524 if (d
->mode
== MODE_HISTORY
)
5525 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
5527 update_status_notify (gp
, _("Type %ls for help"),
5528 key_lookup (global_keys
, do_global_help
));
5532 wtimeout (boardw
, WINDOW_TIMEOUT
);
5537 char fdbuf
[8192] = { 0 };
5539 struct timeval tv
= { 0, 0 };
5547 for (i
= 0; i
< gtotal
; i
++)
5551 if (d
->engine
&& d
->engine
->pid
!= -1)
5553 if (d
->engine
->fd
[ENGINE_IN_FD
] > 2)
5555 if (d
->engine
->fd
[ENGINE_IN_FD
] > n
)
5556 n
= d
->engine
->fd
[ENGINE_IN_FD
];
5558 FD_SET (d
->engine
->fd
[ENGINE_IN_FD
], &rfds
);
5561 if (d
->engine
->fd
[ENGINE_OUT_FD
] > 2)
5563 if (d
->engine
->fd
[ENGINE_OUT_FD
] > n
)
5564 n
= d
->engine
->fd
[ENGINE_OUT_FD
];
5566 FD_SET (d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
);
5573 if ((n
= select (n
+ 1, &rfds
, &wfds
, NULL
, &tv
)) > 0)
5575 for (i
= 0; i
< gtotal
; i
++)
5579 if (d
->engine
&& d
->engine
->pid
!= -1)
5581 if (FD_ISSET (d
->engine
->fd
[ENGINE_IN_FD
], &rfds
))
5583 len
= read (d
->engine
->fd
[ENGINE_IN_FD
], fdbuf
,
5588 if (d
->engine
->iobuf
)
5590 Realloc (d
->engine
->iobuf
,
5591 d
->engine
->len
+ len
+ 1);
5593 d
->engine
->iobuf
= Calloc (1, len
+ 1);
5595 memcpy (&(d
->engine
->iobuf
[d
->engine
->len
]),
5597 d
->engine
->len
+= len
;
5598 d
->engine
->iobuf
[d
->engine
->len
] = 0;
5601 * The fdbuf is full or no newline
5602 * was found. So we'll append the next
5603 * read() to this games buffer.
5605 if (d
->engine
->iobuf
[d
->engine
->len
- 1] !=
5609 parse_engine_output (game
[i
], d
->engine
->iobuf
);
5610 free (d
->engine
->iobuf
);
5611 d
->engine
->iobuf
= NULL
;
5616 if (errno
!= EAGAIN
)
5618 cmessage (ERROR_STR
, ANY_KEY_STR
,
5619 "Engine read(): %s",
5621 waitpid (d
->engine
->pid
, &n
, 0);
5629 if (FD_ISSET (d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
))
5631 if (d
->engine
->queue
)
5632 send_engine_command (game
[i
]);
5640 cmessage (ERROR_STR
, ANY_KEY_STR
, "select(): %s",
5650 * This is needed to detect terminal resizing.
5653 if (LINES
!= LINES_OLD
|| COLS
!= COLS_OLD
)
5657 do_window_resize ();
5661 * Finds the top level window in the window stack so we know what
5662 * window the wget_wch()'ed key belongs to.
5666 for (i
= 0; wins
[i
]; i
++);
5669 wtimeout (wp
, WINDOW_TIMEOUT
);
5680 if (macros
&& macro_match
>= 0)
5682 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
5689 input_c
= macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
5695 if (wget_wch (wp
, &input_c
) == ERR
|| input_c
== KEY_RESIZE
)
5697 if (input_c
== KEY_RESIZE
)
5702 window_resize_all ();
5717 * Run the function associated with the window. When the
5718 * function returns 0 win->efunc is ran (if not NULL) with
5719 * win as the one and only parameter. Then the window is
5722 * The exit function may create another window which will
5723 * mess up the window stack when window_destroy() is called.
5724 * So don't destory the window until the top window is
5725 * destroyable. See window_destroy().
5727 if ((*win
->func
) (win
) == 0)
5730 (*win
->efunc
) (win
);
5733 window_destroy (win
);
5741 if (!keycount
&& status
.notify
)
5742 update_status_notify (gp
, NULL
);
5745 if (TEST_FLAG (d
->flags
, CF_PERL
))
5747 CLEAR_FLAG (d
->flags
, CF_PERL
);
5748 pgn_board_init_fen (gp
, d
->b
, d
->perlfen
);
5749 gp
->flags
= d
->perlflags
;
5751 pgn_tag_add (&gp
->tag
, (char *) "FEN", d
->oldfen
);
5753 d
->perlfen
= d
->oldfen
= NULL
;
5759 if (macros
&& macro_match
< 0)
5762 if ((n
= globalkeys ()) == 1)
5764 if (macro_match
== -1)
5775 for (i
= 0; edit_keys
[i
]; i
++)
5777 if (input_c
== edit_keys
[i
]->c
)
5779 (*edit_keys
[i
]->f
) ();
5785 for (i
= 0; play_keys
[i
]; i
++)
5787 if (input_c
== play_keys
[i
]->c
)
5789 (*play_keys
[i
]->f
) ();
5794 do_play_config_command ();
5797 for (i
= 0; history_keys
[i
]; i
++)
5799 if (input_c
== history_keys
[i
]->c
)
5801 (*history_keys
[i
]->f
) ();
5812 update_status_notify (gp
, NULL
);
5822 usage (const char *pn
, int ret
)
5824 fprintf ((ret
) ? stderr
: stdout
, "%s%s",
5826 _("Usage: cboard [-hvCD] [-u [N]] [-p [-VtRSE] <file>]\n"
5827 " -D Dump libchess debugging info to \"libchess.debug\" (stderr)\n"),
5829 _("Usage: cboard [-hvC] [-u [N]] [-p [-VtRSE] <file>]\n"),
5831 _(" -p Load PGN file.\n"
5832 " -V Validate a game file.\n"
5833 " -S Validate and output a PGN formatted game.\n"
5834 " -R Like -S but write a reduced PGN formatted game.\n"
5835 " -t Also write custom PGN tags from config file.\n"
5836 " -E Stop processing on file parsing error (overrides config).\n"
5837 " -C Enable strict castling (overrides config).\n"
5838 " -u Enable/disable UTF-8 pieces (1=enable, 0=disable, overrides config).\n"
5839 " -v Version information.\n" " -h This help text.\n"));
5852 free (config
.engine_cmd
);
5853 free (config
.pattern
);
5854 free (config
.ccfile
);
5855 free (config
.nagfile
);
5856 free (config
.configfile
);
5860 for (i
= 0; config
.keys
[i
]; i
++)
5862 free (config
.keys
[i
]->str
);
5863 free (config
.keys
[i
]);
5871 for (i
= 0; config
.einit
[i
]; i
++)
5872 free (config
.einit
[i
]);
5874 free (config
.einit
);
5878 pgn_tag_free (config
.tag
);
5880 free (config
.datadir
);
5882 if (curses_initialized
)
5885 del_panel (historyp
);
5886 del_panel (statusp
);
5895 del_panel (enginep
);
5908 signal_save_pgn (int sig
)
5912 char *p
= config
.savedirectory
? config
.savedirectory
: config
.datadir
;
5915 asprintf (&buf
, "%s/signal-%i-%li.pgn", p
, sig
, now
);
5917 if (do_game_write (buf
, "w", 0, gtotal
))
5919 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s: %s", p
, strerror (errno
));
5920 update_status_notify (gp
, "%s", _("Save game failed."));
5928 catch_signal (int which
, siginfo_t
*info
, void *ctx
)
5939 if (which
== SIGPIPE
&& quit
)
5942 if (which
== SIGPIPE
)
5943 cmessage (NULL
, ANY_KEY_STR
, "%s", _("Broken pipe. Quitting."));
5946 exit (EXIT_FAILURE
);
5953 do_window_resize ();
5954 keypad (boardw
, TRUE
);
5960 signal_save_pgn (which
);
5968 loading_progress (long total
, long offset
)
5970 int n
= (100 * (offset
/ 100) / (total
/ 100));
5972 if (curses_initialized
)
5973 update_loading_window (n
);
5976 fprintf (stderr
, _("Loading... %i%% (%i games)%c"), n
, gtotal
, '\r');
5984 set_config_defaults ();
5985 set_default_keys ();
5986 filetype
= FILE_NONE
;
5987 pgn_config_set (PGN_PROGRESS
, 1024);
5988 pgn_config_set (PGN_PROGRESS_FUNC
, loading_progress
);
5992 main (int argc
, char *argv
[])
5996 char buf
[FILENAME_MAX
];
5997 char datadir
[FILENAME_MAX
];
5998 int ret
= EXIT_SUCCESS
;
5999 int validate_only
= 0, validate_and_write
= 0;
6000 int write_custom_tags
= 0;
6003 int utf8_pieces
= -1;
6004 struct sigaction sigact
;
6006 setlocale (LC_ALL
, "");
6007 bindtextdomain ("cboard", LOCALE_DIR
);
6008 textdomain ("cboard");
6011 #ifndef HAVE_PROGNAME
6012 __progname
= argv
[0];
6015 if ((config
.pwd
= getpwuid (getuid ())) == NULL
)
6016 err (EXIT_FAILURE
, "getpwuid()");
6018 snprintf (datadir
, sizeof (datadir
), "%s/.cboard", config
.pwd
->pw_dir
);
6019 config
.datadir
= strdup (datadir
);
6020 snprintf (buf
, sizeof (buf
), "%s/cc.data", datadir
);
6021 config
.ccfile
= strdup (buf
);
6022 snprintf (buf
, sizeof (buf
), "%s/nag.data", datadir
);
6023 config
.nagfile
= strdup (buf
);
6024 snprintf (buf
, sizeof (buf
), "%s/config", datadir
);
6025 config
.configfile
= strdup (buf
);
6027 if (stat (datadir
, &st
) == -1)
6029 if (errno
== ENOENT
)
6031 if (mkdir (datadir
, 0755) == -1)
6032 err (EXIT_FAILURE
, "%s", datadir
);
6035 err (EXIT_FAILURE
, "%s", datadir
);
6037 stat (datadir
, &st
);
6040 if (!S_ISDIR (st
.st_mode
))
6041 errx (EXIT_FAILURE
, "%s: %s", datadir
, _("Not a directory."));
6046 while ((opt
= getopt (argc
, argv
, "DCEVtSRhp:vu::")) != -1)
6049 while ((opt
= getopt (argc
, argv
, "ECVtSRhp:vu::")) != -1)
6056 unlink ("libchess.debug");
6057 pgn_config_set (PGN_DEBUG
, 1);
6061 pgn_config_set (PGN_STRICT_CASTLING
, 1);
6064 write_custom_tags
= 1;
6070 pgn_config_set (PGN_REDUCED
, 1);
6072 validate_and_write
= 1;
6077 printf ("%s (%s)\n%s\n%s\n", PACKAGE_STRING
, curses_version (),
6078 COPYRIGHT
, CBOARD_URL
);
6079 exit (EXIT_SUCCESS
);
6081 filetype
= FILE_PGN
;
6082 strncpy (loadfile
, optarg
, sizeof (loadfile
));
6083 loadfile
[sizeof (loadfile
) - 1] = 0;
6086 utf8_pieces
= optarg
? atoi (optarg
) : 1;
6090 usage (argv
[0], EXIT_SUCCESS
);
6094 if ((validate_only
|| validate_and_write
) && !*loadfile
)
6095 usage (argv
[0], EXIT_FAILURE
);
6097 if (access (config
.configfile
, R_OK
) == 0)
6098 parse_rcfile (config
.configfile
);
6101 pgn_config_set (PGN_STOP_ON_ERROR
, 1);
6103 memset (&sigact
, 0, sizeof (sigact
));
6104 sigact
.sa_flags
= SA_SIGINFO
;
6105 sigact
.sa_sigaction
= catch_signal
;
6106 sigaction (SIGPIPE
, &sigact
, NULL
);
6107 sigaction (SIGCONT
, &sigact
, NULL
);
6108 sigaction (SIGSTOP
, &sigact
, NULL
);
6109 sigaction (SIGINT
, &sigact
, NULL
);
6110 sigaction (SIGALRM
, &sigact
, NULL
);
6111 sigaction (SIGTERM
, &sigact
, NULL
);
6112 signal (SIGCHLD
, SIG_IGN
);
6114 srandom (getpid ());
6119 if (pgn_open (loadfile
, "r", &pgn
) != E_PGN_OK
)
6120 err (EXIT_FAILURE
, "%s", loadfile
);
6122 ret
= pgn_parse (pgn
);
6126 //ret = parse_fen_file(loadfile);
6128 case FILE_EPD
: // Not implemented.
6131 // No file specified. Empty game.
6132 ret
= pgn_parse (NULL
);
6134 add_custom_tags (&gp
->tag
);
6138 if (validate_only
|| validate_and_write
)
6140 if (validate_and_write
)
6142 if (pgn_open ("-", "r", &pgn
) != E_PGN_OK
)
6143 err (EXIT_FAILURE
, "pgn_open()");
6145 for (i
= 0; i
< gtotal
; i
++)
6147 if (write_custom_tags
)
6148 add_custom_tags (&game
[i
]->tag
);
6150 pgn_write (pgn
, game
[i
]);
6155 fm_loaded_file
= TRUE
;
6161 else if (ret
== E_PGN_ERR
)
6164 if (utf8_pieces
!= -1)
6165 config
.utf8_pieces
= utf8_pieces
;
6167 init_wchar_pieces ();
6168 yes_wchar
= str_to_wchar (_("y"));
6169 all_wchar
= str_to_wchar (_("a"));
6170 overwrite_wchar
= str_to_wchar (_("o"));
6171 resume_wchar
= str_to_wchar (_("r"));
6172 current_wchar
= str_to_wchar (_("c"));
6173 append_wchar
= str_to_wchar (_("a"));
6174 translatable_tag_names
[0] = _("Event");
6175 translatable_tag_names
[1] = _("Site");
6176 translatable_tag_names
[2] = _("Date");
6177 translatable_tag_names
[3] = _("Round");
6178 translatable_tag_names
[4] = _("White");
6179 translatable_tag_names
[5] = _("Black");
6180 translatable_tag_names
[6] = _("Result");
6184 * This fixes window resizing in an xterm.
6186 if (getenv ("DISPLAY") != NULL
)
6188 putenv ((char *) "LINES=");
6189 putenv ((char *) "COLUMNS=");
6192 if (initscr () == NULL
)
6193 errx (EXIT_FAILURE
, "%s", _("Could not initialize curses."));
6195 curses_initialized
= 1;
6197 if (LINES
< 23 || COLS
< 74)
6200 errx (EXIT_FAILURE
, _("Need at least an 74x23 terminal."));
6206 if (has_colors () == TRUE
&& start_color () == OK
)
6207 init_color_pairs ();
6209 boardw
= newwin (BOARD_HEIGHT
, BOARD_WIDTH
, 0, COLS
- BOARD_WIDTH
);
6210 boardp
= new_panel (boardw
);
6211 historyw
= newwin (HISTORY_HEIGHT
, HISTORY_WIDTH
, LINES
- HISTORY_HEIGHT
,
6212 COLS
- HISTORY_WIDTH
);
6213 historyp
= new_panel (historyw
);
6214 statusw
= newwin (STATUS_HEIGHT
, STATUS_WIDTH
, 0, 0);
6215 statusp
= new_panel (statusw
);
6216 tagw
= newwin (TAG_HEIGHT
, TAG_WIDTH
, STATUS_HEIGHT
+ 1, 0);
6217 tagp
= new_panel (tagw
);
6218 keypad (boardw
, TRUE
);
6219 // leaveok(boardw, TRUE);
6220 leaveok (tagw
, TRUE
);
6221 leaveok (statusw
, TRUE
);
6222 leaveok (historyw
, TRUE
);
6226 draw_window_decor ();
6229 free (w_pawn_wchar
);
6230 free (w_rook_wchar
);
6231 free (w_bishop_wchar
);
6232 free (w_knight_wchar
);
6233 free (w_queen_wchar
);
6234 free (w_king_wchar
);
6235 free (b_pawn_wchar
);
6236 free (b_rook_wchar
);
6237 free (b_bishop_wchar
);
6238 free (b_knight_wchar
);
6239 free (b_queen_wchar
);
6240 free (b_king_wchar
);
6242 free (enpassant_wchar
);
6245 free (overwrite_wchar
);
6246 free (resume_wchar
);
6247 free (current_wchar
);
6248 free (append_wchar
);
6249 free (status
.notify
);
6250 exit (EXIT_SUCCESS
);