Added the Ecore::X::Cursor class.
[ruby-ecore.git] / src / ecore_x / rb_ecore_x.c
1 /*
2  * $Id: rb_ecore_x.c 98 2004-08-26 13:12:55Z 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 #include "rb_cursor.h"
32
33 static VALUE default_root;
34
35 static void at_exit ()
36 {
37         ecore_x_shutdown ();
38         ecore_shutdown ();
39 }
40
41 static VALUE m_default_root_window_get (VALUE self)
42 {
43         return default_root;
44 }
45
46 static VALUE c_ev_key_down_init (VALUE self, VALUE event)
47 {
48         VALUE c = CLASS_OF (self);
49         Ecore_X_Event_Key_Down *e = (void *) event;
50
51         rb_define_attr (c, "keyname", 1, 0);
52         rb_define_attr (c, "keysymbol", 1, 0);
53         rb_define_attr (c, "key_compose", 1, 0);
54         rb_define_attr (c, "modifiers", 1, 0);
55         rb_define_attr (c, "window", 1, 0);
56         rb_define_attr (c, "event_window", 1, 0);
57         rb_define_attr (c, "time", 1, 0);
58
59         rb_iv_set (self, "@keyname",
60                    e->keyname ?  rb_str_new2 (e->keyname) : Qnil);
61         rb_iv_set (self, "@keysymbol",
62                    e->keysymbol ? rb_str_new2 (e->keysymbol) : Qnil);
63         rb_iv_set (self, "@key_compose",
64                    e->key_compose ? rb_str_new2 (e->key_compose) : Qnil);
65         rb_iv_set (self, "@modifiers", INT2FIX (e->modifiers));
66         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
67         rb_iv_set (self, "@event_window",
68                    TO_ECORE_X_WINDOW (Qnil, e->event_win));
69         rb_iv_set (self, "@time", UINT2NUM (e->time));
70
71         return self;
72 }
73
74 static VALUE c_ev_mouse_button_down_init (VALUE self, VALUE event)
75 {
76         VALUE c = CLASS_OF (self);
77         Ecore_X_Event_Mouse_Button_Down *e = (void *) event;
78
79         rb_define_attr (c, "button", 1, 0);
80         rb_define_attr (c, "modifiers", 1, 0);
81         rb_define_attr (c, "x", 1, 0);
82         rb_define_attr (c, "y", 1, 0);
83         rb_define_attr (c, "root", 1, 0);
84         rb_define_attr (c, "window", 1, 0);
85         rb_define_attr (c, "event_window", 1, 0);
86         rb_define_attr (c, "time", 1, 0);
87         rb_define_attr (c, "double_click", 1, 0);
88         rb_define_attr (c, "triple_click", 1, 0);
89
90         rb_iv_set (self, "@button", INT2FIX (e->button));
91         rb_iv_set (self, "@modifiers", INT2FIX (e->modifiers));
92         rb_iv_set (self, "@x", INT2FIX (e->x));
93         rb_iv_set (self, "@y", INT2FIX (e->x));
94         rb_iv_set (self, "@root", rb_ary_new3 (2, INT2FIX (e->root.x),
95                                                INT2FIX (e->root.y)));
96         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
97         rb_iv_set (self, "@event_window",
98                    TO_ECORE_X_WINDOW (Qnil, e->event_win));
99         rb_iv_set (self, "@time", UINT2NUM (e->time));
100         rb_iv_set (self, "@double_click",
101                    e->double_click ? Qtrue : Qfalse);
102         rb_iv_set (self, "@triple_click",
103                    e->triple_click ? Qtrue : Qfalse);
104
105         return self;
106 }
107
108 static VALUE c_ev_mouse_button_up_init (VALUE self, VALUE event)
109 {
110         VALUE c = CLASS_OF (self);
111         Ecore_X_Event_Mouse_Button_Up *e = (void *) event;
112
113         rb_define_attr (c, "button", 1, 0);
114         rb_define_attr (c, "modifiers", 1, 0);
115         rb_define_attr (c, "x", 1, 0);
116         rb_define_attr (c, "y", 1, 0);
117         rb_define_attr (c, "root", 1, 0);
118         rb_define_attr (c, "window", 1, 0);
119         rb_define_attr (c, "event_window", 1, 0);
120         rb_define_attr (c, "time", 1, 0);
121
122         rb_iv_set (self, "@button", INT2FIX (e->button));
123         rb_iv_set (self, "@modifiers", INT2FIX (e->modifiers));
124         rb_iv_set (self, "@x", INT2FIX (e->x));
125         rb_iv_set (self, "@y", INT2FIX (e->x));
126         rb_iv_set (self, "@root", rb_ary_new3 (2, INT2FIX (e->root.x),
127                                                INT2FIX (e->root.y)));
128         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
129         rb_iv_set (self, "@event_window",
130                    TO_ECORE_X_WINDOW (Qnil, e->event_win));
131         rb_iv_set (self, "@time", UINT2NUM (e->time));
132
133         return self;
134 }
135
136 static VALUE c_ev_mouse_move_init (VALUE self, VALUE event)
137 {
138         VALUE c = CLASS_OF (self);
139         Ecore_X_Event_Mouse_Move *e = (void *) event;
140
141         rb_define_attr (c, "modifiers", 1, 0);
142         rb_define_attr (c, "x", 1, 0);
143         rb_define_attr (c, "y", 1, 0);
144         rb_define_attr (c, "root", 1, 0);
145         rb_define_attr (c, "window", 1, 0);
146         rb_define_attr (c, "event_window", 1, 0);
147         rb_define_attr (c, "time", 1, 0);
148
149         rb_iv_set (self, "@modifiers", INT2FIX (e->modifiers));
150         rb_iv_set (self, "@x", INT2FIX (e->x));
151         rb_iv_set (self, "@y", INT2FIX (e->y));
152         rb_iv_set (self, "@root", rb_ary_new3 (2, INT2FIX (e->root.x),
153                                                INT2FIX (e->root.y)));
154         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
155         rb_iv_set (self, "@event_window",
156                    TO_ECORE_X_WINDOW (Qnil, e->event_win));
157         rb_iv_set (self, "@time", UINT2NUM (e->time));
158
159         return self;
160 }
161
162 static VALUE c_ev_mouse_in_init (VALUE self, VALUE event)
163 {
164         VALUE c = CLASS_OF (self);
165         Ecore_X_Event_Mouse_In *e = (void *) event;
166
167         rb_define_attr (c, "modifiers", 1, 0);
168         rb_define_attr (c, "x", 1, 0);
169         rb_define_attr (c, "y", 1, 0);
170         rb_define_attr (c, "root", 1, 0);
171         rb_define_attr (c, "window", 1, 0);
172         rb_define_attr (c, "event_window", 1, 0);
173         rb_define_attr (c, "mode", 1, 0);
174         rb_define_attr (c, "detail", 1, 0);
175         rb_define_attr (c, "time", 1, 0);
176
177         rb_iv_set (self, "@modifiers", INT2FIX (e->modifiers));
178         rb_iv_set (self, "@x", INT2FIX (e->x));
179         rb_iv_set (self, "@y", INT2FIX (e->y));
180         rb_iv_set (self, "@root", rb_ary_new3 (2, INT2FIX (e->root.x),
181                                                INT2FIX (e->root.y)));
182         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
183         rb_iv_set (self, "@event_window",
184                    TO_ECORE_X_WINDOW (Qnil, e->event_win));
185         rb_iv_set (self, "@mode", INT2FIX (e->mode));
186         rb_iv_set (self, "@detail", INT2FIX (e->detail));
187         rb_iv_set (self, "@time", UINT2NUM (e->time));
188
189         return self;
190 }
191
192 static VALUE c_ev_win_focus_change_init (VALUE self, VALUE event)
193 {
194         VALUE c = CLASS_OF (self);
195         Ecore_X_Event_Window_Focus_In *e = (void *) event;
196
197         rb_define_attr (c, "window", 1, 0);
198         rb_define_attr (c, "mode", 1, 0);
199         rb_define_attr (c, "detail", 1, 0);
200         rb_define_attr (c, "time", 1, 0);
201
202         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
203         rb_iv_set (self, "@mode", INT2FIX (e->mode));
204         rb_iv_set (self, "@detail", INT2FIX (e->detail));
205         rb_iv_set (self, "@time", UINT2NUM (e->time));
206
207         return self;
208 }
209
210 static VALUE c_ev_win_delete_request_init (VALUE self, VALUE event)
211 {
212         VALUE c = CLASS_OF (self);
213         Ecore_X_Event_Window_Delete_Request *e = (void *) event;
214
215         rb_define_attr (c, "window", 1, 0);
216         rb_define_attr (c, "time", 1, 0);
217
218         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
219         rb_iv_set (self, "@time", UINT2NUM (e->time));
220
221         return self;
222 }
223
224 static VALUE c_ev_win_configure_init (VALUE self, VALUE event)
225 {
226         VALUE c = CLASS_OF (self);
227         Ecore_X_Event_Window_Configure *e = (void *) event;
228
229         rb_define_attr (c, "window", 1, 0);
230         rb_define_attr (c, "window_above", 1, 0);
231         rb_define_attr (c, "x", 1, 0);
232         rb_define_attr (c, "y", 1, 0);
233         rb_define_attr (c, "w", 1, 0);
234         rb_define_attr (c, "h", 1, 0);
235         rb_define_attr (c, "border", 1, 0);
236         rb_define_attr (c, "override", 1, 0);
237         rb_define_attr (c, "from_wm", 1, 0);
238         rb_define_attr (c, "time", 1, 0);
239
240         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
241         rb_iv_set (self, "@window_above", TO_ECORE_X_WINDOW (Qnil,
242                                                              e->abovewin));
243         rb_iv_set (self, "@x", INT2FIX (e->x));
244         rb_iv_set (self, "@y", INT2FIX (e->y));
245         rb_iv_set (self, "@w", INT2FIX (e->w));
246         rb_iv_set (self, "@h", INT2FIX (e->h));
247         rb_iv_set (self, "@border", INT2FIX (e->border));
248         rb_iv_set (self, "@override", e->override ? Qtrue : Qfalse);
249         rb_iv_set (self, "@from_wm", e->from_wm ? Qtrue : Qfalse);
250         rb_iv_set (self, "@time", UINT2NUM (e->time));
251
252         return self;
253 }
254
255 static VALUE c_ev_win_visibility_change_init (VALUE self, VALUE event)
256 {
257         VALUE c = CLASS_OF (self);
258         Ecore_X_Event_Window_Visibility_Change *e = (void *) event;
259
260         rb_define_attr (c, "window", 1, 0);
261         rb_define_attr (c, "fully_obscured", 1, 0);
262         rb_define_attr (c, "time", 1, 0);
263
264         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
265         rb_iv_set (self, "@fully_obscured", e->fully_obscured ? Qtrue : Qfalse);
266         rb_iv_set (self, "@time", UINT2NUM (e->time));
267
268         return self;
269 }
270
271 static VALUE c_ev_win_create_init (VALUE self, VALUE event)
272 {
273         VALUE c = CLASS_OF (self);
274         Ecore_X_Event_Window_Create *e = (void *) event;
275
276         rb_define_attr (c, "window", 1, 0);
277         rb_define_attr (c, "override", 1, 0);
278         rb_define_attr (c, "time", 1, 0);
279
280         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
281         rb_iv_set (self, "@override", e->override ? Qtrue : Qfalse);
282         rb_iv_set (self, "@time", UINT2NUM (e->time));
283
284         return self;
285 }
286
287 static VALUE c_ev_win_show_request_init (VALUE self, VALUE event)
288 {
289         VALUE c = CLASS_OF (self);
290         Ecore_X_Event_Window_Show_Request *e = (void *) event;
291
292         rb_define_attr (c, "window", 1, 0);
293         rb_define_attr (c, "parent", 1, 0);
294         rb_define_attr (c, "time", 1, 0);
295
296         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
297         rb_iv_set (self, "@parent", TO_ECORE_X_WINDOW (Qnil, e->parent));
298         rb_iv_set (self, "@time", UINT2NUM (e->time));
299
300         return self;
301 }
302
303 void Init_ecore_x (void)
304 {
305         VALUE c;
306         Ecore_X_Window w;
307
308         rb_require ("ecore");
309
310         /* we need to call ecore_x_init () here, to make sure the
311          * event ids are set properly
312          */
313         ecore_init ();
314         ecore_x_init (getenv ("DISPLAY"));
315         atexit (at_exit);
316
317         mX = rb_define_module_under (mEcore, "X");
318         rb_define_module_function (mX, "default_root_window",
319                                    m_default_root_window_get, 0);
320
321         Init_Window ();
322         Init_Cursor ();
323
324         /* now create the default root window object */
325         w = DefaultRootWindow (ecore_x_display_get ());
326         default_root = TO_ECORE_X_WINDOW (Qnil, w);
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 }