[gpm]on screen clock

Marco d'Itri md@Linux.IT
Mon, 29 Oct 2001 03:19:45 +0100


--sdtB3X0nJg68CQEu
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

What do you think about this patch? It prints the current time when the
mouse cursor is moved in the upper left corner of the screen.

-- 
ciao,
Marco

--sdtB3X0nJg68CQEu
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="clock.diff"

diff -u gpm-1.19.3/gpm.c gpm-1.19.3-Md/gpm.c
--- gpm-1.19.3/gpm.c	Fri Oct 26 01:36:53 2001
+++ gpm-1.19.3-Md/gpm.c	Mon Oct 29 03:14:32 2001
@@ -32,6 +32,7 @@
 #include <sys/fcntl.h>     /* O_RDONLY */
 #include <sys/wait.h>      /* wait()   */
 #include <sys/stat.h>      /* mkdir()  */
+#include <time.h>          /* time()   */
 #include <sys/time.h>      /* timeval */
 #include <sys/types.h>     /* socket() */
 #include <sys/socket.h>    /* socket() */
@@ -79,6 +80,7 @@
 int opt_aged = 0;
 char *opt_special=NULL; /* special commands, like reboot or such */
 int opt_rawrep=0;
+int opt_clock = 0;
 Gpm_Type *repeated_type=0;
 
 char *consolename = "/dev/tty0";
@@ -230,10 +232,56 @@
   close(fd);
 }
 
+#define CLOCK_FMT "%02d:%02d"
+#define CLOCK_LEN 5
+#define POSITION (4 + 2 * (0 * 80 + (maxx - CLOCK_LEN - 1)))
+
+static void display_clock(int restore)
+{
+    int fd;
+    static char save[CLOCK_LEN * 2];
+
+    if ((fd = open("/dev/vcc/a", O_RDWR)) < 1) {
+	if (errno != ENOENT)
+	    oops("open(\"/dev/vcc/a\")");
+	if ((fd = open("/dev/vcsa", O_RDWR)) < 1)
+	    oops("open(\"/dev/vcsa\")");
+    }
+    if (restore == 2) {			/* restore the old characters */
+	lseek(fd, POSITION, 0);
+	write(fd, &save, sizeof(save));
+    } else {				/* print the clock */
+	char buf[CLOCK_LEN * 2], buf0[CLOCK_LEN];
+	time_t t;
+	struct tm *tm;
+	int i, j;
+
+	if (restore == 0) {		/* save the old characters for later */
+	    lseek(fd, POSITION, 0);
+	    read(fd, &save, sizeof(save));
+	}
+	t = time(NULL);
+	tm = localtime(&t);
+	sprintf(buf0, CLOCK_FMT, tm->tm_hour, tm->tm_min);
+	i = j = 0;
+	while (i <= sizeof(buf0)) {	/* add the color attribute */
+	    buf[j++] = buf0[i++];
+	    buf[j++] = '\032';		/* green on blue */
+	}
+	lseek(fd, POSITION, 0);
+	write(fd, &buf, sizeof(buf));
+    }
+    close(fd);
+}
+
+#undef POSITION
+#undef CLOCK_FMT
+#undef CLOCK_LEN
+
 /*-------------------------------------------------------------------*/
 static  inline int do_selection(Gpm_Event *event)  /* returns 0, always */
 {
-  static int x1=1, y1=1, x2, y2;
+  static int clock_printed = 0, x1=1, y1=1, x2, y2;
 #define UNPOINTER() 0
 
   x2=event->x; y2=event->y;
@@ -242,6 +290,13 @@
     case GPM_MOVE:
       if (x2<1) x2++; else if (x2>maxx) x2--;
       if (y2<1) y2++; else if (y2>maxy) y2--;
+      if (opt_clock && x2 == maxx && y2 == 1) {
+	display_clock(clock_printed);	/* print the clock */
+	clock_printed = 1;
+      } else if (clock_printed) {
+	display_clock(2);		/* restore the screen */
+	clock_printed = 0;
+      }
       selection_copy(x2,y2,x2,y2,3); /* just highlight pointer */
       return 0;
 
Only in gpm-1.19.3-Md/: gpm.d
Only in gpm-1.19.3-Md/: gpm.o
diff -u gpm-1.19.3/gpmInt.h gpm-1.19.3-Md/gpmInt.h
--- gpm-1.19.3/gpmInt.h	Fri Oct 26 01:36:53 2001
+++ gpm-1.19.3-Md/gpmInt.h	Mon Oct 29 03:13:39 2001
@@ -129,6 +129,7 @@
 extern time_t opt_age_limit;
 extern char *opt_special;
 extern int opt_rawrep;
+extern int opt_clock;
 extern int fifofd;
 extern char *consolename; /* the selected one */
 
diff -u gpm-1.19.3/gpn.c gpm-1.19.3-Md/gpn.c
--- gpm-1.19.3/gpn.c	Fri Oct 26 01:36:54 2001
+++ gpm-1.19.3-Md/gpn.c	Mon Oct 29 03:14:37 2001
@@ -158,6 +158,7 @@
          "    -A [limit]       start with selection disabled (`aged')\n"
          "    -b baud-rate     sets the baud rate (default %d)\n"
          "    -B sequence      allows changing the buttons (default '%s')\n"
+         "    -c               enable clock printing\n"
          "    -d delta         sets the delta value (default %d) (must be 2 or more)\n"
          "    -D	       debug mode: don't auto-background\n"
          "    -g tap-button    sets the button (1-3) that is emulated by tapping on\n"
@@ -294,7 +295,7 @@
 int 
 cmdline(int argc, char **argv)
 {
-  char options[]="a:A::b:B:d:Dg:hi:kl:m:Mo:pr:FR::s:S:t:TveV::23";
+  char options[]="a:A::b:B:cd:Dg:hi:kl:m:Mo:pr:FR::s:S:t:TveV::23";
   int i, opt;
   static struct {char *in; char *out;} seq[] = {
     {"123","01234567"},
@@ -327,6 +328,7 @@
           break;
         case 'b': opt_baud = atoi(optarg); break;
         case 'B': opt_sequence = optarg; break;
+        case 'c': opt_clock = 1; break;
         case 'd': opt_delta = atoi(optarg); break;
         case 'D': gpm_log_daemon = 0; break;
         case 'g':

--sdtB3X0nJg68CQEu--