1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2018 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-2018 " 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 ();
219 coordofmove (GAME g
, char *move
, char *prow
, char *pcol
)
221 char l
= strlen (move
);
225 *prow
= (g
->turn
== WHITE
) ? 8 : 1;
226 *pcol
= (l
<= 4) ? 7 : 3;
232 while (!isdigit (*move
))
235 *prow
= RANKTOINT (*move
--);
236 *pcol
= FILETOINT (*move
);
239 #define INV_INT(x) (9 - x)
240 #define INV_INT0(x) (7 - x)
242 // Posición por rotación de tablero.
243 // Rotation board position.
245 rotate_position (char *prow
, char *pcol
)
247 *prow
= INV_INT (*prow
);
248 *pcol
= INV_INT (*pcol
);
252 update_cursor (GAME g
, int idx
)
254 int t
= pgn_history_total (g
->hp
);
255 struct userdata_s
*d
= g
->data
;
258 * If not deincremented then r and c would be the next move.
262 if (idx
> t
|| idx
< 0 || !t
|| !g
->hp
[idx
]->move
)
263 d
->c_row
= 2, d
->c_col
= 5;
265 coordofmove (g
, g
->hp
[idx
]->move
, &d
->c_row
, &d
->c_col
);
267 if (d
->mode
== MODE_HISTORY
&& d
->rotate
)
268 rotate_position (&d
->c_row
, &d
->c_col
);
278 if ((fp
= fopen (config
.nagfile
, "r")) == NULL
)
280 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.nagfile
,
285 nags
= Realloc (nags
, (i
+ 2) * sizeof (char *));
286 nags
[i
++] = strdup (_("none"));
291 if (fscanf (fp
, " %[^\n] ", line
) == 1)
293 nags
= Realloc (nags
, (i
+ 2) * sizeof (char *));
294 nags
[i
++] = strdup (line
);
305 edit_nag_toggle_item (struct menu_input_s
*m
)
307 struct input_s
*in
= m
->data
;
308 struct input_data_s
*id
= in
->data
;
309 HISTORY
*h
= id
->data
;
312 if (m
->selected
== 0)
314 for (i
= 0; i
< MAX_PGN_NAG
; i
++)
317 for (i
= 0; m
->items
[i
]; i
++)
318 m
->items
[i
]->selected
= 0;
323 for (i
= 0; i
< MAX_PGN_NAG
; i
++)
325 if (h
->nag
[i
] == m
->selected
)
326 h
->nag
[i
] = m
->selected
= 0;
331 h
->nag
[i
] = m
->selected
;
339 edit_nag_save (struct menu_input_s
*m
)
345 edit_nag_help (struct menu_input_s
*m
)
347 message (_("NAG Menu Keys"), ANY_KEY_STR
, "%s",
348 _(" UP/DOWN - previous/next menu item\n"
349 " HOME/END - first/last menu item\n"
350 " PGDN/PGUP - next/previous page\n"
351 " a-zA-Z0-9 - jump to item\n"
352 " SPACE - toggle selected item\n"
353 " CTRL-X - quit with changes"));
356 struct menu_item_s
**
357 get_nag_items (WIN
* win
)
360 struct menu_input_s
*m
= win
->data
;
361 struct input_s
*in
= m
->data
;
362 struct input_data_s
*id
= in
->data
;
363 struct menu_item_s
**items
= m
->items
;
364 HISTORY
*h
= id
->data
;
368 for (i
= 0; items
[i
]; i
++)
372 for (i
= 0; nags
[i
]; i
++)
374 items
= Realloc (items
, (i
+ 2) * sizeof (struct menu_item_s
*));
375 items
[i
] = Malloc (sizeof (struct menu_item_s
));
376 items
[i
]->name
= nags
[i
];
377 items
[i
]->value
= NULL
;
379 for (n
= 0; n
< MAX_PGN_NAG
; n
++)
383 items
[i
]->selected
= 1;
390 items
[i
]->selected
= 0;
400 nag_print (WIN
* win
)
402 struct menu_input_s
*m
= win
->data
;
404 mvwprintw (win
->w
, m
->print_line
, 1, "%-*s", win
->cols
- 2, m
->item
->name
);
410 struct menu_key_s
**keys
= NULL
;
418 add_menu_key (&keys
, ' ', edit_nag_toggle_item
);
419 add_menu_key (&keys
, CTRL_KEY ('x'), edit_nag_save
);
420 add_menu_key (&keys
, keycode_lookup (global_keys
, do_global_help
),
422 construct_menu (0, 0, -1, -1, _("Numeric Annotation Glyphs"), 1,
423 get_nag_items
, keys
, arg
, nag_print
, NULL
, NULL
);
430 HISTORY
*h
= (HISTORY
*) arg
;
432 char line
[LINE_MAX
] = { 0 };
435 snprintf (buf
, sizeof (buf
), "%s \"%s\"", _("Viewing NAG for"), h
->move
);
443 for (i
= 0; i
< MAX_PGN_NAG
; i
++)
450 if (h
->nag
[i
] >= nag_total
)
451 strncat (line
, itoa (h
->nag
[i
], buf2
), sizeof (line
) - 1);
453 strncat (line
, nags
[h
->nag
[i
]], sizeof (line
) - 1);
455 strncat (line
, "\n", sizeof (line
) - 1);
458 line
[strlen (line
) - 1] = 0;
459 message (buf
, ANY_KEY_STR
, "%s", line
);
464 view_annotation (HISTORY
* h
)
466 char buf
[MAX_SAN_MOVE_LEN
+ strlen (_("Viewing Annotation for")) + 4];
467 int nag
= 0, comment
= 0;
472 if (h
->comment
&& h
->comment
[0])
478 if (!nag
&& !comment
)
481 snprintf (buf
, sizeof (buf
), "%s \"%s\"", _("Viewing Annotation for"),
485 construct_message (buf
,
486 (nag
) ? _("Any other key to continue") : ANY_KEY_STR
,
487 0, 1, (nag
) ? _("Press 'n' to view NAG") : NULL
,
488 (nag
) ? view_nag
: NULL
, (nag
) ? h
: NULL
, NULL
,
489 (nag
) ? 'n' : 0, 0, NULL
, "%s", h
->comment
);
491 construct_message (buf
, _("Any other key to continue"), 0, 1,
492 _("Press 'n' to view NAG"), view_nag
, h
, NULL
, 'n', 0,
493 NULL
, "%s", _("No comment text for this move"));
497 do_game_write (char *filename
, const char *mode
, int start
, int end
)
500 struct userdata_s
*d
;
503 i
= pgn_open (filename
, mode
, &pgn
);
507 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s\n%s", filename
, strerror (errno
));
510 else if (i
== E_PGN_INVALID
)
512 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s\n%s", filename
,
513 _("Not a regular file"));
517 for (i
= (start
== -1) ? 0 : start
; i
< end
; i
++)
520 pgn_write (pgn
, game
[i
]);
521 CLEAR_FLAG (d
->flags
, CF_MODIFIED
);
524 if (pgn_close (pgn
) != E_PGN_OK
)
525 message (ERROR_STR
, ANY_KEY_STR
, "%s", strerror (errno
));
529 strncpy (loadfile
, filename
, sizeof (loadfile
));
530 loadfile
[sizeof (loadfile
) - 1] = 0;
545 do_save_game_overwrite_confirm (WIN
* win
)
547 const char *mode
= "w";
548 struct save_game_s
*s
= win
->data
;
549 wchar_t str
[] = { win
->c
, 0 };
551 if (!wcscmp (str
, append_wchar
))
553 else if (!wcscmp (str
, overwrite_wchar
))
558 if (do_game_write (s
->filename
, mode
, s
->start
, s
->end
))
559 update_status_notify (gp
, "%s", _("Save game failed."));
561 update_status_notify (gp
, "%s", _("Game saved."));
568 /* If the saveindex argument is -1, all games will be saved. Otherwise it's a
572 save_pgn (char *filename
, int saveindex
)
574 char buf
[FILENAME_MAX
];
576 int end
= (saveindex
== -1) ? gtotal
: saveindex
+ 1;
577 struct save_game_s
*s
;
579 if (filename
[0] != '/' && config
.savedirectory
)
581 if (stat (config
.savedirectory
, &st
) == -1)
585 if (mkdir (config
.savedirectory
, 0755) == -1)
587 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s: %s",
588 config
.savedirectory
, strerror (errno
));
594 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s: %s",
595 config
.savedirectory
, strerror (errno
));
600 if (stat (config
.savedirectory
, &st
) == -1)
602 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
,
607 if (!S_ISDIR (st
.st_mode
))
609 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s: %s", config
.savedirectory
,
610 _("Not a directory."));
614 snprintf (buf
, sizeof (buf
), "%s/%s", config
.savedirectory
, filename
);
618 if (access (filename
, W_OK
) == 0)
620 s
= Malloc (sizeof (struct save_game_s
));
621 s
->filename
= strdup (filename
);
622 s
->start
= saveindex
;
624 construct_message (NULL
, _("What would you like to do?"), 0, 1, NULL
,
625 NULL
, s
, do_save_game_overwrite_confirm
, 0, 0, NULL
,
626 "%s \"%s\"\nPress \"%ls\" to append to this file, \"%ls\" to overwrite or any other key to cancel.",
627 _("File exists:"), filename
, append_wchar
,
632 if (do_game_write (filename
, "a", saveindex
, end
))
633 update_status_notify (gp
, "%s", _("Save game failed."));
635 update_status_notify (gp
, "%s", _("Game saved."));
639 castling_state (GAME g
, BOARD b
, int row
, int col
, int piece
, int mod
)
641 if (pgn_piece_to_int (piece
) == ROOK
&& col
== 7
643 (TEST_FLAG (g
->flags
, GF_WK_CASTLE
) || mod
) &&
644 pgn_piece_to_int (b
[7][4].icon
) == KING
&& isupper (piece
))
647 TOGGLE_FLAG (g
->flags
, GF_WK_CASTLE
);
650 else if (pgn_piece_to_int (piece
) == ROOK
&& col
== 0
652 (TEST_FLAG (g
->flags
, GF_WQ_CASTLE
) || mod
) &&
653 pgn_piece_to_int (b
[7][4].icon
) == KING
&& isupper (piece
))
656 TOGGLE_FLAG (g
->flags
, GF_WQ_CASTLE
);
659 else if (pgn_piece_to_int (piece
) == ROOK
&& col
== 7
661 (TEST_FLAG (g
->flags
, GF_BK_CASTLE
) || mod
) &&
662 pgn_piece_to_int (b
[0][4].icon
) == KING
&& islower (piece
))
665 TOGGLE_FLAG (g
->flags
, GF_BK_CASTLE
);
668 else if (pgn_piece_to_int (piece
) == ROOK
&& col
== 0
670 (TEST_FLAG (g
->flags
, GF_BQ_CASTLE
) || mod
) &&
671 pgn_piece_to_int (b
[0][4].icon
) == KING
&& islower (piece
))
674 TOGGLE_FLAG (g
->flags
, GF_BQ_CASTLE
);
677 else if (pgn_piece_to_int (piece
) == KING
&& col
== 4
679 (mod
|| (pgn_piece_to_int (b
[7][7].icon
) == ROOK
&&
680 TEST_FLAG (g
->flags
, GF_WK_CASTLE
))
682 (pgn_piece_to_int (b
[7][0].icon
) == ROOK
&&
683 TEST_FLAG (g
->flags
, GF_WQ_CASTLE
))) && isupper (piece
))
687 if (TEST_FLAG (g
->flags
, GF_WK_CASTLE
) ||
688 TEST_FLAG (g
->flags
, GF_WQ_CASTLE
))
689 CLEAR_FLAG (g
->flags
, GF_WK_CASTLE
| GF_WQ_CASTLE
);
691 SET_FLAG (g
->flags
, GF_WK_CASTLE
| GF_WQ_CASTLE
);
695 else if (pgn_piece_to_int (piece
) == KING
&& col
== 4
697 (mod
|| (pgn_piece_to_int (b
[0][7].icon
) == ROOK
&&
698 TEST_FLAG (g
->flags
, GF_BK_CASTLE
))
700 (pgn_piece_to_int (b
[0][0].icon
) == ROOK
&&
701 TEST_FLAG (g
->flags
, GF_BQ_CASTLE
))) && islower (piece
))
705 if (TEST_FLAG (g
->flags
, GF_BK_CASTLE
) ||
706 TEST_FLAG (g
->flags
, GF_BQ_CASTLE
))
707 CLEAR_FLAG (g
->flags
, GF_BK_CASTLE
| GF_BQ_CASTLE
);
709 SET_FLAG (g
->flags
, GF_BK_CASTLE
| GF_BQ_CASTLE
);
717 #define IS_ENPASSANT(c) (c == 'x') ? CP_BOARD_ENPASSANT : isupper(c) ? CP_BOARD_WHITE : CP_BOARD_BLACK
718 #define ATTRS(cp) (cp & (A_BOLD|A_STANDOUT|A_BLINK|A_DIM|A_UNDERLINE|A_INVIS|A_REVERSE))
723 w_pawn_wchar
= str_to_wchar (config
.utf8_pieces
? "♙" : "P");
724 w_rook_wchar
= str_to_wchar (config
.utf8_pieces
? "♖" : "R");
725 w_bishop_wchar
= str_to_wchar (config
.utf8_pieces
? "♗" : "B");
726 w_knight_wchar
= str_to_wchar (config
.utf8_pieces
? "♘" : "N");
727 w_queen_wchar
= str_to_wchar (config
.utf8_pieces
? "♕" : "Q");
728 w_king_wchar
= str_to_wchar (config
.utf8_pieces
? "♔" : "K");
729 b_pawn_wchar
= str_to_wchar (config
.utf8_pieces
? "♟" : "p");
730 b_rook_wchar
= str_to_wchar (config
.utf8_pieces
? "♜" : "r");
731 b_bishop_wchar
= str_to_wchar (config
.utf8_pieces
? "♝" : "b");
732 b_knight_wchar
= str_to_wchar (config
.utf8_pieces
? "♞" : "n");
733 b_queen_wchar
= str_to_wchar (config
.utf8_pieces
? "♛" : "q");
734 b_king_wchar
= str_to_wchar (config
.utf8_pieces
? "♚" : "k");
735 empty_wchar
= str_to_wchar (" ");
736 enpassant_wchar
= str_to_wchar ("x");
740 piece_to_wchar (unsigned char p
)
753 return w_bishop_wchar
;
755 return b_bishop_wchar
;
757 return w_knight_wchar
;
759 return b_knight_wchar
;
761 return w_queen_wchar
;
763 return b_queen_wchar
;
769 return enpassant_wchar
;
776 piece_can_attack (GAME g
, int rank
, int file
)
778 struct userdata_s
*d
= g
->data
;
779 char *m
, *frfr
= NULL
;
781 int row
, col
, p
, v
, pi
, cpi
;
785 rotate_position (&d
->c_row
, &d
->c_col
);
786 rotate_position (&d
->sp
.srow
, &d
->sp
.scol
);
789 v
= d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].valid
;
790 pi
= pgn_piece_to_int (d
->b
[RANKTOBOARD (rank
)][FILETOBOARD (file
)].icon
);
793 pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->sp
.srow
)]
794 [FILETOBOARD (d
->sp
.scol
)].icon
) :
795 pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->c_row
)]
796 [FILETOBOARD (d
->c_col
)].icon
);
798 if (pi
== OPEN_SQUARE
|| cpi
== OPEN_SQUARE
|| !VALIDFILE (file
)
799 || !VALIDRANK (rank
))
803 rotate_position (&d
->c_row
, &d
->c_col
);
804 rotate_position (&d
->sp
.srow
, &d
->sp
.scol
);
812 col
= v
? d
->c_col
: d
->sp
.scol
;
813 row
= v
? d
->c_row
: d
->sp
.srow
;
821 m
= malloc (MAX_SAN_MOVE_LEN
+ 1);
822 m
[0] = INTTOFILE (file
);
823 m
[1] = INTTORANK (rank
);
824 m
[2] = INTTOFILE (col
);
825 m
[3] = INTTORANK (row
);
832 memcpy (b
, d
->b
, sizeof (BOARD
));
833 p
= b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
;
834 b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
835 pgn_int_to_piece (WHITE
, OPEN_SQUARE
);
836 b
[RANKTOBOARD (row
)][FILETOBOARD (col
)].icon
= p
;
838 e
= pgn_validate_move (g
, b
, &m
, &frfr
);
843 if (e
!= E_PGN_OK
&& pgn_piece_to_int (d
->sp
.icon
) == PAWN
)
845 int n
= (d
->sp
.srow
== 7 && islower (d
->sp
.icon
) && rank
== 5) ? 6 :
846 (d
->sp
.srow
== 2 && isupper (d
->sp
.icon
) && rank
== 4) ? 3 : 0;
848 if (n
&& (file
== d
->c_col
- 1 || file
== d
->c_col
+ 1))
850 memcpy (b
, d
->b
, sizeof (BOARD
));
851 p
= b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
;
852 b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
853 pgn_int_to_piece (WHITE
, OPEN_SQUARE
);
854 b
[RANKTOBOARD (row
)][FILETOBOARD (col
)].icon
= p
;
855 b
[RANKTOBOARD (n
)][FILETOBOARD (d
->sp
.scol
)].enpassant
= 1;
856 m
= malloc (MAX_SAN_MOVE_LEN
+ 1);
857 m
[0] = INTTOFILE (file
);
858 m
[1] = INTTORANK (rank
);
859 m
[2] = INTTOFILE (col
);
860 m
[3] = INTTORANK (n
);
863 SET_FLAG (g
->flags
, GF_ENPASSANT
);
864 e
= pgn_validate_move (g
, b
, &m
, &frfr
);
865 CLEAR_FLAG (g
->flags
, GF_ENPASSANT
);
876 e
= pgn_validate_move (g
, d
->b
, &m
, &frfr
);
879 if (!strcmp (m
, "O-O") || !strcmp (m
, "O-O-O"))
884 int sf
= FILETOINT (frfr
[0]), sr
= RANKTOINT (frfr
[1]);
885 int df
= FILETOINT (frfr
[2]);
887 pi
= d
->b
[RANKTOBOARD (sr
)][FILETOBOARD (sf
)].icon
;
888 pi
= pgn_piece_to_int (pi
);
889 if (pi
== PAWN
&& sf
== df
)
899 rotate_position (&d
->c_row
, &d
->c_col
);
900 rotate_position (&d
->sp
.srow
, &d
->sp
.scol
);
903 return e
== E_PGN_OK
? 1 : 0;
907 print_piece (WINDOW
* w
, int l
, int c
, char p
)
911 for (i
= 0; i
< 13; i
+= 2)
913 if (p
== piece_chars
[i
] || p
== piece_chars
[i
+ 1])
915 for (y
= 0; y
< 3; y
++)
916 mvwprintw (w
, l
+ y
, c
, "%s", f_pieces
[i
+ ff
+ y
]);
923 for (y
= 0; y
< 3; y
++)
924 mvwprintw (w
, l
+ y
, c
, f_pieces
[0]);
928 board_prev_move_play (GAME g
)
930 struct userdata_s
*d
= g
->data
;
931 char l
= strlen (d
->pm_frfr
);
935 char q
= (l
> 4) ? 2 : 1;
937 d
->pm_row
= RANKTOINT (d
->pm_frfr
[l
- q
++]);
938 d
->pm_col
= FILETOINT (d
->pm_frfr
[l
- q
++]);
939 d
->ospm_row
= RANKTOINT (d
->pm_frfr
[l
- q
++]);
940 d
->ospm_col
= FILETOINT (d
->pm_frfr
[l
- q
]);
943 rotate_position (&d
->pm_row
, &d
->pm_col
);
944 rotate_position (&d
->ospm_row
, &d
->ospm_col
);
957 board_prev_move_history (GAME g
)
959 struct userdata_s
*d
= g
->data
;
963 char *move
= g
->hp
[g
->hindex
- 1]->move
;
967 if (d
->mode
== MODE_PLAY
)
968 coordofmove (g
, move
, &d
->pm_row
, &d
->pm_col
);
977 d
->ospm_row
= g
->turn
== WHITE
? 8 : 1;
989 HISTORY
*h
= pgn_history_by_n (g
->hp
, g
->hindex
- 2);
992 pgn_board_init_fen (g
, ob
, h
->fen
);
997 for (f
= 0; f
< 8; f
++)
999 for (r
= 0; r
< 8; r
++)
1001 if (ob
[f
][r
].icon
!= '.' && d
->b
[f
][r
].icon
== '.')
1003 d
->ospm_row
= INV_INT0 (f
) + 1;
1004 d
->ospm_col
= r
+ 1;
1012 if (d
->mode
== MODE_PLAY
)
1013 rotate_position (&d
->pm_row
, &d
->pm_col
);
1015 rotate_position (&d
->ospm_row
, &d
->ospm_col
);
1031 is_the_square (int brow
, int bcol
, int row
, int col
, int prow
, int pcol
)
1033 if ((!BIG_BOARD
&& row
== ROWTOMATRIX (prow
) && col
== COLTOMATRIX (pcol
))
1034 || (BIG_BOARD
&& brow
+ 1 == INV_INT (prow
) && bcol
+ 1 == pcol
))
1041 is_prev_move (struct userdata_s
*d
, int brow
, int bcol
, int row
, int col
)
1043 if (is_the_square (brow
, bcol
, row
, col
, d
->pm_row
, d
->pm_col
))
1050 update_board_window (GAME g
)
1053 int bcol
= 0, brow
= 0;
1054 int l
= config
.coordsyleft
;
1055 int maxy
= BOARD_HEIGHT
, maxx
= BOARD_WIDTH
;
1056 int ncols
= 0, offset
= 1;
1057 int rowr
= (MEGA_BOARD
) ? 6 : (BIG_BOARD
) ? 4 : 2;
1058 int colr
= (MEGA_BOARD
) ? 12 : (BIG_BOARD
) ? 8 : 4;
1059 unsigned coords_y
= 8, cxgc
= 0;
1060 unsigned i
, cpd
= 0;
1061 struct userdata_s
*d
= g
->data
;
1063 if (config
.bprevmove
&& d
->mode
!= MODE_EDIT
)
1065 if (!d
->pm_undo
&& d
->mode
== MODE_PLAY
)
1066 board_prev_move_play (g
);
1068 board_prev_move_history (g
);
1078 if (d
->mode
!= MODE_PLAY
&& d
->mode
!= MODE_EDIT
)
1079 update_cursor (g
, g
->hindex
);
1100 for (row
= 0; row
< maxy
; row
++)
1117 for (col
= 0; col
< maxx
; col
++)
1120 chtype attrs
= 0, old_attrs
= 0;
1125 if (row
== 0 || row
== maxy
- 2)
1128 mvwaddch (boardw
, row
, col
+ l
,
1130 ? ACS_LLCORNER
| CP_BOARD_GRAPHICS
1131 : ACS_ULCORNER
| CP_BOARD_GRAPHICS
));
1132 else if (col
== maxx
- 2)
1133 mvwaddch (boardw
, row
, col
+ l
,
1135 ? ACS_LRCORNER
| CP_BOARD_GRAPHICS
1136 : ACS_URCORNER
| CP_BOARD_GRAPHICS
));
1137 else if (!(col
% colr
))
1138 mvwaddch (boardw
, row
, col
+ l
,
1140 ? ACS_BTEE
| CP_BOARD_GRAPHICS
1141 : ACS_TTEE
| CP_BOARD_GRAPHICS
));
1144 if (col
!= maxx
- 1)
1145 mvwaddch (boardw
, row
, col
+ l
,
1146 LINE_GRAPHIC (ACS_HLINE
| CP_BOARD_GRAPHICS
));
1152 if ((row
% 2) && col
== maxx
- 1 && (coords_y
> 0 && coords_y
< 9))
1154 wattron (boardw
, CP_BOARD_COORDS
);
1156 (BIG_BOARD
) ? row
* ((MEGA_BOARD
) ? 3 : 2)
1157 : row
, (l
) ? 0 : col
, "%d",
1158 (d
->rotate
) ? coords_y
++ : coords_y
--);
1159 wattroff (boardw
, CP_BOARD_COORDS
);
1163 if ((col
== 0 || col
== maxx
- 2) && row
!= maxy
- 1)
1166 mvwaddch (boardw
, row
, col
+ l
,
1167 LINE_GRAPHIC ((col
) ?
1168 ACS_RTEE
| CP_BOARD_GRAPHICS
:
1169 ACS_LTEE
| CP_BOARD_GRAPHICS
));
1171 mvwaddch (boardw
, row
, col
+ l
,
1172 LINE_GRAPHIC (ACS_VLINE
| CP_BOARD_GRAPHICS
));
1177 if ((row
% rowr
) && !(col
% colr
) && row
!= maxy
- 1)
1179 mvwaddch (boardw
, row
, col
+ l
,
1180 LINE_GRAPHIC (ACS_VLINE
| CP_BOARD_GRAPHICS
));
1184 if (!(col
% colr
) && row
!= maxy
- 1)
1186 mvwaddch (boardw
, row
, col
+ l
,
1187 LINE_GRAPHIC (ACS_PLUS
| CP_BOARD_GRAPHICS
));
1196 attrwhich
= (cb
[brow
][bcol
]) ? WHITE
: BLACK
;
1205 if (((ncols
% 2) && !(offset
% 2))
1206 || (!(ncols
% 2) && (offset
% 2)))
1212 if (BIG_BOARD
&& d
->rotate
)
1214 brow
= INV_INT0 (brow
);
1215 bcol
= INV_INT0 (bcol
);
1219 p
= d
->b
[brow
][bcol
].icon
;
1221 p
= d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].icon
;
1223 int pi
= pgn_piece_to_int (p
);
1225 if (config
.details
&&
1228 b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].enpassant
)
1229 || (BIG_BOARD
&& d
->b
[brow
][bcol
].enpassant
)))
1232 attrs
= mix_cp (CP_BOARD_ENPASSANT
,
1234 WHITE
) ? CP_BOARD_WHITE
:
1236 ATTRS (CP_BOARD_ENPASSANT
), A_FG_B_BG
);
1239 if (config
.showattacks
&& config
.details
1240 && piece_can_attack (g
,
1241 BIG_BOARD
? INV_INT0 (brow
) +
1243 BIG_BOARD
? bcol
+ 1 : bcol
))
1245 attrs
= CP_BOARD_ATTACK
;
1250 if (config
.validmoves
&&
1252 && d
->b
[RANKTOBOARD (brow
)][FILETOBOARD (bcol
)].valid
)
1253 || (BIG_BOARD
&& d
->b
[brow
][bcol
].valid
)))
1258 if (attrwhich
== WHITE
)
1259 attrs
= mix_cp (CP_BOARD_MOVES_WHITE
,
1261 ATTRS (CP_BOARD_MOVES_WHITE
),
1264 attrs
= mix_cp (CP_BOARD_MOVES_BLACK
,
1266 ATTRS (CP_BOARD_MOVES_BLACK
),
1269 else if (p
!= 'x' && !can_attack
)
1271 (attrwhich
== WHITE
) ? CP_BOARD_WHITE
: CP_BOARD_BLACK
;
1273 if (BIG_BOARD
&& d
->rotate
)
1275 brow
= INV_INT0 (brow
);
1276 bcol
= INV_INT0 (bcol
);
1279 if (is_the_square (brow
, bcol
, row
, col
, d
->c_row
,
1282 attrs
= mix_cp (CP_BOARD_CURSOR
, IS_ENPASSANT (p
),
1283 ATTRS (CP_BOARD_CURSOR
), B_FG_A_BG
);
1286 else if (is_the_square (brow
, bcol
, row
, col
,
1287 d
->sp
.srow
, d
->sp
.scol
))
1289 attrs
= mix_cp (CP_BOARD_SELECTED
, IS_ENPASSANT (p
),
1290 ATTRS (CP_BOARD_SELECTED
), B_FG_A_BG
);
1293 else if ((is_prev_move (d
, brow
, bcol
, row
, col
) && !valid
)
1294 || (is_the_square (brow
, bcol
, row
, col
,
1295 d
->ospm_row
, d
->ospm_col
)))
1297 attrs
= mix_cp (CP_BOARD_PREVMOVE
, IS_ENPASSANT (p
),
1298 ATTRS (CP_BOARD_PREVMOVE
), B_FG_A_BG
);
1302 if (row
== maxy
- 1)
1307 int n
= is_prev_move (d
, brow
, bcol
, row
, col
);
1308 chtype a
= n
&& !valid
1309 ? CP_BOARD_PREVMOVE
: attrwhich
==
1310 WHITE
? valid
? CP_BOARD_MOVES_WHITE
: CP_BOARD_WHITE
1311 : valid
? CP_BOARD_MOVES_BLACK
: CP_BOARD_BLACK
;
1313 mix_cp (CP_BOARD_ATTACK
, a
, ATTRS (CP_BOARD_ATTACK
),
1319 wmove (boardw
, row
, col
+ ((MEGA_BOARD
) ? 5 : 3) + l
);
1321 mvwaddch (boardw
, row
, col
+ l
, ' ' | attrs
);
1323 if (row
== maxy
- 1 && cxgc
< 8)
1326 "abcdefgh"[(BIG_BOARD
) ? bcol
: bcol
-
1327 1] | CP_BOARD_COORDS
);
1332 if (old_attrs
== -1)
1340 if (pi
!= OPEN_SQUARE
&& p
!= 'x' && !can_attack
)
1342 if (attrwhich
== WHITE
)
1345 attrs
= CP_BOARD_W_W
;
1347 attrs
= CP_BOARD_W_B
;
1352 attrs
= CP_BOARD_B_W
;
1354 attrs
= CP_BOARD_B_B
;
1361 if (config
.details
&& !can_attack
1362 && castling_state (g
, d
->b
,
1363 (d
->rotate
) ? INV_INT0 (brow
+ 1) - 1 : brow
,
1364 (d
->rotate
) ? INV_INT0 (bcol
+ 1) - 1 : bcol
,
1366 attrs
= mix_cp (CP_BOARD_CASTLING
, attrs
,
1367 ATTRS (CP_BOARD_CASTLING
), A_FG_B_BG
);
1371 if (config
.details
&& !can_attack
1372 && castling_state (g
, d
->b
, RANKTOBOARD (brow
),
1373 FILETOBOARD (bcol
), p
, 0))
1375 attrs
= mix_cp (CP_BOARD_CASTLING
, attrs
,
1376 ATTRS (CP_BOARD_CASTLING
),
1383 // FIXME: Reimpresión de piezas(+4).
1386 wattron (boardw
, attrs
);
1389 for (i
= 0; i
< 5; i
++)
1390 mvwprintw (boardw
, i
+ brow
* 6 + 1,
1393 if (pi
!= OPEN_SQUARE
)
1394 print_piece (boardw
, brow
* 6 + 2,
1395 bcol
* 12 + 3 + l
, p
);
1399 print_piece (boardw
, brow
* 4 + 1,
1401 (pi
!= OPEN_SQUARE
) ? p
: 0);
1404 wattroff (boardw
, attrs
);
1410 wattron (boardw
, attrs
);
1412 piece_to_wchar (pi
!=
1413 OPEN_SQUARE
? p
: 0));
1414 wattroff (boardw
, attrs
);
1421 col
+= (MEGA_BOARD
) ? 10 : 6;
1424 waddch (boardw
, ' ' | attrs
);
1444 if (col
!= maxx
- 1)
1445 mvwaddch (boardw
, row
, col
+ l
,
1446 LINE_GRAPHIC (ACS_HLINE
| CP_BOARD_GRAPHICS
));
1469 invalid_move (int n
, int e
, const char *m
)
1471 if (curses_initialized
)
1472 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s \"%s\" (round #%i)",
1474 E_PGN_AMBIGUOUS
) ? _("Ambiguous move") : _("Invalid move"), m
,
1477 warnx ("%s: %s \"%s\" (round #%i)", loadfile
, (e
== E_PGN_AMBIGUOUS
)
1478 ? _("Ambiguous move") : _("Invalid move"), m
, n
);
1484 struct userdata_s
*d
= g
->data
;
1486 SET_FLAG (g
->flags
, GF_GAMEOVER
);
1487 d
->mode
= MODE_HISTORY
;
1492 update_clock (GAME g
, struct itimerval it
)
1494 struct userdata_s
*d
= g
->data
;
1496 if (TEST_FLAG (d
->flags
, CF_CLOCK
) && g
->turn
== WHITE
)
1498 d
->wclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1499 d
->wclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1501 if (d
->wclock
.elapsed
.tv_usec
> 1000000 - 1)
1503 d
->wclock
.elapsed
.tv_sec
+= d
->wclock
.elapsed
.tv_usec
/ 1000000;
1504 d
->wclock
.elapsed
.tv_usec
= d
->wclock
.elapsed
.tv_usec
% 1000000;
1507 if (d
->wclock
.tc
[d
->wclock
.tcn
][1] &&
1508 d
->wclock
.elapsed
.tv_sec
>= d
->wclock
.tc
[d
->wclock
.tcn
][1])
1510 pgn_tag_add (&g
->tag
, (char *) "Result", (char *) "0-1");
1514 else if (TEST_FLAG (d
->flags
, CF_CLOCK
) && g
->turn
== BLACK
)
1516 d
->bclock
.elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1517 d
->bclock
.elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1519 if (d
->bclock
.elapsed
.tv_usec
> 1000000 - 1)
1521 d
->bclock
.elapsed
.tv_sec
+= d
->bclock
.elapsed
.tv_usec
/ 1000000;
1522 d
->bclock
.elapsed
.tv_usec
= d
->bclock
.elapsed
.tv_usec
% 1000000;
1525 if (d
->bclock
.tc
[d
->bclock
.tcn
][1] &&
1526 d
->bclock
.elapsed
.tv_sec
>= d
->bclock
.tc
[d
->bclock
.tcn
][1])
1528 pgn_tag_add (&g
->tag
, (char *) "Result", (char *) "1-0");
1533 d
->elapsed
.tv_sec
+= it
.it_value
.tv_sec
;
1534 d
->elapsed
.tv_usec
+= it
.it_value
.tv_usec
;
1536 if (d
->elapsed
.tv_usec
> 1000000 - 1)
1538 d
->elapsed
.tv_sec
+= d
->elapsed
.tv_usec
/ 1000000;
1539 d
->elapsed
.tv_usec
= d
->elapsed
.tv_usec
% 1000000;
1544 update_time_control (GAME g
)
1546 struct userdata_s
*d
= g
->data
;
1547 struct clock_s
*clk
= (g
->turn
== WHITE
) ? &d
->wclock
: &d
->bclock
;
1550 clk
->tc
[clk
->tcn
][1] += clk
->incr
;
1552 if (!clk
->tc
[clk
->tcn
][1])
1557 if (!clk
->tc
[clk
->tcn
][0] || clk
->move
>= clk
->tc
[clk
->tcn
][0])
1560 clk
->tc
[clk
->tcn
+ 1][1] +=
1561 labs (clk
->elapsed
.tv_sec
- clk
->tc
[clk
->tcn
][1]);
1562 memset (&clk
->elapsed
, 0, sizeof (clk
->elapsed
));
1568 update_history_window (GAME g
)
1570 char buf
[HISTORY_WIDTH
- 1];
1573 int t
= pgn_history_total (g
->hp
);
1575 n
= (g
->hindex
+ 1) / 2;
1578 total
= (t
+ 1) / 2;
1583 snprintf (buf
, sizeof (buf
), "%u %s %u%s", n
, _("of"), total
,
1584 (movestep
== 1) ? _(" (ply)") : "");
1586 strncpy (buf
, _("not available"), sizeof (buf
) - 1);
1588 buf
[sizeof (buf
) - 1] = 0;
1589 mvwprintw (historyw
, 2, 1, "%*s %-*s", 10, _("Move:"),
1590 HISTORY_WIDTH
- 14, buf
);
1592 h
= pgn_history_by_n (g
->hp
, g
->hindex
);
1593 snprintf (buf
, sizeof (buf
), "%s",
1594 (h
&& h
->move
) ? h
->move
1595 : (LINES
< 24) ? _("empty") : _("not available"));
1598 if (h
&& ((h
->comment
) || h
->nag
[0]))
1600 strncat (buf
, _(" (Annotated"), sizeof (buf
) - 1);
1606 strncat (buf
, (n
) ? ",+" : " (+", sizeof (buf
) - 1);
1612 strncat (buf
, (n
) ? ",-" : " (-", sizeof (buf
) - 1);
1617 strncat (buf
, ")", sizeof (buf
) - 1);
1619 mvwprintw (historyw
, 3, ((LINES
< 24) ? 17 : 1), "%s %-*s",
1620 (LINES
< 24) ? _("Next:") : _("Next move:"),
1621 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1623 h
= pgn_history_by_n (g
->hp
, g
->hindex
- 1);
1624 snprintf (buf
, sizeof (buf
), "%s",
1625 (h
&& h
->move
) ? h
->move
1626 : (LINES
< 24) ? _("empty") : _("not available"));
1629 if (h
&& ((h
->comment
) || h
->nag
[0]))
1631 strncat (buf
, _(" (Annotated"), sizeof (buf
) - 1);
1637 strncat (buf
, (n
) ? ",+" : " (+", sizeof (buf
) - 1);
1643 strncat (buf
, (n
) ? ",-" : " (-", sizeof (buf
) - 1);
1648 strncat (buf
, ")", sizeof (buf
) - 1);
1650 mvwprintw (historyw
, ((LINES
< 24) ? 3 : 4), 1, "%s %-*s",
1651 (LINES
< 24) ? _("Prev.:") : _("Prev move:"),
1652 HISTORY_WIDTH
- ((LINES
< 24) ? 26 : 14), buf
);
1656 do_validate_move (char **move
)
1658 struct userdata_s
*d
= gp
->data
;
1662 if (TEST_FLAG (d
->flags
, CF_HUMAN
))
1664 if ((n
= pgn_parse_move (gp
, d
->b
, move
, &frfr
)) != E_PGN_OK
)
1666 invalid_move (d
->n
+ 1, n
, *move
);
1670 strcpy (d
->pm_frfr
, frfr
);
1671 update_time_control (gp
);
1672 pgn_history_add (gp
, d
->b
, *move
);
1673 pgn_switch_turn (gp
);
1677 if ((n
= pgn_validate_move (gp
, d
->b
, move
, &frfr
)) != E_PGN_OK
)
1679 invalid_move (d
->n
+ 1, n
, *move
);
1683 add_engine_command (gp
, ENGINE_THINKING
, "%s\n",
1684 (config
.engine_protocol
== 1) ? frfr
: *move
);
1687 d
->sp
.srow
= d
->sp
.scol
= d
->sp
.icon
= 0;
1689 if (config
.validmoves
)
1690 pgn_reset_valid_moves (d
->b
);
1692 if (TEST_FLAG (gp
->flags
, GF_GAMEOVER
))
1693 d
->mode
= MODE_HISTORY
;
1695 SET_FLAG (d
->flags
, CF_MODIFIED
);
1699 update_history_window (gp
);
1700 update_board_window (gp
);
1705 do_promotion_piece_finalize (WIN
* win
)
1707 char *p
, *str
= win
->data
;
1709 if (pgn_piece_to_int (win
->c
) == -1)
1712 p
= str
+ strlen (str
);
1713 *p
++ = toupper (win
->c
);
1715 do_validate_move (&str
);
1721 move_to_engine (GAME g
)
1723 struct userdata_s
*d
= g
->data
;
1727 if (config
.validmoves
&&
1728 !d
->b
[RANKTOBOARD (d
->sp
.row
)][FILETOBOARD (d
->sp
.col
)].valid
)
1731 str
= Malloc (MAX_SAN_MOVE_LEN
+ 1);
1732 snprintf (str
, MAX_SAN_MOVE_LEN
+ 1, "%c%u%c%u",
1733 _("abcdefgh")[d
->sp
.scol
- 1],
1734 d
->sp
.srow
, _("abcdefgh")[d
->sp
.col
- 1], d
->sp
.row
);
1737 pgn_piece_to_int (d
->b
[RANKTOBOARD (d
->sp
.srow
)]
1738 [FILETOBOARD (d
->sp
.scol
)].icon
);
1740 if (piece
== PAWN
&& (d
->sp
.row
== 8 || d
->sp
.row
== 1))
1742 construct_message (_("Select Pawn Promotion Piece"), _("R/N/B/Q"), 1, 1,
1743 NULL
, NULL
, str
, do_promotion_piece_finalize
, 0, 0,
1745 _("R = Rook, N = Knight, B = Bishop, Q = Queen"));
1749 do_validate_move (&str
);
1754 clock_to_char (long n
)
1756 static char buf
[16];
1757 int h
= 0, m
= 0, s
= 0;
1760 m
= (n
% 3600) / 60;
1761 s
= (n
% 3600) % 60;
1762 snprintf (buf
, sizeof (buf
), "%.2i:%.2i:%.2i", h
, m
, s
);
1767 timeval_to_char (struct timeval t
, long limit
)
1770 unsigned h
= 0, m
= 0, s
= 0;
1771 int n
= limit
? labs (limit
- t
.tv_sec
) : 0;
1774 m
= (n
% 3600) / 60;
1775 s
= (n
% 3600) % 60;
1776 snprintf (buf
, sizeof (buf
), "%.2u:%.2u:%.2u", h
, m
, s
);
1781 time_control_status (struct clock_s
*clk
)
1783 static char buf
[80] = { 0 };
1787 if (clk
->tc
[clk
->tcn
][0] && clk
->tc
[clk
->tcn
+ 1][1])
1788 snprintf (buf
, sizeof (buf
), " M%.2i/%s",
1789 abs (clk
->tc
[clk
->tcn
][0] - clk
->move
),
1790 clock_to_char (clk
->tc
[clk
->tcn
+ 1][1]));
1791 else if (!clk
->incr
)
1798 strncat (tbuf
, " I", sizeof (tbuf
) - 1);
1799 strncat (tbuf
, itoa (clk
->incr
, buf
), sizeof (tbuf
) - 1);
1806 update_status_window (GAME g
)
1810 char tmp
[15] = { 0 }, *engine
, *mode
;
1816 struct userdata_s
*d
= g
->data
;
1820 if (!curses_initialized
)
1823 getmaxyx (statusw
, maxy
, maxx
);
1830 wchar_t *loadfilew
= loadfile
[0]
1831 ? str_etc (loadfile
, w
, 1) : str_to_wchar (_("not available"));
1832 mvwprintw (statusw
, y
++, 1, "%*s %-*ls", 7, _("File:"), w
, loadfilew
);
1834 snprintf (buf
, len
, "%i %s %i", gindex
+ 1, _("of"), gtotal
);
1835 mvwprintw (statusw
, y
++, 1, "%*s %-*s", 7, _("Game:"), w
, buf
);
1846 if (TEST_FLAG (d
->flags
, CF_DELETE
))
1855 if (TEST_FLAG (g
->flags
, GF_PERROR
))
1864 if (TEST_FLAG (d
->flags
, CF_MODIFIED
))
1873 pgn_config_get (PGN_STRICT_CASTLING
, &n
);
1884 if (TEST_FLAG (d
->flags
, CF_PERL
))
1895 mvwprintw (statusw
, y
++, 1, "%*s %-*s", 7, _("Flags:"), w
,
1896 (tmp
[0]) ? tmp
: "-");
1901 mode
= _("move history");
1910 mode
= _("(empty value)");
1914 snprintf (buf
, len
- 1, "%*s %s", 7, _("Mode:"), mode
);
1916 if (d
->mode
== MODE_PLAY
)
1918 if (TEST_FLAG (d
->flags
, CF_HUMAN
))
1919 strncat (buf
, _(" (human/human)"), len
- 1);
1920 else if (TEST_FLAG (d
->flags
, CF_ENGINE_LOOP
))
1921 strncat (buf
, _(" (engine/engine)"), len
- 1);
1923 strncat (buf
, (d
->play_mode
== PLAY_EH
) ?
1924 _(" (engine/human)") : _(" (human/engine)"), len
- 1);
1928 mvwprintw (statusw
, y
++, 1, "%-*s", len
, buf
);
1933 switch (d
->engine
->status
)
1935 case ENGINE_THINKING
:
1936 engine
= _("pondering...");
1939 engine
= _("ready");
1941 case ENGINE_INITIALIZING
:
1942 engine
= _("initializing...");
1944 case ENGINE_OFFLINE
:
1945 engine
= _("offline");
1948 engine
= _("(empty value)");
1953 engine
= _("offline");
1955 mvwprintw (statusw
, y
, 1, "%*s %-*s", 7, _("Engine:"), w
, " ");
1956 wattron (statusw
, CP_STATUS_ENGINE
);
1957 mvwaddstr (statusw
, y
++, 9, engine
);
1958 wattroff (statusw
, CP_STATUS_ENGINE
);
1960 mvwprintw (statusw
, y
++, 1, "%*s %-*s", 7, _("Turn:"), w
,
1961 (g
->turn
== WHITE
) ? _("white") : _("black"));
1963 strncpy (tmp
, _("white"), sizeof (tmp
) - 1);
1964 tmp
[0] = toupper (tmp
[0]);
1965 snprintf (t
, sizeof (t
), "%s%s",
1966 timeval_to_char (d
->wclock
.elapsed
,
1967 d
->wclock
.tc
[d
->wclock
.tcn
][1]),
1968 time_control_status (&d
->wclock
));
1969 mvwprintw (statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1971 strncpy (tmp
, _("black"), sizeof (tmp
) - 1);
1972 tmp
[0] = toupper (tmp
[0]);
1973 snprintf (t
, sizeof (t
), "%s%s",
1974 timeval_to_char (d
->bclock
.elapsed
,
1975 d
->bclock
.tc
[d
->bclock
.tcn
][1]),
1976 time_control_status (&d
->bclock
));
1977 mvwprintw (statusw
, y
++, 1, "%*s: %-*s", 6, tmp
, w
, t
);
1979 mvwprintw (statusw
, y
++, 1, "%*s %-*s", 7, _("Total:"), w
,
1980 clock_to_char (d
->elapsed
.tv_sec
));
1982 // for (i = 0; i < STATUS_WIDTH; i++)
1983 // mvwprintw(stdscr, STATUS_HEIGHT, i, " ");
1989 snprintf (tbuf
, sizeof (tbuf
), _("Type %ls for help"),
1990 key_lookup (global_keys
, do_global_help
));
1991 status
.notify
= str_to_wchar (tbuf
);
1994 wattron (stdscr
, CP_STATUS_NOTIFY
);
1995 for (i
= (config
.boardleft
) ? BOARD_WIDTH
: 0;
1996 i
< ((config
.boardleft
) ? COLS
: STATUS_WIDTH
); i
++)
1997 mvwprintw (stdscr
, STATUS_HEIGHT
, i
, " ");
1998 mvwprintw (stdscr
, STATUS_HEIGHT
, CENTERX (STATUS_WIDTH
, status
.notify
)
1999 + ((config
.boardleft
) ? BOARD_WIDTH
: 0), "%ls", status
.notify
);
2000 wattroff (stdscr
, CP_STATUS_NOTIFY
);
2004 translate_tag_name (const char *tag
)
2006 if (!strcmp (tag
, "Event"))
2007 return str_to_wchar (translatable_tag_names
[0]);
2008 else if (!strcmp (tag
, "Site"))
2009 return str_to_wchar (translatable_tag_names
[1]);
2010 else if (!strcmp (tag
, "Date"))
2011 return str_to_wchar (translatable_tag_names
[2]);
2012 else if (!strcmp (tag
, "Round"))
2013 return str_to_wchar (translatable_tag_names
[3]);
2014 else if (!strcmp (tag
, "White"))
2015 return str_to_wchar (translatable_tag_names
[4]);
2016 else if (!strcmp (tag
, "Black"))
2017 return str_to_wchar (translatable_tag_names
[5]);
2018 else if (!strcmp (tag
, "Result"))
2019 return str_to_wchar (translatable_tag_names
[6]);
2021 return str_to_wchar (tag
);
2025 update_tag_window (TAG
** t
)
2030 for (i
= 0; t
[i
]; i
++)
2032 wchar_t *namewc
= translate_tag_name (t
[i
]->name
);
2034 l
= wcslen (namewc
);
2040 w
= TAG_WIDTH
- namel
- 4;
2042 for (i
= 0; t
[i
] && i
< TAG_HEIGHT
- 3; i
++)
2044 wchar_t *namewc
= translate_tag_name (t
[i
]->name
);
2045 wchar_t *valuewc
= str_etc (t
[i
]->value
, w
, 0);
2047 mvwprintw (tagw
, (i
+ 2), 1, "%*ls: %-*ls", namel
, namewc
, w
, valuewc
);
2052 for (; i
< TAG_HEIGHT
- 3; i
++)
2053 mvwprintw (tagw
, (i
+ 2), 1, "%*s", namel
+ w
+ 2, " ");
2057 append_enginebuf (GAME g
, char *line
)
2060 struct userdata_s
*d
= g
->data
;
2062 if (d
->engine
->enginebuf
)
2063 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++);
2067 free (d
->engine
->enginebuf
[0]);
2069 for (i
= 0; d
->engine
->enginebuf
[i
+ 1]; i
++)
2070 d
->engine
->enginebuf
[i
] = d
->engine
->enginebuf
[i
+ 1];
2072 d
->engine
->enginebuf
[i
] = strdup (line
);
2076 d
->engine
->enginebuf
=
2077 Realloc (d
->engine
->enginebuf
, (i
+ 2) * sizeof (char *));
2078 d
->engine
->enginebuf
[i
++] = strdup (line
);
2079 d
->engine
->enginebuf
[i
] = NULL
;
2084 update_engine_window (GAME g
)
2087 struct userdata_s
*d
= g
->data
;
2089 wmove (enginew
, 0, 0);
2090 wclrtobot (enginew
);
2092 if (d
->engine
&& d
->engine
->enginebuf
)
2094 for (i
= 0; d
->engine
->enginebuf
[i
]; i
++)
2095 mvwprintw (enginew
, i
+ 2, 1, "%s", d
->engine
->enginebuf
[i
]);
2098 window_draw_title (enginew
, _("Engine IO Window"), COLS
, CP_MESSAGE_TITLE
,
2105 struct userdata_s
*d
= g
->data
;
2108 * In the middle of a macro. Don't update the screen.
2110 if (macro_match
!= -1)
2113 wmove (boardw
, ROWTOMATRIX (d
->c_row
), COLTOMATRIX (d
->c_col
));
2114 update_board_window (g
);
2115 update_status_window (g
);
2116 update_history_window (g
);
2117 update_tag_window (g
->tag
);
2118 update_engine_window (g
);
2124 game_next_prev (GAME g
, int n
, int count
)
2131 if (gindex
+ count
> gtotal
- 1)
2134 gindex
= gtotal
- 1;
2143 if (gindex
- count
< 0)
2148 gindex
= gtotal
- 1;
2158 delete_game (int which
)
2161 struct userdata_s
*d
;
2163 for (i
= 0; i
< gtotal
; i
++)
2167 if (i
== w
|| TEST_FLAG (d
->flags
, CF_DELETE
))
2171 free_userdata_once (game
[i
]);
2174 for (n
= i
; n
+ 1 < gtotal
; n
++)
2175 game
[n
] = game
[n
+ 1];
2185 if (which
+ 1 >= gtotal
)
2186 gindex
= gtotal
- 1;
2191 gindex
= gtotal
- 1;
2194 gp
->hp
= gp
->history
;
2198 * FIXME find across multiple games.
2201 find_move_exp (GAME g
, regex_t r
, int which
, int count
)
2209 incr
= (which
== 0) ? -1 : 1;
2211 for (i
= g
->hindex
+ incr
- 1, found
= 0;; i
+= incr
)
2213 if (i
== g
->hindex
- 1)
2216 if (i
>= pgn_history_total (g
->hp
))
2219 i
= pgn_history_total (g
->hp
) - 1;
2222 ret
= regexec (&r
, g
->hp
[i
]->move
, 0, 0, 0);
2226 if (count
== ++found
)
2233 if (ret
!= REG_NOMATCH
)
2235 regerror (ret
, &r
, errbuf
, sizeof (errbuf
));
2236 cmessage (_("Error Matching Regular Expression"), ANY_KEY_STR
,
2247 toggle_delete_flag (int n
)
2250 struct userdata_s
*d
= game
[n
]->data
;
2252 TOGGLE_FLAG (d
->flags
, CF_DELETE
);
2255 for (i
= x
= 0; i
< gtotal
; i
++)
2259 if (TEST_FLAG (d
->flags
, CF_DELETE
))
2265 cmessage (NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
2267 CLEAR_FLAG (d
->flags
, CF_DELETE
);
2275 find_game_exp (char *str
, int which
, int count
)
2277 char *nstr
= NULL
, *exp
= NULL
;
2281 char buf
[255] = { 0 }, *tmp
;
2284 int incr
= (which
== 0) ? -(1) : 1;
2286 strncpy (buf
, str
, sizeof (buf
) - 1);
2289 if (strstr (tmp
, ":") != NULL
)
2291 nstr
= strsep (&tmp
, ":");
2293 if ((ret
= regcomp (&nexp
, nstr
,
2294 REG_ICASE
| REG_EXTENDED
| REG_NOSUB
)) != 0)
2296 regerror (ret
, &nexp
, errbuf
, sizeof (errbuf
));
2297 cmessage (_("Error Compiling Regular Expression"), ANY_KEY_STR
,
2306 while (exp
&& *exp
&& isspace (*exp
))
2312 if ((ret
= regcomp (&vexp
, exp
, REG_EXTENDED
| REG_NOSUB
)) != 0)
2314 regerror (ret
, &vexp
, errbuf
, sizeof (errbuf
));
2315 cmessage (_("Error Compiling Regular Expression"), ANY_KEY_STR
, "%s",
2323 for (g
= gindex
+ incr
, found
= 0;; g
+= incr
)
2335 for (t
= 0; game
[g
]->tag
[t
]; t
++)
2339 if (regexec (&nexp
, game
[g
]->tag
[t
]->name
, 0, 0, 0) == 0)
2341 if (regexec (&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0)
2343 if (count
== ++found
)
2353 if (regexec (&vexp
, game
[g
]->tag
[t
]->value
, 0, 0, 0) == 0)
2355 if (count
== ++found
)
2378 * Updates the notification line in the status window then refreshes the
2382 update_status_notify (GAME g
, const char *fmt
, ...)
2385 #ifdef HAVE_VASPRINTF
2391 free (status
.notify
);
2392 status
.notify
= NULL
;
2398 #ifdef HAVE_VASPRINTF
2399 vasprintf (&line
, fmt
, ap
);
2401 vsnprintf (line
, sizeof (line
), fmt
, ap
);
2405 status
.notify
= str_to_wchar (line
);
2407 #ifdef HAVE_VASPRINTF
2413 rav_next_prev (GAME g
, BOARD b
, int n
)
2418 if ((!g
->ravlevel
&& g
->hindex
&& g
->hp
[g
->hindex
- 1]->rav
== NULL
) ||
2419 (!g
->ravlevel
&& !g
->hindex
&& g
->hp
[g
->hindex
]->rav
== NULL
) ||
2420 (g
->ravlevel
&& g
->hp
[g
->hindex
]->rav
== NULL
))
2423 g
->rav
= Realloc (g
->rav
, (g
->ravlevel
+ 1) * sizeof (RAV
));
2424 g
->rav
[g
->ravlevel
].hp
= g
->hp
;
2425 g
->rav
[g
->ravlevel
].flags
= g
->flags
;
2426 g
->rav
[g
->ravlevel
].fen
= pgn_game_to_fen (g
, b
);
2427 g
->rav
[g
->ravlevel
].hindex
= g
->hindex
;
2429 (!g
->ravlevel
) ? (g
->hindex
) ? g
->hp
[g
->hindex
-
2432 g
->hp
[g
->hindex
]->rav
;
2435 pgn_board_update (g
, b
, g
->hindex
+ 1);
2439 if (g
->ravlevel
- 1 < 0)
2444 pgn_board_init_fen (g
, b
, g
->rav
[g
->ravlevel
].fen
);
2445 free (g
->rav
[g
->ravlevel
].fen
);
2446 g
->hp
= g
->rav
[g
->ravlevel
].hp
;
2447 g
->flags
= g
->rav
[g
->ravlevel
].flags
;
2448 g
->hindex
= g
->rav
[g
->ravlevel
].hindex
;
2453 draw_window_decor ()
2455 move_panel (boardp
, 0, (config
.boardleft
) ? 0 : COLS
- BOARD_WIDTH
);
2456 move_panel (historyp
, LINES
- HISTORY_HEIGHT
,
2457 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
: 0 :
2458 (MEGA_BOARD
) ? 0 : COLS
- HISTORY_WIDTH
);
2459 move_panel (statusp
, 0, (config
.boardleft
) ? BOARD_WIDTH
: 0);
2460 move_panel (tagp
, STATUS_HEIGHT
+ 1,
2461 (config
.boardleft
) ? (MEGA_BOARD
) ? BOARD_WIDTH
:
2464 wbkgd (boardw
, CP_BOARD_WINDOW
);
2465 wbkgd (statusw
, CP_STATUS_WINDOW
);
2466 window_draw_title (statusw
, _("Game Status"), STATUS_WIDTH
,
2467 CP_STATUS_TITLE
, CP_STATUS_BORDER
);
2468 wbkgd (tagw
, CP_TAG_WINDOW
);
2469 window_draw_title (tagw
, _("Roster Tags"), TAG_WIDTH
, CP_TAG_TITLE
,
2471 wbkgd (historyw
, CP_HISTORY_WINDOW
);
2472 window_draw_title (historyw
, _("Move History"), HISTORY_WIDTH
,
2473 CP_HISTORY_TITLE
, CP_HISTORY_BORDER
);
2477 history_menu_resize (WIN
*w
)
2479 struct menu_input_s
*m
;
2484 w
->rows
= MEGA_BOARD
? LINES
- HISTORY_HEIGHT_MB
: LINES
;
2485 w
->cols
= TAG_WIDTH
;
2487 w
->posx
= config
.boardleft
? BOARD_WIDTH
: 0;
2489 m
->ystatic
= w
->posy
;
2490 m
->xstatic
= w
->posx
;
2497 if (LINES
< 23 || COLS
< 74)
2500 resizeterm (LINES
, COLS
);
2502 wresize (boardw
, BOARD_HEIGHT
, BOARD_WIDTH
);
2503 wresize (historyw
, HISTORY_HEIGHT
, HISTORY_WIDTH
);
2504 wresize (statusw
, STATUS_HEIGHT
, STATUS_WIDTH
);
2505 wresize (tagw
, TAG_HEIGHT
, TAG_WIDTH
);
2506 //resize_history_menu ();
2515 draw_window_decor ();
2517 keypad (boardw
, TRUE
);
2526 do_window_resize ();
2532 memset (&clock_timer
, 0, sizeof (struct itimerval
));
2533 setitimer (ITIMER_REAL
, &clock_timer
, NULL
);
2537 start_clock (GAME g
)
2539 struct userdata_s
*d
= g
->data
;
2541 if (clock_timer
.it_interval
.tv_usec
)
2544 memset (&d
->elapsed
, 0, sizeof (struct timeval
));
2545 clock_timer
.it_value
.tv_sec
= 0;
2546 clock_timer
.it_value
.tv_usec
= 100000;
2547 clock_timer
.it_interval
.tv_sec
= 0;
2548 clock_timer
.it_interval
.tv_usec
= 100000;
2549 setitimer (ITIMER_REAL
, &clock_timer
, NULL
);
2556 struct userdata_s
*d
;
2557 struct itimerval it
;
2560 getitimer (ITIMER_REAL
, &it
);
2562 for (i
= 0; i
< gtotal
; i
++)
2566 if (d
&& d
->mode
== MODE_PLAY
)
2568 if (d
->paused
== 1 || TEST_FLAG (d
->flags
, CF_NEW
))
2570 else if (d
->paused
== -1)
2572 if (game
[i
]->side
== game
[i
]->turn
)
2579 update_clock (game
[i
], it
);
2588 update_status_window (gp
);
2594 #define SKIP_SPACE(str) { while (isspace(*str)) str++; }
2597 parse_clock_time (char **str
)
2613 while (isdigit (*p
))
2656 parse_clock_input (struct clock_s
*clk
, char *str
, int *incr
)
2679 memset (clk
, 0, sizeof (struct clock_s
));
2683 if (strncasecmp (p
, "SD", 2) == 0)
2690 n
= parse_clock_time (&p
);
2705 /* Sudden death without a previous time control. */
2711 n
= parse_clock_time (&p
);
2718 message (ERROR_STR
, ANY_KEY_STR
, "%s (%i)",
2719 _("Maximum number of time controls reached"), MAX_TC
);
2724 clk
->tc
[tc
++][1] = n
;
2739 clk
->tc
[clk
->tcn
][1] =
2740 (n
<= clk
->elapsed
.tv_sec
) ? clk
->elapsed
.tv_sec
+ n
: n
;
2750 n
= parse_clock_time (&p
);
2771 parse_which_clock (struct clock_s
*clk
, char *str
)
2776 memcpy (&tmp
, clk
, sizeof (struct clock_s
));
2778 if (parse_clock_input (&tmp
, str
, &incr
))
2780 cmessage (ERROR_STR
, ANY_KEY_STR
, _("Invalid clock specification"));
2784 memcpy (clk
, &tmp
, sizeof (struct clock_s
));
2785 clk
->tc
[clk
->tcn
][1] += incr
;
2790 do_clock_input_finalize (WIN
* win
)
2792 struct userdata_s
*d
= gp
->data
;
2793 struct input_data_s
*in
= win
->data
;
2804 if (tolower (*p
) == 'w')
2808 if (parse_which_clock (&d
->wclock
, p
))
2811 else if (tolower (*p
) == 'b')
2815 if (parse_which_clock (&d
->bclock
, p
))
2820 if (parse_which_clock (&d
->wclock
, p
))
2823 if (parse_which_clock (&d
->bclock
, p
))
2827 if (!d
->wclock
.tc
[0][1] && !d
->bclock
.tc
[0][1])
2828 CLEAR_FLAG (d
->flags
, CF_CLOCK
);
2830 SET_FLAG (d
->flags
, CF_CLOCK
);
2838 do_engine_command_finalize (WIN
* win
)
2840 struct userdata_s
*d
= gp
->data
;
2841 struct input_data_s
*in
= win
->data
;
2853 x
= d
->engine
->status
;
2854 send_to_engine (gp
, -1, "%s\n", in
->str
);
2855 d
->engine
->status
= x
;
2865 config
.details
= (config
.details
) ? 0 : 1;
2869 do_toggle_strict_castling ()
2873 pgn_config_get (PGN_STRICT_CASTLING
, &n
);
2876 pgn_config_set (PGN_STRICT_CASTLING
, 1);
2878 pgn_config_set (PGN_STRICT_CASTLING
, 0);
2882 do_play_set_clock ()
2884 struct input_data_s
*in
;
2886 in
= Calloc (1, sizeof (struct input_data_s
));
2887 in
->efunc
= do_clock_input_finalize
;
2888 construct_input (_("Set Clock"), NULL
, 1, 1,
2890 ("Format: [W | B] [+]T[+I] | ++I | M/T [M/T [...] [SD/T]] [+I]\n"
2891 "T = time (hms), I = increment, M = moves per, SD = sudden death\ne.g., 30m or 4m+12s or 35/90m SD/30m"),
2892 NULL
, NULL
, 0, in
, INPUT_HIST_CLOCK
, NULL
, -1);
2896 do_play_toggle_human ()
2898 struct userdata_s
*d
= gp
->data
;
2900 TOGGLE_FLAG (d
->flags
, CF_HUMAN
);
2902 if (!TEST_FLAG (d
->flags
, CF_HUMAN
) && pgn_history_total (gp
->hp
))
2904 if (init_chess_engine (gp
))
2908 CLEAR_FLAG (d
->flags
, CF_ENGINE_LOOP
);
2911 d
->engine
->status
= ENGINE_READY
;
2915 do_play_toggle_engine ()
2917 struct userdata_s
*d
= gp
->data
;
2919 TOGGLE_FLAG (d
->flags
, CF_ENGINE_LOOP
);
2920 CLEAR_FLAG (d
->flags
, CF_HUMAN
);
2922 if (d
->engine
&& TEST_FLAG (d
->flags
, CF_ENGINE_LOOP
))
2924 char *fen
= pgn_game_to_fen (gp
, d
->b
);
2926 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
2927 add_engine_command (gp
, ENGINE_READY
, "setboard %s\n", fen
);
2933 * This will send a command to the engine skipping the command queue.
2936 do_play_send_command ()
2938 struct userdata_s
*d
= gp
->data
;
2939 struct input_data_s
*in
;
2941 if (!d
->engine
|| d
->engine
->status
== ENGINE_OFFLINE
)
2943 if (init_chess_engine (gp
))
2947 in
= Calloc (1, sizeof (struct input_data_s
));
2948 in
->efunc
= do_engine_command_finalize
;
2949 construct_input (_("Engine Command"), NULL
, 1, 1, NULL
, NULL
, NULL
, 0, in
,
2950 INPUT_HIST_ENGINE
, NULL
, -1);
2954 void do_play_switch_turn()
2956 struct userdata_s *d = gp->data;
2958 pgn_switch_side(gp);
2959 pgn_switch_turn(gp);
2961 if (!TEST_FLAG(d->flags, CF_HUMAN))
2962 add_engine_command(gp, -1,
2963 (gp->side == WHITE) ? "white\n" : "black\n");
2965 update_status_window(gp);
2969 do_play_toggle_eh_mode ()
2971 struct userdata_s
*d
= gp
->data
;
2973 if (!TEST_FLAG (d
->flags
, CF_HUMAN
))
2977 pgn_switch_side (gp
, TRUE
);
2978 d
->play_mode
= (d
->play_mode
) ? PLAY_HE
: PLAY_EH
;
2979 if (gp
->side
== BLACK
)
2980 update_status_notify (gp
, _("Press 'g' to start the game"));
2982 d
->rotate
= !d
->rotate
;
2985 message (NULL
, ANY_KEY_STR
,
2986 _("You may only switch sides at the start of the \n"
2987 "game. Press ^K or ^N to begin a new game."));
2994 struct userdata_s
*d
= gp
->data
;
2996 if (!pgn_history_total (gp
->hp
))
3001 if (gp
->hindex
- keycount
< 0)
3006 gp
->hindex
-= (keycount
* 2) - 1;
3008 gp
->hindex
-= keycount
* 2;
3013 if (gp
->hindex
- 2 < 0)
3024 pgn_history_free (gp
->hp
, gp
->hindex
);
3025 gp
->hindex
= pgn_history_total (gp
->hp
);
3026 pgn_board_update (gp
, d
->b
, gp
->hindex
);
3028 if (d
->engine
&& d
->engine
->status
== ENGINE_READY
)
3030 char *fen
= pgn_game_to_fen (gp
, d
->b
);
3032 add_engine_command (gp
, ENGINE_READY
, "setboard %s\n", fen
);
3034 d
->engine
->status
= ENGINE_READY
;
3037 update_history_window (gp
);
3041 pgn_switch_side (gp
, FALSE
);
3049 do_play_toggle_pause ()
3051 struct userdata_s
*d
= gp
->data
;
3053 if (!TEST_FLAG (d
->flags
, CF_HUMAN
) && gp
->turn
!= gp
->side
)
3059 d
->paused
= (d
->paused
) ? 0 : 1;
3065 struct userdata_s
*d
= gp
->data
;
3067 if (TEST_FLAG (d
->flags
, CF_HUMAN
))
3070 if (fm_loaded_file
&& gp
->side
!= gp
->turn
)
3072 pgn_switch_side (gp
, FALSE
);
3073 add_engine_command (gp
, ENGINE_THINKING
, "black\n");
3076 add_engine_command (gp
, ENGINE_THINKING
, "go\n");
3078 // Completa la función para que permita seguir jugando al usarla.
3079 // Complete the function to allow continue playing when using.
3080 if (gp
->side
== gp
->turn
)
3081 pgn_switch_side (gp
, FALSE
);
3087 do_play_config_command ()
3093 for (x
= 0; config
.keys
[x
]; x
++)
3095 if (config
.keys
[x
]->c
== input_c
)
3097 switch (config
.keys
[x
]->type
)
3100 add_engine_command (gp
, -1, "%ls\n", config
.keys
[x
]->str
);
3106 add_engine_command (gp
, -1,
3107 "%ls %i\n", config
.keys
[x
]->str
,
3115 for (w
= 0; w
< keycount
; w
++)
3116 add_engine_command (gp
, -1, "%ls\n", config
.keys
[x
]->str
);
3124 update_status_notify (gp
, NULL
);
3128 do_play_cancel_selected ()
3130 struct userdata_s
*d
= gp
->data
;
3132 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3134 pgn_reset_valid_moves (d
->b
);
3135 update_status_notify (gp
, NULL
);
3141 struct userdata_s
*d
= gp
->data
;
3143 pushkey
= keycount
= 0;
3144 update_status_notify (gp
, NULL
);
3146 if (!TEST_FLAG (d
->flags
, CF_HUMAN
) &&
3147 (!d
->engine
|| d
->engine
->status
== ENGINE_THINKING
))
3153 d
->sp
.row
= d
->c_row
;
3154 d
->sp
.col
= d
->c_col
;
3158 rotate_position (&d
->sp
.row
, &d
->sp
.col
);
3159 rotate_position (&d
->sp
.srow
, &d
->sp
.scol
);
3162 move_to_engine (gp
);
3164 // Completa la función para que permita seguir jugando cuando se carga un
3165 // archivo pgn (con juego no terminado) que inicie con turno del lado
3167 // Complete the function to allow continue playing when loading a file
3168 // pgn (with unfinished game) you start to turn black side.
3169 if (gp
->side
!= gp
->turn
)
3170 pgn_switch_side (gp
, FALSE
);
3172 if (d
->rotate
&& d
->sp
.icon
)
3173 rotate_position (&d
->sp
.srow
, &d
->sp
.scol
);
3176 fm_loaded_file
= FALSE
;
3183 struct userdata_s
*d
= gp
->data
;
3185 if (!TEST_FLAG (d
->flags
, CF_HUMAN
) && (!d
->engine
||
3186 d
->engine
->status
==
3189 if (init_chess_engine (gp
))
3193 if (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
)
3197 do_play_cancel_selected ();
3200 rotate_position (&d
->c_row
, &d
->c_col
);
3202 d
->sp
.icon
= d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].icon
;
3204 if (pgn_piece_to_int (d
->sp
.icon
) == OPEN_SQUARE
)
3210 if (((islower (d
->sp
.icon
) && gp
->turn
!= BLACK
)
3211 || (isupper (d
->sp
.icon
) && gp
->turn
!= WHITE
)))
3214 char *str
= Malloc (512);
3216 for (k
= play_keys
; *k
; k
++)
3218 if ((*k
)->f
== do_play_toggle_eh_mode
)
3224 ("It is not your turn to move. You may switch playing sides by pressing \"%lc\"."),
3225 *k
? (*k
)->c
: '?');
3226 message (NULL
, ANY_KEY_STR
, "%s", str
);
3231 if (pgn_history_total (gp
->hp
))
3233 message (NULL
, ANY_KEY_STR
, "%s",
3234 _("It is not your turn to move. You can switch sides "));
3240 if (pgn_tag_find (gp
->tag
, "FEN") != E_PGN_ERR
)
3243 add_engine_command (gp
, ENGINE_READY
, "black\n");
3244 pgn_switch_turn (gp
);
3246 if (gp
->side
!= BLACK
)
3247 pgn_switch_side (gp
);
3252 d
->sp
.srow
= d
->c_row
;
3253 d
->sp
.scol
= d
->c_col
;
3255 if (config
.validmoves
)
3256 pgn_find_valid_moves (gp
, d
->b
, d
->sp
.scol
, d
->sp
.srow
);
3260 rotate_position (&d
->c_row
, &d
->c_col
);
3261 rotate_position (&d
->sp
.srow
, &d
->sp
.scol
);
3264 CLEAR_FLAG (d
->flags
, CF_NEW
);
3269 build_help_line_once (wchar_t *buf
, wchar_t **pp
, struct key_s
*k
, int t
,
3272 wchar_t *p
= *pp
, *wc
;
3276 n
= wcslen (k
->key
);
3287 wcsncat (buf
, k
->key
, t
- 1);
3288 p
= buf
+ wcslen (buf
);
3299 wcsncat (buf
, k
->d
, t
- 1);
3303 wc
= str_to_wchar ("*");
3304 wcsncat (buf
, wc
, t
- 1);
3308 wc
= str_to_wchar ("\n");
3311 *pp
= buf
+ wcslen (buf
);
3315 calc_help_len (struct key_s
*k
, int *t
, int *nlen
, int *len
)
3322 if (wcslen (k
->key
) > *nlen
)
3324 *nlen
= wcslen (k
->key
);
3335 if (wcslen (k
->d
) > *len
)
3336 *len
= wcslen (k
->d
);
3344 build_help (struct key_s
**keys
)
3346 int i
, m
= 0, nlen
= 1, len
, t
;
3347 wchar_t *buf
= NULL
;
3349 wchar_t *more_help
= str_to_wchar (_("more help"));
3350 const wchar_t *more_help_key
= key_lookup (global_keys
, do_global_help
);
3352 int mode
= MODE_ANY
;
3360 for (i
= t
= len
= 0; keys
[i
]; i
++)
3361 calc_help_len (keys
[i
], &t
, &nlen
, &len
);
3365 if (keys
== play_keys
)
3367 else if (keys
== history_keys
)
3368 mode
= MODE_HISTORY
;
3369 else if (keys
== edit_keys
)
3374 for (m
= 0; macros
[m
]; m
++)
3376 if (macros
[m
]->mode
== mode
)
3378 k
.d
= macros
[m
]->desc
;
3380 k
.key
= str_to_wchar (fancy_key_name (macros
[m
]->c
));
3381 calc_help_len (&k
, &t
, &nlen
, &len
);
3389 k
.key
= (wchar_t *)more_help_key
;
3390 calc_help_len (&k
, &t
, &nlen
, &len
);
3392 t
+= 4 + i
+ 1 + m
+ 1; // +1 for more_help
3393 buf
= Malloc ((t
+ 1) * sizeof (wchar_t));
3396 for (i
= 0; keys
[i
]; i
++)
3401 build_help_line_once (buf
, &p
, keys
[i
], t
, nlen
);
3406 for (m
= 0; macros
[m
]; m
++)
3408 if (macros
[m
]->mode
== mode
)
3410 k
.d
= macros
[m
]->desc
;
3412 k
.key
= str_to_wchar (fancy_key_name (macros
[m
]->c
));
3413 build_help_line_once (buf
, &p
, &k
, t
, nlen
);
3421 k
.key
= (wchar_t *)more_help_key
;
3422 build_help_line_once (buf
, &p
, &k
, t
, nlen
);
3430 struct userdata_s
*d
= gp
->data
;
3433 if (!d
->global_help
)
3452 buf
= build_help (global_keys
);
3453 construct_message (_("Global Game Keys (* = can take a repeat count)"),
3454 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
, buf
, do_more_help
,
3455 0, 1, NULL
, "%ls", buf
);
3459 do_main_help (WIN
* win
)
3461 struct userdata_s
*d
= gp
->data
;
3484 do_more_help (WIN
* win
)
3488 for (i
= 0; global_keys
[i
]; i
++)
3490 if (global_keys
[i
]->f
== do_global_help
3491 && global_keys
[i
]->c
== win
->c
)
3492 construct_message (_("Command Key Index"),
3493 _("p/h/e/g or any other key to quit"), 0, 0,
3494 NULL
, NULL
, NULL
, do_main_help
, 0, 0, NULL
, "%s",
3495 _(" p - play mode keys\n"
3496 " h - history mode keys\n"
3497 " e - board edit mode keys\n"
3498 " g - global game keys"));
3505 wchar_t *buf
= build_help (play_keys
);
3507 construct_message (_("Play Mode Keys (* = can take a repeat count)"),
3508 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
,
3509 buf
, do_more_help
, 0, 1, NULL
, "%ls", buf
);
3513 do_play_history_mode ()
3515 struct userdata_s
*d
= gp
->data
;
3517 if (!pgn_history_total (gp
->hp
) ||
3518 (d
->engine
&& d
->engine
->status
== ENGINE_THINKING
))
3521 d
->mode
= MODE_HISTORY
;
3522 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
3526 do_play_edit_mode ()
3528 struct userdata_s
*d
= gp
->data
;
3530 if (pgn_history_total (gp
->hp
))
3533 pgn_board_init_fen (gp
, d
->b
, NULL
);
3535 d
->mode
= MODE_EDIT
;
3539 do_edit_insert_finalize (WIN
* win
)
3541 struct userdata_s
*d
= win
->data
;
3543 if (pgn_piece_to_int (win
->c
) == -1)
3546 d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].icon
= win
->c
;
3552 struct userdata_s
*d
= gp
->data
;
3557 d
->sp
.icon
= d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].icon
;
3559 if (pgn_piece_to_int (d
->sp
.icon
) == OPEN_SQUARE
)
3565 d
->sp
.srow
= d
->c_row
;
3566 d
->sp
.scol
= d
->c_col
;
3573 struct userdata_s
*d
= gp
->data
;
3575 pushkey
= keycount
= 0;
3576 update_status_notify (gp
, NULL
);
3581 d
->sp
.row
= d
->c_row
;
3582 d
->sp
.col
= d
->c_col
;
3583 p
= d
->b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
;
3584 d
->b
[RANKTOBOARD (d
->sp
.row
)][FILETOBOARD (d
->sp
.col
)].icon
= p
;
3585 d
->b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
3586 pgn_int_to_piece (gp
->turn
, OPEN_SQUARE
);
3587 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3593 struct userdata_s
*d
= gp
->data
;
3596 d
->b
[RANKTOBOARD (d
->sp
.srow
)][FILETOBOARD (d
->sp
.scol
)].icon
=
3597 pgn_int_to_piece (gp
->turn
, OPEN_SQUARE
);
3599 d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].icon
=
3600 pgn_int_to_piece (gp
->turn
, OPEN_SQUARE
);
3602 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3606 do_edit_cancel_selected ()
3608 struct userdata_s
*d
= gp
->data
;
3610 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
3612 update_status_notify (gp
, NULL
);
3616 do_edit_switch_turn ()
3618 pgn_switch_turn (gp
);
3622 do_edit_toggle_castle ()
3624 struct userdata_s
*d
= gp
->data
;
3626 castling_state (gp
, d
->b
, RANKTOBOARD (d
->c_row
),
3627 FILETOBOARD (d
->c_col
),
3628 d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].icon
,
3635 struct userdata_s
*d
= gp
->data
;
3637 construct_message (_("Insert Piece"),
3638 _("P=pawn, R=rook, N=knight, B=bishop, Q=queen, K=king"),
3639 0, 0, NULL
, NULL
, d
->b
, do_edit_insert_finalize
, 0, 0,
3641 _("Type the piece letter to insert. Lowercase for a black piece, uppercase for a white piece."));
3645 do_edit_enpassant ()
3647 struct userdata_s
*d
= gp
->data
;
3649 if (d
->c_row
== 6 || d
->c_row
== 3)
3651 int n
= d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].enpassant
;
3653 pgn_reset_enpassant (d
->b
);
3655 d
->b
[RANKTOBOARD (d
->c_row
)][FILETOBOARD (d
->c_col
)].enpassant
= 1;
3662 wchar_t *buf
= build_help (edit_keys
);
3664 construct_message (_("Edit Mode Keys (* = can take a repeat count)"),
3665 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
, buf
, do_more_help
,
3666 0, 1, NULL
, "%ls", buf
);
3672 struct userdata_s
*d
= gp
->data
;
3673 char *fen
= pgn_game_to_fen (gp
, d
->b
);
3676 pgn_tag_add (&gp
->tag
, (char *) "FEN", fen
);
3678 pgn_tag_add (&gp
->tag
, (char *) "SetUp", (char *) "1");
3679 pgn_tag_sort (gp
->tag
);
3680 pgn_board_update (gp
, d
->b
, gp
->hindex
);
3681 d
->mode
= MODE_PLAY
;
3685 really_do_annotate_finalize (struct input_data_s
*in
, struct userdata_s
*d
)
3687 HISTORY
*h
= in
->data
;
3700 len
= strlen (in
->str
);
3701 h
->comment
= Realloc (h
->comment
, len
+ 1);
3702 strncpy (h
->comment
, in
->str
, len
);
3703 h
->comment
[len
] = 0;
3708 SET_FLAG (d
->flags
, CF_MODIFIED
);
3712 do_annotate_finalize (WIN
* win
)
3714 struct userdata_s
*d
= gp
->data
;
3715 struct input_data_s
*in
= win
->data
;
3717 really_do_annotate_finalize (in
, d
);
3721 do_find_move_exp_finalize (int init
, int which
)
3724 struct userdata_s
*d
= gp
->data
;
3725 static int firstrun
;
3730 if (init
|| !firstrun
)
3735 if ((ret
= regcomp (&r
, moveexp
, REG_EXTENDED
| REG_NOSUB
)) != 0)
3737 regerror (ret
, &r
, errbuf
, sizeof (errbuf
));
3738 cmessage (_("Error Compiling Regular Expression"), ANY_KEY_STR
,
3746 if ((n
= find_move_exp (gp
, r
,
3747 (which
== -1) ? 0 : 1,
3748 (keycount
) ? keycount
: 1)) == -1)
3752 pgn_board_update (gp
, d
->b
, gp
->hindex
);
3756 do_find_move_exp (WIN
* win
)
3758 struct input_data_s
*in
= win
->data
;
3764 strncpy (moveexp
, in
->str
, sizeof (moveexp
) - 1);
3765 moveexp
[sizeof (moveexp
) - 1] = 0;
3766 do_find_move_exp_finalize (1, which
);
3775 do_move_jump_finalize (int n
)
3777 struct userdata_s
*d
= gp
->data
;
3779 if (n
< 0 || n
> (pgn_history_total (gp
->hp
) / 2))
3783 update_status_notify (gp
, NULL
);
3784 gp
->hindex
= (n
) ? n
* 2 - 1 : n
* 2;
3785 pgn_board_update (gp
, d
->b
, gp
->hindex
);
3789 do_move_jump (WIN
* win
)
3791 struct input_data_s
*in
= win
->data
;
3793 if (!in
->str
|| !isinteger (in
->str
))
3802 do_move_jump_finalize (atoi (in
->str
));
3807 struct history_menu_s
3817 free_history_menu_data (struct history_menu_s
**h
)
3824 for (i
= 0; h
[i
]; i
++)
3834 get_history_data (HISTORY
** hp
, struct history_menu_s
***menu
, int m
,
3838 int t
= pgn_history_total (hp
);
3839 char buf
[MAX_SAN_MOVE_LEN
+ 4];
3841 struct history_menu_s
**hmenu
= *menu
;
3844 for (n
= 0; hmenu
[n
]; n
++);
3848 for (i
= 0; i
< t
; i
++)
3850 hmenu
= Realloc (hmenu
, (n
+ 2) * sizeof (struct history_menu_s
*));
3851 hmenu
[n
] = Malloc (sizeof (struct history_menu_s
));
3852 snprintf (buf
, sizeof (buf
), "%c%s%s", (turn
== WHITE
) ? 'W' : 'B',
3853 hp
[i
]->move
, (hp
[i
]->comment
|| hp
[i
]->nag
[0]) ? " !" : "");
3854 hmenu
[n
]->line
= strdup (buf
);
3855 hmenu
[n
]->hindex
= i
;
3856 hmenu
[n
]->indent
= 0;
3857 hmenu
[n
]->ravlevel
= depth
;
3858 hmenu
[n
]->move
= (n
&& depth
> hmenu
[n
- 1]->ravlevel
) ? m
++ : m
;
3866 get_history_data (hp
[i
]->rav
, &hmenu
, m
, turn
);
3867 for (n
= 0; hmenu
[n
]; n
++);
3875 turn
= (turn
== WHITE
) ? BLACK
: WHITE
;
3882 history_draw_update (struct menu_input_s
*m
)
3885 struct userdata_s
*d
= g
->data
;
3887 g
->hindex
= m
->selected
+ 1;
3888 update_cursor (g
, m
->selected
);
3889 pgn_board_update (g
, d
->b
, m
->selected
+ 1);
3892 struct menu_item_s
**
3893 get_history_items (WIN
* win
)
3895 struct menu_input_s
*m
= win
->data
;
3897 struct userdata_s
*d
= g
->data
;
3898 struct history_menu_s
**hm
= d
->data
;
3899 struct menu_item_s
**items
= m
->items
;
3904 get_history_data (g
->history
, &hm
, 0,
3905 TEST_FLAG (g
->flags
, GF_BLACK_OPENING
));
3906 m
->selected
= g
->hindex
- 1;
3908 if (m
->selected
< 0)
3911 m
->draw_exit_func
= history_draw_update
;
3918 for (i
= 0; items
[i
]; i
++)
3925 for (i
= 0; hm
[i
]; i
++)
3927 items
= Realloc (items
, (i
+ 2) * sizeof (struct menu_item_s
*));
3928 items
[i
] = Malloc (sizeof (struct menu_item_s
));
3929 items
[i
]->name
= hm
[i
]->line
;
3930 items
[i
]->value
= NULL
;
3931 items
[i
]->selected
= 0;
3943 history_menu_quit (struct menu_input_s
*m
)
3949 history_menu_exit (WIN
* win
)
3952 struct userdata_s
*d
= g
->data
;
3953 struct history_menu_s
**hm
= d
->data
;
3959 for (i
= 0; hm
[i
]; i
++)
3971 history_menu_next (struct menu_input_s
*m
)
3974 struct userdata_s
*d
= g
->data
;
3975 struct history_menu_s
**hm
= d
->data
;
3978 for (t
= 0; hm
[t
]; t
++);
3980 if (m
->selected
+ 1 == t
)
3983 n
= hm
[m
->selected
+ 1]->hindex
;
3991 history_menu_prev (struct menu_input_s
*m
)
3994 struct userdata_s
*d
= g
->data
;
3995 struct history_menu_s
**hm
= d
->data
;
3998 for (t
= 0; hm
[t
]; t
++);
4000 if (m
->selected
- 1 < 0)
4003 n
= hm
[m
->selected
- 1]->hindex
;
4010 history_menu_help (struct menu_input_s
*m
)
4012 message (_("History Menu Help"), ANY_KEY_STR
, "%s",
4013 _(" UP/DOWN - previous/next menu item\n"
4014 " HOME/END - first/last menu item\n"
4015 " PGDN/PGUP - next/previous page\n"
4016 " a-zA-Z0-9 - jump to item\n"
4017 " CTRL-a - annotate the selected move\n"
4018 " ENTER - view annotation\n"
4019 " CTRL-d - toggle board details\n"
4020 " ESCAPE/M - return to move history"));
4024 do_annotate_move (HISTORY
* hp
)
4027 struct input_data_s
*in
;
4029 snprintf (buf
, sizeof (buf
), "%s \"%s\"", _("Editing Annotation for"),
4031 in
= Calloc (1, sizeof (struct input_data_s
));
4033 in
->efunc
= do_annotate_finalize
;
4034 construct_input (buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
4035 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
,
4036 CTRL_KEY ('T'), in
, -1, NULL
, -1);
4040 history_menu_view_annotation (struct menu_input_s
*m
)
4045 view_annotation (g
->history
[m
->selected
]);
4049 history_menu_annotate_finalize (WIN
* win
)
4051 struct input_data_s
*in
= win
->data
;
4052 GAME g
= in
->moredata
;
4053 struct userdata_s
*d
= g
->data
;
4054 struct history_menu_s
**hm
= d
->data
;
4056 really_do_annotate_finalize (in
, d
);
4057 free_history_menu_data (hm
);
4059 get_history_data (g
->history
, &hm
, 0,
4060 TEST_FLAG (g
->flags
, GF_BLACK_OPENING
));
4062 pushkey
= REFRESH_MENU
;
4066 history_menu_annotate (struct menu_input_s
*m
)
4070 struct input_data_s
*in
;
4071 HISTORY
*hp
= g
->history
[m
->selected
]; // FIXME RAV
4073 snprintf (buf
, sizeof (buf
), "%s \"%s\"", _("Editing Annotation for"),
4075 in
= Calloc (1, sizeof (struct input_data_s
));
4077 in
->moredata
= m
->data
;
4078 in
->efunc
= history_menu_annotate_finalize
;
4079 construct_input (buf
, hp
->comment
, MAX_PGN_LINE_LEN
/ INPUT_WIDTH
, 0,
4080 _("Type CTRL-t to edit NAG"), edit_nag
, NULL
,
4081 CTRL_KEY ('T'), in
, -1, NULL
, -1);
4085 history_menu_details (struct menu_input_s
*m
)
4087 do_board_details ();
4092 history_menu_print (WIN
* win
)
4094 struct menu_input_s
*m
= win
->data
;
4096 struct userdata_s
*d
= g
->data
;
4097 struct history_menu_s
**hm
= d
->data
;
4098 struct history_menu_s
*h
= hm
[m
->top
];
4100 char *p
= m
->item
->name
;
4101 int line
= m
->print_line
- 2;
4103 * Solaris 5.9 doesn't have wattr_get() or any function that requires an
4110 for (total
= 0; hm
[total
]; total
++);
4111 wattr_get (win
->w
, &attrs
, &pair
, NULL
);
4112 wattroff (win
->w
, COLOR_PAIR (pair
));
4113 mvwaddch (win
->w
, m
->print_line
, 1,
4114 *p
== 'W' ? *p
| mix_cp (CP_BOARD_WHITE
, CP_HISTORY_WINDOW
,
4115 ATTRS (CP_BOARD_WHITE
),
4116 A_FG_B_BG
) : *p
| mix_cp (CP_BOARD_BLACK
,
4123 if (h
->hindex
== 0 && line
== 0)
4124 waddch (win
->w
, ACS_ULCORNER
| CP_HISTORY_MENU_LG
);
4125 else if ((!hm
[h
->hindex
+ (win
->rows
- 5) + 1] && line
== win
->rows
- 5) ||
4126 (m
->top
+ line
== total
- 1))
4127 waddch (win
->w
, ACS_LLCORNER
| CP_HISTORY_MENU_LG
);
4128 else if (hm
[m
->top
+ 1]->ravlevel
!= h
->ravlevel
|| !h
->ravlevel
)
4129 waddch (win
->w
, ACS_LTEE
| CP_HISTORY_MENU_LG
);
4131 waddch (win
->w
, ACS_VLINE
| CP_HISTORY_MENU_LG
);
4133 wattron (win
->w
, COLOR_PAIR (pair
) | attrs
);
4135 for (i
= 2; *p
; p
++, i
++)
4136 waddch (win
->w
, (*p
== '!') ? *p
| A_BOLD
: *p
);
4138 while (i
++ < win
->cols
- 2)
4139 waddch (win
->w
, ' ');
4143 history_menu (GAME g
)
4145 struct menu_key_s
**keys
= NULL
;
4147 add_menu_key (&keys
, KEY_ESCAPE
, history_menu_quit
);
4148 add_menu_key (&keys
, 'M', history_menu_quit
);
4149 add_menu_key (&keys
, KEY_UP
, history_menu_prev
);
4150 add_menu_key (&keys
, KEY_DOWN
, history_menu_next
);
4151 add_menu_key (&keys
, keycode_lookup (global_keys
, do_global_help
),
4153 add_menu_key (&keys
, CTRL_KEY ('a'), history_menu_annotate
);
4154 add_menu_key (&keys
, CTRL_KEY ('d'), history_menu_details
);
4155 add_menu_key (&keys
, '\n', history_menu_view_annotation
);
4156 construct_menu (MEGA_BOARD
? LINES
- HISTORY_HEIGHT_MB
: LINES
, TAG_WIDTH
,
4157 0, config
.boardleft
? BOARD_WIDTH
: 0,
4158 _("Move History Tree"), 1, get_history_items
, keys
, g
,
4159 history_menu_print
, history_menu_exit
, history_menu_resize
);
4169 do_history_half_move_toggle ()
4171 movestep
= (movestep
== 1) ? 2 : 1;
4172 update_history_window (gp
);
4176 do_history_rotate_board ()
4178 struct userdata_s
*d
= gp
->data
;
4179 d
->rotate
= !d
->rotate
;
4183 do_history_jump_next ()
4185 struct userdata_s
*d
= gp
->data
;
4187 pgn_history_next (gp
, d
->b
, (keycount
> 0) ?
4188 config
.jumpcount
* keycount
* movestep
:
4189 config
.jumpcount
* movestep
);
4193 do_history_jump_prev ()
4195 struct userdata_s
*d
= gp
->data
;
4197 pgn_history_prev (gp
, d
->b
, (keycount
) ?
4198 config
.jumpcount
* keycount
* movestep
:
4199 config
.jumpcount
* movestep
);
4205 struct userdata_s
*d
= gp
->data
;
4207 pgn_history_prev (gp
, d
->b
, (keycount
) ? keycount
* movestep
: movestep
);
4213 struct userdata_s
*d
= gp
->data
;
4215 pgn_history_next (gp
, d
->b
, (keycount
) ? keycount
* movestep
: movestep
);
4219 do_history_mode_finalize (struct userdata_s
*d
)
4222 d
->mode
= MODE_PLAY
;
4226 do_history_mode_confirm (WIN
* win
)
4228 struct userdata_s
*d
= gp
->data
;
4229 wchar_t str
[] = { win
->c
, 0 };
4231 if (!wcscmp (str
, resume_wchar
))
4233 pgn_history_free (gp
->hp
, gp
->hindex
);
4234 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
4239 if (pgn_history_rav_new (gp
, d
->b
, gp
->hindex
) != E_PGN_OK
)
4247 if (!TEST_FLAG (d
->flags
, CF_HUMAN
))
4249 char *fen
= pgn_game_to_fen (gp
, d
->b
);
4251 add_engine_command (gp
, ENGINE_READY
, "setboard %s\n", fen
);
4255 do_history_mode_finalize (d
);
4259 do_history_toggle ()
4261 struct userdata_s
*d
= gp
->data
;
4263 // FIXME Resuming from previous history could append to a RAV.
4264 if (gp
->hindex
!= pgn_history_total (gp
->hp
))
4267 construct_message (NULL
, _("What would you like to do?"), 0, 1,
4268 NULL
, NULL
, NULL
, do_history_mode_confirm
, 0, 0,
4270 ("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."),
4276 if (TEST_FLAG (gp
->flags
, GF_GAMEOVER
))
4280 if (gp
->side
!= gp
->turn
)
4282 d
->play_mode
= PLAY_EH
;
4286 d
->play_mode
= PLAY_HE
;
4290 do_history_mode_finalize (d
);
4294 do_history_annotate ()
4298 if (n
&& gp
->hp
[n
- 1]->move
)
4303 do_annotate_move (gp
->hp
[n
]);
4309 wchar_t *buf
= build_help (history_keys
);
4311 construct_message (_("History Mode Keys (* = can take a repeat count)"),
4312 ANY_KEY_SCROLL_STR
, 0, 0, NULL
, NULL
,
4313 buf
, do_more_help
, 0, 1, NULL
, "%ls", buf
);
4317 do_history_find (int which
)
4319 struct input_data_s
*in
;
4322 if (pgn_history_total (gp
->hp
) < 2)
4325 in
= Calloc (1, sizeof (struct input_data_s
));
4326 p
= Malloc (sizeof (int));
4329 in
->efunc
= do_find_move_exp
;
4331 if (!*moveexp
|| which
== 0)
4333 construct_input (_("Find Move Text Expression"), NULL
, 1, 0, NULL
, NULL
,
4334 NULL
, 0, in
, INPUT_HIST_MOVE_EXP
, NULL
, -1);
4340 do_find_move_exp_finalize (0, which
);
4344 do_history_find_new ()
4346 do_history_find (0);
4350 do_history_find_prev ()
4352 do_history_find (-1);
4356 do_history_find_next ()
4358 do_history_find (1);
4362 do_history_rav (int which
)
4364 struct userdata_s
*d
= gp
->data
;
4366 rav_next_prev (gp
, d
->b
, which
);
4370 do_history_rav_next ()
4376 do_history_rav_prev ()
4384 struct input_data_s
*in
;
4386 if (pgn_history_total (gp
->hp
) < 2)
4391 in
= Calloc (1, sizeof (struct input_data_s
));
4392 in
->efunc
= do_move_jump
;
4394 construct_input (_("Jump to Move Number"), NULL
, 1, 1, NULL
,
4395 NULL
, NULL
, 0, in
, -1, NULL
, 0);
4399 do_move_jump_finalize (keycount
);
4403 free_userdata_once (GAME g
)
4405 struct userdata_s
*d
= g
->data
;
4414 if (d
->engine
->enginebuf
)
4418 for (n
= 0; d
->engine
->enginebuf
[n
]; n
++)
4419 free (d
->engine
->enginebuf
[n
]);
4421 free (d
->engine
->enginebuf
);
4424 if (d
->engine
->queue
)
4428 for (q
= d
->engine
->queue
; *q
; q
++)
4431 free (d
->engine
->queue
);
4454 for (i
= 0; i
< gtotal
; i
++)
4456 free_userdata_once (game
[i
]);
4457 game
[i
]->data
= NULL
;
4462 update_loading_window (int n
)
4468 loadingw
= newwin (3, COLS
/ 2, CALCPOSY (3), CALCPOSX (COLS
/ 2));
4469 loadingp
= new_panel (loadingw
);
4470 wbkgd (loadingw
, CP_MESSAGE_WINDOW
);
4473 wmove (loadingw
, 0, 0);
4474 wclrtobot (loadingw
);
4475 wattron (loadingw
, CP_MESSAGE_BORDER
);
4476 box (loadingw
, ACS_VLINE
, ACS_HLINE
);
4477 wattroff (loadingw
, CP_MESSAGE_BORDER
);
4478 mvwprintw (loadingw
, 1, CENTER_INT ((COLS
/ 2), 11 +
4479 strlen (itoa (gtotal
, buf
))),
4480 _("Loading... %i%% (%i games)"), n
, gtotal
);
4486 init_userdata_once (GAME g
, int n
)
4488 struct userdata_s
*d
= NULL
;
4490 d
= Calloc (1, sizeof (struct userdata_s
));
4492 d
->c_row
= 2, d
->c_col
= 5;
4493 SET_FLAG (d
->flags
, CF_NEW
);
4496 if (pgn_board_init_fen (g
, d
->b
, NULL
) != E_PGN_OK
)
4497 pgn_board_init (d
->b
);
4505 for (i
= 0; i
< gtotal
; i
++)
4506 init_userdata_once (game
[i
], i
);
4510 fix_marks (int *start
, int *end
)
4514 *start
= (*start
< 0) ? 0 : *start
;
4515 *end
= (*end
< 0) ? 0 : *end
;
4524 *end
= (*end
> gtotal
) ? gtotal
: *end
;
4528 do_new_game_finalize (GAME g
)
4530 struct userdata_s
*d
= g
->data
;
4532 d
->mode
= MODE_PLAY
;
4533 update_status_notify (g
, NULL
);
4539 do_new_game_from_scratch (WIN
* win
)
4541 wchar_t str
[] = { win
->c
, 0 };
4543 if (wcscmp (str
, yes_wchar
))
4550 add_custom_tags (&gp
->tag
);
4553 do_new_game_finalize (gp
);
4561 add_custom_tags (&gp
->tag
);
4562 init_userdata_once (gp
, gindex
);
4563 do_new_game_finalize (gp
);
4567 do_game_delete_finalize (int n
)
4569 struct userdata_s
*d
;
4571 delete_game ((!n
) ? gindex
: -1);
4573 if (d
->mode
!= MODE_EDIT
)
4574 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
4578 do_game_delete_confirm (WIN
* win
)
4581 wchar_t str
[] = { win
->c
, 0 };
4583 if (wcscmp (str
, yes_wchar
))
4589 n
= (int *) win
->data
;
4590 do_game_delete_finalize (*n
);
4599 struct userdata_s
*d
;
4604 cmessage (NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
4610 for (i
= n
= 0; i
< gtotal
; i
++)
4614 if (TEST_FLAG (d
->flags
, CF_DELETE
))
4619 tmp
= _("Delete the current game?");
4624 cmessage (NULL
, ANY_KEY_STR
, "%s", _("Cannot delete last game."));
4628 tmp
= _("Delete all games marked for deletion?");
4631 if (config
.deleteprompt
)
4633 p
= Malloc (sizeof (int));
4635 construct_message (NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, p
,
4636 do_game_delete_confirm
, 0, 0, NULL
, "%s", tmp
);
4640 do_game_delete_finalize (n
);
4644 do_find_game_exp_finalize (int which
)
4646 struct userdata_s
*d
= gp
->data
;
4649 if ((n
= find_game_exp (gameexp
, (which
== -1) ? 0 : 1,
4650 (keycount
) ? keycount
: 1)) == -1)
4652 update_status_notify (gp
, "%s", _("No matches found"));
4659 if (pgn_history_total (gp
->hp
))
4660 d
->mode
= MODE_HISTORY
;
4662 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
4666 do_find_game_exp (WIN
* win
)
4668 struct input_data_s
*in
= win
->data
;
4674 strncpy (gameexp
, in
->str
, sizeof (gameexp
));
4675 gameexp
[sizeof (gameexp
) - 1] = 0;
4680 do_find_game_exp_finalize (c
);
4689 do_game_jump_finalize (int n
)
4691 struct userdata_s
*d
;
4693 if (--n
> gtotal
- 1 || n
< 0)
4699 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
4700 update_status_notify (gp
, NULL
);
4704 do_game_jump (WIN
* win
)
4706 struct input_data_s
*in
= win
->data
;
4708 if (!in
->str
|| !isinteger (in
->str
))
4717 do_game_jump_finalize (atoi (in
->str
));
4723 do_load_file (WIN
* win
)
4725 struct input_data_s
*in
= win
->data
;
4726 char *tmp
= in
->str
;
4727 struct userdata_s
*d
;
4728 PGN_FILE
*pgn
= NULL
;
4737 if ((tmp
= pathfix (tmp
)) == NULL
)
4740 n
= pgn_open (tmp
, "r", &pgn
);
4744 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
, strerror (errno
));
4747 else if (n
== E_PGN_INVALID
)
4749 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s\n%s", tmp
,
4750 _("Not a regular file"));
4756 if (pgn_parse (pgn
) == E_PGN_ERR
)
4758 del_panel (loadingp
);
4766 del_panel (loadingp
);
4771 strncpy (loadfile
, tmp
, sizeof (loadfile
));
4772 loadfile
[sizeof (loadfile
) - 1] = 0;
4776 if (pgn_history_total (gp
->hp
))
4777 d
->mode
= MODE_HISTORY
;
4779 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
4781 fm_loaded_file
= TRUE
;
4794 do_game_save (WIN
* win
)
4796 struct input_data_s
*in
= win
->data
;
4799 char *tmp
= in
->str
;
4800 char tfile
[FILENAME_MAX
];
4803 struct userdata_s
*d
;
4805 if (!tmp
|| (tmp
= pathfix (tmp
)) == NULL
)
4808 if (pgn_is_compressed (tmp
) == E_PGN_ERR
)
4810 p
= tmp
+ strlen (tmp
) - 1;
4812 if (*p
!= 'n' || *(p
- 1) != 'g' || *(p
- 2) != 'p' || *(p
- 3) != '.')
4814 snprintf (tfile
, sizeof (tfile
), "%s.pgn", tmp
);
4820 * When in edit mode, update the FEN tag.
4824 for (i
= 0; i
< gtotal
; i
++)
4828 if (d
->mode
== MODE_EDIT
)
4830 char *fen
= pgn_game_to_fen (game
[i
], d
->b
);
4832 pgn_tag_add (&game
[i
]->tag
, (char *) "FEN", fen
);
4841 if (d
->mode
== MODE_EDIT
)
4843 char *fen
= pgn_game_to_fen (game
[n
], d
->b
);
4845 pgn_tag_add (&game
[n
]->tag
, (char *) "FEN", fen
);
4861 do_get_game_save_input (int n
)
4863 struct input_data_s
*in
= Calloc (1, sizeof (struct input_data_s
));
4864 int *p
= Malloc (sizeof (int));
4866 in
->efunc
= do_game_save
;
4870 construct_input (_("Save Game Filename"), loadfile
, 1, 1,
4871 _("Type TAB for file browser"), file_browser
, NULL
, '\t',
4872 in
, INPUT_HIST_FILE
, NULL
, -1);
4876 do_game_save_multi_confirm (WIN
* win
)
4879 wchar_t str
[] = { win
->c
, 0 };
4881 if (!wcscmp (str
, current_wchar
))
4883 else if (!wcscmp (str
, all_wchar
))
4887 update_status_notify (gp
, "%s", _("Save game aborted."));
4891 do_get_game_save_input (i
);
4897 cmessage (_("ABOUT"), ANY_KEY_STR
,
4898 _("%s\nUsing %s with %i colors and %i color pairs\n%s\n%s"),
4899 PACKAGE_STRING
, curses_version (), COLORS
, COLOR_PAIRS
,
4900 COPYRIGHT
, CBOARD_URL
);
4904 global_game_next_prev (int which
)
4906 struct userdata_s
*d
;
4908 game_next_prev (gp
, (which
== 1) ? 1 : 0, (keycount
) ? keycount
: 1);
4915 markend
= markstart
+ delete_count
;
4920 markend
= markstart
- delete_count
+ 1;
4921 delete_count
= -1; // to fix gindex in the other direction
4924 fix_marks (&markstart
, &markend
);
4925 do_global_toggle_delete ();
4928 if (d
->mode
== MODE_HISTORY
)
4929 pgn_board_update (gp
, d
->b
, gp
->hindex
);
4930 else if (d
->mode
== MODE_PLAY
)
4931 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
4935 do_global_next_game ()
4937 global_game_next_prev (1);
4941 do_global_prev_game ()
4943 global_game_next_prev (0);
4947 global_find (int which
)
4949 struct input_data_s
*in
;
4955 in
= Calloc (1, sizeof (struct input_data_s
));
4956 p
= Malloc (sizeof (int));
4959 in
->efunc
= do_find_game_exp
;
4961 if (!*gameexp
|| which
== 0)
4963 construct_input (_("Find Game by Tag Expression"), NULL
, 1, 0,
4964 _("[name expression:]value expression"), NULL
, NULL
, 0,
4965 in
, INPUT_HIST_GAME_EXP
, NULL
, -1);
4971 do_find_game_exp_finalize (which
);
4975 do_global_find_new ()
4981 do_global_find_next ()
4987 do_global_find_prev ()
4993 do_global_game_jump ()
5000 struct input_data_s
*in
;
5002 in
= Calloc (1, sizeof (struct input_data_s
));
5003 in
->efunc
= do_game_jump
;
5004 construct_input (_("Jump to Game Number"), NULL
, 1, 1, NULL
, NULL
, NULL
,
5005 0, in
, -1, NULL
, 0);
5009 do_game_jump_finalize (keycount
);
5013 do_global_toggle_delete ()
5022 if (keycount
&& delete_count
== 0)
5025 delete_count
= keycount
;
5026 update_status_notify (gp
, "%s (delete)", status
.notify
);
5030 if (markstart
>= 0 && markend
>= 0)
5032 for (i
= markstart
; i
< markend
; i
++)
5034 if (toggle_delete_flag (i
))
5040 gindex
= (delete_count
< 0) ? markstart
: i
- 1;
5044 if (toggle_delete_flag (gindex
))
5048 markstart
= markend
= -1;
5050 update_status_window (gp
);
5054 do_global_delete_game ()
5060 do_global_tag_edit ()
5062 struct userdata_s
*d
= gp
->data
;
5064 edit_tags (gp
, d
->b
, 1);
5068 do_global_tag_view ()
5070 struct userdata_s
*d
= gp
->data
;
5072 edit_tags (gp
, d
->b
, 0);
5076 do_global_resume_game ()
5078 struct input_data_s
*in
;
5080 in
= Calloc (1, sizeof (struct input_data_s
));
5081 in
->efunc
= do_load_file
;
5082 construct_input (_("Load Filename"), NULL
, 1, 1,
5083 _("Type TAB for file browser"), file_browser
, NULL
, '\t',
5084 in
, INPUT_HIST_FILE
, NULL
, -1);
5088 do_global_save_game ()
5092 construct_message (NULL
, _("What would you like to do?"), 0, 1,
5093 NULL
, NULL
, NULL
, do_game_save_multi_confirm
, 0, 0,
5095 ("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."),
5096 current_wchar
, all_wchar
);
5100 do_get_game_save_input (-1);
5104 do_global_new_game ()
5110 copy_game_common (int fen
)
5114 struct userdata_s
*d
= gp
->data
;
5115 char *fentag
= fen
? pgn_game_to_fen (gp
, d
->b
) : NULL
;
5117 do_global_new_game ();
5119 n
= pgn_tag_total (game
[g
]->tag
);
5121 for (i
= 0; i
< n
; i
++)
5122 pgn_tag_add (&gp
->tag
, game
[g
]->tag
[i
]->name
, game
[g
]->tag
[i
]->value
);
5126 pgn_tag_add (&gp
->tag
, (char *) "SetUp", (char *) "1");
5127 pgn_tag_add (&gp
->tag
, (char *) "FEN", fentag
);
5129 pgn_tag_sort (gp
->tag
);
5133 pgn_board_init_fen (gp
, d
->b
, NULL
);
5134 n
= pgn_history_total (game
[g
]->history
);
5137 for (i
= 0; i
< n
; i
++)
5140 char *move
= strdup (game
[g
]->history
[i
]->move
);
5142 if (pgn_parse_move (gp
, d
->b
, &move
, &frfr
) != E_PGN_OK
)
5145 SET_FLAG (gp
->flags
, GF_PERROR
);
5149 pgn_history_add (gp
, d
->b
, move
);
5152 pgn_switch_turn (gp
);
5156 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
5160 do_global_copy_game ()
5162 copy_game_common (0);
5166 do_global_copy_game_fen ()
5168 copy_game_common (1);
5172 do_global_new_all ()
5174 construct_message (NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
5175 do_new_game_from_scratch
, 0, 0, NULL
, "%s",
5176 _("Really start a new game from scratch?"));
5182 wchar_t str
[] = { win
->c
, 0 };
5183 int n
= wcscmp (str
, yes_wchar
);
5194 if (config
.exitdialogbox
)
5195 construct_message (NULL
, _("[ Yes or No ]"), 1, 1, NULL
, NULL
, NULL
,
5196 do_quit
, 0, 0, NULL
, "%s", _("Want to Quit?"));
5202 do_global_toggle_engine_window ()
5206 enginew
= newwin (LINES
, COLS
, 0, 0);
5207 enginep
= new_panel (enginew
);
5208 window_draw_title (enginew
, _("Engine IO Window"), COLS
,
5209 CP_MESSAGE_TITLE
, CP_MESSAGE_BORDER
);
5210 hide_panel (enginep
);
5213 if (panel_hidden (enginep
))
5215 update_engine_window (gp
);
5216 top_panel (enginep
);
5220 hide_panel (enginep
);
5225 do_global_toggle_board_details ()
5227 do_board_details ();
5231 do_play_toggle_strict_castling ()
5233 do_toggle_strict_castling ();
5236 // Global and other keys.
5240 struct userdata_s
*d
= gp
->data
;
5244 * These cannot be modified and other game mode keys cannot conflict with
5250 d
->sp
.icon
= d
->sp
.srow
= d
->sp
.scol
= 0;
5251 markend
= markstart
= 0;
5256 update_status_notify (gp
, NULL
);
5259 if (config
.validmoves
)
5260 pgn_reset_valid_moves (d
->b
);
5267 keycount
= keycount
* 10 + i
;
5271 update_status_notify (gp
, _("Repeat %i"), keycount
);
5274 if (d
->mode
== MODE_HISTORY
)
5278 d
->c_row
+= keycount
;
5287 if (d
->mode
== MODE_HISTORY
)
5292 d
->c_row
-= keycount
;
5293 update_status_notify (gp
, NULL
);
5303 if (d
->mode
== MODE_HISTORY
)
5307 d
->c_col
-= keycount
;
5316 if (d
->mode
== MODE_HISTORY
)
5320 d
->c_col
+= keycount
;
5332 for (i
= 0; global_keys
[i
]; i
++)
5334 if (input_c
== global_keys
[i
]->c
&& global_keys
[i
]->f
)
5336 (*global_keys
[i
]->f
) ();
5348 perl_error (const char *fmt
, ...)
5354 vasprintf (&buf
, fmt
, ap
);
5357 message (ERROR_STR
, ANY_KEY_STR
, "%s", buf
);
5362 do_perl_finalize (WIN
* win
)
5364 struct input_data_s
*in
= win
->data
;
5366 struct userdata_s
*d
= g
->data
;
5368 char *result
= NULL
;
5372 asprintf (&filename
, "%s/perl.pl", config
.datadir
);
5377 if (perl_init_file (filename
, perl_error
))
5380 arg
= pgn_game_to_fen (g
, d
->b
);
5382 if (perl_call_sub (trim (in
->str
), arg
, &result
))
5385 d
->perlfen
= pgn_game_to_fen (g
, d
->b
);
5386 d
->perlflags
= g
->flags
;
5388 if (pgn_board_init_fen (g
, d
->b
, result
) != E_PGN_OK
)
5390 message (ERROR_STR
, ANY_KEY_STR
, "%s", _("FEN parse error."));
5391 pgn_board_init_fen (g
, d
->b
, d
->perlfen
);
5392 g
->flags
= d
->perlflags
;
5398 SET_FLAG (d
->flags
, CF_PERL
);
5399 n
= pgn_tag_find (g
->tag
, "FEN");
5402 d
->oldfen
= strdup (g
->tag
[n
]->value
);
5404 pgn_tag_add (&g
->tag
, (char *) "FEN", result
);
5405 update_status_notify (g
, "%s", ANY_KEY_STR
);
5419 struct input_data_s
*in
;
5421 in
= Calloc (1, sizeof (struct input_data_s
));
5423 in
->efunc
= do_perl_finalize
;
5424 construct_input (_("PERL Subroutine Filter"), NULL
, 1, 0, NULL
, NULL
, NULL
,
5425 0, in
, INPUT_HIST_PERL
, NULL
, -1);
5430 * A macro may contain a key that belongs to another macro so macro_match will
5431 * need to be updated to the new index of the matching macro.
5434 find_macro (struct userdata_s
*d
)
5439 * Macros can't contain macros when in a window.
5445 for (i
= 0; macros
[i
]; i
++)
5447 if ((macros
[i
]->mode
== -1 || macros
[i
]->mode
== d
->mode
) &&
5448 input_c
== macros
[i
]->c
)
5450 input_c
= macros
[i
]->keys
[macros
[i
]->n
++];
5452 if (!macro_depth_n
&& macro_match
> -1)
5455 realloc (macro_depth
, (macro_depth_n
+ 1) * sizeof (int));
5456 macro_depth
[macro_depth_n
++] = macro_match
;
5460 realloc (macro_depth
, (macro_depth_n
+ 1) * sizeof (int));
5461 macro_depth
[macro_depth_n
++] = i
;
5469 * Resets the position in each macro to the first key.
5475 struct userdata_s
*d
= gp
->data
;
5478 if (macro_depth_n
> 0)
5481 macro_match
= macro_depth
[macro_depth_n
];
5483 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
5486 input_c
= macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
5491 for (i
= 0; macros
[i
]; i
++)
5503 struct userdata_s
*d
;
5506 gindex
= gtotal
- 1;
5510 if (pgn_history_total (gp
->hp
))
5511 d
->mode
= MODE_HISTORY
;
5514 d
->mode
= MODE_PLAY
;
5515 d
->play_mode
= PLAY_HE
;
5521 d
->pm_frfr
[0] = '\0';
5523 if (d
->mode
== MODE_HISTORY
)
5524 pgn_board_update (gp
, d
->b
, pgn_history_total (gp
->hp
));
5526 update_status_notify (gp
, _("Type %ls for help"),
5527 key_lookup (global_keys
, do_global_help
));
5531 wtimeout (boardw
, WINDOW_TIMEOUT
);
5536 char fdbuf
[8192] = { 0 };
5538 struct timeval tv
= { 0, 0 };
5546 for (i
= 0; i
< gtotal
; i
++)
5550 if (d
->engine
&& d
->engine
->pid
!= -1)
5552 if (d
->engine
->fd
[ENGINE_IN_FD
] > 2)
5554 if (d
->engine
->fd
[ENGINE_IN_FD
] > n
)
5555 n
= d
->engine
->fd
[ENGINE_IN_FD
];
5557 FD_SET (d
->engine
->fd
[ENGINE_IN_FD
], &rfds
);
5560 if (d
->engine
->fd
[ENGINE_OUT_FD
] > 2)
5562 if (d
->engine
->fd
[ENGINE_OUT_FD
] > n
)
5563 n
= d
->engine
->fd
[ENGINE_OUT_FD
];
5565 FD_SET (d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
);
5572 if ((n
= select (n
+ 1, &rfds
, &wfds
, NULL
, &tv
)) > 0)
5574 for (i
= 0; i
< gtotal
; i
++)
5578 if (d
->engine
&& d
->engine
->pid
!= -1)
5580 if (FD_ISSET (d
->engine
->fd
[ENGINE_IN_FD
], &rfds
))
5582 len
= read (d
->engine
->fd
[ENGINE_IN_FD
], fdbuf
,
5587 if (d
->engine
->iobuf
)
5589 Realloc (d
->engine
->iobuf
,
5590 d
->engine
->len
+ len
+ 1);
5592 d
->engine
->iobuf
= Calloc (1, len
+ 1);
5594 memcpy (&(d
->engine
->iobuf
[d
->engine
->len
]),
5596 d
->engine
->len
+= len
;
5597 d
->engine
->iobuf
[d
->engine
->len
] = 0;
5600 * The fdbuf is full or no newline
5601 * was found. So we'll append the next
5602 * read() to this games buffer.
5604 if (d
->engine
->iobuf
[d
->engine
->len
- 1] !=
5608 parse_engine_output (game
[i
], d
->engine
->iobuf
);
5609 free (d
->engine
->iobuf
);
5610 d
->engine
->iobuf
= NULL
;
5615 if (errno
!= EAGAIN
)
5617 cmessage (ERROR_STR
, ANY_KEY_STR
,
5618 "Engine read(): %s",
5620 waitpid (d
->engine
->pid
, &n
, 0);
5628 if (FD_ISSET (d
->engine
->fd
[ENGINE_OUT_FD
], &wfds
))
5630 if (d
->engine
->queue
)
5631 send_engine_command (game
[i
]);
5639 cmessage (ERROR_STR
, ANY_KEY_STR
, "select(): %s",
5649 * This is needed to detect terminal resizing.
5652 if (LINES
!= LINES_OLD
|| COLS
!= COLS_OLD
)
5656 do_window_resize ();
5660 * Finds the top level window in the window stack so we know what
5661 * window the wget_wch()'ed key belongs to.
5665 for (i
= 0; wins
[i
]; i
++);
5668 wtimeout (wp
, WINDOW_TIMEOUT
);
5679 if (macros
&& macro_match
>= 0)
5681 if (macros
[macro_match
]->n
>= macros
[macro_match
]->total
)
5686 macros
[macro_match
]->keys
[macros
[macro_match
]->n
++];
5692 if (wget_wch (wp
, &input_c
) == ERR
|| input_c
== KEY_RESIZE
)
5694 if (input_c
== KEY_RESIZE
)
5699 window_resize_all ();
5714 * Run the function associated with the window. When the
5715 * function returns 0 win->efunc is ran (if not NULL) with
5716 * win as the one and only parameter. Then the window is
5719 * The exit function may create another window which will
5720 * mess up the window stack when window_destroy() is called.
5721 * So don't destory the window until the top window is
5722 * destroyable. See window_destroy().
5724 if ((*win
->func
) (win
) == 0)
5727 (*win
->efunc
) (win
);
5730 window_destroy (win
);
5738 if (!keycount
&& status
.notify
)
5739 update_status_notify (gp
, NULL
);
5742 if (TEST_FLAG (d
->flags
, CF_PERL
))
5744 CLEAR_FLAG (d
->flags
, CF_PERL
);
5745 pgn_board_init_fen (gp
, d
->b
, d
->perlfen
);
5746 gp
->flags
= d
->perlflags
;
5748 pgn_tag_add (&gp
->tag
, (char *) "FEN", d
->oldfen
);
5750 d
->perlfen
= d
->oldfen
= NULL
;
5756 if (macros
&& macro_match
< 0)
5759 if ((n
= globalkeys ()) == 1)
5761 if (macro_match
== -1)
5772 for (i
= 0; edit_keys
[i
]; i
++)
5774 if (input_c
== edit_keys
[i
]->c
)
5776 (*edit_keys
[i
]->f
) ();
5782 for (i
= 0; play_keys
[i
]; i
++)
5784 if (input_c
== play_keys
[i
]->c
)
5786 (*play_keys
[i
]->f
) ();
5791 do_play_config_command ();
5794 for (i
= 0; history_keys
[i
]; i
++)
5796 if (input_c
== history_keys
[i
]->c
)
5798 (*history_keys
[i
]->f
) ();
5809 update_status_notify (gp
, NULL
);
5819 usage (const char *pn
, int ret
)
5821 fprintf ((ret
) ? stderr
: stdout
, "%s%s",
5823 _("Usage: cboard [-hvCD] [-u [N]] [-p [-VtRSE] <file>]\n"
5824 " -D Dump libchess debugging info to \"libchess.debug\" (stderr)\n"),
5826 _("Usage: cboard [-hvC] [-u [N]] [-p [-VtRSE] <file>]\n"),
5828 _(" -p Load PGN file.\n"
5829 " -V Validate a game file.\n"
5830 " -S Validate and output a PGN formatted game.\n"
5831 " -R Like -S but write a reduced PGN formatted game.\n"
5832 " -t Also write custom PGN tags from config file.\n"
5833 " -E Stop processing on file parsing error (overrides config).\n"
5834 " -C Enable strict castling (overrides config).\n"
5835 " -u Enable/disable UTF-8 pieces (1=enable, 0=disable, overrides config).\n"
5836 " -v Version information.\n" " -h This help text.\n"));
5849 free (config
.engine_cmd
);
5850 free (config
.pattern
);
5851 free (config
.ccfile
);
5852 free (config
.nagfile
);
5853 free (config
.configfile
);
5857 for (i
= 0; config
.keys
[i
]; i
++)
5859 free (config
.keys
[i
]->str
);
5860 free (config
.keys
[i
]);
5868 for (i
= 0; config
.einit
[i
]; i
++)
5869 free (config
.einit
[i
]);
5871 free (config
.einit
);
5875 pgn_tag_free (config
.tag
);
5877 free (config
.datadir
);
5879 if (curses_initialized
)
5882 del_panel (historyp
);
5883 del_panel (statusp
);
5892 del_panel (enginep
);
5905 signal_save_pgn (int sig
)
5909 char *p
= config
.savedirectory
? config
.savedirectory
: config
.datadir
;
5912 asprintf (&buf
, "%s/signal-%i-%li.pgn", p
, sig
, now
);
5914 if (do_game_write (buf
, "w", 0, gtotal
))
5916 cmessage (ERROR_STR
, ANY_KEY_STR
, "%s: %s", p
, strerror (errno
));
5917 update_status_notify (gp
, "%s", _("Save game failed."));
5925 catch_signal (int which
, siginfo_t
*info
, void *ctx
)
5936 if (which
== SIGPIPE
&& quit
)
5939 if (which
== SIGPIPE
)
5940 cmessage (NULL
, ANY_KEY_STR
, "%s", _("Broken pipe. Quitting."));
5943 exit (EXIT_FAILURE
);
5950 do_window_resize ();
5951 keypad (boardw
, TRUE
);
5957 signal_save_pgn (which
);
5965 loading_progress (long total
, long offset
)
5967 int n
= (100 * (offset
/ 100) / (total
/ 100));
5969 if (curses_initialized
)
5970 update_loading_window (n
);
5973 fprintf (stderr
, _("Loading... %i%% (%i games)%c"), n
, gtotal
, '\r');
5981 set_config_defaults ();
5982 set_default_keys ();
5983 filetype
= FILE_NONE
;
5984 pgn_config_set (PGN_PROGRESS
, 1024);
5985 pgn_config_set (PGN_PROGRESS_FUNC
, loading_progress
);
5989 main (int argc
, char *argv
[])
5993 char buf
[FILENAME_MAX
];
5994 char datadir
[FILENAME_MAX
];
5995 int ret
= EXIT_SUCCESS
;
5996 int validate_only
= 0, validate_and_write
= 0;
5997 int write_custom_tags
= 0;
6000 int utf8_pieces
= -1;
6001 struct sigaction sigact
;
6003 setlocale (LC_ALL
, "");
6004 bindtextdomain ("cboard", LOCALE_DIR
);
6005 textdomain ("cboard");
6008 #ifndef HAVE_PROGNAME
6009 __progname
= argv
[0];
6012 if ((config
.pwd
= getpwuid (getuid ())) == NULL
)
6013 err (EXIT_FAILURE
, "getpwuid()");
6015 snprintf (datadir
, sizeof (datadir
), "%s/.cboard", config
.pwd
->pw_dir
);
6016 config
.datadir
= strdup (datadir
);
6017 snprintf (buf
, sizeof (buf
), "%s/cc.data", datadir
);
6018 config
.ccfile
= strdup (buf
);
6019 snprintf (buf
, sizeof (buf
), "%s/nag.data", datadir
);
6020 config
.nagfile
= strdup (buf
);
6021 snprintf (buf
, sizeof (buf
), "%s/config", datadir
);
6022 config
.configfile
= strdup (buf
);
6024 if (stat (datadir
, &st
) == -1)
6026 if (errno
== ENOENT
)
6028 if (mkdir (datadir
, 0755) == -1)
6029 err (EXIT_FAILURE
, "%s", datadir
);
6032 err (EXIT_FAILURE
, "%s", datadir
);
6034 stat (datadir
, &st
);
6037 if (!S_ISDIR (st
.st_mode
))
6038 errx (EXIT_FAILURE
, "%s: %s", datadir
, _("Not a directory."));
6043 while ((opt
= getopt (argc
, argv
, "DCEVtSRhp:vu::")) != -1)
6046 while ((opt
= getopt (argc
, argv
, "ECVtSRhp:vu::")) != -1)
6053 unlink ("libchess.debug");
6054 pgn_config_set (PGN_DEBUG
, 1);
6058 pgn_config_set (PGN_STRICT_CASTLING
, 1);
6061 write_custom_tags
= 1;
6067 pgn_config_set (PGN_REDUCED
, 1);
6069 validate_and_write
= 1;
6074 printf ("%s (%s)\n%s\n%s\n", PACKAGE_STRING
, curses_version (),
6075 COPYRIGHT
, CBOARD_URL
);
6076 exit (EXIT_SUCCESS
);
6078 filetype
= FILE_PGN
;
6079 strncpy (loadfile
, optarg
, sizeof (loadfile
));
6080 loadfile
[sizeof (loadfile
) - 1] = 0;
6083 utf8_pieces
= optarg
? atoi (optarg
) : 1;
6087 usage (argv
[0], EXIT_SUCCESS
);
6091 if ((validate_only
|| validate_and_write
) && !*loadfile
)
6092 usage (argv
[0], EXIT_FAILURE
);
6094 if (access (config
.configfile
, R_OK
) == 0)
6095 parse_rcfile (config
.configfile
);
6098 pgn_config_set (PGN_STOP_ON_ERROR
, 1);
6100 memset (&sigact
, 0, sizeof (sigact
));
6101 sigact
.sa_flags
= SA_SIGINFO
;
6102 sigact
.sa_sigaction
= catch_signal
;
6103 sigaction (SIGPIPE
, &sigact
, NULL
);
6104 sigaction (SIGCONT
, &sigact
, NULL
);
6105 sigaction (SIGSTOP
, &sigact
, NULL
);
6106 sigaction (SIGINT
, &sigact
, NULL
);
6107 sigaction (SIGALRM
, &sigact
, NULL
);
6108 sigaction (SIGTERM
, &sigact
, NULL
);
6109 signal (SIGCHLD
, SIG_IGN
);
6111 srandom (getpid ());
6116 if (pgn_open (loadfile
, "r", &pgn
) != E_PGN_OK
)
6117 err (EXIT_FAILURE
, "%s", loadfile
);
6119 ret
= pgn_parse (pgn
);
6123 //ret = parse_fen_file(loadfile);
6125 case FILE_EPD
: // Not implemented.
6128 // No file specified. Empty game.
6129 ret
= pgn_parse (NULL
);
6131 add_custom_tags (&gp
->tag
);
6135 if (validate_only
|| validate_and_write
)
6137 if (validate_and_write
)
6139 if (pgn_open ("-", "r", &pgn
) != E_PGN_OK
)
6140 err (EXIT_FAILURE
, "pgn_open()");
6142 for (i
= 0; i
< gtotal
; i
++)
6144 if (write_custom_tags
)
6145 add_custom_tags (&game
[i
]->tag
);
6147 pgn_write (pgn
, game
[i
]);
6152 fm_loaded_file
= TRUE
;
6158 else if (ret
== E_PGN_ERR
)
6161 if (utf8_pieces
!= -1)
6162 config
.utf8_pieces
= utf8_pieces
;
6164 init_wchar_pieces ();
6165 yes_wchar
= str_to_wchar (_("y"));
6166 all_wchar
= str_to_wchar (_("a"));
6167 overwrite_wchar
= str_to_wchar (_("o"));
6168 resume_wchar
= str_to_wchar (_("r"));
6169 current_wchar
= str_to_wchar (_("c"));
6170 append_wchar
= str_to_wchar (_("a"));
6171 translatable_tag_names
[0] = _("Event");
6172 translatable_tag_names
[1] = _("Site");
6173 translatable_tag_names
[2] = _("Date");
6174 translatable_tag_names
[3] = _("Round");
6175 translatable_tag_names
[4] = _("White");
6176 translatable_tag_names
[5] = _("Black");
6177 translatable_tag_names
[6] = _("Result");
6181 * This fixes window resizing in an xterm.
6183 if (getenv ("DISPLAY") != NULL
)
6185 putenv ((char *) "LINES=");
6186 putenv ((char *) "COLUMNS=");
6189 if (initscr () == NULL
)
6190 errx (EXIT_FAILURE
, "%s", _("Could not initialize curses."));
6192 curses_initialized
= 1;
6194 if (LINES
< 23 || COLS
< 74)
6197 errx (EXIT_FAILURE
, _("Need at least an 74x23 terminal."));
6203 if (has_colors () == TRUE
&& start_color () == OK
)
6204 init_color_pairs ();
6206 boardw
= newwin (BOARD_HEIGHT
, BOARD_WIDTH
, 0, COLS
- BOARD_WIDTH
);
6207 boardp
= new_panel (boardw
);
6208 historyw
= newwin (HISTORY_HEIGHT
, HISTORY_WIDTH
, LINES
- HISTORY_HEIGHT
,
6209 COLS
- HISTORY_WIDTH
);
6210 historyp
= new_panel (historyw
);
6211 statusw
= newwin (STATUS_HEIGHT
, STATUS_WIDTH
, 0, 0);
6212 statusp
= new_panel (statusw
);
6213 tagw
= newwin (TAG_HEIGHT
, TAG_WIDTH
, STATUS_HEIGHT
+ 1, 0);
6214 tagp
= new_panel (tagw
);
6215 keypad (boardw
, TRUE
);
6216 // leaveok(boardw, TRUE);
6217 leaveok (tagw
, TRUE
);
6218 leaveok (statusw
, TRUE
);
6219 leaveok (historyw
, TRUE
);
6223 draw_window_decor ();
6226 free (w_pawn_wchar
);
6227 free (w_rook_wchar
);
6228 free (w_bishop_wchar
);
6229 free (w_knight_wchar
);
6230 free (w_queen_wchar
);
6231 free (w_king_wchar
);
6232 free (b_pawn_wchar
);
6233 free (b_rook_wchar
);
6234 free (b_bishop_wchar
);
6235 free (b_knight_wchar
);
6236 free (b_queen_wchar
);
6237 free (b_king_wchar
);
6239 free (enpassant_wchar
);
6242 free (overwrite_wchar
);
6243 free (resume_wchar
);
6244 free (current_wchar
);
6245 free (append_wchar
);
6246 free (status
.notify
);
6247 exit (EXIT_SUCCESS
);