Fix compile time warnings and a couple other cleanups.
[cboard.git] / src / common.h
blob8960b22ce9ba1527dd666307877aff54b538d8d1
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2002-2006 Ben Kibbey <bjk@arbornet.org>
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
19 #ifndef COMMON_H
20 #define COMMON_H
22 #ifdef HAVE_LIMITS_H
23 #include <limits.h>
24 #endif
26 #ifdef HAVE_SYS_PARAM_H
27 #include <sys/param.h>
28 #endif
30 #ifdef HAVE_FORM_H
31 #include <form.h>
32 #endif
34 #ifdef HAVE_PANEL_H
35 #include <panel.h>
36 #endif
38 #if defined(__linux__) && defined(HAVE_VASPRINTF)
39 extern int vasprintf(char **, const char *, va_list);
40 #endif
42 #include "strings.h"
44 #ifndef LINE_MAX
45 #ifdef _POSIX2_LINE_MAX
46 #define LINE_MAX _POSIX2_LINE_MAX
47 #elif defined(_POSIX_LINE_MAX)
48 #define LINE_MAX _POSIX_LINE_MAX
49 #else
50 #define LINE_MAX 2048
51 #endif
52 #endif
54 #ifdef DEBUG
55 #define ACK (curses_initialized && message("ack", "ack", "ack"))
56 #define ACK2 message("ack2", "ack2", "ack2")
57 #endif
59 #define NARRAY(arr) (sizeof(arr) / sizeof(arr[0]))
60 #define CTRL(x) ((x) & 0x1f)
61 #define KEY_ESCAPE CTRL('[')
63 #define CALCPOSY(y) ((y > LINES - 1) ? 0 : LINES / 2 - y / 2)
64 #define CALCPOSX(x) (COLS / 2 - x / 2)
65 #define CENTERX(x, str) (x / 2 - strlen(str) / 2)
67 #define VALIDFILE(f) ((f >= 1 && f <= 8) ? 1 : 0)
68 #define ROWTOBOARD(r) (8 - r)
69 #define COLTOBOARD(c) (c - 1)
70 #define ROWTOINT(r) (r - '0')
71 #define COLTOINT(c) (c - ('a' - 1))
73 #define SET_FLAG(var, f) (var |= f)
74 #define CLEAR_FLAG(var, f) (var &= ~(f))
75 #define TOGGLE_FLAG(var, f) (var ^= (f))
76 #define TEST_FLAG(var, f) (var & f)
78 /* Game flags. */
79 #define GF_PERROR 0x0001 /* Parse error for this game. */
80 #define GF_DELETE 0x0002 /* Flagged for deletion ('x' command). */
81 #define GF_MODIFIED 0x0004 /* Modified tags or history. */
82 #define GF_ENPASSANT 0x0008 /* For En Passant validation. */
83 #define GF_GAMEOVER 0x0010 /* End of game. */
84 #define GF_WK 0x0020 /* For castling validation ... */
85 #define GF_BK 0x0040
86 #define GF_WKR 0x0080
87 #define GF_WQR 0x0100
88 #define GF_BKR 0x0200
89 #define GF_BQR 0x0400
91 enum {
92 OPEN_SQUARE, PAWN, BISHOP, ROOK, KNIGHT, QUEEN, KING, MAX_PIECES
95 enum {
96 WHITE, BLACK
99 enum {
100 TAG_EVENT, TAG_SITE, TAG_DATE, TAG_ROUND, TAG_WHITE, TAG_BLACK, TAG_RESULT
103 enum {
104 GNUCHESS, CRAFTY, MAX_ENGINES
107 typedef struct board_matrix {
108 chtype icon;
109 short valid;
110 short movecount;
111 } BOARD[8][8];
113 enum {
114 BOOK_OFF, BOOK_PREFER, BOOK_BEST, BOOK_WORST, BOOK_RANDOM, BOOK_MAX
117 enum {
118 ENGINE_OFFLINE = -1, ENGINE_READY, ENGINE_THINKING, ENGINE_INITIALIZING
121 enum {
122 MODE_HISTORY, MODE_PLAY, MODE_EDIT
125 #define cmessage(title, prompt, args...) \
126 dump_message(title, prompt, 1, NULL, NULL, NULL, 0, ##args)
128 #define message(title, prompt, args...) \
129 dump_message(title, prompt, 0, NULL, NULL, NULL, 0, ##args)
131 #define show_message(title, prompt, ehelp, func, arg, key, args...) \
132 dump_message(title, prompt, 0, ehelp, func, arg, key, ##args)
134 #define SEND_TO_ENGINE(fmt, args...) (engine_initialized && !noengine) ? \
135 send_to_engine(fmt, ##args) : 0
137 #define get_input_str(title, init) \
138 get_input(title, init, 1, 0, NULL, NULL, NULL, 0, -1, 20)
140 #define get_input_str_clear(title, init) \
141 get_input(title, init, 1, 1, NULL, NULL, NULL, 0, -1, 20)
143 struct {
144 int engine;
145 int side;
146 char *notify;
147 int turn;
148 int mode;
149 } status;
151 typedef struct tags {
152 char *name;
153 char *value;
154 } TAG;
156 typedef struct history {
157 char move[MAX_PGN_MOVE_LEN + 1];
158 char *comment;
159 int nag[MAX_PGN_NAG];
160 } HISTORY;
162 /* This is an array of 'games' structures. One for each game in a file, or
163 * the current game.
165 typedef struct games {
166 TAG *tag;
167 int tindex;
168 int fentag;
169 HISTORY *history;
170 int hindex;
171 int htotal;
172 int sockfd;
173 int ply;
174 int wcaptures;
175 int bcaptures;
176 double moveclock;
177 int flags;
178 int openingside;
179 int castle;
180 } GAME;
182 GAME *game;
184 /* This holds the selected piece info. */
185 struct {
186 chtype icon;
187 int row;
188 int col;
189 int destrow;
190 int destcol;
191 } sp;
193 struct {
194 char *pgn;
195 char *fancy;
196 } fancy_results[4];
198 enum {
199 CONF_BWHITE, CONF_BBLACK, CONF_BSELECTED, CONF_BCURSOR, CONF_BGRAPHICS,
200 CONF_BCOORDS, CONF_BMOVESW, CONF_BMOVESB, CONF_BCOUNT, CONF_BDWINDOW,
201 CONF_TWINDOW, CONF_TTITLE, CONF_TBORDER,
202 CONF_SWINDOW, CONF_STITLE, CONF_SBORDER, CONF_SNOTIFY, CONF_SENGINE,
203 CONF_HWINDOW, CONF_HTITLE, CONF_HBORDER,
204 CONF_MWINDOW, CONF_MTITLE, CONF_MBORDER, CONF_MPROMPT,
205 CONF_IWINDOW, CONF_ITITLE, CONF_IBORDER, CONF_IPROMPT,
206 CONF_MAX_COLORS
209 struct colors {
210 short fg;
211 short bg;
212 int attrs; /* Attributes for a color terminal. */
213 int nattrs; /* Attributes for a non-color terminal. */
216 struct {
217 int jumpcount;
218 int book_method;
219 int engine_depth;
220 int historyagony;
221 int agony;
222 int linegraphics;
223 int saveprompt;
224 int deleteprompt;
225 int clevel;
226 int validmoves;
227 char ics_server[MAXHOSTNAMELEN];
228 int ics_port;
229 struct passwd *pwd;
230 char *ics_user;
231 char *ics_passwd;
232 char *nagfile;
233 char *agonyfile;
234 char *configfile;
235 char *ccfile;
236 char *fifo;
237 char *tmpfile;
238 char *savedirectory;
239 char *engine_cmd;
240 int engine;
241 struct colors color[CONF_MAX_COLORS];
242 TAG *tag;
243 int tindex;
244 } config;
246 enum {
247 PGN_FILE, FEN_FILE, EPD_FILE
250 /* Chess engine file descriptors. 0 = from, 1 = to. */
251 int enginefd[2];
253 int validate_move;
254 int newgameinit;
255 int curses_initialized;
256 char loadfile[FILENAME_MAX];
257 int filetype;
258 int gindex, gtotal; /* Current game and total number of games. */
259 int engine_initialized;
260 int oldhistorytotal; /* This is a failsafe when resuming a game. */
261 int movestep;
262 int noengine;
264 enum {
265 FIELD_TYPE_ALNUM, FIELD_TYPE_ALPHA, FIELD_TYPE_INTEGER,
266 FIELD_TYPE_NUMERIC, FIELD_TYPE_REGEXP, FIELD_TYPE_IPV4, FIELD_TYPE_ENUM,
267 FIELD_TYPE_PGN_TAG_NAME, FIELD_TYPE_PGN_DATE, FIELD_TYPE_PGN_ROUND
270 char *get_input(const char *, const char *, int, int, const char *,
271 char *(*)(void *), void *, chtype, int, ...);
272 void *Calloc(size_t, size_t);
273 void *Realloc(void *, size_t);
274 int dump_message(const char *, const char *, int, const char *,
275 void (*)(void *), void *, int, const char *, ...);
276 char *trim(char *);
278 #ifdef DEBUG
279 #define DUMP(fmt, args...) (write_debug_output(0, fmt, ## args))
280 #define DUMP_F(fmt, args...) (write_debug_output(1, fmt, ## args))
281 void write_debug_output(int, const char *, ...);
282 void dump_board(int, BOARD);
283 void dump_flags(int);
284 int debug;
285 #endif
287 #ifdef WITH_DMALLOC
288 #include <dmalloc.h>
289 #endif
291 #endif