http://x.cybermirror.org/R5contrib/xscreensaver-1.21.tar.Z
[xscreensaver] / utils / usleep.c
1 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@lucid.com>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 #if __STDC__
13 #include <stdlib.h>
14 #endif
15
16 #include <X11/Xlib.h>
17 #include <X11/Xos.h>    /* lazy way out */
18
19 /* usleep() doesn't exist everywhere, and select() is faster anyway.
20  */
21
22 #ifndef VMS
23
24 #ifdef NO_SELECT
25   /* If you don't have select() or usleep(), I guess you lose...
26      Maybe you have napms() instead?  Let me know.
27    */
28 void
29 screenhack_usleep (usecs)
30      unsigned long usecs;
31 {
32   usleep (usecs);
33 }
34
35 #else /* ! NO_SELECT */
36
37 void
38 screenhack_usleep (usecs)
39      unsigned long usecs;
40 {
41   struct timeval tv;
42   tv.tv_sec  = usecs / 1000000L;
43   tv.tv_usec = usecs % 1000000L;
44   (void) select (0, 0, 0, 0, &tv);
45 }
46
47 #endif /* ! NO_SELECT */
48
49 #else /* VMS */
50
51 #define SEC_DELTA  "0000 00:00:01.00"
52 #define TICK_DELTA "0000 00:00:00.08"
53 static int bin_sec_delta[2], bin_tick_delta[2], deltas_set = 0;
54
55 static void
56 set_deltas ()
57 {
58   int status;
59   extern int SYS$BINTIM ();
60   $DESCRIPTOR (str_sec_delta,  SEC_DELTA);
61   $DESCRIPTOR (str_tick_delta, TICK_DELTA);
62   if (!deltas_set)
63     {
64       status = SYS$BINTIM (&str_sec_delta, &bin_sec_delta);
65       if (!(status & 1))
66         {
67           fprintf (stderr, "%s: cannot convert delta time ", progname);
68           fprintf (stderr, SEC_DELTA);
69           fprintf (stderr, "; status code = %d\n", status);
70           exit (status);
71         }
72       status = SYS$BINTIM (&str_tick_delta, &bin_tick_delta);
73       if (!(status & 1))
74         {
75           fprintf (stderr, "%s: cannot convert delta time ", progname);
76           fprintf (stderr, TICK_DELTA);
77           fprintf (stderr, "; status code = %d\n", status);
78           exit (status);
79         }
80       deltas_set = 1;
81     }
82 }
83
84 void
85 screenhack_usleep (usecs)
86      unsigned long usecs;
87 {
88   int status, *bin_delta;
89   extern int SYS$SCHWDK (), SYS$HIBER (); 
90   
91   if (!deltas_set) set_deltas ();
92   bin_delta = (usecs == TICK_INTERVAL) ? &bin_tick_delta : &bin_sec_delta;
93   status = SYS$SCHDWK (0, 0, bin_delta, 0);
94   if ((status & 1)) (void) SYS$HIBER ();
95 }
96
97 #endif /*VMS */