10 #include <X11/keysym.h>
11 #include <X11/extensions/XShm.h>
15 enum { QUIT = 1, REDRAW = 2 };
17 static Window create_win(int width, int height, int bpp);
18 static void handle_event(XEvent *ev);
19 static int translate_keysym(KeySym sym);
20 static int parse_args(int argc, char **argv);
21 static void sig(int s);
23 static int win_width, win_height;
25 static unsigned int pending;
27 static Window win, root;
30 static Atom xa_wm_proto, xa_wm_delwin;
33 static XShmSegmentInfo shm;
34 static int wait_putimg;
35 static int xshm_ev_completion;
37 int main(int argc, char **argv)
41 struct timeval tv, tv0;
44 shm.shmaddr = (void*)-1;
48 read_config("rbench.cfg");
50 if(parse_args(argc, argv) == -1) {
54 if(!(dpy = XOpenDisplay(0))) {
55 fprintf(stderr, "failed to connect to the X server\n");
58 root = DefaultRootWindow(dpy);
59 xa_wm_proto = XInternAtom(dpy, "WM_PROTOCOLS", 0);
60 xa_wm_delwin = XInternAtom(dpy, "WM_DELETE_WINDOW", 0);
62 if(!XShmQueryExtension(dpy)) {
63 fprintf(stderr, "X shared memory extension is not available\n");
67 xshm_ev_completion = XShmGetEventBase(dpy) + ShmCompletion;
69 if(!(win = create_win(opt.width, opt.height, opt.bpp))) {
72 gc = XCreateGC(dpy, win, 0, 0);
74 if(!(ximg = XShmCreateImage(dpy, vis, opt.bpp, ZPixmap, 0, &shm, opt.width, opt.height))) {
75 fprintf(stderr, "failed to create shared memory image\n");
78 if((shm.shmid = shmget(IPC_PRIVATE, ximg->bytes_per_line * ximg->height, IPC_CREAT | 0777)) == -1) {
79 fprintf(stderr, "failed to create shared memory block\n");
82 if((shm.shmaddr = ximg->data = shmat(shm.shmid, 0, 0)) == (void*)-1) {
83 fprintf(stderr, "failed to attach shared memory block\n");
87 if(!XShmAttach(dpy, &shm)) {
88 fprintf(stderr, "XShmAttach failed");
93 fb_height = opt.height;
95 fb_bpp = ximg->bytes_per_line < fb_width * 4 ? 24 : 32;
99 framebuf = ximg->data;
100 fb_pitch = ximg->bytes_per_line;
101 fb_rmask = ximg->red_mask;
102 fb_gmask = ximg->green_mask;
103 fb_bmask = ximg->blue_mask;
104 fb_rshift = mask_to_shift(fb_rmask);
105 fb_gshift = mask_to_shift(fb_gmask);
106 fb_bshift = mask_to_shift(fb_bmask);
112 gettimeofday(&tv0, 0);
114 while(!(pending & QUIT)) {
115 if(mapped) {/* && !wait_putimg) { */
116 while(XPending(dpy)) {
117 XNextEvent(dpy, &ev);
119 if(pending & QUIT) goto end;
123 gettimeofday(&tv, 0);
124 time_msec = (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
129 XShmPutImage(dpy, win, gc, ximg, 0, 0, 0, 0, ximg->width, ximg->height, True);
133 XNextEvent(dpy, &ev);
135 if(pending & QUIT) goto end;
142 XShmDetach(dpy, &shm);
144 if(shm.shmaddr != (void*)-1) {
147 if(shm.shmid != -1) {
148 shmctl(shm.shmid, IPC_RMID, 0);
153 XDestroyWindow(dpy, win);
158 printf("avg framerate: %.1f fps\n", (10000 * num_frames / time_msec) / 10.0f);
163 static Window create_win(int width, int height, int bpp)
167 XVisualInfo *vinf, vtmpl;
168 unsigned int vinf_mask;
169 XSetWindowAttributes xattr;
170 XTextProperty txname;
172 const char *name = "retrobench X11";
174 scr = DefaultScreen(dpy);
178 vtmpl.class = bpp <= 8 ? PseudoColor : TrueColor;
179 vinf_mask = VisualScreenMask | VisualDepthMask | VisualClassMask;
180 if(!(vinf = XGetVisualInfo(dpy, vinf_mask, &vtmpl, &num_vis))) {
181 fprintf(stderr, "failed to find appropriate visual for %d bpp\n", bpp);
186 if(!(cmap = XCreateColormap(dpy, root, vis, bpp <= 8 ? AllocAll : AllocNone))) {
187 fprintf(stderr, "failed to allocate colormap\n");
191 xattr.background_pixel = BlackPixel(dpy, scr);
192 xattr.colormap = cmap;
193 xattr.override_redirect = True;
194 win = XCreateWindow(dpy, root, 0, 0, width, height, 0, vinf->depth,
195 InputOutput, vis, CWColormap | CWBackPixel | CWOverrideRedirect, &xattr);
198 XSelectInput(dpy, win, StructureNotifyMask | ExposureMask | KeyPressMask |
201 XStringListToTextProperty((char**)&name, 1, &txname);
202 XSetWMName(dpy, win, &txname);
203 XSetWMIconName(dpy, win, &txname);
206 XSetWMProtocols(dpy, win, &xa_wm_delwin, 1);
208 XMapWindow(dpy, win);
212 static void handle_event(XEvent *ev)
230 case ConfigureNotify:
231 if(ev->xconfigure.width != win_width || ev->xconfigure.height != win_height) {
232 win_width = ev->xconfigure.width;
233 win_height = ev->xconfigure.height;
240 if((sym = XKeycodeToKeysym(dpy, ev->xkey.keycode, 0))) {
241 if(sym == XK_Escape) {
245 if((key = translate_keysym(sym))) {
246 key_event(key, ev->xkey.type == KeyPress);
252 if(ev->xclient.message_type == xa_wm_proto) {
253 if(ev->xclient.data.l[0] == xa_wm_delwin) {
260 if(ev->type == xshm_ev_completion) {
267 static int translate_keysym(KeySym sym)
269 if(sym < 128) return sym;
281 static const char *usage_str =
282 "Usage: %s [options]\n"
284 " -s <WxH>: resolution\n"
285 " -b <bpp>: color depth\n"
286 " -h: print usage and exit\n";
288 static int parse_args(int argc, char **argv)
292 for(i=1; i<argc; i++) {
293 if(argv[i][0] == '-') {
299 if(!argv[++i] || sscanf(argv[i], "%dx%d", &opt.width, &opt.height) != 2) {
300 fprintf(stderr, "-s must be followed by WxH\n");
306 if(!argv[++i] || !(opt.bpp = atoi(argv[i]))) {
307 fprintf(stderr, "-b must be followed by the color depth\n");
313 printf(usage_str, argv[0]);
320 inval_arg: fprintf(stderr, "invalid argument: %s\n", argv[i]);
327 static void sig(int s)