1 #include "kvm/sdl.h" 2 3 #include "kvm/framebuffer.h" 4 #include "kvm/i8042.h" 5 #include "kvm/util.h" 6 #include "kvm/kvm.h" 7 8 #include <SDL/SDL.h> 9 #include <pthread.h> 10 #include <signal.h> 11 12 #define FRAME_RATE 25 13 14 #define SCANCODE_UNKNOWN 0 15 #define SCANCODE_NORMAL 1 16 #define SCANCODE_ESCAPED 2 17 #define SCANCODE_KEY_PAUSE 3 18 #define SCANCODE_KEY_PRNTSCRN 4 19 20 struct set2_scancode { 21 u8 code; 22 u8 type; 23 }; 24 25 #define DEFINE_SC(_code) {\ 26 .code = _code,\ 27 .type = SCANCODE_NORMAL,\ 28 } 29 30 /* escaped scancodes */ 31 #define DEFINE_ESC(_code) {\ 32 .code = _code,\ 33 .type = SCANCODE_ESCAPED,\ 34 } 35 36 static const struct set2_scancode const keymap[255] = { 37 [9] = DEFINE_SC(0x76), /* <esc> */ 38 [10] = DEFINE_SC(0x16), /* 1 */ 39 [11] = DEFINE_SC(0x1e), /* 2 */ 40 [12] = DEFINE_SC(0x26), /* 3 */ 41 [13] = DEFINE_SC(0x25), /* 4 */ 42 [14] = DEFINE_SC(0x27), /* 5 */ 43 [15] = DEFINE_SC(0x36), /* 6 */ 44 [16] = DEFINE_SC(0x3d), /* 7 */ 45 [17] = DEFINE_SC(0x3e), /* 8 */ 46 [18] = DEFINE_SC(0x46), /* 9 */ 47 [19] = DEFINE_SC(0x45), /* 9 */ 48 [20] = DEFINE_SC(0x4e), /* - */ 49 [21] = DEFINE_SC(0x55), /* + */ 50 [22] = DEFINE_SC(0x66), /* <backspace> */ 51 [23] = DEFINE_SC(0x0d), /* <tab> */ 52 [24] = DEFINE_SC(0x15), /* q */ 53 [25] = DEFINE_SC(0x1d), /* w */ 54 [26] = DEFINE_SC(0x24), /* e */ 55 [27] = DEFINE_SC(0x2d), /* r */ 56 [28] = DEFINE_SC(0x2c), /* t */ 57 [29] = DEFINE_SC(0x35), /* y */ 58 [30] = DEFINE_SC(0x3c), /* u */ 59 [31] = DEFINE_SC(0x43), /* i */ 60 [32] = DEFINE_SC(0x44), /* o */ 61 [33] = DEFINE_SC(0x4d), /* p */ 62 [34] = DEFINE_SC(0x54), /* [ */ 63 [35] = DEFINE_SC(0x5b), /* ] */ 64 [36] = DEFINE_SC(0x5a), /* <enter> */ 65 [37] = DEFINE_SC(0x14), /* <left ctrl> */ 66 [38] = DEFINE_SC(0x1c), /* a */ 67 [39] = DEFINE_SC(0x1b), /* s */ 68 [40] = DEFINE_SC(0x23), /* d */ 69 [41] = DEFINE_SC(0x2b), /* f */ 70 [42] = DEFINE_SC(0x34), /* g */ 71 [43] = DEFINE_SC(0x33), /* h */ 72 [44] = DEFINE_SC(0x3b), /* j */ 73 [45] = DEFINE_SC(0x42), /* k */ 74 [46] = DEFINE_SC(0x4b), /* l */ 75 [47] = DEFINE_SC(0x4c), /* ; */ 76 [48] = DEFINE_SC(0x52), /* ' */ 77 [49] = DEFINE_SC(0x0e), /* ` */ 78 [50] = DEFINE_SC(0x12), /* <left shift> */ 79 [51] = DEFINE_SC(0x5d), /* \ */ 80 [52] = DEFINE_SC(0x1a), /* z */ 81 [53] = DEFINE_SC(0x22), /* x */ 82 [54] = DEFINE_SC(0x21), /* c */ 83 [55] = DEFINE_SC(0x2a), /* v */ 84 [56] = DEFINE_SC(0x32), /* b */ 85 [57] = DEFINE_SC(0x31), /* n */ 86 [58] = DEFINE_SC(0x3a), /* m */ 87 [59] = DEFINE_SC(0x41), /* < */ 88 [60] = DEFINE_SC(0x49), /* > */ 89 [61] = DEFINE_SC(0x4a), /* / */ 90 [62] = DEFINE_SC(0x59), /* <right shift> */ 91 [63] = DEFINE_SC(0x7c), /* keypad * */ 92 [64] = DEFINE_SC(0x11), /* <left alt> */ 93 [65] = DEFINE_SC(0x29), /* <space> */ 94 95 [67] = DEFINE_SC(0x05), /* <F1> */ 96 [68] = DEFINE_SC(0x06), /* <F2> */ 97 [69] = DEFINE_SC(0x04), /* <F3> */ 98 [70] = DEFINE_SC(0x0c), /* <F4> */ 99 [71] = DEFINE_SC(0x03), /* <F5> */ 100 [72] = DEFINE_SC(0x0b), /* <F6> */ 101 [73] = DEFINE_SC(0x83), /* <F7> */ 102 [74] = DEFINE_SC(0x0a), /* <F8> */ 103 [75] = DEFINE_SC(0x01), /* <F9> */ 104 [76] = DEFINE_SC(0x09), /* <F10> */ 105 106 [79] = DEFINE_SC(0x6c), /* keypad 7 */ 107 [80] = DEFINE_SC(0x75), /* keypad 8 */ 108 [81] = DEFINE_SC(0x7d), /* keypad 9 */ 109 [82] = DEFINE_SC(0x7b), /* keypad - */ 110 [83] = DEFINE_SC(0x6b), /* keypad 4 */ 111 [84] = DEFINE_SC(0x73), /* keypad 5 */ 112 [85] = DEFINE_SC(0x74), /* keypad 6 */ 113 [86] = DEFINE_SC(0x79), /* keypad + */ 114 [87] = DEFINE_SC(0x69), /* keypad 1 */ 115 [88] = DEFINE_SC(0x72), /* keypad 2 */ 116 [89] = DEFINE_SC(0x7a), /* keypad 3 */ 117 [90] = DEFINE_SC(0x70), /* keypad 0 */ 118 [91] = DEFINE_SC(0x71), /* keypad . */ 119 120 [94] = DEFINE_SC(0x61), /* <INT 1> */ 121 [95] = DEFINE_SC(0x78), /* <F11> */ 122 [96] = DEFINE_SC(0x07), /* <F12> */ 123 124 [104] = DEFINE_ESC(0x5a), /* keypad <enter> */ 125 [105] = DEFINE_ESC(0x14), /* <right ctrl> */ 126 [106] = DEFINE_ESC(0x4a), /* keypad / */ 127 [108] = DEFINE_ESC(0x11), /* <right alt> */ 128 [110] = DEFINE_ESC(0x6c), /* <home> */ 129 [111] = DEFINE_ESC(0x75), /* <up> */ 130 [112] = DEFINE_ESC(0x7d), /* <pag up> */ 131 [113] = DEFINE_ESC(0x6b), /* <left> */ 132 [114] = DEFINE_ESC(0x74), /* <right> */ 133 [115] = DEFINE_ESC(0x69), /* <end> */ 134 [116] = DEFINE_ESC(0x72), /* <down> */ 135 [117] = DEFINE_ESC(0x7a), /* <pag down> */ 136 [118] = DEFINE_ESC(0x70), /* <ins> */ 137 [119] = DEFINE_ESC(0x71), /* <delete> */ 138 }; 139 140 static const struct set2_scancode *to_code(u8 scancode) 141 { 142 return &keymap[scancode]; 143 } 144 145 static void key_press(const struct set2_scancode *sc) 146 { 147 switch (sc->type) { 148 case SCANCODE_ESCAPED: 149 kbd_queue(0xe0); 150 /* fallthrough */ 151 case SCANCODE_NORMAL: 152 kbd_queue(sc->code); 153 break; 154 case SCANCODE_KEY_PAUSE: 155 kbd_queue(0xe1); 156 kbd_queue(0x14); 157 kbd_queue(0x77); 158 kbd_queue(0xe1); 159 kbd_queue(0xf0); 160 kbd_queue(0x14); 161 kbd_queue(0x77); 162 break; 163 case SCANCODE_KEY_PRNTSCRN: 164 kbd_queue(0xe0); 165 kbd_queue(0x12); 166 kbd_queue(0xe0); 167 kbd_queue(0x7c); 168 break; 169 } 170 } 171 172 static void key_release(const struct set2_scancode *sc) 173 { 174 switch (sc->type) { 175 case SCANCODE_ESCAPED: 176 kbd_queue(0xe0); 177 /* fallthrough */ 178 case SCANCODE_NORMAL: 179 kbd_queue(0xf0); 180 kbd_queue(sc->code); 181 break; 182 case SCANCODE_KEY_PAUSE: 183 /* nothing to do */ 184 break; 185 case SCANCODE_KEY_PRNTSCRN: 186 kbd_queue(0xe0); 187 kbd_queue(0xf0); 188 kbd_queue(0x7c); 189 kbd_queue(0xe0); 190 kbd_queue(0xf0); 191 kbd_queue(0x12); 192 break; 193 } 194 } 195 196 static void *sdl__thread(void *p) 197 { 198 Uint32 rmask, gmask, bmask, amask; 199 struct framebuffer *fb = p; 200 SDL_Surface *guest_screen; 201 SDL_Surface *screen; 202 SDL_Event ev; 203 Uint32 flags; 204 205 if (SDL_Init(SDL_INIT_VIDEO) != 0) 206 die("Unable to initialize SDL"); 207 208 rmask = 0x000000ff; 209 gmask = 0x0000ff00; 210 bmask = 0x00ff0000; 211 amask = 0x00000000; 212 213 guest_screen = SDL_CreateRGBSurfaceFrom(fb->mem, fb->width, fb->height, fb->depth, fb->width * fb->depth / 8, rmask, gmask, bmask, amask); 214 if (!guest_screen) 215 die("Unable to create SDL RBG surface"); 216 217 flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_DOUBLEBUF; 218 219 SDL_WM_SetCaption("KVM tool", "KVM tool"); 220 221 screen = SDL_SetVideoMode(fb->width, fb->height, fb->depth, flags); 222 if (!screen) 223 die("Unable to set SDL video mode"); 224 225 SDL_EnableKeyRepeat(200, 50); 226 227 for (;;) { 228 SDL_BlitSurface(guest_screen, NULL, screen, NULL); 229 SDL_Flip(screen); 230 231 while (SDL_PollEvent(&ev)) { 232 switch (ev.type) { 233 case SDL_KEYDOWN: { 234 const struct set2_scancode *sc = to_code(ev.key.keysym.scancode); 235 if (sc->type == SCANCODE_UNKNOWN) { 236 pr_warning("key '%d' not found in keymap", ev.key.keysym.scancode); 237 break; 238 } 239 key_press(sc); 240 break; 241 } 242 case SDL_KEYUP: { 243 const struct set2_scancode *sc = to_code(ev.key.keysym.scancode); 244 if (sc->type == SCANCODE_UNKNOWN) 245 break; 246 key_release(sc); 247 break; 248 } 249 case SDL_QUIT: 250 goto exit; 251 } 252 } 253 254 SDL_Delay(1000 / FRAME_RATE); 255 } 256 exit: 257 kill(0, SIGKVMSTOP); 258 259 return NULL; 260 } 261 262 static int sdl__start(struct framebuffer *fb) 263 { 264 pthread_t thread; 265 266 if (pthread_create(&thread, NULL, sdl__thread, fb) != 0) 267 return -1; 268 269 return 0; 270 } 271 272 static struct fb_target_operations sdl_ops = { 273 .start = sdl__start, 274 }; 275 276 void sdl__init(struct framebuffer *fb) 277 { 278 fb__attach(fb, &sdl_ops); 279 } 280