Implemented get_protocol and the 'sticky' property.
[ruby-ecore.git] / src / ecore_x / rb_ecore_x.c
1 /*
2  * $Id: rb_ecore_x.c 91 2004-08-22 23:06:05Z 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",
62                    e->keyname ?  rb_str_new2 (e->keyname) : Qnil);
63         rb_iv_set (self, "@keysymbol",
64                    e->keysymbol ? rb_str_new2 (e->keysymbol) : Qnil);
65         rb_iv_set (self, "@key_compose",
66                    e->key_compose ? rb_str_new2 (e->key_compose) : Qnil);
67         rb_iv_set (self, "@modifiers", INT2FIX (e->modifiers));
68         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
69         rb_iv_set (self, "@event_window",
70                    TO_ECORE_X_WINDOW (Qnil, e->event_win));
71         rb_iv_set (self, "@time", UINT2NUM (e->time));
72
73         return self;
74 }
75
76 static VALUE c_ev_mouse_button_down_init (VALUE self, VALUE event)
77 {
78         VALUE c = CLASS_OF (self);
79         Ecore_X_Event_Mouse_Button_Down *e = (void *) event;
80
81         rb_define_attr (c, "button", 1, 0);
82         rb_define_attr (c, "modifiers", 1, 0);
83         rb_define_attr (c, "x", 1, 0);
84         rb_define_attr (c, "y", 1, 0);
85         rb_define_attr (c, "root", 1, 0);
86         rb_define_attr (c, "window", 1, 0);
87         rb_define_attr (c, "event_window", 1, 0);
88         rb_define_attr (c, "time", 1, 0);
89         rb_define_attr (c, "double_click", 1, 0);
90         rb_define_attr (c, "triple_click", 1, 0);
91
92         rb_iv_set (self, "@button", INT2FIX (e->button));
93         rb_iv_set (self, "@modifiers", INT2FIX (e->modifiers));
94         rb_iv_set (self, "@x", INT2FIX (e->x));
95         rb_iv_set (self, "@y", INT2FIX (e->x));
96         rb_iv_set (self, "@root", rb_ary_new3 (2, INT2FIX (e->root.x),
97                                                INT2FIX (e->root.y)));
98         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
99         rb_iv_set (self, "@event_window",
100                    TO_ECORE_X_WINDOW (Qnil, e->event_win));
101         rb_iv_set (self, "@time", UINT2NUM (e->time));
102         rb_iv_set (self, "@double_click",
103                    e->double_click ? Qtrue : Qfalse);
104         rb_iv_set (self, "@triple_click",
105                    e->triple_click ? Qtrue : Qfalse);
106
107         return self;
108 }
109
110 static VALUE c_ev_mouse_button_up_init (VALUE self, VALUE event)
111 {
112         VALUE c = CLASS_OF (self);
113         Ecore_X_Event_Mouse_Button_Up *e = (void *) event;
114
115         rb_define_attr (c, "button", 1, 0);
116         rb_define_attr (c, "modifiers", 1, 0);
117         rb_define_attr (c, "x", 1, 0);
118         rb_define_attr (c, "y", 1, 0);
119         rb_define_attr (c, "root", 1, 0);
120         rb_define_attr (c, "window", 1, 0);
121         rb_define_attr (c, "event_window", 1, 0);
122         rb_define_attr (c, "time", 1, 0);
123
124         rb_iv_set (self, "@button", INT2FIX (e->button));
125         rb_iv_set (self, "@modifiers", INT2FIX (e->modifiers));
126         rb_iv_set (self, "@x", INT2FIX (e->x));
127         rb_iv_set (self, "@y", INT2FIX (e->x));
128         rb_iv_set (self, "@root", rb_ary_new3 (2, INT2FIX (e->root.x),
129                                                INT2FIX (e->root.y)));
130         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
131         rb_iv_set (self, "@event_window",
132                    TO_ECORE_X_WINDOW (Qnil, e->event_win));
133         rb_iv_set (self, "@time", UINT2NUM (e->time));
134
135         return self;
136 }
137
138 static VALUE c_ev_mouse_move_init (VALUE self, VALUE event)
139 {
140         VALUE c = CLASS_OF (self);
141         Ecore_X_Event_Mouse_Move *e = (void *) event;
142
143         rb_define_attr (c, "modifiers", 1, 0);
144         rb_define_attr (c, "x", 1, 0);
145         rb_define_attr (c, "y", 1, 0);
146         rb_define_attr (c, "root", 1, 0);
147         rb_define_attr (c, "window", 1, 0);
148         rb_define_attr (c, "event_window", 1, 0);
149         rb_define_attr (c, "time", 1, 0);
150
151         rb_iv_set (self, "@modifiers", INT2FIX (e->modifiers));
152         rb_iv_set (self, "@x", INT2FIX (e->x));
153         rb_iv_set (self, "@y", INT2FIX (e->y));
154         rb_iv_set (self, "@root", rb_ary_new3 (2, INT2FIX (e->root.x),
155                                                INT2FIX (e->root.y)));
156         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
157         rb_iv_set (self, "@event_window",
158                    TO_ECORE_X_WINDOW (Qnil, e->event_win));
159         rb_iv_set (self, "@time", UINT2NUM (e->time));
160
161         return self;
162 }
163
164 static VALUE c_ev_mouse_in_init (VALUE self, VALUE event)
165 {
166         VALUE c = CLASS_OF (self);
167         Ecore_X_Event_Mouse_In *e = (void *) event;
168
169         rb_define_attr (c, "modifiers", 1, 0);
170         rb_define_attr (c, "x", 1, 0);
171         rb_define_attr (c, "y", 1, 0);
172         rb_define_attr (c, "root", 1, 0);
173         rb_define_attr (c, "window", 1, 0);
174         rb_define_attr (c, "event_window", 1, 0);
175         rb_define_attr (c, "mode", 1, 0);
176         rb_define_attr (c, "detail", 1, 0);
177         rb_define_attr (c, "time", 1, 0);
178
179         rb_iv_set (self, "@modifiers", INT2FIX (e->modifiers));
180         rb_iv_set (self, "@x", INT2FIX (e->x));
181         rb_iv_set (self, "@y", INT2FIX (e->y));
182         rb_iv_set (self, "@root", rb_ary_new3 (2, INT2FIX (e->root.x),
183                                                INT2FIX (e->root.y)));
184         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
185         rb_iv_set (self, "@event_window",
186                    TO_ECORE_X_WINDOW (Qnil, e->event_win));
187         rb_iv_set (self, "@mode", INT2FIX (e->mode));
188         rb_iv_set (self, "@detail", INT2FIX (e->detail));
189         rb_iv_set (self, "@time", UINT2NUM (e->time));
190
191         return self;
192 }
193
194 static VALUE c_ev_win_focus_change_init (VALUE self, VALUE event)
195 {
196         VALUE c = CLASS_OF (self);
197         Ecore_X_Event_Window_Focus_In *e = (void *) event;
198
199         rb_define_attr (c, "window", 1, 0);
200         rb_define_attr (c, "mode", 1, 0);
201         rb_define_attr (c, "detail", 1, 0);
202         rb_define_attr (c, "time", 1, 0);
203
204         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
205         rb_iv_set (self, "@mode", INT2FIX (e->mode));
206         rb_iv_set (self, "@detail", INT2FIX (e->detail));
207         rb_iv_set (self, "@time", UINT2NUM (e->time));
208
209         return self;
210 }
211
212 static VALUE c_ev_win_delete_request_init (VALUE self, VALUE event)
213 {
214         VALUE c = CLASS_OF (self);
215         Ecore_X_Event_Window_Delete_Request *e = (void *) event;
216
217         rb_define_attr (c, "window", 1, 0);
218         rb_define_attr (c, "time", 1, 0);
219
220         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
221         rb_iv_set (self, "@time", UINT2NUM (e->time));
222
223         return self;
224 }
225
226 static VALUE c_ev_win_configure_init (VALUE self, VALUE event)
227 {
228         VALUE c = CLASS_OF (self);
229         Ecore_X_Event_Window_Configure *e = (void *) event;
230
231         rb_define_attr (c, "window", 1, 0);
232         rb_define_attr (c, "window_above", 1, 0);
233         rb_define_attr (c, "x", 1, 0);
234         rb_define_attr (c, "y", 1, 0);
235         rb_define_attr (c, "w", 1, 0);
236         rb_define_attr (c, "h", 1, 0);
237         rb_define_attr (c, "border", 1, 0);
238         rb_define_attr (c, "override", 1, 0);
239         rb_define_attr (c, "from_wm", 1, 0);
240         rb_define_attr (c, "time", 1, 0);
241
242         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
243         rb_iv_set (self, "@window_above", TO_ECORE_X_WINDOW (Qnil,
244                                                              e->abovewin));
245         rb_iv_set (self, "@x", INT2FIX (e->x));
246         rb_iv_set (self, "@y", INT2FIX (e->y));
247         rb_iv_set (self, "@w", INT2FIX (e->w));
248         rb_iv_set (self, "@h", INT2FIX (e->h));
249         rb_iv_set (self, "@border", INT2FIX (e->border));
250         rb_iv_set (self, "@override", e->override ? Qtrue : Qfalse);
251         rb_iv_set (self, "@from_wm", e->from_wm ? Qtrue : Qfalse);
252         rb_iv_set (self, "@time", UINT2NUM (e->time));
253
254         return self;
255 }
256
257 static VALUE c_ev_win_visibility_change_init (VALUE self, VALUE event)
258 {
259         VALUE c = CLASS_OF (self);
260         Ecore_X_Event_Window_Visibility_Change *e = (void *) event;
261
262         rb_define_attr (c, "window", 1, 0);
263         rb_define_attr (c, "fully_obscured", 1, 0);
264         rb_define_attr (c, "time", 1, 0);
265
266         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
267         rb_iv_set (self, "@fully_obscured", e->fully_obscured ? Qtrue : Qfalse);
268         rb_iv_set (self, "@time", UINT2NUM (e->time));
269
270         return self;
271 }
272
273 static VALUE c_ev_win_create_init (VALUE self, VALUE event)
274 {
275         VALUE c = CLASS_OF (self);
276         Ecore_X_Event_Window_Create *e = (void *) event;
277
278         rb_define_attr (c, "window", 1, 0);
279         rb_define_attr (c, "override", 1, 0);
280         rb_define_attr (c, "time", 1, 0);
281
282         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
283         rb_iv_set (self, "@override", e->override ? Qtrue : Qfalse);
284         rb_iv_set (self, "@time", UINT2NUM (e->time));
285
286         return self;
287 }
288
289 static VALUE c_ev_win_show_request_init (VALUE self, VALUE event)
290 {
291         VALUE c = CLASS_OF (self);
292         Ecore_X_Event_Window_Show_Request *e = (void *) event;
293
294         rb_define_attr (c, "window", 1, 0);
295         rb_define_attr (c, "parent", 1, 0);
296         rb_define_attr (c, "time", 1, 0);
297
298         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
299         rb_iv_set (self, "@parent", TO_ECORE_X_WINDOW (Qnil, e->parent));
300         rb_iv_set (self, "@time", UINT2NUM (e->time));
301
302         return self;
303 }
304
305 void Init_ecore_x (void)
306 {
307         VALUE c;
308
309         rb_require ("ecore");
310
311         /* we need to call ecore_x_init () here, to make sure the
312          * event ids are set properly
313          */
314         ecore_init ();
315         ecore_x_init (getenv ("DISPLAY"));
316         atexit (at_exit);
317
318         mX = rb_define_module_under (mEcore, "X");
319         rb_define_module_function (mX, "default_root_window",
320                                    m_default_root_window_get, 0);
321
322         Init_Window ();
323
324         /* now create the default root window object */
325         default_root = TO_ECORE_X_WINDOW (Qnil, 0);
326         OBJ_FREEZE (default_root);
327         rb_global_variable (&default_root);
328
329         /* event mask values */
330         c = rb_define_class_under (mX, "EventMask", rb_cObject);
331         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
332         DEF_CONST (c, ECORE_X_EVENT_MASK_, KEY_DOWN);
333         DEF_CONST (c, ECORE_X_EVENT_MASK_, KEY_UP);
334         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_DOWN);
335         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_UP);
336         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_IN);
337         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_OUT);
338         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_MOVE);
339         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_DAMAGE);
340         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_VISIBILITY);
341         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_CONFIGURE);
342         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_RESIZE_MANAGE);
343         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_MANAGE);
344         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_CHILD_CONFIGURE);
345         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_FOCUS_CHANGE);
346         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_PROPERTY);
347         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_COLORMAP);
348         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_GRAB);
349         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_WHEEL);
350         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_FOCUS_IN);
351         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_FOCUS_OUT);
352
353         /* wm protocols */
354         c = rb_define_class_under (mX, "WmProtocol", rb_cObject);
355         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
356         DEF_CONST (c, ECORE_X_WM_PROTOCOL_, DELETE_REQUEST);
357         DEF_CONST (c, ECORE_X_WM_PROTOCOL_, TAKE_FOCUS);
358
359         /* event modes */
360         c = rb_define_class_under (mX, "EventMode", rb_cObject);
361         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
362         DEF_CONST (c, ECORE_X_EVENT_MODE_, NORMAL);
363         DEF_CONST (c, ECORE_X_EVENT_MODE_, WHILE_GRABBED);
364         DEF_CONST (c, ECORE_X_EVENT_MODE_, GRAB);
365         DEF_CONST (c, ECORE_X_EVENT_MODE_, UNGRAB);
366
367         /* event details */
368         c = rb_define_class_under (mX, "EventDetail", rb_cObject);
369         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
370         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, ANCESTOR);
371         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, VIRTUAL);
372         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, INFERIOR);
373         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, NON_LINEAR);
374         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, NON_LINEAR_VIRTUAL);
375         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, POINTER);
376         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, POINTER_ROOT);
377         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, DETAIL_NONE);
378
379         /* key modifiers */
380         c = rb_define_class_under (mX, "Modifier", rb_cObject);
381         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
382         DEF_CONST (c, ECORE_X_MODIFIER_, SHIFT);
383         DEF_CONST (c, ECORE_X_MODIFIER_, CTRL);
384         DEF_CONST (c, ECORE_X_MODIFIER_, ALT);
385         DEF_CONST (c, ECORE_X_MODIFIER_, WIN);
386
387         /* events */
388         ADD_EVENT (mX, ECORE_X_EVENT_, KEY_DOWN, "KeyDown", c);
389         rb_define_private_method (c, "initialize",
390                                   c_ev_key_down_init, 1);
391
392         ADD_EVENT (mX, ECORE_X_EVENT_, KEY_UP, "KeyUp", c);
393         rb_define_private_method (c, "initialize",
394                                   c_ev_key_down_init, 1);
395
396         ADD_EVENT (mX, ECORE_X_EVENT_, MOUSE_BUTTON_DOWN,
397                    "MouseButtonDown", c);
398         rb_define_private_method (c, "initialize",
399                                   c_ev_mouse_button_down_init, 1);
400
401         ADD_EVENT (mX, ECORE_X_EVENT_, MOUSE_BUTTON_UP,
402                    "MouseButtonUp", c);
403         rb_define_private_method (c, "initialize",
404                                   c_ev_mouse_button_up_init, 1);
405
406         ADD_EVENT (mX, ECORE_X_EVENT_, MOUSE_MOVE, "MouseMove", c);
407         rb_define_private_method (c, "initialize",
408                                   c_ev_mouse_move_init, 1);
409
410         ADD_EVENT (mX, ECORE_X_EVENT_, MOUSE_IN, "MouseIn", c);
411         rb_define_private_method (c, "initialize",
412                                   c_ev_mouse_in_init, 1);
413
414         ADD_EVENT (mX, ECORE_X_EVENT_, MOUSE_OUT, "MouseOut", c);
415         rb_define_private_method (c, "initialize",
416                                   c_ev_mouse_in_init, 1);
417
418         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_FOCUS_IN,
419                    "WindowFocusIn", c);
420         rb_define_private_method (c, "initialize",
421                                   c_ev_win_focus_change_init, 1);
422
423         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_FOCUS_OUT,
424                    "WindowFocusOut", c);
425         rb_define_private_method (c, "initialize",
426                                   c_ev_win_focus_change_init, 1);
427
428         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_DELETE_REQUEST,
429                    "WindowDeleteRequest", c);
430         rb_define_private_method (c, "initialize",
431                                   c_ev_win_delete_request_init, 1);
432
433         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_CONFIGURE_REQUEST,
434                    "WindowConfigureRequest", c);
435         rb_define_private_method (c, "initialize",
436                                   c_ev_win_delete_request_init, 1);
437
438         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_CONFIGURE,
439                    "WindowConfigure", c);
440         rb_define_private_method (c, "initialize",
441                                   c_ev_win_configure_init, 1);
442
443         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_VISIBILITY_CHANGE,
444                    "WindowVisibilityChange", c);
445         rb_define_private_method (c, "initialize",
446                                   c_ev_win_visibility_change_init, 1);
447
448         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_CREATE, "WindowCreate", c);
449         rb_define_private_method (c, "initialize",
450                                   c_ev_win_create_init, 1);
451
452         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_DESTROY, "WindowDestroy", c);
453         rb_define_private_method (c, "initialize",
454                                   c_ev_win_delete_request_init, 1);
455
456         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_SHOW, "WindowShow", c);
457         rb_define_private_method (c, "initialize",
458                                   c_ev_win_delete_request_init, 1);
459
460         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_HIDE, "WindowHide", c);
461         rb_define_private_method (c, "initialize",
462                                   c_ev_win_delete_request_init, 1);
463
464         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_SHOW_REQUEST,
465                    "WindowShowRequest", c);
466         rb_define_private_method (c, "initialize",
467                                   c_ev_win_show_request_init, 1);
468 }