diff --git a/rpi/CMakeLists.txt b/rpi/CMakeLists.txt index 21747a1..2c12e8b 100644 --- a/rpi/CMakeLists.txt +++ b/rpi/CMakeLists.txt @@ -5,6 +5,7 @@ find_package(FFmpeg REQUIRED COMPONENTS avformat avcodec avutil avfilter swscale add_executable(vanilla-pi drm.c main.c + ui.c ) install(TARGETS vanilla-pi) diff --git a/rpi/main.c b/rpi/main.c index 7cda9b9..fdbbe20 100644 --- a/rpi/main.c +++ b/rpi/main.c @@ -17,6 +17,7 @@ #include #include "drm.h" +#include "ui.h" AVFrame *present_frame; AVFrame *decoding_frame; @@ -156,7 +157,6 @@ int run_backend(void *data) SDL_Thread *decode_thread = SDL_CreateThread(decode_loop, "vanilla-decode", NULL); while (vanilla_wait_event(&event)) { - printf("front end got event\n"); if (event.type == VANILLA_EVENT_VIDEO) { SDL_LockMutex(decode_loop_mutex); @@ -170,10 +170,6 @@ int run_backend(void *data) SDL_CondBroadcast(decode_loop_cond); SDL_UnlockMutex(decode_loop_mutex); - - /*uint8_t *data = av_malloc(event.size); - memcpy(data, event.data, event.size); - decode(data, event.size);*/ } else if (event.type == VANILLA_EVENT_AUDIO) { if (audio) { if (SDL_QueueAudio(audio, event.data, event.size) < 0) { @@ -431,8 +427,15 @@ int main(int argc, const char **argv) SDL_GameController *controller = find_valid_controller(); + vui_context_t *vui = vui_alloc(); + Uint32 ticks = SDL_GetTicks(); while (running) { + vui_reset(vui); + + int sync_btn = vui_draw_button(vui, 100, 100, 100, 100, "Sync"); + int connect_btn = vui_draw_button(vui, 300, 100, 100, 100, "Connect"); + SDL_Event event; #ifdef RASPBERRY_PI if (SDL_WaitEvent(&event)) { @@ -491,6 +494,18 @@ int main(int argc, const char **argv) running = 0; } break; + case SDL_MOUSEBUTTONDOWN: + { + int btn = vui_process_click(vui, event.button.x, event.button.y); + if (btn != -1) { + if (btn == sync_btn) { + // Go to sync menu + } else if (btn == connect_btn) { + // Go to connect menu + } + } + break; + } } if (controller) { @@ -518,6 +533,8 @@ int main(int argc, const char **argv) #endif } + vui_free(vui); + // Terminate background threads and wait for them to end gracefully vanilla_stop(); diff --git a/rpi/ui.c b/rpi/ui.c new file mode 100644 index 0000000..fbc93e0 --- /dev/null +++ b/rpi/ui.c @@ -0,0 +1,73 @@ +#include "ui.h" + +#include +#include + +#define MAX_BUTTON_COUNT 8 +#define MAX_BUTTON_TEXT 16 + +typedef struct { + int x; + int y; + int w; + int h; + char text[MAX_BUTTON_TEXT]; +} vui_button_t; + +typedef struct vui_context_t { + vui_button_t buttons[MAX_BUTTON_COUNT]; + int button_count; +} vui_context_t; + +vui_context_t *vui_alloc() +{ + return malloc(sizeof(vui_context_t)); +} + +void vui_free(vui_context_t *vui) +{ + vui_reset(vui); + free(vui); +} + +int vui_reset(vui_context_t *ctx) +{ + ctx->button_count = 0; +} + +int vui_draw_button(vui_context_t *ctx, int x, int y, int w, int h, const char *text) +{ + if (ctx->button_count == MAX_BUTTON_COUNT) { + return -1; + } + + int index = ctx->button_count; + + vui_button_t *btn = &ctx->buttons[index]; + + btn->x = x; + btn->y = y; + btn->w = w; + btn->h = h; + + // Copy text (enforce null terminator) + strncpy(btn->text, text, MAX_BUTTON_TEXT); + btn->text[MAX_BUTTON_TEXT-1] = 0; + + ctx->button_count++; + + return index; +} + +int vui_process_click(vui_context_t *ctx, int x, int y) +{ + for (int i = 0; i < ctx->button_count; i++) { + vui_button_t *btn = &ctx->buttons[i]; + + if (x >= btn->x && y >= btn->y && x < btn->x + btn->w && y < btn->y + btn->h) { + return i; + } + } + + return -1; +} \ No newline at end of file diff --git a/rpi/ui.h b/rpi/ui.h new file mode 100644 index 0000000..6e9f9da --- /dev/null +++ b/rpi/ui.h @@ -0,0 +1,12 @@ +#ifndef VANILLA_PI_UI_H +#define VANILLA_PI_UI_H + +typedef struct vui_context_t vui_context_t; + +vui_context_t *vui_alloc(); +int vui_reset(vui_context_t *ctx); +int vui_draw_button(vui_context_t *ctx, int x, int y, int w, int h, const char *text); +int vui_process_click(vui_context_t *ctx, int x, int y); +void vui_free(vui_context_t *ctx); + +#endif // VANILLA_PI_UI_H \ No newline at end of file