Statistics
| Branch: | Revision:

root / input.c @ a74cdab4

History | View | Annotate | Download (7.7 kB)

1 8f0056b7 Paolo Bonzini
/*
2 8f0056b7 Paolo Bonzini
 * QEMU System Emulator
3 8f0056b7 Paolo Bonzini
 *
4 8f0056b7 Paolo Bonzini
 * Copyright (c) 2003-2008 Fabrice Bellard
5 8f0056b7 Paolo Bonzini
 *
6 8f0056b7 Paolo Bonzini
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 8f0056b7 Paolo Bonzini
 * of this software and associated documentation files (the "Software"), to deal
8 8f0056b7 Paolo Bonzini
 * in the Software without restriction, including without limitation the rights
9 8f0056b7 Paolo Bonzini
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 8f0056b7 Paolo Bonzini
 * copies of the Software, and to permit persons to whom the Software is
11 8f0056b7 Paolo Bonzini
 * furnished to do so, subject to the following conditions:
12 8f0056b7 Paolo Bonzini
 *
13 8f0056b7 Paolo Bonzini
 * The above copyright notice and this permission notice shall be included in
14 8f0056b7 Paolo Bonzini
 * all copies or substantial portions of the Software.
15 8f0056b7 Paolo Bonzini
 *
16 8f0056b7 Paolo Bonzini
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 8f0056b7 Paolo Bonzini
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 8f0056b7 Paolo Bonzini
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 8f0056b7 Paolo Bonzini
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 8f0056b7 Paolo Bonzini
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 8f0056b7 Paolo Bonzini
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 8f0056b7 Paolo Bonzini
 * THE SOFTWARE.
23 8f0056b7 Paolo Bonzini
 */
24 8f0056b7 Paolo Bonzini
25 8f0056b7 Paolo Bonzini
#include "sysemu.h"
26 8f0056b7 Paolo Bonzini
#include "net.h"
27 8f0056b7 Paolo Bonzini
#include "monitor.h"
28 8f0056b7 Paolo Bonzini
#include "console.h"
29 8f0056b7 Paolo Bonzini
#include "qjson.h"
30 8f0056b7 Paolo Bonzini
31 8f0056b7 Paolo Bonzini
static QEMUPutKBDEvent *qemu_put_kbd_event;
32 8f0056b7 Paolo Bonzini
static void *qemu_put_kbd_event_opaque;
33 03a23a85 Gerd Hoffmann
static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers = QTAILQ_HEAD_INITIALIZER(led_handlers);
34 6fef28ee Anthony Liguori
static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
35 6fef28ee Anthony Liguori
    QTAILQ_HEAD_INITIALIZER(mouse_handlers);
36 7e581fb3 Anthony Liguori
static NotifierList mouse_mode_notifiers = 
37 7e581fb3 Anthony Liguori
    NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
38 8f0056b7 Paolo Bonzini
39 8f0056b7 Paolo Bonzini
void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
40 8f0056b7 Paolo Bonzini
{
41 8f0056b7 Paolo Bonzini
    qemu_put_kbd_event_opaque = opaque;
42 8f0056b7 Paolo Bonzini
    qemu_put_kbd_event = func;
43 8f0056b7 Paolo Bonzini
}
44 8f0056b7 Paolo Bonzini
45 46aaebff Jes Sorensen
void qemu_remove_kbd_event_handler(void)
46 46aaebff Jes Sorensen
{
47 46aaebff Jes Sorensen
    qemu_put_kbd_event_opaque = NULL;
48 46aaebff Jes Sorensen
    qemu_put_kbd_event = NULL;
49 46aaebff Jes Sorensen
}
50 46aaebff Jes Sorensen
51 7e581fb3 Anthony Liguori
static void check_mode_change(void)
52 7e581fb3 Anthony Liguori
{
53 7e581fb3 Anthony Liguori
    static int current_is_absolute, current_has_absolute;
54 7e581fb3 Anthony Liguori
    int is_absolute;
55 7e581fb3 Anthony Liguori
    int has_absolute;
56 7e581fb3 Anthony Liguori
57 7e581fb3 Anthony Liguori
    is_absolute = kbd_mouse_is_absolute();
58 7e581fb3 Anthony Liguori
    has_absolute = kbd_mouse_has_absolute();
59 7e581fb3 Anthony Liguori
60 7e581fb3 Anthony Liguori
    if (is_absolute != current_is_absolute ||
61 7e581fb3 Anthony Liguori
        has_absolute != current_has_absolute) {
62 7e581fb3 Anthony Liguori
        notifier_list_notify(&mouse_mode_notifiers);
63 7e581fb3 Anthony Liguori
    }
64 7e581fb3 Anthony Liguori
65 7e581fb3 Anthony Liguori
    current_is_absolute = is_absolute;
66 7e581fb3 Anthony Liguori
    current_has_absolute = has_absolute;
67 7e581fb3 Anthony Liguori
}
68 7e581fb3 Anthony Liguori
69 8f0056b7 Paolo Bonzini
QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
70 8f0056b7 Paolo Bonzini
                                                void *opaque, int absolute,
71 8f0056b7 Paolo Bonzini
                                                const char *name)
72 8f0056b7 Paolo Bonzini
{
73 6fef28ee Anthony Liguori
    QEMUPutMouseEntry *s;
74 6fef28ee Anthony Liguori
    static int mouse_index = 0;
75 8f0056b7 Paolo Bonzini
76 8f0056b7 Paolo Bonzini
    s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
77 8f0056b7 Paolo Bonzini
78 8f0056b7 Paolo Bonzini
    s->qemu_put_mouse_event = func;
79 8f0056b7 Paolo Bonzini
    s->qemu_put_mouse_event_opaque = opaque;
80 8f0056b7 Paolo Bonzini
    s->qemu_put_mouse_event_absolute = absolute;
81 8f0056b7 Paolo Bonzini
    s->qemu_put_mouse_event_name = qemu_strdup(name);
82 6fef28ee Anthony Liguori
    s->index = mouse_index++;
83 8f0056b7 Paolo Bonzini
84 6fef28ee Anthony Liguori
    QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
85 8f0056b7 Paolo Bonzini
86 7e581fb3 Anthony Liguori
    check_mode_change();
87 7e581fb3 Anthony Liguori
88 8f0056b7 Paolo Bonzini
    return s;
89 8f0056b7 Paolo Bonzini
}
90 8f0056b7 Paolo Bonzini
91 6fef28ee Anthony Liguori
void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
92 8f0056b7 Paolo Bonzini
{
93 6fef28ee Anthony Liguori
    QTAILQ_REMOVE(&mouse_handlers, entry, node);
94 6fef28ee Anthony Liguori
    QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
95 8f0056b7 Paolo Bonzini
96 6fef28ee Anthony Liguori
    check_mode_change();
97 6fef28ee Anthony Liguori
}
98 8f0056b7 Paolo Bonzini
99 6fef28ee Anthony Liguori
void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
100 6fef28ee Anthony Liguori
{
101 6fef28ee Anthony Liguori
    QTAILQ_REMOVE(&mouse_handlers, entry, node);
102 8f0056b7 Paolo Bonzini
103 8f0056b7 Paolo Bonzini
    qemu_free(entry->qemu_put_mouse_event_name);
104 8f0056b7 Paolo Bonzini
    qemu_free(entry);
105 7e581fb3 Anthony Liguori
106 7e581fb3 Anthony Liguori
    check_mode_change();
107 8f0056b7 Paolo Bonzini
}
108 8f0056b7 Paolo Bonzini
109 03a23a85 Gerd Hoffmann
QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
110 03a23a85 Gerd Hoffmann
                                            void *opaque)
111 03a23a85 Gerd Hoffmann
{
112 03a23a85 Gerd Hoffmann
    QEMUPutLEDEntry *s;
113 03a23a85 Gerd Hoffmann
114 03a23a85 Gerd Hoffmann
    s = qemu_mallocz(sizeof(QEMUPutLEDEntry));
115 03a23a85 Gerd Hoffmann
116 03a23a85 Gerd Hoffmann
    s->put_led = func;
117 03a23a85 Gerd Hoffmann
    s->opaque = opaque;
118 03a23a85 Gerd Hoffmann
    QTAILQ_INSERT_TAIL(&led_handlers, s, next);
119 03a23a85 Gerd Hoffmann
    return s;
120 03a23a85 Gerd Hoffmann
}
121 03a23a85 Gerd Hoffmann
122 03a23a85 Gerd Hoffmann
void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
123 03a23a85 Gerd Hoffmann
{
124 03a23a85 Gerd Hoffmann
    if (entry == NULL)
125 03a23a85 Gerd Hoffmann
        return;
126 03a23a85 Gerd Hoffmann
    QTAILQ_REMOVE(&led_handlers, entry, next);
127 03a23a85 Gerd Hoffmann
    qemu_free(entry);
128 03a23a85 Gerd Hoffmann
}
129 03a23a85 Gerd Hoffmann
130 8f0056b7 Paolo Bonzini
void kbd_put_keycode(int keycode)
131 8f0056b7 Paolo Bonzini
{
132 8f0056b7 Paolo Bonzini
    if (qemu_put_kbd_event) {
133 8f0056b7 Paolo Bonzini
        qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
134 8f0056b7 Paolo Bonzini
    }
135 8f0056b7 Paolo Bonzini
}
136 8f0056b7 Paolo Bonzini
137 03a23a85 Gerd Hoffmann
void kbd_put_ledstate(int ledstate)
138 03a23a85 Gerd Hoffmann
{
139 03a23a85 Gerd Hoffmann
    QEMUPutLEDEntry *cursor;
140 03a23a85 Gerd Hoffmann
141 03a23a85 Gerd Hoffmann
    QTAILQ_FOREACH(cursor, &led_handlers, next) {
142 03a23a85 Gerd Hoffmann
        cursor->put_led(cursor->opaque, ledstate);
143 03a23a85 Gerd Hoffmann
    }
144 03a23a85 Gerd Hoffmann
}
145 03a23a85 Gerd Hoffmann
146 8f0056b7 Paolo Bonzini
void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
147 8f0056b7 Paolo Bonzini
{
148 6fef28ee Anthony Liguori
    QEMUPutMouseEntry *entry;
149 8f0056b7 Paolo Bonzini
    QEMUPutMouseEvent *mouse_event;
150 8f0056b7 Paolo Bonzini
    void *mouse_event_opaque;
151 8f0056b7 Paolo Bonzini
    int width;
152 8f0056b7 Paolo Bonzini
153 6fef28ee Anthony Liguori
    if (QTAILQ_EMPTY(&mouse_handlers)) {
154 8f0056b7 Paolo Bonzini
        return;
155 8f0056b7 Paolo Bonzini
    }
156 8f0056b7 Paolo Bonzini
157 6fef28ee Anthony Liguori
    entry = QTAILQ_FIRST(&mouse_handlers);
158 6fef28ee Anthony Liguori
159 6fef28ee Anthony Liguori
    mouse_event = entry->qemu_put_mouse_event;
160 6fef28ee Anthony Liguori
    mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
161 8f0056b7 Paolo Bonzini
162 8f0056b7 Paolo Bonzini
    if (mouse_event) {
163 8f0056b7 Paolo Bonzini
        if (graphic_rotate) {
164 97697373 Brad Hards
            if (entry->qemu_put_mouse_event_absolute) {
165 8f0056b7 Paolo Bonzini
                width = 0x7fff;
166 97697373 Brad Hards
            } else {
167 8f0056b7 Paolo Bonzini
                width = graphic_width - 1;
168 97697373 Brad Hards
            }
169 97697373 Brad Hards
            mouse_event(mouse_event_opaque, width - dy, dx, dz, buttons_state);
170 97697373 Brad Hards
        } else {
171 97697373 Brad Hards
            mouse_event(mouse_event_opaque, dx, dy, dz, buttons_state);
172 97697373 Brad Hards
        }
173 8f0056b7 Paolo Bonzini
    }
174 8f0056b7 Paolo Bonzini
}
175 8f0056b7 Paolo Bonzini
176 8f0056b7 Paolo Bonzini
int kbd_mouse_is_absolute(void)
177 8f0056b7 Paolo Bonzini
{
178 6fef28ee Anthony Liguori
    if (QTAILQ_EMPTY(&mouse_handlers)) {
179 8f0056b7 Paolo Bonzini
        return 0;
180 6fef28ee Anthony Liguori
    }
181 8f0056b7 Paolo Bonzini
182 6fef28ee Anthony Liguori
    return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
183 8f0056b7 Paolo Bonzini
}
184 8f0056b7 Paolo Bonzini
185 eb2e259d Anthony Liguori
int kbd_mouse_has_absolute(void)
186 eb2e259d Anthony Liguori
{
187 eb2e259d Anthony Liguori
    QEMUPutMouseEntry *entry;
188 eb2e259d Anthony Liguori
189 eb2e259d Anthony Liguori
    QTAILQ_FOREACH(entry, &mouse_handlers, node) {
190 eb2e259d Anthony Liguori
        if (entry->qemu_put_mouse_event_absolute) {
191 eb2e259d Anthony Liguori
            return 1;
192 eb2e259d Anthony Liguori
        }
193 eb2e259d Anthony Liguori
    }
194 eb2e259d Anthony Liguori
195 eb2e259d Anthony Liguori
    return 0;
196 eb2e259d Anthony Liguori
}
197 eb2e259d Anthony Liguori
198 8f0056b7 Paolo Bonzini
static void info_mice_iter(QObject *data, void *opaque)
199 8f0056b7 Paolo Bonzini
{
200 8f0056b7 Paolo Bonzini
    QDict *mouse;
201 8f0056b7 Paolo Bonzini
    Monitor *mon = opaque;
202 8f0056b7 Paolo Bonzini
203 8f0056b7 Paolo Bonzini
    mouse = qobject_to_qdict(data);
204 1aaee43c Anthony Liguori
    monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
205 8f0056b7 Paolo Bonzini
                  (qdict_get_bool(mouse, "current") ? '*' : ' '),
206 1aaee43c Anthony Liguori
                   qdict_get_int(mouse, "index"), qdict_get_str(mouse, "name"),
207 1aaee43c Anthony Liguori
                   qdict_get_bool(mouse, "absolute") ? " (absolute)" : "");
208 8f0056b7 Paolo Bonzini
}
209 8f0056b7 Paolo Bonzini
210 8f0056b7 Paolo Bonzini
void do_info_mice_print(Monitor *mon, const QObject *data)
211 8f0056b7 Paolo Bonzini
{
212 8f0056b7 Paolo Bonzini
    QList *mice_list;
213 8f0056b7 Paolo Bonzini
214 8f0056b7 Paolo Bonzini
    mice_list = qobject_to_qlist(data);
215 8f0056b7 Paolo Bonzini
    if (qlist_empty(mice_list)) {
216 8f0056b7 Paolo Bonzini
        monitor_printf(mon, "No mouse devices connected\n");
217 8f0056b7 Paolo Bonzini
        return;
218 8f0056b7 Paolo Bonzini
    }
219 8f0056b7 Paolo Bonzini
220 8f0056b7 Paolo Bonzini
    qlist_iter(mice_list, info_mice_iter, mon);
221 8f0056b7 Paolo Bonzini
}
222 8f0056b7 Paolo Bonzini
223 8f0056b7 Paolo Bonzini
void do_info_mice(Monitor *mon, QObject **ret_data)
224 8f0056b7 Paolo Bonzini
{
225 8f0056b7 Paolo Bonzini
    QEMUPutMouseEntry *cursor;
226 8f0056b7 Paolo Bonzini
    QList *mice_list;
227 6fef28ee Anthony Liguori
    int current;
228 8f0056b7 Paolo Bonzini
229 8f0056b7 Paolo Bonzini
    mice_list = qlist_new();
230 8f0056b7 Paolo Bonzini
231 6fef28ee Anthony Liguori
    if (QTAILQ_EMPTY(&mouse_handlers)) {
232 8f0056b7 Paolo Bonzini
        goto out;
233 8f0056b7 Paolo Bonzini
    }
234 8f0056b7 Paolo Bonzini
235 6fef28ee Anthony Liguori
    current = QTAILQ_FIRST(&mouse_handlers)->index;
236 6fef28ee Anthony Liguori
237 6fef28ee Anthony Liguori
    QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
238 8f0056b7 Paolo Bonzini
        QObject *obj;
239 1aaee43c Anthony Liguori
        obj = qobject_from_jsonf("{ 'name': %s,"
240 1aaee43c Anthony Liguori
                                 "  'index': %d,"
241 1aaee43c Anthony Liguori
                                 "  'current': %i,"
242 1aaee43c Anthony Liguori
                                 "  'absolute': %i }",
243 8f0056b7 Paolo Bonzini
                                 cursor->qemu_put_mouse_event_name,
244 6fef28ee Anthony Liguori
                                 cursor->index,
245 1aaee43c Anthony Liguori
                                 cursor->index == current,
246 1aaee43c Anthony Liguori
                                 !!cursor->qemu_put_mouse_event_absolute);
247 8f0056b7 Paolo Bonzini
        qlist_append_obj(mice_list, obj);
248 8f0056b7 Paolo Bonzini
    }
249 8f0056b7 Paolo Bonzini
250 8f0056b7 Paolo Bonzini
out:
251 8f0056b7 Paolo Bonzini
    *ret_data = QOBJECT(mice_list);
252 8f0056b7 Paolo Bonzini
}
253 8f0056b7 Paolo Bonzini
254 8f0056b7 Paolo Bonzini
void do_mouse_set(Monitor *mon, const QDict *qdict)
255 8f0056b7 Paolo Bonzini
{
256 8f0056b7 Paolo Bonzini
    QEMUPutMouseEntry *cursor;
257 8f0056b7 Paolo Bonzini
    int index = qdict_get_int(qdict, "index");
258 6fef28ee Anthony Liguori
    int found = 0;
259 8f0056b7 Paolo Bonzini
260 6fef28ee Anthony Liguori
    if (QTAILQ_EMPTY(&mouse_handlers)) {
261 8f0056b7 Paolo Bonzini
        monitor_printf(mon, "No mouse devices connected\n");
262 8f0056b7 Paolo Bonzini
        return;
263 8f0056b7 Paolo Bonzini
    }
264 8f0056b7 Paolo Bonzini
265 6fef28ee Anthony Liguori
    QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
266 6fef28ee Anthony Liguori
        if (cursor->index == index) {
267 6fef28ee Anthony Liguori
            found = 1;
268 6fef28ee Anthony Liguori
            qemu_activate_mouse_event_handler(cursor);
269 6fef28ee Anthony Liguori
            break;
270 6fef28ee Anthony Liguori
        }
271 8f0056b7 Paolo Bonzini
    }
272 8f0056b7 Paolo Bonzini
273 6fef28ee Anthony Liguori
    if (!found) {
274 8f0056b7 Paolo Bonzini
        monitor_printf(mon, "Mouse at given index not found\n");
275 6fef28ee Anthony Liguori
    }
276 7e581fb3 Anthony Liguori
277 7e581fb3 Anthony Liguori
    check_mode_change();
278 7e581fb3 Anthony Liguori
}
279 7e581fb3 Anthony Liguori
280 7e581fb3 Anthony Liguori
void qemu_add_mouse_mode_change_notifier(Notifier *notify)
281 7e581fb3 Anthony Liguori
{
282 7e581fb3 Anthony Liguori
    notifier_list_add(&mouse_mode_notifiers, notify);
283 7e581fb3 Anthony Liguori
}
284 7e581fb3 Anthony Liguori
285 7e581fb3 Anthony Liguori
void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
286 7e581fb3 Anthony Liguori
{
287 7e581fb3 Anthony Liguori
    notifier_list_remove(&mouse_mode_notifiers, notify);
288 8f0056b7 Paolo Bonzini
}