2 * \brief Event handling of DOpE client library
4 * \author Norman Feske <nf2@inf.tu-dresden.de>
8 * Copyright (C) 2002-2003 Norman Feske <nf2@os.inf.tu-dresden.de>
9 * Technische Universitaet Dresden, Operating Systems Research Group
11 * This file is part of the DOpE package, which is distributed under
12 * the terms of the GNU General Public Licence 2. Please see the
13 * COPYING file for details.
16 #include "dopeapp-server.h"
17 #include "dopeapp-client.h"
22 #include "app_struct.h"
26 /*** UTILITIY: CONVERT DOPE EVENT TO IDL TYPE ***/
27 static void dope_event__to__dope_event_u(dope_event *ev, dope_event_u *ev_u) {
31 case EVENT_TYPE_COMMAND:
33 ev_u->_u.command.cmd = ev->command.cmd;
36 case EVENT_TYPE_MOTION:
38 ev_u->_u.motion.rel_x = ev->motion.rel_x;
39 ev_u->_u.motion.rel_y = ev->motion.rel_y;
40 ev_u->_u.motion.abs_x = ev->motion.abs_x;
41 ev_u->_u.motion.abs_y = ev->motion.abs_y;
44 case EVENT_TYPE_PRESS:
46 ev_u->_u.press.code = ev->press.code;
49 case EVENT_TYPE_RELEASE:
51 ev_u->_u.release.code = ev->release.code;
54 case EVENT_TYPE_KEYREPEAT:
56 ev_u->_u.keyrepeat.code = ev->keyrepeat.code;
65 /*** UTILITY: CONVERT EVENT IDL TYPE TO DOPE EVENT ***
67 * \param ev_u source dope event union
68 * \param cmd destination buffer for command string
69 * \param ev destination buffer for dope_event
71 static void dope_event_u__to__dope_event(const dope_event_u *ev_u,
72 char *cmd, dope_event *ev) {
77 ev->type = EVENT_TYPE_COMMAND;
78 ev->command.cmd = cmd;
79 strncpy(cmd, ev_u->_u.command.cmd, EVENT_CMD_SIZE);
84 ev->type = EVENT_TYPE_MOTION;
85 ev->motion.rel_x = ev_u->_u.motion.rel_x;
86 ev->motion.rel_y = ev_u->_u.motion.rel_y;
87 ev->motion.abs_x = ev_u->_u.motion.abs_x;
88 ev->motion.abs_y = ev_u->_u.motion.abs_y;
93 ev->type = EVENT_TYPE_PRESS;
94 ev->press.code = ev_u->_u.press.code;
99 ev->type = EVENT_TYPE_RELEASE;
100 ev->release.code = ev_u->_u.release.code;
103 /* keyrepeat event */
105 ev->type = EVENT_TYPE_KEYREPEAT;
106 ev->keyrepeat.code = ev_u->_u.keyrepeat.code;
109 /* undefined event */
111 ev->type = EVENT_TYPE_UNDEFINED;
117 int dopelib_init_eventqueue(int id) {
118 dopelib_apps[id]->queue_sem = dopelib_sem_create(1);
119 return dopelib_apps[id]->queue_sem ? 0 : -1;
123 long dopeapp_listener_event_component(CORBA_Object _dice_corba_obj,
124 const dope_event_u *e,
126 CORBA_Server_Environment *_dice_corba_env) {
128 struct dopelib_app *app = dopelib_apps[(unsigned long)_dice_corba_env->user_data];
129 long first = app->first;
130 dope_event *event = &app->event_queue[first];
132 /* do not handle events for non-initialized event queue */
133 if (!app->queue_sem) {
134 printf("%s: event queue for app_id=%d no initialized\n",
135 __FUNCTION__, (int)app->app_id);
139 app->first = (first + 1) % EVENT_QUEUE_SIZE;
141 strncpy(app->bindarg_queue[first], bindarg, 250);
143 dope_event_u__to__dope_event(e, &app->bindcmd_queue[first][0], event);
145 /* signal incoming event */
146 if (app->queue_sem) dopelib_sem_post(app->queue_sem);
151 /*** INTERFACE: RETURN NUMBER OF PENDING EVENTS ***/
152 int dope_events_pending(int id) {
153 struct dopelib_app *app = get_app(id);
154 return (app ? (app->first - app->last) : 0);
159 /*** WAIT FOR AN EVENT ***/
160 void dopelib_wait_event(int id, dope_event **e_out,char **bindarg_out) {
161 struct dopelib_app *app = get_app(id);
167 /* wait for an incoming event */
168 dopelib_sem_wait(app->queue_sem);
169 app->last = (app->last + 1) % EVENT_QUEUE_SIZE;
171 /* fill out the results of the function */
172 if (bindarg_out) *bindarg_out = &app->bindarg_queue[curr][0];
173 if (e_out) *e_out = &app->event_queue[curr];
177 void dope_inject_event(long app_id, dope_event *ev,
178 void (*callback)(dope_event *,void *), void *arg) {
181 CORBA_Environment env = dice_default_environment;
183 /* convert dope_event to idl-compatible event union */
185 dope_event__to__dope_event_u(ev, &ev_u);
187 dopelib_callback_to_bindarg(callback, arg, buf, sizeof(buf));
189 /* submit event to local listener server */
190 dopeapp_listener_event_call(&dopelib_apps[app_id]->listener,
193 if (DICE_IS_EXCEPTION(&env, CORBA_SYSTEM_EXCEPTION) &&
194 (DICE_EXCEPTION_MINOR(&env) == CORBA_DICE_EXCEPTION_IPC_ERROR))
195 printf ("IPC Error at client: 0x%02x\n", DICE_IPC_ERROR(&env));