Annotation of 43BSDReno/games/chess/Xchess/clock.c, revision 1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.