# HG changeset patch # User Thinker K.F. Li # Date 1275747844 -28800 # Node ID 249bcbf07eb0bfa9ce5b7823e83d2d8b511426e2 # Parent 49f8f57f184ad06df0d71d14ee713932cbf6960a Reuse and adapt X_supp.c by implmeneting X_supp_njs.c diff -r 49f8f57f184a -r 249bcbf07eb0 nodejs/Makefile.am --- a/nodejs/Makefile.am Sat Jun 05 22:23:57 2010 +0800 +++ b/nodejs/Makefile.am Sat Jun 05 22:24:04 2010 +0800 @@ -1,13 +1,18 @@ mbfly_node_SRCS = hello.cc +mbfly_node_CFLAGS= -I$(abs_top_srcdir)/include -I$(prefix)/include \ + @pangocairo_CFLAGS@ $(CFLAGS) all: mbfly.node clean: clean-mbfly-node mbfly.node: wscript $(mbfly_node_SRCS) cd $(srcdir); \ - WAFLOCK=$(abs_builddir)/.lock-wscript $(NODE_WAF) \ - configure build --blddir=$(abs_builddir) + CFLAGS="$(mbfly_node_CFLAGS)" \ + WAFLOCK=$(abs_builddir)/.lock-wscript \ + $(NODE_WAF) configure build --blddir=$(abs_builddir) clean-mbfly-node: - $(NODE_WAF) clean + cd $(srcdir); \ + WAFLOCK=$(abs_builddir)/.lock-wscript \ + $(NODE_WAF) clean diff -r 49f8f57f184a -r 249bcbf07eb0 nodejs/X_supp_njs.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nodejs/X_supp_njs.c Sat Jun 05 22:24:04 2010 +0800 @@ -0,0 +1,126 @@ +/*! \brief Implement X11 backend for nodejs plugin. + * + * Since nodejs use libev to handle event loops, part of X11 backend + * code can not be used directly. The part of code should be rewrote. + * The part is about + */ +#include +#include +#include +#include +#include "mb_X_supp.h" +#include "mb_tools.h" + +#ifndef ASSERT +#define ASSERT(x) +#endif + +typedef struct _njs_ev_data { + ev_io iowatcher; + ev_timer tmwatcher; + int enable_timer; + void *rt; +} njs_ev_data_t; + +static void timer_cb(EV_P_ ev_timer *tmwatcher, int revent); + +/*! \brief Register next timeout with libev. + */ +static void +set_next_timeout(njs_ev_data_t *ev_data) { + mb_tman_t *tman; + mb_timeval_t now, tmo; + ev_tstamp tout; + int r; + + tman = X_MB_tman(ev_data->rt); + get_now(&now); + r = mb_tman_next_timeout(tman, &now, &tmo); + if(r == 0) { + MB_TIMEVAL_DIFF(&tmo, &now); + tout = (ev_tstamp)MB_TIMEVAL_SEC(&tmo) + + (ev_tstamp)MB_TIMEVAL_USEC(&tmo) / 1000000; + ev_timer_init(&ev_data->tmwatcher, timer_cb, tout, 0); + ev_timer_start(&ev_data->tmwatcher); + ev_data->enable_timer = 1; + } else + ev_data->enable_timer = 0; +} + +static void +x_conn_cb(EV_P_ ev_io *iowatcher, int revent) { + njs_ev_data_t *ev_data = MEM2OBJ(iowatcher, njs_ev_data_t, iowatcher); + redraw_man_t *rdman; + extern void _X_MB_handle_x_event_for_nodejs(X_MB_runtime_t *rt); + + rdman = X_MB_rdman(ev_data->rt); + _X_MB_handle_x_event_for_nodejs(ev_data->rt); + rdman_redraw_changed(rdman); + + if(ev_data->enable_timer == 0) /* no installed timeout */ + set_next_timeout(ev_data); +} + +static void +timer_cb(EV_P_ ev_timer *tmwatcher, int revent) { + njs_ev_data_t *ev_data = MEM2OBJ(tmwatcher, njs_ev_data_t, tmwatcher); + mb_tman_t *tman; + redraw_man_t *rdman; + mb_timeval_t now; + extern int _X_MB_flush_x_conn_nodejs(X_MB_runtime_t *rt); + + tman = X_MB_tman(ev_data->rt); + get_now(&now); + mb_tman_handle_timeout(tman, &now); + + rdman = X_MB_rdman(ev_data->rt); + rdman_redraw_changed(rdman); + _X_MB_flush_x_conn_nodejs(ev_data->rt); + + set_next_timeout(ev_data); +} + +/*! \brief Handle connection coming data and timeout of timers. + * + * \param rt is a runtime object for X. + */ +void X_MB_handle_connection_njs(void *rt) { + X_MB_runtime_t *_rt = (X_MB_runtime_t *)rt; + njs_ev_data_t *ev_data; + mb_tman_t *tman; + mb_timeval_t now, tmo; + ev_tstamp tout; + int fd; + int r; + extern int _X_MB_get_x_conn_for_nodejs(X_MB_runtime_t *rt); + + ev_data = (njs_ev_data_t *)malloc(sizeof(njs_ev_data_t)); + ASSERT(ev_data != NULL); + + _X_MB_set_data_nodejs(_rt, ev_data); + ev_data->rt = _rt; + ev_default_loop(0); + + /* + * Setup watcher for X connection. + */ + fd = _X_MB_get_x_conn_for_nodejs(_rt); + ev_io_init(&ev_data->iowatcher, x_conn_cb, fd, EV_READ); + ev_io_start(&ev_data->iowatcher); + + set_next_timeout(ev_data); +} + +void X_MB_stop_njs(void *rt) { + X_MB_runtime_t *_rt = (X_MB_runtime_t *)rt; + njs_ev_data_t *ev_data; + + ev_data = (njs_ev_data_t *)_X_MB_get_x_conn_for_nodejs(_rt); + + /* + * stop IO and timer watcher + */ + ev_io_stop(&ev_data->iowatcher); + if(ev_data->enable_timer) + ev_timer_stop(&ev_data->tmwatcher); +} diff -r 49f8f57f184a -r 249bcbf07eb0 nodejs/mbfly_njs.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nodejs/mbfly_njs.cc Sat Jun 05 22:24:04 2010 +0800 @@ -0,0 +1,2 @@ +#include + diff -r 49f8f57f184a -r 249bcbf07eb0 nodejs/wscript --- a/nodejs/wscript Sat Jun 05 22:23:57 2010 +0800 +++ b/nodejs/wscript Sat Jun 05 22:24:04 2010 +0800 @@ -4,17 +4,25 @@ def set_options(opt): opt.tool_options('compiler_cxx') + opt.tool_options('compiler_cc') pass def configure(conf): import Options conf.check_tool('compiler_cxx') + conf.check_tool('compiler_cc') conf.check_tool('node_addon') conf.env.SRCDIR = Options.options.srcdir pass def build(conf): + import os obj = conf.new_task_gen('cxx', 'shlib', 'node_addon') obj.target = 'mbfly' - obj.source = 'hello.cc' + obj.source = 'hello.cc mbfly_njs.cc' + obj.add_objects = 'X_supp_njs.o' + + obj = conf.new_task_gen('cc', 'shlib', 'node_addon') + obj.target = 'X_supp_njs.o' + obj.source = 'X_supp_njs.c' pass