home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / utility / mclock.zoo / cl.c next >
C/C++ Source or Header  |  1991-03-20  |  6KB  |  242 lines

  1. /* Clock program, in Hurs */
  2. /* By Robert Fischer */
  3. /* This program is in the public domain */
  4. #include <gemdefs.h>
  5. #include <nobdefs.h>
  6. #include <e_osbind.h>
  7. #include "mclock.h"
  8. /* --------------------------------------------------------- */
  9. long milli;        /* Current time */
  10. LONG ft;            /* Next clock tick where we should change, * 5 */
  11. LONG hz;            /* ft/5 */
  12. int hour, minute, second;    /* Current time in old style */
  13. LONG next_second;    /* Next clock tick that will be a second */
  14. OBJECT *box;
  15. BOOLEAN mtime = FALSE;    /* Should we display the new kind of time? */
  16. BOOLEAN otime = TRUE;    /* Should we display the old kind of time? */
  17. BOOLEAN h24 = TRUE;        /* Use 24 hour format? */
  18. int changepos = 0;            /* Position to put cursor at when changing time */
  19. /* --------------------------------------------------------- */
  20. /* --------------------------------------------------------- */
  21. dial_form(d, n)    /* Does a form_dial on box d, with mode n */
  22. register OBJECT *d;
  23. int n;
  24. {
  25. int border;
  26.     border = d->ob_spec.boxchar.thickness;
  27.     if (border > 128) border = 256-border;
  28.     else border = 0;
  29.     if (border < 4) border = 4;
  30.  
  31.     form_dial(n, d->ob_x + (d->ob_width >> 1), d->ob_y + (d->ob_height >> 1),
  32.         0, 0, d->ob_x - border, d->ob_y - border,
  33.         d->ob_width + (border << 1), d->ob_height + (border << 1));
  34. }
  35. /* --------------------------------------------------------- */
  36. choose_timekind()        /* Decide what kind of time to use */
  37. {
  38.     /* Draw the box */
  39.     dial_form(box, FMD_START);
  40.     objc_draw(box, 0, 256, 0, 0, 0, 0);
  41.  
  42.     /* Let user play */
  43.     box[form_do(box, 0)].ob_state = NORMAL;
  44.  
  45.     /* Undraw box */
  46.     dial_form(box, FMD_FINISH);
  47.  
  48.     /* Blank the areas that were used */
  49.     changepos = 80;
  50.     if (mtime) changepos = 73;
  51.     if (otime) {
  52.         changepos -= 9;
  53.         if (!h24) changepos -= 3;
  54.     }
  55.     if (changepos == 80) changepos = 0;
  56.  
  57.     /* Set up defaults */
  58.     mtime = (box[MTIMEON].ob_state & SELECTED);
  59.     otime = (box[OTIMEON].ob_state & SELECTED);
  60.     h24 = (box[H24ON].ob_state & SELECTED);
  61.  
  62. }
  63. /* --------------------------------------------------------- */
  64. set_time()
  65. {
  66. long allsec;        /* Count of seconds in the day */
  67. BIOS_DT_REC t;
  68. long newtime;
  69. long diff;
  70.     t.l = Gettime();
  71.  
  72.     /* Convert to hurs, by converting to seconds & then to hurs */
  73.     allsec = (long)t.dt.time.hour * 3600L +
  74.         (long)t.dt.time.minute * 60L +
  75.         (long)t.dt.time.second * 2;
  76.  
  77.     newtime = (allsec * 1000L) / 864;
  78.     diff = milli - newtime;
  79.     if (diff > 3 || diff < -3) {
  80.         milli = newtime;
  81.         hour = t.dt.time.hour;
  82.         minute = t.dt.time.minute;
  83.         second = t.dt.time.second * 2;
  84.     }
  85. }
  86. /* --------------------------------------------------------- */
  87. vblank_rout()    /* This is the routine that goes around all the time */
  88. {
  89. extern int curpos;        /* Current position of emulated cursor */
  90. extern conout();        /* Put a char without BIOS or Line A */
  91. register int i;
  92.  
  93.     /* Change cursor position if requested */
  94.     if (changepos) {
  95.         curpos = changepos;
  96.         while (curpos < 80) conout(' ');
  97.         changepos = 0;
  98.     }
  99.  
  100.     if (HZ_200 > hz) {    /* Add 1 to milli and display */
  101.         milli++;
  102.         if (milli > 99999) milli = 0;
  103.  
  104.         ft += 864;        /* Number of fivetimes in one millihur */
  105.         hz = ft/5;
  106.  
  107.         if (mtime) {
  108.             /* Output current time */
  109.             curpos = 74;
  110.             conout((int)(milli/10000) + '0');
  111.             conout((int)((milli/1000) % 10) + '0');
  112.             conout('.');
  113.             conout((int)((milli/100) % 10) + '0');
  114.             conout((int)((milli/10) % 10) + '0');
  115.             conout((int)((milli % 10) + '0'));
  116.         }
  117.     }
  118.     if (HZ_200 > next_second) {        /* Add 1 to the second count */
  119.         next_second += 200;
  120.         second++;
  121.         if (second == 60) {
  122.             minute ++;
  123.             second = 0;
  124.         }
  125.         if (minute == 60) {
  126.             hour ++;
  127.             if (hour > 23) hour = 0;
  128.             minute = 0;
  129.         }
  130.  
  131.         if (otime) {
  132.             curpos = 72;
  133.             if (mtime) curpos -= 7;
  134.             if (h24) {
  135.                 conout(hour / 10 + '0');
  136.                 conout((hour % 10) + '0');
  137.             } else {
  138.                 curpos -= 3;
  139.                 i = hour % 12;
  140.                 if (i == 0) i = 12;
  141.                 conout(i / 10 + '0');
  142.                 conout((i % 10) + '0');
  143.             }
  144.  
  145.             conout(':');
  146.             conout(minute / 10 + '0');
  147.             conout((minute % 10) + '0');
  148.             conout(':');
  149.             conout(second / 10 + '0');
  150.             conout((second % 10) + '0');
  151.  
  152.             if (!h24) {
  153.                 conout(' ');
  154.                 conout((hour > 11 ? 'P' : 'A'));
  155.                 conout('M');
  156.             }
  157.         }
  158.     }
  159. }
  160. /* --------------------------------------------------------- */
  161. main()
  162. {
  163. LONG usp;
  164. int i;
  165. int acc_handle;        /* Desk accessory's handle in Desk menu */
  166. int msg[8];
  167. char c[3];            /* The info read from MCLOCK.INF */
  168.  
  169.     /* Load time defaults */
  170.     i = Fopen("MCLOCK.INF", G_READ);
  171.     if (i > 0) {
  172.         mtime = h24 = otime = FALSE;
  173.         c[0] = c[1] = c[2] = 0;
  174.         Fread(i, 3L, c);
  175.         Fclose(i);
  176.  
  177.         for (i = 0; i < 3; i++) {
  178.             switch (c[i]) {
  179.                 case 'O' :
  180.                 case 'o' : otime = TRUE; break;
  181.                 case 'M' :
  182.                 case 'm' : mtime = TRUE; break;
  183.                 case '2' :
  184.                 case '4' : h24 = TRUE; break;
  185.             }
  186.         }
  187.     }
  188.  
  189.     /* Set up clock part */
  190.     milli = -5;
  191.     set_time();
  192.  
  193.     usp = Super(0);
  194.     ft = HZ_200 * 5 + 864;
  195.     hz = ft/5;
  196.     next_second = HZ_200 + 200;
  197.  
  198.     /* Install interrupt routine */
  199.     init_vt();
  200.     for (i=0; i<8; i++) {
  201.         if (VBLQUEUE[i] == NULL) {
  202.             VBLQUEUE[i]=&vblank_rout;    /* install scheduler in vblank queue */
  203.             break;
  204.         }
  205.     }
  206.     Super(usp);
  207.  
  208.     if (i == 8) {
  209.         Cconws("Trouble installing VBLANK routine for MCLOCK!\n");
  210.         exit(-1);
  211.     }
  212.  
  213.     /* Set the clock when GEM lets us */
  214.     i = appl_init();
  215.  
  216.     /* Check resolution */
  217.     if (Getrez() != HIGH_RES) {
  218.         otime = mtime = FALSE;
  219.     } else {    /* Set up resource */
  220.         if (rsrc_load("MCLOCK.RSC")) {
  221.             acc_handle = menu_register(i, "  M-Clock");
  222.             rsrc_gaddr(0, THETREE, &box);
  223.             box[MTIMEON + (mtime ? 0 : 1)].ob_state = SELECTED;
  224.             box[OTIMEON + (otime ? 0 : 1)].ob_state = SELECTED;
  225.             box[H24ON + (h24 ? 0 : 1)].ob_state = SELECTED;
  226.             form_center(box, &i, &i, &i, &i);
  227.         }
  228.     }
  229.  
  230.     while (TRUE) {
  231.         if (MU_MESAG & evnt_multi(MU_TIMER | MU_MESAG,
  232.             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  233.             msg, 1000, 0,
  234.             &i, &i, &i, &i, &i, &i)) {        /* Must be a message event */
  235.             if (msg[0] == AC_OPEN) choose_timekind();
  236.         }
  237.  
  238.         set_time();
  239.     }
  240. }
  241. /* --------------------------------------------------------- */
  242.