annotate nodejs/X_supp_njs.c @ 1041:eb3d2e3381cb refine_backend_if

Fix typo for mb_runtime_free_keep_win()
author Thinker K.F. Li <thinker@codemud.net>
date Tue, 23 Nov 2010 11:42:01 +0800
parents 22e5cf38d8e8
children 18256f404193
rev   line source
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 753
diff changeset
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*-
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 753
diff changeset
2 // vim: sw=4:ts=8:sts=4
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3 /*! \brief Implement X11 backend for nodejs plugin.
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4 *
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 * Since nodejs use libev to handle event loops, part of X11 backend
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6 * code can not be used directly. The part of code should be rewrote.
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7 * The part is about
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8 */
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9 #include <stdio.h>
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 #include <X11/Xlib.h>
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 #include <X11/Xutil.h>
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 #include <ev.h>
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 #include "mb_X_supp.h"
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 #include "mb_tools.h"
560
ce7a35abcb0d Function to instantiate coord for Javascript
Thinker K.F. Li <thinker@branda.to>
parents: 556
diff changeset
15 #include "X_supp_njs.h"
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 #ifndef ASSERT
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 #define ASSERT(x)
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 #endif
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20
1039
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
21 #define OK 0
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
22 #define ERR -1
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
23
1040
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
24
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
25 /*! \defgroup njs_timer_man Timer manager for nodejs.
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
26 * @{
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
27 */
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
28 struct _njs_timer_tiemout {
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
29 ev_timer tmwatcher;
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
30 mb_timer_cb_t cb;
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
31 mb_timeval_t *timeout;
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
32 void *data;
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
33 };
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
34
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
35 static int njs_timer_man_timeout(struct _mb_timer_man *tm_man,
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
36 mb_timeval_t *tm_out,
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
37 mb_timer_cb_t cb, void *data);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
38 static void njs_timer_man_remove(struct _mb_timer_man *tm_man, int tm_hdl);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
39 static mb_timer_man_t *njs_timer_man_new(void);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
40 static void njs_timer_man_free(mb_timer_man_t *timer_man);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
41
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
42 static mb_timer_man_t *njs_timer_man = {
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
43 njs_timer_man_timeout,
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
44 njs_timer_man_remove
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
45 };
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
46
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
47 static mb_timer_factory_t njs_timer_factory = {
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
48 njs_timer_man_new,
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
49 njs_timer_man_free
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
50 };
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
51
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
52 static void
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
53 njs_timer_man_cb(EV_P_ ev_timer *tmwatcher, int revent) {
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
54 struct _njs_timer_timeout *timer_timeout =
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
55 MEM2OBJ(tmwatcher, struct _njs_timer_timeout, tmwatcher);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
56 mb_timeval_t now;
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
57
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
58 get_now(&now);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
59 timer_timeout->cb((int)timer_timeout, timer_timeout->timeout, &now,
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
60 timer_timeout->data);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
61 }
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
62
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
63 static int
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
64 njs_timer_man_timeout(struct _mb_timer_man *tm_man,
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
65 mb_timeval_t *timeout,
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
66 mb_timer_cb_t cb, void *data) {
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
67 struct _njs_timer_timeout *timer_timeout;
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
68 mb_timeval_t now, timeout_diff;
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
69 ev_tstamp timeout_stamp;
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
70
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
71 timer_timeout = O_ALLOC(struct _njs_timer_timeout);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
72 if(timer_timeout == NULL)
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
73 return ERR;
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
74
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
75 timer_timeout->cb = cb;
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
76 timer_timeout->timeout = timeout;
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
77
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
78 get_now(&now);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
79
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
80 memcpy(&timeout_diff, timeout, sizeof(mb_timeval_t));
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
81 MB_TIMEVAL_DIFF(&timeout_diff, &now);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
82 timeout_stamp = (ev_tstamp)MB_TIMEVAL_SEC(&timeout_diff) +
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
83 (ev_tstamp)MB_TIMEVAL_USEC(&timeout_diff) / 1000000;
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
84 ev_timer_init(&timer_timeout->tmwatcher, njs_timer_man_cb,
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
85 timeout_stamp, 0);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
86 ev_timer_start(&timer_timeout->tmwatcher);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
87
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
88 return (int)timer_timeout;
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
89 }
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
90
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
91 static void
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
92 njs_timer_man_remove(struct _mb_timer_man *tm_man, int tm_hdl) {
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
93 struct _njs_timer_timeout *timer_timeout =
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
94 MEM2OBJ(tmwatcher, struct _njs_timer_timeout, tmwatcher);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
95
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
96 ev_timer_stop(&timer_timeout->tmwatcher);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
97 free(timer_timeout);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
98 }
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
99
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
100 static mb_timer_man_t *
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
101 njs_timer_man_new(void) {
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
102 return &njs_timr_man;
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
103 }
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
104
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
105 static void
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
106 njs_timer_man_free(mb_timer_man_t *timer_man) {
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
107 }
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
108
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
109 void
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
110 X_njs_MB_reg_timer_man(void) {
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
111 mb_reg_timer_factory(&njs_timer_factory);
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
112 }
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
113
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
114 /* @} */
22e5cf38d8e8 Timer manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 1039
diff changeset
115
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116 static void timer_cb(EV_P_ ev_timer *tmwatcher, int revent);
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 /*! \brief Register next timeout with libev.
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
119 */
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120 static void
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
121 set_next_timeout(njs_runtime_t *rt) {
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
122 mb_tman_t *tman;
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
123 mb_timeval_t now, tmo;
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
124 ev_tstamp tout;
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
125 int r;
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 753
diff changeset
126
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
127 tman = X_MB_tman(rt->xrt);
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
128 get_now(&now);
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
129 r = mb_tman_next_timeout(tman, &now, &tmo);
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
130 if(r == 0) {
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
131 MB_TIMEVAL_DIFF(&tmo, &now);
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
132 tout = (ev_tstamp)MB_TIMEVAL_SEC(&tmo) +
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
133 (ev_tstamp)MB_TIMEVAL_USEC(&tmo) / 1000000;
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
134 ev_timer_init(&rt->tmwatcher, timer_cb, tout, 0);
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
135 ev_timer_start(&rt->tmwatcher);
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
136 rt->enable_timer = 1;
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
137 } else
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
138 rt->enable_timer = 0;
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
139 }
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
140
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
141 static void
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
142 x_conn_cb(EV_P_ ev_io *iowatcher, int revent) {
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
143 njs_runtime_t *rt = MEM2OBJ(iowatcher, njs_runtime_t, iowatcher);
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
144 redraw_man_t *rdman;
550
127499ab2412 Upgrade ndoejs plugin to level of a new runtime.
Thinker K.F. Li <thinker@branda.to>
parents: 546
diff changeset
145 extern void _X_MB_handle_x_event_for_nodejs(void *rt);
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
146
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
147 rdman = X_MB_rdman(rt->xrt);
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
148 _X_MB_handle_x_event_for_nodejs(rt->xrt);
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
149 rdman_redraw_changed(rdman);
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 753
diff changeset
150
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
151 if(rt->enable_timer == 0) /* no installed timeout */
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
152 set_next_timeout(rt);
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
153 }
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
154
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
155 static void
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
156 timer_cb(EV_P_ ev_timer *tmwatcher, int revent) {
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
157 njs_runtime_t *rt = MEM2OBJ(tmwatcher, njs_runtime_t, tmwatcher);
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
158 mb_tman_t *tman;
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
159 redraw_man_t *rdman;
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
160 mb_timeval_t now;
556
c9d23f7279a4 The first testcase that nodejs code can show a MadButterfly window.
Thinker K.F. Li <thinker@branda.to>
parents: 554
diff changeset
161 extern int _X_MB_flush_x_conn_for_nodejs(void *rt);
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 753
diff changeset
162
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
163 tman = X_MB_tman(rt->xrt);
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
164 get_now(&now);
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
165 mb_tman_handle_timeout(tman, &now);
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
166
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
167 rdman = X_MB_rdman(rt->xrt);
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
168 rdman_redraw_changed(rdman);
556
c9d23f7279a4 The first testcase that nodejs code can show a MadButterfly window.
Thinker K.F. Li <thinker@branda.to>
parents: 554
diff changeset
169 _X_MB_flush_x_conn_for_nodejs(rt->xrt);
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 753
diff changeset
170
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
171 set_next_timeout(rt);
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
172 }
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
173
1039
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
174 /*! \defgroup njs_io_man IO manager for nodejs.
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
175 * @{
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
176 */
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
177 struct _njs_io_reg {
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
178 ev_io iowatcher;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
179 int fd;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
180 mb_IO_cb_t cb;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
181 void *data;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
182 };
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
183
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
184 static int njs_io_man_reg(struct _mb_IO_man *io_man,
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
185 int fd, MB_IO_TYPE type, mb_IO_cb_t cb, void *data);
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
186 static void njs_io_man_unreg(struct _mb_IO_man *io_man, int io_hdl);
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
187 static mb_IO_man_t *njs_io_man_new(void);
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
188 static void njs_io_man_free(mb_IO_man_t *io_man);
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
189
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
190 static mb_IO_man_t njs_io_man = {
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
191 njs_io_man_reg,
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
192 njs_io_man_unreg
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
193 };
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
194
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
195 /*! \brief IO factory to integrate MadButterfly to event loop of nodejs.
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
196 */
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
197 static mb_IO_factory_t njs_io_factory = {
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
198 njs_io_man_new,
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
199 njs_io_man_free
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
200 };
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
201
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
202 /*! \brief Bridge libev callback to IO manager callback.
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
203 */
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
204 static void
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
205 njs_io_man_cb(EV_P_ ev_io *iowatcher, int revent) {
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
206 struct _njs_io_reg *io_reg =
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
207 MEM2OBJ(iowatcher, struct _njs_io_reg, iowatcher);
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
208 MB_IO_TYPE type;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
209
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
210 switch(revent & (EV_READ | EV_WRITE)) {
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
211 case EV_READ:
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
212 type = MB_IO_R;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
213 break;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
214 case EV_WRITE:
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
215 type = MB_IO_W;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
216 break;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
217 case EV_READ | EV_WRITE:
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
218 type = MB_IO_RW;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
219 break;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
220 }
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
221
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
222 io_reg->cb((int)io_reg, io_reg->fd, type, io_reg->data);
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
223 }
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
224
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
225 static int
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
226 njs_io_man_reg(struct _mb_IO_man *io_man,
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
227 int fd, MB_IO_TYPE type, mb_IO_cb_t cb, void *data) {
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
228 int _type;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
229 struct _njs_io_reg *io_reg;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
230
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
231 if(type == MB_IO_R)
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
232 _type = EV_READ;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
233 else if(type == MB_IO_W)
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
234 _type == EV_WRITE;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
235 else if(type == MB_IO_RW)
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
236 _type == EV_READ | EV_WRITE;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
237 else
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
238 return ERR;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
239
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
240 io_reg = O_ALLOC(struct _njs_io_reg);
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
241 if(io_reg == NULL)
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
242 return ERR;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
243
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
244 io_reg->fd = fd;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
245 io_reg->cb = cb;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
246 io_reg->data = data;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
247
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
248 ev_io_init(&io_reg->iowatcher, njs_io_man_cb, fd, _type);
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
249 ev_io_start(&io_reg->iowatcher);
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
250
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
251 return (int)io_reg;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
252 }
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
253
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
254 static void
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
255 njs_io_man_unreg(struct _mb_IO_man *io_man, int io_hdl) {
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
256 struct _njs_io_reg *io_reg = (struct _njs_io_reg *)io_hdl;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
257
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
258 ev_io_stop(&io_reg->iowatcher);
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
259 free(io_reg);
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
260 }
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
261
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
262 static mb_IO_man_t *
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
263 njs_io_man_new(void) {
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
264 return &njs_io_man;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
265 }
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
266
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
267 static void
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
268 njs_io_man_free(mb_IO_man_t *io_man) {
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
269 }
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
270
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
271 /*! \brief Register an IO factory with MadButterfly backend.
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
272 */
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
273 void
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
274 X_njs_MB_reg_IO_man(void) {
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
275 mb_reg_IO_facotry(&njs_io_factory);
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
276 }
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
277
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
278 /* @} */
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
279
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
280 /*! \brief Handle connection coming data and timeout of timers.
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
281 *
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
282 * \param rt is a runtime object for X.
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
283 */
550
127499ab2412 Upgrade ndoejs plugin to level of a new runtime.
Thinker K.F. Li <thinker@branda.to>
parents: 546
diff changeset
284 void
556
c9d23f7279a4 The first testcase that nodejs code can show a MadButterfly window.
Thinker K.F. Li <thinker@branda.to>
parents: 554
diff changeset
285 X_njs_MB_init_handle_connection(njs_runtime_t *rt) {
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
286 void *xrt = rt->xrt;
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
287 int fd;
550
127499ab2412 Upgrade ndoejs plugin to level of a new runtime.
Thinker K.F. Li <thinker@branda.to>
parents: 546
diff changeset
288 extern int _X_MB_get_x_conn_for_nodejs(void *rt);
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
289
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
290 /*
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
291 * Setup watcher for X connection.
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
292 */
551
9e69c4a0f565 rename njs_ev_data_t to njs_runtime_t
Thinker K.F. Li <thinker@branda.to>
parents: 550
diff changeset
293 fd = _X_MB_get_x_conn_for_nodejs(xrt);
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
294 ev_io_init(&rt->iowatcher, x_conn_cb, fd, EV_READ);
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
295 ev_io_start(&rt->iowatcher);
554
4caf6090825f Fix leaking on freeing a njs runtime object
Thinker K.F. Li <thinker@branda.to>
parents: 553
diff changeset
296 rt->enable_io = 1;
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
297
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
298 set_next_timeout(rt);
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
299 }
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
300
551
9e69c4a0f565 rename njs_ev_data_t to njs_runtime_t
Thinker K.F. Li <thinker@branda.to>
parents: 550
diff changeset
301 /*! \brief Free njs_runtime_t.
550
127499ab2412 Upgrade ndoejs plugin to level of a new runtime.
Thinker K.F. Li <thinker@branda.to>
parents: 546
diff changeset
302 */
127499ab2412 Upgrade ndoejs plugin to level of a new runtime.
Thinker K.F. Li <thinker@branda.to>
parents: 546
diff changeset
303 void
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
304 X_njs_MB_free(njs_runtime_t *rt) {
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
305 /*
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
306 * stop IO and timer watcher
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
307 */
554
4caf6090825f Fix leaking on freeing a njs runtime object
Thinker K.F. Li <thinker@branda.to>
parents: 553
diff changeset
308 if(rt->enable_io)
4caf6090825f Fix leaking on freeing a njs runtime object
Thinker K.F. Li <thinker@branda.to>
parents: 553
diff changeset
309 ev_io_stop(&rt->iowatcher);
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
310 if(rt->enable_timer)
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
311 ev_timer_stop(&rt->tmwatcher);
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 753
diff changeset
312
554
4caf6090825f Fix leaking on freeing a njs runtime object
Thinker K.F. Li <thinker@branda.to>
parents: 553
diff changeset
313 X_MB_free(rt->xrt);
4caf6090825f Fix leaking on freeing a njs runtime object
Thinker K.F. Li <thinker@branda.to>
parents: 553
diff changeset
314 free(rt);
546
249bcbf07eb0 Reuse and adapt X_supp.c by implmeneting X_supp_njs.c
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
315 }
550
127499ab2412 Upgrade ndoejs plugin to level of a new runtime.
Thinker K.F. Li <thinker@branda.to>
parents: 546
diff changeset
316
871
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
317 /*! \brief Free njs_runtime_t.
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
318 */
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
319 void
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
320 X_njs_MB_free_keep_win(njs_runtime_t *rt) {
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
321 /*
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
322 * stop IO and timer watcher
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
323 */
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
324 if(rt->enable_io)
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
325 ev_io_stop(&rt->iowatcher);
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
326 if(rt->enable_timer)
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
327 ev_timer_stop(&rt->tmwatcher);
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
328
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
329 X_MB_free_keep_win(rt->xrt);
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
330 free(rt);
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
331 }
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
332
571
13b15b7a463b Redraw all and changed in Javascript
Thinker K.F. Li <thinker@branda.to>
parents: 560
diff changeset
333 int
13b15b7a463b Redraw all and changed in Javascript
Thinker K.F. Li <thinker@branda.to>
parents: 560
diff changeset
334 X_njs_MB_flush(njs_runtime_t *rt) {
13b15b7a463b Redraw all and changed in Javascript
Thinker K.F. Li <thinker@branda.to>
parents: 560
diff changeset
335 void *xrt = rt->xrt;
13b15b7a463b Redraw all and changed in Javascript
Thinker K.F. Li <thinker@branda.to>
parents: 560
diff changeset
336 int r;
13b15b7a463b Redraw all and changed in Javascript
Thinker K.F. Li <thinker@branda.to>
parents: 560
diff changeset
337 extern int _X_MB_flush_x_conn_for_nodejs(void *rt);
13b15b7a463b Redraw all and changed in Javascript
Thinker K.F. Li <thinker@branda.to>
parents: 560
diff changeset
338
13b15b7a463b Redraw all and changed in Javascript
Thinker K.F. Li <thinker@branda.to>
parents: 560
diff changeset
339 _X_MB_flush_x_conn_for_nodejs(xrt);
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 753
diff changeset
340
571
13b15b7a463b Redraw all and changed in Javascript
Thinker K.F. Li <thinker@branda.to>
parents: 560
diff changeset
341 return r;
13b15b7a463b Redraw all and changed in Javascript
Thinker K.F. Li <thinker@branda.to>
parents: 560
diff changeset
342 }
13b15b7a463b Redraw all and changed in Javascript
Thinker K.F. Li <thinker@branda.to>
parents: 560
diff changeset
343
551
9e69c4a0f565 rename njs_ev_data_t to njs_runtime_t
Thinker K.F. Li <thinker@branda.to>
parents: 550
diff changeset
344 njs_runtime_t *
550
127499ab2412 Upgrade ndoejs plugin to level of a new runtime.
Thinker K.F. Li <thinker@branda.to>
parents: 546
diff changeset
345 X_njs_MB_new(char *display_name, int w, int h) {
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
346 njs_runtime_t *rt;
551
9e69c4a0f565 rename njs_ev_data_t to njs_runtime_t
Thinker K.F. Li <thinker@branda.to>
parents: 550
diff changeset
347 void *xrt;
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 753
diff changeset
348
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
349 rt = (njs_runtime_t *)malloc(sizeof(njs_runtime_t));
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
350 ASSERT(rt != NULL);
550
127499ab2412 Upgrade ndoejs plugin to level of a new runtime.
Thinker K.F. Li <thinker@branda.to>
parents: 546
diff changeset
351
551
9e69c4a0f565 rename njs_ev_data_t to njs_runtime_t
Thinker K.F. Li <thinker@branda.to>
parents: 550
diff changeset
352 xrt = X_MB_new(display_name, w, h);
550
127499ab2412 Upgrade ndoejs plugin to level of a new runtime.
Thinker K.F. Li <thinker@branda.to>
parents: 546
diff changeset
353
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
354 rt->xrt = xrt;
554
4caf6090825f Fix leaking on freeing a njs runtime object
Thinker K.F. Li <thinker@branda.to>
parents: 553
diff changeset
355 rt->enable_io = 0;
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
356 rt->enable_timer = 0; /* no timer, now */
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 753
diff changeset
357
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
358 return rt;
550
127499ab2412 Upgrade ndoejs plugin to level of a new runtime.
Thinker K.F. Li <thinker@branda.to>
parents: 546
diff changeset
359 }
127499ab2412 Upgrade ndoejs plugin to level of a new runtime.
Thinker K.F. Li <thinker@branda.to>
parents: 546
diff changeset
360
871
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
361 /*! \brief Create a njs_runtime_t for an existed window.
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
362 *
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
363 * The njs_runtime_t created by this function must be free by
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
364 * X_njs_MB_free_keep_win().
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
365 */
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
366 njs_runtime_t *
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
367 X_njs_MB_new_with_win(void *display, long win) {
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
368 njs_runtime_t *rt;
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
369 void *xrt;
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
370
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
371 rt = (njs_runtime_t *)malloc(sizeof(njs_runtime_t));
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
372 ASSERT(rt != NULL);
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
373
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
374 xrt = X_MB_new_with_win((Display *)display, win);
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
375
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
376 rt->xrt = xrt;
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
377 rt->enable_io = 0;
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
378 rt->enable_timer = 0; /* no timer, now */
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
379
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
380 return rt;
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
381 }
67d0fed24120 Export a function to create a runtime for an existed window for JS
Thinker K.F. Li <thinker@codemud.net>
parents: 869
diff changeset
382
869
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
383 /*! \brief Pass a X event to X runtime.
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
384 */
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
385 void
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
386 X_njs_MB_handle_single_event(njs_runtime_t *rt, void *evt) {
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
387 void *xrt = rt->xrt;
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
388 extern void _X_MB_handle_single_event(void *rt, void *evt);
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
389
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
390 _X_MB_handle_single_event(xrt, evt);
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
391 }
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
392
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
393 /*! \brief Called at end of an iteration of event loop.
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
394 */
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
395 void
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
396 X_njs_MB_no_more_event(njs_runtime_t *rt) {
1039
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
397 mb_rt_t *xrt = rt->xrt;
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
398 extern void _X_MB_no_more_event(mb_rt_t *rt);
869
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
399
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
400 _X_MB_no_more_event(xrt);
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
401 }
c18058fb48ee Export API for JS to feed X events.
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
402
553
0a77b88500bf rename _X_njs_MB_get_runtime to _X_njs_MB_get_X_runtime
Thinker K.F. Li <thinker@branda.to>
parents: 552
diff changeset
403 /*! \brief Get X runtime that is backend of this njs runtime.
0a77b88500bf rename _X_njs_MB_get_runtime to _X_njs_MB_get_X_runtime
Thinker K.F. Li <thinker@branda.to>
parents: 552
diff changeset
404 */
1039
176cba3ad32c IO manager for njs
Thinker K.F. Li <thinker@codemud.net>
parents: 871
diff changeset
405 mb_rt_t *
553
0a77b88500bf rename _X_njs_MB_get_runtime to _X_njs_MB_get_X_runtime
Thinker K.F. Li <thinker@branda.to>
parents: 552
diff changeset
406 _X_njs_MB_get_X_runtime(njs_runtime_t *rt) {
552
dd8d32b4c899 rename ev_data to rt
Thinker K.F. Li <thinker@branda.to>
parents: 551
diff changeset
407 return rt->xrt;
550
127499ab2412 Upgrade ndoejs plugin to level of a new runtime.
Thinker K.F. Li <thinker@branda.to>
parents: 546
diff changeset
408 }