Mercurial > MadButterfly
comparison nodejs/X_supp_njs.c @ 546:249bcbf07eb0 Android_Skia
Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 05 Jun 2010 22:24:04 +0800 |
parents | |
children | 127499ab2412 |
comparison
equal
deleted
inserted
replaced
545:49f8f57f184a | 546:249bcbf07eb0 |
---|---|
1 /*! \brief Implement X11 backend for nodejs plugin. | |
2 * | |
3 * Since nodejs use libev to handle event loops, part of X11 backend | |
4 * code can not be used directly. The part of code should be rewrote. | |
5 * The part is about | |
6 */ | |
7 #include <stdio.h> | |
8 #include <X11/Xlib.h> | |
9 #include <X11/Xutil.h> | |
10 #include <ev.h> | |
11 #include "mb_X_supp.h" | |
12 #include "mb_tools.h" | |
13 | |
14 #ifndef ASSERT | |
15 #define ASSERT(x) | |
16 #endif | |
17 | |
18 typedef struct _njs_ev_data { | |
19 ev_io iowatcher; | |
20 ev_timer tmwatcher; | |
21 int enable_timer; | |
22 void *rt; | |
23 } njs_ev_data_t; | |
24 | |
25 static void timer_cb(EV_P_ ev_timer *tmwatcher, int revent); | |
26 | |
27 /*! \brief Register next timeout with libev. | |
28 */ | |
29 static void | |
30 set_next_timeout(njs_ev_data_t *ev_data) { | |
31 mb_tman_t *tman; | |
32 mb_timeval_t now, tmo; | |
33 ev_tstamp tout; | |
34 int r; | |
35 | |
36 tman = X_MB_tman(ev_data->rt); | |
37 get_now(&now); | |
38 r = mb_tman_next_timeout(tman, &now, &tmo); | |
39 if(r == 0) { | |
40 MB_TIMEVAL_DIFF(&tmo, &now); | |
41 tout = (ev_tstamp)MB_TIMEVAL_SEC(&tmo) + | |
42 (ev_tstamp)MB_TIMEVAL_USEC(&tmo) / 1000000; | |
43 ev_timer_init(&ev_data->tmwatcher, timer_cb, tout, 0); | |
44 ev_timer_start(&ev_data->tmwatcher); | |
45 ev_data->enable_timer = 1; | |
46 } else | |
47 ev_data->enable_timer = 0; | |
48 } | |
49 | |
50 static void | |
51 x_conn_cb(EV_P_ ev_io *iowatcher, int revent) { | |
52 njs_ev_data_t *ev_data = MEM2OBJ(iowatcher, njs_ev_data_t, iowatcher); | |
53 redraw_man_t *rdman; | |
54 extern void _X_MB_handle_x_event_for_nodejs(X_MB_runtime_t *rt); | |
55 | |
56 rdman = X_MB_rdman(ev_data->rt); | |
57 _X_MB_handle_x_event_for_nodejs(ev_data->rt); | |
58 rdman_redraw_changed(rdman); | |
59 | |
60 if(ev_data->enable_timer == 0) /* no installed timeout */ | |
61 set_next_timeout(ev_data); | |
62 } | |
63 | |
64 static void | |
65 timer_cb(EV_P_ ev_timer *tmwatcher, int revent) { | |
66 njs_ev_data_t *ev_data = MEM2OBJ(tmwatcher, njs_ev_data_t, tmwatcher); | |
67 mb_tman_t *tman; | |
68 redraw_man_t *rdman; | |
69 mb_timeval_t now; | |
70 extern int _X_MB_flush_x_conn_nodejs(X_MB_runtime_t *rt); | |
71 | |
72 tman = X_MB_tman(ev_data->rt); | |
73 get_now(&now); | |
74 mb_tman_handle_timeout(tman, &now); | |
75 | |
76 rdman = X_MB_rdman(ev_data->rt); | |
77 rdman_redraw_changed(rdman); | |
78 _X_MB_flush_x_conn_nodejs(ev_data->rt); | |
79 | |
80 set_next_timeout(ev_data); | |
81 } | |
82 | |
83 /*! \brief Handle connection coming data and timeout of timers. | |
84 * | |
85 * \param rt is a runtime object for X. | |
86 */ | |
87 void X_MB_handle_connection_njs(void *rt) { | |
88 X_MB_runtime_t *_rt = (X_MB_runtime_t *)rt; | |
89 njs_ev_data_t *ev_data; | |
90 mb_tman_t *tman; | |
91 mb_timeval_t now, tmo; | |
92 ev_tstamp tout; | |
93 int fd; | |
94 int r; | |
95 extern int _X_MB_get_x_conn_for_nodejs(X_MB_runtime_t *rt); | |
96 | |
97 ev_data = (njs_ev_data_t *)malloc(sizeof(njs_ev_data_t)); | |
98 ASSERT(ev_data != NULL); | |
99 | |
100 _X_MB_set_data_nodejs(_rt, ev_data); | |
101 ev_data->rt = _rt; | |
102 ev_default_loop(0); | |
103 | |
104 /* | |
105 * Setup watcher for X connection. | |
106 */ | |
107 fd = _X_MB_get_x_conn_for_nodejs(_rt); | |
108 ev_io_init(&ev_data->iowatcher, x_conn_cb, fd, EV_READ); | |
109 ev_io_start(&ev_data->iowatcher); | |
110 | |
111 set_next_timeout(ev_data); | |
112 } | |
113 | |
114 void X_MB_stop_njs(void *rt) { | |
115 X_MB_runtime_t *_rt = (X_MB_runtime_t *)rt; | |
116 njs_ev_data_t *ev_data; | |
117 | |
118 ev_data = (njs_ev_data_t *)_X_MB_get_x_conn_for_nodejs(_rt); | |
119 | |
120 /* | |
121 * stop IO and timer watcher | |
122 */ | |
123 ev_io_stop(&ev_data->iowatcher); | |
124 if(ev_data->enable_timer) | |
125 ev_timer_stop(&ev_data->tmwatcher); | |
126 } |