ee5274d28cc683589c4155540e246a8488696be5
[gnushogi.git] / gnushogi / dspwrappers.c
1 /*
2  * FILE: dspwrappers.c
3  *
4  *     Wrapper functions which call analogous functions in rawdsp.c
5  *     or cursesdsp.c depending on the interface.
6  *
7  * ----------------------------------------------------------------------
8  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
9  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
10  *
11  * GNU SHOGI is based on GNU CHESS
12  *
13  * Copyright (c) 1988, 1989, 1990 John Stanback
14  * Copyright (c) 1992 Free Software Foundation
15  *
16  * This file is part of GNU SHOGI.
17  *
18  * GNU Shogi is free software; you can redistribute it and/or modify it
19  * under the terms of the GNU General Public License as published by the
20  * Free Software Foundation; either version 3 of the License,
21  * or (at your option) any later version.
22  *
23  * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
24  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26  * for more details.
27  *
28  * You should have received a copy of the GNU General Public License along
29  * with GNU Shogi; see the file COPYING. If not, see
30  * <http://www.gnu.org/licenses/>.
31  * ----------------------------------------------------------------------
32  *
33  */
34
35 /* request *snprintf prototypes*/
36 #define _POSIX_C_SOURCE 200112L
37
38 #include "gnushogi.h"
39 #include "dspwrappers.h"
40 #include "rawdsp.h"
41 #include "cursesdsp.h"
42
43 #include <stdio.h>
44
45 #define CASE_DSP_RAW(func,args) \
46   case DISPLAY_RAW:             \
47   case DISPLAY_X:               \
48     func args;                  \
49     break
50
51 #ifdef HAVE_LIBCURSES
52 #define CASE_DSP_CURSES(func,args) \
53   case DISPLAY_CURSES:             \
54     func args;                     \
55     break;
56 #else
57 #define CASE_DSP_CURSES(func,args)
58 #endif
59
60 #define DISPLAY_FUNC(func,argsdecl,args)        \
61   void func argsdecl                            \
62   {                                             \
63     switch (display_type) {                     \
64       CASE_DSP_RAW(Raw_ ## func, args);         \
65       CASE_DSP_CURSES(Curses_ ## func, args);   \
66     }                                           \
67   }
68 #define DISPLAY_VOIDFUNC(func)                  \
69   DISPLAY_FUNC(func,(void),())
70
71 #define DISPLAY_STDARGFUNC(func,argsdecl,last,args)     \
72   void func argsdecl                                    \
73   {                                                     \
74     va_list ap;                                         \
75     va_start(ap, last);                                 \
76     switch (display_type) {                             \
77       CASE_DSP_RAW(Raw_ ## func, args);                 \
78       CASE_DSP_CURSES(Curses_ ## func, args);           \
79     }                                                   \
80     va_end(ap);                                         \
81   }
82
83 DISPLAY_VOIDFUNC(ChangeAlphaWindow)
84 DISPLAY_VOIDFUNC(ChangeBetaWindow)
85 DISPLAY_VOIDFUNC(ChangeHashDepth)
86 DISPLAY_VOIDFUNC(ChangeSearchDepth)
87 DISPLAY_VOIDFUNC(ChangeXwindow)
88 DISPLAY_VOIDFUNC(ClearScreen)
89 DISPLAY_VOIDFUNC(DoDebug)
90 DISPLAY_FUNC(DoTable, (short table[NO_SQUARES]), (table))
91 DISPLAY_VOIDFUNC(EditBoard)
92 DISPLAY_VOIDFUNC(ExitShogi)
93 DISPLAY_VOIDFUNC(GiveHint)
94 DISPLAY_VOIDFUNC(Initialize)
95 DISPLAY_FUNC(ShowNodeCnt, (long NodeCnt), (NodeCnt))
96 DISPLAY_VOIDFUNC(OutputMove)
97 DISPLAY_VOIDFUNC(PollForInput)
98 DISPLAY_VOIDFUNC(SetContempt)
99 DISPLAY_FUNC(SearchStartStuff, (short side), (side))
100 DISPLAY_FUNC(SelectLevel, (char *sx), (sx))
101 DISPLAY_VOIDFUNC(SetupBoard)
102 DISPLAY_FUNC(ShowCurrentMove, (short pnt, short f, short t), (pnt, f, t))
103 DISPLAY_FUNC(ShowDepth, (char ch), (ch))
104 DISPLAY_VOIDFUNC(ShowGameType)
105 DISPLAY_FUNC(ShowLine, (unsigned short *bstline), (bstline))
106 DISPLAY_FUNC(ShowMessage, (char *s), (s))
107 DISPLAY_STDARGFUNC(AlwaysShowMessage, (const char *format, ...), format, (format, ap))
108 DISPLAY_STDARGFUNC(Printf, (const char *format, ...), format, (format, ap))
109 DISPLAY_FUNC(ShowPatternCount, (short side, short n), (side, n))
110 DISPLAY_FUNC(ShowPostnValue, (short sq), (sq))
111 DISPLAY_VOIDFUNC(ShowPostnValues)
112 DISPLAY_VOIDFUNC(ShowPrompt)
113 DISPLAY_VOIDFUNC(ShowResponseTime)
114 DISPLAY_FUNC(ShowResults, (short score, unsigned short *bstline, char ch), (score, bstline, ch))
115 DISPLAY_VOIDFUNC(ShowSidetoMove)
116 DISPLAY_VOIDFUNC(ShowStage)
117 DISPLAY_FUNC(TerminateSearch, (int sig), (sig))
118 DISPLAY_VOIDFUNC(UpdateClocks)
119 DISPLAY_FUNC(UpdateDisplay, (short f, short t, short redraw, short isspec), (f, t, redraw, isspec))
120 DISPLAY_VOIDFUNC(help)
121
122 DISPLAY_FUNC(doRequestInputString, (const char* fmt, char* buffer), (fmt, buffer))
123 void RequestInputString(char* buffer, unsigned bufsize)
124 {
125     static char fmt[10];
126     int ret = snprintf(fmt, sizeof(fmt), "%%%us", bufsize);
127     if (ret >= sizeof(fmt)) {
128         fprintf(stderr,
129                 "Insufficient format-buffer size in %s for bufsize=%u\n",
130                 __FUNCTION__, bufsize);
131         exit(1);
132     }
133     doRequestInputString(fmt, buffer);
134 }
135
136 /*********/
137
138 #define CASE_DSPFUNC_RAW(func,args) \
139   case DISPLAY_RAW:                 \
140   case DISPLAY_X:                   \
141     return (func args);             \
142     break
143
144 #ifdef HAVE_LIBCURSES
145 #define CASE_DSPFUNC_CURSES(func,args) \
146   case DISPLAY_CURSES:                 \
147     return (func args);                \
148     break;
149 #else
150 #define CASE_DSPFUNC_CURSES(func,args)
151 #endif
152
153 #define DISPLAY_INTFUNC(func,argsdecl,args)         \
154   int func argsdecl                                 \
155   {                                                 \
156     switch (display_type) {                         \
157       CASE_DSPFUNC_RAW(Raw_ ## func, args);         \
158       CASE_DSPFUNC_CURSES(Curses_ ## func, args);   \
159     }                                               \
160     assert(0);                                      \
161   }
162
163 DISPLAY_INTFUNC(GetString, (char* sx), (sx))