use ints for keycode

This commit is contained in:
Anthony Pesch 2017-10-05 11:21:35 -04:00
parent ff29b1a885
commit 8d4486b2c3
9 changed files with 162 additions and 161 deletions

View File

@ -688,7 +688,7 @@ int emu_load(struct emu *emu, const char *path) {
return dc_load(emu->dc, path);
}
void emu_keydown(struct emu *emu, int port, enum keycode key, int16_t value) {
void emu_keydown(struct emu *emu, int port, int key, int16_t value) {
if (key == K_F1 && value > 0) {
OPTION_debug = !OPTION_debug;
}

View File

@ -13,7 +13,7 @@ void emu_destroy(struct emu *emu);
void emu_vid_created(struct emu *emu, struct render_backend *r);
void emu_vid_destroyed(struct emu *emu);
void emu_vid_swapped(struct emu *emu);
void emu_keydown(struct emu *emu, int port, enum keycode key, int16_t value);
void emu_keydown(struct emu *emu, int port, int key, int16_t value);
int emu_load(struct emu *emu, const char *path);
void emu_render_frame(struct emu *emu);

View File

@ -2,133 +2,135 @@
#include "core/core.h"
struct key {
enum keycode code;
int code;
const char *name;
};
static struct key keys[] = {{K_UNKNOWN, "unknown"},
{K_SPACE, "space"},
{(enum keycode)'!', "!"},
{(enum keycode)'"', "\""},
{(enum keycode)'#', "#"},
{(enum keycode)'$', "$"},
{(enum keycode)'%', "%%"},
{(enum keycode)'&', "&"},
{(enum keycode)'\'', "\'"},
{(enum keycode)'(', "("},
{(enum keycode)')', ")"},
{(enum keycode)'*', "*"},
{(enum keycode)'+', "+"},
{(enum keycode)',', ","},
{(enum keycode)'-', "-"},
{(enum keycode)'.', "."},
{(enum keycode)'/', "/"},
{(enum keycode)'0', "0"},
{(enum keycode)'1', "1"},
{(enum keycode)'2', "2"},
{(enum keycode)'3', "3"},
{(enum keycode)'4', "4"},
{(enum keycode)'5', "5"},
{(enum keycode)'6', "6"},
{(enum keycode)'7', "7"},
{(enum keycode)'8', "8"},
{(enum keycode)'9', "9"},
{(enum keycode)':', ":"},
{(enum keycode)';', ";"},
{(enum keycode)'<', "<"},
{(enum keycode)'=', "="},
{(enum keycode)'>', ">"},
{(enum keycode)'?', "?"},
{(enum keycode)'@', "@"},
{(enum keycode)'[', "["},
{(enum keycode)'\\', "\\"},
{(enum keycode)']', "]"},
{(enum keycode)'^', "^"},
{(enum keycode)'_', "_"},
{(enum keycode)'`', "`"},
{(enum keycode)'a', "a"},
{(enum keycode)'b', "b"},
{(enum keycode)'c', "c"},
{(enum keycode)'d', "d"},
{(enum keycode)'e', "e"},
{(enum keycode)'f', "f"},
{(enum keycode)'g', "g"},
{(enum keycode)'h', "h"},
{(enum keycode)'i', "i"},
{(enum keycode)'j', "j"},
{(enum keycode)'k', "k"},
{(enum keycode)'l', "l"},
{(enum keycode)'m', "m"},
{(enum keycode)'n', "n"},
{(enum keycode)'o', "o"},
{(enum keycode)'p', "p"},
{(enum keycode)'q', "q"},
{(enum keycode)'r', "r"},
{(enum keycode)'s', "s"},
{(enum keycode)'t', "t"},
{(enum keycode)'u', "u"},
{(enum keycode)'v', "v"},
{(enum keycode)'w', "w"},
{(enum keycode)'x', "x"},
{(enum keycode)'y', "y"},
{(enum keycode)'z', "z"},
{(enum keycode)'{', "{"},
{(enum keycode)'|', "|"},
{(enum keycode)'}', "}"},
{(enum keycode)'~', "~"},
{K_CAPSLOCK, "capslock"},
{K_RETURN, "return"},
{K_ESCAPE, "escape"},
{K_BACKSPACE, "backspace"},
{K_TAB, "tab"},
{K_PAGEUP, "pageup"},
{K_PAGEDOWN, "pagedown"},
{K_DELETE, "delete"},
{K_RIGHT, "right"},
{K_LEFT, "left"},
{K_DOWN, "down"},
{K_UP, "up"},
{K_LCTRL, "lctrl"},
{K_LSHIFT, "lshift"},
{K_LALT, "lalt"},
{K_LGUI, "lgui"},
{K_RCTRL, "rctrl"},
{K_RSHIFT, "rshift"},
{K_RALT, "ralt"},
{K_RGUI, "rgui"},
{K_F1, "f1"},
{K_F2, "f2"},
{K_F3, "f3"},
{K_F4, "f4"},
{K_F5, "f5"},
{K_F6, "f6"},
{K_F7, "f7"},
{K_F8, "f8"},
{K_F9, "f9"},
{K_F10, "f10"},
{K_F11, "f11"},
{K_F12, "f12"},
{K_F13, "f13"},
{K_F14, "f14"},
{K_F15, "f15"},
{K_F16, "f16"},
{K_F17, "f17"},
{K_F18, "f18"},
{K_F19, "f19"},
{K_F20, "f20"},
{K_F21, "f21"},
{K_F22, "f22"},
{K_F23, "f23"},
{K_F24, "f24"},
{K_MOUSE1, "mouse1"},
{K_MOUSE2, "mouse2"},
{K_MOUSE3, "mouse3"},
{K_MOUSE4, "mouse4"},
{K_MOUSE5, "mouse5"},
{K_MWHEELUP, "mwheelup"},
{K_MWHEELDOWN, "mwheeldown"}};
static struct key keys[] = {
{K_UNKNOWN, "unknown"},
{K_SPACE, "space"},
{'!', "!"},
{'"', "\""},
{'#', "#"},
{'$', "$"},
{'%', "%%"},
{'&', "&"},
{'\'', "\'"},
{'(', "("},
{')', ")"},
{'*', "*"},
{'+', "+"},
{',', ","},
{'-', "-"},
{'.', "."},
{'/', "/"},
{'0', "0"},
{'1', "1"},
{'2', "2"},
{'3', "3"},
{'4', "4"},
{'5', "5"},
{'6', "6"},
{'7', "7"},
{'8', "8"},
{'9', "9"},
{':', ":"},
{';', ";"},
{'<', "<"},
{'=', "="},
{'>', ">"},
{'?', "?"},
{'@', "@"},
{'[', "["},
{'\\', "\\"},
{']', "]"},
{'^', "^"},
{'_', "_"},
{'`', "`"},
{'a', "a"},
{'b', "b"},
{'c', "c"},
{'d', "d"},
{'e', "e"},
{'f', "f"},
{'g', "g"},
{'h', "h"},
{'i', "i"},
{'j', "j"},
{'k', "k"},
{'l', "l"},
{'m', "m"},
{'n', "n"},
{'o', "o"},
{'p', "p"},
{'q', "q"},
{'r', "r"},
{'s', "s"},
{'t', "t"},
{'u', "u"},
{'v', "v"},
{'w', "w"},
{'x', "x"},
{'y', "y"},
{'z', "z"},
{'{', "{"},
{'|', "|"},
{'}', "}"},
{'~', "~"},
{K_CAPSLOCK, "capslock"},
{K_RETURN, "return"},
{K_ESCAPE, "escape"},
{K_BACKSPACE, "backspace"},
{K_TAB, "tab"},
{K_PAGEUP, "pageup"},
{K_PAGEDOWN, "pagedown"},
{K_DELETE, "delete"},
{K_RIGHT, "right"},
{K_LEFT, "left"},
{K_DOWN, "down"},
{K_UP, "up"},
{K_LCTRL, "lctrl"},
{K_LSHIFT, "lshift"},
{K_LALT, "lalt"},
{K_LGUI, "lgui"},
{K_RCTRL, "rctrl"},
{K_RSHIFT, "rshift"},
{K_RALT, "ralt"},
{K_RGUI, "rgui"},
{K_F1, "f1"},
{K_F2, "f2"},
{K_F3, "f3"},
{K_F4, "f4"},
{K_F5, "f5"},
{K_F6, "f6"},
{K_F7, "f7"},
{K_F8, "f8"},
{K_F9, "f9"},
{K_F10, "f10"},
{K_F11, "f11"},
{K_F12, "f12"},
{K_F13, "f13"},
{K_F14, "f14"},
{K_F15, "f15"},
{K_F16, "f16"},
{K_F17, "f17"},
{K_F18, "f18"},
{K_F19, "f19"},
{K_F20, "f20"},
{K_F21, "f21"},
{K_F22, "f22"},
{K_F23, "f23"},
{K_F24, "f24"},
{K_MOUSE1, "mouse1"},
{K_MOUSE2, "mouse2"},
{K_MOUSE3, "mouse3"},
{K_MOUSE4, "mouse4"},
{K_MOUSE5, "mouse5"},
{K_MWHEELUP, "mwheelup"},
{K_MWHEELDOWN, "mwheeldown"},
};
enum keycode get_key_by_name(const char *keyname) {
int get_key_by_name(const char *keyname) {
char buffer[256] = {0};
int len = 0;
@ -148,7 +150,7 @@ enum keycode get_key_by_name(const char *keyname) {
return K_UNKNOWN;
}
const char *get_name_by_key(enum keycode keycode) {
const char *get_name_by_key(int keycode) {
static const char *unknown = "unknown";
for (size_t i = 0, l = sizeof(keys) / sizeof(struct key); i < l; i++) {

View File

@ -11,7 +11,7 @@
#define KEY_DOWN INT16_MAX
#define KEY_UP 0
enum keycode {
enum {
K_UNKNOWN,
K_SPACE = 32,
@ -94,7 +94,7 @@ enum keycode {
K_NUM_KEYS
};
enum keycode get_key_by_name(const char *keyname);
const char *get_name_by_key(enum keycode keycode);
int get_key_by_name(const char *keyname);
const char *get_name_by_key(int key);
#endif

View File

@ -297,12 +297,12 @@ static int video_restart(struct host *host) {
*/
static void host_poll_events(struct host *host);
static enum keycode translate_sdl_key(SDL_Keysym keysym) {
enum keycode out = K_UNKNOWN;
static int translate_sdl_key(SDL_Keysym keysym) {
int out = K_UNKNOWN;
if (keysym.sym >= SDLK_SPACE && keysym.sym <= SDLK_z) {
/* this range maps 1:1 with ASCII chars */
out = (enum keycode)keysym.sym;
out = keysym.sym;
} else {
switch (keysym.sym) {
case SDLK_CAPSLOCK:
@ -464,9 +464,8 @@ static void input_mousemove(struct host *host, int port, int x, int y) {
imgui_mousemove(host->imgui, x, y);
}
static void input_keydown(struct host *host, int port, enum keycode key,
int16_t value) {
enum keycode keys[2] = {key, 0};
static void input_keydown(struct host *host, int port, int key, int16_t value) {
int keys[2] = {key, 0};
int num_keys = 1;
/* send an extra event for keys that map to a controller button */
@ -577,7 +576,7 @@ static void host_poll_events(struct host *host) {
while (SDL_PollEvent(&ev)) {
switch (ev.type) {
case SDL_KEYDOWN: {
enum keycode keycode = translate_sdl_key(ev.key.keysym);
int keycode = translate_sdl_key(ev.key.keysym);
if (keycode != K_UNKNOWN) {
input_keydown(host, 0, keycode, KEY_DOWN);
@ -585,7 +584,7 @@ static void host_poll_events(struct host *host) {
} break;
case SDL_KEYUP: {
enum keycode keycode = translate_sdl_key(ev.key.keysym);
int keycode = translate_sdl_key(ev.key.keysym);
if (keycode != K_UNKNOWN) {
input_keydown(host, 0, keycode, KEY_UP);
@ -594,7 +593,7 @@ static void host_poll_events(struct host *host) {
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP: {
enum keycode keycode;
int keycode;
switch (ev.button.button) {
case SDL_BUTTON_LEFT:
@ -651,7 +650,7 @@ static void host_poll_events(struct host *host) {
case SDL_CONTROLLERAXISMOTION: {
int port = input_find_controller_port(host, ev.caxis.which);
enum keycode key = K_UNKNOWN;
int key = K_UNKNOWN;
int16_t value = ev.caxis.value;
switch (ev.caxis.axis) {
@ -677,7 +676,7 @@ static void host_poll_events(struct host *host) {
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP: {
int port = input_find_controller_port(host, ev.cbutton.which);
enum keycode key = K_UNKNOWN;
int key = K_UNKNOWN;
int16_t value = ev.type == SDL_CONTROLLERBUTTONDOWN ? KEY_DOWN : KEY_UP;
switch (ev.cbutton.button) {

View File

@ -125,33 +125,33 @@ void imgui_begin_frame(struct imgui *imgui) {
#endif
}
void imgui_keydown(struct imgui *imgui, enum keycode code, int16_t value) {
void imgui_keydown(struct imgui *imgui, int key, int16_t value) {
#ifdef HAVE_IMGUI
ImGuiIO &io = ImGui::GetIO();
int down = value > 0;
if (code == K_MWHEELUP) {
if (key == K_MWHEELUP) {
io.MouseWheel = 1.0f;
} else if (code == K_MWHEELDOWN) {
} else if (key == K_MWHEELDOWN) {
io.MouseWheel = -1.0f;
} else if (code == K_MOUSE1) {
} else if (key == K_MOUSE1) {
io.MouseDown[0] = down;
} else if (code == K_MOUSE2) {
} else if (key == K_MOUSE2) {
io.MouseDown[1] = down;
} else if (code == K_MOUSE3) {
} else if (key == K_MOUSE3) {
io.MouseDown[2] = down;
} else if (code == K_LALT || code == K_RALT) {
imgui->alt[code == K_LALT ? 0 : 1] = down;
} else if (key == K_LALT || key == K_RALT) {
imgui->alt[key == K_LALT ? 0 : 1] = down;
io.KeyAlt = imgui->alt[0] || imgui->alt[1];
} else if (code == K_LCTRL || code == K_RCTRL) {
imgui->ctrl[code == K_LCTRL ? 0 : 1] = down;
} else if (key == K_LCTRL || key == K_RCTRL) {
imgui->ctrl[key == K_LCTRL ? 0 : 1] = down;
io.KeyCtrl = imgui->ctrl[0] || imgui->ctrl[1];
} else if (code == K_LSHIFT || code == K_RSHIFT) {
imgui->shift[code == K_LSHIFT ? 0 : 1] = down;
} else if (key == K_LSHIFT || key == K_RSHIFT) {
imgui->shift[key == K_LSHIFT ? 0 : 1] = down;
io.KeyShift = imgui->shift[0] || imgui->shift[1];
} else {
imgui->keys[code] = value;
io.KeysDown[code] = down;
imgui->keys[key] = value;
io.KeysDown[key] = down;
}
#endif
}

View File

@ -19,7 +19,7 @@ void imgui_destroy(struct imgui *imgui);
void imgui_vid_created(struct imgui *imgui, struct render_backend *r);
void imgui_vid_destroyed(struct imgui *imgui);
void imgui_mousemove(struct imgui *imgui, int x, int y);
void imgui_keydown(struct imgui *imgui, enum keycode key, int16_t value);
void imgui_keydown(struct imgui *imgui, int key, int16_t value);
void imgui_begin_frame(struct imgui *imgui);
void imgui_end_frame(struct imgui *imgui);

View File

@ -690,14 +690,14 @@ int tracer_load(struct tracer *tracer, const char *path) {
return 1;
}
void tracer_keydown(struct tracer *tracer, enum keycode key, int16_t value) {
if (key == K_LEFT && value > 0) {
void tracer_keydown(struct tracer *tracer, int key, int16_t value) {
if (key == K_LEFT && value) {
tracer_prev_context(tracer);
} else if (key == K_RIGHT && value > 0) {
} else if (key == K_RIGHT && value) {
tracer_next_context(tracer);
} else if (key == K_UP && value > 0) {
} else if (key == K_UP && value) {
tracer_prev_param(tracer);
} else if (key == K_DOWN && value > 0) {
} else if (key == K_DOWN && value) {
tracer_next_param(tracer);
}
}

View File

@ -12,7 +12,7 @@ void tracer_destroy(struct tracer *tracer);
void tracer_vid_created(struct tracer *tracer, struct render_backend *r);
void tracer_vid_destroyed(struct tracer *tracer);
void tracer_keydown(struct tracer *tracer, enum keycode key, int16_t value);
void tracer_keydown(struct tracer *tracer, int key, int16_t value);
int tracer_load(struct tracer *tracer, const char *path);
void tracer_render_frame(struct tracer *tracer);