unleashed-firmware/applications/gui/gui.c

488 lines
16 KiB
C
Raw Normal View History

#include "gui/canvas.h"
#include "gui_i.h"
#define TAG "GuiSrv"
ViewPort* gui_view_port_find_enabled(ViewPortArray_t array) {
// Iterating backward
ViewPortArray_it_t it;
ViewPortArray_it_last(it, array);
while(!ViewPortArray_end_p(it)) {
ViewPort* view_port = *ViewPortArray_ref(it);
if(view_port_is_enabled(view_port)) {
return view_port;
}
ViewPortArray_previous(it);
}
return NULL;
}
void gui_update(Gui* gui) {
furi_assert(gui);
osThreadFlagsSet(gui->thread, GUI_THREAD_FLAG_DRAW);
}
void gui_input_events_callback(const void* value, void* ctx) {
furi_assert(value);
furi_assert(ctx);
Gui* gui = ctx;
osMessageQueuePut(gui->input_queue, value, 0, osWaitForever);
osThreadFlagsSet(gui->thread, GUI_THREAD_FLAG_INPUT);
}
// Only Fullscreen supports vertical display for now
2020-10-15 15:05:28 +00:00
bool gui_redraw_fs(Gui* gui) {
canvas_set_orientation(gui->canvas, CanvasOrientationHorizontal);
canvas_frame_set(gui->canvas, 0, 0, GUI_DISPLAY_WIDTH, GUI_DISPLAY_HEIGHT);
ViewPort* view_port = gui_view_port_find_enabled(gui->layers[GuiLayerFullscreen]);
if(view_port) {
view_port_draw(view_port, gui->canvas);
return true;
} else {
return false;
}
}
static void gui_redraw_status_bar(Gui* gui, bool need_attention) {
ViewPortArray_it_t it;
uint8_t x;
uint8_t x_used = 0;
uint8_t width;
ViewPort* view_port;
canvas_set_orientation(gui->canvas, CanvasOrientationHorizontal);
canvas_frame_set(
gui->canvas, GUI_STATUS_BAR_X, GUI_STATUS_BAR_Y, GUI_DISPLAY_WIDTH, GUI_STATUS_BAR_HEIGHT);
/* for support black theme - paint white area and
* draw icon with transparent white color
*/
canvas_set_color(gui->canvas, ColorWhite);
canvas_draw_box(gui->canvas, 1, 1, 9, 7);
canvas_draw_box(gui->canvas, 7, 3, 58, 6);
canvas_draw_box(gui->canvas, 61, 1, 32, 7);
canvas_draw_box(gui->canvas, 89, 3, 38, 6);
canvas_set_color(gui->canvas, ColorBlack);
canvas_set_bitmap_mode(gui->canvas, 1);
canvas_draw_icon(gui->canvas, 0, 0, &I_Background_128x11);
canvas_set_bitmap_mode(gui->canvas, 0);
// Right side
x = GUI_DISPLAY_WIDTH;
ViewPortArray_it(it, gui->layers[GuiLayerStatusBarRight]);
while(!ViewPortArray_end_p(it) && x_used < GUI_STATUS_BAR_WIDTH) {
// Render view_port;
view_port = *ViewPortArray_ref(it);
if(view_port_is_enabled(view_port)) {
width = view_port_get_width(view_port);
if(!width) width = 8;
x_used += width;
x -= (width + 2);
canvas_frame_set(
gui->canvas, x - 3, GUI_STATUS_BAR_Y, width + 5, GUI_STATUS_BAR_HEIGHT);
canvas_set_color(gui->canvas, ColorWhite);
canvas_draw_box(gui->canvas, 2, 1, width + 2, 10);
canvas_set_color(gui->canvas, ColorBlack);
canvas_draw_rframe(
gui->canvas, 0, 0, canvas_width(gui->canvas), canvas_height(gui->canvas), 1);
canvas_draw_line(gui->canvas, 1, 1, 1, canvas_height(gui->canvas) - 2);
canvas_draw_line(
gui->canvas,
2,
canvas_height(gui->canvas) - 2,
canvas_width(gui->canvas) - 2,
canvas_height(gui->canvas) - 2);
canvas_frame_set(
gui->canvas, x, GUI_STATUS_BAR_Y + 1, width, GUI_STATUS_BAR_WORKAREA_HEIGHT);
view_port_draw(view_port, gui->canvas);
}
ViewPortArray_next(it);
}
// Left side
x = 0;
ViewPortArray_it(it, gui->layers[GuiLayerStatusBarLeft]);
while(!ViewPortArray_end_p(it) && x_used < GUI_STATUS_BAR_WIDTH) {
// Render view_port;
view_port = *ViewPortArray_ref(it);
if(view_port_is_enabled(view_port)) {
width = view_port_get_width(view_port);
if(!width) width = 8;
x_used += width;
canvas_frame_set(
gui->canvas, 0, GUI_STATUS_BAR_Y, x + width + 5, GUI_STATUS_BAR_HEIGHT);
canvas_draw_rframe(
gui->canvas, 0, 0, canvas_width(gui->canvas), canvas_height(gui->canvas), 1);
canvas_draw_line(gui->canvas, 1, 1, 1, canvas_height(gui->canvas) - 2);
canvas_draw_line(
gui->canvas,
2,
canvas_height(gui->canvas) - 2,
canvas_width(gui->canvas) - 2,
canvas_height(gui->canvas) - 2);
canvas_frame_set(gui->canvas, x, GUI_STATUS_BAR_Y, width + 5, GUI_STATUS_BAR_HEIGHT);
canvas_set_color(gui->canvas, ColorWhite);
canvas_draw_box(gui->canvas, 2, 1, width + 2, 10);
canvas_set_color(gui->canvas, ColorBlack);
canvas_frame_set(
gui->canvas, x + 3, GUI_STATUS_BAR_Y + 2, width, GUI_STATUS_BAR_WORKAREA_HEIGHT);
view_port_draw(view_port, gui->canvas);
x += (width + 2);
}
ViewPortArray_next(it);
}
if(need_attention) {
width = icon_get_width(&I_Attention_5x8);
canvas_frame_set(gui->canvas, 0, GUI_STATUS_BAR_Y, x + width + 5, GUI_STATUS_BAR_HEIGHT);
canvas_draw_rframe(
gui->canvas, 0, 0, canvas_width(gui->canvas), canvas_height(gui->canvas), 1);
canvas_draw_line(gui->canvas, 1, 1, 1, canvas_height(gui->canvas) - 2);
canvas_draw_line(
gui->canvas,
2,
canvas_height(gui->canvas) - 2,
canvas_width(gui->canvas) - 2,
canvas_height(gui->canvas) - 2);
canvas_frame_set(gui->canvas, x, GUI_STATUS_BAR_Y, width + 5, GUI_STATUS_BAR_HEIGHT);
canvas_set_color(gui->canvas, ColorWhite);
canvas_draw_box(gui->canvas, 2, 1, width + 2, 10);
canvas_set_color(gui->canvas, ColorBlack);
canvas_frame_set(
gui->canvas, x + 3, GUI_STATUS_BAR_Y + 2, width, GUI_STATUS_BAR_WORKAREA_HEIGHT);
canvas_draw_icon(gui->canvas, 0, 0, &I_Attention_5x8);
}
}
bool gui_redraw_window(Gui* gui) {
canvas_set_orientation(gui->canvas, CanvasOrientationHorizontal);
canvas_frame_set(gui->canvas, GUI_WINDOW_X, GUI_WINDOW_Y, GUI_WINDOW_WIDTH, GUI_WINDOW_HEIGHT);
ViewPort* view_port = gui_view_port_find_enabled(gui->layers[GuiLayerWindow]);
if(view_port) {
view_port_draw(view_port, gui->canvas);
return true;
}
return false;
}
bool gui_redraw_desktop(Gui* gui) {
canvas_set_orientation(gui->canvas, CanvasOrientationHorizontal);
canvas_frame_set(gui->canvas, 0, 0, GUI_DISPLAY_WIDTH, GUI_DISPLAY_HEIGHT);
ViewPort* view_port = gui_view_port_find_enabled(gui->layers[GuiLayerDesktop]);
if(view_port) {
view_port_draw(view_port, gui->canvas);
return true;
}
return false;
}
2020-10-15 15:05:28 +00:00
void gui_redraw(Gui* gui) {
furi_assert(gui);
gui_lock(gui);
canvas_reset(gui->canvas);
if(gui->lockdown) {
gui_redraw_desktop(gui);
bool need_attention =
(gui_view_port_find_enabled(gui->layers[GuiLayerWindow]) != 0 ||
gui_view_port_find_enabled(gui->layers[GuiLayerFullscreen]) != 0);
gui_redraw_status_bar(gui, need_attention);
} else {
if(!gui_redraw_fs(gui)) {
if(!gui_redraw_window(gui)) {
gui_redraw_desktop(gui);
}
gui_redraw_status_bar(gui, false);
}
}
canvas_commit(gui->canvas);
for
M_EACH(p, gui->canvas_callback_pair, CanvasCallbackPairArray_t) {
p->callback(
canvas_get_buffer(gui->canvas), canvas_get_buffer_size(gui->canvas), p->context);
}
gui_unlock(gui);
}
2020-10-15 15:05:28 +00:00
void gui_input(Gui* gui, InputEvent* input_event) {
furi_assert(gui);
furi_assert(input_event);
// Check input complementarity
uint8_t key_bit = (1 << input_event->key);
if(input_event->type == InputTypeRelease) {
gui->ongoing_input &= ~key_bit;
} else if(input_event->type == InputTypePress) {
gui->ongoing_input |= key_bit;
} else if(!(gui->ongoing_input & key_bit)) {
FURI_LOG_D(
TAG,
"non-complementary input, discarding key: %s type: %s, sequence: %p",
input_get_key_name(input_event->key),
input_get_type_name(input_event->type),
input_event->sequence);
return;
}
gui_lock(gui);
ViewPort* view_port = NULL;
if(gui->lockdown) {
view_port = gui_view_port_find_enabled(gui->layers[GuiLayerDesktop]);
} else {
view_port = gui_view_port_find_enabled(gui->layers[GuiLayerFullscreen]);
if(!view_port) view_port = gui_view_port_find_enabled(gui->layers[GuiLayerWindow]);
if(!view_port) view_port = gui_view_port_find_enabled(gui->layers[GuiLayerDesktop]);
}
if(!(gui->ongoing_input & ~key_bit) && input_event->type == InputTypePress) {
gui->ongoing_input_view_port = view_port;
}
if(view_port && view_port == gui->ongoing_input_view_port) {
view_port_input(view_port, input_event);
} else if(gui->ongoing_input_view_port && input_event->type == InputTypeRelease) {
FURI_LOG_D(
TAG,
"ViewPort changed while key press %p -> %p. Sending key: %s, type: %s, sequence: %p to previous view port",
gui->ongoing_input_view_port,
view_port,
input_get_key_name(input_event->key),
input_get_type_name(input_event->type),
input_event->sequence);
view_port_input(gui->ongoing_input_view_port, input_event);
} else {
FURI_LOG_D(
TAG,
"ViewPort changed while key press %p -> %p. Discarding key: %s, type: %s, sequence: %p",
gui->ongoing_input_view_port,
view_port,
input_get_key_name(input_event->key),
input_get_type_name(input_event->type),
input_event->sequence);
}
gui_unlock(gui);
}
void gui_lock(Gui* gui) {
furi_assert(gui);
furi_check(osMutexAcquire(gui->mutex, osWaitForever) == osOK);
}
void gui_unlock(Gui* gui) {
furi_assert(gui);
furi_check(osMutexRelease(gui->mutex) == osOK);
}
void gui_add_view_port(Gui* gui, ViewPort* view_port, GuiLayer layer) {
furi_assert(gui);
furi_assert(view_port);
furi_check(layer < GuiLayerMAX);
// Only fullscreen supports Vertical orientation for now
furi_assert(
(layer == GuiLayerFullscreen) || (view_port->orientation != ViewPortOrientationVertical));
gui_lock(gui);
// Verify that view port is not yet added
ViewPortArray_it_t it;
for(size_t i = 0; i < GuiLayerMAX; i++) {
ViewPortArray_it(it, gui->layers[i]);
while(!ViewPortArray_end_p(it)) {
furi_assert(*ViewPortArray_ref(it) != view_port);
ViewPortArray_next(it);
}
}
// Add view port and link with gui
ViewPortArray_push_back(gui->layers[layer], view_port);
view_port_gui_set(view_port, gui);
gui_unlock(gui);
gui_update(gui);
}
void gui_remove_view_port(Gui* gui, ViewPort* view_port) {
furi_assert(gui);
furi_assert(view_port);
gui_lock(gui);
view_port_gui_set(view_port, NULL);
ViewPortArray_it_t it;
for(size_t i = 0; i < GuiLayerMAX; i++) {
ViewPortArray_it(it, gui->layers[i]);
while(!ViewPortArray_end_p(it)) {
if(*ViewPortArray_ref(it) == view_port) {
ViewPortArray_remove(gui->layers[i], it);
} else {
ViewPortArray_next(it);
}
}
}
2020-10-16 15:25:06 +00:00
if(gui->ongoing_input_view_port == view_port) {
gui->ongoing_input_view_port = NULL;
}
gui_unlock(gui);
}
2020-10-15 08:32:48 +00:00
void gui_view_port_send_to_front(Gui* gui, ViewPort* view_port) {
furi_assert(gui);
furi_assert(view_port);
gui_lock(gui);
// Remove
GuiLayer layer = GuiLayerMAX;
ViewPortArray_it_t it;
for(size_t i = 0; i < GuiLayerMAX; i++) {
ViewPortArray_it(it, gui->layers[i]);
while(!ViewPortArray_end_p(it)) {
if(*ViewPortArray_ref(it) == view_port) {
ViewPortArray_remove(gui->layers[i], it);
furi_assert(layer == GuiLayerMAX);
layer = i;
} else {
ViewPortArray_next(it);
}
}
}
furi_assert(layer != GuiLayerMAX);
// Return to the top
ViewPortArray_push_back(gui->layers[layer], view_port);
gui_unlock(gui);
}
void gui_view_port_send_to_back(Gui* gui, ViewPort* view_port) {
furi_assert(gui);
furi_assert(view_port);
gui_lock(gui);
// Remove
GuiLayer layer = GuiLayerMAX;
ViewPortArray_it_t it;
for(size_t i = 0; i < GuiLayerMAX; i++) {
ViewPortArray_it(it, gui->layers[i]);
while(!ViewPortArray_end_p(it)) {
if(*ViewPortArray_ref(it) == view_port) {
ViewPortArray_remove(gui->layers[i], it);
furi_assert(layer == GuiLayerMAX);
layer = i;
} else {
ViewPortArray_next(it);
}
}
}
furi_assert(layer != GuiLayerMAX);
// Return to the top
ViewPortArray_push_at(gui->layers[layer], 0, view_port);
gui_unlock(gui);
}
void gui_add_framebuffer_callback(Gui* gui, GuiCanvasCommitCallback callback, void* context) {
furi_assert(gui);
const CanvasCallbackPair p = {callback, context};
gui_lock(gui);
furi_assert(CanvasCallbackPairArray_count(gui->canvas_callback_pair, p) == 0);
CanvasCallbackPairArray_push_back(gui->canvas_callback_pair, p);
gui_unlock(gui);
gui_update(gui);
}
void gui_remove_framebuffer_callback(Gui* gui, GuiCanvasCommitCallback callback, void* context) {
furi_assert(gui);
const CanvasCallbackPair p = {callback, context};
gui_lock(gui);
furi_assert(CanvasCallbackPairArray_count(gui->canvas_callback_pair, p) == 1);
CanvasCallbackPairArray_remove_val(gui->canvas_callback_pair, p);
gui_unlock(gui);
}
size_t gui_get_framebuffer_size(Gui* gui) {
furi_assert(gui);
return canvas_get_buffer_size(gui->canvas);
}
void gui_set_lockdown(Gui* gui, bool lockdown) {
furi_assert(gui);
gui_lock(gui);
gui->lockdown = lockdown;
gui_unlock(gui);
gui_update(gui);
}
Gui* gui_alloc() {
[FL-2274] Inventing streams and moving FFF to them (#981) * Streams: string stream * String stream: updated insert/delete api * Streams: generic stream interface and string stream implementation * Streams: helpers for insert and delete_and_insert * FFF: now compatible with streams * MinUnit: introduced tests with arguments * FFF: stream access violation * Streams: copy data between streams * Streams: file stream * FFF: documentation * FFStream: documentation * FFF: alloc as file * MinUnit: support for nested tests * Streams: changed delete_and_insert, now it returns success flag. Added ability dump stream inner parameters and data to cout. * FFF: simplified file open function * Streams: unit tests * FFF: tests * Streams: declare cache_size constant as define, to allow variable modified arrays * FFF: lib moved to a separate folder * iButton: new FFF * RFID: new FFF * Animations: new FFF * IR: new FFF * NFC: new FFF * Flipper file format: delete lib * U2F: new FFF * Subghz: new FFF and streams * Streams: read line * Streams: split * FuriCore: implement memset with extra asserts * FuriCore: implement extra heap asserts without inventing memset * Scene manager: protected access to the scene id stack with a size check * NFC worker: dirty fix for issue where hal_nfc was busy on app start * Furi: update allocator to erase memory on allocation. Replace furi_alloc with malloc. * FuriCore: cleanup memmgr code. * Furi HAL: furi_hal_init is split into critical and non-critical parts. The critical part is currently clock and console. * Memmgr: added ability to track allocations and deallocations through console. * FFStream: some speedup * Streams, FF: minor fixes * Tests: restore * File stream: a slightly more thread-safe version of file_stream_delete_and_insert Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2022-02-18 19:53:46 +00:00
Gui* gui = malloc(sizeof(Gui));
// Thread ID
gui->thread = osThreadGetId();
// Allocate mutex
gui->mutex = osMutexNew(NULL);
furi_check(gui->mutex);
// Layers
for(size_t i = 0; i < GuiLayerMAX; i++) {
ViewPortArray_init(gui->layers[i]);
}
// Drawing canvas
gui->canvas = canvas_init();
CanvasCallbackPairArray_init(gui->canvas_callback_pair);
// Input
gui->input_queue = osMessageQueueNew(8, sizeof(InputEvent), NULL);
gui->input_events = furi_record_open("input_events");
furi_check(gui->input_events);
furi_pubsub_subscribe(gui->input_events, gui_input_events_callback, gui);
2020-10-16 15:25:06 +00:00
return gui;
}
int32_t gui_srv(void* p) {
2020-10-15 15:05:28 +00:00
Gui* gui = gui_alloc();
2020-10-15 08:32:48 +00:00
[FL-140] Core api dynamic records (#296) * SYSTEM: tickless mode with deep sleep. * Move FreeRTOS ticks to lptim2 * API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source. * API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode. * NFC: support for tickless mode. * API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications. * BLE: prevent sleep while core2 starting * HAL: nap while in insomnia mode * init records work * try to implement record delete * tests and flapp * flapp subsystem * new core functions to get app stat, simplify core code * fix thread termination * add strdup to core * fix tests * Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage * Refactoring: update furi record api usage, cleanup code * Fix broken merge for freertos apps * Core, Target: fix compilation warnings * Drop firmware target local * HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode. * SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial. * delete old app example-ipc * delete old app fatfs list * fix strobe app, add input header * delete old display driver * comment old app qr-code * fix sd-card test, add forced widget update * remove unused new core test * increase heap to 128k * comment and assert old core tests * fix syntax Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 16:09:26 +00:00
furi_record_create("gui", gui);
2020-10-15 08:32:48 +00:00
while(1) {
uint32_t flags = osThreadFlagsWait(GUI_THREAD_FLAG_ALL, osFlagsWaitAny, osWaitForever);
// Process and dispatch input
if(flags & GUI_THREAD_FLAG_INPUT) {
// Process till queue become empty
InputEvent input_event;
while(osMessageQueueGet(gui->input_queue, &input_event, NULL, 0) == osOK) {
gui_input(gui, &input_event);
}
}
// Process and dispatch draw call
if(flags & GUI_THREAD_FLAG_DRAW) {
// Clear flags that arrived on input step
osThreadFlagsClear(GUI_THREAD_FLAG_DRAW);
gui_redraw(gui);
}
}
return 0;
}