inputmanager.c 10.2 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
    SDL_assert_release(x >= 0 && x < 0x10000 && y >= 0 && y < 0x10000);
    return (struct point) {
        .x = (Uint16) x,
        .y = (Uint16) y,
    };
}

R
Romain Vimont 已提交
32 33 34 35
static const int ACTION_DOWN = 1;
static const int ACTION_UP = 1 << 1;

static void send_keycode(struct controller *controller, enum android_keycode keycode, int actions, const char *name) {
36
    // send DOWN event
R
Romain Vimont 已提交
37 38 39 40
    struct control_event control_event;
    control_event.type = CONTROL_EVENT_TYPE_KEYCODE;
    control_event.keycode_event.keycode = keycode;
    control_event.keycode_event.metastate = 0;
41

R
Romain Vimont 已提交
42 43 44 45 46 47
    if (actions & ACTION_DOWN) {
        control_event.keycode_event.action = AKEY_EVENT_ACTION_DOWN;
        if (!controller_push_event(controller, &control_event)) {
            LOGW("Cannot send %s (DOWN)", name);
            return;
        }
48 49
    }

R
Romain Vimont 已提交
50 51 52 53 54
    if (actions & ACTION_UP) {
        control_event.keycode_event.action = AKEY_EVENT_ACTION_UP;
        if (!controller_push_event(controller, &control_event)) {
            LOGW("Cannot send %s (UP)", name);
        }
55 56 57
    }
}

R
Romain Vimont 已提交
58 59
static inline void action_home(struct controller *controller, int actions) {
    send_keycode(controller, AKEYCODE_HOME, actions, "HOME");
60 61
}

R
Romain Vimont 已提交
62 63
static inline void action_back(struct controller *controller, int actions) {
    send_keycode(controller, AKEYCODE_BACK, actions, "BACK");
64 65
}

R
Romain Vimont 已提交
66 67
static inline void action_app_switch(struct controller *controller, int actions) {
    send_keycode(controller, AKEYCODE_APP_SWITCH, actions, "APP_SWITCH");
68 69
}

R
Romain Vimont 已提交
70 71
static inline void action_power(struct controller *controller, int actions) {
    send_keycode(controller, AKEYCODE_POWER, actions, "POWER");
72 73
}

R
Romain Vimont 已提交
74 75
static inline void action_volume_up(struct controller *controller, int actions) {
    send_keycode(controller, AKEYCODE_VOLUME_UP, actions, "VOLUME_UP");
76 77
}

R
Romain Vimont 已提交
78 79
static inline void action_volume_down(struct controller *controller, int actions) {
    send_keycode(controller, AKEYCODE_VOLUME_DOWN, actions, "VOLUME_DOWN");
80 81
}

R
Romain Vimont 已提交
82 83
static inline void action_menu(struct controller *controller, int actions) {
    send_keycode(controller, AKEYCODE_MENU, actions, "MENU");
84 85
}

86 87
// turn the screen on if it was off, press BACK otherwise
static void press_back_or_turn_screen_on(struct controller *controller) {
R
Romain Vimont 已提交
88 89
    struct control_event control_event;
    control_event.type = CONTROL_EVENT_TYPE_COMMAND;
90
    control_event.command_event.action = CONTROL_EVENT_COMMAND_BACK_OR_SCREEN_ON;
R
Romain Vimont 已提交
91

92
    if (!controller_push_event(controller, &control_event)) {
R
Romain Vimont 已提交
93
        LOGW("Cannot turn screen on");
94 95 96
    }
}

97 98 99 100 101 102 103 104 105 106 107 108
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);
}

R
Romain Vimont 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
static void clipboard_paste(struct controller *controller) {
    char *text = SDL_GetClipboardText();
    if (!text) {
        LOGW("Cannot get clipboard text: %s", SDL_GetError());
        return;
    }
    if (!*text) {
        // empty text
        SDL_free(text);
        return;
    }

    struct control_event control_event;
    control_event.type = CONTROL_EVENT_TYPE_TEXT;
    control_event.text_event.text = text;
    if (!controller_push_event(controller, &control_event)) {
        SDL_free(text);
        LOGW("Cannot send clipboard paste event");
    }
}

130 131
void input_manager_process_text_input(struct input_manager *input_manager,
                                      const SDL_TextInputEvent *event) {
132 133
    struct control_event control_event;
    control_event.type = CONTROL_EVENT_TYPE_TEXT;
R
Romain Vimont 已提交
134 135 136 137 138
    control_event.text_event.text = SDL_strdup(event->text);
    if (!control_event.text_event.text) {
        LOGW("Cannot strdup input text");
        return;
    }
139
    if (!controller_push_event(input_manager->controller, &control_event)) {
R
Romain Vimont 已提交
140
        LOGW("Cannot send text event");
141 142 143
    }
}

144 145
void input_manager_process_key(struct input_manager *input_manager,
                               const SDL_KeyboardEvent *event) {
146 147 148 149
    SDL_bool ctrl = event->keysym.mod & (KMOD_LCTRL | KMOD_RCTRL);

    // capture all Ctrl events
    if (ctrl) {
150
        SDL_bool shift = event->keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT);
151
        if (shift) {
R
Romain Vimont 已提交
152
            // currently, there is no shortcut involving SHIFT
153 154 155
            return;
        }

156
        SDL_Keycode keycode = event->keysym.sym;
R
Romain Vimont 已提交
157
        int action = event->type == SDL_KEYDOWN ? ACTION_DOWN : ACTION_UP;
R
Romain Vimont 已提交
158
        SDL_bool repeat = event->repeat;
159 160
        switch (keycode) {
            case SDLK_h:
R
Romain Vimont 已提交
161 162 163
                if (!repeat) {
                    action_home(input_manager->controller, action);
                }
164 165 166
                return;
            case SDLK_b: // fall-through
            case SDLK_BACKSPACE:
R
Romain Vimont 已提交
167 168 169
                if (!repeat) {
                    action_back(input_manager->controller, action);
                }
170
                return;
171
            case SDLK_s:
R
Romain Vimont 已提交
172 173 174
                if (!repeat) {
                    action_app_switch(input_manager->controller, action);
                }
175
                return;
176
            case SDLK_m:
R
Romain Vimont 已提交
177 178 179
                if (!repeat) {
                    action_menu(input_manager->controller, action);
                }
180
                return;
181
            case SDLK_p:
R
Romain Vimont 已提交
182 183 184
                if (!repeat) {
                    action_power(input_manager->controller, action);
                }
185
                return;
R
Romain Vimont 已提交
186
            case SDLK_DOWN:
R
Romain Vimont 已提交
187
                // forward repeated events
R
Romain Vimont 已提交
188
                action_volume_down(input_manager->controller, action);
R
Romain Vimont 已提交
189 190
                return;
            case SDLK_UP:
R
Romain Vimont 已提交
191
                // forward repeated events
R
Romain Vimont 已提交
192
                action_volume_up(input_manager->controller, action);
R
Romain Vimont 已提交
193
                return;
R
Romain Vimont 已提交
194
            case SDLK_v:
R
Romain Vimont 已提交
195
                if (!repeat && event->type == SDL_KEYDOWN) {
R
Romain Vimont 已提交
196 197
                    clipboard_paste(input_manager->controller);
                }
R
Romain Vimont 已提交
198
                return;
199
            case SDLK_f:
R
Romain Vimont 已提交
200
                if (!repeat && event->type == SDL_KEYDOWN) {
R
Romain Vimont 已提交
201 202
                    screen_switch_fullscreen(input_manager->screen);
                }
203 204
                return;
            case SDLK_x:
R
Romain Vimont 已提交
205
                if (!repeat && event->type == SDL_KEYDOWN) {
R
Romain Vimont 已提交
206 207
                    screen_resize_to_fit(input_manager->screen);
                }
208 209
                return;
            case SDLK_g:
R
Romain Vimont 已提交
210
                if (!repeat && event->type == SDL_KEYDOWN) {
R
Romain Vimont 已提交
211 212
                    screen_resize_to_pixel_perfect(input_manager->screen);
                }
213
                return;
214
            case SDLK_i:
R
Romain Vimont 已提交
215
                if (!repeat && event->type == SDL_KEYDOWN) {
R
Romain Vimont 已提交
216 217
                    switch_fps_counter_state(input_manager->frames);
                }
218
                return;
219 220 221 222 223 224 225
        }

        return;
    }

    struct control_event control_event;
    if (input_key_from_sdl_to_android(event, &control_event)) {
226
        if (!controller_push_event(input_manager->controller, &control_event)) {
R
Romain Vimont 已提交
227
            LOGW("Cannot send control event");
228 229 230 231
        }
    }
}

232 233
void input_manager_process_mouse_motion(struct input_manager *input_manager,
                                        const SDL_MouseMotionEvent *event) {
234 235 236 237 238
    if (!event->state) {
        // do not send motion events when no button is pressed
        return;
    }
    struct control_event control_event;
239 240
    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 已提交
241
            LOGW("Cannot send mouse motion event");
242 243 244 245
        }
    }
}

246 247
void input_manager_process_mouse_button(struct input_manager *input_manager,
                                        const SDL_MouseButtonEvent *event) {
R
Romain Vimont 已提交
248 249 250 251 252 253
    if (event->type == SDL_MOUSEBUTTONDOWN) {
        if (event->button == SDL_BUTTON_RIGHT) {
            press_back_or_turn_screen_on(input_manager->controller);
            return;
        }
        if (event->button == SDL_BUTTON_MIDDLE) {
R
Romain Vimont 已提交
254
            action_home(input_manager->controller, ACTION_DOWN | ACTION_UP);
R
Romain Vimont 已提交
255 256
            return;
        }
257 258 259 260 261
        // double-click on black borders resize to fit the device screen
        if (event->button == SDL_BUTTON_LEFT && event->clicks == 2) {
            SDL_bool outside_device_screen =
                    event->x < 0 || event->x >= input_manager->screen->frame_size.width ||
                    event->y < 0 || event->y >= input_manager->screen->frame_size.height;
R
Romain Vimont 已提交
262 263 264 265 266
            if (outside_device_screen) {
                screen_resize_to_fit(input_manager->screen);
                return;
            }
            // otherwise, send the click event to the device
267
        }
268 269
    };
    struct control_event control_event;
270 271
    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 已提交
272
            LOGW("Cannot send mouse button event");
273 274 275 276
        }
    }
}

277 278
void input_manager_process_mouse_wheel(struct input_manager *input_manager,
                                       const SDL_MouseWheelEvent *event) {
279
    struct position position = {
280
        .screen_size = input_manager->screen->frame_size,
R
Romain Vimont 已提交
281
        .point = get_mouse_point(input_manager->screen),
282 283 284
    };
    struct control_event control_event;
    if (mouse_wheel_from_sdl_to_android(event, position, &control_event)) {
285
        if (!controller_push_event(input_manager->controller, &control_event)) {
R
Romain Vimont 已提交
286
            LOGW("Cannot send mouse wheel event");
287 288 289
        }
    }
}