xref: /openbmc/qemu/ui/sdl2-input.c (revision 77a8257e)
1 /*
2  * QEMU SDL display driver
3  *
4  * Copyright (c) 2003 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 /* Ported SDL 1.2 code to 2.0 by Dave Airlie. */
25 
26 /* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
27 #undef WIN32_LEAN_AND_MEAN
28 
29 #include <SDL.h>
30 #include <SDL_syswm.h>
31 
32 #include "qemu-common.h"
33 #include "ui/console.h"
34 #include "ui/input.h"
35 #include "ui/sdl2.h"
36 #include "sysemu/sysemu.h"
37 
38 #include "sdl2-keymap.h"
39 
40 static uint8_t modifiers_state[SDL_NUM_SCANCODES];
41 
42 void sdl2_reset_keys(struct sdl2_console *scon)
43 {
44     QemuConsole *con = scon ? scon->dcl.con : NULL;
45     int i;
46 
47     for (i = 0; i < SDL_NUM_SCANCODES; i++) {
48         if (modifiers_state[i]) {
49             int qcode = sdl2_scancode_to_qcode[i];
50             qemu_input_event_send_key_qcode(con, qcode, false);
51             modifiers_state[i] = 0;
52         }
53     }
54 }
55 
56 void sdl2_process_key(struct sdl2_console *scon,
57                       SDL_KeyboardEvent *ev)
58 {
59     int qcode = sdl2_scancode_to_qcode[ev->keysym.scancode];
60     QemuConsole *con = scon ? scon->dcl.con : NULL;
61 
62     if (!qemu_console_is_graphic(con)) {
63         if (ev->type == SDL_KEYDOWN) {
64             switch (ev->keysym.scancode) {
65             case SDL_SCANCODE_RETURN:
66                 kbd_put_keysym_console(con, '\n');
67                 break;
68             case SDL_SCANCODE_BACKSPACE:
69                 kbd_put_keysym_console(con, QEMU_KEY_BACKSPACE);
70                 break;
71             default:
72                 kbd_put_qcode_console(con, qcode);
73                 break;
74             }
75         }
76         return;
77     }
78 
79     switch (ev->keysym.scancode) {
80 #if 0
81     case SDL_SCANCODE_NUMLOCKCLEAR:
82     case SDL_SCANCODE_CAPSLOCK:
83         /* SDL does not send the key up event, so we generate it */
84         qemu_input_event_send_key_qcode(con, qcode, true);
85         qemu_input_event_send_key_qcode(con, qcode, false);
86         return;
87 #endif
88     case SDL_SCANCODE_LCTRL:
89     case SDL_SCANCODE_LSHIFT:
90     case SDL_SCANCODE_LALT:
91     case SDL_SCANCODE_LGUI:
92     case SDL_SCANCODE_RCTRL:
93     case SDL_SCANCODE_RSHIFT:
94     case SDL_SCANCODE_RALT:
95     case SDL_SCANCODE_RGUI:
96         if (ev->type == SDL_KEYUP) {
97             modifiers_state[ev->keysym.scancode] = 0;
98         } else {
99             modifiers_state[ev->keysym.scancode] = 1;
100         }
101         /* fall though */
102     default:
103         qemu_input_event_send_key_qcode(con, qcode,
104                                         ev->type == SDL_KEYDOWN);
105     }
106 }
107