2 * $Id: rb_event_handler.c 357 2006-02-12 15:40:30Z tilman $
4 * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
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.
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.
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
26 #define __RB_EVENT_HANDLER_C
28 #include "rb_event_handler.h"
31 Ecore_Event_Handler *real;
36 static int on_ecore_event (void *data, int type, void *event);
38 VALUE event_classes, cEcoreEvent;
39 static VALUE handlers;
41 static void c_mark (RbEventHandler *h)
43 rb_gc_mark (h->callback);
46 static void c_free (RbEventHandler *h)
48 RbEventHandler *h2 = NULL;
49 int len = RARRAY (handlers)->len, i;
52 if (!h->real || h->deleted) {
57 for (i = 0; i < len; i++) {
58 el = rb_ary_shift (handlers);
60 Data_Get_Struct (el, RbEventHandler, h2);
63 ecore_event_handler_del (h->real);
67 rb_ary_push (handlers, el);
73 static VALUE c_alloc (VALUE klass)
75 RbEventHandler *h = NULL;
77 return Data_Make_Struct (klass, RbEventHandler, c_mark, c_free, h);
80 static VALUE c_init (VALUE self, VALUE type)
82 RbEventHandler *h = NULL;
85 if (!rb_block_given_p ())
88 Data_Get_Struct (self, RbEventHandler, h);
92 if (t <= ECORE_EVENT_NONE)
95 rb_iv_set (self, "@type", type);
97 h->callback = rb_block_proc ();
99 h->real = ecore_event_handler_add (t, on_ecore_event, NULL);
101 rb_ary_push (handlers, self);
106 static VALUE c_delete (VALUE self)
108 RbEventHandler *h = NULL;
109 int len = RARRAY (handlers)->len, i;
112 for (i = 0; i < len; i++) {
113 el = rb_ary_shift (handlers);
115 Data_Get_Struct (self, RbEventHandler, h);
116 ecore_event_handler_del (h->real);
123 rb_ary_push (handlers, el);
129 static int on_ecore_event (void *data, int type, void *event)
131 RbEventHandler *h = NULL;
132 VALUE handler, klass, obj, argv[1], res;
133 int handler_type, len, i;
136 /* instantiate the event object
137 * first, find the class we're gonna use
139 if (NIL_P (klass = rb_hash_aref (event_classes, INT2NUM (type)))) {
140 rb_raise (rb_eException, "Cannot find event class "
141 "for event %i\n", type);
145 /* now create and init the object */
146 obj = rb_obj_alloc (klass);
147 argv[0] = (VALUE) event;
148 rb_obj_call_init (obj, 1, argv);
150 len = RARRAY (handlers)->len;
152 for (i = 0; i < len; i++) {
153 handler = rb_ary_entry (handlers, i);
154 handler_type = NUM2INT (rb_iv_get (handler, "@type"));
156 if (handler_type == type) {
157 Data_Get_Struct (handler, RbEventHandler, h);
158 res = rb_funcall (h->callback, rb_intern ("call"), 1, obj);
161 /* if the block returned false, don't call the other
169 if (type == ECORE_EVENT_SIGNAL_EXIT && !called)
170 ecore_main_loop_quit ();
172 /* call other event handlers, too */
176 VALUE c_ev_generic_init (VALUE self, VALUE event)
182 void Init_EventHandler (void)
186 cEventHandler = rb_define_class_under (mEcore, "EventHandler",
189 rb_define_alloc_func (cEventHandler, c_alloc);
190 rb_define_method (cEventHandler, "initialize", c_init, 1);
191 rb_define_method (cEventHandler, "delete", c_delete, 0);
193 handlers = rb_ary_new ();
194 rb_global_variable (&handlers);
196 event_classes = rb_hash_new ();
197 rb_global_variable (&event_classes);
199 /* define a base event class */
200 cEcoreEvent = rb_define_class_under (mEcore, "Event", rb_cObject);
201 rb_define_private_method (rb_singleton_class (cEcoreEvent),