sync with latest
[sdk/emulator/qemu.git] / input.c
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24
25 #include "sysemu.h"
26 #include "net.h"
27 #include "monitor.h"
28 #include "console.h"
29 #include "error.h"
30 #include "qmp-commands.h"
31 #include "tizen/src/debug_ch.h"
32
33 MULTI_DEBUG_CHANNEL(tizen, input);
34
35 static QEMUPutKBDEvent *qemu_put_kbd_event;
36 static void *qemu_put_kbd_event_opaque;
37 #ifdef CONFIG_MARU
38 static QEMUPutKBDEvent *qemu_put_ps2kbd_event;
39 static void *qemu_put_ps2kbd_event_opaque;
40 #endif
41 static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_handlers);
42 static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
43     QTAILQ_HEAD_INITIALIZER(mouse_handlers);
44 static NotifierList mouse_mode_notifiers = 
45     NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
46
47 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
48 {
49     qemu_put_kbd_event_opaque = opaque;
50     qemu_put_kbd_event = func;
51 }
52
53 void qemu_remove_kbd_event_handler(void)
54 {
55     qemu_put_kbd_event_opaque = NULL;
56     qemu_put_kbd_event = NULL;
57 }
58
59 #ifdef CONFIG_MARU
60 /* use ps2kbd device as a hardkey device. */
61 void qemu_add_ps2kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
62 {
63     qemu_put_ps2kbd_event_opaque = opaque;
64     qemu_put_ps2kbd_event = func;
65 }
66
67 void qemu_remove_ps2kbd_event_handler(void)
68 {
69     qemu_put_ps2kbd_event_opaque = NULL;
70     qemu_put_ps2kbd_event = NULL;
71 }
72
73 void ps2kbd_put_keycode(int keycode)
74 {
75     if (qemu_put_ps2kbd_event) {
76         qemu_put_ps2kbd_event(qemu_put_ps2kbd_event_opaque, keycode);
77     }
78 }
79 #endif
80
81 static void check_mode_change(void)
82 {
83     static int current_is_absolute, current_has_absolute;
84     int is_absolute;
85     int has_absolute;
86
87     is_absolute = kbd_mouse_is_absolute();
88     has_absolute = kbd_mouse_has_absolute();
89
90     if (is_absolute != current_is_absolute ||
91         has_absolute != current_has_absolute) {
92         notifier_list_notify(&mouse_mode_notifiers, NULL);
93     }
94
95     current_is_absolute = is_absolute;
96     current_has_absolute = has_absolute;
97 }
98
99 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
100                                                 void *opaque, int absolute,
101                                                 const char *name)
102 {
103     QEMUPutMouseEntry *s;
104     static int mouse_index = 0;
105
106     s = g_malloc0(sizeof(QEMUPutMouseEntry));
107
108     s->qemu_put_mouse_event = func;
109     s->qemu_put_mouse_event_opaque = opaque;
110     s->qemu_put_mouse_event_absolute = absolute;
111     s->qemu_put_mouse_event_name = g_strdup(name);
112     s->index = mouse_index++;
113
114     QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
115
116     check_mode_change();
117
118     return s;
119 }
120
121 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
122 {
123     QTAILQ_REMOVE(&mouse_handlers, entry, node);
124     QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
125
126     check_mode_change();
127 }
128
129 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
130 {
131     QTAILQ_REMOVE(&mouse_handlers, entry, node);
132
133     g_free(entry->qemu_put_mouse_event_name);
134     g_free(entry);
135
136     check_mode_change();
137 }
138
139 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
140                                             void *opaque)
141 {
142     QEMUPutLEDEntry *s;
143
144     s = g_malloc0(sizeof(QEMUPutLEDEntry));
145
146     s->put_led = func;
147     s->opaque = opaque;
148     QTAILQ_INSERT_TAIL(&led_handlers, s, next);
149     return s;
150 }
151
152 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
153 {
154     if (entry == NULL)
155         return;
156     QTAILQ_REMOVE(&led_handlers, entry, next);
157     g_free(entry);
158 }
159
160 void kbd_put_keycode(int keycode)
161 {
162     if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
163         return;
164     }
165     if (qemu_put_kbd_event) {
166         qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
167     }
168 }
169
170 void kbd_put_ledstate(int ledstate)
171 {
172     QEMUPutLEDEntry *cursor;
173
174     QTAILQ_FOREACH(cursor, &led_handlers, next) {
175         cursor->put_led(cursor->opaque, ledstate);
176     }
177 }
178
179 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
180 {
181     QEMUPutMouseEntry *entry;
182     QEMUPutMouseEvent *mouse_event;
183     void *mouse_event_opaque;
184     int width, height;
185
186     if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
187         return;
188     }
189     if (QTAILQ_EMPTY(&mouse_handlers)) {
190         return;
191     }
192 #if defined (CONFIG_MARU)
193     QTAILQ_FOREACH(entry, &mouse_handlers, node) {
194         /* if mouse event is wheelup ,wheeldown or move
195            then go to ps2 mouse event(index == 0) */
196         if((buttons_state > 3  && entry->index == 0)) {
197             //INFO("input device: %s, event: %d\n", entry->qemu_put_mouse_event_name, buttons_state);
198             buttons_state = 0; 
199             mouse_event = entry->qemu_put_mouse_event;
200             mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
201             break;
202         }
203     }
204     /* other events(mouse up, down and drag), go to touch screen */
205     if(!entry) {
206         entry = QTAILQ_FIRST(&mouse_handlers);
207         mouse_event = entry->qemu_put_mouse_event;
208         mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
209         //INFO("input device: %s, event: %d\n", entry->qemu_put_mouse_event_name, buttons_state);
210     }
211 #else
212     entry = QTAILQ_FIRST(&mouse_handlers);
213
214     mouse_event = entry->qemu_put_mouse_event;
215     mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
216 #endif
217     if (mouse_event) {
218         if (entry->qemu_put_mouse_event_absolute) {
219             width = 0x7fff;
220             height = 0x7fff;
221         } else {
222             width = graphic_width - 1;
223             height = graphic_height - 1;
224         }
225
226         switch (graphic_rotate) {
227         case 0:
228             mouse_event(mouse_event_opaque,
229                         dx, dy, dz, buttons_state);
230             break;
231         case 90:
232             mouse_event(mouse_event_opaque,
233                         width - dy, dx, dz, buttons_state);
234             break;
235         case 180:
236             mouse_event(mouse_event_opaque,
237                         width - dx, height - dy, dz, buttons_state);
238             break;
239         case 270:
240             mouse_event(mouse_event_opaque,
241                         dy, height - dx, dz, buttons_state);
242             break;
243         }
244     }
245 }
246
247 int kbd_mouse_is_absolute(void)
248 {
249     if (QTAILQ_EMPTY(&mouse_handlers)) {
250         return 0;
251     }
252
253     return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
254 }
255
256 int kbd_mouse_has_absolute(void)
257 {
258     QEMUPutMouseEntry *entry;
259
260     QTAILQ_FOREACH(entry, &mouse_handlers, node) {
261         if (entry->qemu_put_mouse_event_absolute) {
262             return 1;
263         }
264     }
265
266     return 0;
267 }
268
269 MouseInfoList *qmp_query_mice(Error **errp)
270 {
271     MouseInfoList *mice_list = NULL;
272     QEMUPutMouseEntry *cursor;
273     bool current = true;
274
275     QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
276         MouseInfoList *info = g_malloc0(sizeof(*info));
277         info->value = g_malloc0(sizeof(*info->value));
278         info->value->name = g_strdup(cursor->qemu_put_mouse_event_name);
279         info->value->index = cursor->index;
280         info->value->absolute = !!cursor->qemu_put_mouse_event_absolute;
281         info->value->current = current;
282
283         current = false;
284
285         info->next = mice_list;
286         mice_list = info;
287     }
288
289     return mice_list;
290 }
291
292 void do_mouse_set(Monitor *mon, const QDict *qdict)
293 {
294     QEMUPutMouseEntry *cursor;
295     int index = qdict_get_int(qdict, "index");
296     int found = 0;
297
298     if (QTAILQ_EMPTY(&mouse_handlers)) {
299         monitor_printf(mon, "No mouse devices connected\n");
300         return;
301     }
302
303     QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
304         if (cursor->index == index) {
305             found = 1;
306             qemu_activate_mouse_event_handler(cursor);
307             break;
308         }
309     }
310
311     if (!found) {
312         monitor_printf(mon, "Mouse at given index not found\n");
313     }
314
315     check_mode_change();
316 }
317
318 void qemu_add_mouse_mode_change_notifier(Notifier *notify)
319 {
320     notifier_list_add(&mouse_mode_notifiers, notify);
321 }
322
323 void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
324 {
325     notifier_remove(notify);
326 }