comparison nodejs/X_supp_njs.c @ 1042:18256f404193 refine_backend_if

nodejs binding passes compiling
author Thinker K.F. Li <thinker@codemud.net>
date Tue, 23 Nov 2010 11:44:27 +0800
parents 22e5cf38d8e8
children df6ee4568ca2
comparison
equal deleted inserted replaced
1041:eb3d2e3381cb 1042:18256f404193
5 * Since nodejs use libev to handle event loops, part of X11 backend 5 * Since nodejs use libev to handle event loops, part of X11 backend
6 * code can not be used directly. The part of code should be rewrote. 6 * code can not be used directly. The part of code should be rewrote.
7 * The part is about 7 * The part is about
8 */ 8 */
9 #include <stdio.h> 9 #include <stdio.h>
10 #include <string.h>
10 #include <X11/Xlib.h> 11 #include <X11/Xlib.h>
11 #include <X11/Xutil.h> 12 #include <X11/Xutil.h>
12 #include <ev.h> 13 #include <ev.h>
13 #include "mb_X_supp.h" 14 #include "mb_X_supp.h"
14 #include "mb_tools.h" 15 #include "mb_tools.h"
16 #include <mb_backend.h>
15 #include "X_supp_njs.h" 17 #include "X_supp_njs.h"
16 18
17 #ifndef ASSERT 19 #ifndef ASSERT
18 #define ASSERT(x) 20 #define ASSERT(x)
19 #endif 21 #endif
23 25
24 26
25 /*! \defgroup njs_timer_man Timer manager for nodejs. 27 /*! \defgroup njs_timer_man Timer manager for nodejs.
26 * @{ 28 * @{
27 */ 29 */
28 struct _njs_timer_tiemout { 30 struct _njs_timer_timeout {
29 ev_timer tmwatcher; 31 ev_timer tmwatcher;
30 mb_timer_cb_t cb; 32 mb_timer_cb_t cb;
31 mb_timeval_t *timeout; 33 mb_timeval_t *timeout;
32 void *data; 34 void *data;
33 }; 35 };
34 36
35 static int njs_timer_man_timeout(struct _mb_timer_man *tm_man, 37 static int njs_timer_man_timeout(mb_timer_man_t *tm_man,
36 mb_timeval_t *tm_out, 38 mb_timeval_t *tm_out,
37 mb_timer_cb_t cb, void *data); 39 mb_timer_cb_t cb, void *data);
38 static void njs_timer_man_remove(struct _mb_timer_man *tm_man, int tm_hdl); 40 static void njs_timer_man_remove(mb_timer_man_t *tm_man, int tm_hdl);
39 static mb_timer_man_t *njs_timer_man_new(void); 41 static mb_timer_man_t *njs_timer_man_new(void);
40 static void njs_timer_man_free(mb_timer_man_t *timer_man); 42 static void njs_timer_man_free(mb_timer_man_t *timer_man);
41 43
42 static mb_timer_man_t *njs_timer_man = { 44 static mb_timer_man_t njs_timer_man = {
43 njs_timer_man_timeout, 45 njs_timer_man_timeout,
44 njs_timer_man_remove 46 njs_timer_man_remove
45 }; 47 };
46 48
47 static mb_timer_factory_t njs_timer_factory = { 49 static mb_timer_factory_t njs_timer_factory = {
59 timer_timeout->cb((int)timer_timeout, timer_timeout->timeout, &now, 61 timer_timeout->cb((int)timer_timeout, timer_timeout->timeout, &now,
60 timer_timeout->data); 62 timer_timeout->data);
61 } 63 }
62 64
63 static int 65 static int
64 njs_timer_man_timeout(struct _mb_timer_man *tm_man, 66 njs_timer_man_timeout(mb_timer_man_t *tm_man,
65 mb_timeval_t *timeout, 67 mb_timeval_t *timeout,
66 mb_timer_cb_t cb, void *data) { 68 mb_timer_cb_t cb, void *data) {
67 struct _njs_timer_timeout *timer_timeout; 69 struct _njs_timer_timeout *timer_timeout;
68 mb_timeval_t now, timeout_diff; 70 mb_timeval_t now, timeout_diff;
69 ev_tstamp timeout_stamp; 71 ev_tstamp timeout_stamp;
89 } 91 }
90 92
91 static void 93 static void
92 njs_timer_man_remove(struct _mb_timer_man *tm_man, int tm_hdl) { 94 njs_timer_man_remove(struct _mb_timer_man *tm_man, int tm_hdl) {
93 struct _njs_timer_timeout *timer_timeout = 95 struct _njs_timer_timeout *timer_timeout =
94 MEM2OBJ(tmwatcher, struct _njs_timer_timeout, tmwatcher); 96 (struct _njs_timer_timeout *)tm_hdl;
95 97
96 ev_timer_stop(&timer_timeout->tmwatcher); 98 ev_timer_stop(&timer_timeout->tmwatcher);
97 free(timer_timeout); 99 free(timer_timeout);
98 } 100 }
99 101
100 static mb_timer_man_t * 102 static mb_timer_man_t *
101 njs_timer_man_new(void) { 103 njs_timer_man_new(void) {
102 return &njs_timr_man; 104 return &njs_timer_man;
103 } 105 }
104 106
105 static void 107 static void
106 njs_timer_man_free(mb_timer_man_t *timer_man) { 108 njs_timer_man_free(mb_timer_man_t *timer_man) {
107 } 109 }
111 mb_reg_timer_factory(&njs_timer_factory); 113 mb_reg_timer_factory(&njs_timer_factory);
112 } 114 }
113 115
114 /* @} */ 116 /* @} */
115 117
118 #ifdef USE_MB_TMAN
116 static void timer_cb(EV_P_ ev_timer *tmwatcher, int revent); 119 static void timer_cb(EV_P_ ev_timer *tmwatcher, int revent);
117 120
118 /*! \brief Register next timeout with libev. 121 /*! \brief Register next timeout with libev.
119 */ 122 */
120 static void 123 static void
169 _X_MB_flush_x_conn_for_nodejs(rt->xrt); 172 _X_MB_flush_x_conn_for_nodejs(rt->xrt);
170 173
171 set_next_timeout(rt); 174 set_next_timeout(rt);
172 } 175 }
173 176
177 #endif /* USE_MB_TMAN */
178
174 /*! \defgroup njs_io_man IO manager for nodejs. 179 /*! \defgroup njs_io_man IO manager for nodejs.
175 * @{ 180 * @{
176 */ 181 */
177 struct _njs_io_reg { 182 struct _njs_io_reg {
178 ev_io iowatcher; 183 ev_io iowatcher;
229 struct _njs_io_reg *io_reg; 234 struct _njs_io_reg *io_reg;
230 235
231 if(type == MB_IO_R) 236 if(type == MB_IO_R)
232 _type = EV_READ; 237 _type = EV_READ;
233 else if(type == MB_IO_W) 238 else if(type == MB_IO_W)
234 _type == EV_WRITE; 239 _type = EV_WRITE;
235 else if(type == MB_IO_RW) 240 else if(type == MB_IO_RW)
236 _type == EV_READ | EV_WRITE; 241 _type = EV_READ | EV_WRITE;
237 else 242 else
238 return ERR; 243 return ERR;
239 244
240 io_reg = O_ALLOC(struct _njs_io_reg); 245 io_reg = O_ALLOC(struct _njs_io_reg);
241 if(io_reg == NULL) 246 if(io_reg == NULL)
270 275
271 /*! \brief Register an IO factory with MadButterfly backend. 276 /*! \brief Register an IO factory with MadButterfly backend.
272 */ 277 */
273 void 278 void
274 X_njs_MB_reg_IO_man(void) { 279 X_njs_MB_reg_IO_man(void) {
275 mb_reg_IO_facotry(&njs_io_factory); 280 mb_reg_IO_factory(&njs_io_factory);
276 } 281 }
277 282
278 /* @} */ 283 /* @} */
279 284
285 #if 0
280 /*! \brief Handle connection coming data and timeout of timers. 286 /*! \brief Handle connection coming data and timeout of timers.
281 * 287 *
282 * \param rt is a runtime object for X. 288 * \param rt is a runtime object for X.
283 */ 289 */
284 void 290 void
295 ev_io_start(&rt->iowatcher); 301 ev_io_start(&rt->iowatcher);
296 rt->enable_io = 1; 302 rt->enable_io = 1;
297 303
298 set_next_timeout(rt); 304 set_next_timeout(rt);
299 } 305 }
306 #endif
300 307
301 /*! \brief Free njs_runtime_t. 308 /*! \brief Free njs_runtime_t.
302 */ 309 */
303 void 310 void
304 X_njs_MB_free(njs_runtime_t *rt) { 311 X_njs_MB_free(njs_runtime_t *rt) {
305 /* 312 /*!
306 * stop IO and timer watcher 313 * TODO: Release all IO and timer request.
307 */ 314 */
308 if(rt->enable_io) 315 mb_runtime_free(rt->xrt);
309 ev_io_stop(&rt->iowatcher);
310 if(rt->enable_timer)
311 ev_timer_stop(&rt->tmwatcher);
312
313 X_MB_free(rt->xrt);
314 free(rt); 316 free(rt);
315 } 317 }
316 318
317 /*! \brief Free njs_runtime_t. 319 /*! \brief Free njs_runtime_t.
318 */ 320 */
319 void 321 void
320 X_njs_MB_free_keep_win(njs_runtime_t *rt) { 322 X_njs_MB_free_keep_win(njs_runtime_t *rt) {
321 /* 323 /*
322 * stop IO and timer watcher 324 * TODO: Release all IO and timer request.
323 */ 325 */
324 if(rt->enable_io) 326 mb_runtime_free_keep_win(rt->xrt);
325 ev_io_stop(&rt->iowatcher);
326 if(rt->enable_timer)
327 ev_timer_stop(&rt->tmwatcher);
328
329 X_MB_free_keep_win(rt->xrt);
330 free(rt); 327 free(rt);
331 } 328 }
332 329
333 int 330 int
334 X_njs_MB_flush(njs_runtime_t *rt) { 331 X_njs_MB_flush(njs_runtime_t *rt) {
335 void *xrt = rt->xrt; 332 mb_rt_t *mb_rt = rt->xrt;
336 int r; 333 int r;
337 extern int _X_MB_flush_x_conn_for_nodejs(void *rt); 334
338 335 r = mb_runtime_flush(mb_rt);
339 _X_MB_flush_x_conn_for_nodejs(xrt);
340
341 return r; 336 return r;
342 } 337 }
343 338
344 njs_runtime_t * 339 njs_runtime_t *
345 X_njs_MB_new(char *display_name, int w, int h) { 340 X_njs_MB_new(char *display_name, int w, int h) {
346 njs_runtime_t *rt; 341 njs_runtime_t *rt;
347 void *xrt; 342 mb_rt_t *mb_rt;
348 343
349 rt = (njs_runtime_t *)malloc(sizeof(njs_runtime_t)); 344 rt = (njs_runtime_t *)malloc(sizeof(njs_runtime_t));
350 ASSERT(rt != NULL); 345 ASSERT(rt != NULL);
351 346
352 xrt = X_MB_new(display_name, w, h); 347 mb_rt = mb_runtime_new(display_name, w, h);
353 348
354 rt->xrt = xrt; 349 rt->xrt = mb_rt;
355 rt->enable_io = 0;
356 rt->enable_timer = 0; /* no timer, now */
357 350
358 return rt; 351 return rt;
359 } 352 }
360 353
361 /*! \brief Create a njs_runtime_t for an existed window. 354 /*! \brief Create a njs_runtime_t for an existed window.
364 * X_njs_MB_free_keep_win(). 357 * X_njs_MB_free_keep_win().
365 */ 358 */
366 njs_runtime_t * 359 njs_runtime_t *
367 X_njs_MB_new_with_win(void *display, long win) { 360 X_njs_MB_new_with_win(void *display, long win) {
368 njs_runtime_t *rt; 361 njs_runtime_t *rt;
369 void *xrt; 362 mb_rt_t *mb_rt;
370 363
371 rt = (njs_runtime_t *)malloc(sizeof(njs_runtime_t)); 364 rt = (njs_runtime_t *)malloc(sizeof(njs_runtime_t));
372 ASSERT(rt != NULL); 365 ASSERT(rt != NULL);
373 366
374 xrt = X_MB_new_with_win((Display *)display, win); 367 mb_rt = mb_runtime_new_with_win((Display *)display, win);
375 368
376 rt->xrt = xrt; 369 rt->xrt = mb_rt;
377 rt->enable_io = 0;
378 rt->enable_timer = 0; /* no timer, now */
379 370
380 return rt; 371 return rt;
381 } 372 }
382 373
383 /*! \brief Pass a X event to X runtime. 374 /*! \brief Pass a X event to X runtime.
384 */ 375 */
385 void 376 void
386 X_njs_MB_handle_single_event(njs_runtime_t *rt, void *evt) { 377 X_njs_MB_handle_single_event(njs_runtime_t *rt, void *evt) {
378 #if 0
387 void *xrt = rt->xrt; 379 void *xrt = rt->xrt;
388 extern void _X_MB_handle_single_event(void *rt, void *evt); 380 extern void _X_MB_handle_single_event(void *rt, void *evt);
389 381
390 _X_MB_handle_single_event(xrt, evt); 382 _X_MB_handle_single_event(xrt, evt);
383 #endif
391 } 384 }
392 385
393 /*! \brief Called at end of an iteration of event loop. 386 /*! \brief Called at end of an iteration of event loop.
394 */ 387 */
395 void 388 void
396 X_njs_MB_no_more_event(njs_runtime_t *rt) { 389 X_njs_MB_no_more_event(njs_runtime_t *rt) {
390 #if 0
397 mb_rt_t *xrt = rt->xrt; 391 mb_rt_t *xrt = rt->xrt;
398 extern void _X_MB_no_more_event(mb_rt_t *rt); 392 extern void _X_MB_no_more_event(mb_rt_t *rt);
399 393
400 _X_MB_no_more_event(xrt); 394 _X_MB_no_more_event(xrt);
395 #endif
401 } 396 }
402 397
403 /*! \brief Get X runtime that is backend of this njs runtime. 398 /*! \brief Get X runtime that is backend of this njs runtime.
404 */ 399 */
405 mb_rt_t * 400 mb_rt_t *