2016-03-27 21:10:31 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +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
|
|
|
*/
|
2022-08-16 14:51:10 +00:00
|
|
|
#define _GNU_SOURCE
|
2016-03-27 21:10:31 +00:00
|
|
|
|
2022-03-28 14:44:52 +00:00
|
|
|
#include <SDL.h>
|
|
|
|
#include <SDL_image.h>
|
2023-03-20 18:22:07 +00:00
|
|
|
#include <math.h>
|
2022-08-16 14:51:10 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
2019-05-20 13:10:47 +00:00
|
|
|
#include <stdio.h>
|
2019-05-15 17:39:11 +00:00
|
|
|
#include <stdlib.h>
|
2022-08-16 14:51:10 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
2022-10-14 10:43:37 +00:00
|
|
|
#include "display-unix.h"
|
2022-08-16 14:51:10 +00:00
|
|
|
#include "display_interface.h"
|
2019-05-20 13:10:47 +00:00
|
|
|
#include "profile.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
|
|
|
|
2022-04-26 11:47:40 +00:00
|
|
|
#if defined TREZOR_MODEL_T
|
2018-09-12 13:23:30 +00:00
|
|
|
|
2018-11-06 18:29:34 +00:00
|
|
|
#ifdef TREZOR_EMULATOR_RASPI
|
2019-03-29 15:26:02 +00:00
|
|
|
#define WINDOW_WIDTH 480
|
|
|
|
#define WINDOW_HEIGHT 320
|
|
|
|
#define TOUCH_OFFSET_X 110
|
|
|
|
#define TOUCH_OFFSET_Y 40
|
2018-11-06 18:29:34 +00:00
|
|
|
#else
|
2019-03-29 15:26:02 +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
|
|
|
|
2022-05-01 12:05:32 +00:00
|
|
|
#elif defined TREZOR_MODEL_1 || defined TREZOR_MODEL_R
|
2018-09-12 13:23:30 +00:00
|
|
|
|
2019-03-29 15:26:02 +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
|
2020-06-06 17:26:47 +00:00
|
|
|
#error Unknown Trezor model
|
2018-09-11 14:38:47 +00:00
|
|
|
#endif
|
2018-02-26 18:44:29 +00:00
|
|
|
|
2022-01-10 13:13:36 +00:00
|
|
|
static SDL_Window *WINDOW;
|
2017-10-02 14:48:24 +00:00
|
|
|
static SDL_Renderer *RENDERER;
|
2023-03-23 14:38:11 +00:00
|
|
|
// BUFFER_TO_DISPLAY will contain the actual pixels to be displayed,
|
|
|
|
// it will be filled from BUFFER by gamma-correcting the pixel colors.
|
|
|
|
// Screenshots will be taken with data from BUFFER.
|
2017-10-02 14:48:24 +00:00
|
|
|
static SDL_Surface *BUFFER;
|
2023-03-23 14:38:11 +00:00
|
|
|
static SDL_Surface *BUFFER_TO_DISPLAY;
|
2018-02-25 01:09:52 +00:00
|
|
|
static SDL_Texture *TEXTURE, *BACKGROUND;
|
2018-02-18 14:50:15 +00:00
|
|
|
|
2019-12-09 16:01:04 +00:00
|
|
|
static SDL_Surface *PREV_SAVED;
|
|
|
|
|
2022-08-16 14:51:10 +00:00
|
|
|
static int DISPLAY_BACKLIGHT = -1;
|
|
|
|
static int DISPLAY_ORIENTATION = -1;
|
2023-03-20 18:23:12 +00:00
|
|
|
float DISPLAY_GAMMA = 0.55f;
|
2023-03-23 14:38:11 +00:00
|
|
|
// Will depend on SDL_VIDEODRIVER env variable
|
|
|
|
static bool DO_GAMMA_CORRECTION = true;
|
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;
|
|
|
|
|
2023-03-23 14:38:11 +00:00
|
|
|
// Using RGB565 (16-bit) color format.
|
|
|
|
typedef uint16_t pixel_color;
|
|
|
|
|
2022-08-16 14:51:10 +00:00
|
|
|
// this is just for compatibility with DMA2D using algorithms
|
|
|
|
uint8_t *const DISPLAY_DATA_ADDRESS = 0;
|
|
|
|
|
2018-02-18 14:50:15 +00:00
|
|
|
static struct {
|
2019-03-29 15:26:02 +00:00
|
|
|
struct {
|
|
|
|
uint16_t x, y;
|
|
|
|
} start;
|
|
|
|
struct {
|
|
|
|
uint16_t x, y;
|
|
|
|
} end;
|
|
|
|
struct {
|
|
|
|
uint16_t x, y;
|
|
|
|
} pos;
|
2018-02-18 14:50:15 +00:00
|
|
|
} PIXELWINDOW;
|
2016-03-27 21:10:31 +00:00
|
|
|
|
2023-03-23 14:38:11 +00:00
|
|
|
pixel_color gamma_correct(pixel_color c) {
|
2023-03-20 18:22:07 +00:00
|
|
|
// NOTE: 0x1f/31 and 0x3f/63 are maximum values of RGB components
|
|
|
|
// given the color is 16-bit (5 bits for R, 6 bits for G, 5 bits for B).
|
|
|
|
int r = (c >> 11) & 0x1f;
|
|
|
|
int g = (c >> 5) & 0x3f;
|
|
|
|
int b = c & 0x1f;
|
|
|
|
|
2023-03-23 14:38:11 +00:00
|
|
|
r = (int)round(pow(r / 31.0, DISPLAY_GAMMA) * 31.0);
|
|
|
|
g = (int)round(pow(g / 63.0, DISPLAY_GAMMA) * 63.0);
|
|
|
|
b = (int)round(pow(b / 31.0, DISPLAY_GAMMA) * 31.0);
|
2023-03-20 18:22:07 +00:00
|
|
|
|
|
|
|
return (r << 11) | (g << 5) | b;
|
|
|
|
}
|
|
|
|
|
2023-03-23 14:38:11 +00:00
|
|
|
void gamma_correct_buffer_to_display(void) {
|
|
|
|
// Gamma correct all the pixels in BUFFER_TO_DISPLAY.
|
|
|
|
pixel_color *pixels = (pixel_color *)BUFFER_TO_DISPLAY->pixels;
|
|
|
|
for (int y = 0; y < BUFFER_TO_DISPLAY->h; y++) {
|
|
|
|
for (int x = 0; x < BUFFER_TO_DISPLAY->w; x++) {
|
|
|
|
int index = y * BUFFER_TO_DISPLAY->pitch / 2 + x;
|
|
|
|
pixels[index] = gamma_correct(pixels[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void display_pixeldata(pixel_color c) {
|
2022-05-01 12:05:32 +00:00
|
|
|
#if defined TREZOR_MODEL_1 || defined TREZOR_MODEL_R
|
2019-03-29 15:26:02 +00:00
|
|
|
// 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;
|
2018-09-13 17:41:52 +00:00
|
|
|
#endif
|
2019-03-29 15:26:02 +00:00
|
|
|
if (!RENDERER) {
|
|
|
|
display_init();
|
|
|
|
}
|
|
|
|
if (PIXELWINDOW.pos.x <= PIXELWINDOW.end.x &&
|
|
|
|
PIXELWINDOW.pos.y <= PIXELWINDOW.end.y) {
|
2023-03-23 14:38:11 +00:00
|
|
|
((pixel_color *)
|
2019-03-29 15:26:02 +00:00
|
|
|
BUFFER->pixels)[PIXELWINDOW.pos.x + PIXELWINDOW.pos.y * BUFFER->pitch /
|
2023-03-23 14:38:11 +00:00
|
|
|
sizeof(pixel_color)] = c;
|
2019-03-29 15:26:02 +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
|
|
|
}
|
|
|
|
|
2022-06-01 09:49:57 +00:00
|
|
|
#define PIXELDATA(c) display_pixeldata(c)
|
|
|
|
|
2022-08-16 14:51:10 +00:00
|
|
|
void display_reset_state() {}
|
2022-04-29 08:42:40 +00:00
|
|
|
|
2021-03-04 17:14:22 +00:00
|
|
|
void display_init_seq(void) {}
|
2021-02-25 17:58:56 +00:00
|
|
|
|
2022-01-10 13:13:36 +00:00
|
|
|
void display_deinit(void) {
|
|
|
|
SDL_FreeSurface(PREV_SAVED);
|
|
|
|
SDL_FreeSurface(BUFFER);
|
2023-03-23 14:38:11 +00:00
|
|
|
SDL_FreeSurface(BUFFER_TO_DISPLAY);
|
2022-01-10 13:13:36 +00:00
|
|
|
if (BACKGROUND != NULL) {
|
|
|
|
SDL_DestroyTexture(BACKGROUND);
|
|
|
|
}
|
|
|
|
if (TEXTURE != NULL) {
|
|
|
|
SDL_DestroyTexture(TEXTURE);
|
|
|
|
}
|
|
|
|
if (RENDERER != NULL) {
|
|
|
|
SDL_DestroyRenderer(RENDERER);
|
|
|
|
}
|
|
|
|
if (WINDOW != NULL) {
|
|
|
|
SDL_DestroyWindow(WINDOW);
|
|
|
|
}
|
|
|
|
SDL_Quit();
|
|
|
|
}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
void display_init(void) {
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
|
|
|
printf("%s\n", SDL_GetError());
|
|
|
|
ensure(secfalse, "SDL_Init error");
|
|
|
|
}
|
2022-01-10 13:13:36 +00:00
|
|
|
atexit(display_deinit);
|
2019-05-20 13:10:47 +00:00
|
|
|
|
2023-03-23 14:38:11 +00:00
|
|
|
// Not doing gamma correction for "dummy" SDL driver
|
|
|
|
// (not to slow down device/UI tests)
|
|
|
|
char *sdl_env = getenv("SDL_VIDEODRIVER");
|
|
|
|
if (sdl_env && strcmp(sdl_env, "dummy") == 0) {
|
|
|
|
DO_GAMMA_CORRECTION = false;
|
|
|
|
} else {
|
|
|
|
DO_GAMMA_CORRECTION = true;
|
|
|
|
}
|
|
|
|
|
2020-08-03 15:11:38 +00:00
|
|
|
char *window_title = NULL;
|
2022-01-10 13:13:36 +00:00
|
|
|
char *window_title_alloc = NULL;
|
|
|
|
if (asprintf(&window_title_alloc, "Trezor^emu: %s", profile_name()) > 0) {
|
|
|
|
window_title = window_title_alloc;
|
|
|
|
} else {
|
2019-06-17 18:27:55 +00:00
|
|
|
window_title = "Trezor^emu";
|
2022-01-10 13:13:36 +00:00
|
|
|
window_title_alloc = NULL;
|
2019-05-20 13:10:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 13:13:36 +00:00
|
|
|
WINDOW =
|
2019-05-20 13:10:47 +00:00
|
|
|
SDL_CreateWindow(window_title, SDL_WINDOWPOS_UNDEFINED,
|
2019-03-29 15:26:02 +00:00
|
|
|
SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT,
|
2018-11-06 18:29:34 +00:00
|
|
|
#ifdef TREZOR_EMULATOR_RASPI
|
2019-03-29 15:26:02 +00:00
|
|
|
SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN
|
2018-11-06 18:29:34 +00:00
|
|
|
#else
|
2019-03-29 15:26:02 +00:00
|
|
|
SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI
|
2018-11-06 18:29:34 +00:00
|
|
|
#endif
|
2019-03-29 15:26:02 +00:00
|
|
|
);
|
2022-01-10 13:13:36 +00:00
|
|
|
free(window_title_alloc);
|
|
|
|
if (!WINDOW) {
|
2019-03-29 15:26:02 +00:00
|
|
|
printf("%s\n", SDL_GetError());
|
|
|
|
ensure(secfalse, "SDL_CreateWindow error");
|
|
|
|
}
|
2022-01-10 13:13:36 +00:00
|
|
|
RENDERER = SDL_CreateRenderer(WINDOW, -1, SDL_RENDERER_SOFTWARE);
|
2019-03-29 15:26:02 +00:00
|
|
|
if (!RENDERER) {
|
|
|
|
printf("%s\n", SDL_GetError());
|
2022-01-10 13:13:36 +00:00
|
|
|
SDL_DestroyWindow(WINDOW);
|
2019-03-29 15:26:02 +00:00
|
|
|
ensure(secfalse, "SDL_CreateRenderer error");
|
|
|
|
}
|
|
|
|
SDL_SetRenderDrawColor(RENDERER, 0, 0, 0, 255);
|
|
|
|
SDL_RenderClear(RENDERER);
|
|
|
|
BUFFER = SDL_CreateRGBSurface(0, MAX_DISPLAY_RESX, MAX_DISPLAY_RESY, 16,
|
|
|
|
0xF800, 0x07E0, 0x001F, 0x0000);
|
2023-03-23 14:38:11 +00:00
|
|
|
BUFFER_TO_DISPLAY =
|
|
|
|
SDL_CreateRGBSurface(0, MAX_DISPLAY_RESX, MAX_DISPLAY_RESY, 16, 0xF800,
|
|
|
|
0x07E0, 0x001F, 0x0000);
|
2019-03-29 15:26:02 +00:00
|
|
|
TEXTURE = SDL_CreateTexture(RENDERER, SDL_PIXELFORMAT_RGB565,
|
|
|
|
SDL_TEXTUREACCESS_STREAMING, DISPLAY_RESX,
|
|
|
|
DISPLAY_RESY);
|
|
|
|
SDL_SetTextureBlendMode(TEXTURE, SDL_BLENDMODE_BLEND);
|
2018-11-26 16:01:08 +00:00
|
|
|
#ifdef __APPLE__
|
2019-03-29 15:26:02 +00:00
|
|
|
// macOS Mojave SDL black screen workaround
|
|
|
|
SDL_PumpEvents();
|
2022-01-10 13:13:36 +00:00
|
|
|
SDL_SetWindowSize(WINDOW, WINDOW_WIDTH, WINDOW_HEIGHT);
|
2018-11-26 16:01:08 +00:00
|
|
|
#endif
|
2018-11-06 18:29:34 +00:00
|
|
|
#ifdef TREZOR_EMULATOR_RASPI
|
2019-05-04 20:32:03 +00:00
|
|
|
#include "background_raspi.h"
|
|
|
|
BACKGROUND = IMG_LoadTexture_RW(
|
|
|
|
RENDERER, SDL_RWFromMem(background_raspi_jpg, background_raspi_jpg_len),
|
|
|
|
0);
|
2018-11-06 18:29:34 +00:00
|
|
|
#else
|
2022-04-26 11:47:40 +00:00
|
|
|
#if defined TREZOR_MODEL_T
|
2019-05-04 20:32:03 +00:00
|
|
|
#include "background_T.h"
|
|
|
|
BACKGROUND = IMG_LoadTexture_RW(
|
|
|
|
RENDERER, SDL_RWFromMem(background_T_jpg, background_T_jpg_len), 0);
|
2022-04-26 11:47:40 +00:00
|
|
|
#elif defined TREZOR_MODEL_1
|
2019-05-04 20:32:03 +00:00
|
|
|
#include "background_1.h"
|
|
|
|
BACKGROUND = IMG_LoadTexture_RW(
|
|
|
|
RENDERER, SDL_RWFromMem(background_1_jpg, background_1_jpg_len), 0);
|
2022-05-01 12:05:32 +00:00
|
|
|
#elif defined TREZOR_MODEL_R
|
|
|
|
#include "background_R.h"
|
|
|
|
BACKGROUND = IMG_LoadTexture_RW(
|
|
|
|
RENDERER, SDL_RWFromMem(background_R_jpg, background_R_jpg_len), 0);
|
2019-05-04 20:32:03 +00:00
|
|
|
#endif
|
2018-11-06 18:29:34 +00:00
|
|
|
#endif
|
2019-03-29 15:26:02 +00:00
|
|
|
if (BACKGROUND) {
|
|
|
|
SDL_SetTextureBlendMode(BACKGROUND, SDL_BLENDMODE_NONE);
|
|
|
|
sdl_touch_offset_x = TOUCH_OFFSET_X;
|
|
|
|
sdl_touch_offset_y = TOUCH_OFFSET_Y;
|
|
|
|
} else {
|
2022-01-10 13:13:36 +00:00
|
|
|
SDL_SetWindowSize(WINDOW, DISPLAY_RESX + 2 * EMULATOR_BORDER,
|
2019-03-29 15:26:02 +00:00
|
|
|
DISPLAY_RESY + 2 * EMULATOR_BORDER);
|
|
|
|
sdl_touch_offset_x = EMULATOR_BORDER;
|
|
|
|
sdl_touch_offset_y = EMULATOR_BORDER;
|
|
|
|
}
|
|
|
|
DISPLAY_BACKLIGHT = 0;
|
2018-11-06 18:29:34 +00:00
|
|
|
#ifdef TREZOR_EMULATOR_RASPI
|
2019-03-29 15:26:02 +00:00
|
|
|
DISPLAY_ORIENTATION = 270;
|
|
|
|
SDL_ShowCursor(SDL_DISABLE);
|
2018-11-06 18:29:34 +00:00
|
|
|
#else
|
2019-03-29 15:26:02 +00:00
|
|
|
DISPLAY_ORIENTATION = 0;
|
2017-03-17 13:14:00 +00:00
|
|
|
#endif
|
2016-03-27 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 09:49:57 +00:00
|
|
|
void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
|
2019-03-29 15:26:02 +00:00
|
|
|
if (!RENDERER) {
|
|
|
|
display_init();
|
|
|
|
}
|
|
|
|
PIXELWINDOW.start.x = x0;
|
|
|
|
PIXELWINDOW.start.y = y0;
|
|
|
|
PIXELWINDOW.end.x = x1;
|
|
|
|
PIXELWINDOW.end.y = y1;
|
|
|
|
PIXELWINDOW.pos.x = x0;
|
|
|
|
PIXELWINDOW.pos.y = y0;
|
2016-03-27 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 11:40:02 +00:00
|
|
|
void display_sync(void) {}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
void display_refresh(void) {
|
|
|
|
if (!RENDERER) {
|
|
|
|
display_init();
|
|
|
|
}
|
|
|
|
if (BACKGROUND) {
|
2020-07-31 10:27:43 +00:00
|
|
|
const SDL_Rect r = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
|
|
|
|
SDL_RenderCopy(RENDERER, BACKGROUND, NULL, &r);
|
2019-03-29 15:26:02 +00:00
|
|
|
} else {
|
|
|
|
SDL_RenderClear(RENDERER);
|
|
|
|
}
|
2023-03-23 14:38:11 +00:00
|
|
|
// Fill BUFFER_TO_DISPLAY with BUFFER data
|
|
|
|
SDL_BlitSurface(BUFFER, NULL, BUFFER_TO_DISPLAY, NULL);
|
|
|
|
#if defined TREZOR_MODEL_T
|
|
|
|
// Gamma-correcting the display buffer for model T when wanted
|
|
|
|
if (DO_GAMMA_CORRECTION) {
|
|
|
|
gamma_correct_buffer_to_display();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// Show the display buffer
|
|
|
|
SDL_UpdateTexture(TEXTURE, NULL, BUFFER_TO_DISPLAY->pixels,
|
|
|
|
BUFFER_TO_DISPLAY->pitch);
|
2018-02-24 23:55:33 +00:00
|
|
|
#define BACKLIGHT_NORMAL 150
|
2019-03-29 15:26:02 +00:00
|
|
|
SDL_SetTextureAlphaMod(TEXTURE,
|
|
|
|
MIN(255, 255 * DISPLAY_BACKLIGHT / BACKLIGHT_NORMAL));
|
|
|
|
if (BACKGROUND) {
|
|
|
|
const SDL_Rect r = {TOUCH_OFFSET_X, TOUCH_OFFSET_Y, DISPLAY_RESX,
|
|
|
|
DISPLAY_RESY};
|
|
|
|
SDL_RenderCopyEx(RENDERER, TEXTURE, NULL, &r, DISPLAY_ORIENTATION, NULL, 0);
|
|
|
|
} else {
|
|
|
|
const SDL_Rect r = {EMULATOR_BORDER, EMULATOR_BORDER, DISPLAY_RESX,
|
|
|
|
DISPLAY_RESY};
|
|
|
|
SDL_RenderCopyEx(RENDERER, TEXTURE, NULL, &r, DISPLAY_ORIENTATION, NULL, 0);
|
|
|
|
}
|
|
|
|
SDL_RenderPresent(RENDERER);
|
2016-03-27 21:10:31 +00:00
|
|
|
}
|
2016-03-29 13:31:41 +00:00
|
|
|
|
2022-08-16 14:51:10 +00:00
|
|
|
int display_orientation(int degrees) {
|
|
|
|
if (degrees != DISPLAY_ORIENTATION) {
|
|
|
|
#if defined TREZOR_MODEL_T
|
|
|
|
if (degrees == 0 || degrees == 90 || degrees == 180 || degrees == 270) {
|
|
|
|
#elif defined TREZOR_MODEL_1 || defined TREZOR_MODEL_R
|
|
|
|
if (degrees == 0 || degrees == 180) {
|
|
|
|
#else
|
|
|
|
#error Unknown Trezor model
|
|
|
|
#endif
|
|
|
|
DISPLAY_ORIENTATION = degrees;
|
|
|
|
display_refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return DISPLAY_ORIENTATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
int display_get_orientation(void) { return DISPLAY_ORIENTATION; }
|
2016-10-05 22:02:46 +00:00
|
|
|
|
2022-08-16 14:51:10 +00:00
|
|
|
int display_backlight(int val) {
|
|
|
|
#if defined TREZOR_MODEL_1
|
|
|
|
val = 255;
|
|
|
|
#endif
|
|
|
|
if (DISPLAY_BACKLIGHT != val && val >= 0 && val <= 255) {
|
|
|
|
DISPLAY_BACKLIGHT = val;
|
|
|
|
display_refresh();
|
|
|
|
}
|
|
|
|
return DISPLAY_BACKLIGHT;
|
|
|
|
}
|
2017-03-17 13:14:00 +00:00
|
|
|
|
2019-04-02 13:26:34 +00:00
|
|
|
const char *display_save(const char *prefix) {
|
2019-03-29 15:26:02 +00:00
|
|
|
if (!RENDERER) {
|
|
|
|
display_init();
|
|
|
|
}
|
2019-04-02 13:26:34 +00:00
|
|
|
static int count;
|
|
|
|
static char filename[256];
|
|
|
|
// take a cropped view of the screen contents
|
2019-03-29 15:26:02 +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);
|
2019-04-02 13:26:34 +00:00
|
|
|
// compare with previous screen, skip if equal
|
2019-12-09 16:01:04 +00:00
|
|
|
if (PREV_SAVED != NULL) {
|
|
|
|
if (memcmp(PREV_SAVED->pixels, crop->pixels, crop->pitch * crop->h) == 0) {
|
2019-04-02 13:26:34 +00:00
|
|
|
SDL_FreeSurface(crop);
|
|
|
|
return filename;
|
|
|
|
}
|
2019-12-09 16:01:04 +00:00
|
|
|
SDL_FreeSurface(PREV_SAVED);
|
2019-04-02 13:26:34 +00:00
|
|
|
}
|
|
|
|
// save to png
|
|
|
|
snprintf(filename, sizeof(filename), "%s%08d.png", prefix, count++);
|
|
|
|
IMG_SavePNG(crop, filename);
|
2019-12-09 16:01:04 +00:00
|
|
|
PREV_SAVED = crop;
|
2019-04-02 13:26:34 +00:00
|
|
|
return filename;
|
2016-10-05 22:02:46 +00:00
|
|
|
}
|
2019-12-09 16:01:04 +00:00
|
|
|
|
|
|
|
void display_clear_save(void) {
|
|
|
|
SDL_FreeSurface(PREV_SAVED);
|
|
|
|
PREV_SAVED = NULL;
|
|
|
|
}
|