From 32d57390d9de2f9d9bdf55823038fea7fde4bc3f Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 3 Sep 2015 11:39:04 +0200 Subject: gl: Use upper control characters for special keys. --- platform/gl/gl-app.h | 42 ++++++++++++++++++++++- platform/gl/gl-input.c | 53 +++++++++++++---------------- platform/gl/gl-main.c | 92 ++++++++++++++++++++++++++++---------------------- 3 files changed, 117 insertions(+), 70 deletions(-) diff --git a/platform/gl/gl-app.h b/platform/gl/gl-app.h index 1cf5b91b..14dd679c 100644 --- a/platform/gl/gl-app.h +++ b/platform/gl/gl-app.h @@ -3,11 +3,51 @@ extern fz_context *ctx; +enum +{ + /* regular control characters */ + KEY_ESCAPE = 27, + KEY_ENTER = '\r', + KEY_TAB = '\t', + KEY_BACKSPACE = '\b', + + KEY_CTL_A = 'A' - 64, + KEY_CTL_B, KEY_CTL_C, KEY_CTL_D, KEY_CTL_E, KEY_CTL_F, + KEY_CTL_G, KEY_CTL_H, KEY_CTL_I, KEY_CTL_J, KEY_CTL_K, KEY_CTL_L, + KEY_CTL_M, KEY_CTL_N, KEY_CTL_O, KEY_CTL_P, KEY_CTL_Q, KEY_CTL_R, + KEY_CTL_S, KEY_CTL_T, KEY_CTL_U, KEY_CTL_V, KEY_CTL_W, KEY_CTL_X, + KEY_CTL_Y, KEY_CTL_Z, + + /* reuse control characters > 127 for special keys */ + KEY_INSERT = 127, + KEY_DELETE, + KEY_PAGE_UP, + KEY_PAGE_DOWN, + KEY_HOME, + KEY_END, + KEY_LEFT, + KEY_UP, + KEY_RIGHT, + KEY_DOWN, + KEY_F1, + KEY_F2, + KEY_F3, + KEY_F4, + KEY_F5, + KEY_F6, + KEY_F7, + KEY_F8, + KEY_F9, + KEY_F10, + KEY_F11, + KEY_F12, +}; + struct ui { int x, y; int down, middle, right; - int key, special, mod; + int key, mod; void *hot, *active, *focus; diff --git a/platform/gl/gl-input.c b/platform/gl/gl-input.c index d0fbcc8f..870db9c7 100644 --- a/platform/gl/gl-input.c +++ b/platform/gl/gl-input.c @@ -1,7 +1,5 @@ #include "gl-app.h" -#define CONTROL(c) (c - 64) - static void draw_string_part(float x, float y, const int *s, const int *e) { ui_begin_text(ctx); @@ -66,9 +64,9 @@ static void ui_input_delete_selection(struct input *input) static int ui_input_key(struct input *input) { - switch (ui.special) + switch (ui.key) { - case GLFW_KEY_LEFT: + case KEY_LEFT: if (ui.mod == GLFW_MOD_CONTROL + GLFW_MOD_SHIFT) { input->q = skip_word_left(input->q, input->text); @@ -93,7 +91,7 @@ static int ui_input_key(struct input *input) input->p = input->q = --(input->q); } break; - case GLFW_KEY_RIGHT: + case KEY_RIGHT: if (ui.mod == GLFW_MOD_CONTROL + GLFW_MOD_SHIFT) { input->q = skip_word_right(input->q, input->end); @@ -118,8 +116,8 @@ static int ui_input_key(struct input *input) input->p = input->q = ++(input->q); } break; - case GLFW_KEY_UP: - case GLFW_KEY_HOME: + case KEY_UP: + case KEY_HOME: if (ui.mod == GLFW_MOD_CONTROL + GLFW_MOD_SHIFT) { input->q = input->text; @@ -137,8 +135,8 @@ static int ui_input_key(struct input *input) input->p = input->q = input->text; } break; - case GLFW_KEY_DOWN: - case GLFW_KEY_END: + case KEY_DOWN: + case KEY_END: if (ui.mod == GLFW_MOD_CONTROL + GLFW_MOD_SHIFT) { input->q = input->end; @@ -156,7 +154,7 @@ static int ui_input_key(struct input *input) input->p = input->q = input->end; } break; - case GLFW_KEY_DELETE: + case KEY_DELETE: if (input->p != input->q) ui_input_delete_selection(input); else if (input->p < input->end) @@ -166,20 +164,27 @@ static int ui_input_key(struct input *input) --(input->end); } break; - } - switch (ui.key) - { - case '\e': + case KEY_ESCAPE: return -1; - case '\r': + case KEY_ENTER: return 1; - case CONTROL('A'): + case KEY_BACKSPACE: + if (input->p != input->q) + ui_input_delete_selection(input); + else if (input->p > input->text && input->end > input->text) + { + memmove(input->p - 1, input->p, (input->end - input->p) * sizeof (*input->p)); + input->q = --(input->p); + --(input->end); + } + break; + case KEY_CTL_A: input->p = input->q = input->text; break; - case CONTROL('E'): + case KEY_CTL_E: input->p = input->q = input->end; break; - case CONTROL('W'): + case KEY_CTL_W: if (input->p != input->q) ui_input_delete_selection(input); else @@ -188,19 +193,9 @@ static int ui_input_key(struct input *input) ui_input_delete_selection(input); } break; - case CONTROL('U'): + case KEY_CTL_U: input->p = input->q = input->end = input->text; break; - case '\b': - if (input->p != input->q) - ui_input_delete_selection(input); - else if (input->p > input->text && input->end > input->text) - { - memmove(input->p - 1, input->p, (input->end - input->p) * sizeof (*input->p)); - input->q = --(input->p); - --(input->end); - } - break; default: if (ui.key >= 32) { diff --git a/platform/gl/gl-main.c b/platform/gl/gl-main.c index cae2a63d..aee84103 100644 --- a/platform/gl/gl-main.c +++ b/platform/gl/gl-main.c @@ -823,10 +823,10 @@ static void smart_move_forward(void) static void do_app(void) { - if (ui.special == GLFW_KEY_F4 && ui.mod == GLFW_MOD_ALT) + if (ui.key == KEY_F4 && ui.mod == GLFW_MOD_ALT) exit(0); - if (!ui.focus && (ui.key || ui.special)) + if (!ui.focus && ui.key) { switch (ui.key) { @@ -899,16 +899,12 @@ static void do_app(void) case 'l': showlinks = !showlinks; break; case '/': search_dir = 1; showsearch = 1; search_input.p = search_input.text; search_input.q = search_input.end; break; case '?': search_dir = -1; showsearch = 1; search_input.p = search_input.text; search_input.q = search_input.end; break; - } - - switch (ui.special) - { - case GLFW_KEY_UP: scroll_y -= 10; break; - case GLFW_KEY_DOWN: scroll_y += 10; break; - case GLFW_KEY_LEFT: scroll_x -= 10; break; - case GLFW_KEY_RIGHT: scroll_x += 10; break; - case GLFW_KEY_PAGE_UP: currentpage -= fz_maxi(number, 1); number = 0; break; - case GLFW_KEY_PAGE_DOWN: currentpage += fz_maxi(number, 1); number = 0; break; + case KEY_UP: scroll_y -= 10; break; + case KEY_DOWN: scroll_y += 10; break; + case KEY_LEFT: scroll_x -= 10; break; + case KEY_RIGHT: scroll_x += 10; break; + case KEY_PAGE_UP: currentpage -= fz_maxi(number, 1); number = 0; break; + case KEY_PAGE_DOWN: currentpage += fz_maxi(number, 1); number = 0; break; } if (ui.key >= '0' && ui.key <= '9') @@ -926,7 +922,7 @@ static void do_app(void) ui_needs_update = 1; - ui.key = ui.special = 0; /* we ate the key event, so zap it */ + ui.key = 0; /* we ate the key event, so zap it */ } } @@ -1021,11 +1017,11 @@ static void run_main_loop(void) { float start_time = glfwGetTime(); - if (ui.key == '\e') + if (ui.key == KEY_ESCAPE) search_active = 0; /* ignore events during search */ - ui.key = ui.special = ui.mod = 0; + ui.key = ui.mod = 0; ui.down = ui.middle = ui.right = 0; while (glfwGetTime() < start_time + 0.2) @@ -1126,41 +1122,57 @@ static void on_char(GLFWwindow *window, unsigned int key, int mod) ui.key = key; ui.mod = mod; run_main_loop(); - ui.key = ui.special = ui.mod = 0; + ui.key = ui.mod = 0; } static void on_key(GLFWwindow *window, int special, int scan, int action, int mod) { if (action == GLFW_PRESS || action == GLFW_REPEAT) { + ui.key = 0; switch (special) { - case GLFW_KEY_INSERT: - case GLFW_KEY_DELETE: - case GLFW_KEY_RIGHT: - case GLFW_KEY_LEFT: - case GLFW_KEY_DOWN: - case GLFW_KEY_UP: - case GLFW_KEY_PAGE_UP: - case GLFW_KEY_PAGE_DOWN: - case GLFW_KEY_HOME: - case GLFW_KEY_END: - case GLFW_KEY_F1: - case GLFW_KEY_F2: - case GLFW_KEY_F3: - case GLFW_KEY_F5: - case GLFW_KEY_F6: - case GLFW_KEY_F7: - case GLFW_KEY_F8: - case GLFW_KEY_F9: - case GLFW_KEY_F10: - case GLFW_KEY_F11: - case GLFW_KEY_F12: - ui.special = special; +#ifndef GLFW_MUPDF_FIXES + /* regular control characters: ^A, ^B, etc. */ + default: + if (special >= 'A' && special <= 'Z' && mod == GLFW_MOD_CONTROL) + ui.key = KEY_CTL_A + special - 'A'; + break; + + /* regular control characters: escape, enter, backspace, tab */ + case GLFW_KEY_ESCAPE: ui.key = KEY_ESCAPE; break; + case GLFW_KEY_ENTER: ui.key = KEY_ENTER; break; + case GLFW_KEY_BACKSPACE: ui.key = KEY_BACKSPACE; break; + case GLFW_KEY_TAB: ui.key = KEY_TAB; break; +#endif + case GLFW_KEY_INSERT: ui.key = KEY_INSERT; break; + case GLFW_KEY_DELETE: ui.key = KEY_DELETE; break; + case GLFW_KEY_RIGHT: ui.key = KEY_RIGHT; break; + case GLFW_KEY_LEFT: ui.key = KEY_LEFT; break; + case GLFW_KEY_DOWN: ui.key = KEY_DOWN; break; + case GLFW_KEY_UP: ui.key = KEY_UP; break; + case GLFW_KEY_PAGE_UP: ui.key = KEY_PAGE_UP; break; + case GLFW_KEY_PAGE_DOWN: ui.key = KEY_PAGE_DOWN; break; + case GLFW_KEY_HOME: ui.key = KEY_HOME; break; + case GLFW_KEY_END: ui.key = KEY_END; break; + case GLFW_KEY_F1: ui.key = KEY_F1; break; + case GLFW_KEY_F2: ui.key = KEY_F2; break; + case GLFW_KEY_F3: ui.key = KEY_F3; break; + case GLFW_KEY_F4: ui.key = KEY_F4; break; + case GLFW_KEY_F5: ui.key = KEY_F5; break; + case GLFW_KEY_F6: ui.key = KEY_F6; break; + case GLFW_KEY_F7: ui.key = KEY_F7; break; + case GLFW_KEY_F8: ui.key = KEY_F8; break; + case GLFW_KEY_F9: ui.key = KEY_F9; break; + case GLFW_KEY_F10: ui.key = KEY_F10; break; + case GLFW_KEY_F11: ui.key = KEY_F11; break; + case GLFW_KEY_F12: ui.key = KEY_F12; break; + } + if (ui.key) + { ui.mod = mod; run_main_loop(); - ui.key = ui.special = ui.mod = 0; - break; + ui.key = ui.mod = 0; } } } -- cgit v1.2.3