home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / MM1 / CMDS / sclock.lzh / sclock.c < prev   
C/C++ Source or Header  |  1992-09-08  |  8KB  |  200 lines

  1. /*
  2.  *  SClock -- Written (August, 1992) by Blair Leduc
  3.  *
  4.  *     This program displays a clock at the top of the screen.  The clock is
  5.  * drawn on the screen with its own internal font.  This font can be modified
  6.  * by patching the program, or by changing the "numbers.h" header file.  See
  7.  * the header file for more information concerning the font.
  8.  *
  9.  *     This is a quick and dirty way of putting a clock on the top of the
  10.  * screen.  A path to the screen is opened only long enough to get the screen
  11.  * information packet.  Be sure to kill the clock _before_ you kill the
  12.  * window, or it will continue to modify the screen memory (even if it is no
  13.  * longer screen memory).  By the way, this is completely invisible to the
  14.  * system, as the system has no way of knowing that the program is modifying
  15.  * the screen (which causes a problem when the arrow is moved over the clock).
  16.  *
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #include <time.h>
  22. #include <wind.h>
  23. #include <ctype.h>
  24. #include "numbers.h"
  25.  
  26. #ifndef TRUE                                  /* make sure TRUE/FALSE is */
  27. #  define TRUE 1                              /* defined                 */
  28. #  define FALSE 0
  29. #endif
  30. #define TICKS 50                              /* amount of time between */
  31.                                               /* time ckecks            */
  32. char rnum[11][7][8];
  33.  
  34. void Help()                                   /* Show user how to use */
  35. {                                             /* program              */
  36.    fprintf(stderr,"Syntax: SClock [<opts>] <device> [<opts>]\n");
  37.    fprintf(stderr,"Function: Display clock anywhere on the screen.\n");
  38.    fprintf(stderr,"Options:\n");
  39.    fprintf(stderr,"    -b=<num>   Background colour of clock\n");
  40.    fprintf(stderr,"    -f=<num>   Foreground colour of clock\n");
  41.    fprintf(stderr,"    -h         Hourly chime\n");
  42.    fprintf(stderr,"    -m         Display in military time format\n");
  43.    fprintf(stderr,"    -c=<col>   Horizontal position of clock\n");
  44.    fprintf(stderr,"    -l=<line>  Vertical position of clock\n");
  45.    exit(0);
  46. }
  47.  
  48. main(argc,argv)
  49. int argc;
  50. char **argv;
  51. {
  52.    register int i,j,k;
  53.    int path, col=0, line=0, clr=255, back=0;
  54.    SCINFO spack;
  55.    struct sgtbuf ti, last;
  56.    char dev[29], done=FALSE, military=FALSE, chime=FALSE;
  57.    char tstr[10], *start;
  58.  
  59.    *dev=(char)NULL;
  60.    
  61.    for(i=1; i<argc; i++)                      /* Parse command line */
  62.        if (argv[i][0]=='-')
  63.        {
  64.            j=1;
  65.            done=FALSE;
  66.            while (argv[i][j] && !done)
  67.                switch (tolower(argv[i][j++]))
  68.                {
  69.                    case 'c':   col=atoi(&argv[i][j+1]);/* Col pos of clock  */
  70.                                done=TRUE;
  71.                                break;
  72.                    case 'f':   clr=atoi(&argv[i][j+1]);/* FG colour of clk  */
  73.                                done=TRUE;
  74.                                break;
  75.                    case 'l':   line=atoi(&argv[i][j+1]);/* Line pos of clk  */
  76.                                done=TRUE;
  77.                                break;
  78.                    case 'b':   back=atoi(&argv[i][j+1]);/* BG colour of clk */
  79.                                done=TRUE;
  80.                                break;
  81.                    case 'm':   military=TRUE; /* Put in military time */
  82.                                break;
  83.                    case 'h':   chime=TRUE;
  84.                                break;
  85.                    default:    Help();        /* Don't understand opts? */
  86.                                break;
  87.                }
  88.        }                                      /* no '-', must be device */
  89.        else 
  90.        {
  91.            if (*dev)                         /* "We already got one!" */
  92.            {
  93.                fprintf(stderr,"SClock: multiple devices not yet supported.\n");
  94.                exit(1);
  95.            }
  96.            else
  97.                strcpy(dev,argv[i]);
  98.        }
  99.     
  100.    if (!(*dev))
  101.    {
  102.        fprintf(stderr,"SClock: Device required!\n");
  103.        exit(1);
  104.    }
  105.    
  106.    path=open(dev,3);                          /* Grab scinfo; that's all */
  107.    if (path<3)
  108.    {
  109.        fprintf(stderr,"SClock: Cannot open device!\n");
  110.        exit(errno);
  111.    }
  112.    _gs_scinfo(path,&spack);
  113.    close(path);                               /* Don't need the open path */
  114.    
  115.    if (spack.sd_bitsperpixel==4)              /* Convert font to screen     */
  116.    {                                          /* format with proper colours */
  117.        clr&=0x0F;                             /* 16-colour screen           */
  118.        back&=0x0F;
  119.        for(i=0; i<11; i++)
  120.            for(j=0; j<7; j++)
  121.                for(k=0; k<4; k++)
  122.                {
  123.                    if (tnumber[i][j][k<<1]=='X')
  124.                        rnum[i][j][k]=clr<<4;
  125.                    else
  126.                        rnum[i][j][k]=back<<4;
  127.                    if (tnumber[i][j][(k<<1)+1]=='X')
  128.                        rnum[i][j][k]|=clr;
  129.                    else
  130.                        rnum[i][j][k]|=back;
  131.                }
  132.    }
  133.    else if (spack.sd_bitsperpixel==8)         /* 256-colour screen */
  134.    {
  135.        clr&=0xFF;
  136.        back&=0xFF;
  137.        for(i=0; i<11; i++)
  138.            for(j=0; j<7; j++)
  139.                for(k=0; k<8; k++)
  140.                    rnum[i][j][k]=(tnumber[i][j][k]=='X')?clr:back;
  141.    }
  142.    else                                       /* something is wrong */
  143.    {
  144.        fprintf(stderr,"SClock: Unknown screen format.\n");
  145.        exit(1);
  146.    }
  147.                                               /* top left corner of clock */
  148.    start=spack.sd_address+(col*spack.sd_bitsperpixel)+(line*spack.sd_phy_width*8);
  149.  
  150.    while(TRUE)                                /* Do this forever */
  151.    {
  152.        getime(&ti);                           /* Get the time */
  153.                                               /* Has it changed? */
  154.        if (memcmp(&ti,&last,sizeof(struct sgtbuf)))
  155.        {
  156.            if (military)                      /* Put time in right format */
  157.                sprintf(tstr,"%02d:%02d:%02d",ti.t_hour,ti.t_minute,ti.t_second);
  158.            else
  159.                sprintf(tstr,"%2d:%02d:%02d",((i=(ti.t_hour)?ti.t_hour:12)>12)?i-12:i,
  160.                    ti.t_minute, ti.t_second);
  161.                                               /* Draw clock on the screen */
  162.            drawclock(start,tstr,8,spack.sd_phy_width,spack.sd_bitsperpixel,back);
  163.            _strass(&last,&ti,sizeof(struct sgtbuf));
  164.            if (ti.t_minute==0 && ti.t_second==0 && chime)
  165.                write(1,"\x07",1);
  166.        }
  167.        tsleep(TICKS);                         /* Nighty-night */
  168.    }
  169. }
  170.                                               /* Draw clock with int. font */
  171. drawclock(ptr,str, size, width, pixel, bclr)
  172. char *ptr, *str;
  173. int size, pixel, bclr;
  174. short width;
  175. {
  176.    register int i,j,k;
  177.    char *pos;
  178.    
  179.    if (pixel==4)                              /* Fill both pixels in byte */
  180.        bclr|=bclr<<4;
  181.    for(i=0; i<7; i++, ptr+=width)             /* Each char is 7 pixels high */
  182.    {
  183.        pos=ptr;                               /* Start at the left */
  184.        for(j=0; j<size; j++)                  /* One character at a time */
  185.        {
  186.            for(k=0; k<pixel; k++)             /* Loop to draw a character */
  187.            {
  188.                if (str[j]==NULL)              /* At end of the string */
  189.                    break;
  190.                if (str[j]==':')               /* Draw colon */
  191.                    *(pos++)=rnum[10][i][k];
  192.                else if (str[j]==' ')          /* Draw space */
  193.                    *(pos++)=bclr;
  194.                else                           /* Draw number */
  195.                    *(pos++)=rnum[str[j]-48][i][k];
  196.            }
  197.        }
  198.    }
  199. }
  200.