| 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
|
|---|
| 15 | * "gpl.txt" in the top directory of this repository.
|
|---|
| 16 | */
|
|---|
| 17 |
|
|---|
| 18 | #include <all.h>
|
|---|
| 19 |
|
|---|
| 20 | int32_t sdl_verbose = 0;
|
|---|
| 21 |
|
|---|
| 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__)
|
|---|
| 25 |
|
|---|
| 26 | typedef void (*sdl_func_t)(void);
|
|---|
| 27 |
|
|---|
| 28 | static sdl_func_t sdl_funcs[] = {
|
|---|
| 29 | ser_sdl, vid_sdl
|
|---|
| 30 | };
|
|---|
| 31 |
|
|---|
| 32 | void sdl_init(void)
|
|---|
| 33 | {
|
|---|
| 34 | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) < 0) {
|
|---|
| 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);
|
|---|
| 40 |
|
|---|
| 41 | if (SDLNet_Init() < 0) {
|
|---|
| 42 | fail("SDLNet_Init() failed: %s", SDLNet_GetError());
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | if (TTF_Init() < 0) {
|
|---|
| 46 | fail("TTF_Init() failed: %s", TTF_GetError());
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
|
|---|
| 50 | SDL_StartTextInput();
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | void sdl_quit(void)
|
|---|
| 54 | {
|
|---|
| 55 | TTF_Quit();
|
|---|
| 56 | SDLNet_Quit();
|
|---|
| 57 | SDL_Quit();
|
|---|
| 58 | }
|
|---|
| 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 |
|
|---|
| 68 | bool rel_mod = false;
|
|---|
| 69 |
|
|---|
| 70 | while (SDL_AtomicGet(&run) != 0) {
|
|---|
| 71 | for (int32_t i = 0; i < ARRAY_COUNT(sdl_funcs); ++i) {
|
|---|
| 72 | sdl_funcs[i]();
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | SDL_Event ev;
|
|---|
| 76 |
|
|---|
| 77 | while (SDL_PollEvent(&ev) > 0) {
|
|---|
| 78 | ver2("sdl ev %d", ev.type);
|
|---|
| 79 |
|
|---|
| 80 | #if defined EMU_LINUX
|
|---|
| 81 | // Work around duplicate key-down events on Linux.
|
|---|
| 82 |
|
|---|
| 83 | if (ev.type == SDL_KEYDOWN) {
|
|---|
| 84 | if (down == ev.key.keysym.scancode) {
|
|---|
| 85 | ver2("sdl dedup: skip %d", (int32_t)down);
|
|---|
| 86 | continue;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | down = ev.key.keysym.scancode;
|
|---|
| 90 | ver2("sdl dedup: %d", (int32_t)down);
|
|---|
| 91 | }
|
|---|
| 92 | else if (ev.type == SDL_KEYUP) {
|
|---|
| 93 | down = SDL_SCANCODE_UNKNOWN;
|
|---|
| 94 | ver2("sdl dedup: reset");
|
|---|
| 95 | }
|
|---|
| 96 | #endif
|
|---|
| 97 |
|
|---|
| 98 | if (ev.type == SDL_QUIT ||
|
|---|
| 99 | (ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_ESCAPE)) {
|
|---|
| 100 | inf("quit event");
|
|---|
| 101 | SDL_AtomicSet(&run, 0);
|
|---|
| 102 | continue;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | if (ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_DOWN) {
|
|---|
| 106 | ver("sdl ev down-arrow");
|
|---|
| 107 | rel_mod = true;
|
|---|
| 108 | ser_mou_res();
|
|---|
| 109 | continue;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | if (ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_UP) {
|
|---|
| 113 | ver("sdl ev up-arrow");
|
|---|
| 114 | rel_mod = false;
|
|---|
| 115 | continue;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | if (rel_mod) {
|
|---|
| 119 | if (ev.type == SDL_MOUSEMOTION) {
|
|---|
| 120 | ver("sdl ev mousemotion (%d, %d)", ev.motion.xrel, ev.motion.yrel);
|
|---|
| 121 | ser_mou_mov(&ev.motion);
|
|---|
| 122 | continue;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | if (ev.type == SDL_MOUSEBUTTONDOWN) {
|
|---|
| 126 | ver("sdl ev mousebuttondown %d", ev.button.button);
|
|---|
| 127 | ser_mou_dn(&ev.button);
|
|---|
| 128 | continue;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | if (ev.type == SDL_MOUSEBUTTONUP) {
|
|---|
| 132 | ver("sdl ev mousebuttonup %d", ev.button.button);
|
|---|
| 133 | ser_mou_up(&ev.button);
|
|---|
| 134 | continue;
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | if (ev.type == SDL_TEXTINPUT) {
|
|---|
| 139 | ver("sdl ev text input %d", ev.text.text[0]);
|
|---|
| 140 | ser_text(&ev.text);
|
|---|
| 141 | continue;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | if (ev.type == SDL_KEYDOWN) {
|
|---|
| 145 | ver("sdl ev key down %d", (int32_t)ev.key.keysym.sym);
|
|---|
| 146 | ser_key(&ev.key);
|
|---|
| 147 | continue;
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | SDL_Delay(50);
|
|---|
| 152 |
|
|---|
| 153 | if (SDL_GetRelativeMouseMode() != rel_mod) {
|
|---|
| 154 | SDL_SetRelativeMouseMode(rel_mod);
|
|---|
| 155 |
|
|---|
| 156 | if (rel_mod) {
|
|---|
| 157 | inf("MOUSE CAPTURED - press UP-ARROW KEY to release");
|
|---|
| 158 | }
|
|---|
| 159 | else {
|
|---|
| 160 | inf("mouse released");
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | inf("leaving SDL loop");
|
|---|
| 166 | }
|
|---|