annotate nodejs/observer.cc @ 679:799c852b9065

Extract event info from C object to JS object
author Thinker K.F. Li <thinker@branda.to>
date Fri, 06 Aug 2010 19:31:37 +0800
parents f264b50c469c
children ddce24029561
rev   line source
651
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 #include <v8.h>
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 #include "mbfly_njs.h"
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4 extern "C" {
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 #include <mb.h>
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6 #include <string.h>
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7 }
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9 #ifndef ASSERT
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 #define ASSERT(x)
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 #endif
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 using namespace v8;
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 struct xnjsmb_observer_data {
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 Persistent<Function> func;
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 };
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 static void
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 event_handler(event_t *evt, void *arg);
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21
679
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
22 static void
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
23 xnjsmb_event_mod(Handle<Object> self, event_t *evt) {
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
24 mouse_event_t *mevt;
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
25 X_kb_event_t *xkbevt;
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
26
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
27 switch(evt->type) {
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
28 case EVT_ANY:
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
29 case EVT_MOUSE_OVER:
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
30 case EVT_MOUSE_OUT:
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
31 case EVT_MOUSE_MOVE:
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
32 case EVT_MOUSE_BUT_PRESS:
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
33 case EVT_MOUSE_BUT_RELEASE:
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
34 mevt = (mouse_event_t *)evt;
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
35 SET(self, "x", Integer::New(mevt->x));
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
36 SET(self, "y", Integer::New(mevt->y));
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
37 SET(self, "but_state", Integer::New(mevt->but_state));
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
38 SET(self, "button", Integer::New(mevt->button));
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
39 break;
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
40
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
41 case EVT_KB_PRESS:
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
42 case EVT_KB_RELEASE:
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
43 xkbevt = (X_kb_event_t *)evt;
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
44 SET(self, "keycode", Integer::New(xkbevt->keycode));
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
45 SET(self, "sym", Integer::New(xkbevt->sym));
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
46 break;
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
47
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
48 case EVT_PROGM_COMPLETE:
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
49 case EVT_RDMAN_REDRAW:
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
50 case EVT_MONITOR_ADD:
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
51 case EVT_MONITOR_REMOVE:
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
52 case EVT_MONITOR_FREE:
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
53 case EVT_MOUSE_MOVE_RAW:
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
54 default:
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
55 /* Not implemented. Do nothing. */
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
56 break;
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
57 }
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
58 }
799c852b9065 Extract event info from C object to JS object
Thinker K.F. Li <thinker@branda.to>
parents: 676
diff changeset
59
651
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 static observer_t *
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 _subject_add_event_observer(subject_t *subject, int type,
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 Handle<Function> func) {
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 observer_t *observer;
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 xnjsmb_observer_data *data;
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 data = new xnjsmb_observer_data;
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 if(data == NULL)
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 return NULL;
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 data->func = Persistent<Function>::New(func);
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 observer = subject_add_event_observer(subject, type,
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 event_handler,
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 data);
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 return observer;
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 }
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77 static void
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 _subject_remove_observer(subject_t *subject, observer_t *observer) {
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 xnjsmb_observer_data *data;
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 subject_remove_observer(subject, observer);
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 data = (xnjsmb_observer_data *)observer->arg;
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 delete data;
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 }
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85
675
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
86 static Handle<Value>
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
87 xnjsmb_event_tgt_getter(Handle<Object> self, event_t *evt, const char **err) {
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
88 Persistent<Object> *hdl;
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
89
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
90 hdl = (Persistent<Object> *)
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
91 mb_prop_get(&((mb_obj_t *)evt->tgt)->props,
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
92 PROP_JSOBJ);
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
93 return Local<Object>::New(*hdl);
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
94 }
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
95
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
96 static void
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
97 xnjsmb_event_tgt_setter(Handle<Object> self, event_t *evt,
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
98 Handle<Value> value, const char **err) {
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
99 *err = "Not implemented";
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
100 }
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
101
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
102 static Handle<Value>
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
103 xnjsmb_event_cur_tgt_getter(Handle<Object> self, event_t *evt,
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
104 const char **err) {
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
105 Persistent<Object> *hdl;
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
106
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
107 hdl = (Persistent<Object> *)
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
108 mb_prop_get(&((mb_obj_t *)evt->cur_tgt)->props,
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
109 PROP_JSOBJ);
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
110 return Local<Object>::New(*hdl);
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
111 }
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
112
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
113 static void
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
114 xnjsmb_event_cur_tgt_setter(Handle<Object> self, event_t *evt,
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
115 Handle<Value> value, const char **err) {
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
116 *err = "Not implemented";
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
117 }
c643af2095c5 Keep and retrieve respective js object to/from property store
Thinker K.F. Li <thinker@branda.to>
parents: 671
diff changeset
118
655
d58251f1bf0b Add doc
Thinker K.F. Li <thinker@branda.to>
parents: 651
diff changeset
119 /* This is the part of the code generated by gen_v8_binding.m4 */
651
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120 #include "observer-inc.h"
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
121
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
122 static void
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
123 event_handler(event_t *evt, void *arg) {
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
124 xnjsmb_observer_data *data = (xnjsmb_observer_data *)arg;
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
125 Handle<Value> evt_obj;
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
126 Handle<Value> func_args[1];
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
127
656
38c720272dd5 Change prefix for the code generated by generator
Thinker K.F. Li <thinker@branda.to>
parents: 655
diff changeset
128 evt_obj = xnjsmb_auto_event_new(evt);
651
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
129 ASSERT(!evt_obj.IsEmpty());
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
130 func_args[0] = evt_obj;
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
131 data->func->Call(Context::GetCurrent()->Global(), 1, func_args);
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
132 }
aa52883534fc Code generated by generator can be built
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
133
676
f264b50c469c Bind subject of mouse_event to JS object of coord
Thinker K.F. Li <thinker@branda.to>
parents: 675
diff changeset
134 Handle<Value>
f264b50c469c Bind subject of mouse_event to JS object of coord
Thinker K.F. Li <thinker@branda.to>
parents: 675
diff changeset
135 export_xnjsmb_auto_subject_new(subject_t *subject) {
f264b50c469c Bind subject of mouse_event to JS object of coord
Thinker K.F. Li <thinker@branda.to>
parents: 675
diff changeset
136 Handle<Value> val;
f264b50c469c Bind subject of mouse_event to JS object of coord
Thinker K.F. Li <thinker@branda.to>
parents: 675
diff changeset
137
f264b50c469c Bind subject of mouse_event to JS object of coord
Thinker K.F. Li <thinker@branda.to>
parents: 675
diff changeset
138 val = xnjsmb_auto_subject_new(subject);
f264b50c469c Bind subject of mouse_event to JS object of coord
Thinker K.F. Li <thinker@branda.to>
parents: 675
diff changeset
139 return val;
f264b50c469c Bind subject of mouse_event to JS object of coord
Thinker K.F. Li <thinker@branda.to>
parents: 675
diff changeset
140 }
f264b50c469c Bind subject of mouse_event to JS object of coord
Thinker K.F. Li <thinker@branda.to>
parents: 675
diff changeset
141
658
62c2de99b03a Remove get_object() method for subject for js
Thinker K.F. Li <thinker@branda.to>
parents: 656
diff changeset
142 void
62c2de99b03a Remove get_object() method for subject for js
Thinker K.F. Li <thinker@branda.to>
parents: 656
diff changeset
143 xnjsmb_observer_init(void) {
671
fc29a343ce7c Track JS object with property store of objects
Thinker K.F. Li <thinker@branda.to>
parents: 658
diff changeset
144 xnjsmb_auto_observer_init();
fc29a343ce7c Track JS object with property store of objects
Thinker K.F. Li <thinker@branda.to>
parents: 658
diff changeset
145 xnjsmb_auto_subject_init();
fc29a343ce7c Track JS object with property store of objects
Thinker K.F. Li <thinker@branda.to>
parents: 658
diff changeset
146 xnjsmb_auto_event_init();
658
62c2de99b03a Remove get_object() method for subject for js
Thinker K.F. Li <thinker@branda.to>
parents: 656
diff changeset
147 }