diff options
| author | Abhinav Prasai <abhinav.prsai@gmail.com> | 2025-11-23 02:16:17 +0000 |
|---|---|---|
| committer | Abhinav Prasai <abhinav.prsai@gmail.com> | 2025-11-23 02:16:17 +0000 |
| commit | 5d5f1c0ad39481cd11960c0f25c1d7c5318fef6d (patch) | |
| tree | aa66cb374ab002f650cc4dfd4e18b7f13ac75395 /src | |
| parent | 3ff5fe71a7c5cba9d4f4706ee0364503d25fefe5 (diff) | |
add engine (no webkit yet, only tab support)
Diffstat (limited to 'src')
| -rw-r--r-- | src/engine.c | 110 | ||||
| -rw-r--r-- | src/quark.c | 116 | ||||
| -rw-r--r-- | src/sdl.c | 2 |
3 files changed, 204 insertions, 24 deletions
diff --git a/src/engine.c b/src/engine.c new file mode 100644 index 0000000..bf6ed35 --- /dev/null +++ b/src/engine.c @@ -0,0 +1,110 @@ +#include <stdbool.h> +#include <stdlib.h> + +#include "engine.h" +#include "quark.h" + +static quark_tab_t tabs[MAX_TABS]; +static int tab_count = 0; +static int current_tab = -1; + +/* TODO: actually implement webkit */ + +int engine_get_current_index(void) +{ + /* fetch private */ + return current_tab; +} + +int engine_get_tab_count(void) +{ + /* fetch private */ + return tab_count; +} + +bool engine_init(void) +{ + LOG_PASS("engine: init"); + return true; +} + +void engine_load_url(int id, const char* url) +{ + if (id < 0 || id >= tab_count) + return; + + free(tabs[id].url); + tabs[id].url = SDL_strdup(url); + tabs[id].loading = true; + + LOG_INFO("engine: url=%s loaded to tab=%d", url, id); +} + +void engine_shutdown(void) +{ + LOG_INFO("engine: shutting down"); + for (int i = 0; i < tab_count; i++) { + free(tabs[i].url); + free(tabs[i].title); + } +} + +void engine_tab_close(int id) +{ + if (id < 0 || id >= tab_count) + return; + + LOG_INFO("engine: closed tab %d", id); + + free(tabs[id].url); + free(tabs[id].title); + + /* collapse */ + for (int i = id; i < tab_count-1; i++) + tabs[i] = tabs[i+1]; + + tab_count--; + if (current_tab >= tab_count) + current_tab = tab_count - 1; +} + +quark_tab_t* engine_tab_current(void) +{ + for (int i = 0; i < tab_count; i++) + if (i == current_tab) + return &tabs[i]; + + return NULL; +} + +int engine_tab_new(const char* url) +{ + if (tab_count >= MAX_TABS) { + LOG_WARN("engine: maximum number of tabs reached"); + return -1; + } + + quark_tab_t* t = &tabs[tab_count]; + t->url = SDL_strdup(url ? url : "about_blank"); + t->title = SDL_strdup("new tab"); /* TODO: customise */ + t->loading = true; + + current_tab = tab_count; + tab_count++; + + LOG_INFO("engine: created new tab: id=%d url=%s", current_tab, url); + return current_tab; +} + +void engine_tab_switch(int id) +{ + if (id < 0 || id >= tab_count) + return; + current_tab = id; + LOG_INFO("engine: switched to tab %d", id); +} + +void engine_update(void) +{ + /* nothing here */ +} diff --git a/src/quark.c b/src/quark.c index ed98a83..f850bd5 100644 --- a/src/quark.c +++ b/src/quark.c @@ -4,84 +4,154 @@ #include <SDL2/SDL_opengles2.h> #include <stdlib.h> +#include "engine.h" #include "quark.h" +#include "render.h" #include "sdl.h" quark_t quark; +void init(void); void quit(void); void render_tmp(void); void run(void); -void setup(void); void quit(void) { - LOG_INFO("quitting quark..."); + engine_shutdown(); + render_shutdown(); sdl_quit(); + LOG_INFO("quark: quitting"); } void render_tmp(void) { - glViewport(0, 0, quark.win_w, quark.win_h); + render_begin(quark.win_w, quark.win_h); - glClearColor(0.0, 0.0, 0.0, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); + /* top bar */ + render_rect(0, 0, quark.win_w, 32, 0.15f, 0.15f, 0.22f, 1.0f); - glEnable(GL_SCISSOR_TEST); - int bar_h = 32; - glScissor(0, quark.win_h - bar_h, quark.win_w, bar_h); - glClearColor(0.25, 0.25, 0.30, 1.0); - glClear(GL_COLOR_BUFFER_BIT); - glDisable(GL_SCISSOR_TEST); + /* number of tabs changes color */ + int n = engine_get_tab_count(); + int cur = engine_get_current_index(); - SDL_GL_SwapWindow(quark.win); + float intensity = 0.2f + 0.05f * (float)n; + if (intensity > 1.0f) + intensity = 1.0f; + + /* tab indicator */ + render_rect(10, 8, 100, 16, intensity, (cur >= 0 ? 0.5f : 0.2f), 0.2f, 1.0f); + + render_end(quark.win); } void run(void) { quark.running = true; - LOG_INFO("starting quark..."); + LOG_INFO("quark: starting"); SDL_Event ev; while (quark.running) { + /* TODO: add event handler + this is just test until WebKit implemented */ while (SDL_PollEvent(&ev)) { if (ev.type == SDL_QUIT) quark.running = false; - if (ev.type == SDL_WINDOWEVENT_RESIZED) { - quark.win_w = ev.window.data1; - quark.win_h = ev.window.data2; + if (ev.type == SDL_WINDOWEVENT) { + if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { + quark.win_w = ev.window.data1; + quark.win_h = ev.window.data2; + } + } + + if (ev.type == SDL_KEYDOWN) { + SDL_Keycode key = ev.key.keysym.sym; + SDL_Keymod mods = ev.key.keysym.mod; + + /* C-t: new tab */ + if ((mods & KMOD_CTRL) && key == SDLK_t) { + int id = engine_tab_new("about:blank"); + if (id >= 0) { + LOG_INFO("quark: new tab %d", id); + engine_tab_switch(id); + } + } + + /* C-w: close current tab */ + if ((mods & KMOD_CTRL) && key == SDLK_w) { + quark_tab_t* cur = engine_tab_current(); + if (cur) { + LOG_INFO("quark: closing current tab"); + engine_tab_close(engine_get_current_index()); + } + } + + /* C-t: switch tab */ + if ((mods & KMOD_CTRL) && key == SDLK_TAB) { + int n = engine_get_tab_count(); + int cur = engine_get_current_index(); + if (n > 0 && cur >= 0) { + int next = (cur + 1) % n; + engine_tab_switch(next); + } + } } } + engine_update(); render_tmp(); SDL_Delay(10); } } -void setup(void) +void init(void) { sdl_init(); quark.win = sdl_create_window("quark", 800, 600); if (!quark.win) { - LOG_ERROR("failed to create quark window"); + LOG_ERROR("sdl: failed to create quark window"); exit(EXIT_FAILURE); } else { - LOG_PASS("created SDL window"); + LOG_PASS("sdl: created SDL window"); } quark.win_w = 800; quark.win_h = 600; - /* create OpenGL context */ + /* init systems */ quark.gl = SDL_GL_CreateContext(quark.win); if (!quark.gl) { - LOG_ERROR("failed to create GL context"); + LOG_ERROR("opengl: failed to create GL context"); + exit(EXIT_FAILURE); + } + else { + LOG_PASS("opengl: created OpenGL ES context"); + } + + if (!render_init()) { + LOG_ERROR("renderer: failed to initialise renderer"); + exit(EXIT_FAILURE); + } + else { + LOG_PASS("renderer: initialised renderer "); + } + + if (!engine_init()) { + LOG_ERROR("engine: failed to init engine"); + exit(EXIT_FAILURE); + } + else { + LOG_ERROR("engine: initialised engine"); + } + + /* starting tab */ + if (engine_tab_new("about:blank") < 0) { + LOG_ERROR("engine: failed to create initial tab"); exit(EXIT_FAILURE); } else { - LOG_PASS("created OpenGL ES context"); + LOG_PASS("engine: created initial tab"); } } @@ -90,7 +160,7 @@ int main(int argc, char** argv) (void) argc; (void) argv; - setup(); + init(); run(); quit(); @@ -13,7 +13,7 @@ SDL_Window* sdl_create_window(const char* title, int w, int h) bool sdl_init(void) { if (SDL_Init(SDL_INIT_VIDEO) != 0) { - LOG_ERROR("SDL_Init failed: %s", SDL_GetError()); + LOG_ERROR("sdl: SDL_Init failed: %s", SDL_GetError()); return false; } |
