~ubuntu-branches/ubuntu/vivid/qemu/vivid

« back to all changes in this revision

Viewing changes to ui/input.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2014-02-25 22:31:43 UTC
  • mfrom: (1.8.5)
  • Revision ID: package-import@ubuntu.com-20140225223143-odhqxfc60wxrjl15
Tags: 2.0.0~rc1+dfsg-0ubuntu1
* Merge 2.0.0-rc1
* debian/rules: consolidate ppc filter entries.
* Move qemu-system-arch64 into qemu-system-arm
* debian/patches/define-trusty-machine-type.patch: define a trusty machine
  type, currently the same as pc-i440fx-2.0, to put is in a better position
  to enable live migrations from trusty onward.  (LP: #1294823)
* debian/control: build-dep on libfdt >= 1.4.0  (LP: #1295072)
* Merge latest upstream git to commit dc9528f
* Debian/rules:
  - remove -enable-uname-release=2.6.32
  - don't make the aarch64 target Ubuntu-specific.
* Remove patches which are now upstream:
  - fix-smb-security-share.patch
  - slirp-smb-redirect-port-445-too.patch 
  - linux-user-Implement-sendmmsg-syscall.patch (better version is upstream)
  - signal-added-a-wrapper-for-sigprocmask-function.patch
  - ubuntu/signal-sigsegv-protection-on-do_sigprocmask.patch
  - ubuntu/Don-t-block-SIGSEGV-at-more-places.patch
  - ubuntu/ppc-force-cpu-threads-count-to-be-power-of-2.patch
* add link for /usr/share/qemu/bios-256k.bin
* Remove all linaro patches.
* Remove all arm64/ patches.  Many but not all are upstream.
* Remove CVE-2013-4377.patch which is upstream.
* debian/control-in: don't make qemu-system-aarch64 ubuntu-specific

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
1
#include "sysemu/sysemu.h"
26
 
#include "monitor/monitor.h"
 
2
#include "qapi-types.h"
 
3
#include "qmp-commands.h"
 
4
#include "trace.h"
 
5
#include "ui/input.h"
27
6
#include "ui/console.h"
28
 
#include "qapi/error.h"
29
 
#include "qmp-commands.h"
30
 
#include "qapi-types.h"
31
 
#include "ui/keymaps.h"
32
 
 
33
 
struct QEMUPutMouseEntry {
34
 
    QEMUPutMouseEvent *qemu_put_mouse_event;
35
 
    void *qemu_put_mouse_event_opaque;
36
 
    int qemu_put_mouse_event_absolute;
37
 
    char *qemu_put_mouse_event_name;
38
 
 
39
 
    int index;
40
 
 
41
 
    /* used internally by qemu for handling mice */
42
 
    QTAILQ_ENTRY(QEMUPutMouseEntry) node;
43
 
};
44
 
 
45
 
struct QEMUPutKbdEntry {
46
 
    QEMUPutKBDEvent *put_kbd;
47
 
    void *opaque;
48
 
    QTAILQ_ENTRY(QEMUPutKbdEntry) next;
49
 
};
50
 
 
51
 
struct QEMUPutLEDEntry {
52
 
    QEMUPutLEDEvent *put_led;
53
 
    void *opaque;
54
 
    QTAILQ_ENTRY(QEMUPutLEDEntry) next;
55
 
};
56
 
 
57
 
static QTAILQ_HEAD(, QEMUPutLEDEntry) led_handlers =
58
 
    QTAILQ_HEAD_INITIALIZER(led_handlers);
59
 
static QTAILQ_HEAD(, QEMUPutKbdEntry) kbd_handlers =
60
 
    QTAILQ_HEAD_INITIALIZER(kbd_handlers);
61
 
static QTAILQ_HEAD(, QEMUPutMouseEntry) mouse_handlers =
62
 
    QTAILQ_HEAD_INITIALIZER(mouse_handlers);
 
7
 
 
8
struct QemuInputHandlerState {
 
9
    DeviceState       *dev;
 
10
    QemuInputHandler  *handler;
 
11
    int               id;
 
12
    int               events;
 
13
    QTAILQ_ENTRY(QemuInputHandlerState) node;
 
14
};
 
15
static QTAILQ_HEAD(, QemuInputHandlerState) handlers =
 
16
    QTAILQ_HEAD_INITIALIZER(handlers);
63
17
static NotifierList mouse_mode_notifiers =
64
18
    NOTIFIER_LIST_INITIALIZER(mouse_mode_notifiers);
65
19
 
66
 
static const int key_defs[] = {
67
 
    [Q_KEY_CODE_SHIFT] = 0x2a,
68
 
    [Q_KEY_CODE_SHIFT_R] = 0x36,
69
 
 
70
 
    [Q_KEY_CODE_ALT] = 0x38,
71
 
    [Q_KEY_CODE_ALT_R] = 0xb8,
72
 
    [Q_KEY_CODE_ALTGR] = 0x64,
73
 
    [Q_KEY_CODE_ALTGR_R] = 0xe4,
74
 
    [Q_KEY_CODE_CTRL] = 0x1d,
75
 
    [Q_KEY_CODE_CTRL_R] = 0x9d,
76
 
 
77
 
    [Q_KEY_CODE_MENU] = 0xdd,
78
 
 
79
 
    [Q_KEY_CODE_ESC] = 0x01,
80
 
 
81
 
    [Q_KEY_CODE_1] = 0x02,
82
 
    [Q_KEY_CODE_2] = 0x03,
83
 
    [Q_KEY_CODE_3] = 0x04,
84
 
    [Q_KEY_CODE_4] = 0x05,
85
 
    [Q_KEY_CODE_5] = 0x06,
86
 
    [Q_KEY_CODE_6] = 0x07,
87
 
    [Q_KEY_CODE_7] = 0x08,
88
 
    [Q_KEY_CODE_8] = 0x09,
89
 
    [Q_KEY_CODE_9] = 0x0a,
90
 
    [Q_KEY_CODE_0] = 0x0b,
91
 
    [Q_KEY_CODE_MINUS] = 0x0c,
92
 
    [Q_KEY_CODE_EQUAL] = 0x0d,
93
 
    [Q_KEY_CODE_BACKSPACE] = 0x0e,
94
 
 
95
 
    [Q_KEY_CODE_TAB] = 0x0f,
96
 
    [Q_KEY_CODE_Q] = 0x10,
97
 
    [Q_KEY_CODE_W] = 0x11,
98
 
    [Q_KEY_CODE_E] = 0x12,
99
 
    [Q_KEY_CODE_R] = 0x13,
100
 
    [Q_KEY_CODE_T] = 0x14,
101
 
    [Q_KEY_CODE_Y] = 0x15,
102
 
    [Q_KEY_CODE_U] = 0x16,
103
 
    [Q_KEY_CODE_I] = 0x17,
104
 
    [Q_KEY_CODE_O] = 0x18,
105
 
    [Q_KEY_CODE_P] = 0x19,
106
 
    [Q_KEY_CODE_BRACKET_LEFT] = 0x1a,
107
 
    [Q_KEY_CODE_BRACKET_RIGHT] = 0x1b,
108
 
    [Q_KEY_CODE_RET] = 0x1c,
109
 
 
110
 
    [Q_KEY_CODE_A] = 0x1e,
111
 
    [Q_KEY_CODE_S] = 0x1f,
112
 
    [Q_KEY_CODE_D] = 0x20,
113
 
    [Q_KEY_CODE_F] = 0x21,
114
 
    [Q_KEY_CODE_G] = 0x22,
115
 
    [Q_KEY_CODE_H] = 0x23,
116
 
    [Q_KEY_CODE_J] = 0x24,
117
 
    [Q_KEY_CODE_K] = 0x25,
118
 
    [Q_KEY_CODE_L] = 0x26,
119
 
    [Q_KEY_CODE_SEMICOLON] = 0x27,
120
 
    [Q_KEY_CODE_APOSTROPHE] = 0x28,
121
 
    [Q_KEY_CODE_GRAVE_ACCENT] = 0x29,
122
 
 
123
 
    [Q_KEY_CODE_BACKSLASH] = 0x2b,
124
 
    [Q_KEY_CODE_Z] = 0x2c,
125
 
    [Q_KEY_CODE_X] = 0x2d,
126
 
    [Q_KEY_CODE_C] = 0x2e,
127
 
    [Q_KEY_CODE_V] = 0x2f,
128
 
    [Q_KEY_CODE_B] = 0x30,
129
 
    [Q_KEY_CODE_N] = 0x31,
130
 
    [Q_KEY_CODE_M] = 0x32,
131
 
    [Q_KEY_CODE_COMMA] = 0x33,
132
 
    [Q_KEY_CODE_DOT] = 0x34,
133
 
    [Q_KEY_CODE_SLASH] = 0x35,
134
 
 
135
 
    [Q_KEY_CODE_ASTERISK] = 0x37,
136
 
 
137
 
    [Q_KEY_CODE_SPC] = 0x39,
138
 
    [Q_KEY_CODE_CAPS_LOCK] = 0x3a,
139
 
    [Q_KEY_CODE_F1] = 0x3b,
140
 
    [Q_KEY_CODE_F2] = 0x3c,
141
 
    [Q_KEY_CODE_F3] = 0x3d,
142
 
    [Q_KEY_CODE_F4] = 0x3e,
143
 
    [Q_KEY_CODE_F5] = 0x3f,
144
 
    [Q_KEY_CODE_F6] = 0x40,
145
 
    [Q_KEY_CODE_F7] = 0x41,
146
 
    [Q_KEY_CODE_F8] = 0x42,
147
 
    [Q_KEY_CODE_F9] = 0x43,
148
 
    [Q_KEY_CODE_F10] = 0x44,
149
 
    [Q_KEY_CODE_NUM_LOCK] = 0x45,
150
 
    [Q_KEY_CODE_SCROLL_LOCK] = 0x46,
151
 
 
152
 
    [Q_KEY_CODE_KP_DIVIDE] = 0xb5,
153
 
    [Q_KEY_CODE_KP_MULTIPLY] = 0x37,
154
 
    [Q_KEY_CODE_KP_SUBTRACT] = 0x4a,
155
 
    [Q_KEY_CODE_KP_ADD] = 0x4e,
156
 
    [Q_KEY_CODE_KP_ENTER] = 0x9c,
157
 
    [Q_KEY_CODE_KP_DECIMAL] = 0x53,
158
 
    [Q_KEY_CODE_SYSRQ] = 0x54,
159
 
 
160
 
    [Q_KEY_CODE_KP_0] = 0x52,
161
 
    [Q_KEY_CODE_KP_1] = 0x4f,
162
 
    [Q_KEY_CODE_KP_2] = 0x50,
163
 
    [Q_KEY_CODE_KP_3] = 0x51,
164
 
    [Q_KEY_CODE_KP_4] = 0x4b,
165
 
    [Q_KEY_CODE_KP_5] = 0x4c,
166
 
    [Q_KEY_CODE_KP_6] = 0x4d,
167
 
    [Q_KEY_CODE_KP_7] = 0x47,
168
 
    [Q_KEY_CODE_KP_8] = 0x48,
169
 
    [Q_KEY_CODE_KP_9] = 0x49,
170
 
 
171
 
    [Q_KEY_CODE_LESS] = 0x56,
172
 
 
173
 
    [Q_KEY_CODE_F11] = 0x57,
174
 
    [Q_KEY_CODE_F12] = 0x58,
175
 
 
176
 
    [Q_KEY_CODE_PRINT] = 0xb7,
177
 
 
178
 
    [Q_KEY_CODE_HOME] = 0xc7,
179
 
    [Q_KEY_CODE_PGUP] = 0xc9,
180
 
    [Q_KEY_CODE_PGDN] = 0xd1,
181
 
    [Q_KEY_CODE_END] = 0xcf,
182
 
 
183
 
    [Q_KEY_CODE_LEFT] = 0xcb,
184
 
    [Q_KEY_CODE_UP] = 0xc8,
185
 
    [Q_KEY_CODE_DOWN] = 0xd0,
186
 
    [Q_KEY_CODE_RIGHT] = 0xcd,
187
 
 
188
 
    [Q_KEY_CODE_INSERT] = 0xd2,
189
 
    [Q_KEY_CODE_DELETE] = 0xd3,
190
 
#ifdef NEED_CPU_H
191
 
#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
192
 
    [Q_KEY_CODE_STOP] = 0xf0,
193
 
    [Q_KEY_CODE_AGAIN] = 0xf1,
194
 
    [Q_KEY_CODE_PROPS] = 0xf2,
195
 
    [Q_KEY_CODE_UNDO] = 0xf3,
196
 
    [Q_KEY_CODE_FRONT] = 0xf4,
197
 
    [Q_KEY_CODE_COPY] = 0xf5,
198
 
    [Q_KEY_CODE_OPEN] = 0xf6,
199
 
    [Q_KEY_CODE_PASTE] = 0xf7,
200
 
    [Q_KEY_CODE_FIND] = 0xf8,
201
 
    [Q_KEY_CODE_CUT] = 0xf9,
202
 
    [Q_KEY_CODE_LF] = 0xfa,
203
 
    [Q_KEY_CODE_HELP] = 0xfb,
204
 
    [Q_KEY_CODE_META_L] = 0xfc,
205
 
    [Q_KEY_CODE_META_R] = 0xfd,
206
 
    [Q_KEY_CODE_COMPOSE] = 0xfe,
207
 
#endif
208
 
#endif
209
 
    [Q_KEY_CODE_MAX] = 0,
210
 
};
211
 
 
212
 
int index_from_key(const char *key)
213
 
{
214
 
    int i;
215
 
 
216
 
    for (i = 0; QKeyCode_lookup[i] != NULL; i++) {
217
 
        if (!strcmp(key, QKeyCode_lookup[i])) {
218
 
            break;
219
 
        }
220
 
    }
221
 
 
222
 
    /* Return Q_KEY_CODE_MAX if the key is invalid */
223
 
    return i;
224
 
}
225
 
 
226
 
int index_from_keycode(int code)
227
 
{
228
 
    int i;
229
 
 
230
 
    for (i = 0; i < Q_KEY_CODE_MAX; i++) {
231
 
        if (key_defs[i] == code) {
232
 
            break;
233
 
        }
234
 
    }
235
 
 
236
 
    /* Return Q_KEY_CODE_MAX if the code is invalid */
237
 
    return i;
238
 
}
239
 
 
240
 
static int *keycodes;
241
 
static int keycodes_size;
242
 
static QEMUTimer *key_timer;
243
 
 
244
 
static int keycode_from_keyvalue(const KeyValue *value)
245
 
{
246
 
    if (value->kind == KEY_VALUE_KIND_QCODE) {
247
 
        return key_defs[value->qcode];
248
 
    } else {
249
 
        assert(value->kind == KEY_VALUE_KIND_NUMBER);
250
 
        return value->number;
251
 
    }
252
 
}
253
 
 
254
 
static void free_keycodes(void)
255
 
{
256
 
    g_free(keycodes);
257
 
    keycodes = NULL;
258
 
    keycodes_size = 0;
259
 
}
260
 
 
261
 
static void release_keys(void *opaque)
262
 
{
263
 
    while (keycodes_size > 0) {
264
 
        if (keycodes[--keycodes_size] & SCANCODE_GREY) {
265
 
            kbd_put_keycode(SCANCODE_EMUL0);
266
 
        }
267
 
        kbd_put_keycode(keycodes[keycodes_size] | SCANCODE_UP);
268
 
    }
269
 
 
270
 
    free_keycodes();
271
 
}
272
 
 
273
 
void qmp_send_key(KeyValueList *keys, bool has_hold_time, int64_t hold_time,
274
 
                  Error **errp)
275
 
{
276
 
    int keycode;
277
 
    KeyValueList *p;
278
 
 
279
 
    if (!key_timer) {
280
 
        key_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, release_keys, NULL);
281
 
    }
282
 
 
283
 
    if (keycodes != NULL) {
284
 
        timer_del(key_timer);
285
 
        release_keys(NULL);
286
 
    }
287
 
 
288
 
    if (!has_hold_time) {
289
 
        hold_time = 100;
290
 
    }
291
 
 
292
 
    for (p = keys; p != NULL; p = p->next) {
293
 
        /* key down events */
294
 
        keycode = keycode_from_keyvalue(p->value);
295
 
        if (keycode < 0x01 || keycode > 0xff) {
296
 
            error_setg(errp, "invalid hex keycode 0x%x", keycode);
297
 
            free_keycodes();
298
 
            return;
299
 
        }
300
 
 
301
 
        if (keycode & SCANCODE_GREY) {
302
 
            kbd_put_keycode(SCANCODE_EMUL0);
303
 
        }
304
 
        kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK);
305
 
 
306
 
        keycodes = g_realloc(keycodes, sizeof(int) * (keycodes_size + 1));
307
 
        keycodes[keycodes_size++] = keycode;
308
 
    }
309
 
 
310
 
    /* delayed key up events */
311
 
    timer_mod(key_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
312
 
                   muldiv64(get_ticks_per_sec(), hold_time, 1000));
313
 
}
314
 
 
315
 
QEMUPutKbdEntry *qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
316
 
{
317
 
    QEMUPutKbdEntry *entry;
318
 
 
319
 
    entry = g_malloc0(sizeof(QEMUPutKbdEntry));
320
 
    entry->put_kbd = func;
321
 
    entry->opaque = opaque;
322
 
    QTAILQ_INSERT_HEAD(&kbd_handlers, entry, next);
323
 
    return entry;
324
 
}
325
 
 
326
 
void qemu_remove_kbd_event_handler(QEMUPutKbdEntry *entry)
327
 
{
328
 
    QTAILQ_REMOVE(&kbd_handlers, entry, next);
329
 
}
330
 
 
331
 
static void check_mode_change(void)
332
 
{
333
 
    static int current_is_absolute, current_has_absolute;
 
20
QemuInputHandlerState *qemu_input_handler_register(DeviceState *dev,
 
21
                                                   QemuInputHandler *handler)
 
22
{
 
23
    QemuInputHandlerState *s = g_new0(QemuInputHandlerState, 1);
 
24
    static int id = 1;
 
25
 
 
26
    s->dev = dev;
 
27
    s->handler = handler;
 
28
    s->id = id++;
 
29
    QTAILQ_INSERT_TAIL(&handlers, s, node);
 
30
 
 
31
    qemu_input_check_mode_change();
 
32
    return s;
 
33
}
 
34
 
 
35
void qemu_input_handler_activate(QemuInputHandlerState *s)
 
36
{
 
37
    QTAILQ_REMOVE(&handlers, s, node);
 
38
    QTAILQ_INSERT_HEAD(&handlers, s, node);
 
39
    qemu_input_check_mode_change();
 
40
}
 
41
 
 
42
void qemu_input_handler_unregister(QemuInputHandlerState *s)
 
43
{
 
44
    QTAILQ_REMOVE(&handlers, s, node);
 
45
    g_free(s);
 
46
    qemu_input_check_mode_change();
 
47
}
 
48
 
 
49
static QemuInputHandlerState*
 
50
qemu_input_find_handler(uint32_t mask)
 
51
{
 
52
    QemuInputHandlerState *s;
 
53
 
 
54
    QTAILQ_FOREACH(s, &handlers, node) {
 
55
        if (mask & s->handler->mask) {
 
56
            return s;
 
57
        }
 
58
    }
 
59
    return NULL;
 
60
}
 
61
 
 
62
static void qemu_input_transform_abs_rotate(InputEvent *evt)
 
63
{
 
64
    switch (graphic_rotate) {
 
65
    case 90:
 
66
        if (evt->abs->axis == INPUT_AXIS_X) {
 
67
            evt->abs->axis = INPUT_AXIS_Y;
 
68
        } else if (evt->abs->axis == INPUT_AXIS_Y) {
 
69
            evt->abs->axis = INPUT_AXIS_X;
 
70
            evt->abs->value = INPUT_EVENT_ABS_SIZE - 1 - evt->abs->value;
 
71
        }
 
72
        break;
 
73
    case 180:
 
74
        evt->abs->value = INPUT_EVENT_ABS_SIZE - 1 - evt->abs->value;
 
75
        break;
 
76
    case 270:
 
77
        if (evt->abs->axis == INPUT_AXIS_X) {
 
78
            evt->abs->axis = INPUT_AXIS_Y;
 
79
            evt->abs->value = INPUT_EVENT_ABS_SIZE - 1 - evt->abs->value;
 
80
        } else if (evt->abs->axis == INPUT_AXIS_Y) {
 
81
            evt->abs->axis = INPUT_AXIS_X;
 
82
        }
 
83
        break;
 
84
    }
 
85
}
 
86
 
 
87
static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt)
 
88
{
 
89
    const char *name;
 
90
    int idx = -1;
 
91
 
 
92
    if (src) {
 
93
        idx = qemu_console_get_index(src);
 
94
    }
 
95
    switch (evt->kind) {
 
96
    case INPUT_EVENT_KIND_KEY:
 
97
        switch (evt->key->key->kind) {
 
98
        case KEY_VALUE_KIND_NUMBER:
 
99
            trace_input_event_key_number(idx, evt->key->key->number,
 
100
                                         evt->key->down);
 
101
            break;
 
102
        case KEY_VALUE_KIND_QCODE:
 
103
            name = QKeyCode_lookup[evt->key->key->qcode];
 
104
            trace_input_event_key_qcode(idx, name, evt->key->down);
 
105
            break;
 
106
        case KEY_VALUE_KIND_MAX:
 
107
            /* keep gcc happy */
 
108
            break;
 
109
        }
 
110
        break;
 
111
    case INPUT_EVENT_KIND_BTN:
 
112
        name = InputButton_lookup[evt->btn->button];
 
113
        trace_input_event_btn(idx, name, evt->btn->down);
 
114
        break;
 
115
    case INPUT_EVENT_KIND_REL:
 
116
        name = InputAxis_lookup[evt->rel->axis];
 
117
        trace_input_event_rel(idx, name, evt->rel->value);
 
118
        break;
 
119
    case INPUT_EVENT_KIND_ABS:
 
120
        name = InputAxis_lookup[evt->abs->axis];
 
121
        trace_input_event_abs(idx, name, evt->abs->value);
 
122
        break;
 
123
    case INPUT_EVENT_KIND_MAX:
 
124
        /* keep gcc happy */
 
125
        break;
 
126
    }
 
127
}
 
128
 
 
129
void qemu_input_event_send(QemuConsole *src, InputEvent *evt)
 
130
{
 
131
    QemuInputHandlerState *s;
 
132
 
 
133
    if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
 
134
        return;
 
135
    }
 
136
 
 
137
    qemu_input_event_trace(src, evt);
 
138
 
 
139
    /* pre processing */
 
140
    if (graphic_rotate && (evt->kind == INPUT_EVENT_KIND_ABS)) {
 
141
            qemu_input_transform_abs_rotate(evt);
 
142
    }
 
143
 
 
144
    /* send event */
 
145
    s = qemu_input_find_handler(1 << evt->kind);
 
146
    if (!s) {
 
147
        return;
 
148
    }
 
149
    s->handler->event(s->dev, src, evt);
 
150
    s->events++;
 
151
}
 
152
 
 
153
void qemu_input_event_sync(void)
 
154
{
 
155
    QemuInputHandlerState *s;
 
156
 
 
157
    if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
 
158
        return;
 
159
    }
 
160
 
 
161
    trace_input_event_sync();
 
162
 
 
163
    QTAILQ_FOREACH(s, &handlers, node) {
 
164
        if (!s->events) {
 
165
            continue;
 
166
        }
 
167
        if (s->handler->sync) {
 
168
            s->handler->sync(s->dev);
 
169
        }
 
170
        s->events = 0;
 
171
    }
 
172
}
 
173
 
 
174
InputEvent *qemu_input_event_new_key(KeyValue *key, bool down)
 
175
{
 
176
    InputEvent *evt = g_new0(InputEvent, 1);
 
177
    evt->key = g_new0(InputKeyEvent, 1);
 
178
    evt->kind = INPUT_EVENT_KIND_KEY;
 
179
    evt->key->key = key;
 
180
    evt->key->down = down;
 
181
    return evt;
 
182
}
 
183
 
 
184
void qemu_input_event_send_key(QemuConsole *src, KeyValue *key, bool down)
 
185
{
 
186
    InputEvent *evt;
 
187
    evt = qemu_input_event_new_key(key, down);
 
188
    qemu_input_event_send(src, evt);
 
189
    qemu_input_event_sync();
 
190
    qapi_free_InputEvent(evt);
 
191
}
 
192
 
 
193
void qemu_input_event_send_key_number(QemuConsole *src, int num, bool down)
 
194
{
 
195
    KeyValue *key = g_new0(KeyValue, 1);
 
196
    key->kind = KEY_VALUE_KIND_NUMBER;
 
197
    key->number = num;
 
198
    qemu_input_event_send_key(src, key, down);
 
199
}
 
200
 
 
201
void qemu_input_event_send_key_qcode(QemuConsole *src, QKeyCode q, bool down)
 
202
{
 
203
    KeyValue *key = g_new0(KeyValue, 1);
 
204
    key->kind = KEY_VALUE_KIND_QCODE;
 
205
    key->qcode = q;
 
206
    qemu_input_event_send_key(src, key, down);
 
207
}
 
208
 
 
209
InputEvent *qemu_input_event_new_btn(InputButton btn, bool down)
 
210
{
 
211
    InputEvent *evt = g_new0(InputEvent, 1);
 
212
    evt->btn = g_new0(InputBtnEvent, 1);
 
213
    evt->kind = INPUT_EVENT_KIND_BTN;
 
214
    evt->btn->button = btn;
 
215
    evt->btn->down = down;
 
216
    return evt;
 
217
}
 
218
 
 
219
void qemu_input_queue_btn(QemuConsole *src, InputButton btn, bool down)
 
220
{
 
221
    InputEvent *evt;
 
222
    evt = qemu_input_event_new_btn(btn, down);
 
223
    qemu_input_event_send(src, evt);
 
224
    qapi_free_InputEvent(evt);
 
225
}
 
226
 
 
227
void qemu_input_update_buttons(QemuConsole *src, uint32_t *button_map,
 
228
                               uint32_t button_old, uint32_t button_new)
 
229
{
 
230
    InputButton btn;
 
231
    uint32_t mask;
 
232
 
 
233
    for (btn = 0; btn < INPUT_BUTTON_MAX; btn++) {
 
234
        mask = button_map[btn];
 
235
        if ((button_old & mask) == (button_new & mask)) {
 
236
            continue;
 
237
        }
 
238
        qemu_input_queue_btn(src, btn, button_new & mask);
 
239
    }
 
240
}
 
241
 
 
242
bool qemu_input_is_absolute(void)
 
243
{
 
244
    QemuInputHandlerState *s;
 
245
 
 
246
    s = qemu_input_find_handler(INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS);
 
247
    return (s != NULL) && (s->handler->mask & INPUT_EVENT_MASK_ABS);
 
248
}
 
249
 
 
250
int qemu_input_scale_axis(int value, int size_in, int size_out)
 
251
{
 
252
    if (size_in < 2) {
 
253
        return size_out / 2;
 
254
    }
 
255
    return (int64_t)value * (size_out - 1) / (size_in - 1);
 
256
}
 
257
 
 
258
InputEvent *qemu_input_event_new_move(InputEventKind kind,
 
259
                                      InputAxis axis, int value)
 
260
{
 
261
    InputEvent *evt = g_new0(InputEvent, 1);
 
262
    InputMoveEvent *move = g_new0(InputMoveEvent, 1);
 
263
 
 
264
    evt->kind = kind;
 
265
    evt->data = move;
 
266
    move->axis = axis;
 
267
    move->value = value;
 
268
    return evt;
 
269
}
 
270
 
 
271
void qemu_input_queue_rel(QemuConsole *src, InputAxis axis, int value)
 
272
{
 
273
    InputEvent *evt;
 
274
    evt = qemu_input_event_new_move(INPUT_EVENT_KIND_REL, axis, value);
 
275
    qemu_input_event_send(src, evt);
 
276
    qapi_free_InputEvent(evt);
 
277
}
 
278
 
 
279
void qemu_input_queue_abs(QemuConsole *src, InputAxis axis, int value, int size)
 
280
{
 
281
    InputEvent *evt;
 
282
    int scaled = qemu_input_scale_axis(value, size, INPUT_EVENT_ABS_SIZE);
 
283
    evt = qemu_input_event_new_move(INPUT_EVENT_KIND_ABS, axis, scaled);
 
284
    qemu_input_event_send(src, evt);
 
285
    qapi_free_InputEvent(evt);
 
286
}
 
287
 
 
288
void qemu_input_check_mode_change(void)
 
289
{
 
290
    static int current_is_absolute;
334
291
    int is_absolute;
335
 
    int has_absolute;
336
 
 
337
 
    is_absolute = kbd_mouse_is_absolute();
338
 
    has_absolute = kbd_mouse_has_absolute();
339
 
 
340
 
    if (is_absolute != current_is_absolute ||
341
 
        has_absolute != current_has_absolute) {
 
292
 
 
293
    is_absolute = qemu_input_is_absolute();
 
294
 
 
295
    if (is_absolute != current_is_absolute) {
 
296
        trace_input_mouse_mode(is_absolute);
342
297
        notifier_list_notify(&mouse_mode_notifiers, NULL);
343
298
    }
344
299
 
345
300
    current_is_absolute = is_absolute;
346
 
    current_has_absolute = has_absolute;
347
 
}
348
 
 
349
 
QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
350
 
                                                void *opaque, int absolute,
351
 
                                                const char *name)
352
 
{
353
 
    QEMUPutMouseEntry *s;
354
 
    static int mouse_index = 0;
355
 
 
356
 
    s = g_malloc0(sizeof(QEMUPutMouseEntry));
357
 
 
358
 
    s->qemu_put_mouse_event = func;
359
 
    s->qemu_put_mouse_event_opaque = opaque;
360
 
    s->qemu_put_mouse_event_absolute = absolute;
361
 
    s->qemu_put_mouse_event_name = g_strdup(name);
362
 
    s->index = mouse_index++;
363
 
 
364
 
    QTAILQ_INSERT_TAIL(&mouse_handlers, s, node);
365
 
 
366
 
    check_mode_change();
367
 
 
368
 
    return s;
369
 
}
370
 
 
371
 
void qemu_activate_mouse_event_handler(QEMUPutMouseEntry *entry)
372
 
{
373
 
    QTAILQ_REMOVE(&mouse_handlers, entry, node);
374
 
    QTAILQ_INSERT_HEAD(&mouse_handlers, entry, node);
375
 
 
376
 
    check_mode_change();
377
 
}
378
 
 
379
 
void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
380
 
{
381
 
    QTAILQ_REMOVE(&mouse_handlers, entry, node);
382
 
 
383
 
    g_free(entry->qemu_put_mouse_event_name);
384
 
    g_free(entry);
385
 
 
386
 
    check_mode_change();
387
 
}
388
 
 
389
 
QEMUPutLEDEntry *qemu_add_led_event_handler(QEMUPutLEDEvent *func,
390
 
                                            void *opaque)
391
 
{
392
 
    QEMUPutLEDEntry *s;
393
 
 
394
 
    s = g_malloc0(sizeof(QEMUPutLEDEntry));
395
 
 
396
 
    s->put_led = func;
397
 
    s->opaque = opaque;
398
 
    QTAILQ_INSERT_TAIL(&led_handlers, s, next);
399
 
    return s;
400
 
}
401
 
 
402
 
void qemu_remove_led_event_handler(QEMUPutLEDEntry *entry)
403
 
{
404
 
    if (entry == NULL)
405
 
        return;
406
 
    QTAILQ_REMOVE(&led_handlers, entry, next);
407
 
    g_free(entry);
408
 
}
409
 
 
410
 
void kbd_put_keycode(int keycode)
411
 
{
412
 
    QEMUPutKbdEntry *entry = QTAILQ_FIRST(&kbd_handlers);
413
 
 
414
 
    if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
415
 
        return;
416
 
    }
417
 
    if (entry) {
418
 
        entry->put_kbd(entry->opaque, keycode);
419
 
    }
420
 
}
421
 
 
422
 
void kbd_put_ledstate(int ledstate)
423
 
{
424
 
    QEMUPutLEDEntry *cursor;
425
 
 
426
 
    QTAILQ_FOREACH(cursor, &led_handlers, next) {
427
 
        cursor->put_led(cursor->opaque, ledstate);
428
 
    }
429
 
}
430
 
 
431
 
void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
432
 
{
433
 
    QEMUPutMouseEntry *entry;
434
 
    QEMUPutMouseEvent *mouse_event;
435
 
    void *mouse_event_opaque;
436
 
    int width, height;
437
 
 
438
 
    if (!runstate_is_running() && !runstate_check(RUN_STATE_SUSPENDED)) {
439
 
        return;
440
 
    }
441
 
    if (QTAILQ_EMPTY(&mouse_handlers)) {
442
 
        return;
443
 
    }
444
 
 
445
 
    entry = QTAILQ_FIRST(&mouse_handlers);
446
 
 
447
 
    mouse_event = entry->qemu_put_mouse_event;
448
 
    mouse_event_opaque = entry->qemu_put_mouse_event_opaque;
449
 
 
450
 
    if (mouse_event) {
451
 
        if (entry->qemu_put_mouse_event_absolute) {
452
 
            width = 0x7fff;
453
 
            height = 0x7fff;
454
 
        } else {
455
 
            width = graphic_width - 1;
456
 
            height = graphic_height - 1;
457
 
        }
458
 
 
459
 
        switch (graphic_rotate) {
460
 
        case 0:
461
 
            mouse_event(mouse_event_opaque,
462
 
                        dx, dy, dz, buttons_state);
463
 
            break;
464
 
        case 90:
465
 
            mouse_event(mouse_event_opaque,
466
 
                        width - dy, dx, dz, buttons_state);
467
 
            break;
468
 
        case 180:
469
 
            mouse_event(mouse_event_opaque,
470
 
                        width - dx, height - dy, dz, buttons_state);
471
 
            break;
472
 
        case 270:
473
 
            mouse_event(mouse_event_opaque,
474
 
                        dy, height - dx, dz, buttons_state);
475
 
            break;
476
 
        }
477
 
    }
478
 
}
479
 
 
480
 
int kbd_mouse_is_absolute(void)
481
 
{
482
 
    if (QTAILQ_EMPTY(&mouse_handlers)) {
483
 
        return 0;
484
 
    }
485
 
 
486
 
    return QTAILQ_FIRST(&mouse_handlers)->qemu_put_mouse_event_absolute;
487
 
}
488
 
 
489
 
int kbd_mouse_has_absolute(void)
490
 
{
491
 
    QEMUPutMouseEntry *entry;
492
 
 
493
 
    QTAILQ_FOREACH(entry, &mouse_handlers, node) {
494
 
        if (entry->qemu_put_mouse_event_absolute) {
495
 
            return 1;
496
 
        }
497
 
    }
498
 
 
499
 
    return 0;
 
301
}
 
302
 
 
303
void qemu_add_mouse_mode_change_notifier(Notifier *notify)
 
304
{
 
305
    notifier_list_add(&mouse_mode_notifiers, notify);
 
306
}
 
307
 
 
308
void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
 
309
{
 
310
    notifier_remove(notify);
500
311
}
501
312
 
502
313
MouseInfoList *qmp_query_mice(Error **errp)
503
314
{
504
315
    MouseInfoList *mice_list = NULL;
505
 
    QEMUPutMouseEntry *cursor;
 
316
    MouseInfoList *info;
 
317
    QemuInputHandlerState *s;
506
318
    bool current = true;
507
319
 
508
 
    QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
509
 
        MouseInfoList *info = g_malloc0(sizeof(*info));
510
 
        info->value = g_malloc0(sizeof(*info->value));
511
 
        info->value->name = g_strdup(cursor->qemu_put_mouse_event_name);
512
 
        info->value->index = cursor->index;
513
 
        info->value->absolute = !!cursor->qemu_put_mouse_event_absolute;
 
320
    QTAILQ_FOREACH(s, &handlers, node) {
 
321
        if (!(s->handler->mask &
 
322
              (INPUT_EVENT_MASK_REL | INPUT_EVENT_MASK_ABS))) {
 
323
            continue;
 
324
        }
 
325
 
 
326
        info = g_new0(MouseInfoList, 1);
 
327
        info->value = g_new0(MouseInfo, 1);
 
328
        info->value->index = s->id;
 
329
        info->value->name = g_strdup(s->handler->name);
 
330
        info->value->absolute = s->handler->mask & INPUT_EVENT_MASK_ABS;
514
331
        info->value->current = current;
515
332
 
516
333
        current = false;
517
 
 
518
334
        info->next = mice_list;
519
335
        mice_list = info;
520
336
    }
524
340
 
525
341
void do_mouse_set(Monitor *mon, const QDict *qdict)
526
342
{
527
 
    QEMUPutMouseEntry *cursor;
 
343
    QemuInputHandlerState *s;
528
344
    int index = qdict_get_int(qdict, "index");
529
345
    int found = 0;
530
346
 
531
 
    if (QTAILQ_EMPTY(&mouse_handlers)) {
532
 
        monitor_printf(mon, "No mouse devices connected\n");
533
 
        return;
534
 
    }
535
 
 
536
 
    QTAILQ_FOREACH(cursor, &mouse_handlers, node) {
537
 
        if (cursor->index == index) {
538
 
            found = 1;
539
 
            qemu_activate_mouse_event_handler(cursor);
540
 
            break;
541
 
        }
 
347
    QTAILQ_FOREACH(s, &handlers, node) {
 
348
        if (s->id != index) {
 
349
            continue;
 
350
        }
 
351
        if (!(s->handler->mask & (INPUT_EVENT_MASK_REL |
 
352
                                  INPUT_EVENT_MASK_ABS))) {
 
353
            error_report("Input device '%s' is not a mouse", s->handler->name);
 
354
            return;
 
355
        }
 
356
        found = 1;
 
357
        qemu_input_handler_activate(s);
 
358
        break;
542
359
    }
543
360
 
544
361
    if (!found) {
545
 
        monitor_printf(mon, "Mouse at given index not found\n");
 
362
        error_report("Mouse at index '%d' not found", index);
546
363
    }
547
364
 
548
 
    check_mode_change();
549
 
}
550
 
 
551
 
void qemu_add_mouse_mode_change_notifier(Notifier *notify)
552
 
{
553
 
    notifier_list_add(&mouse_mode_notifiers, notify);
554
 
}
555
 
 
556
 
void qemu_remove_mouse_mode_change_notifier(Notifier *notify)
557
 
{
558
 
    notifier_remove(notify);
 
365
    qemu_input_check_mode_change();
559
366
}