Implemented the WINDOW_CREATE and WINDOW_DESTROY events.
[ruby-ecore.git] / src / ecore_x / rb_ecore_x.c
1 /*
2  * $Id: rb_ecore_x.c 84 2004-08-21 20:16: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
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_win_focus_change_init (VALUE self, VALUE event)
49 {
50         VALUE c = CLASS_OF (self);
51         Ecore_X_Event_Window_Focus_In *e = (void *) event;
52
53         rb_define_attr (c, "window", 1, 0);
54         rb_define_attr (c, "mode", 1, 0);
55         rb_define_attr (c, "detail", 1, 0);
56         rb_define_attr (c, "time", 1, 0);
57
58         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
59         rb_iv_set (self, "@mode", INT2FIX (e->mode));
60         rb_iv_set (self, "@detail", INT2FIX (e->detail));
61         rb_iv_set (self, "@time", UINT2NUM (e->time));
62
63         return self;
64 }
65
66 static VALUE c_ev_win_delete_request_init (VALUE self, VALUE event)
67 {
68         VALUE c = CLASS_OF (self);
69         Ecore_X_Event_Window_Delete_Request *e = (void *) event;
70
71         rb_define_attr (c, "window", 1, 0);
72         rb_define_attr (c, "time", 1, 0);
73
74         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
75         rb_iv_set (self, "@time", UINT2NUM (e->time));
76
77         return self;
78 }
79
80 static VALUE c_ev_win_configure_init (VALUE self, VALUE event)
81 {
82         VALUE c = CLASS_OF (self);
83         Ecore_X_Event_Window_Configure *e = (void *) event;
84
85         rb_define_attr (c, "window", 1, 0);
86         rb_define_attr (c, "window_above", 1, 0);
87         rb_define_attr (c, "x", 1, 0);
88         rb_define_attr (c, "y", 1, 0);
89         rb_define_attr (c, "w", 1, 0);
90         rb_define_attr (c, "h", 1, 0);
91         rb_define_attr (c, "border", 1, 0);
92         rb_define_attr (c, "override", 1, 0);
93         rb_define_attr (c, "from_wm", 1, 0);
94         rb_define_attr (c, "time", 1, 0);
95
96         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
97         rb_iv_set (self, "@window_above", TO_ECORE_X_WINDOW (Qnil,
98                                                              e->abovewin));
99         rb_iv_set (self, "@x", INT2FIX (e->x));
100         rb_iv_set (self, "@y", INT2FIX (e->y));
101         rb_iv_set (self, "@w", INT2FIX (e->w));
102         rb_iv_set (self, "@h", INT2FIX (e->h));
103         rb_iv_set (self, "@border", INT2FIX (e->border));
104         rb_iv_set (self, "@override", e->override ? Qtrue : Qfalse);
105         rb_iv_set (self, "@from_wm", e->from_wm ? Qtrue : Qfalse);
106         rb_iv_set (self, "@time", UINT2NUM (e->time));
107
108         return self;
109 }
110
111 static VALUE c_ev_win_visibility_change_init (VALUE self, VALUE event)
112 {
113         VALUE c = CLASS_OF (self);
114         Ecore_X_Event_Window_Visibility_Change *e = (void *) event;
115
116         rb_define_attr (c, "window", 1, 0);
117         rb_define_attr (c, "fully_obscured", 1, 0);
118         rb_define_attr (c, "time", 1, 0);
119
120         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
121         rb_iv_set (self, "@fully_obscured", e->fully_obscured ? Qtrue : Qfalse);
122         rb_iv_set (self, "@time", UINT2NUM (e->time));
123
124         return self;
125 }
126
127 static VALUE c_ev_win_create_init (VALUE self, VALUE event)
128 {
129         VALUE c = CLASS_OF (self);
130         Ecore_X_Event_Window_Create *e = (void *) event;
131
132         rb_define_attr (c, "window", 1, 0);
133         rb_define_attr (c, "override", 1, 0);
134         rb_define_attr (c, "time", 1, 0);
135
136         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
137         rb_iv_set (self, "@override", e->override ? Qtrue : Qfalse);
138         rb_iv_set (self, "@time", UINT2NUM (e->time));
139
140         return self;
141 }
142
143 static VALUE c_ev_win_show_request_init (VALUE self, VALUE event)
144 {
145         VALUE c = CLASS_OF (self);
146         Ecore_X_Event_Window_Show_Request *e = (void *) event;
147
148         rb_define_attr (c, "window", 1, 0);
149         rb_define_attr (c, "parent", 1, 0);
150         rb_define_attr (c, "time", 1, 0);
151
152         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
153         rb_iv_set (self, "@parent", TO_ECORE_X_WINDOW (Qnil, e->parent));
154         rb_iv_set (self, "@time", UINT2NUM (e->time));
155
156         return self;
157 }
158
159 void Init_ecore_x (void)
160 {
161         VALUE c;
162
163         rb_require ("ecore");
164
165         /* we need to call ecore_x_init () here, to make sure the
166          * event ids are set properly
167          */
168         ecore_init ();
169         ecore_x_init (getenv ("DISPLAY"));
170         atexit (at_exit);
171
172         mX = rb_define_module_under (mEcore, "X");
173         rb_define_module_function (mX, "default_root_window",
174                                    m_default_root_window_get, 0);
175
176         Init_Window ();
177
178         /* now create the default root window object */
179         default_root = TO_ECORE_X_WINDOW (Qnil, 0);
180         OBJ_FREEZE (default_root);
181         rb_global_variable (&default_root);
182
183         /* event mask values */
184         c = rb_define_class_under (mX, "EventMask", rb_cObject);
185         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
186         DEF_CONST (c, ECORE_X_EVENT_MASK_, KEY_DOWN);
187         DEF_CONST (c, ECORE_X_EVENT_MASK_, KEY_UP);
188         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_DOWN);
189         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_UP);
190         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_IN);
191         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_OUT);
192         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_MOVE);
193         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_DAMAGE);
194         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_VISIBILITY);
195         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_CONFIGURE);
196         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_RESIZE_MANAGE);
197         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_MANAGE);
198         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_CHILD_CONFIGURE);
199         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_FOCUS_CHANGE);
200         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_PROPERTY);
201         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_COLORMAP);
202         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_GRAB);
203         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_WHEEL);
204         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_FOCUS_IN);
205         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_FOCUS_OUT);
206
207         /* wm protocols */
208         c = rb_define_class_under (mX, "WmProtocol", rb_cObject);
209         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
210         DEF_CONST (c, ECORE_X_WM_PROTOCOL_, DELETE_REQUEST);
211         DEF_CONST (c, ECORE_X_WM_PROTOCOL_, TAKE_FOCUS);
212
213         /* event modes */
214         c = rb_define_class_under (mX, "EventMode", rb_cObject);
215         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
216         DEF_CONST (c, ECORE_X_EVENT_MODE_, NORMAL);
217         DEF_CONST (c, ECORE_X_EVENT_MODE_, WHILE_GRABBED);
218         DEF_CONST (c, ECORE_X_EVENT_MODE_, GRAB);
219         DEF_CONST (c, ECORE_X_EVENT_MODE_, UNGRAB);
220
221         /* event details */
222         c = rb_define_class_under (mX, "EventDetails", rb_cObject);
223         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
224         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, ANCESTOR);
225         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, VIRTUAL);
226         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, INFERIOR);
227         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, NON_LINEAR);
228         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, NON_LINEAR_VIRTUAL);
229         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, POINTER);
230         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, POINTER_ROOT);
231         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, DETAIL_NONE);
232
233         /* events */
234         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_FOCUS_IN,
235                    "WindowFocusIn", c);
236         rb_define_private_method (c, "initialize",
237                                   c_ev_win_focus_change_init, 1);
238
239         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_FOCUS_OUT,
240                    "WindowFocusOut", c);
241         rb_define_private_method (c, "initialize",
242                                   c_ev_win_focus_change_init, 1);
243
244         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_DELETE_REQUEST,
245                    "WindowDeleteRequest", c);
246         rb_define_private_method (c, "initialize",
247                                   c_ev_win_delete_request_init, 1);
248
249         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_CONFIGURE_REQUEST,
250                    "WindowConfigureRequest", c);
251         rb_define_private_method (c, "initialize",
252                                   c_ev_win_delete_request_init, 1);
253
254         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_CONFIGURE,
255                    "WindowConfigure", c);
256         rb_define_private_method (c, "initialize",
257                                   c_ev_win_configure_init, 1);
258
259         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_VISIBILITY_CHANGE,
260                    "WindowVisibilityChange", c);
261         rb_define_private_method (c, "initialize",
262                                   c_ev_win_visibility_change_init, 1);
263
264         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_CREATE, "WindowCreate", c);
265         rb_define_private_method (c, "initialize",
266                                   c_ev_win_create_init, 1);
267
268         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_DESTROY, "WindowDestroy", c);
269         rb_define_private_method (c, "initialize",
270                                   c_ev_win_delete_request_init, 1);
271
272         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_SHOW, "WindowShow", c);
273         rb_define_private_method (c, "initialize",
274                                   c_ev_win_delete_request_init, 1);
275
276         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_HIDE, "WindowHide", c);
277         rb_define_private_method (c, "initialize",
278                                   c_ev_win_delete_request_init, 1);
279
280         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_SHOW_REQUEST,
281                    "WindowShowRequest", c);
282         rb_define_private_method (c, "initialize",
283                                   c_ev_win_show_request_init, 1);
284 }