9 #include <X11/keysym.h>
10 #include <X11/extensions/XShm.h>
13 enum { QUIT = 1, REDRAW = 2 };
15 static Window create_win(int width, int height, int bpp);
16 static void handle_event(XEvent *ev);
17 static int translate_keysym(KeySym sym);
18 static int parse_args(int argc, char **argv);
20 static int win_width, win_height;
22 static unsigned int pending;
24 static Window win, root;
27 static Atom xa_wm_proto, xa_wm_delwin;
30 static XShmSegmentInfo shm;
31 static int wait_putimg;
32 static int xshm_ev_completion;
34 int main(int argc, char **argv)
38 struct timeval tv, tv0;
41 shm.shmaddr = (void*)-1;
43 read_config("rbench.cfg");
45 if(parse_args(argc, argv) == -1) {
49 if(!(dpy = XOpenDisplay(0))) {
50 fprintf(stderr, "failed to connect to the X server\n");
53 root = DefaultRootWindow(dpy);
54 xa_wm_proto = XInternAtom(dpy, "WM_PROTOCOLS", 0);
55 xa_wm_delwin = XInternAtom(dpy, "WM_DELETE_WINDOW", 0);
57 if(!XShmQueryExtension(dpy)) {
58 fprintf(stderr, "X shared memory extension is not available\n");
62 xshm_ev_completion = XShmGetEventBase(dpy) + ShmCompletion;
64 if(!(win = create_win(opt.width, opt.height, opt.bpp))) {
67 gc = XCreateGC(dpy, win, 0, 0);
69 if(!(ximg = XShmCreateImage(dpy, vis, opt.bpp, ZPixmap, 0, &shm, opt.width, opt.height))) {
70 fprintf(stderr, "failed to create shared memory image\n");
73 if((shm.shmid = shmget(IPC_PRIVATE, ximg->bytes_per_line * ximg->height, IPC_CREAT | 0777)) == -1) {
74 fprintf(stderr, "failed to create shared memory block\n");
77 if((shm.shmaddr = ximg->data = shmat(shm.shmid, 0, 0)) == (void*)-1) {
78 fprintf(stderr, "failed to attach shared memory block\n");
82 if(!XShmAttach(dpy, &shm)) {
83 fprintf(stderr, "XShmAttach failed");
88 fb_height = opt.height;
89 fb_bpp = opt.bpp >= 24 ? 32 : opt.bpp;
90 framebuf = ximg->data;
91 fb_pitch = ximg->bytes_per_line;
97 gettimeofday(&tv0, 0);
101 if(mapped) {/* && !wait_putimg) { */
102 while(XPending(dpy)) {
103 XNextEvent(dpy, &ev);
105 if(pending & QUIT) goto end;
108 gettimeofday(&tv, 0);
109 time_msec = (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
114 XShmPutImage(dpy, win, gc, ximg, 0, 0, 0, 0, ximg->width, ximg->height, False);
117 XNextEvent(dpy, &ev);
119 if(pending & QUIT) goto end;
126 XShmDetach(dpy, &shm);
128 if(shm.shmaddr != (void*)-1) {
131 if(shm.shmid != -1) {
132 shmctl(shm.shmid, IPC_RMID, 0);
137 XDestroyWindow(dpy, win);
142 printf("avg framerate: %.1f fps\n", (10000 * num_frames / time_msec) / 10.0f);
147 static Window create_win(int width, int height, int bpp)
151 XVisualInfo *vinf, vtmpl;
152 unsigned int vinf_mask;
153 XSetWindowAttributes xattr;
154 XTextProperty txname;
156 const char *name = "retrobench X11";
158 scr = DefaultScreen(dpy);
162 vtmpl.class = bpp <= 8 ? PseudoColor : TrueColor;
163 vinf_mask = VisualScreenMask | VisualDepthMask | VisualClassMask;
164 if(!(vinf = XGetVisualInfo(dpy, vinf_mask, &vtmpl, &num_vis))) {
165 fprintf(stderr, "failed to find appropriate visual for %d bpp\n", bpp);
170 if(!(cmap = XCreateColormap(dpy, root, vis, bpp <= 8 ? AllocAll : AllocNone))) {
171 fprintf(stderr, "failed to allocate colormap\n");
175 xattr.background_pixel = BlackPixel(dpy, scr);
176 xattr.colormap = cmap;
177 win = XCreateWindow(dpy, root, 0, 0, width, height, 0, vinf->depth,
178 InputOutput, vis, CWColormap | CWBackPixel, &xattr);
181 XSelectInput(dpy, win, StructureNotifyMask | ExposureMask | KeyPressMask |
184 XStringListToTextProperty((char**)&name, 1, &txname);
185 XSetWMName(dpy, win, &txname);
186 XSetWMIconName(dpy, win, &txname);
189 XSetWMProtocols(dpy, win, &xa_wm_delwin, 1);
191 XMapWindow(dpy, win);
195 static void handle_event(XEvent *ev)
213 case ConfigureNotify:
214 if(ev->xconfigure.width != win_width || ev->xconfigure.height != win_height) {
215 win_width = ev->xconfigure.width;
216 win_height = ev->xconfigure.height;
223 if((sym = XKeycodeToKeysym(dpy, ev->xkey.keycode, 0))) {
224 if(sym == XK_Escape) {
228 if((key = translate_keysym(sym))) {
229 key_event(key, ev->xkey.type == KeyPress);
235 if(ev->xclient.message_type == xa_wm_proto) {
236 if(ev->xclient.data.l[0] == xa_wm_delwin) {
243 if(ev->type == xshm_ev_completion) {
250 static int translate_keysym(KeySym sym)
252 if(sym < 128) return sym;
264 static const char *usage_str =
265 "Usage: %s [options]\n"
267 " -s <WxH>: resolution\n"
268 " -b <bpp>: color depth\n"
269 " -h: print usage and exit\n";
271 static int parse_args(int argc, char **argv)
275 for(i=1; i<argc; i++) {
276 if(argv[i][0] == '-') {
282 if(!argv[++i] || sscanf(argv[i], "%dx%d", &opt.width, &opt.height) != 2) {
283 fprintf(stderr, "-s must be followed by WxH\n");
289 if(!argv[++i] || !(opt.bpp = atoi(argv[i]))) {
290 fprintf(stderr, "-b must be followed by the color depth\n");
296 printf(usage_str, argv[0]);
303 inval_arg: fprintf(stderr, "invalid argument: %s\n", argv[i]);