Statistics
| Branch: | Revision:

root / input.c @ 76b523db

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