[a06aa8b] | 1 | /*
|
---|
| 2 | * Copyright (C) 2017 The Contributors
|
---|
| 3 | *
|
---|
| 4 | * This program is free software: you can redistribute it and/or modify
|
---|
| 5 | * it under the terms of the GNU General Public License as published by
|
---|
| 6 | * the Free Software Foundation, either version 3 of the License, or (at
|
---|
| 7 | * your option) any later version.
|
---|
| 8 | *
|
---|
| 9 | * This program is distributed in the hope that it will be useful, but
|
---|
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 12 | * General Public License for more details.
|
---|
| 13 | *
|
---|
| 14 | * A copy of the GNU General Public License can be found in the file
|
---|
[2147e53] | 15 | * "gpl.txt" in the top directory of this repository.
|
---|
[a06aa8b] | 16 | */
|
---|
| 17 |
|
---|
[ff8d800] | 18 | #include <all.h>
|
---|
| 19 |
|
---|
[7cc6ac0] | 20 | int32_t sdl_verbose = 0;
|
---|
[ff8d800] | 21 |
|
---|
[4c71d39] | 22 | #define ver(...) _ver(sdl_verbose, 0, __VA_ARGS__)
|
---|
| 23 | #define ver2(...) _ver(sdl_verbose, 1, __VA_ARGS__)
|
---|
| 24 | #define ver3(...) _ver(sdl_verbose, 2, __VA_ARGS__)
|
---|
[ff8d800] | 25 |
|
---|
[6e313dd] | 26 | typedef void (*sdl_func_t)(void);
|
---|
| 27 |
|
---|
| 28 | static sdl_func_t sdl_funcs[] = {
|
---|
[7ba68aa] | 29 | lcd_sdl, ser_sdl, vid_sdl
|
---|
[6e313dd] | 30 | };
|
---|
| 31 |
|
---|
[ff8d800] | 32 | void sdl_init(void)
|
---|
| 33 | {
|
---|
[ebc8f69] | 34 | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) < 0) {
|
---|
[ff8d800] | 35 | fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
|
---|
| 36 | exit(1);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_VERBOSE);
|
---|
[2f9f352] | 40 |
|
---|
[ea878ba] | 41 | if (SDLNet_Init() < 0) {
|
---|
| 42 | fail("SDLNet_Init() failed: %s", SDLNet_GetError());
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[2f9f352] | 45 | if (TTF_Init() < 0) {
|
---|
| 46 | fail("TTF_Init() failed: %s", TTF_GetError());
|
---|
| 47 | }
|
---|
[bb4fd0c] | 48 |
|
---|
| 49 | SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
|
---|
| 50 | SDL_StartTextInput();
|
---|
[ff8d800] | 51 | }
|
---|
| 52 |
|
---|
| 53 | void sdl_quit(void)
|
---|
| 54 | {
|
---|
[2f9f352] | 55 | TTF_Quit();
|
---|
[ea878ba] | 56 | SDLNet_Quit();
|
---|
[ff8d800] | 57 | SDL_Quit();
|
---|
| 58 | }
|
---|
[bdd5a63] | 59 |
|
---|
| 60 | void sdl_loop(void)
|
---|
| 61 | {
|
---|
| 62 | inf("entering SDL loop");
|
---|
| 63 |
|
---|
| 64 | #if defined EMU_LINUX
|
---|
| 65 | SDL_Scancode down = SDL_SCANCODE_UNKNOWN;
|
---|
| 66 | #endif
|
---|
| 67 |
|
---|
[b48c8a5] | 68 | bool rel_mod = false;
|
---|
[4ed9bfe] | 69 | uint32_t win = 0;
|
---|
[b48c8a5] | 70 |
|
---|
[bdd5a63] | 71 | while (SDL_AtomicGet(&run) != 0) {
|
---|
[6e313dd] | 72 | for (int32_t i = 0; i < ARRAY_COUNT(sdl_funcs); ++i) {
|
---|
| 73 | sdl_funcs[i]();
|
---|
| 74 | }
|
---|
[bdd5a63] | 75 |
|
---|
| 76 | SDL_Event ev;
|
---|
| 77 |
|
---|
| 78 | while (SDL_PollEvent(&ev) > 0) {
|
---|
[b48c8a5] | 79 | ver2("sdl ev %d", ev.type);
|
---|
| 80 |
|
---|
[bdd5a63] | 81 | #if defined EMU_LINUX
|
---|
| 82 | // Work around duplicate key-down events on Linux.
|
---|
| 83 |
|
---|
| 84 | if (ev.type == SDL_KEYDOWN) {
|
---|
| 85 | if (down == ev.key.keysym.scancode) {
|
---|
[b48c8a5] | 86 | ver2("sdl dedup: skip %d", (int32_t)down);
|
---|
[bdd5a63] | 87 | continue;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | down = ev.key.keysym.scancode;
|
---|
[b48c8a5] | 91 | ver2("sdl dedup: %d", (int32_t)down);
|
---|
[bdd5a63] | 92 | }
|
---|
| 93 | else if (ev.type == SDL_KEYUP) {
|
---|
| 94 | down = SDL_SCANCODE_UNKNOWN;
|
---|
[b48c8a5] | 95 | ver2("sdl dedup: reset");
|
---|
[bdd5a63] | 96 | }
|
---|
| 97 | #endif
|
---|
| 98 |
|
---|
| 99 | if (ev.type == SDL_QUIT ||
|
---|
| 100 | (ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_ESCAPE)) {
|
---|
[b48c8a5] | 101 | inf("quit event");
|
---|
[bdd5a63] | 102 | SDL_AtomicSet(&run, 0);
|
---|
| 103 | continue;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[4ed9bfe] | 106 | if (ev.type == SDL_WINDOWEVENT) {
|
---|
| 107 | if (ev.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) {
|
---|
| 108 | ver("sdl ev win %u", ev.window.windowID);
|
---|
| 109 | win = ev.window.windowID;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[b48c8a5] | 113 | if (ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_DOWN) {
|
---|
| 114 | ver("sdl ev down-arrow");
|
---|
| 115 | rel_mod = true;
|
---|
[657abdf] | 116 | ser_mou_res();
|
---|
[b48c8a5] | 117 | continue;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | if (ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_UP) {
|
---|
| 121 | ver("sdl ev up-arrow");
|
---|
| 122 | rel_mod = false;
|
---|
| 123 | continue;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[657abdf] | 126 | if (rel_mod) {
|
---|
| 127 | if (ev.type == SDL_MOUSEMOTION) {
|
---|
| 128 | ver("sdl ev mousemotion (%d, %d)", ev.motion.xrel, ev.motion.yrel);
|
---|
| 129 | ser_mou_mov(&ev.motion);
|
---|
| 130 | continue;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | if (ev.type == SDL_MOUSEBUTTONDOWN) {
|
---|
| 134 | ver("sdl ev mousebuttondown %d", ev.button.button);
|
---|
| 135 | ser_mou_dn(&ev.button);
|
---|
| 136 | continue;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | if (ev.type == SDL_MOUSEBUTTONUP) {
|
---|
| 140 | ver("sdl ev mousebuttonup %d", ev.button.button);
|
---|
| 141 | ser_mou_up(&ev.button);
|
---|
| 142 | continue;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[bdd5a63] | 146 | if (ev.type == SDL_TEXTINPUT) {
|
---|
[b48c8a5] | 147 | ver("sdl ev text input %d", ev.text.text[0]);
|
---|
[4ed9bfe] | 148 |
|
---|
| 149 | if (win == ser_win) {
|
---|
| 150 | ser_text(&ev.text);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[bdd5a63] | 153 | continue;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | if (ev.type == SDL_KEYDOWN) {
|
---|
[b48c8a5] | 157 | ver("sdl ev key down %d", (int32_t)ev.key.keysym.sym);
|
---|
[4ed9bfe] | 158 |
|
---|
| 159 | if (win == ser_win) {
|
---|
| 160 | ser_key(&ev.key);
|
---|
| 161 | }
|
---|
| 162 | else if (win == vid_win) {
|
---|
[81d7cdc] | 163 | kbd_key(&ev.key, true, true);
|
---|
| 164 | }
|
---|
| 165 | else if (win == lcd_win) {
|
---|
| 166 | kbd_key(&ev.key, false, true);
|
---|
[ca77925] | 167 | }
|
---|
| 168 |
|
---|
| 169 | continue;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | if (ev.type == SDL_KEYUP) {
|
---|
| 173 | ver("sdl ev key up %d", (int32_t)ev.key.keysym.sym);
|
---|
| 174 |
|
---|
| 175 | if (win == vid_win) {
|
---|
[81d7cdc] | 176 | kbd_key(&ev.key, true, false);
|
---|
| 177 | }
|
---|
| 178 | else if (win == lcd_win) {
|
---|
| 179 | kbd_key(&ev.key, false, false);
|
---|
[4ed9bfe] | 180 | }
|
---|
| 181 |
|
---|
[bdd5a63] | 182 | continue;
|
---|
| 183 | }
|
---|
[b48c8a5] | 184 | }
|
---|
| 185 |
|
---|
| 186 | SDL_Delay(50);
|
---|
[bdd5a63] | 187 |
|
---|
[b48c8a5] | 188 | if (SDL_GetRelativeMouseMode() != rel_mod) {
|
---|
| 189 | SDL_SetRelativeMouseMode(rel_mod);
|
---|
| 190 |
|
---|
| 191 | if (rel_mod) {
|
---|
| 192 | inf("MOUSE CAPTURED - press UP-ARROW KEY to release");
|
---|
| 193 | }
|
---|
| 194 | else {
|
---|
| 195 | inf("mouse released");
|
---|
| 196 | }
|
---|
[bdd5a63] | 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | inf("leaving SDL loop");
|
---|
| 201 | }
|
---|