made override_redirect an envvar option (RBENCH_NO_WM)
[retrobench] / src / x11 / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <signal.h>
6 #include <sys/time.h>
7 #include <sys/ipc.h>
8 #include <sys/shm.h>
9 #include <X11/Xlib.h>
10 #include <X11/Xutil.h>
11 #include <X11/keysym.h>
12 #include <X11/extensions/XShm.h>
13 #include "rbench.h"
14 #include "util.h"
15
16 enum { QUIT = 1, REDRAW = 2 };
17
18 static Window create_win(int width, int height, int bpp);
19 static void handle_event(XEvent *ev);
20 static int translate_keysym(KeySym sym);
21 static int parse_args(int argc, char **argv);
22 static void sig(int s);
23
24 static int win_width, win_height;
25 static int mapped;
26 static unsigned int pending;
27 static Display *dpy;
28 static Window win, root;
29 static GC gc;
30 static Visual *vis;
31 static Atom xa_wm_proto, xa_wm_delwin;
32 static int no_wm;
33
34 static XImage *ximg;
35 static XShmSegmentInfo shm;
36 static int wait_putimg;
37 static int xshm_ev_completion;
38
39
40 int main(int argc, char **argv)
41 {
42         int num_frames = 0;
43         XEvent ev;
44         struct timeval tv, tv0;
45         char *env;
46
47         if((env = getenv("RBENCH_NO_WM"))) {
48                 if(isdigit(env[0])) {
49                         no_wm = atoi(env);
50                 } else {
51                         no_wm = 1;
52                 }
53         }
54
55         shm.shmid = -1;
56         shm.shmaddr = (void*)-1;
57
58         signal(SIGINT, sig);
59
60         read_config("rbench.cfg");
61
62         if(parse_args(argc, argv) == -1) {
63                 return 1;
64         }
65
66         if(!(dpy = XOpenDisplay(0))) {
67                 fprintf(stderr, "failed to connect to the X server\n");
68                 return 1;
69         }
70         root = DefaultRootWindow(dpy);
71         xa_wm_proto = XInternAtom(dpy, "WM_PROTOCOLS", 0);
72         xa_wm_delwin = XInternAtom(dpy, "WM_DELETE_WINDOW", 0);
73
74         if(!XShmQueryExtension(dpy)) {
75                 fprintf(stderr, "X shared memory extension is not available\n");
76                 XCloseDisplay(dpy);
77                 return 1;
78         }
79         xshm_ev_completion = XShmGetEventBase(dpy) + ShmCompletion;
80
81         if(!(win = create_win(opt.width, opt.height, opt.bpp))) {
82                 return 1;
83         }
84         gc = XCreateGC(dpy, win, 0, 0);
85
86         if(!(ximg = XShmCreateImage(dpy, vis, opt.bpp, ZPixmap, 0, &shm, opt.width, opt.height))) {
87                 fprintf(stderr, "failed to create shared memory image\n");
88                 goto end;
89         }
90         if((shm.shmid = shmget(IPC_PRIVATE, ximg->bytes_per_line * ximg->height, IPC_CREAT | 0777)) == -1) {
91                 fprintf(stderr, "failed to create shared memory block\n");
92                 goto end;
93         }
94         if((shm.shmaddr = ximg->data = shmat(shm.shmid, 0, 0)) == (void*)-1) {
95                 fprintf(stderr, "failed to attach shared memory block\n");
96                 goto end;
97         }
98         shm.readOnly = True;
99         if(!XShmAttach(dpy, &shm)) {
100                 fprintf(stderr, "XShmAttach failed");
101                 goto end;
102         }
103
104         fb_width = opt.width;
105         fb_height = opt.height;
106         if(opt.bpp >= 24) {
107                 fb_bpp = ximg->bytes_per_line < fb_width * 4 ? 24 : 32;
108         } else {
109                 fb_bpp = opt.bpp;
110         }
111         framebuf = ximg->data;
112         fb_pitch = ximg->bytes_per_line;
113         fb_rmask = ximg->red_mask;
114         fb_gmask = ximg->green_mask;
115         fb_bmask = ximg->blue_mask;
116         fb_rshift = mask_to_shift(fb_rmask);
117         fb_gshift = mask_to_shift(fb_gmask);
118         fb_bshift = mask_to_shift(fb_bmask);
119
120         if(init() == -1) {
121                 goto end;
122         }
123
124         gettimeofday(&tv0, 0);
125
126         while(!(pending & QUIT)) {
127                 if(mapped) {/* && !wait_putimg) { */
128                         while(XPending(dpy)) {
129                                 XNextEvent(dpy, &ev);
130                                 handle_event(&ev);
131                                 if(pending & QUIT) goto end;
132                         }
133
134                         if(!wait_putimg) {
135                                 gettimeofday(&tv, 0);
136                                 time_msec = (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
137                                 num_frames++;
138
139                                 redraw();
140
141                                 XShmPutImage(dpy, win, gc, ximg, 0, 0, 0, 0, ximg->width, ximg->height, True);
142                                 wait_putimg = 1;
143                         }
144                 } else {
145                         XNextEvent(dpy, &ev);
146                         handle_event(&ev);
147                         if(pending & QUIT) goto end;
148                 }
149         }
150
151 end:
152         cleanup();
153         if(ximg) {
154                 XShmDetach(dpy, &shm);
155                 XDestroyImage(ximg);
156                 if(shm.shmaddr != (void*)-1) {
157                         shmdt(shm.shmaddr);
158                 }
159                 if(shm.shmid != -1) {
160                         shmctl(shm.shmid, IPC_RMID, 0);
161                 }
162         }
163         if(win) {
164                 XFreeGC(dpy, gc);
165                 XDestroyWindow(dpy, win);
166         }
167         XCloseDisplay(dpy);
168
169         if(num_frames) {
170                 printf("avg framerate: %.1f fps\n", (10000 * num_frames / time_msec) / 10.0f);
171         }
172         return 0;
173 }
174
175 static Window create_win(int width, int height, int bpp)
176 {
177         int scr, num_vis;
178         Window win;
179         XVisualInfo *vinf, vtmpl;
180         unsigned int vinf_mask;
181         XSetWindowAttributes xattr;
182         XTextProperty txname;
183         Colormap cmap;
184         const char *name = "retrobench X11";
185
186         scr = DefaultScreen(dpy);
187
188         vtmpl.screen = scr;
189         vtmpl.depth = bpp;
190         vtmpl.class = bpp <= 8 ? PseudoColor : TrueColor;
191         vinf_mask = VisualScreenMask | VisualDepthMask | VisualClassMask;
192         if(!(vinf = XGetVisualInfo(dpy, vinf_mask, &vtmpl, &num_vis))) {
193                 fprintf(stderr, "failed to find appropriate visual for %d bpp\n", bpp);
194                 return 0;
195         }
196         vis = vinf->visual;
197
198         if(!(cmap = XCreateColormap(dpy, root, vis, bpp <= 8 ? AllocAll : AllocNone))) {
199                 fprintf(stderr, "failed to allocate colormap\n");
200                 return 0;
201         }
202
203         xattr.background_pixel = BlackPixel(dpy, scr);
204         xattr.colormap = cmap;
205         xattr.override_redirect = no_wm ? True : False;
206         win = XCreateWindow(dpy, root, 0, 0, width, height, 0, vinf->depth,
207                         InputOutput, vis, CWColormap | CWBackPixel | CWOverrideRedirect, &xattr);
208         if(!win) return 0;
209
210         XSelectInput(dpy, win, StructureNotifyMask | ExposureMask | KeyPressMask |
211                         KeyReleaseMask);
212
213         XStringListToTextProperty((char**)&name, 1, &txname);
214         XSetWMName(dpy, win, &txname);
215         XSetWMIconName(dpy, win, &txname);
216         XFree(txname.value);
217
218         XSetWMProtocols(dpy, win, &xa_wm_delwin, 1);
219
220         XMapWindow(dpy, win);
221         return win;
222 }
223
224 static void handle_event(XEvent *ev)
225 {
226         int key;
227         KeySym sym;
228
229         switch(ev->type) {
230         case MapNotify:
231                 mapped = 1;
232                 break;
233
234         case UnmapNotify:
235                 mapped = 0;
236                 break;
237
238         case Expose:
239                 pending |= REDRAW;
240                 break;
241
242         case ConfigureNotify:
243                 if(ev->xconfigure.width != win_width || ev->xconfigure.height != win_height) {
244                         win_width = ev->xconfigure.width;
245                         win_height = ev->xconfigure.height;
246                         /* TODO */
247                 }
248                 break;
249
250         case KeyPress:
251         case KeyRelease:
252                 if((sym = XKeycodeToKeysym(dpy, ev->xkey.keycode, 0))) {
253                         if(sym == XK_Escape) {
254                                 pending |= QUIT;
255                                 break;
256                         }
257                         if((key = translate_keysym(sym))) {
258                                 key_event(key, ev->xkey.type == KeyPress);
259                         }
260                 }
261                 break;
262
263         case ClientMessage:
264                 if(ev->xclient.message_type == xa_wm_proto) {
265                         if(ev->xclient.data.l[0] == xa_wm_delwin) {
266                                 pending |= QUIT;
267                         }
268                 }
269                 break;
270
271         default:
272                 if(ev->type == xshm_ev_completion) {
273                         wait_putimg = 0;
274                 }
275                 break;
276         }
277 }
278
279 static int translate_keysym(KeySym sym)
280 {
281         if(sym < 128) return sym;
282
283         switch(sym) {
284         case XK_Escape:
285                 return 27;
286
287         default:
288                 break;
289         }
290         return 0;
291 }
292
293 static const char *usage_str =
294         "Usage: %s [options]\n"
295         "Options:\n"
296         "  -s <WxH>: resolution\n"
297         "  -b <bpp>: color depth\n"
298         "  -h: print usage and exit\n";
299
300 static int parse_args(int argc, char **argv)
301 {
302         int i;
303
304         for(i=1; i<argc; i++) {
305                 if(argv[i][0] == '-') {
306                         if(argv[i][2]) {
307                                 goto inval_arg;
308                         }
309                         switch(argv[i][1]) {
310                         case 's':
311                                 if(!argv[++i] || sscanf(argv[i], "%dx%d", &opt.width, &opt.height) != 2) {
312                                         fprintf(stderr, "-s must be followed by WxH\n");
313                                         return -1;
314                                 }
315                                 break;
316
317                         case 'b':
318                                 if(!argv[++i] || !(opt.bpp = atoi(argv[i]))) {
319                                         fprintf(stderr, "-b must be followed by the color depth\n");
320                                         return -1;
321                                 }
322                                 break;
323
324                         case 'h':
325                                 printf(usage_str, argv[0]);
326                                 exit(0);
327
328                         default:
329                                 goto inval_arg;
330                         }
331                 } else {
332 inval_arg:      fprintf(stderr, "invalid argument: %s\n", argv[i]);
333                         return -1;
334                 }
335         }
336         return 0;
337 }
338
339 static void sig(int s)
340 {
341         pending |= QUIT;
342 }