Updated copyright notice. Added event handler code.
[ruby-ecore.git] / src / ecore_x / rb_ecore_x.c
1 /*
2  * $Id: rb_ecore_x.c 77 2004-08-19 17:39:29Z 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_win_show_req_init (VALUE self, VALUE event)
50 {
51         Ecore_X_Event_Window_Show_Request *e = (void *) event;
52
53         rb_iv_set (self, "@window", TO_ECORE_X_WINDOW (Qnil, e->win));
54         rb_iv_set (self, "@parent", TO_ECORE_X_WINDOW (Qnil, e->parent));
55         rb_iv_set (self, "@time", UINT2NUM (e->time));
56
57         return self;
58 }
59
60 void Init_ecore_x (void)
61 {
62         Ecore_X_Window w;
63         VALUE c;
64
65         rb_require ("ecore");
66
67         /* we need to call ecore_x_init () here, to make sure the
68          * event ids are set properly
69          */
70         ecore_init ();
71         ecore_x_init (getenv ("DISPLAY"));
72         atexit (at_exit);
73
74         mX = rb_define_module_under (mEcore, "X");
75         rb_define_module_function (mX, "default_root_window",
76                                    m_default_root_window_get, 0);
77
78         Init_Window ();
79
80         /* now create the default root window object */
81         w = DefaultRootWindow (ecore_x_display_get ());
82         default_root = TO_ECORE_X_WINDOW (Qnil, w);
83         OBJ_FREEZE (default_root);
84         rb_global_variable (&default_root);
85
86         /* event mask values */
87         c = rb_define_class_under (mX, "EventMask", rb_cObject);
88         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
89         DEF_CONST (c, ECORE_X_EVENT_MASK_, KEY_DOWN);
90         DEF_CONST (c, ECORE_X_EVENT_MASK_, KEY_UP);
91         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_DOWN);
92         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_UP);
93         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_IN);
94         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_OUT);
95         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_MOVE);
96         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_DAMAGE);
97         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_VISIBILITY);
98         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_CONFIGURE);
99         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_RESIZE_MANAGE);
100         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_MANAGE);
101         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_CHILD_CONFIGURE);
102         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_FOCUS_CHANGE);
103         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_PROPERTY);
104         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_COLORMAP);
105         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_GRAB);
106         DEF_CONST (c, ECORE_X_EVENT_MASK_, MOUSE_WHEEL);
107         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_FOCUS_IN);
108         DEF_CONST (c, ECORE_X_EVENT_MASK_, WINDOW_FOCUS_OUT);
109
110         /* wm protocols */
111         c = rb_define_class_under (mX, "WmProtocol", rb_cObject);
112         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0);
113         DEF_CONST (c, ECORE_X_WM_PROTOCOL_, DELETE_REQUEST);
114         DEF_CONST (c, ECORE_X_WM_PROTOCOL_, TAKE_FOCUS);
115
116         /* events */
117         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_FOCUS_IN,
118                    "WindowFocusIn", c);
119         rb_define_private_method (c, "initialize", c_ev_generic_init, 1);
120
121         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_FOCUS_OUT,
122                    "WindowFocusOut", c);
123         rb_define_private_method (c, "initialize", c_ev_generic_init, 1);
124
125         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_DELETE_REQUEST,
126                    "WindowDeleteRequest", c);
127         rb_define_private_method (c, "initialize", c_ev_generic_init, 1);
128
129         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_CONFIGURE_REQUEST,
130                    "WindowConfigureRequest", c);
131         rb_define_private_method (c, "initialize", c_ev_generic_init, 1);
132
133         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_CONFIGURE,
134                    "WindowConfigure", c);
135         rb_define_private_method (c, "initialize", c_ev_generic_init, 1);
136
137         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_VISIBILITY_CHANGE,
138                    "WindowVisibilityChange", c);
139         rb_define_private_method (c, "initialize", c_ev_generic_init, 1);
140
141         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_SHOW, "WindowShow", c);
142         rb_define_private_method (c, "initialize", c_ev_generic_init, 1);
143
144         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_HIDE, "WindowHide", c);
145         rb_define_private_method (c, "initialize", c_ev_generic_init, 1);
146
147         ADD_EVENT (mX, ECORE_X_EVENT_, WINDOW_SHOW_REQUEST,
148                    "WindowShowRequest", c);
149         rb_define_private_method (c, "initialize",
150                                   c_ev_win_show_req_init, 1);
151
152         rb_define_attr (c, "window", 1, 0);
153         rb_define_attr (c, "parent", 1, 0);
154         rb_define_attr (c, "time", 1, 0);
155 }