46dd33463ef63f9d0e15da1cc7cddc3c12ebfb6d
[xboard.git] / xtimer.c
1 /*
2  * xtimer.c -- timing functions for X front end of XBoard
3  *
4  * Copyright 1991 by Digital Equipment Corporation, Maynard,
5  * Massachusetts.
6  *
7  * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
8  * 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
9  *
10  * The following terms apply to Digital Equipment Corporation's copyright
11  * interest in XBoard:
12  * ------------------------------------------------------------------------
13  * All Rights Reserved
14  *
15  * Permission to use, copy, modify, and distribute this software and its
16  * documentation for any purpose and without fee is hereby granted,
17  * provided that the above copyright notice appear in all copies and that
18  * both that copyright notice and this permission notice appear in
19  * supporting documentation, and that the name of Digital not be
20  * used in advertising or publicity pertaining to distribution of the
21  * software without specific, written prior permission.
22  *
23  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
24  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
25  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
26  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
27  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
28  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
29  * SOFTWARE.
30  * ------------------------------------------------------------------------
31  *
32  * The following terms apply to the enhanced version of XBoard
33  * distributed by the Free Software Foundation:
34  * ------------------------------------------------------------------------
35  *
36  * GNU XBoard is free software: you can redistribute it and/or modify
37  * it under the terms of the GNU General Public License as published by
38  * the Free Software Foundation, either version 3 of the License, or (at
39  * your option) any later version.
40  *
41  * GNU XBoard is distributed in the hope that it will be useful, but
42  * WITHOUT ANY WARRANTY; without even the implied warranty of
43  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
44  * General Public License for more details.
45  *
46  * You should have received a copy of the GNU General Public License
47  * along with this program. If not, see http://www.gnu.org/licenses/.  *
48  *
49  *------------------------------------------------------------------------
50  ** See the file ChangeLog for a revision history.  */
51
52 #define HIGHDRAG 1
53
54 #include "config.h"
55
56 #include <stdio.h>
57 #include <signal.h>
58 #include <sys/types.h>
59
60 #if STDC_HEADERS
61 # include <stdlib.h>
62 # include <string.h>
63 #else /* not STDC_HEADERS */
64 extern char *getenv();
65 # if HAVE_STRING_H
66 #  include <string.h>
67 # else /* not HAVE_STRING_H */
68 #  include <strings.h>
69 # endif /* not HAVE_STRING_H */
70 #endif /* not STDC_HEADERS */
71
72 #if HAVE_SYS_SYSTEMINFO_H
73 # include <sys/systeminfo.h>
74 #endif /* HAVE_SYS_SYSTEMINFO_H */
75
76 #if TIME_WITH_SYS_TIME
77 # include <sys/time.h>
78 # include <time.h>
79 #else
80 # if HAVE_SYS_TIME_H
81 #  include <sys/time.h>
82 # else
83 #  include <time.h>
84 # endif
85 #endif
86
87 #if HAVE_UNISTD_H
88 # include <unistd.h>
89 #endif
90
91 #if HAVE_SYS_WAIT_H
92 # include <sys/wait.h>
93 #endif
94
95 #include <X11/Intrinsic.h>
96
97 // [HGM] bitmaps: put before incuding the bitmaps / pixmaps, to know how many piece types there are.
98 #include "common.h"
99 #include "frontend.h"
100 #include "backend.h"
101 #include "backendz.h"
102 #include "moves.h"
103 #include "xboard.h"
104 #include "xboard2.h"
105
106
107 #ifdef __EMX__
108 #ifndef HAVE_USLEEP
109 #define HAVE_USLEEP
110 #endif
111 #define usleep(t)   _sleep2(((t)+500)/1000)
112 #endif
113
114 // in xboard.c (should go to xtimer.h ?)
115 extern XtAppContext appContext;
116
117 XtIntervalId delayedEventTimerXID = 0;
118 DelayedEventCallback delayedEventCallback = 0;
119
120 void
121 FireDelayedEvent ()
122 {
123     delayedEventTimerXID = 0;
124     delayedEventCallback();
125 }
126
127 void
128 ScheduleDelayedEvent (DelayedEventCallback cb, long millisec)
129 {
130     if(delayedEventTimerXID && delayedEventCallback == cb)
131         // [HGM] alive: replace, rather than add or flush identical event
132         XtRemoveTimeOut(delayedEventTimerXID);
133     delayedEventCallback = cb;
134     delayedEventTimerXID =
135       XtAppAddTimeOut(appContext, millisec,
136                       (XtTimerCallbackProc) FireDelayedEvent, (XtPointer) 0);
137 }
138
139 DelayedEventCallback
140 GetDelayedEvent ()
141 {
142   if (delayedEventTimerXID) {
143     return delayedEventCallback;
144   } else {
145     return NULL;
146   }
147 }
148
149 void
150 CancelDelayedEvent ()
151 {
152   if (delayedEventTimerXID) {
153     XtRemoveTimeOut(delayedEventTimerXID);
154     delayedEventTimerXID = 0;
155   }
156 }
157
158 XtIntervalId loadGameTimerXID = 0;
159
160 int
161 LoadGameTimerRunning ()
162 {
163     return loadGameTimerXID != 0;
164 }
165
166 int
167 StopLoadGameTimer ()
168 {
169     if (loadGameTimerXID != 0) {
170         XtRemoveTimeOut(loadGameTimerXID);
171         loadGameTimerXID = 0;
172         return TRUE;
173     } else {
174         return FALSE;
175     }
176 }
177
178 void
179 LoadGameTimerCallback (XtPointer arg, XtIntervalId *id)
180 {
181     loadGameTimerXID = 0;
182     AutoPlayGameLoop();
183 }
184
185 void
186 StartLoadGameTimer (long millisec)
187 {
188     loadGameTimerXID =
189       XtAppAddTimeOut(appContext, millisec,
190                       (XtTimerCallbackProc) LoadGameTimerCallback,
191                       (XtPointer) 0);
192 }
193
194 XtIntervalId analysisClockXID = 0;
195
196 void
197 AnalysisClockCallback (XtPointer arg, XtIntervalId *id)
198 {
199     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile
200          || appData.icsEngineAnalyze) { // [DM]
201         AnalysisPeriodicEvent(0);
202         StartAnalysisClock();
203     }
204 }
205
206 void
207 StartAnalysisClock ()
208 {
209     analysisClockXID =
210       XtAppAddTimeOut(appContext, 2000,
211                       (XtTimerCallbackProc) AnalysisClockCallback,
212                       (XtPointer) 0);
213 }
214
215 XtIntervalId clockTimerXID = 0;
216
217 int
218 ClockTimerRunning ()
219 {
220     return clockTimerXID != 0;
221 }
222
223 int
224 StopClockTimer ()
225 {
226     if (clockTimerXID != 0) {
227         XtRemoveTimeOut(clockTimerXID);
228         clockTimerXID = 0;
229         return TRUE;
230     } else {
231         return FALSE;
232     }
233 }
234
235 void
236 ClockTimerCallback (XtPointer arg, XtIntervalId *id)
237 {
238     clockTimerXID = 0;
239     DecrementClocks();
240 }
241
242 void
243 StartClockTimer (long millisec)
244 {
245     clockTimerXID =
246       XtAppAddTimeOut(appContext, millisec,
247                       (XtTimerCallbackProc) ClockTimerCallback,
248                       (XtPointer) 0);
249 }
250
251