2017-03-16 18:14:12 +00:00
|
|
|
/*
|
2018-02-26 13:06:10 +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
|
|
|
*/
|
|
|
|
|
2017-03-16 18:40:20 +00:00
|
|
|
#include <stdint.h>
|
2018-09-03 15:56:53 +00:00
|
|
|
#ifndef TREZOR_EMULATOR_NOUI
|
2017-03-16 18:14:12 +00:00
|
|
|
#include <SDL2/SDL.h>
|
2017-03-16 18:40:20 +00:00
|
|
|
#endif
|
2017-03-16 18:14:12 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
extern void __shutdown(void);
|
2018-07-02 14:24:42 +00:00
|
|
|
extern void display_save(const char *prefix);
|
2017-12-07 14:31:23 +00:00
|
|
|
|
2017-03-16 18:14:12 +00:00
|
|
|
uint32_t touch_read(void)
|
|
|
|
{
|
2018-09-03 15:56:53 +00:00
|
|
|
#ifndef TREZOR_EMULATOR_NOUI
|
2017-03-16 18:14:12 +00:00
|
|
|
SDL_Event event;
|
|
|
|
int x, y;
|
|
|
|
SDL_PumpEvents();
|
|
|
|
if (SDL_PollEvent(&event) > 0) {
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
2018-02-26 18:44:29 +00:00
|
|
|
x = event.button.x - sdl_touch_offset_x;
|
|
|
|
y = event.button.y - sdl_touch_offset_y;
|
2018-03-01 13:59:36 +00:00
|
|
|
if (x < 0 || y < 0 || x >= sdl_display_res_x || y >= sdl_display_res_y) {
|
|
|
|
if (event.motion.state) {
|
|
|
|
int clamp_x = (x < 0) ? 0 : ((x >= sdl_display_res_x) ? sdl_display_res_x - 1 : x);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2017-03-16 18:14:12 +00:00
|
|
|
switch (event.type) {
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
2018-01-24 12:41:02 +00:00
|
|
|
return TOUCH_START | touch_pack_xy(x, y);
|
2017-03-16 18:14:12 +00:00
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
// remove other SDL_MOUSEMOTION events from queue
|
|
|
|
SDL_FlushEvent(SDL_MOUSEMOTION);
|
|
|
|
if (event.motion.state) {
|
2018-01-24 12:41:02 +00:00
|
|
|
return TOUCH_MOVE | touch_pack_xy(x, y);
|
2017-03-16 18:14:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
2018-01-24 12:41:02 +00:00
|
|
|
return TOUCH_END | touch_pack_xy(x, y);
|
2017-03-16 18:14:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_KEYUP:
|
2018-08-31 13:53:36 +00:00
|
|
|
switch (event.key.keysym.sym) {
|
|
|
|
case SDLK_ESCAPE:
|
|
|
|
__shutdown();
|
|
|
|
break;
|
|
|
|
case SDLK_p:
|
|
|
|
display_save("emu");
|
|
|
|
break;
|
|
|
|
case SDLK_RIGHT:
|
|
|
|
return TOUCH_CONFIRM;
|
|
|
|
case SDLK_LEFT:
|
|
|
|
return TOUCH_CANCEL;
|
2018-07-02 14:24:42 +00:00
|
|
|
}
|
2017-03-16 18:14:12 +00:00
|
|
|
break;
|
|
|
|
case SDL_QUIT:
|
2017-12-07 14:31:23 +00:00
|
|
|
__shutdown();
|
2017-03-16 18:14:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-16 18:40:20 +00:00
|
|
|
#endif
|
2017-03-16 18:14:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|