2 * $Id: rb_event_handler.c 343 2005-05-07 20:22:56Z 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"
30 static int on_ecore_event (void *data, int type, void *event);
32 VALUE event_classes, cEcoreEvent;
33 static VALUE handlers;
35 static VALUE c_init (VALUE self, VALUE type)
39 if (!rb_block_given_p ())
44 if (t <= ECORE_EVENT_NONE)
47 rb_iv_set (self, "@type", type);
48 rb_iv_set (self, "handler", rb_block_proc ());
50 rb_ary_push (handlers, self);
52 ecore_event_handler_add (t, on_ecore_event, NULL);
57 static VALUE c_delete (VALUE self)
59 int len = RARRAY (handlers)->len, i;
62 for (i = 0; i < len; i++) {
63 el = rb_ary_shift (handlers);
67 rb_ary_push (handlers, el);
73 static int on_ecore_event (void *data, int type, void *event)
75 VALUE handler, klass, obj, argv[1], res;
76 int handler_type, len, i;
79 /* instantiate the event object
80 * first, find the class we're gonna use
82 if (NIL_P (klass = rb_hash_aref (event_classes, INT2NUM (type)))) {
83 rb_raise (rb_eException, "Cannot find event class "
84 "for event %i\n", type);
88 /* now create and init the object */
89 obj = rb_obj_alloc (klass);
90 argv[0] = (VALUE) event;
91 rb_obj_call_init (obj, 1, argv);
93 len = RARRAY (handlers)->len;
95 for (i = 0; i < len; i++) {
96 handler = rb_ary_entry (handlers, i);
97 handler_type = NUM2INT (rb_iv_get (handler, "@type"));
99 if (handler_type == type) {
100 res = rb_funcall (rb_iv_get (handler, "handler"),
101 rb_intern ("call"), 1, obj);
104 /* if the block returned false, don't call the other
112 if (type == ECORE_EVENT_SIGNAL_EXIT && !called)
113 ecore_main_loop_quit ();
115 /* call other event handlers, too */
119 VALUE c_ev_generic_init (VALUE self, VALUE event)
125 void Init_EventHandler (void)
129 cEventHandler = rb_define_class_under (mEcore, "EventHandler",
132 rb_define_method (cEventHandler, "initialize", c_init, 1);
133 rb_define_method (cEventHandler, "delete", c_delete, 0);
135 handlers = rb_ary_new ();
136 rb_global_variable (&handlers);
138 event_classes = rb_hash_new ();
139 rb_global_variable (&event_classes);
141 /* define a base event class */
142 cEcoreEvent = rb_define_class_under (mEcore, "Event", rb_cObject);
143 rb_define_private_method (rb_singleton_class (cEcoreEvent),