Ticket #10: pc.c

File pc.c, 4.1 KB (added by jeremyhu@…, 3 years ago)

pc.c

Line 
1/* gcc -DEBUG pc.c -o pc -L/usr/X11/lib -lX11 */
2/* gcc pc.c -o pc -L/usr/X11/lib -lX11 */
3
4/*
5 * Cobbled together from:
6 *   http://bellet.info/XVideo/testxv.c
7 *   http://users.actcom.co.il/~choo/lupg/tutorials/xlib-programming/xlib-programming.html
8 */
9
10#include <errno.h>
11#include <stdlib.h>
12#include <stdio.h>
13#include <string.h>
14#include <X11/Xlib.h>
15#include <X11/Xutil.h>
16
17/* trick to verbosely do an expression */
18#ifdef  EBUG
19#define debug(expr, fmt, args...) \
20        ((void)fprintf(stderr, "%s = " fmt "\n", # expr, expr, ## args))
21#else
22#define debug(expr, fmt, args...)       (void)(expr)
23#endif
24
25/* Used to fill in the name class array */
26#define NC(c)   { # c, c }
27
28/* Used to match class name strings to the constants used by XLib */
29static struct {
30        char    *name;
31        int     class;
32} nc[] = {
33        NC(PseudoColor),
34        NC(GrayScale),
35        NC(DirectColor),
36        NC(TrueColor),
37        NC(StaticColor),
38        NC(StaticGray)
39};
40
41/* Number of elements in an array */
42#define NELEM(a)        (sizeof (a)/sizeof ((a)[0]))
43
44/* gross way of marking the falied lookup, but whatever */
45static int
46nclkup(char *name)
47{
48        int i;
49
50        for (i = 0; i < NELEM(nc); i++) {
51                debug(i, "%d:");
52                debug(nc[i].name, "%s");
53                debug(nc[i].class, "%d");
54
55                if (strcmp(nc[i].name, name) != 0)
56                        continue;
57
58                return (nc[i].class);
59        }
60
61        /* Mark that the lookup failed */
62        i = name[0];
63        name[0] = 0;
64
65        return (i);
66}
67
68int
69main(int argc, char *argv[])
70{
71        int width = 320;
72        int height = 200;
73        Display* dpy;
74        XSetWindowAttributes xswa;
75        XVisualInfo xvi;
76        Window window;
77        XGCValues vals;
78        GC gc;
79        XEvent event;
80        unsigned long mask;
81        int res, screen, class, depth;
82
83        if (argc != 3) {
84                (void)fprintf(stderr, "Usage %s class depth\n", argv[0]);
85                (void)fprintf(stderr, "  examples:\n");
86                (void)fprintf(stderr, "    %s PseudoColor 8\n", argv[0]);
87                (void)fprintf(stderr, "    %s TrueColor 24\n", argv[0]);
88                return (2);
89        }
90
91        class = nclkup(argv[1]);
92        if (argv[1][0] == 0) {
93                argv[1][0] = class;
94                (void)fprintf(stderr, "%s: bad class: %s\n", argv[0], argv[1]);
95                return (1);
96        }
97
98        errno = 0;
99        debug(depth = strtoul(argv[2], NULL, 10), "%d, argv[2] = %s", argv[2]);
100        if (errno != 0) {
101                (void)fprintf(stderr, "%s: bad depth argument: %s\n",
102                    argv[0], strerror(errno));
103                return (1);
104        }
105
106        debug(dpy = XOpenDisplay(NULL),
107              "0x%x");
108        if (dpy == NULL) {
109                (void)fprintf(stderr, "Cannot connect to X server\n");
110                return (1);
111        }
112
113        debug(screen = DefaultScreen(dpy), "%d");
114        if (screen < 0) {
115                fprintf(stderr, "%s: screen not found\n", argv[0]);
116                return (1);
117        }
118
119        debug(res = XMatchVisualInfo(dpy, screen, depth, class, &xvi), "%d");
120        if (res == 0) {
121                fprintf(stderr, "%s: visual not found\n", argv[0]);
122                return (1);
123        }
124
125        debug(xswa.colormap = XCreateColormap(dpy, DefaultRootWindow(dpy),
126            xvi.visual, AllocNone), "0x%x");
127        xswa.event_mask = StructureNotifyMask | ExposureMask;
128        xswa.background_pixel = 0;
129        xswa.border_pixel = 0;
130
131        mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
132
133        debug(window = XCreateWindow(dpy, DefaultRootWindow(dpy),
134            0, 0,
135            width,
136            height,
137            0, xvi.depth,
138            InputOutput,
139            xvi.visual,
140            mask, &xswa), "0x%x");
141
142        debug(XMapWindow(dpy, window), "%d");
143
144        do {
145                XNextEvent(dpy, &event);
146        }
147        while (event.type != MapNotify || event.xmap.event != window);
148
149        debug(gc = XCreateGC(dpy, window, 0, &vals), "0x%x");
150
151        debug(XSetForeground(dpy, gc, BlackPixel(dpy, screen)), "%d");
152        debug(XSetBackground(dpy, gc, WhitePixel(dpy, screen)), "%d");
153
154        debug(XSetLineAttributes(dpy, gc, 2, LineSolid, CapButt, JoinBevel),
155            "%d");
156        debug(XSetFillStyle(dpy, gc, FillSolid), "%d");
157
158        debug(XSync(dpy, False), "%d");
159
160        debug(XDrawLine(dpy, window, gc, width / 2, 0, width / 2, height),
161            "%d");
162
163        debug(XSync(dpy, False), "%d");
164
165        debug(XSetForeground(dpy, gc, WhitePixel(dpy, screen)), "%d");
166        debug(XSetBackground(dpy, gc, BlackPixel(dpy, screen)), "%d");
167
168        debug(XSetLineAttributes(dpy, gc, 2, LineSolid, CapButt, JoinBevel),
169            "%d");
170        debug(XSetFillStyle(dpy, gc, FillSolid), "%d");
171
172        debug(XSync(dpy, False), "%d");
173
174        debug(XDrawLine(dpy, window, gc, 0, height / 2, width, height / 2),
175            "%d");
176
177        debug(XFlush(dpy), "%d");
178
179        sleep(10);
180
181        XCloseDisplay(dpy);
182
183        return (0);
184}