Implemented the KEY_DOWN and KEY_UP events.
[ruby-ecore.git] / src / ecore_x / rb_ecore_x.c
1 /*
2  * $Id: rb_ecore_x.c 89 2004-08-22 15:12:39Z tilman $
3  *
4  * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ruby.h>
22
23 #include <Ecore.h>
24 #include <Ecore_X.h>
25
26 #include "../ecore/rb_ecore.h"
27 #include "../ecore/rb_event_handler.h"
28 #include "rb_ecore_x.h"
29 #include "rb_window.h"
30
31 #define DEF_CONST(mod, prefix, name) \
32         rb_define_const ((mod), #name, \
33                          INT2FIX (prefix##name));
34
35 static VALUE default_root;
36
37 static void at_exit ()
38 {
39         ecore_x_shutdown ();
40         ecore_shutdown ();
41 }
42
43 static VALUE m_default_root_window_get (VALUE self)
44 {
45         return default_root;
46 }
47
48 static VALUE c_ev_key_down_init (VALUE self, VALUE event)
49 {
50         VALUE c = CLASS_OF (self);
51         Ecore_X_Event_Key_Down *e = (void *) event;
52
53         rb_define_attr (c, "keyname", 1, 0);
54         rb_define_attr (c, "keysymbol", 1, 0);
55         rb_define_attr (c, "key_compose", 1, 0);
56         rb_define_attr (c, "modifiers", 1, 0);
57         rb_define_attr (c, "window", 1, 0);
58         rb_define_attr (c, "event_window", 1, 0);
59         rb_define_attr (c, "time", 1, 0);
60
61         rb_iv_set (self, "@keyname", rb_str_new2 (e->keyname));
62         rb_iv_set (self, "@keysymbol", rb_str_new2 (e->keysymbol));
63         rb_iv_set (self, "@key_compose", rb_str_new2 (e->key_compose));
64         rb_iv_set (self, "@modifiers", INT2FIX (e->modifiers));
65         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
66         rb_iv_set (self, "@event_window",
67                    TO_ECORE_X_WINDOW (Qnil, e->event_win));
68         rb_iv_set (self, "@time", UINT2NUM (e->time));
69
70         return self;
71 }
72
73 static VALUE c_ev_mouse_move_init (VALUE self, VALUE event)
74 {
75         VALUE c = CLASS_OF (self);
76         Ecore_X_Event_Mouse_Move *e = (void *) event;
77
78         rb_define_attr (c, "modifiers", 1, 0);
79         rb_define_attr (c, "x", 1, 0);
80         rb_define_attr (c, "y", 1, 0);
81         rb_define_attr (c, "root", 1, 0);
82         rb_define_attr (c, "window", 1, 0);
83         rb_define_attr (c, "event_window", 1, 0);
84         rb_define_attr (c, "time", 1, 0);
85
86         rb_iv_set (self, "@modifiers", INT2FIX (e->modifiers));
87         rb_iv_set (self, "@x", INT2FIX (e->x));
88         rb_iv_set (self, "@y", INT2FIX (e->y));
89         rb_iv_set (self, "@root", rb_ary_new3 (2, INT2FIX (e->root.x),
90                                                INT2FIX (e->root.y)));
91         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
92         rb_iv_set (self, "@event_window",
93                    TO_ECORE_X_WINDOW (Qnil, e->event_win));
94         rb_iv_set (self, "@time", UINT2NUM (e->time));
95
96         return self;
97 }
98
99 static VALUE c_ev_mouse_in_init (VALUE self, VALUE event)
100 {
101         VALUE c = CLASS_OF (self);
102         Ecore_X_Event_Mouse_In *e = (void *) event;
103
104         rb_define_attr (c, "modifiers", 1, 0);
105         rb_define_attr (c, "x", 1, 0);
106         rb_define_attr (c, "y", 1, 0);
107         rb_define_attr (c, "root", 1, 0);
108         rb_define_attr (c, "window", 1, 0);
109         rb_define_attr (c, "event_window", 1, 0);
110         rb_define_attr (c, "mode", 1, 0);
111         rb_define_attr (c, "detail", 1, 0);
112         rb_define_attr (c, "time", 1, 0);
113
114         rb_iv_set (self, "@modifiers", INT2FIX (e->modifiers));
115         rb_iv_set (self, "@x", INT2FIX (e->x));
116         rb_iv_set (self, "@y", INT2FIX (e->y));
117         rb_iv_set (self, "@root", rb_ary_new3 (2, INT2FIX (e->root.x),
118                                                INT2FIX (e->root.y)));
119         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
120         rb_iv_set (self, "@event_window",
121                    TO_ECORE_X_WINDOW (Qnil, e->event_win));
122         rb_iv_set (self, "@mode", INT2FIX (e->mode));
123         rb_iv_set (self, "@detail", INT2FIX (e->detail));
124         rb_iv_set (self, "@time", UINT2NUM (e->time));
125
126         return self;
127 }
128
129 static VALUE c_ev_win_focus_change_init (VALUE self, VALUE event)
130 {
131         VALUE c = CLASS_OF (self);
132         Ecore_X_Event_Window_Focus_In *e = (void *) event;
133
134         rb_define_attr (c, "window", 1, 0);
135         rb_define_attr (c, "mode", 1, 0);
136         rb_define_attr (c, "detail", 1, 0);
137         rb_define_attr (c, "time", 1, 0);
138
139         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
140         rb_iv_set (self, "@mode", INT2FIX (e->mode));
141         rb_iv_set (self, "@detail", INT2FIX (e->detail));
142         rb_iv_set (self, "@time", UINT2NUM (e->time));
143
144         return self;
145 }
146
147 static VALUE c_ev_win_delete_request_init (VALUE self, VALUE event)
148 {
149         VALUE c = CLASS_OF (self);
150         Ecore_X_Event_Window_Delete_Request *e = (void *) event;
151
152         rb_define_attr (c, "window", 1, 0);
153         rb_define_attr (c, "time", 1, 0);
154
155         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
156         rb_iv_set (self, "@time", UINT2NUM (e->time));
157
158         return self;
159 }
160
161 static VALUE c_ev_win_configure_init (VALUE self, VALUE event)
162 {
163         VALUE c = CLASS_OF (self);
164         Ecore_X_Event_Window_Configure *e = (void *) event;
165
166         rb_define_attr (c, "window", 1, 0);
167         rb_define_attr (c, "window_above", 1, 0);
168         rb_define_attr (c, "x", 1, 0);
169         rb_define_attr (c, "y", 1, 0);
170         rb_define_attr (c, "w", 1, 0);
171         rb_define_attr (c, "h", 1, 0);
172         rb_define_attr (c, "border", 1, 0);
173         rb_define_attr (c, "override", 1, 0);
174         rb_define_attr (c, "from_wm", 1, 0);
175         rb_define_attr (c, "time", 1, 0);
176
177         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
178         rb_iv_set (self, "@window_above", TO_ECORE_X_WINDOW (Qnil,
179                                                              e->abovewin));
180         rb_iv_set (self, "@x", INT2FIX (e->x));
181         rb_iv_set (self, "@y", INT2FIX (e->y));
182         rb_iv_set (self, "@w", INT2FIX (e->w));
183         rb_iv_set (self, "@h", INT2FIX (e->h));
184         rb_iv_set (self, "@border", INT2FIX (e->border));
185         rb_iv_set (self, "@override", e->override ? Qtrue : Qfalse);
186         rb_iv_set (self, "@from_wm", e->from_wm ? Qtrue : Qfalse);
187         rb_iv_set (self, "@time", UINT2NUM (e->time));
188
189         return self;
190 }
191
192 static VALUE c_ev_win_visibility_change_init (VALUE self, VALUE event)
193 {
194         VALUE c = CLASS_OF (self);
195         Ecore_X_Event_Window_Visibility_Change *e = (void *) event;
196
197         rb_define_attr (c, "window", 1, 0);
198         rb_define_attr (c, "fully_obscured", 1, 0);
199         rb_define_attr (c, "time", 1, 0);
200
201         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
202         rb_iv_set (self, "@fully_obscured", e->fully_obscured ? Qtrue : Qfalse);
203         rb_iv_set (self, "@time", UINT2NUM (e->time));
204
205         return self;
206 }
207
208 static VALUE c_ev_win_create_init (VALUE self, VALUE event)
209 {
210         VALUE c = CLASS_OF (self);
211         Ecore_X_Event_Window_Create *e = (void *) event;
212
213         rb_define_attr (c, "window", 1, 0);
214         rb_define_attr (c, "override", 1, 0);
215         rb_define_attr (c, "time", 1, 0);
216
217         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
218         rb_iv_set (self, "@override", e->override ? Qtrue : Qfalse);
219         rb_iv_set (self, "@time", UINT2NUM (e->time));
220
221         return self;
222 }
223
224 static VALUE c_ev_win_show_request_init (VALUE self, VALUE event)
225 {
226         VALUE c = CLASS_OF (self);
227         Ecore_X_Event_Window_Show_Request *e = (void *) event;
228
229         rb_define_attr (c, "window", 1, 0);
230         rb_define_attr (c, "parent", 1, 0);
231         rb_define_attr (c, "time", 1, 0);
232
233         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
234         rb_iv_set (self, "@parent", TO_ECORE_X_WINDOW (Qnil, e->parent));
235         rb_iv_set (self, "@time", UINT2NUM (e->time));
236
237         return self;
238 }
239
240 void Init_ecore_x (void)
241 {
242         VALUE c;
243
244         rb_require ("ecore");
245
246         /* we need to call ecore_x_init () here, to make sure the
247          * event ids are set properly
248          */
249         ecore_init ();
250         ecore_x_init (getenv ("DISPLAY"));
251         atexit (at_exit);
252
253         mX = rb_define_module_under (mEcore, "X");
254         rb_define_module_function (mX, "default_root_window",
255                                    m_default_root_window_get, 0);
256
257         Init_Window ();
258
259         /* now create the default root window object */
260         default_root = TO_ECORE_X_WINDOW (Qnil, 0);
261         OBJ_FREEZE (default_root);
262         rb_global_variable (&default_root);
263
264         /* event mask values */
265         c = rb_define_class_under (mX, "EventMask", rb_cObject);
266         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
267         DEF_CONST (c, ECORE_X_EVENT_MASK_, KEY_DOWN);
268         DEF_CONST (c, ECORE_X_EVENT_MASK_, KEY_UP);
269         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_DOWN);
270         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_UP);
271         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_IN);
272         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_OUT);
273         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_MOVE);
274         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_DAMAGE);
275         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_VISIBILITY);
276         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_CONFIGURE);
277         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_RESIZE_MANAGE);
278         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_MANAGE);
279         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_CHILD_CONFIGURE);
280         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_FOCUS_CHANGE);
281         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_PROPERTY);
282         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_COLORMAP);
283         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_GRAB);
284         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_WHEEL);
285         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_FOCUS_IN);
286         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_FOCUS_OUT);
287
288         /* wm protocols */
289         c = rb_define_class_under (mX, "WmProtocol", rb_cObject);
290         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
291         DEF_CONST (c, ECORE_X_WM_PROTOCOL_, DELETE_REQUEST);
292         DEF_CONST (c, ECORE_X_WM_PROTOCOL_, TAKE_FOCUS);
293
294         /* event modes */
295         c = rb_define_class_under (mX, "EventMode", rb_cObject);
296         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
297         DEF_CONST (c, ECORE_X_EVENT_MODE_, NORMAL);
298         DEF_CONST (c, ECORE_X_EVENT_MODE_, WHILE_GRABBED);
299         DEF_CONST (c, ECORE_X_EVENT_MODE_, GRAB);
300         DEF_CONST (c, ECORE_X_EVENT_MODE_, UNGRAB);
301
302         /* event details */
303         c = rb_define_class_under (mX, "EventDetails", rb_cObject);
304         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
305         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, ANCESTOR);
306         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, VIRTUAL);
307         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, INFERIOR);
308         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, NON_LINEAR);
309         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, NON_LINEAR_VIRTUAL);
310         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, POINTER);
311         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, POINTER_ROOT);
312         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, DETAIL_NONE);
313
314         /* events */
315         ADD_EVENT (mX, ECORE_X_EVENT_, KEY_DOWN, "KeyDown", c);
316         rb_define_private_method (c, "initialize",
317                                   c_ev_key_down_init, 1);
318
319         ADD_EVENT (mX, ECORE_X_EVENT_, KEY_UP, "KeyUp", c);
320         rb_define_private_method (c, "initialize",
321                                   c_ev_key_down_init, 1);
322
323         ADD_EVENT (mX, ECORE_X_EVENT_, MOUSE_MOVE, "MouseMove", c);
324         rb_define_private_method (c, "initialize",
325                                   c_ev_mouse_move_init, 1);
326
327         ADD_EVENT (mX, ECORE_X_EVENT_, MOUSE_IN, "MouseIn", c);
328         rb_define_private_method (c, "initialize",
329                                   c_ev_mouse_in_init, 1);
330
331         ADD_EVENT (mX, ECORE_X_EVENT_, MOUSE_OUT, "MouseOut", c);
332         rb_define_private_method (c, "initialize",
333                                   c_ev_mouse_in_init, 1);
334
335         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_FOCUS_IN,
336                    "WindowFocusIn", c);
337         rb_define_private_method (c, "initialize",
338                                   c_ev_win_focus_change_init, 1);
339
340         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_FOCUS_OUT,
341                    "WindowFocusOut", c);
342         rb_define_private_method (c, "initialize",
343                                   c_ev_win_focus_change_init, 1);
344
345         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_DELETE_REQUEST,
346                    "WindowDeleteRequest", c);
347         rb_define_private_method (c, "initialize",
348                                   c_ev_win_delete_request_init, 1);
349
350         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_CONFIGURE_REQUEST,
351                    "WindowConfigureRequest", c);
352         rb_define_private_method (c, "initialize",
353                                   c_ev_win_delete_request_init, 1);
354
355         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_CONFIGURE,
356                    "WindowConfigure", c);
357         rb_define_private_method (c, "initialize",
358                                   c_ev_win_configure_init, 1);
359
360         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_VISIBILITY_CHANGE,
361                    "WindowVisibilityChange", c);
362         rb_define_private_method (c, "initialize",
363                                   c_ev_win_visibility_change_init, 1);
364
365         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_CREATE, "WindowCreate", c);
366         rb_define_private_method (c, "initialize",
367                                   c_ev_win_create_init, 1);
368
369         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_DESTROY, "WindowDestroy", c);
370         rb_define_private_method (c, "initialize",
371                                   c_ev_win_delete_request_init, 1);
372
373         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_SHOW, "WindowShow", c);
374         rb_define_private_method (c, "initialize",
375                                   c_ev_win_delete_request_init, 1);
376
377         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_HIDE, "WindowHide", c);
378         rb_define_private_method (c, "initialize",
379                                   c_ev_win_delete_request_init, 1);
380
381         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_SHOW_REQUEST,
382                    "WindowShowRequest", c);
383         rb_define_private_method (c, "initialize",
384                                   c_ev_win_show_request_init, 1);
385 }