inputmanager.c 7.6 KB
Newer Older
1
#include "inputmanager.h"
2 3

#include "convert.h"
4
#include "lockutil.h"
R
Romain Vimont 已提交
5
#include "log.h"
6

R
Romain Vimont 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Convert window coordinates (as provided by SDL_GetMouseState() to renderer coordinates (as provided in SDL mouse events)
//
// See my question:
// <https://stackoverflow.com/questions/49111054/how-to-get-mouse-position-on-mouse-wheel-event>
static void convert_to_renderer_coordinates(SDL_Renderer *renderer, int *x, int *y) {
    SDL_Rect viewport;
    float scale_x, scale_y;
    SDL_RenderGetViewport(renderer, &viewport);
    SDL_RenderGetScale(renderer, &scale_x, &scale_y);
    *x = (int) (*x / scale_x) - viewport.x;
    *y = (int) (*y / scale_y) - viewport.y;
}

static struct point get_mouse_point(struct screen *screen) {
21 22 23
    int x;
    int y;
    SDL_GetMouseState(&x, &y);
R
Romain Vimont 已提交
24
    convert_to_renderer_coordinates(screen->renderer, &x, &y);
25 26 27 28 29 30 31 32 33 34 35 36 37 38
    SDL_assert_release(x >= 0 && x < 0x10000 && y >= 0 && y < 0x10000);
    return (struct point) {
        .x = (Uint16) x,
        .y = (Uint16) y,
    };
}

static SDL_bool is_ctrl_down(void) {
    const Uint8 *state = SDL_GetKeyboardState(NULL);
    return state[SDL_SCANCODE_LCTRL] || state[SDL_SCANCODE_RCTRL];
}

static void send_keycode(struct controller *controller, enum android_keycode keycode, const char *name) {
    // send DOWN event
R
Romain Vimont 已提交
39 40 41 42 43
    struct control_event control_event;
    control_event.type = CONTROL_EVENT_TYPE_KEYCODE;
    control_event.keycode_event.action = AKEY_EVENT_ACTION_DOWN;
    control_event.keycode_event.keycode = keycode;
    control_event.keycode_event.metastate = 0;
44 45

    if (!controller_push_event(controller, &control_event)) {
R
Romain Vimont 已提交
46
        LOGW("Cannot send %s (DOWN)", name);
47 48 49 50 51 52
        return;
    }

    // send UP event
    control_event.keycode_event.action = AKEY_EVENT_ACTION_UP;
    if (!controller_push_event(controller, &control_event)) {
R
Romain Vimont 已提交
53
        LOGW("Cannot send %s (UP)", name);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    }
}

static inline void action_home(struct controller *controller) {
    send_keycode(controller, AKEYCODE_HOME, "HOME");
}

static inline void action_back(struct controller *controller) {
    send_keycode(controller, AKEYCODE_BACK, "BACK");
}

static inline void action_app_switch(struct controller *controller) {
    send_keycode(controller, AKEYCODE_APP_SWITCH, "APP_SWITCH");
}

static inline void action_power(struct controller *controller) {
    send_keycode(controller, AKEYCODE_POWER, "POWER");
}

static inline void action_volume_up(struct controller *controller) {
    send_keycode(controller, AKEYCODE_VOLUME_UP, "VOLUME_UP");
}

static inline void action_volume_down(struct controller *controller) {
    send_keycode(controller, AKEYCODE_VOLUME_DOWN, "VOLUME_DOWN");
}

static void turn_screen_on(struct controller *controller) {
R
Romain Vimont 已提交
82 83 84 85
    struct control_event control_event;
    control_event.type = CONTROL_EVENT_TYPE_COMMAND;
    control_event.command_event.action = CONTROL_EVENT_COMMAND_SCREEN_ON;

86
    if (!controller_push_event(controller, &control_event)) {
R
Romain Vimont 已提交
87
        LOGW("Cannot turn screen on");
88 89 90
    }
}

91 92 93 94 95 96 97 98 99 100 101 102
static void switch_fps_counter_state(struct frames *frames) {
    mutex_lock(frames->mutex);
    if (frames->fps_counter.started) {
        LOGI("FPS counter stopped");
        fps_counter_stop(&frames->fps_counter);
    } else {
        LOGI("FPS counter started");
        fps_counter_start(&frames->fps_counter);
    }
    mutex_unlock(frames->mutex);
}

103 104
void input_manager_process_text_input(struct input_manager *input_manager,
                                      const SDL_TextInputEvent *event) {
105 106 107
    if (is_ctrl_down()) {
        switch (event->text[0]) {
            case '+':
108
                action_volume_up(input_manager->controller);
109 110
                break;
            case '-':
111
                action_volume_down(input_manager->controller);
112 113 114 115 116 117 118 119 120
                break;
        }
        return;
    }

    struct control_event control_event;
    control_event.type = CONTROL_EVENT_TYPE_TEXT;
    strncpy(control_event.text_event.text, event->text, TEXT_MAX_LENGTH);
    control_event.text_event.text[TEXT_MAX_LENGTH] = '\0';
121
    if (!controller_push_event(input_manager->controller, &control_event)) {
R
Romain Vimont 已提交
122
        LOGW("Cannot send text event");
123 124 125
    }
}

126 127
void input_manager_process_key(struct input_manager *input_manager,
                               const SDL_KeyboardEvent *event) {
128 129 130 131
    SDL_bool ctrl = event->keysym.mod & (KMOD_LCTRL | KMOD_RCTRL);

    // capture all Ctrl events
    if (ctrl) {
132 133
        SDL_bool repeat = event->repeat;

134 135 136 137 138
        // only consider keydown events, and ignore repeated events
        if (repeat || event->type != SDL_KEYDOWN) {
            return;
        }

139
        SDL_bool shift = event->keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT);
140 141 142 143 144
        if (shift) {
            // currently, there is no shortcut implying SHIFT
            return;
        }

145
        SDL_Keycode keycode = event->keysym.sym;
146 147
        switch (keycode) {
            case SDLK_h:
148
                action_home(input_manager->controller);
149 150 151
                return;
            case SDLK_b: // fall-through
            case SDLK_BACKSPACE:
152
                action_back(input_manager->controller);
153 154
                return;
            case SDLK_m:
155
                action_app_switch(input_manager->controller);
156 157
                return;
            case SDLK_p:
158
                action_power(input_manager->controller);
159 160
                return;
            case SDLK_f:
161
                screen_switch_fullscreen(input_manager->screen);
162 163
                return;
            case SDLK_x:
164
                screen_resize_to_fit(input_manager->screen);
165 166
                return;
            case SDLK_g:
167
                screen_resize_to_pixel_perfect(input_manager->screen);
168
                return;
169 170 171
            case SDLK_i:
                switch_fps_counter_state(input_manager->frames);
                return;
172 173 174 175 176 177 178
        }

        return;
    }

    struct control_event control_event;
    if (input_key_from_sdl_to_android(event, &control_event)) {
179
        if (!controller_push_event(input_manager->controller, &control_event)) {
R
Romain Vimont 已提交
180
            LOGW("Cannot send control event");
181 182 183 184
        }
    }
}

185 186
void input_manager_process_mouse_motion(struct input_manager *input_manager,
                                        const SDL_MouseMotionEvent *event) {
187 188 189 190 191
    if (!event->state) {
        // do not send motion events when no button is pressed
        return;
    }
    struct control_event control_event;
192 193
    if (mouse_motion_from_sdl_to_android(event, input_manager->screen->frame_size, &control_event)) {
        if (!controller_push_event(input_manager->controller, &control_event)) {
R
Romain Vimont 已提交
194
            LOGW("Cannot send mouse motion event");
195 196 197 198
        }
    }
}

199 200
void input_manager_process_mouse_button(struct input_manager *input_manager,
                                        const SDL_MouseButtonEvent *event) {
201
    if (event->button == SDL_BUTTON_RIGHT && event->type == SDL_MOUSEBUTTONDOWN) {
202
        turn_screen_on(input_manager->controller);
203 204 205
        return;
    };
    struct control_event control_event;
206 207
    if (mouse_button_from_sdl_to_android(event, input_manager->screen->frame_size, &control_event)) {
        if (!controller_push_event(input_manager->controller, &control_event)) {
R
Romain Vimont 已提交
208
            LOGW("Cannot send mouse button event");
209 210 211 212
        }
    }
}

213 214
void input_manager_process_mouse_wheel(struct input_manager *input_manager,
                                       const SDL_MouseWheelEvent *event) {
215
    struct position position = {
216
        .screen_size = input_manager->screen->frame_size,
R
Romain Vimont 已提交
217
        .point = get_mouse_point(input_manager->screen),
218 219 220
    };
    struct control_event control_event;
    if (mouse_wheel_from_sdl_to_android(event, position, &control_event)) {
221
        if (!controller_push_event(input_manager->controller, &control_event)) {
R
Romain Vimont 已提交
222
            LOGW("Cannot send wheel button event");
223 224 225
        }
    }
}