summaryrefslogtreecommitdiff
path: root/src/quartz.c
blob: 8c7d92e56dc5592288ca58aa5323134158fa37b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdbool.h>

#include <SDL2/SDL.h>
#include <SDL2/SDL_opengles2.h>
#include <stdlib.h>

#include "engine.h"
#include "quartz.h"
#include "render.h"
#include "sdl.h"

quartz_t quartz;

void init(void);
void quit(void);
void render_tmp(void);
void run(void);

void quit(void)
{
	engine_shutdown();
	render_shutdown();
	sdl_quit();
	LOG_INFO("quartz: quitting");
}

void render_tmp(void)
{
	render_begin(quartz.win_w, quartz.win_h);

	/* top bar */
	render_rect(0, 0, quartz.win_w, 32, 0.15f, 0.15f, 0.22f, 1.0f);

	/* number of tabs changes color */
	int n = engine_get_tab_count();
	int cur = engine_get_current_index();

	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(quartz.win);
}

void run(void)
{
	quartz.running = true;
	LOG_INFO("quartz: starting");

	SDL_Event ev;

	while (quartz.running) {
		/* TODO: add event handler + this is just test until WebKit implemented */
		while (SDL_PollEvent(&ev)) {
			if (ev.type == SDL_QUIT)
				quartz.running = false;

			if (ev.type == SDL_WINDOWEVENT) {
				if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
					quartz.win_w = ev.window.data1;
					quartz.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("quartz: new tab %d", id);
						engine_tab_switch(id);
					}
				}

				/* C-w: close current tab */
				if ((mods & KMOD_CTRL) && key == SDLK_w) {
					quartz_tab_t* cur = engine_tab_current();
					if (cur) {
						LOG_INFO("quartz: 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 init(void)
{
	sdl_init();
	quartz.win = sdl_create_window("quartz", 800, 600);
	if (!quartz.win) {
		LOG_ERROR("sdl: failed to create quartz window");
		exit(EXIT_FAILURE);
	}
	else {
		LOG_PASS("sdl: created SDL window");
	}
	quartz.win_w = 800;
	quartz.win_h = 600;

	/* init systems */
	quartz.gl = SDL_GL_CreateContext(quartz.win);
	if (!quartz.gl) {
		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("engine: created initial tab");
	}
}

int main(int argc, char** argv)
{
	(void) argc;
	(void) argv;

	init();
	run();
	quit();

	return EXIT_SUCCESS;
}