1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-18 13:38:12 +00:00
trezor-firmware/micropython/extmod/modtrezorui/display-unix-sdl.h

143 lines
4.3 KiB
C
Raw Normal View History

2016-03-27 21:10:31 +00:00
/*
* Copyright (c) Pavol Rusnak, SatoshiLabs
*
2016-05-28 12:37:32 +00:00
* Licensed under TREZOR License
* see LICENSE file for details
2016-03-27 21:10:31 +00:00
*/
2016-04-28 19:43:22 +00:00
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
2016-03-27 21:10:31 +00:00
2016-05-02 15:16:55 +00:00
#define DISPLAY_BORDER 16
2016-04-30 13:35:23 +00:00
2016-03-27 21:10:31 +00:00
static SDL_Renderer *RENDERER = 0;
static SDL_Surface *BUFFER = 0;
2016-03-27 21:10:31 +00:00
static SDL_Texture *TEXTURE = 0;
static int DATAODD = 0;
static int POSX, POSY, SX, SY, EX, EY = 0;
#define CMD(X) (void)(X);
2016-05-11 19:05:08 +00:00
void DATA(uint8_t x) {
2016-03-27 21:10:31 +00:00
if (POSX <= EX && POSY <= EY) {
((uint8_t *)BUFFER->pixels)[POSX * 2 + POSY * BUFFER->pitch + (DATAODD ^ 1)] = x;
2016-03-27 21:10:31 +00:00
}
DATAODD = !DATAODD;
if (DATAODD == 0) {
POSX++;
if (POSX > EX) {
POSX = SX;
POSY++;
}
}
}
// this should match values used in msg_poll_ui_event() in modtrezormsg/modtrezormsg-stmhal.h
uint32_t trezorui_poll_event(void)
{
SDL_Event event;
int x, y;
SDL_PumpEvents();
2016-04-29 01:15:18 +00:00
if (SDL_PollEvent(&event) > 0) {
switch (event.type) {
case SDL_MOUSEBUTTONDOWN:
2016-03-31 22:59:09 +00:00
case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONUP:
x = event.button.x - DISPLAY_BORDER;
y = event.button.y - DISPLAY_BORDER;
2016-09-24 14:53:43 +00:00
if (x < 0 || y < 0 || x >= DISPLAY_RESX || y >= DISPLAY_RESY) break;
2016-03-31 22:59:09 +00:00
switch (event.type) {
case SDL_MOUSEBUTTONDOWN:
2016-04-28 20:58:15 +00:00
return (0x00 << 24) | (0x01 << 16) | (x << 8) | y; // touch_start
2016-03-31 22:59:09 +00:00
break;
case SDL_MOUSEMOTION:
// remove other SDL_MOUSEMOTION events from queue
SDL_FlushEvent(SDL_MOUSEMOTION);
2016-03-31 22:59:09 +00:00
if (event.motion.state) {
2016-04-28 20:58:15 +00:00
return (0x00 << 24) | (0x02 << 16) | (x << 8) | y; // touch_move
2016-03-31 22:59:09 +00:00
}
break;
case SDL_MOUSEBUTTONUP:
2016-05-12 14:15:47 +00:00
return (0x00 << 24) | (0x04 << 16) | (x << 8) | y; // touch_end
2016-03-31 22:59:09 +00:00
break;
}
break;
case SDL_KEYUP:
if (event.key.keysym.sym == SDLK_ESCAPE) {
exit(3);
}
break;
2016-04-28 19:43:22 +00:00
case SDL_QUIT:
exit(3);
break;
}
}
2016-04-28 20:58:15 +00:00
return 0;
}
2016-05-11 19:05:08 +00:00
void display_init(void)
2016-03-27 21:10:31 +00:00
{
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("SDL_Init Error: %s\n", SDL_GetError());
}
2016-09-24 14:53:43 +00:00
SDL_Window *win = SDL_CreateWindow("TREZOR", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, DISPLAY_RESX + 2 * DISPLAY_BORDER, DISPLAY_RESY + 2 * DISPLAY_BORDER, SDL_WINDOW_SHOWN);
2016-03-27 21:10:31 +00:00
if (!win) {
printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
SDL_Quit();
}
RENDERER = SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE);
if (!RENDERER) {
printf("SDL_CreateRenderer Error: %s\n", SDL_GetError());
SDL_DestroyWindow(win);
SDL_Quit();
}
SDL_SetRenderDrawColor(RENDERER, BACKLIGHT, BACKLIGHT, BACKLIGHT, 255);
2016-03-27 21:10:31 +00:00
SDL_RenderClear(RENDERER);
BUFFER = SDL_CreateRGBSurface(0, DISPLAY_RESX, 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);
2016-05-17 14:24:51 +00:00
SDL_SetTextureBlendMode(TEXTURE, SDL_BLENDMODE_NONE);
SDL_SetTextureAlphaMod(TEXTURE, 0);
2016-03-27 21:10:31 +00:00
}
void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
2016-05-11 19:05:08 +00:00
{
SX = x0; SY = y0;
EX = x1; EY = y1;
2016-03-27 21:10:31 +00:00
POSX = SX; POSY = SY;
DATAODD = 0;
}
2016-10-03 09:52:19 +00:00
void display_refresh(void)
2016-03-27 21:10:31 +00:00
{
2016-03-29 13:31:41 +00:00
SDL_RenderClear(RENDERER);
SDL_UpdateTexture(TEXTURE, NULL, BUFFER->pixels, BUFFER->pitch);
2016-09-24 14:53:43 +00:00
const SDL_Rect r = {DISPLAY_BORDER, DISPLAY_BORDER, DISPLAY_RESX, DISPLAY_RESY};
SDL_RenderCopyEx(RENDERER, TEXTURE, NULL, &r, ORIENTATION, NULL, 0);
2016-03-27 21:10:31 +00:00
SDL_RenderPresent(RENDERER);
}
2016-03-29 13:31:41 +00:00
int display_orientation(int degrees)
2016-03-29 13:31:41 +00:00
{
if (degrees != ORIENTATION) {
if (degrees == 0 || degrees == 90 || degrees == 180 || degrees == 270) {
ORIENTATION = degrees;
}
}
return ORIENTATION;
2016-03-29 13:31:41 +00:00
}
int display_backlight(int val)
2016-04-03 13:26:49 +00:00
{
2016-06-06 15:42:50 +00:00
if (BACKLIGHT != val && val >= 0 && val <= 255) {
BACKLIGHT = val;
2016-06-06 15:42:50 +00:00
SDL_SetRenderDrawColor(RENDERER, BACKLIGHT, BACKLIGHT, BACKLIGHT, 255);
}
return BACKLIGHT;
2016-04-03 13:26:49 +00:00
}
void display_save(const char *filename)
{
IMG_SavePNG(BUFFER, filename);
}