Moved the rb_define_attr calls. Implemented more events and enums.
[ruby-ecore.git] / src / ecore_x / rb_ecore_x.c
1 /*
2  * $Id: rb_ecore_x.c 83 2004-08-21 19:55:35Z 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_show_request_init (VALUE self, VALUE event)
128 {
129         VALUE c = CLASS_OF (self);
130         Ecore_X_Event_Window_Show_Request *e = (void *) event;
131
132         rb_define_attr (c, "window", 1, 0);
133         rb_define_attr (c, "parent", 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, "@parent", TO_ECORE_X_WINDOW (Qnil, e->parent));
138         rb_iv_set (self, "@time", UINT2NUM (e->time));
139
140         return self;
141 }
142
143 void Init_ecore_x (void)
144 {
145         VALUE c;
146
147         rb_require ("ecore");
148
149         /* we need to call ecore_x_init () here, to make sure the
150          * event ids are set properly
151          */
152         ecore_init ();
153         ecore_x_init (getenv ("DISPLAY"));
154         atexit (at_exit);
155
156         mX = rb_define_module_under (mEcore, "X");
157         rb_define_module_function (mX, "default_root_window",
158                                    m_default_root_window_get, 0);
159
160         Init_Window ();
161
162         /* now create the default root window object */
163         default_root = TO_ECORE_X_WINDOW (Qnil, 0);
164         OBJ_FREEZE (default_root);
165         rb_global_variable (&default_root);
166
167         /* event mask values */
168         c = rb_define_class_under (mX, "EventMask", rb_cObject);
169         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
170         DEF_CONST (c, ECORE_X_EVENT_MASK_, KEY_DOWN);
171         DEF_CONST (c, ECORE_X_EVENT_MASK_, KEY_UP);
172         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_DOWN);
173         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_UP);
174         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_IN);
175         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_OUT);
176         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_MOVE);
177         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_DAMAGE);
178         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_VISIBILITY);
179         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_CONFIGURE);
180         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_RESIZE_MANAGE);
181         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_MANAGE);
182         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_CHILD_CONFIGURE);
183         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_FOCUS_CHANGE);
184         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_PROPERTY);
185         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_COLORMAP);
186         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_GRAB);
187         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_WHEEL);
188         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_FOCUS_IN);
189         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_FOCUS_OUT);
190
191         /* wm protocols */
192         c = rb_define_class_under (mX, "WmProtocol", rb_cObject);
193         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
194         DEF_CONST (c, ECORE_X_WM_PROTOCOL_, DELETE_REQUEST);
195         DEF_CONST (c, ECORE_X_WM_PROTOCOL_, TAKE_FOCUS);
196
197         /* event modes */
198         c = rb_define_class_under (mX, "EventMode", rb_cObject);
199         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
200         DEF_CONST (c, ECORE_X_EVENT_MODE_, NORMAL);
201         DEF_CONST (c, ECORE_X_EVENT_MODE_, WHILE_GRABBED);
202         DEF_CONST (c, ECORE_X_EVENT_MODE_, GRAB);
203         DEF_CONST (c, ECORE_X_EVENT_MODE_, UNGRAB);
204
205         /* event details */
206         c = rb_define_class_under (mX, "EventDetails", rb_cObject);
207         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
208         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, ANCESTOR);
209         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, VIRTUAL);
210         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, INFERIOR);
211         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, NON_LINEAR);
212         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, NON_LINEAR_VIRTUAL);
213         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, POINTER);
214         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, POINTER_ROOT);
215         DEF_CONST (c, ECORE_X_EVENT_DETAIL_, DETAIL_NONE);
216
217         /* events */
218         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_FOCUS_IN,
219                    "WindowFocusIn", c);
220         rb_define_private_method (c, "initialize",
221                                   c_ev_win_focus_change_init, 1);
222
223         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_FOCUS_OUT,
224                    "WindowFocusOut", c);
225         rb_define_private_method (c, "initialize",
226                                   c_ev_win_focus_change_init, 1);
227
228         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_DELETE_REQUEST,
229                    "WindowDeleteRequest", c);
230         rb_define_private_method (c, "initialize",
231                                   c_ev_win_delete_request_init, 1);
232
233         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_CONFIGURE_REQUEST,
234                    "WindowConfigureRequest", c);
235         rb_define_private_method (c, "initialize",
236                                   c_ev_win_delete_request_init, 1);
237
238         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_CONFIGURE,
239                    "WindowConfigure", c);
240         rb_define_private_method (c, "initialize",
241                                   c_ev_win_configure_init, 1);
242
243         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_VISIBILITY_CHANGE,
244                    "WindowVisibilityChange", c);
245         rb_define_private_method (c, "initialize",
246                                   c_ev_win_visibility_change_init, 1);
247
248         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_SHOW, "WindowShow", c);
249         rb_define_private_method (c, "initialize",
250                                   c_ev_win_delete_request_init, 1);
251
252         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_HIDE, "WindowHide", c);
253         rb_define_private_method (c, "initialize",
254                                   c_ev_win_delete_request_init, 1);
255
256         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_SHOW_REQUEST,
257                    "WindowShowRequest", c);
258         rb_define_private_method (c, "initialize",
259                                   c_ev_win_show_request_init, 1);
260 }