2016-03-27 21:10:31 +00:00
|
|
|
/*
|
2018-02-26 13:06:10 +00:00
|
|
|
* This file is part of the TREZOR project, https://trezor.io/
|
2016-03-27 21:10:31 +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/>.
|
2016-03-27 21:10:31 +00:00
|
|
|
*/
|
|
|
|
|
2016-04-28 19:43:22 +00:00
|
|
|
#include <stdlib.h>
|
2018-09-03 15:56:53 +00:00
|
|
|
#ifndef TREZOR_EMULATOR_NOUI
|
2016-10-03 17:56:45 +00:00
|
|
|
#include <SDL2/SDL.h>
|
2017-03-17 13:14:00 +00:00
|
|
|
#include <SDL2/SDL_image.h>
|
2016-03-27 21:10:31 +00:00
|
|
|
|
2018-09-11 14:38:47 +00:00
|
|
|
#define EMULATOR_BORDER 16
|
2018-02-26 18:44:29 +00:00
|
|
|
|
2018-09-11 14:38:47 +00:00
|
|
|
#if TREZOR_MODEL == T
|
2018-09-12 13:23:30 +00:00
|
|
|
|
2018-11-06 18:29:34 +00:00
|
|
|
#ifdef TREZOR_EMULATOR_RASPI
|
|
|
|
#define WINDOW_WIDTH 480
|
|
|
|
#define WINDOW_HEIGHT 320
|
|
|
|
#define TOUCH_OFFSET_X 110
|
|
|
|
#define TOUCH_OFFSET_Y 40
|
|
|
|
#else
|
2018-09-11 14:38:47 +00:00
|
|
|
#define WINDOW_WIDTH 400
|
|
|
|
#define WINDOW_HEIGHT 600
|
|
|
|
#define TOUCH_OFFSET_X 80
|
|
|
|
#define TOUCH_OFFSET_Y 110
|
2018-11-06 18:29:34 +00:00
|
|
|
#endif
|
2018-09-12 13:23:30 +00:00
|
|
|
|
2018-09-11 14:38:47 +00:00
|
|
|
#elif TREZOR_MODEL == 1
|
2018-09-12 13:23:30 +00:00
|
|
|
|
2018-09-11 14:38:47 +00:00
|
|
|
#define WINDOW_WIDTH 200
|
|
|
|
#define WINDOW_HEIGHT 340
|
|
|
|
#define TOUCH_OFFSET_X 36
|
|
|
|
#define TOUCH_OFFSET_Y 92
|
2018-09-12 13:23:30 +00:00
|
|
|
|
2018-09-11 14:38:47 +00:00
|
|
|
#else
|
2018-09-12 13:23:30 +00:00
|
|
|
#error Unknown TREZOR Model
|
2018-09-11 14:38:47 +00:00
|
|
|
#endif
|
2018-02-26 18:44:29 +00:00
|
|
|
|
2017-10-02 14:48:24 +00:00
|
|
|
static SDL_Renderer *RENDERER;
|
|
|
|
static SDL_Surface *BUFFER;
|
2018-02-25 01:09:52 +00:00
|
|
|
static SDL_Texture *TEXTURE, *BACKGROUND;
|
2018-02-18 14:50:15 +00:00
|
|
|
|
2018-10-02 15:24:03 +00:00
|
|
|
int sdl_display_res_x = DISPLAY_RESX, sdl_display_res_y = DISPLAY_RESY;
|
2018-02-26 18:44:29 +00:00
|
|
|
int sdl_touch_offset_x, sdl_touch_offset_y;
|
|
|
|
|
2018-02-18 14:50:15 +00:00
|
|
|
static struct {
|
|
|
|
struct {
|
|
|
|
uint16_t x, y;
|
|
|
|
} start;
|
|
|
|
struct {
|
|
|
|
uint16_t x, y;
|
|
|
|
} end;
|
|
|
|
struct {
|
|
|
|
uint16_t x, y;
|
|
|
|
} pos;
|
|
|
|
} PIXELWINDOW;
|
2016-03-27 21:10:31 +00:00
|
|
|
|
2018-02-15 16:57:30 +00:00
|
|
|
void PIXELDATA(uint16_t c) {
|
2018-09-13 17:41:52 +00:00
|
|
|
#if TREZOR_MODEL == 1
|
|
|
|
// set to white if highest bits of all R, G, B values are set to 1
|
|
|
|
// bin(10000 100000 10000) = hex(0x8410)
|
|
|
|
// otherwise set to black
|
|
|
|
c = (c & 0x8410) ? 0xFFFF : 0x0000;
|
|
|
|
#endif
|
2017-10-02 14:22:27 +00:00
|
|
|
if (!RENDERER) {
|
|
|
|
display_init();
|
|
|
|
}
|
2018-02-18 14:50:15 +00:00
|
|
|
if (PIXELWINDOW.pos.x <= PIXELWINDOW.end.x && PIXELWINDOW.pos.y <= PIXELWINDOW.end.y) {
|
|
|
|
((uint16_t *)BUFFER->pixels)[PIXELWINDOW.pos.x + PIXELWINDOW.pos.y * BUFFER->pitch / sizeof(uint16_t)] = c;
|
2016-03-27 21:10:31 +00:00
|
|
|
}
|
2018-02-18 14:50:15 +00:00
|
|
|
PIXELWINDOW.pos.x++;
|
|
|
|
if (PIXELWINDOW.pos.x > PIXELWINDOW.end.x) {
|
|
|
|
PIXELWINDOW.pos.x = PIXELWINDOW.start.x;
|
|
|
|
PIXELWINDOW.pos.y++;
|
2016-03-27 21:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-17 13:14:00 +00:00
|
|
|
#else
|
2018-02-15 16:57:30 +00:00
|
|
|
#define PIXELDATA(X) (void)(X)
|
2017-03-17 13:14:00 +00:00
|
|
|
#endif
|
2016-03-27 21:10:31 +00:00
|
|
|
|
2017-10-20 13:25:24 +00:00
|
|
|
void display_init(void)
|
2016-03-27 21:10:31 +00:00
|
|
|
{
|
2018-09-03 15:56:53 +00:00
|
|
|
#ifndef TREZOR_EMULATOR_NOUI
|
2016-03-27 21:10:31 +00:00
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
2017-12-07 11:44:38 +00:00
|
|
|
printf("%s\n", SDL_GetError());
|
|
|
|
ensure(secfalse, "SDL_Init error");
|
2016-03-27 21:10:31 +00:00
|
|
|
}
|
2017-12-07 14:31:23 +00:00
|
|
|
atexit(SDL_Quit);
|
2018-11-06 18:29:34 +00:00
|
|
|
SDL_Window *win = SDL_CreateWindow("TREZOR Emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT,
|
|
|
|
#ifdef TREZOR_EMULATOR_RASPI
|
|
|
|
SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN
|
|
|
|
#else
|
|
|
|
SDL_WINDOW_SHOWN
|
|
|
|
#endif
|
|
|
|
);
|
2016-03-27 21:10:31 +00:00
|
|
|
if (!win) {
|
2017-12-07 11:44:38 +00:00
|
|
|
printf("%s\n", SDL_GetError());
|
|
|
|
ensure(secfalse, "SDL_CreateWindow error");
|
2016-03-27 21:10:31 +00:00
|
|
|
}
|
2018-09-11 16:08:40 +00:00
|
|
|
RENDERER = SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE);
|
2016-03-27 21:10:31 +00:00
|
|
|
if (!RENDERER) {
|
2017-12-07 11:44:38 +00:00
|
|
|
printf("%s\n", SDL_GetError());
|
2016-03-27 21:10:31 +00:00
|
|
|
SDL_DestroyWindow(win);
|
2017-12-07 11:44:38 +00:00
|
|
|
ensure(secfalse, "SDL_CreateRenderer error");
|
2016-03-27 21:10:31 +00:00
|
|
|
}
|
2018-02-24 23:55:33 +00:00
|
|
|
SDL_SetRenderDrawColor(RENDERER, 0, 0, 0, 255);
|
2016-03-27 21:10:31 +00:00
|
|
|
SDL_RenderClear(RENDERER);
|
2017-10-02 14:47:45 +00:00
|
|
|
BUFFER = SDL_CreateRGBSurface(0, MAX_DISPLAY_RESX, MAX_DISPLAY_RESY, 16, 0xF800, 0x07E0, 0x001F, 0x0000);
|
2016-09-24 14:53:43 +00:00
|
|
|
TEXTURE = SDL_CreateTexture(RENDERER, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, DISPLAY_RESX, DISPLAY_RESY);
|
2018-02-24 23:55:33 +00:00
|
|
|
SDL_SetTextureBlendMode(TEXTURE, SDL_BLENDMODE_BLEND);
|
2018-02-25 01:09:52 +00:00
|
|
|
// TODO: find better way how to embed/distribute background image
|
2018-11-06 18:29:34 +00:00
|
|
|
#ifdef TREZOR_EMULATOR_RASPI
|
|
|
|
BACKGROUND = IMG_LoadTexture(RENDERER, "../embed/unix/background_raspi.jpg");
|
|
|
|
#else
|
2018-09-11 13:59:05 +00:00
|
|
|
BACKGROUND = IMG_LoadTexture(RENDERER, "../embed/unix/background_" XSTR(TREZOR_MODEL) ".jpg");
|
2018-11-06 18:29:34 +00:00
|
|
|
#endif
|
2018-02-25 01:09:52 +00:00
|
|
|
if (BACKGROUND) {
|
|
|
|
SDL_SetTextureBlendMode(BACKGROUND, SDL_BLENDMODE_NONE);
|
2018-09-11 14:38:47 +00:00
|
|
|
sdl_touch_offset_x = TOUCH_OFFSET_X;
|
|
|
|
sdl_touch_offset_y = TOUCH_OFFSET_Y;
|
2018-02-26 18:44:29 +00:00
|
|
|
} else {
|
2018-09-11 14:38:47 +00:00
|
|
|
SDL_SetWindowSize(win, DISPLAY_RESX + 2 * EMULATOR_BORDER, DISPLAY_RESY + 2 * EMULATOR_BORDER);
|
|
|
|
sdl_touch_offset_x = EMULATOR_BORDER;
|
|
|
|
sdl_touch_offset_y = EMULATOR_BORDER;
|
2018-02-25 01:09:52 +00:00
|
|
|
}
|
2017-10-02 14:29:21 +00:00
|
|
|
DISPLAY_BACKLIGHT = 0;
|
2018-11-06 18:29:34 +00:00
|
|
|
#ifdef TREZOR_EMULATOR_RASPI
|
|
|
|
DISPLAY_ORIENTATION = 270;
|
|
|
|
SDL_ShowCursor(SDL_DISABLE);
|
|
|
|
#else
|
2017-10-02 14:29:21 +00:00
|
|
|
DISPLAY_ORIENTATION = 0;
|
2017-03-17 13:14:00 +00:00
|
|
|
#endif
|
2018-11-06 18:29:34 +00:00
|
|
|
#endif
|
2016-03-27 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 19:26:54 +00:00
|
|
|
static void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
|
2016-05-11 19:05:08 +00:00
|
|
|
{
|
2018-09-03 15:56:53 +00:00
|
|
|
#ifndef TREZOR_EMULATOR_NOUI
|
2017-10-02 14:22:27 +00:00
|
|
|
if (!RENDERER) {
|
|
|
|
display_init();
|
|
|
|
}
|
2018-02-18 14:50:15 +00:00
|
|
|
PIXELWINDOW.start.x = x0; PIXELWINDOW.start.y = y0;
|
|
|
|
PIXELWINDOW.end.x = x1; PIXELWINDOW.end.y = y1;
|
|
|
|
PIXELWINDOW.pos.x = x0; PIXELWINDOW.pos.y = y0;
|
2017-03-17 13:14:00 +00:00
|
|
|
#endif
|
2016-03-27 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 09:52:19 +00:00
|
|
|
void display_refresh(void)
|
2016-03-27 21:10:31 +00:00
|
|
|
{
|
2018-09-03 15:56:53 +00:00
|
|
|
#ifndef TREZOR_EMULATOR_NOUI
|
2017-10-02 14:22:27 +00:00
|
|
|
if (!RENDERER) {
|
|
|
|
display_init();
|
|
|
|
}
|
2018-02-25 01:09:52 +00:00
|
|
|
if (BACKGROUND) {
|
|
|
|
SDL_RenderCopy(RENDERER, BACKGROUND, NULL, NULL);
|
|
|
|
} else {
|
|
|
|
SDL_RenderClear(RENDERER);
|
|
|
|
}
|
2016-10-05 22:02:46 +00:00
|
|
|
SDL_UpdateTexture(TEXTURE, NULL, BUFFER->pixels, BUFFER->pitch);
|
2018-02-24 23:55:33 +00:00
|
|
|
#define BACKLIGHT_NORMAL 150
|
|
|
|
SDL_SetTextureAlphaMod(TEXTURE, MIN(255, 255 * DISPLAY_BACKLIGHT / BACKLIGHT_NORMAL));
|
2018-02-26 18:44:29 +00:00
|
|
|
if (BACKGROUND) {
|
2018-09-11 14:38:47 +00:00
|
|
|
const SDL_Rect r = {TOUCH_OFFSET_X, TOUCH_OFFSET_Y, DISPLAY_RESX, DISPLAY_RESY};
|
2018-02-26 18:44:29 +00:00
|
|
|
SDL_RenderCopyEx(RENDERER, TEXTURE, NULL, &r, DISPLAY_ORIENTATION, NULL, 0);
|
|
|
|
} else {
|
2018-09-11 14:38:47 +00:00
|
|
|
const SDL_Rect r = {EMULATOR_BORDER, EMULATOR_BORDER, DISPLAY_RESX, DISPLAY_RESY};
|
2018-02-26 18:44:29 +00:00
|
|
|
SDL_RenderCopyEx(RENDERER, TEXTURE, NULL, &r, DISPLAY_ORIENTATION, NULL, 0);
|
|
|
|
}
|
2016-03-27 21:10:31 +00:00
|
|
|
SDL_RenderPresent(RENDERER);
|
2017-03-17 13:14:00 +00:00
|
|
|
#endif
|
2016-03-27 21:10:31 +00:00
|
|
|
}
|
2016-03-29 13:31:41 +00:00
|
|
|
|
2017-03-08 16:36:11 +00:00
|
|
|
static void display_set_orientation(int degrees)
|
2016-03-29 13:31:41 +00:00
|
|
|
{
|
2018-02-24 23:55:33 +00:00
|
|
|
display_refresh();
|
2016-04-03 13:26:49 +00:00
|
|
|
}
|
2016-10-05 22:02:46 +00:00
|
|
|
|
2017-03-08 16:36:11 +00:00
|
|
|
static void display_set_backlight(int val)
|
2016-10-05 22:02:46 +00:00
|
|
|
{
|
2018-02-24 23:55:33 +00:00
|
|
|
display_refresh();
|
2017-03-17 13:14:00 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 18:05:40 +00:00
|
|
|
void display_save(const char *prefix)
|
2017-03-17 13:14:00 +00:00
|
|
|
{
|
2018-09-03 15:56:53 +00:00
|
|
|
#ifndef TREZOR_EMULATOR_NOUI
|
2017-10-02 14:22:27 +00:00
|
|
|
if (!RENDERER) {
|
|
|
|
display_init();
|
|
|
|
}
|
2018-02-27 18:05:40 +00:00
|
|
|
static uint32_t cnt = 0;
|
|
|
|
char fname[256];
|
|
|
|
snprintf(fname, sizeof(fname), "%s%08d.png", prefix, cnt);
|
2018-07-02 15:10:18 +00:00
|
|
|
const SDL_Rect rect = {0, 0, DISPLAY_RESX, DISPLAY_RESY};
|
|
|
|
SDL_Surface *crop = SDL_CreateRGBSurface(BUFFER->flags, rect.w, rect.h, BUFFER->format->BitsPerPixel, BUFFER->format->Rmask, BUFFER->format->Gmask, BUFFER->format->Bmask, BUFFER->format->Amask);
|
|
|
|
SDL_BlitSurface(BUFFER, &rect, crop, NULL);
|
|
|
|
IMG_SavePNG(crop, fname);
|
|
|
|
SDL_FreeSurface(crop);
|
|
|
|
fprintf(stderr, "Saved screenshot to %s\n", fname);
|
2018-02-27 18:05:40 +00:00
|
|
|
cnt++;
|
2017-03-17 13:14:00 +00:00
|
|
|
#endif
|
2016-10-05 22:02:46 +00:00
|
|
|
}
|