Skip to content
This repository has been archived by the owner on Aug 18, 2024. It is now read-only.

Commit

Permalink
added proper gui
Browse files Browse the repository at this point in the history
  • Loading branch information
Milk-Cool committed Sep 16, 2023
1 parent 116f517 commit 629bd46
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"stdlib.h": "c",
"record.h": "c",
"crash.h": "c",
"unistd.h": "c"
"unistd.h": "c",
"random": "c"
},
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ CC_PREFIX_FINAL = gcc -Wall -Wno-main
all: $(OUT_APP)

$(OUT_APP):# $(OUT_LIB)#
$(CC_PREFIX_FINAL) -I$(LIBS) -I$(HELPERS) $(SRC_APP) $(SOURCES) -lncurses -o $(OUT_APP)
$(CC_PREFIX_FINAL) -I$(LIBS) -I$(HELPERS) $(SRC_APP) $(SOURCES) -lSDL2 -o $(OUT_APP)

#$(OUT_LIB): $(OBJECTS)
# ar rcs $(OUT_LIB) $(OBJECTS)
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
An emulator for the Flipper Zero\
Based on [Flipper Zero Firmware](https://github.com/flipperdevices/flipperzero-firmware)

![Screenshot](https://raw.githubusercontent.com/Milk-Cool/flippulator/main/images/screenshot.png)

## Requirements
A linux system\
Packages `make`, `gcc`, `libncurses5`
Expand All @@ -12,11 +14,10 @@ Packages `make`, `gcc`, `libncurses5`
## Limitations
- The main function cannot be called `main`
- `furi.h` must be included
- `furi.h` includes `ncurses.h`, which reserves some function names such as `clear` and `refresh`
- Due to ncurses limitations, only the `InputTypePress` input type is supported.
- Right now, only the `InputTypePress` and `InputTypeRelease` input types are supported.

## TODOs
- [ ] Use GTK instead of ncurses
- [x] Use ~~GTK~~ SDL instead of ncurses
- [ ] Write support for FuriTypes (FuriString, FuriMutex, etc.)


Expand Down
1 change: 0 additions & 1 deletion flipper/furi.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <stdlib.h>
#include <ncurses.h>

#include "core/base.h"
#include "core/message_queue.h"
Expand Down
81 changes: 53 additions & 28 deletions flipper/gui/gui.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#include "gui_i.h"

#include <pthread.h>
#include <ncurses.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#include <SDL2/SDL.h>

static SDL_Renderer* renderer;
static SDL_Window* window;
static SDL_Rect rect;
static SDL_Event event;
static bool running = true;

#include <stdio.h>

Expand All @@ -16,41 +22,60 @@ Gui* gui_alloc() {
return gui;
}

void exit_sdl(uint8_t code) {
// FIXME: crashes when exiting
// SDL_DestroyRenderer(renderer);
// SDL_DestroyWindow(window);
// SDL_Quit();
exit(code);
}

// TODO: long presses and stuff
void* handle_input(void* _view_port) {
static void* handle_input(void* _view_port) {
ViewPort* view_port = _view_port;
while(true) {
char c;
c = getch();
InputEvent* event = malloc(sizeof(InputEvent));
event->type = InputTypePress;
if(c == 3) event->key = InputKeyUp;
else if(c == 2) event->key = InputKeyDown;
else if(c == 4) event->key = InputKeyLeft;
else if(c == 5) event->key = InputKeyRight;
else if(c == 122) event->key = InputKeyOk;
else if(c == 120) event->key = InputKeyBack;
else if(c == 113) {
endwin();
exit(0);
while(running) {
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) exit_sdl(0);
if(event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) {
InputEvent* e = malloc(sizeof(InputEvent));
e->type = event.type == SDL_KEYDOWN ? InputTypePress : InputTypeRelease;
bool flag = true;
if(event.key.keysym.sym == SDLK_UP) e->key = InputKeyUp;
else if(event.key.keysym.sym == SDLK_DOWN) e->key = InputKeyDown;
else if(event.key.keysym.sym == SDLK_LEFT) e->key = InputKeyLeft;
else if(event.key.keysym.sym == SDLK_RIGHT) e->key = InputKeyRight;
else if(event.key.keysym.sym == SDLK_z) e->key = InputKeyOk;
else if(event.key.keysym.sym == SDLK_x) e->key = InputKeyBack;
else flag = false;
if(view_port->input_callback != NULL && flag)
view_port->input_callback(e, view_port->input_callback_context);
}
}
view_port->input_callback(event, view_port->input_callback_context);
}
return NULL;
}
void* handle_gui(void* _view_port) {
static void* handle_gui(void* _view_port) {
ViewPort* view_port = _view_port;
while(true) {
while(running) {
if(view_port->draw_callback != NULL)
view_port->draw_callback(view_port->gui->canvas, view_port->draw_callback_context);
clear();

SDL_SetRenderDrawColor(renderer, 0xff, 0x82, 0x00, 0xff);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xff);

for(uint8_t x = 0; x < view_port->width; x++)
for(uint8_t y = 0; y < view_port->height; y++) {
move(y, x);
printw("%c", view_port->gui->canvas->fb[view_port->gui->canvas->offset_x + x][view_port->gui->canvas->offset_y + y] == 1 ? 'X' : ' ');
if(view_port->gui->canvas->fb[view_port->gui->canvas->offset_x + x][view_port->gui->canvas->offset_y + y] != 1)
continue;
rect.x = x * 5;
rect.y = y * 5;
rect.w = 5;
rect.h = 5;
SDL_RenderDrawRect(renderer, &rect);
SDL_RenderFillRect(renderer, &rect);
}
refresh();
SDL_RenderPresent(renderer);
// ~60 FPS
#ifdef _WIN32
Sleep(16); // 16ms
Expand All @@ -67,11 +92,11 @@ void gui_add_view_port(Gui* gui, ViewPort* view_port, GuiLayer layer) {
gui->view_port = view_port;
view_port->gui = gui;

initscr();
noecho();
cbreak();
keypad(stdscr, TRUE);
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(640, 320, 0, &window, &renderer);
SDL_SetWindowTitle(window, "Flippulator");

pthread_t draw_thread_id;
pthread_create(&draw_thread_id, NULL, handle_gui, view_port);
pthread_t input_thread_id;
Expand Down
2 changes: 2 additions & 0 deletions flipper/gui/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ typedef void (*GuiCanvasCommitCallback)(

typedef struct Gui Gui;

void exit_sdl(uint8_t code);

void gui_add_view_port(Gui* gui, ViewPort* view_port, GuiLayer layer);
void gui_remove_view_port(Gui* gui, ViewPort* view_port);
Gui* gui_alloc();
Expand Down
5 changes: 2 additions & 3 deletions helpers/crash.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "crash.h"
#include <ncurses.h>
#include <gui/gui.h>

void crash(uint8_t code, const char* msg) {
endwin();
printf("\e[1;91mCRASHED DUE TO: %s\e[0m\r\n", msg);
exit(code);
exit_sdl(code);
}
Binary file added images/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ const APP_COPY = "flippulator_app_copy";
int main() {
furi_init();
${manifest.entry_point}(NULL);
endwin();
return 0;
exit_sdl(0);
}`);
spawn("make", [], {
"cwd": process.cwd(),
Expand Down

0 comments on commit 629bd46

Please sign in to comment.