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, 2013, 2014, 2015 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 #include <gtk/gtk.h>
61
62 #if STDC_HEADERS
63 # include <stdlib.h>
64 # include <string.h>
65 #else /* not STDC_HEADERS */
66 extern char *getenv();
67 # if HAVE_STRING_H
68 # include <string.h>
69 # else /* not HAVE_STRING_H */
70 # include <strings.h>
71 # endif /* not HAVE_STRING_H */
72 #endif /* not STDC_HEADERS */
73
74 #if HAVE_SYS_SYSTEMINFO_H
75 # include <sys/systeminfo.h>
76 #endif /* HAVE_SYS_SYSTEMINFO_H */
77
78 #if TIME_WITH_SYS_TIME
79 # include <sys/time.h>
80 # include <time.h>
81 #else
82 # if HAVE_SYS_TIME_H
83 # include <sys/time.h>
84 # else
85 # include <time.h>
86 # endif
87 #endif
88
89 #if HAVE_UNISTD_H
90 # include <unistd.h>
91 #endif
92
93 #if HAVE_SYS_WAIT_H
94 # include <sys/wait.h>
95 #endif
96
97 #include "common.h"
98 #include "backend.h"
99 #include "frontend.h"
100
101 #ifdef __EMX__
102 #ifndef HAVE_USLEEP
103 #define HAVE_USLEEP
104 #endif
105 #define usleep(t) _sleep2(((t)+500)/1000)
106 #endif
107
108 guint delayedEventTimerTag = 0;
109 DelayedEventCallback delayedEventCallback = 0;
110
111 void
FireDelayedEvent(gpointer data)112 FireDelayedEvent(gpointer data)
113 {
114 g_source_remove(delayedEventTimerTag);
115 delayedEventTimerTag = 0;
116 delayedEventCallback();
117 }
118
119 void
ScheduleDelayedEvent(DelayedEventCallback cb,long millisec)120 ScheduleDelayedEvent (DelayedEventCallback cb, long millisec)
121 {
122 if(delayedEventTimerTag && delayedEventCallback == cb)
123 // [HGM] alive: replace, rather than add or flush identical event
124 g_source_remove(delayedEventTimerTag);
125 delayedEventCallback = cb;
126 delayedEventCallback = cb;
127 delayedEventTimerTag = g_timeout_add(millisec,(GSourceFunc) FireDelayedEvent, NULL);
128 }
129
130 DelayedEventCallback
GetDelayedEvent()131 GetDelayedEvent ()
132 {
133 if (delayedEventTimerTag) {
134 return delayedEventCallback;
135 } else {
136 return NULL;
137 }
138 }
139
140 void
CancelDelayedEvent()141 CancelDelayedEvent ()
142 {
143 if (delayedEventTimerTag) {
144 g_source_remove(delayedEventTimerTag);
145 delayedEventTimerTag = 0;
146 }
147 }
148
149
150 guint loadGameTimerTag = 0;
151
LoadGameTimerRunning()152 int LoadGameTimerRunning()
153 {
154 return loadGameTimerTag != 0;
155 }
156
157 int
StopLoadGameTimer()158 StopLoadGameTimer ()
159 {
160 if (loadGameTimerTag != 0) {
161 g_source_remove(loadGameTimerTag);
162 loadGameTimerTag = 0;
163 return TRUE;
164 } else {
165 return FALSE;
166 }
167 }
168
169 void
LoadGameTimerCallback(gpointer data)170 LoadGameTimerCallback(gpointer data)
171 {
172 g_source_remove(loadGameTimerTag);
173 loadGameTimerTag = 0;
174 AutoPlayGameLoop();
175 }
176
177 void
StartLoadGameTimer(long millisec)178 StartLoadGameTimer (long millisec)
179 {
180 loadGameTimerTag =
181 g_timeout_add( millisec, (GSourceFunc) LoadGameTimerCallback, NULL);
182 }
183
184 guint analysisClockTag = 0;
185
186 int
AnalysisClockCallback(gpointer data)187 AnalysisClockCallback(gpointer data)
188 {
189 if (gameMode == AnalyzeMode || gameMode == AnalyzeFile
190 || appData.icsEngineAnalyze) { // [DM]
191 AnalysisPeriodicEvent(0);
192 }
193 return 1;
194 }
195
196 void
StartAnalysisClock()197 StartAnalysisClock ()
198 {
199 analysisClockTag =
200 g_timeout_add( 2000,(GSourceFunc) AnalysisClockCallback, NULL);
201 }
202
203 guint clockTimerTag = 0;
204
205 int
ClockTimerRunning()206 ClockTimerRunning ()
207 {
208 return clockTimerTag != 0;
209 }
210
211 int
StopClockTimer()212 StopClockTimer ()
213 {
214 if (clockTimerTag != 0)
215 {
216 g_source_remove(clockTimerTag);
217 clockTimerTag = 0;
218 return TRUE;
219 } else {
220 return FALSE;
221 }
222 }
223
224 void
ClockTimerCallback(gpointer data)225 ClockTimerCallback(gpointer data)
226 {
227 /* remove timer */
228 g_source_remove(clockTimerTag);
229 clockTimerTag = 0;
230
231 DecrementClocks();
232 }
233
234 void
StartClockTimer(long millisec)235 StartClockTimer (long millisec)
236 {
237 clockTimerTag = g_timeout_add(millisec,(GSourceFunc) ClockTimerCallback,NULL);
238 }
239