comparison src/X_supp.c @ 462:af4b506ad56f

Add backend layer to seperate the backend with the MBAF. Currently, X is the only backend. If we have more than one backend, we need to modify the Makefile to sleect the backend or implement a backend selection mechanism in the runtime.
author wycc@122-116-38-188.HINET-IP.hinet.net
date Wed, 28 Oct 2009 02:39:02 +0800
parents 5c9e2a8a4bd8
children e98ae1407ca2
comparison
equal deleted inserted replaced
461:61a0bceb369d 462:af4b506ad56f
23 subject_t *kbevents; 23 subject_t *kbevents;
24 ob_factory_t *ob_factory; 24 ob_factory_t *ob_factory;
25 }; 25 };
26 26
27 /* @} */ 27 /* @} */
28 28 #define MAX_MONITORS 200
29 typedef struct {
30 int type;
31 int fd;
32 mb_eventcb_t f;
33 void *arg;
34 } monitor_t;
29 struct _X_MB_runtime { 35 struct _X_MB_runtime {
30 Display *display; 36 Display *display;
31 Window win; 37 Window win;
32 Visual *visual; 38 Visual *visual;
33 mbe_surface_t *surface, *backend_surface; 39 mbe_surface_t *surface, *backend_surface;
36 mb_tman_t *tman; 42 mb_tman_t *tman;
37 mb_img_ldr_t *img_ldr; 43 mb_img_ldr_t *img_ldr;
38 int w, h; 44 int w, h;
39 45
40 X_kb_info_t kbinfo; 46 X_kb_info_t kbinfo;
47 monitor_t monitors[MAX_MONITORS];
48 int n_monitor;
41 49
42 #ifndef ONLY_MOUSE_MOVE_RAW 50 #ifndef ONLY_MOUSE_MOVE_RAW
43 /* States */ 51 /* States */
44 shape_t *last; 52 shape_t *last;
45 #endif 53 #endif
325 * \param tman is a timer manager. 333 * \param tman is a timer manager.
326 * 334 *
327 * The display is managed by specified rdman and tman. rdman draws 335 * The display is managed by specified rdman and tman. rdman draws
328 * on the display, and tman trigger actions according timers. 336 * on the display, and tman trigger actions according timers.
329 */ 337 */
330 void X_MB_handle_connection(X_MB_runtime_t *rt) { 338 void X_MB_handle_connection(void *be) {
339 X_MB_runtime_t *rt = (X_MB_runtime_t *) be;
331 Display *display = rt->display; 340 Display *display = rt->display;
332 redraw_man_t *rdman = rt->rdman; 341 redraw_man_t *rdman = rt->rdman;
333 mb_tman_t *tman = rt->tman; 342 mb_tman_t *tman = rt->tman;
334 int fd; 343 int fd;
335 mb_timeval_t now, tmo; 344 mb_timeval_t now, tmo;
336 struct timeval tv; 345 struct timeval tv;
337 fd_set rfds; 346 fd_set rfds,wfds;
338 int nfds; 347 int nfds;
339 int r, r1; 348 int r, r1,i;
340 349
341 handle_x_event(rt); 350 handle_x_event(rt);
342 351
343 fd = XConnectionNumber(display); 352 fd = XConnectionNumber(display);
344 nfds = fd + 1; 353 nfds = fd + 1;
345 while(1) { 354 while(1) {
346 FD_ZERO(&rfds); 355 FD_ZERO(&rfds);
356 FD_ZERO(&wfds);
347 FD_SET(fd, &rfds); 357 FD_SET(fd, &rfds);
358 for(i=0;i<rt->n_monitor;i++) {
359 if (rt->monitors[i].type == MONITOR_READ)
360 FD_SET(rt->monitors[i].fd, &rfds);
361 else if (rt->monitors[i].type == MONITOR_WRITE)
362 FD_SET(rt->monitors[i].fd, &wfds);
363 }
348 364
349 get_now(&now); 365 get_now(&now);
350 r = mb_tman_next_timeout(tman, &now, &tmo); 366 r = mb_tman_next_timeout(tman, &now, &tmo);
351 367
352 if(r == 0) { 368 if(r == 0) {
366 mb_tman_handle_timeout(tman, &now); 382 mb_tman_handle_timeout(tman, &now);
367 rdman_redraw_changed(rdman); 383 rdman_redraw_changed(rdman);
368 XFlush(display); 384 XFlush(display);
369 } else if(FD_ISSET(fd, &rfds)){ 385 } else if(FD_ISSET(fd, &rfds)){
370 handle_x_event(rt); 386 handle_x_event(rt);
387 } else {
388 for(i=0;i<rt->n_monitor;i++) {
389 if (rt->monitors[i].type == MONITOR_READ)
390 if (FD_ISSET(rt->monitors[i].fd, &rfds))
391 rt->monitors[i].f(rt->monitors[i].fd,rt->monitors[i].arg);
392 else if (rt->monitors[i].type == MONITOR_WRITE)
393 if (FD_ISSET(rt->monitors[i].fd, &wfds))
394 rt->monitors[i].f(rt->monitors[i].fd,rt->monitors[i].arg);
395 }
371 } 396 }
372 } 397 }
373 } 398 }
374 399
375 #define ERR -1 400 #define ERR -1
465 xmb_rt->tman = mb_tman_new(); 490 xmb_rt->tman = mb_tman_new();
466 491
467 img_ldr = simple_mb_img_ldr_new(""); 492 img_ldr = simple_mb_img_ldr_new("");
468 xmb_rt->img_ldr = img_ldr; 493 xmb_rt->img_ldr = img_ldr;
469 rdman_set_img_ldr(xmb_rt->rdman, img_ldr); 494 rdman_set_img_ldr(xmb_rt->rdman, img_ldr);
495 memset(xmb_rt->monitors,0,sizeof(xmb_rt->monitors));
470 496
471 #ifndef ONLY_MOUSE_MOVE_RAW 497 #ifndef ONLY_MOUSE_MOVE_RAW
472 xmb_rt->last = NULL; 498 xmb_rt->last = NULL;
473 #endif 499 #endif
474 500
503 XCloseDisplay(xmb_rt->display); 529 XCloseDisplay(xmb_rt->display);
504 530
505 X_kb_destroy(&xmb_rt->kbinfo); 531 X_kb_destroy(&xmb_rt->kbinfo);
506 } 532 }
507 533
508 X_MB_runtime_t *X_MB_new(const char *display_name, int w, int h) { 534 void *X_MB_new(const char *display_name, int w, int h) {
509 X_MB_runtime_t *rt; 535 X_MB_runtime_t *rt;
510 int r; 536 int r;
511 537
512 rt = O_ALLOC(X_MB_runtime_t); 538 rt = O_ALLOC(X_MB_runtime_t);
513 if(rt == NULL) 539 if(rt == NULL)
518 return NULL; 544 return NULL;
519 545
520 return rt; 546 return rt;
521 } 547 }
522 548
523 void X_MB_free(X_MB_runtime_t *rt) { 549 void X_MB_free(void *rt) {
524 X_MB_destroy(rt); 550 X_MB_destroy((X_MB_runtime_t *) rt);
525 free(rt); 551 free(rt);
526 } 552 }
527 553
528 subject_t *X_MB_kbevents(X_MB_runtime_t *xmb_rt) { 554 subject_t *X_MB_kbevents(void *rt) {
555 X_MB_runtime_t *xmb_rt = (X_MB_runtime_t *) rt;
529 return xmb_rt->kbinfo.kbevents; 556 return xmb_rt->kbinfo.kbevents;
530 } 557 }
531 558
532 redraw_man_t *X_MB_rdman(X_MB_runtime_t *xmb_rt) { 559 redraw_man_t *X_MB_rdman(void *rt) {
560 X_MB_runtime_t *xmb_rt = (X_MB_runtime_t *) rt;
533 return xmb_rt->rdman; 561 return xmb_rt->rdman;
534 } 562 }
535 563
536 mb_tman_t *X_MB_tman(X_MB_runtime_t *xmb_rt) { 564 mb_tman_t *X_MB_tman(void *rt) {
565 X_MB_runtime_t *xmb_rt = (X_MB_runtime_t *) rt;
537 return xmb_rt->tman; 566 return xmb_rt->tman;
538 } 567 }
539 568
540 ob_factory_t *X_MB_ob_factory(X_MB_runtime_t *xmb_rt) { 569 ob_factory_t *X_MB_ob_factory(void *rt) {
570 X_MB_runtime_t *xmb_rt = (X_MB_runtime_t *) rt;
541 ob_factory_t *factory; 571 ob_factory_t *factory;
542 572
543 factory = rdman_get_ob_factory(xmb_rt->rdman); 573 factory = rdman_get_ob_factory(xmb_rt->rdman);
544 return factory; 574 return factory;
545 } 575 }
546 576
547 mb_img_ldr_t *X_MB_img_ldr(X_MB_runtime_t *xmb_rt) { 577 mb_img_ldr_t *X_MB_img_ldr(void *rt) {
578 X_MB_runtime_t *xmb_rt = (X_MB_runtime_t *) rt;
548 X_MB_runtime_t *img_ldr; 579 X_MB_runtime_t *img_ldr;
549 580
550 img_ldr = xmb_rt->img_ldr; 581 img_ldr = xmb_rt->img_ldr;
551 582
552 return img_ldr; 583 return img_ldr;
553 } 584 }
585
586 void X_add_event(void *rt, int type, int fd, mb_eventcb_t f,void *arg)
587 {
588 X_MB_runtime_t *xmb_rt = (X_MB_runtime_t *) rt;
589 int i;
590
591 for(i=0;i<xmb_rt->n_monitor;i++) {
592 if (xmb_rt->monitors[i].type == type && xmb_rt->monitors[i].fd == fd) {
593 xmb_rt->monitors[i].f = f;
594 xmb_rt->monitors[i].arg = arg;
595 return;
596 }
597 }
598 for(i=0;i<xmb_rt->n_monitor;i++) {
599 if (xmb_rt->monitors[i].type == 0) {
600 xmb_rt->monitors[i].type = type;
601 xmb_rt->monitors[i].fd = fd;
602 xmb_rt->monitors[i].f = f;
603 xmb_rt->monitors[i].arg = arg;
604 return;
605 }
606 }
607 if (i == MAX_MONITORS) return;
608 xmb_rt->monitors[i].type = type;
609 xmb_rt->monitors[i].fd = fd;
610 xmb_rt->monitors[i].f = f;
611 xmb_rt->monitors[i].arg = arg;
612 i++;
613 xmb_rt->n_monitor=i;
614 }
615
616 void X_remove_event(void *rt, int type, int fd)
617 {
618 X_MB_runtime_t *xmb_rt = (X_MB_runtime_t *) rt;
619 int i;
620 for(i=0;i<xmb_rt->n_monitor;i++) {
621 if (xmb_rt->monitors[i].type == type && xmb_rt->monitors[i].fd == fd) {
622 xmb_rt->monitors[i].type = 0;
623 return;
624 }
625 }
626 }
627 mb_backend_t backend = { X_MB_new,
628 X_MB_free,
629 X_add_event,
630 X_remove_event,
631 X_MB_handle_connection,
632 X_MB_kbevents,
633 X_MB_rdman,
634 X_MB_tman,
635 X_MB_ob_factory,
636 X_MB_img_ldr
637 };
638