]> rtime.felk.cvut.cz Git - lisovros/qemu_apohw.git/blob - input.c
apohw: initial keyboard implementation.
[lisovros/qemu_apohw.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
32 static QEMUPutKBDEvent *qemu_put_kbd_event;
33 static void *qemu_put_kbd_event_opaque;
34 static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_handlers);
35 static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
36     QTAILQ_HEAD_INITIALIZER(mouse_handlers);
37 static NotifierList mouse_mode_notifiers = 
38     NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
39
40 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
41 {
42     qemu_put_kbd_event_opaque = opaque;
43     qemu_put_kbd_event = func;
44 }
45
46 void qemu_remove_kbd_event_handler(void)
47 {
48     qemu_put_kbd_event_opaque = NULL;
49     qemu_put_kbd_event = NULL;
50 }
51
52 static void check_mode_change(void)
53 {
54     static int current_is_absolute, current_has_absolute;
55     int is_absolute;
56     int has_absolute;
57
58     is_absolute = kbd_mouse_is_absolute();
59     has_absolute = kbd_mouse_has_absolute();
60
61     if (is_absolute != current_is_absolute ||
62         has_absolute != current_has_absolute) {
63         notifier_list_notify(&mouse_mode_notifiers, NULL);
64     }
65
66     current_is_absolute = is_absolute;
67     current_has_absolute = has_absolute;
68 }
69
70 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
71                                                 void *opaque, int absolute,
72                                                 const char *name)
73 {
74     QEMUPutMouseEntry *s;
75     static int mouse_index = 0;
76
77     s = g_malloc0(sizeof(QEMUPutMouseEntry));
78
79     s->qemu_put_mouse_event = func;
80     s->qemu_put_mouse_event_opaque = opaque;
81     s->qemu_put_mouse_event_absolute = absolute;
82     s->qemu_put_mouse_event_name = g_strdup(name);
83     s->index = mouse_index++;
84
85     QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
86
87     check_mode_change();
88
89     return s;
90 }
91
92 void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
93 {
94     QTAILQ_REMOVE(&mouse_handlers, entry, node);
95     QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
96
97     check_mode_change();
98 }
99
100 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
101 {
102     QTAILQ_REMOVE(&mouse_handlers, entry, node);
103
104     g_free(entry->qemu_put_mouse_event_name);
105     g_free(entry);
106
107     check_mode_change();
108 }
109
110 QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
111                                             void *opaque)
112 {
113     QEMUPutLEDEntry *s;
114
115     s = g_malloc0(sizeof(QEMUPutLEDEntry));
116
117     s->put_led = func;
118     s->opaque = opaque;
119     QTAILQ_INSERT_TAIL(&led_handlers, s, next);
120     return s;
121 }
122
123 void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
124 {
125     if (entry == NULL)
126         return;
127     QTAILQ_REMOVE(&led_handlers, entry, next);
128     g_free(entry);
129 }
130
131 void kbd_put_keycode(int keycode)
132 {
133     if (qemu_put_kbd_event) {
134         qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
135     }
136 }
137
138 void kbd_put_ledstate(int ledstate)
139 {
140     QEMUPutLEDEntry *cursor;
141
142     QTAILQ_FOREACH(cursor, &led_handlers, next) {
143         cursor->put_led(cursor->opaque, ledstate);
144     }
145 }
146
147 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
148 {
149     QEMUPutMouseEntry *entry;
150     QEMUPutMouseEvent *mouse_event;
151     void *mouse_event_opaque;
152     int width, height;
153
154     if (QTAILQ_EMPTY(&mouse_handlers)) {
155         return;
156     }
157
158     entry = QTAILQ_FIRST(&mouse_handlers);
159
160     mouse_event = entry->qemu_put_mouse_event;
161     mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
162
163     if (mouse_event) {
164         if (entry->qemu_put_mouse_event_absolute) {
165             width = 0x7fff;
166             height = 0x7fff;
167         } else {
168             width = graphic_width - 1;
169             height = graphic_height - 1;
170         }
171
172         switch (graphic_rotate) {
173         case 0:
174             mouse_event(mouse_event_opaque,
175                         dx, dy, dz, buttons_state);
176             break;
177         case 90:
178             mouse_event(mouse_event_opaque,
179                         width - dy, dx, dz, buttons_state);
180             break;
181         case 180:
182             mouse_event(mouse_event_opaque,
183                         width - dx, height - dy, dz, buttons_state);
184             break;
185         case 270:
186             mouse_event(mouse_event_opaque,
187                         dy, height - dx, dz, buttons_state);
188             break;
189         }
190     }
191 }
192
193 int kbd_mouse_is_absolute(void)
194 {
195     if (QTAILQ_EMPTY(&mouse_handlers)) {
196         return 0;
197     }
198
199     return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
200 }
201
202 int kbd_mouse_has_absolute(void)
203 {
204     QEMUPutMouseEntry *entry;
205
206     QTAILQ_FOREACH(entry, &mouse_handlers, node) {
207         if (entry->qemu_put_mouse_event_absolute) {
208             return 1;
209         }
210     }
211
212     return 0;
213 }
214
215 MouseInfoList *qmp_query_mice(Error **errp)
216 {
217     MouseInfoList *mice_list = NULL;
218     QEMUPutMouseEntry *cursor;
219     bool current = true;
220
221     QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
222         MouseInfoList *info = g_malloc0(sizeof(*info));
223         info->value = g_malloc0(sizeof(*info->value));
224         info->value->name = g_strdup(cursor->qemu_put_mouse_event_name);
225         info->value->index = cursor->index;
226         info->value->absolute = !!cursor->qemu_put_mouse_event_absolute;
227         info->value->current = current;
228
229         current = false;
230
231         info->next = mice_list;
232         mice_list = info;
233     }
234
235     return mice_list;
236 }
237
238 void do_mouse_set(Monitor *mon, const QDict *qdict)
239 {
240     QEMUPutMouseEntry *cursor;
241     int index = qdict_get_int(qdict, "index");
242     int found = 0;
243
244     if (QTAILQ_EMPTY(&mouse_handlers)) {
245         monitor_printf(mon, "No mouse devices connected\n");
246         return;
247     }
248
249     QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
250         if (cursor->index == index) {
251             found = 1;
252             qemu_activate_mouse_event_handler(cursor);
253             break;
254         }
255     }
256
257     if (!found) {
258         monitor_printf(mon, "Mouse at given index not found\n");
259     }
260
261     check_mode_change();
262 }
263
264 void qemu_add_mouse_mode_change_notifier(Notifier *notify)
265 {
266     notifier_list_add(&mouse_mode_notifiers, notify);
267 }
268
269 void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
270 {
271     notifier_list_remove(&mouse_mode_notifiers, notify);
272 }