aboutsummaryrefslogtreecommitdiff
path: root/subprojects/d2tk/src/frontend_glfw.c
blob: 6950c00072ebe32e50b25e3cd6c96181e1f190e1 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
 * Copyright (c) 2018-2019 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#include <stdio.h>
#include <string.h>
#include <time.h>

#include "core_internal.h"
#include <d2tk/frontend_glfw.h>

#include <d2tk/backend.h>

#define GLFW_INCLUDE_ES3
#define GLFW_INCLUDE_GLEXT
#include <GLFW/glfw3.h>

struct _d2tk_frontend_t {
	const d2tk_glfw_config_t *config;
	int w;
	int h;
	GLFWwindow * window;
	d2tk_base_t *base;
	void *ctx;
};

static inline void
_d2tk_frontend_expose(d2tk_frontend_t *dglfw)
{
	d2tk_base_t *base = dglfw->base;

	d2tk_coord_t w;
	d2tk_coord_t h;
	d2tk_base_get_dimensions(base, &w, &h);

	if(d2tk_base_pre(base, NULL) == 0)
	{
		dglfw->config->expose(dglfw->config->data, w, h);

		d2tk_base_post(base);
	}
}

D2TK_API int
d2tk_frontend_poll(d2tk_frontend_t *dglfw, double timeout)
{
	d2tk_base_t *base = dglfw->base;
	int width;
	int height;

	if(timeout < 0.0)
	{
		glfwWaitEvents();
	}
	else if(timeout == 0.0)
	{
		glfwPollEvents();
	}
	else
	{
		glfwWaitEventsTimeout(timeout);
	}

	glfwGetFramebufferSize(dglfw->window, &width, &height);
	if( (dglfw->w != width) || (dglfw->h != height) )
	{
		dglfw->w = width;
		dglfw->h = height;

		d2tk_base_set_dimensions(base, dglfw->w, dglfw->h);
	}

	_d2tk_frontend_expose(dglfw);
	glfwSwapBuffers(dglfw->window);

	return 0;
}

D2TK_API int
d2tk_frontend_get_file_descriptors(d2tk_frontend_t *dglfw, int *fds, int numfds)
{
	return d2tk_base_get_file_descriptors(dglfw->base, fds, numfds);
}

D2TK_API int
d2tk_frontend_step(d2tk_frontend_t *dglfw)
{
	return d2tk_frontend_poll(dglfw, 0.0);
}

D2TK_API void
d2tk_frontend_run(d2tk_frontend_t *dglfw, const sig_atomic_t *done)
{
	while(!*done)
	{
		if(d2tk_frontend_poll(dglfw, -1.0))
		{
			break;
		}
	}
}

D2TK_API void
d2tk_frontend_free(d2tk_frontend_t *dglfw)
{
	glfwDestroyWindow(dglfw->window);
	free(dglfw);

	glfwTerminate();
}

D2TK_API float
d2tk_frontend_get_scale()
{
#if 0
	float xscale;
	float yscale;

	glfwGetWindowContentScale(dglfw->window, &xscale, &yscale);

	return xscale;
#else
	return 1.f;
#endif
}

static void
_d2tk_frontend_modifiers(d2tk_base_t *base, int mods)
{
	d2tk_base_set_modmask(base, D2TK_MODMASK_SHIFT,
		(mods & GLFW_MOD_SHIFT) ? true : false);
	d2tk_base_set_modmask(base, D2TK_MODMASK_CTRL,
		(mods & GLFW_MOD_CONTROL) ? true : false);
	d2tk_base_set_modmask(base, D2TK_MODMASK_ALT,
		(mods & GLFW_MOD_ALT) ? true : false);
}

static void
_d2tk_key(GLFWwindow *window, int key, int scancode, int action, int mods)
{
	d2tk_frontend_t *dglfw = glfwGetWindowUserPointer(window);
	d2tk_base_t *base = dglfw->base;
	const bool pressed = action == GLFW_PRESS;

	_d2tk_frontend_modifiers(base, mods);

	fprintf(stderr, "[%s] %i %i %i %i\n", __func__, key, scancode, action, mods);

	d2tk_keymask_t mask = D2TK_KEYMASK_NONE;
	unsigned int codepoint = 0;

	switch(key)
	{
		case GLFW_KEY_ENTER:
		{
			mask = D2TK_KEYMASK_ENTER;
			codepoint = '\n';
		} break; 
		case GLFW_KEY_TAB:
		{
			mask = D2TK_KEYMASK_TAB;
			codepoint = '\t';
		} break; 
		case GLFW_KEY_BACKSPACE:
		{
			mask = D2TK_KEYMASK_BACKSPACE;
			codepoint = '\b';
		} break; 
		case GLFW_KEY_ESCAPE:
		{
			mask = D2TK_KEYMASK_ESCAPE;
			codepoint = 0x1B;
		} break; 

		case GLFW_KEY_UP:
		{
			mask = D2TK_KEYMASK_UP;
		} break; 
		case GLFW_KEY_DOWN:
		{
			mask = D2TK_KEYMASK_DOWN;
		} break; 
		case GLFW_KEY_LEFT:
		{
			mask = D2TK_KEYMASK_LEFT;
		} break; 
		case GLFW_KEY_RIGHT:
		{
			mask = D2TK_KEYMASK_RIGHT;
		} break; 

		case GLFW_KEY_INSERT:
		{
			mask = D2TK_KEYMASK_INS;
		} break; 
		case GLFW_KEY_DELETE:
		{
			mask = D2TK_KEYMASK_DEL;
		} break; 
		case GLFW_KEY_HOME:
		{
			mask = D2TK_KEYMASK_HOME;
		} break; 
		case GLFW_KEY_END:
		{
			mask = D2TK_KEYMASK_END;
		} break; 
		case GLFW_KEY_PAGE_UP:
		{
			mask = D2TK_KEYMASK_PAGEUP;
		} break; 
		case GLFW_KEY_PAGE_DOWN:
		{
			mask = D2TK_KEYMASK_PAGEDOWN;
		} break; 
	}

	if(mask != D2TK_KEYMASK_NONE)
	{
		d2tk_base_set_keymask(base, mask, pressed);
	}

	if(codepoint && pressed)
	{
		d2tk_base_append_utf8(base, codepoint);
	}
}

static void
_d2tk_char(GLFWwindow *window, unsigned int codepoint)
{
	d2tk_frontend_t *dglfw = glfwGetWindowUserPointer(window);
	d2tk_base_t *base = dglfw->base;

	fprintf(stderr, "[%s] %u\n", __func__, codepoint);

	d2tk_base_append_utf8(base, codepoint);
}

static void
_d2tk_cursor_pos(GLFWwindow *window, double xpos, double ypos)
{
	d2tk_frontend_t *dglfw = glfwGetWindowUserPointer(window);
	d2tk_base_t *base = dglfw->base;

	d2tk_base_set_mouse_pos(base, xpos, ypos);
}

static void
_d2tk_cursor_enter(GLFWwindow *window, int entered)
{
	d2tk_frontend_t *dglfw = glfwGetWindowUserPointer(window);
	d2tk_base_t *base = dglfw->base;

	if(entered)
	{
		d2tk_base_set_full_refresh(base);
	}
}

static void
_d2tk_mouse_button(GLFWwindow *window, int button, int action, int mods)
{
	d2tk_frontend_t *dglfw = glfwGetWindowUserPointer(window);
	d2tk_base_t *base = dglfw->base;
	const bool pressed = action == GLFW_PRESS;

	_d2tk_frontend_modifiers(base, mods);

	switch(button)
	{
		case 3:
		{
			d2tk_base_set_butmask(base, D2TK_BUTMASK_RIGHT, pressed);
		} break;
		case 2:
		{
			d2tk_base_set_butmask(base, D2TK_BUTMASK_MIDDLE, pressed);
		} break;
		case 1:
			// fall-through
		default:
		{
			d2tk_base_set_butmask(base, D2TK_BUTMASK_LEFT, pressed);
		} break;
	}
}

static void
_d2tk_scroll(GLFWwindow *window, double xoffset, double yoffset)
{
	d2tk_frontend_t *dglfw = glfwGetWindowUserPointer(window);
	d2tk_base_t *base = dglfw->base;

	d2tk_base_add_mouse_scroll(base, xoffset, yoffset);
}

D2TK_API d2tk_frontend_t *
d2tk_glfw_new(const d2tk_glfw_config_t *config)
{
	if(glfwInit() != GLFW_TRUE)
	{
		return NULL;
	}

	glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);  
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);  
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

	d2tk_frontend_t *dglfw = calloc(1, sizeof(d2tk_frontend_t));
	if(!dglfw)
	{
		goto fail;
	}

	dglfw->config = config;

	dglfw->window = glfwCreateWindow(dglfw->config->w, dglfw->config->h, "d2tk",
		NULL, NULL);

	if(!dglfw->window)
	{
		goto fail;
	}

	glfwSetWindowUserPointer(dglfw->window, dglfw);

	glfwSetKeyCallback(dglfw->window, _d2tk_key);
	glfwSetCharCallback(dglfw->window, _d2tk_char);
	glfwSetCursorPosCallback(dglfw->window, _d2tk_cursor_pos);
	glfwSetCursorEnterCallback(dglfw->window, _d2tk_cursor_enter);
	glfwSetMouseButtonCallback(dglfw->window, _d2tk_mouse_button);
	glfwSetScrollCallback(dglfw->window, _d2tk_scroll);
	glfwMakeContextCurrent(dglfw->window);
	glfwSwapInterval(1);

	dglfw->ctx = d2tk_core_driver.new(dglfw->config->bundle_path);

	if(!dglfw->ctx)
	{
		goto fail;
	}

	dglfw->base = d2tk_base_new(&d2tk_core_driver, dglfw->ctx);
	if(!dglfw->base)
	{
		goto fail;
	}

	return dglfw;

fail:
	if(dglfw)
	{
		if(dglfw->window)
		{
			glfwDestroyWindow(dglfw->window);
		}

		free(dglfw);

		glfwTerminate();
	}

	return NULL;
}

D2TK_API void
d2tk_frontend_redisplay(d2tk_frontend_t *dglfw)
{
	(void)dglfw;
	//FIXME
}

D2TK_API int
d2tk_frontend_set_size(d2tk_frontend_t *dglfw, d2tk_coord_t w, d2tk_coord_t h)
{
	d2tk_base_set_dimensions(dglfw->base, w, h);
	d2tk_frontend_redisplay(dglfw);

	return 0;
}

D2TK_API int
d2tk_frontend_get_size(d2tk_frontend_t *dglfw, d2tk_coord_t *w, d2tk_coord_t *h)
{
	int width;
	int height;

	glfwGetWindowSize(dglfw->window, &width, &height);

	*w = width;
	*h = height;

	return 0;
}

D2TK_API d2tk_base_t *
d2tk_frontend_get_base(d2tk_frontend_t *dglfw)
{
	return dglfw->base;
}

D2TK_API int
d2tk_frontend_set_clipboard(d2tk_frontend_t *dpugl, const char *type,
	const void *buf, size_t buf_len)
{
	(void)dpugl;
	(void)type;
	(void)buf;
	(void)buf_len;
	return 1; //FIXME
}

D2TK_API const void *
d2tk_frontend_get_clipboard(d2tk_frontend_t *dpugl, const char **type,
	size_t *buf_len)
{
	(void)dpugl;
	(void)type;
	(void)buf_len;
	return NULL; //FIXME
}