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