summaryrefslogtreecommitdiffstats
path: root/driver/test-apm.c
blob: 6b87c7e708426b12ec9b914e8b4b8014a256b8ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* test-apm.c --- playing with the APM library.
 * xscreensaver, Copyright (c) 1999 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdlib.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include <stdio.h>
#include <time.h>
#include <sys/time.h>

#include <X11/Xlib.h>
#include <X11/Intrinsic.h>

#include <apm.h>

#define countof(x) (sizeof((x))/sizeof(*(x)))


char *progname = 0;
char *progclass = "XScreenSaver";

static const char *
blurb (void)
{
  static char buf[255];
  time_t now = time ((time_t *) 0);
  char *ct = (char *) ctime (&now);
  int n = strlen(progname);
  if (n > 100) n = 99;
  strncpy(buf, progname, n);
  buf[n++] = ':';
  buf[n++] = ' ';
  strncpy(buf+n, ct+11, 8);
  strcpy(buf+n+9, ": ");
  return buf;
}

static void
apm_cb (XtPointer closure, int *fd, XtInputId *id)
{
  apm_event_t events[100];
  int n, i;
  while ((n = apm_get_events (*fd, 0, events, countof(events)))
         > 0)
    for (i = 0; i < n; i++)
      {
        fprintf (stderr, "%s: APM event 0x%x: %s.\n", blurb(),
                 events[i], apm_event_name (events[i]));
#if 0
        switch (events[i])
          {
          case APM_SYS_STANDBY:
          case APM_USER_STANDBY:
          case APM_SYS_SUSPEND:
          case APM_USER_SUSPEND:
          case APM_CRITICAL_SUSPEND:
            break;
          }
#endif
      }
}

int
main (int argc, char **argv)
{
  XtAppContext app;
  Widget toplevel_shell = XtAppInitialize (&app, progclass, 0, 0,
					   &argc, argv, 0, 0, 0);
  Display *dpy = XtDisplay (toplevel_shell);
  int fd;
  XtInputId id;
  XtGetApplicationNameAndClass (dpy, &progname, &progclass);

  fd = apm_open ();
  if (fd <= 0)
    {
      fprintf (stderr, "%s: couldn't initialize APM.\n", blurb());
      exit (1);
    }

  id = XtAppAddInput(app, fd,
                     (XtPointer) (XtInputReadMask | XtInputWriteMask),
                     apm_cb, 0);
  XtAppMainLoop (app);
  exit (0);
}