Annotation of researchv9/jtools/src/demo/clock.c, revision 1.1

1.1     ! root        1: #include <jerq.h>
        !             2: #undef min
        !             3: short stip_bits[]={
        !             4:        0x1111, 0x4444, 0x1111, 0x4444, 0x1111, 0x4444, 0x1111, 0x4444,
        !             5:        0x1111, 0x4444, 0x1111, 0x4444, 0x1111, 0x4444, 0x1111, 0x4444,
        !             6: };
        !             7: Texture stip;
        !             8: 
        !             9: #define        atoi2(p)        ((*(p)-'0')*10 + *((p)+1)-'0')
        !            10: #define        itoa2(n, s)     { (*(s) = (n)/10 + '0'); (*((s)+1) = n % 10 + '0'); }
        !            11: #define NOALARM "no alarm"
        !            12: Point  ctr, p, cur;
        !            13: int    dx, dy;
        !            14: int    h, m, s;        /* hour, min, sec */
        !            15: int    rh, rm, rs;     /* radius of ... */
        !            16: int    ah, am, as;     /* angle */
        !            17: int    rad;
        !            18: int    olds;
        !            19: char   alarmstr[10] = NOALARM;
        !            20: char   alarmlen;
        !            21: int    alarmset;
        !            22: 
        !            23: main(argc, argv)
        !            24:        char *argv[];
        !            25: {
        !            26:        char date[40], *p;
        !            27:        register long oldtime, newtime;
        !            28:        int stat, c, ds;
        !            29:        int first = 1;
        !            30: 
        !            31:        request(KBD|ALARM);
        !            32:        initdisplay(argc,argv);
        !            33:        stip = ToTexture(stip_bits);
        !            34:        rectf(&display, Drect, F_XOR);
        !            35:        if(argc!=2){
        !            36:                string(&defont,"Usage: clock \"`date`\"",
        !            37:                        &display,Drect.origin,F_XOR);
        !            38:                sleep(600);
        !            39:                exit();
        !            40:        }
        !            41:        initface();
        !            42:        strcpy(date, argv[1]);
        !            43:        h = atoi2(date+11);
        !            44:        m = atoi2(date+14);
        !            45:        s = atoi2(date+17);
        !            46: 
        !            47:        oldtime=realtime();
        !            48:        for ( ds = 0;; ) {
        !            49:                while ((newtime = realtime()) <= oldtime) {
        !            50:                        if(own()&(KBD|ALARM))
        !            51:                                checkalarm();
        !            52:                        sleep(60);
        !            53:                }
        !            54:                ds += newtime-oldtime;
        !            55:                oldtime=newtime;
        !            56:                s += ds / 60;
        !            57:                ds %= 60;
        !            58:                if (olds == s)
        !            59:                        continue;
        !            60:                while (s >= 60) {
        !            61:                        s -= 60;
        !            62:                        m++;
        !            63:                }
        !            64:                olds = s;
        !            65:                while (m >= 60) {
        !            66:                        m -= 60;
        !            67:                        h++;
        !            68:                        if (h >= 24)
        !            69:                                h = 0;
        !            70:                }
        !            71: 
        !            72:                if (!first) {   /* zap previous rays */
        !            73:                        ray(rs, as);
        !            74:                        if (am != as)
        !            75:                                ray(rm, am);
        !            76:                        if (ah != am && ah != as)
        !            77:                                ray(rh, ah);
        !            78:                }
        !            79:                ah = (30 * (h%12) + 30 * m / 60);
        !            80:                am = 6 * m;
        !            81:                as = 6 * s;
        !            82: 
        !            83:                if (P->state & RESHAPED) {
        !            84:                        initface();
        !            85:                        first=1;
        !            86:                        P->state &= ~RESHAPED;
        !            87:                }
        !            88:                if (!first)
        !            89:                        string(&defont,date,&display,Drect.origin,F_XOR);
        !            90:                sprintf(date, "00:00:00");
        !            91:                itoa2(h, date);
        !            92:                itoa2(m, date+3);
        !            93:                itoa2(s, date+6);
        !            94:                string(&defont,date,&display,Drect.origin,first?F_STORE:F_XOR);
        !            95:                first = 0;
        !            96:                ray(rs, as);    /* longest */
        !            97:                if (am != as)
        !            98:                        ray(rm, am);
        !            99:                if (ah != as && ah != am)
        !           100:                        ray(rh, ah);
        !           101:        }
        !           102: }
        !           103: 
        !           104: initface()     /* set up clock circle in window */
        !           105: {
        !           106:        rectf(&display, Drect, F_CLR);
        !           107:        texture(&display, display.rect, &stip, F_STORE);
        !           108:        ctr.x = (Drect.corner.x + Drect.origin.x) / 2;
        !           109:        ctr.y = (Drect.corner.y + Drect.origin.y) / 2;
        !           110:        rad = Drect.corner.x - Drect.origin.x;
        !           111:        if (rad > Drect.corner.y - Drect.origin.y)
        !           112:                rad = Drect.corner.y - Drect.origin.y;
        !           113:        rad = rad/2 - 2;
        !           114:        rh = 6 * rad / 10;
        !           115:        rm = 9 * rad / 10;
        !           116:        rs = rad - 1;
        !           117:        circle(&display, ctr, rad, F_XOR);
        !           118: /*****
        !           119:        circle(&display, ctr, rad-1, F_XOR);
        !           120: ***/
        !           121:        disc(&display, ctr, rad, F_STORE);
        !           122:        string(&defont,alarmstr,&display,
        !           123:                Pt(Drect.origin.x,Drect.corner.y-fontheight(&defont)),F_STORE);
        !           124: }
        !           125: 
        !           126: ray(r, ang)    /* draw ray r at angle ang */
        !           127:        int r, ang;
        !           128: {
        !           129:        int dx, dy;
        !           130: 
        !           131:        dx = muldiv(r, sin(ang), 1024);
        !           132:        dy = muldiv(-r, cos(ang), 1024);
        !           133:        segment(&display, ctr, add(ctr, Pt(dx,dy)), F_XOR);
        !           134: }
        !           135: 
        !           136: checkalarm()
        !           137: {
        !           138:        int c;
        !           139:        if(alarmset && (own()&ALARM)) {
        !           140:                ringbell();
        !           141:                if(--alarmset<=0)
        !           142:                        noalarm();
        !           143:        }
        !           144:        if(own()&KBD) {
        !           145:                while((c=kbdchar())!=-1) {
        !           146:                        if(c=='\r')
        !           147:                                alarmlen = 0;
        !           148:                        else if(c=='\b') {
        !           149:                                if(alarmlen>0) {
        !           150:                                        alarmstring();
        !           151:                                        alarmstr[--alarmlen] = 0;
        !           152:                                        alarmstring();
        !           153:                                }
        !           154:                        } else if(alarmlen<8 && (c==':'||c>='0'&&c<='9')) {
        !           155:                                alarmstring();
        !           156:                                alarmstr[alarmlen++] = c;
        !           157:                                alarmstr[alarmlen] = 0;
        !           158:                                alarmstring();
        !           159:                        }
        !           160:                }
        !           161:                setalarm();
        !           162:        }
        !           163: }
        !           164: 
        !           165: setalarm()
        !           166: {
        !           167:        extern char *strchr();
        !           168:        char *p = alarmstr;
        !           169:        int hr = atoi(p);
        !           170:        int min = 0;
        !           171:        int sec = 0;
        !           172:        int dt;
        !           173:        if((p=strchr(p,':'))!=0)
        !           174:                min = atoi(++p);
        !           175:        if(p && (p=strchr(p,':'))!=0)
        !           176:                sec = atoi(++p);
        !           177:        dt = sec-s + 60*(min-m + 60*(hr-h));
        !           178:        while(dt<0)
        !           179:                dt += 24*60*60;
        !           180:        alarm(dt*60);
        !           181:        alarmset = 10;
        !           182: }
        !           183: 
        !           184: noalarm()
        !           185: {
        !           186:        alarmset = alarmlen = 0;
        !           187:        P->state &= ~ALARM;
        !           188:        alarmstring();
        !           189:        strcpy(alarmstr, NOALARM);
        !           190:        alarmstring();
        !           191: }
        !           192: 
        !           193: alarmstring()
        !           194: {
        !           195:        string(&defont,alarmstr,&display,
        !           196:                Pt(Drect.origin.x,Drect.corner.y-fontheight(&defont)),F_XOR);
        !           197: }

unix.superglobalmegacorp.com

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