clock.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* This file contains code for X-CHESS.
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. This file is part of X-CHESS.
  4. X-CHESS is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the X-CHESS General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. X-CHESS, but only under the conditions described in the
  12. X-CHESS General Public License. A copy of this license is
  13. supposed to have been given to you along with X-CHESS so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. /* RCS Info: $Revision: 1.4 $ on $Date: 86/11/26 12:09:47 $
  18. * $Source: /users/faustus/xchess/RCS/clock.c,v $
  19. * Copyright (c) 1986 Wayne A. Christopher, U. C. Berkeley CAD Group
  20. * Permission is granted to do anything with this code except sell it
  21. * or remove this message.
  22. *
  23. * Do stuff with the clocks. The way things work is as follows. We call
  24. * clock_init to draw the clocks initially, but they don't actually start
  25. * running until we call clock_switch for the first time.
  26. */
  27. #include "xchess.h"
  28. int movesperunit = 0;
  29. int timeunit = 0;
  30. bool clock_started = false;
  31. int whiteseconds, blackseconds;
  32. static bool white_running = true;
  33. static long lastwhite, lastblack;
  34. static bool firstmove = true;
  35. extern void dohands(), hilight();
  36. #define PI 3.1415926535897932384
  37. void
  38. clock_draw(win, col)
  39. windata *win;
  40. color col;
  41. {
  42. int i;
  43. char buf[BSIZE];
  44. int x = CLOCK_WIDTH / 2, y = CLOCK_WIDTH / 2;
  45. int xp, yp;
  46. int rad = CLOCK_WIDTH / 2 - 10;
  47. Window w = ((col == WHITE) ? win->wclockwin : win->bclockwin);
  48. XSetDisplay(win->display);
  49. /* Draw a clock face and the hands. */
  50. XCircle(w, x, y, rad, 0.0, 0.0, 1, 1, win->textcolor.pixel, GXcopy,
  51. AllPlanes);
  52. rad -= 8;
  53. for (i = 1; i <= 12; i++) {
  54. xp = x + rad * cos(PI * 3 / 2 + i * PI / 6) - 4;
  55. yp = y + rad * sin(PI * 3 / 2 + i * PI / 6) - 5;
  56. sprintf(buf, "%d", i);
  57. XText(w, xp, yp, buf, strlen(buf), win->small->id,
  58. win->textcolor.pixel, win->textback.pixel);
  59. }
  60. dohands(win, col);
  61. if (white_running) {
  62. hilight(win, WHITE, true);
  63. hilight(win, BLACK, false);
  64. } else {
  65. hilight(win, WHITE, false);
  66. hilight(win, BLACK, true);
  67. }
  68. return;
  69. }
  70. void
  71. clock_init(win, col)
  72. windata *win;
  73. color col;
  74. {
  75. whiteseconds = blackseconds = 0;
  76. clock_started = false;
  77. firstmove = true;
  78. clock_draw(win, col);
  79. return;
  80. }
  81. void
  82. clock_update()
  83. {
  84. int now = time((long *) NULL);
  85. int i;
  86. if (!clock_started) {
  87. lastwhite = lastblack = now;
  88. return;
  89. }
  90. if (white_running) {
  91. whiteseconds += now - lastwhite;
  92. lastwhite = now;
  93. dohands(win1, WHITE);
  94. if (!oneboard)
  95. dohands(win2, WHITE);
  96. if (timeunit) {
  97. i = whiteseconds / timeunit;
  98. if ((i > 0) && (whiteseconds > i * timeunit) &&
  99. (whiteseconds < i * timeunit + 10) &&
  100. (movesperunit * i > movenum)) {
  101. message_add(win1,
  102. "White has exceeded his time limit\n",
  103. true);
  104. if (!oneboard) {
  105. message_add(win2,
  106. "White has exceeded his time limit\n",
  107. true);
  108. }
  109. timeunit = 0;
  110. }
  111. }
  112. } else {
  113. blackseconds += now - lastblack;
  114. lastblack = now;
  115. dohands(win1, BLACK);
  116. if (!oneboard)
  117. dohands(win2, BLACK);
  118. if (timeunit) {
  119. i = blackseconds / timeunit;
  120. if ((i > 0) && (blackseconds > i * timeunit) &&
  121. (blackseconds < i * timeunit + 10) &&
  122. (movesperunit * i > movenum)) {
  123. message_add(win1,
  124. "Black has exceeded his time limit\n",
  125. true);
  126. if (!oneboard) {
  127. message_add(win2,
  128. "Black has exceeded his time limit\n",
  129. true);
  130. }
  131. timeunit = 0;
  132. }
  133. }
  134. }
  135. return;
  136. }
  137. void
  138. clock_switch()
  139. {
  140. if (firstmove) {
  141. clock_started = true;
  142. firstmove = false;
  143. lastwhite = lastblack = time((long *) NULL);
  144. }
  145. if (white_running) {
  146. white_running = false;
  147. lastblack = time((long *) NULL);
  148. hilight(win1, WHITE, false);
  149. hilight(win1, BLACK, true);
  150. if (!oneboard) {
  151. hilight(win2, WHITE, false);
  152. hilight(win2, BLACK, true);
  153. }
  154. } else {
  155. white_running = true;
  156. lastwhite = time((long *) NULL);
  157. hilight(win1, WHITE, true);
  158. hilight(win1, BLACK, false);
  159. if (!oneboard) {
  160. hilight(win2, WHITE, true);
  161. hilight(win2, BLACK, false);
  162. }
  163. }
  164. return;
  165. }
  166. static void
  167. dohands(win, col)
  168. windata *win;
  169. color col;
  170. {
  171. int cx = CLOCK_WIDTH / 2, cy = CLOCK_WIDTH / 2;
  172. double *h = (col == WHITE) ? win->whitehands : win->blackhands;
  173. Window w = (col == WHITE) ? win->wclockwin : win->bclockwin;
  174. long secs = (col == WHITE) ? whiteseconds : blackseconds;
  175. int rad, x, y, i;
  176. XSetDisplay(win->display);
  177. /* First erase the old hands. */
  178. rad = CLOCK_WIDTH / 2 - 30;
  179. for (i = 0; i < 3; i++) {
  180. x = cx + rad * sin(PI - h[i]);
  181. y = cy + rad * cos(PI - h[i]);
  182. XLine(w, cx, cy, x, y, i + 1, i + 1, win->textback.pixel,
  183. GXcopy, AllPlanes);
  184. rad -= 8;
  185. }
  186. h[0] = (secs % 60) * 2 * PI / 60;
  187. h[1] = ((secs / 60) % 60) * 2 * PI / 60;
  188. h[2] = ((secs / 3600) % 12) * 2 * PI / 12;
  189. /* Now draw the new ones. */
  190. rad = CLOCK_WIDTH / 2 - 30;
  191. for (i = 0; i < 3; i++) {
  192. x = cx + rad * sin(PI - h[i]);
  193. y = cy + rad * cos(PI - h[i]);
  194. XLine(w, cx, cy, x, y, i + 1, i + 1, win->textcolor.pixel,
  195. GXcopy, AllPlanes);
  196. rad -= 8;
  197. }
  198. return;
  199. }
  200. static void
  201. hilight(win, col, on)
  202. windata *win;
  203. color col;
  204. bool on;
  205. {
  206. Window w = (col == WHITE) ? win->wclockwin : win->bclockwin;
  207. char *s = (col == WHITE) ? " WHITE " : " BLACK ";
  208. int x, y;
  209. XSetDisplay(win->display);
  210. x = XStringWidth(s, win->large, 0, 0);
  211. XLine(w, 0, CLOCK_HEIGHT - 26, CLOCK_WIDTH, CLOCK_HEIGHT - 26,
  212. BORDER_WIDTH, BORDER_WIDTH, win->border.pixel, GXcopy,
  213. AllPlanes);
  214. XText(w, (CLOCK_WIDTH - x) / 2, CLOCK_HEIGHT - 21, s, strlen(s),
  215. win->large->id, on ? win->textback.pixel :
  216. win->textcolor.pixel, on ? win->textcolor.pixel :
  217. win->textback.pixel);
  218. return;
  219. }