http://ftp.ksu.edu.tw/FTP/FreeBSD/distfiles/xscreensaver-4.20.tar.gz
[xscreensaver] / driver / test-apm.c
1 /* test-apm.c --- playing with the APM library.
2  * xscreensaver, Copyright (c) 1999 Jamie Zawinski <jwz@jwz.org>
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.  No representations are made about the suitability of this
9  * software for any purpose.  It is provided "as is" without express or 
10  * implied warranty.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <stdlib.h>
18 #ifdef HAVE_UNISTD_H
19 # include <unistd.h>
20 #endif
21
22 #include <stdio.h>
23 #include <time.h>
24 #include <sys/time.h>
25
26 #include <X11/Xlib.h>
27 #include <X11/Intrinsic.h>
28
29 #include <apm.h>
30
31 #define countof(x) (sizeof((x))/sizeof(*(x)))
32
33
34 char *progname = 0;
35 char *progclass = "XScreenSaver";
36
37 static const char *
38 blurb (void)
39 {
40   static char buf[255];
41   time_t now = time ((time_t *) 0);
42   char *ct = (char *) ctime (&now);
43   int n = strlen(progname);
44   if (n > 100) n = 99;
45   strncpy(buf, progname, n);
46   buf[n++] = ':';
47   buf[n++] = ' ';
48   strncpy(buf+n, ct+11, 8);
49   strcpy(buf+n+9, ": ");
50   return buf;
51 }
52
53 static void
54 apm_cb (XtPointer closure, int *fd, XtInputId *id)
55 {
56   apm_event_t events[100];
57   int n, i;
58   while ((n = apm_get_events (*fd, 0, events, countof(events)))
59          > 0)
60     for (i = 0; i < n; i++)
61       {
62         fprintf (stderr, "%s: APM event 0x%x: %s.\n", blurb(),
63                  events[i], apm_event_name (events[i]));
64 #if 0
65         switch (events[i])
66           {
67           case APM_SYS_STANDBY:
68           case APM_USER_STANDBY:
69           case APM_SYS_SUSPEND:
70           case APM_USER_SUSPEND:
71           case APM_CRITICAL_SUSPEND:
72             break;
73           }
74 #endif
75       }
76 }
77
78 int
79 main (int argc, char **argv)
80 {
81   XtAppContext app;
82   Widget toplevel_shell = XtAppInitialize (&app, progclass, 0, 0,
83                                            &argc, argv, 0, 0, 0);
84   Display *dpy = XtDisplay (toplevel_shell);
85   int fd;
86   XtInputId id;
87   XtGetApplicationNameAndClass (dpy, &progname, &progclass);
88
89   fd = apm_open ();
90   if (fd <= 0)
91     {
92       fprintf (stderr, "%s: couldn't initialize APM.\n", blurb());
93       exit (1);
94     }
95
96   id = XtAppAddInput(app, fd,
97                      (XtPointer) (XtInputReadMask | XtInputWriteMask),
98                      apm_cb, 0);
99   XtAppMainLoop (app);
100   exit (0);
101 }