This source file includes following definitions.
- fatal
- usage
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #include <X11/Xlib.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <unistd.h>
26 #include <string.h>
27
28 void
29 fatal (const char *fmt, ...)
30 {
31 va_list ap;
32
33 va_start (ap, fmt);
34 vfprintf (stderr, fmt, ap);
35 fputc ('\n', stderr);
36 va_end (ap);
37 exit (1);
38 }
39
40 void
41 usage (const char *progname)
42 {
43 fprintf (stderr, "Usage %s options\n", progname);
44 fprintf (stderr, "-n NCOLORS allocate NCOLORS colors\n");
45 exit (1);
46 }
47
48 int
49 main (int argc, char **argv)
50 {
51 Display *dpy;
52 int opt, ncolors = 0, i;
53 XColor *allocated;
54 int nallocated;
55 XColor color;
56 Colormap cmap;
57
58 while ((opt = getopt (argc, argv, "n:")) != EOF)
59 switch (opt)
60 {
61 case 'n':
62 ncolors = atoi (optarg);
63 break;
64
65 case '?':
66 usage (argv[0]);
67 }
68
69 if (ncolors == 0)
70 usage (argv[0]);
71
72 dpy = XOpenDisplay ("");
73 if (dpy == NULL)
74 fatal ("Cannot open display");
75 cmap = DefaultColormap (dpy, 0);
76
77 allocated = malloc (ncolors * sizeof *allocated);
78 nallocated = 0;
79 memset (&color, 0, sizeof color);
80
81 while (nallocated < ncolors
82 && color.red < 65536)
83 {
84 allocated[nallocated] = color;
85 if (XAllocColor (dpy, cmap, &allocated[nallocated]))
86 {
87 for (i = 0; i < nallocated; ++i)
88 if (allocated[i].red == allocated[nallocated].red
89 && allocated[i].green == allocated[nallocated].green
90 && allocated[i].blue == allocated[nallocated].blue)
91 break;
92
93 if (i == nallocated)
94 {
95 printf ("allocated %d/%d/%d\n",
96 allocated[nallocated].red,
97 allocated[nallocated].green,
98 allocated[nallocated].blue);
99 ++nallocated;
100 }
101 }
102
103 ++color.red;
104 ++color.green;
105 ++color.blue;
106 }
107
108 fprintf (stderr, "Waiting. Press ^C to stop.\n");
109 while (1)
110 sleep (10);
111
112 XCloseDisplay (dpy);
113 return 0;
114 }