2017-03-16 18:14:12 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +00:00
|
|
|
* This file is part of the Trezor project, https://trezor.io/
|
2017-03-16 18:14:12 +00:00
|
|
|
*
|
2018-02-26 13:06:10 +00:00
|
|
|
* Copyright (c) SatoshiLabs
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-03-16 18:14:12 +00:00
|
|
|
*/
|
|
|
|
|
2022-03-28 14:44:52 +00:00
|
|
|
#include <SDL.h>
|
2021-09-21 18:09:27 +00:00
|
|
|
#include <stdbool.h>
|
2019-05-15 17:39:11 +00:00
|
|
|
#include <stdint.h>
|
2017-03-16 18:14:12 +00:00
|
|
|
|
2022-04-26 11:47:40 +00:00
|
|
|
#if defined TREZOR_MODEL_T
|
2021-09-21 18:09:27 +00:00
|
|
|
|
2023-03-23 14:42:21 +00:00
|
|
|
#include "common.h"
|
2017-12-14 23:06:09 +00:00
|
|
|
#include "touch.h"
|
2017-03-16 18:14:12 +00:00
|
|
|
|
2018-02-26 18:44:29 +00:00
|
|
|
extern int sdl_display_res_x, sdl_display_res_y;
|
|
|
|
extern int sdl_touch_offset_x, sdl_touch_offset_y;
|
|
|
|
|
2023-03-23 14:42:21 +00:00
|
|
|
static bool _touch_detected = false;
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
uint32_t touch_read(void) {
|
2023-03-23 14:42:21 +00:00
|
|
|
emulator_poll_events();
|
2019-03-29 15:26:02 +00:00
|
|
|
SDL_Event event;
|
|
|
|
SDL_PumpEvents();
|
|
|
|
if (SDL_PollEvent(&event) > 0) {
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
case SDL_MOUSEBUTTONUP: {
|
|
|
|
const int x = event.button.x - sdl_touch_offset_x;
|
|
|
|
const int y = event.button.y - sdl_touch_offset_y;
|
|
|
|
if (x < 0 || y < 0 || x >= sdl_display_res_x ||
|
|
|
|
y >= sdl_display_res_y) {
|
|
|
|
if (event.motion.state) {
|
|
|
|
const int clamp_x =
|
|
|
|
(x < 0)
|
|
|
|
? 0
|
|
|
|
: ((x >= sdl_display_res_x) ? sdl_display_res_x - 1 : x);
|
|
|
|
const int clamp_y =
|
|
|
|
(y < 0)
|
|
|
|
? 0
|
|
|
|
: ((y >= sdl_display_res_y) ? sdl_display_res_y - 1 : y);
|
|
|
|
return TOUCH_END | touch_pack_xy(clamp_x, clamp_y);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
2023-03-23 14:42:21 +00:00
|
|
|
_touch_detected = true;
|
2019-03-29 15:26:02 +00:00
|
|
|
return TOUCH_START | touch_pack_xy(x, y);
|
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
// remove other SDL_MOUSEMOTION events from queue
|
|
|
|
SDL_FlushEvent(SDL_MOUSEMOTION);
|
|
|
|
if (event.motion.state) {
|
|
|
|
return TOUCH_MOVE | touch_pack_xy(x, y);
|
2018-09-20 13:09:01 +00:00
|
|
|
}
|
2019-03-29 15:26:02 +00:00
|
|
|
break;
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
2023-03-23 14:42:21 +00:00
|
|
|
_touch_detected = false;
|
2019-03-29 15:26:02 +00:00
|
|
|
return TOUCH_END | touch_pack_xy(x, y);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-09-21 18:09:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-23 14:42:21 +00:00
|
|
|
void touch_init(void) {}
|
|
|
|
void touch_power_on(void) {}
|
|
|
|
|
|
|
|
uint32_t touch_is_detected(void) { return _touch_detected; }
|
|
|
|
|
2022-05-01 12:05:32 +00:00
|
|
|
#elif defined TREZOR_MODEL_1 || defined TREZOR_MODEL_R
|
2021-09-21 18:09:27 +00:00
|
|
|
|
|
|
|
#include "button.h"
|
|
|
|
|
|
|
|
uint32_t button_read(void) {
|
|
|
|
SDL_Event event;
|
|
|
|
SDL_PumpEvents();
|
|
|
|
if (SDL_PollEvent(&event) > 0) {
|
|
|
|
switch (event.type) {
|
2019-03-29 15:26:02 +00:00
|
|
|
case SDL_KEYDOWN:
|
|
|
|
if (event.key.repeat) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (event.key.keysym.sym) {
|
|
|
|
case SDLK_LEFT:
|
2021-09-21 18:09:27 +00:00
|
|
|
return BTN_EVT_DOWN | BTN_LEFT;
|
2019-03-29 15:26:02 +00:00
|
|
|
case SDLK_RIGHT:
|
2021-09-21 18:09:27 +00:00
|
|
|
return BTN_EVT_DOWN | BTN_RIGHT;
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_KEYUP:
|
|
|
|
if (event.key.repeat) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (event.key.keysym.sym) {
|
|
|
|
case SDLK_LEFT:
|
2021-09-21 18:09:27 +00:00
|
|
|
return BTN_EVT_UP | BTN_LEFT;
|
2019-03-29 15:26:02 +00:00
|
|
|
case SDLK_RIGHT:
|
2021-09-21 18:09:27 +00:00
|
|
|
return BTN_EVT_UP | BTN_RIGHT;
|
2017-03-16 18:14:12 +00:00
|
|
|
}
|
2019-03-29 15:26:02 +00:00
|
|
|
break;
|
2017-03-16 18:14:12 +00:00
|
|
|
}
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2017-03-16 18:14:12 +00:00
|
|
|
}
|
2021-09-21 18:09:27 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
#error Unknown Trezor model
|
|
|
|
#endif
|