eqemu

view src/main.cc @ 6:977bc1cb055b

almost done
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 18 Jul 2014 02:35:06 +0300
parents 3d3656360a82
children e9ab4861536d
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <float.h>
4 #include <assert.h>
5 #include <errno.h>
6 #include <unistd.h>
7 #include <sys/select.h>
8 #include <GL/glew.h>
9 #include <X11/Xlib.h>
10 #include <GL/glx.h>
11 #include "dev.h"
12 #include "scene.h"
14 static bool init();
15 static void cleanup();
16 static void display();
17 static void keyb(int key, bool pressed);
18 static void mouse(int bn, bool pressed, int x, int y);
19 static void motion(int x, int y);
20 static Ray calc_pick_ray(int x, int y);
22 static Window create_window(const char *title, int xsz, int ysz);
23 static void process_events();
24 static int translate_keysym(KeySym sym);
26 static int proc_args(int argc, char **argv);
28 static Display *dpy;
29 static Window win;
30 static GLXContext ctx;
31 static Atom xa_wm_prot, xa_wm_del_win;
33 static int win_width, win_height;
35 static bool draw_pending;
36 static bool win_mapped;
38 static int fakefd = -1;
39 static char *fake_devpath;
41 static float cam_theta, cam_phi, cam_dist = 140;
42 static Scene *scn;
44 enum { BN_TICKET, BN_NEXT, NUM_BUTTONS };
45 static const char *button_names[] = { "button1", "button2" };
46 static Object *button_obj[NUM_BUTTONS];
47 static Object *disp_obj[2];
48 static Object *led_obj[2];
49 static Vector3 led_on_emissive;
51 int main(int argc, char **argv)
52 {
53 if(proc_args(argc, argv) == -1) {
54 return 1;
55 }
56 if(!init()) {
57 return 1;
58 }
59 atexit(cleanup);
61 int xfd = ConnectionNumber(dpy);
63 // run once through pending events before going into the select loop
64 process_events();
66 for(;;) {
67 fd_set rd;
68 FD_ZERO(&rd);
70 FD_SET(xfd, &rd);
71 FD_SET(fakefd, &rd);
73 struct timeval noblock = {0, 0};
74 int maxfd = xfd > fakefd ? xfd : fakefd;
75 while(select(maxfd + 1, &rd, 0, 0, draw_pending ? &noblock : 0) == -1 && errno == EINTR);
77 if(FD_ISSET(xfd, &rd)) {
78 process_events();
79 }
80 if(FD_ISSET(fakefd, &rd)) {
81 proc_dev_input();
82 }
84 if(draw_pending) {
85 display();
86 draw_pending = false;
87 }
88 }
89 return 0;
90 }
92 static bool init()
93 {
94 if(fake_devpath) {
95 if((fakefd = start_dev(fake_devpath)) == -1) {
96 return false;
97 }
98 }
100 if(!(dpy = XOpenDisplay(0))) {
101 fprintf(stderr, "failed to connect to the X server!\n");
102 return false;
103 }
105 if(!(win = create_window("equeue device emulator", 800, 600))) {
106 return false;
107 }
109 glewInit();
111 scn = new Scene;
112 if(!scn->load("data/device.obj")) {
113 fprintf(stderr, "failed to load device 3D model\n");
114 return false;
115 }
117 for(int i=0; i<NUM_BUTTONS; i++) {
118 button_obj[i] = scn->get_object(button_names[i]);
119 if(!button_obj[i]) {
120 fprintf(stderr, "invalid 3D model\n");
121 return false;
122 }
123 BSphere &bs = button_obj[i]->get_mesh()->get_bounds();
124 bs.set_radius(bs.get_radius() * 1.5);
125 }
127 disp_obj[0] = scn->get_object("7seg0");
128 disp_obj[1] = scn->get_object("7seg1");
129 if(!disp_obj[0] || !disp_obj[1]) {
130 fprintf(stderr, "invalid 3D model\n");
131 return false;
132 }
133 scn->remove_object(disp_obj[0]);
134 scn->remove_object(disp_obj[1]);
136 led_obj[0] = scn->get_object("led1");
137 led_obj[1] = scn->get_object("led2");
138 if(!led_obj[0] || !led_obj[1]) {
139 fprintf(stderr, "invalid 3D model\n");
140 return false;
141 }
142 scn->remove_object(led_obj[0]);
143 scn->remove_object(led_obj[1]);
144 led_on_emissive = led_obj[0]->mtl.emissive;
146 glEnable(GL_DEPTH_TEST);
147 glEnable(GL_CULL_FACE);
148 glEnable(GL_LIGHTING);
149 glEnable(GL_LIGHT0);
151 glClearColor(0.1, 0.1, 0.1, 1);
153 return true;
154 }
156 static void cleanup()
157 {
158 delete scn;
160 stop_dev();
162 if(!dpy) return;
164 if(win) {
165 XDestroyWindow(dpy, win);
166 }
167 XCloseDisplay(dpy);
168 }
170 #define DIGIT_USZ (1.0 / 11.0)
172 static void display()
173 {
174 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
176 glMatrixMode(GL_MODELVIEW);
177 glLoadIdentity();
178 glTranslatef(0, 0, -cam_dist);
179 glRotatef(cam_phi, 1, 0, 0);
180 glRotatef(cam_theta, 0, 1, 0);
182 float lpos[] = {-7, 5, 10, 0};
183 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
185 scn->render();
187 // shift the textures and modify the materials to make the display match our state
188 for(int i=0; i<2; i++) {
189 int digit = get_display_number();
190 for(int j=0; j<i; j++) {
191 digit /= 10;
192 }
193 digit %= 10;
195 float uoffs = DIGIT_USZ + DIGIT_USZ * digit;
197 disp_obj[i]->mtl.tex_offset[TEX_DIFFUSE] = Vector2(uoffs, 0);
198 disp_obj[i]->render();
200 // LEDs
201 if(get_led_state(i)) {
202 led_obj[i]->mtl.emissive = led_on_emissive;
203 } else {
204 led_obj[i]->mtl.emissive = Vector3(0, 0, 0);
205 }
206 led_obj[i]->render();
207 }
209 glXSwapBuffers(dpy, win);
210 assert(glGetError() == GL_NO_ERROR);
211 }
213 static void reshape(int x, int y)
214 {
215 glViewport(0, 0, x, y);
217 glMatrixMode(GL_PROJECTION);
218 glLoadIdentity();
219 gluPerspective(50.0, (float)x / (float)y, 1.0, 1000.0);
221 win_width = x;
222 win_height = y;
223 }
225 static void keyb(int key, bool pressed)
226 {
227 if(pressed) {
228 switch(key) {
229 case 27:
230 exit(0);
231 }
232 }
233 }
235 static bool bnstate[32];
236 static int prev_x, prev_y;
238 static void mouse(int bn, bool pressed, int x, int y)
239 {
240 bnstate[bn] = pressed;
241 prev_x = x;
242 prev_y = y;
244 if(bn == 0 && pressed) {
245 // do picking
246 Ray ray = calc_pick_ray(x, win_height - y);
248 HitPoint minhit;
249 minhit.t = FLT_MAX;
250 int hit_found = -1;
252 for(int i=0; i<NUM_BUTTONS; i++) {
253 HitPoint hit;
254 if(button_obj[i]->get_mesh()->get_bounds().intersect(ray, &hit) && hit.t < minhit.t) {
255 minhit = hit;
256 hit_found = i;
257 }
258 }
260 if(hit_found != -1) {
261 switch(hit_found) {
262 case BN_TICKET:
263 issue_ticket();
264 printf("issue ticket\n");
265 break;
267 case BN_NEXT:
268 next_customer();
269 printf("next customer\n");
270 break;
271 }
272 draw_pending = true;
273 }
274 }
275 }
277 static void motion(int x, int y)
278 {
279 int dx = x - prev_x;
280 int dy = y - prev_y;
281 prev_x = x;
282 prev_y = y;
284 if(bnstate[0]) {
285 cam_theta += dx * 0.5;
286 cam_phi += dy * 0.5;
287 if(cam_phi < -90) cam_phi = -90;
288 if(cam_phi > 90) cam_phi = 90;
290 } else if(bnstate[2]) {
291 cam_dist += dy * 0.5;
292 if(cam_dist < 0.0) cam_dist = 0.0;
294 } else {
295 float xoffs = 2.0 * x / win_width - 1.0;
296 float yoffs = 2.0 * y / win_height - 1.0;
297 cam_theta = -xoffs * 15.0 * (win_width / win_height);
298 cam_phi = -yoffs * 15.0;
299 }
300 draw_pending = true;
301 }
303 static Ray calc_pick_ray(int x, int y)
304 {
305 double mv[16], proj[16];
306 int vp[4];
307 double resx, resy, resz;
308 Ray ray;
310 glGetDoublev(GL_MODELVIEW_MATRIX, mv);
311 glGetDoublev(GL_PROJECTION_MATRIX, proj);
312 glGetIntegerv(GL_VIEWPORT, vp);
314 gluUnProject(x, y, 0, mv, proj, vp, &resx, &resy, &resz);
315 ray.origin = Vector3(resx, resy, resz);
317 gluUnProject(x, y, 1, mv, proj, vp, &resx, &resy, &resz);
318 ray.dir = normalize(Vector3(resx, resy, resz) - ray.origin);
320 return ray;
321 }
323 static Window create_window(const char *title, int xsz, int ysz)
324 {
325 int scr = DefaultScreen(dpy);
326 Window root = RootWindow(dpy, scr);
328 int glxattr[] = {
329 GLX_RGBA,
330 GLX_RED_SIZE, 8,
331 GLX_GREEN_SIZE, 8,
332 GLX_BLUE_SIZE, 8,
333 GLX_DEPTH_SIZE, 24,
334 GLX_DOUBLEBUFFER,
335 #if defined(GLX_VERSION_1_4) || defined(GLX_ARB_multisample)
336 GLX_SAMPLE_BUFFERS_ARB, 1,
337 GLX_SAMPLES_ARB, 1,
338 #endif
339 None
340 };
342 XVisualInfo *vis = glXChooseVisual(dpy, scr, glxattr);
343 if(!vis) {
344 fprintf(stderr, "failed to find a suitable visual\n");
345 return 0;
346 }
348 if(!(ctx = glXCreateContext(dpy, vis, 0, True))) {
349 fprintf(stderr, "failed to create OpenGL context\n");
350 XFree(vis);
351 return -1;
352 }
354 XSetWindowAttributes xattr;
355 xattr.background_pixel = xattr.border_pixel = BlackPixel(dpy, scr);
356 xattr.colormap = XCreateColormap(dpy, root, vis->visual, AllocNone);
357 unsigned int xattr_mask = CWColormap | CWBackPixel | CWBorderPixel;
359 Window win = XCreateWindow(dpy, root, 0, 0, xsz, ysz, 0, vis->depth, InputOutput,
360 vis->visual, xattr_mask, &xattr);
361 if(!win) {
362 fprintf(stderr, "failed to create window\n");
363 glXDestroyContext(dpy, ctx);
364 XFree(vis);
365 return -1;
366 }
367 XFree(vis);
369 unsigned int evmask = StructureNotifyMask | VisibilityChangeMask | ExposureMask |
370 KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
371 PointerMotionMask;
372 XSelectInput(dpy, win, evmask);
374 xa_wm_prot = XInternAtom(dpy, "WM_PROTOCOLS", False);
375 xa_wm_del_win = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
376 XSetWMProtocols(dpy, win, &xa_wm_del_win, 1);
378 XClassHint hint;
379 hint.res_name = hint.res_class = (char*)"equeue_win";
380 XSetClassHint(dpy, win, &hint);
382 XTextProperty wm_name;
383 XStringListToTextProperty((char**)&title, 1, &wm_name);
384 XSetWMName(dpy, win, &wm_name);
385 XSetWMIconName(dpy, win, &wm_name);
386 XFree(wm_name.value);
388 XMapWindow(dpy, win);
389 glXMakeCurrent(dpy, win, ctx);
391 return win;
392 }
394 static void process_events()
395 {
396 XEvent ev;
398 while(XPending(dpy)) {
399 XNextEvent(dpy, &ev);
400 switch(ev.type) {
401 case MapNotify:
402 win_mapped = true;
403 break;
405 case UnmapNotify:
406 win_mapped = false;
407 break;
409 case Expose:
410 if(win_mapped && ev.xexpose.count == 0) {
411 draw_pending = true;
412 }
413 break;
415 case MotionNotify:
416 motion(ev.xmotion.x, ev.xmotion.y);
417 break;
419 case ButtonPress:
420 mouse(ev.xbutton.button - 1, true, ev.xbutton.x, ev.xbutton.y);
421 break;
423 case ButtonRelease:
424 mouse(ev.xbutton.button - 1, false, ev.xbutton.x, ev.xbutton.y);
425 break;
427 case KeyPress:
428 {
429 KeySym sym = XLookupKeysym(&ev.xkey, 0);
430 keyb(translate_keysym(sym), true);
431 }
432 break;
434 case KeyRelease:
435 {
436 KeySym sym = XLookupKeysym(&ev.xkey, 0);
437 keyb(translate_keysym(sym), false);
438 }
439 break;
441 case ConfigureNotify:
442 {
443 int xsz = ev.xconfigure.width;
444 int ysz = ev.xconfigure.height;
446 if(xsz != win_width || ysz != win_height) {
447 win_width = xsz;
448 win_height = ysz;
449 reshape(xsz, ysz);
450 }
451 }
452 break;
454 case ClientMessage:
455 if(ev.xclient.message_type == xa_wm_prot) {
456 if((Atom)ev.xclient.data.l[0] == xa_wm_del_win) {
457 exit(0);
458 }
459 }
460 break;
462 default:
463 break;
464 }
466 }
467 }
469 static int translate_keysym(KeySym sym)
470 {
471 switch(sym) {
472 case XK_BackSpace:
473 return '\b';
474 case XK_Tab:
475 return '\t';
476 case XK_Linefeed:
477 return '\r';
478 case XK_Return:
479 return '\n';
480 case XK_Escape:
481 return 27;
482 default:
483 break;
484 }
485 return (int)sym;
486 }
488 static int proc_args(int argc, char **argv)
489 {
490 for(int i=1; i<argc; i++) {
491 if(argv[i][0] == '-') {
492 fprintf(stderr, "unexpected option: %s\n", argv[i]);
493 return -1;
495 } else {
496 if(fake_devpath) {
497 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
498 return -1;
499 }
500 fake_devpath = argv[i];
501 }
502 }
503 if(!fake_devpath) {
504 fprintf(stderr, "no device path specified, running standalone\n");
505 }
506 return 0;
507 }