]> git.proxmox.com Git - qemu.git/blame - input.c
vnc: rename vnc-encoding-* vnc-enc-*
[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"
29#include "qjson.h"
30
8f0056b7
PB
31static QEMUPutKBDEvent *qemu_put_kbd_event;
32static void *qemu_put_kbd_event_opaque;
03a23a85 33static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_handlers);
6fef28ee
AL
34static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
35 QTAILQ_HEAD_INITIALIZER(mouse_handlers);
7e581fb3
AL
36static NotifierList mouse_mode_notifiers =
37 NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
8f0056b7
PB
38
39void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
40{
41 qemu_put_kbd_event_opaque = opaque;
42 qemu_put_kbd_event = func;
43}
44
46aaebff
JS
45void qemu_remove_kbd_event_handler(void)
46{
47 qemu_put_kbd_event_opaque = NULL;
48 qemu_put_kbd_event = NULL;
49}
50
7e581fb3
AL
51static void check_mode_change(void)
52{
53 static int current_is_absolute, current_has_absolute;
54 int is_absolute;
55 int has_absolute;
56
57 is_absolute = kbd_mouse_is_absolute();
58 has_absolute = kbd_mouse_has_absolute();
59
60 if (is_absolute != current_is_absolute ||
61 has_absolute != current_has_absolute) {
62 notifier_list_notify(&mouse_mode_notifiers);
63 }
64
65 current_is_absolute = is_absolute;
66 current_has_absolute = has_absolute;
67}
68
8f0056b7
PB
69QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
70 void *opaque, int absolute,
71 const char *name)
72{
6fef28ee
AL
73 QEMUPutMouseEntry *s;
74 static int mouse_index = 0;
8f0056b7
PB
75
76 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
77
78 s->qemu_put_mouse_event = func;
79 s->qemu_put_mouse_event_opaque = opaque;
80 s->qemu_put_mouse_event_absolute = absolute;
81 s->qemu_put_mouse_event_name = qemu_strdup(name);
6fef28ee 82 s->index = mouse_index++;
8f0056b7 83
6fef28ee 84 QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
8f0056b7 85
7e581fb3
AL
86 check_mode_change();
87
8f0056b7
PB
88 return s;
89}
90
6fef28ee 91void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
8f0056b7 92{
6fef28ee
AL
93 QTAILQ_REMOVE(&mouse_handlers, entry, node);
94 QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
8f0056b7 95
6fef28ee
AL
96 check_mode_change();
97}
8f0056b7 98
6fef28ee
AL
99void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
100{
101 QTAILQ_REMOVE(&mouse_handlers, entry, node);
8f0056b7
PB
102
103 qemu_free(entry->qemu_put_mouse_event_name);
104 qemu_free(entry);
7e581fb3
AL
105
106 check_mode_change();
8f0056b7
PB
107}
108
03a23a85
GH
109QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
110 void *opaque)
111{
112 QEMUPutLEDEntry *s;
113
114 s = qemu_mallocz(sizeof(QEMUPutLEDEntry));
115
116 s->put_led = func;
117 s->opaque = opaque;
118 QTAILQ_INSERT_TAIL(&led_handlers, s, next);
119 return s;
120}
121
122void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
123{
124 if (entry == NULL)
125 return;
126 QTAILQ_REMOVE(&led_handlers, entry, next);
127 qemu_free(entry);
128}
129
8f0056b7
PB
130void kbd_put_keycode(int keycode)
131{
132 if (qemu_put_kbd_event) {
133 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
134 }
135}
136
03a23a85
GH
137void kbd_put_ledstate(int ledstate)
138{
139 QEMUPutLEDEntry *cursor;
140
141 QTAILQ_FOREACH(cursor, &led_handlers, next) {
142 cursor->put_led(cursor->opaque, ledstate);
143 }
144}
145
8f0056b7
PB
146void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
147{
6fef28ee 148 QEMUPutMouseEntry *entry;
8f0056b7
PB
149 QEMUPutMouseEvent *mouse_event;
150 void *mouse_event_opaque;
151 int width;
152
6fef28ee 153 if (QTAILQ_EMPTY(&mouse_handlers)) {
8f0056b7
PB
154 return;
155 }
156
6fef28ee
AL
157 entry = QTAILQ_FIRST(&mouse_handlers);
158
159 mouse_event = entry->qemu_put_mouse_event;
160 mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
8f0056b7
PB
161
162 if (mouse_event) {
163 if (graphic_rotate) {
6fef28ee 164 if (entry->qemu_put_mouse_event_absolute)
8f0056b7
PB
165 width = 0x7fff;
166 else
167 width = graphic_width - 1;
168 mouse_event(mouse_event_opaque,
6fef28ee 169 width - dy, dx, dz, buttons_state);
8f0056b7
PB
170 } else
171 mouse_event(mouse_event_opaque,
6fef28ee 172 dx, dy, dz, buttons_state);
8f0056b7
PB
173 }
174}
175
176int kbd_mouse_is_absolute(void)
177{
6fef28ee 178 if (QTAILQ_EMPTY(&mouse_handlers)) {
8f0056b7 179 return 0;
6fef28ee 180 }
8f0056b7 181
6fef28ee 182 return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
8f0056b7
PB
183}
184
eb2e259d
AL
185int kbd_mouse_has_absolute(void)
186{
187 QEMUPutMouseEntry *entry;
188
189 QTAILQ_FOREACH(entry, &mouse_handlers, node) {
190 if (entry->qemu_put_mouse_event_absolute) {
191 return 1;
192 }
193 }
194
195 return 0;
196}
197
8f0056b7
PB
198static void info_mice_iter(QObject *data, void *opaque)
199{
200 QDict *mouse;
201 Monitor *mon = opaque;
202
203 mouse = qobject_to_qdict(data);
1aaee43c 204 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
8f0056b7 205 (qdict_get_bool(mouse, "current") ? '*' : ' '),
1aaee43c
AL
206 qdict_get_int(mouse, "index"), qdict_get_str(mouse, "name"),
207 qdict_get_bool(mouse, "absolute") ? " (absolute)" : "");
8f0056b7
PB
208}
209
210void do_info_mice_print(Monitor *mon, const QObject *data)
211{
212 QList *mice_list;
213
214 mice_list = qobject_to_qlist(data);
215 if (qlist_empty(mice_list)) {
216 monitor_printf(mon, "No mouse devices connected\n");
217 return;
218 }
219
220 qlist_iter(mice_list, info_mice_iter, mon);
221}
222
8f0056b7
PB
223void do_info_mice(Monitor *mon, QObject **ret_data)
224{
225 QEMUPutMouseEntry *cursor;
226 QList *mice_list;
6fef28ee 227 int current;
8f0056b7
PB
228
229 mice_list = qlist_new();
230
6fef28ee 231 if (QTAILQ_EMPTY(&mouse_handlers)) {
8f0056b7
PB
232 goto out;
233 }
234
6fef28ee
AL
235 current = QTAILQ_FIRST(&mouse_handlers)->index;
236
237 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
8f0056b7 238 QObject *obj;
1aaee43c
AL
239 obj = qobject_from_jsonf("{ 'name': %s,"
240 " 'index': %d,"
241 " 'current': %i,"
242 " 'absolute': %i }",
8f0056b7 243 cursor->qemu_put_mouse_event_name,
6fef28ee 244 cursor->index,
1aaee43c
AL
245 cursor->index == current,
246 !!cursor->qemu_put_mouse_event_absolute);
8f0056b7 247 qlist_append_obj(mice_list, obj);
8f0056b7
PB
248 }
249
250out:
251 *ret_data = QOBJECT(mice_list);
252}
253
254void do_mouse_set(Monitor *mon, const QDict *qdict)
255{
256 QEMUPutMouseEntry *cursor;
8f0056b7 257 int index = qdict_get_int(qdict, "index");
6fef28ee 258 int found = 0;
8f0056b7 259
6fef28ee 260 if (QTAILQ_EMPTY(&mouse_handlers)) {
8f0056b7
PB
261 monitor_printf(mon, "No mouse devices connected\n");
262 return;
263 }
264
6fef28ee
AL
265 QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
266 if (cursor->index == index) {
267 found = 1;
268 qemu_activate_mouse_event_handler(cursor);
269 break;
270 }
8f0056b7
PB
271 }
272
6fef28ee 273 if (!found) {
8f0056b7 274 monitor_printf(mon, "Mouse at given index not found\n");
6fef28ee 275 }
7e581fb3
AL
276
277 check_mode_change();
278}
279
280void qemu_add_mouse_mode_change_notifier(Notifier *notify)
281{
282 notifier_list_add(&mouse_mode_notifiers, notify);
283}
284
285void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
286{
287 notifier_list_remove(&mouse_mode_notifiers, notify);
8f0056b7 288}