]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/mag/server/src/lua_glue.swg
update
[l4.git] / l4 / pkg / mag / server / src / lua_glue.swg
1 // vi:ft=cpp
2 /*
3  * (c) 2011 Alexander Warg <warg@os.inf.tu-dresden.de>
4  *     economic rights: Technische Universität Dresden (Germany)
5  *
6  * This file is part of TUD:OS and distributed under the terms of the
7  * GNU General Public License 2.
8  * Please see the COPYING-GPL-2 file for details.
9  */
10
11 %module Mag
12
13 %include <typemaps.i>
14 %include <lua_typemap.i>
15
16 /* typemap for our input values (supporting and invalid value as 'nil') */
17 %typemap(out) Mag_server::Value<int>
18 %{  if ($1.valid()) lua_pushinteger(L, $1.val()); else lua_pushnil(L); SWIG_arg++; %}
19
20 %typemap(in) Mag_server::Value<int>
21 %{
22   if (lua_isnil(L, $input))
23     $1 = Mag_server::Value<int>();
24   else 
25     {
26       SWIG_contract_assert(lua_isinteger(L, $input), "argument must be a number")
27       $1 = Mag_server::Valua<int>(lua_tointeger(L, $input));
28     }
29 %}
30
31 /* we report key events as pair of code and value in lua */
32 %typemap(out) Mag_server::Hid_report::Key_event const *
33 %{
34     if ($1)
35       {
36         lua_pushinteger(L, $1->code);
37         lua_pushinteger(L, $1->value);
38         SWIG_arg += 2;
39       }
40 %}
41
42
43 %{
44 #include <l4/mag/server/hid_report>
45 #include <l4/mag/server/valuator>
46 #include <l4/mag/server/user_state>
47 #include <l4/mag/server/input_source>
48 #include "axis_buf.h"
49
50 namespace Mag_server {
51 extern Mag_server::User_state *user_state;
52 }
53
54 int luaopen_Mag(lua_State*);
55
56 %}
57
58 %ignore Mag_server::View_proxy::view;
59 %ignore Mag_server::View_proxy::forget;
60 %ignore Mag_server::View;
61
62 %ignore Mag_server::User_state::User_state;
63 %ignore Mag_server::User_state::forget_view;
64 %ignore Mag_server::Mode;
65 %ignore L4Re::Event;
66 %ignore L4Re::Default_event_payload;
67 %ignore L4Re::Event_buffer_t;
68 %ignore L4Re::Event_buffer;
69
70 #pragma SWIG nowarn=401,325
71
72 #define L4_EXPORT
73 #define L4_KOBJECT(x)
74
75 %include <server/mode>
76 %include <server/valuator>
77 %apply unsigned *OUTPUT { unsigned *bus, unsigned *vendor, unsigned *product, unsigned *version };
78 %include <l4re/include/event>
79 %clear unsigned *bus, unsigned *vendor, unsigned *product, unsigned *version;
80 %include <axis_buf.h>
81
82
83 %template(Valuatori) Mag_server::Valuator<int>;
84 %template(Valuei) Mag_server::Value<int>;
85
86 namespace Mag_server {
87
88 class Axis_info
89 {
90 public:
91   int value;
92   int min;
93   int max;
94   int fuzz;
95   int flat;
96   int resolution;
97   int delta;
98   int mode;
99 };
100
101 class Axis_info_vector
102 {
103 public:
104   Axis_info_vector();
105   explicit Axis_info_vector(unsigned size);
106   ~Axis_info_vector();
107
108   unsigned size() const;
109   Axis_info const *get(unsigned idx) const;
110   Axis_info *get(unsigned idx);
111   Axis_info *create(unsigned idx);
112   %apply SWIGTYPE *DISOWN { Axis_info *info };
113   bool set(unsigned idx, Axis_info *info);
114   %clear Axis_info *info;
115 };
116
117 %extend Axis_info_vector
118 {
119   Mag_server::Axis_info *__getitem__(unsigned idx)
120   {
121     return self->get(idx);
122   }
123 };
124
125 class Hid_report
126 {
127 public:
128   struct Key_event;
129   Hid_report(l4_umword_t device_id, unsigned rels, unsigned abss, unsigned mscs,
130              unsigned sws, unsigned mts);
131
132   ~Hid_report();
133
134   bool get(unsigned char type, unsigned code, int &val) const;
135   void set(unsigned char type, unsigned code, int val);
136
137   bool mt_get(unsigned id, unsigned code, int &val) const;
138
139   void mt_set(unsigned code, int val);
140   bool submit_mt();
141
142   Valuator<int> const *get_vals(unsigned char type) const;
143   Valuator<int> *get_vals(unsigned char type);
144   Valuator<int> const *get_mt_vals(unsigned id) const;
145
146   bool add_key(int code, int value);
147   Key_event const *get_key_event(unsigned idx) const;
148   Key_event const *find_key_event(int code) const;
149   void remove_key_event(int code);
150
151   void sync(long long time);
152   long long time() const;
153   void clear();
154   l4_umword_t device_id() const;
155
156   Axis_info_vector const *abs_infos() const;
157   Axis_info_vector *abs_infos();
158   void set_abs_info(Axis_info_vector *i);
159 };
160
161 }
162
163 %extend Mag_server::Valuator<int>
164 {
165   Mag_server::Value<int> __getitem__(unsigned idx) const
166   {
167     return self->get(idx);
168   }
169 };
170
171 %extend Mag_server::Hid_report
172 {
173   Mag_server::Valuator<int> *__getitem__(unsigned char type)
174   {
175     return self->get_vals(type);
176   }
177 };
178
179 %nodefaultctor;
180 class Mag_server::User_state
181 {
182 public:
183   void set_pointer(int x, int y, bool hide);
184 };
185
186 class Mag_server::View_proxy
187 {
188 public:
189   View_proxy(Mag_server::User_state *ust);
190 };
191
192 %clearnodefaultctor;
193
194 %extend Mag_server::User_state
195 {
196   bool set_kbd_focus(Mag_server::View_proxy *vp)
197   {
198     if (!vp)
199       return self->set_focus(0);
200     else
201       return self->set_focus(vp->view());
202   }
203
204   void post_event(Mag_server::View_proxy *vp, Mag_server::Hid_report *e, bool update,
205                   bool core_ev)
206   {
207     Mag_server::View *v = vp ? vp->view() : self->kbd_focus();
208     if (update)
209       self->vstack()->update_all_views();
210
211     //maybe_screenshot(ust, e);
212
213     if (v && (!self->vstack()->mode().kill() || v->super_view()))
214       v->handle_event(e, self->mouse_pos(), core_ev);
215   }
216
217   void toggle_mode(Mag_server::Mode::Mode_flag mode)
218   {
219     self->vstack()->toggle_mode(mode);
220   }
221
222   void find_pointed_view(Mag_server::View_proxy *vp)
223   {
224     if (!vp)
225       return;
226
227     vp->view(self->vstack()->find(self->mouse_pos()));
228   }
229
230   %apply int *OUTPUT {int *w, int*h};
231   void screen_size(int *w, int *h)
232   {
233     Mag_gfx::Area s = self->vstack()->canvas()->size();
234     *w = s.w();
235     *h = s.h();
236   }
237 };
238
239 %extend Mag_server::View_proxy
240 {
241   void set(Mag_server::View_proxy *src)
242   {
243     self->view(src ? src->view() : 0);
244   }
245 };
246
247 %nodefaultctor;
248 %typemap(in) ORIG_l4_umword_t = l4_umword_t;
249 %typemap(in,checkfn="lua_isuserdata") l4_umword_t id
250 %{ $1 = (l4_umword_t)lua_touserdata(L, $input); %}
251
252 class Mag_server::Input_source
253 {};
254
255 %extend Mag_server::Input_source
256 {
257 public:
258
259   int get_stream_info(l4_umword_t id, L4Re::Event_stream_info **OUTPUT)
260   {
261     *OUTPUT = new L4Re::Event_stream_info;
262     return self->get_stream_info(id, *OUTPUT);
263   }
264
265   int get_abs_info(l4_umword_t id, unsigned axis, Mag_server::Axis_info **OUTPUT)
266   {
267     *OUTPUT = 0;
268     unsigned axes[1];
269     axes[0] = axis;
270     L4Re::Event_absinfo info[1];
271     int res = self->get_axis_info(id, 1, axes, info);
272     if (res < 0)
273       return res;
274
275     Mag_server::Axis_info *i = new Mag_server::Axis_info();
276     *OUTPUT = i;
277
278     i->value = info->value;
279     i->min = info->min;
280     i->max = info->max;
281     i->fuzz = info->fuzz;
282     i->flat = info->flat;
283     i->resolution = info->resolution;
284     i->delta = info->max - info->min;
285     i->mode = 0;
286     return res;
287   }
288   %typemap(in) l4_umword_t id = ORIG_l4_umword_t;
289 };
290 %clearnodefaultctor;
291
292 %extend L4Re::Event_stream_info
293 {
294 public:
295   void get_device_id(unsigned *bus, unsigned *vendor, unsigned *product, unsigned *version)
296   {
297     *bus = self->id.bustype;
298     *vendor = self->id.vendor;
299     *product = self->id.product;
300     *version = self->id.version;
301   }
302 };
303
304 %inline %{
305 static Mag_server::User_state *get_user_state()
306 {
307   return Mag_server::user_state;
308 }
309 %}
310
311