2 * $Id: rb_event_handler.c 107 2004-08-29 18:37:58Z 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 VALUE event_classes, cEcoreEvent;
31 static VALUE handlers;
33 static VALUE c_init (VALUE self, VALUE type)
37 if (!rb_block_given_p ())
42 if (t <= ECORE_EVENT_NONE)
45 rb_iv_set (self, "@type", type);
46 rb_iv_set (self, "handler", rb_block_proc ());
48 rb_ary_push (handlers, self);
50 ecore_event_handler_add (t, on_ecore_event, NULL);
55 static VALUE c_delete (VALUE self)
57 int len = RARRAY (handlers)->len, i;
60 for (i = 0; i < len; i++) {
61 el = rb_ary_shift (handlers);
65 rb_ary_push (handlers, el);
71 int on_ecore_event (void *data, int type, void *event)
73 VALUE handler, klass, obj, argv[1], res;
74 int handler_type, len, i;
77 /* instantiate the event object
78 * first, find the class we're gonna use
80 if (NIL_P (klass = rb_hash_aref (event_classes, INT2NUM (type)))) {
81 rb_raise (rb_eException, "Cannot find event class "
82 "for event %i\n", type);
86 /* now create and init the object */
87 obj = rb_obj_alloc (klass);
88 argv[0] = (VALUE) event;
89 rb_obj_call_init (obj, 1, argv);
91 len = RARRAY (handlers)->len;
93 for (i = 0; i < len; i++) {
94 handler = rb_ary_entry (handlers, i);
95 handler_type = NUM2INT (rb_iv_get (handler, "@type"));
97 if (handler_type == type) {
98 res = rb_funcall (rb_iv_get (handler, "handler"),
99 rb_intern ("call"), 1, obj);
102 /* if the block returned false, don't call the other
110 if (type == ECORE_EVENT_SIGNAL_EXIT && !called)
111 ecore_main_loop_quit ();
113 /* call other event handlers, too */
117 VALUE c_ev_generic_init (VALUE self, VALUE event)
123 void Init_EventHandler (void)
127 cEventHandler = rb_define_class_under (mEcore, "EventHandler",
130 rb_define_method (cEventHandler, "initialize", c_init, 1);
131 rb_define_method (cEventHandler, "delete", c_delete, 0);
133 handlers = rb_ary_new ();
134 rb_global_variable (&handlers);
136 event_classes = rb_hash_new ();
137 rb_global_variable (&event_classes);
139 /* define a base event class */
140 cEcoreEvent = rb_define_class_under (mEcore, "Event", rb_cObject);
141 rb_define_private_method (rb_singleton_class (cEcoreEvent),