]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/dope/lib/dope/dice/events.c
Inital import
[l4.git] / l4 / pkg / dope / lib / dope / dice / events.c
1 /*
2  * \brief   Event handling of DOpE client library
3  * \date    2002-11-13
4  * \author  Norman Feske <nf2@inf.tu-dresden.de>
5  */
6
7 /*
8  * Copyright (C) 2002-2003  Norman Feske  <nf2@os.inf.tu-dresden.de>
9  * Technische Universitaet Dresden, Operating Systems Research Group
10  *
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.
14  */
15
16 #include "dopeapp-server.h"
17 #include "dopeapp-client.h"
18 #include "dopestd.h"
19 #include "dopelib.h"
20 #include "events.h"
21 #include "sync.h"
22 #include "app_struct.h"
23 #include "misc.h"
24
25
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) {
28
29         switch (ev->type) {
30
31                 case EVENT_TYPE_COMMAND:
32                         ev_u->type = 1;
33                         ev_u->_u.command.cmd = ev->command.cmd;
34                         break;
35
36                 case EVENT_TYPE_MOTION:
37                         ev_u->type = 2;
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;
42                         break;
43
44                 case EVENT_TYPE_PRESS:
45                         ev_u->type = 3;
46                         ev_u->_u.press.code = ev->press.code;
47                         break;
48
49                 case EVENT_TYPE_RELEASE:
50                         ev_u->type = 4;
51                         ev_u->_u.release.code = ev->release.code;
52                         break;
53
54                 case EVENT_TYPE_KEYREPEAT:
55                         ev_u->type = 5;
56                         ev_u->_u.keyrepeat.code = ev->keyrepeat.code;
57                         break;
58
59                 default:
60                         ev_u->type = 0;
61         }
62 }
63
64
65 /*** UTILITY: CONVERT EVENT IDL TYPE TO DOPE EVENT ***
66  *
67  * \param ev_u  source dope event union
68  * \param cmd   destination buffer for command string
69  * \param ev    destination buffer for dope_event
70  */
71 static void dope_event_u__to__dope_event(const dope_event_u *ev_u,
72                                          char *cmd, dope_event *ev) {
73         switch (ev_u->type) {
74
75                 /* command event */
76                 case 1:
77                         ev->type = EVENT_TYPE_COMMAND;
78                         ev->command.cmd = cmd;
79                         strncpy(cmd, ev_u->_u.command.cmd, EVENT_CMD_SIZE);
80                         break;
81
82                 /* motion event */
83                 case 2:
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;
89                         break;
90
91                 /* press event */
92                 case 3:
93                         ev->type = EVENT_TYPE_PRESS;
94                         ev->press.code = ev_u->_u.press.code;
95                         break;
96
97                 /* release event */
98                 case 4:
99                         ev->type = EVENT_TYPE_RELEASE;
100                         ev->release.code = ev_u->_u.release.code;
101                         break;
102
103                 /* keyrepeat event */
104                 case 5:
105                         ev->type = EVENT_TYPE_KEYREPEAT;
106                         ev->keyrepeat.code = ev_u->_u.keyrepeat.code;
107                         break;
108
109                 /* undefined event */
110                 default:
111                         ev->type = EVENT_TYPE_UNDEFINED;
112                         break;
113         }
114 }
115
116
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;
120 }
121
122
123 long dopeapp_listener_event_component(CORBA_Object _dice_corba_obj,
124                                       const dope_event_u *e,
125                                       const char* bindarg,
126                                       CORBA_Server_Environment *_dice_corba_env) {
127
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];
131
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);
136                 return -1;
137         }
138
139         app->first = (first + 1) % EVENT_QUEUE_SIZE;
140
141         strncpy(app->bindarg_queue[first], bindarg, 250);
142
143         dope_event_u__to__dope_event(e, &app->bindcmd_queue[first][0], event);
144
145         /* signal incoming event */
146         if (app->queue_sem) dopelib_sem_post(app->queue_sem);
147         return 42;
148 }
149
150
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);
155 }
156
157
158 #if 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);
162         long curr;
163
164         if (!app) return;
165         curr = app->last;
166
167         /* wait for an incoming event */
168         dopelib_sem_wait(app->queue_sem);
169         app->last = (app->last + 1) % EVENT_QUEUE_SIZE;
170
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];
174 }
175 #endif
176
177 void dope_inject_event(long app_id, dope_event *ev,
178                        void (*callback)(dope_event *,void *), void *arg) {
179
180         char buf[128];
181         CORBA_Environment env = dice_default_environment;
182
183         /* convert dope_event to idl-compatible event union */
184         dope_event_u ev_u;
185         dope_event__to__dope_event_u(ev, &ev_u);
186
187         dopelib_callback_to_bindarg(callback, arg, buf, sizeof(buf));
188
189         /* submit event to local listener server */
190         dopeapp_listener_event_call(&dopelib_apps[app_id]->listener,
191                                     &ev_u, buf, &env);
192
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));
196 }