summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--include/engine.h22
-rw-r--r--src/engine.c110
-rw-r--r--src/quark.c116
-rw-r--r--src/sdl.c2
5 files changed, 228 insertions, 26 deletions
diff --git a/Makefile b/Makefile
index 9a92cd0..a6e197f 100644
--- a/Makefile
+++ b/Makefile
@@ -5,8 +5,8 @@ INCLUDE = -Iinclude
CFLAGS = -std=c99 -O2 -Wall -Wextra $(INCLUDE)
LDFLAGS = $(DEPS)
-SRC = src/quark.c src/sdl.c
-OBJ = build/quark.o build/sdl.o
+SRC = src/quark.c src/sdl.c src/render.c src/engine.c
+OBJ = build/quark.o build/sdl.o build/render.o build/engine.o
all: quark
diff --git a/include/engine.h b/include/engine.h
new file mode 100644
index 0000000..6228465
--- /dev/null
+++ b/include/engine.h
@@ -0,0 +1,22 @@
+#pragma once
+
+#include <stdbool.h>
+
+#define MAX_TABS 32 /* TODO: move to config */
+
+typedef struct {
+ char* url;
+ char* title;
+ bool loading;
+} quark_tab_t;
+
+int engine_get_current_index(void);
+int engine_get_tab_count(void);
+bool engine_init(void);
+void engine_load_url(int id, const char* url);
+void engine_shutdown(void);
+void engine_tab_close(int id);
+quark_tab_t* engine_tab_current(void);
+int engine_tab_new(const char* url);
+void engine_tab_switch(int id);
+void engine_update(void);
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();
diff --git a/src/sdl.c b/src/sdl.c
index 4614969..03821a0 100644
--- a/src/sdl.c
+++ b/src/sdl.c
@@ -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;
}