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