aboutsummaryrefslogtreecommitdiff
path: root/src/frontend_pugl.c
blob: 994cce9ad9f9d9dda93bc591aee5c7a8d19992df (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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
 * 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 <time.h>

#if defined(__APPLE__)
// FIXME
#elif defined(_WIN32)
# include <windows.h>
#else
# include <X11/Xresource.h>
#endif

#include <pugl/pugl.h>

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

#include <d2tk/backend.h>

struct _d2tk_pugl_t {
	const d2tk_pugl_config_t *config;
	bool done;
	PuglView *view;
	d2tk_base_t *base;
	void *ctx;
};

static inline void
_d2tk_pugl_close(d2tk_pugl_t *dpugl)
{
	dpugl->done = true;
}

static inline void
_d2tk_pugl_expose(d2tk_pugl_t *dpugl)
{
	d2tk_base_t *base = dpugl->base;

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

	d2tk_base_pre(base);

	dpugl->config->expose(dpugl->config->data, w, h);

	d2tk_base_post(base);

	if(d2tk_base_get_again(base))
	{
		puglPostRedisplay(dpugl->view);
	}
}

static void
_d2tk_pugl_modifiers(d2tk_pugl_t *dpugl, unsigned state)
{
	d2tk_base_t *base = dpugl->base;

	d2tk_base_set_shift(base, state & PUGL_MOD_SHIFT ? true : false);
	d2tk_base_set_ctrl(base, state & PUGL_MOD_CTRL ? true : false);
	d2tk_base_set_alt(base, state & PUGL_MOD_ALT ? true : false);
}

static void
_d2tk_pugl_event_func(PuglView *view, const PuglEvent *e)
{
	d2tk_pugl_t *dpugl = puglGetHandle(view);
	d2tk_base_t *base = dpugl->base;

	switch(e->type)
	{
		case PUGL_CONFIGURE:
		{
			d2tk_base_set_dimensions(base, e->configure.width, e->configure.height);
			puglPostRedisplay(dpugl->view);
		}	break;
		case PUGL_EXPOSE:
		{
			_d2tk_pugl_expose(dpugl);
		}	break;
		case PUGL_CLOSE:
		{
			_d2tk_pugl_close(dpugl);
		}	break;

		case PUGL_FOCUS_IN:
			// fall-through
		case PUGL_FOCUS_OUT:
		{
			puglPostRedisplay(dpugl->view);
		} break;

		case PUGL_ENTER_NOTIFY:
		case PUGL_LEAVE_NOTIFY:
		{
			_d2tk_pugl_modifiers(dpugl, e->crossing.state);
			d2tk_base_set_mouse_pos(base, e->crossing.x, e->crossing.y);

			puglPostRedisplay(dpugl->view);
		} break;

		case PUGL_BUTTON_PRESS:
		case PUGL_BUTTON_RELEASE:
		{
			_d2tk_pugl_modifiers(dpugl, e->button.state);
			d2tk_base_set_mouse_pos(base, e->button.x, e->button.y);

			switch(e->button.button)
			{
				case 3:
				{
					d2tk_base_set_mouse_r(base, e->type == PUGL_BUTTON_PRESS);
				} break;
				case 2:
				{
					d2tk_base_set_mouse_m(base, e->type == PUGL_BUTTON_PRESS);
				} break;
				case 1:
					// fall-through
				default:
				{
					d2tk_base_set_mouse_l(base, e->type == PUGL_BUTTON_PRESS);
				} break;
			}

			puglPostRedisplay(dpugl->view);
		} break;

		case PUGL_MOTION_NOTIFY:
		{
			_d2tk_pugl_modifiers(dpugl, e->motion.state);
			d2tk_base_set_mouse_pos(base, e->motion.x, e->motion.y);

			puglPostRedisplay(dpugl->view);
		} break;

		case PUGL_SCROLL:
		{
			_d2tk_pugl_modifiers(dpugl, e->scroll.state);
			d2tk_base_set_mouse_pos(base, e->scroll.x, e->scroll.y);
			d2tk_base_add_mouse_scroll(base, e->scroll.dx, e->scroll.dy);

			puglPostRedisplay(dpugl->view);
		} break;

		case PUGL_KEY_PRESS:
		{
			_d2tk_pugl_modifiers(dpugl, e->key.state);

			if(e->key.special)
			{
				bool handled = false;

				switch(e->key.special)
				{
					case PUGL_KEY_LEFT:
					{
						d2tk_base_set_left(base, true);
						handled = true;
					} break;
					case PUGL_KEY_RIGHT:
					{
						d2tk_base_set_right(base, true);
						handled = true;
					} break;
					case PUGL_KEY_UP:
					{
						d2tk_base_set_up(base, true);
						handled = true;
					} break;
					case PUGL_KEY_DOWN:
					{
						d2tk_base_set_down(base, true);
						handled = true;
					} break;
					case PUGL_KEY_SHIFT:
					{
						d2tk_base_set_shift(base, true);
						handled = true;
					} break;
					case PUGL_KEY_CTRL:
					{
						d2tk_base_set_ctrl(base, true);
						handled = true;
					} break;
					case PUGL_KEY_ALT:
					{
						d2tk_base_set_alt(base, true);
						handled = true;
					} break;
					default:
					{
						// nothing
					} break;
				}

				if(handled)
				{
					puglPostRedisplay(dpugl->view);
				}
			}
			else
			{
				const char character = d2tk_base_get_ctrl(base)
					? e->key.character | 0x60
					: e->key.utf8[0];
				d2tk_base_append_char(base, character);
				puglPostRedisplay(dpugl->view);
			}
		} break;
		case PUGL_KEY_RELEASE:
		{
			_d2tk_pugl_modifiers(dpugl, e->key.state);

			if(e->key.special)
			{
				bool handled = false;

				switch(e->key.special)
				{
					case PUGL_KEY_LEFT:
					{
						d2tk_base_set_left(base, false);
						handled = true;
					} break;
					case PUGL_KEY_RIGHT:
					{
						d2tk_base_set_right(base, false);
						handled = true;
					} break;
					case PUGL_KEY_UP:
					{
						d2tk_base_set_up(base, false);
						handled = true;
					} break;
					case PUGL_KEY_DOWN:
					{
						d2tk_base_set_down(base, false);
						handled = true;
					} break;
					case PUGL_KEY_SHIFT:
					{
						d2tk_base_set_shift(base, false);
						handled = true;
					} break;
					case PUGL_KEY_CTRL:
					{
						d2tk_base_set_ctrl(base, false);
						handled = true;
					} break;
					case PUGL_KEY_ALT:
					{
						d2tk_base_set_alt(base, false);
						handled = true;
					} break;
					default:
					{
						// nothing
					} break;
				}

				if(handled)
				{
					puglPostRedisplay(dpugl->view);
				}
			}
			else
			{
				puglPostRedisplay(dpugl->view);
			}
		} break;
		case PUGL_NOTHING:
		{
			// nothing
		}	break;
	}
}

D2TK_API int
d2tk_pugl_step(d2tk_pugl_t *dpugl)
{
	const PuglStatus stat = puglProcessEvents(dpugl->view);
	(void)stat;

	return dpugl->done;
}

//#define D2TK_PUGL_POLLING

D2TK_API void
d2tk_pugl_run(d2tk_pugl_t *dpugl, const sig_atomic_t *done)
{
#if !defined(D2TK_PUGL_POLLING) && !defined(__APPLE__) && !defined(_WIN32)
	const unsigned step = 1000000000 / 24;
	struct timespec to;
	clock_gettime(CLOCK_MONOTONIC, &to);
#endif

	while(!*done)
	{
#if !defined(D2TK_PUGL_POLLING) && !defined(__APPLE__) && !defined(_WIN32)
		if(clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &to, NULL))
		{
			continue;
		}

		to.tv_nsec += step;
		while(to.tv_nsec >= 1000000000)
		{
			to.tv_sec += 1;
			to.tv_nsec -= 1000000000;
		}

#else
		puglWaitForEvent(dpugl->view);
#endif

		if(d2tk_pugl_step(dpugl))
		{
			break;
		}
	}
}

D2TK_API void
d2tk_pugl_free(d2tk_pugl_t *dpugl)
{
	if(dpugl->ctx)
	{
		puglEnterContext(dpugl->view);
		if(dpugl->base)
		{
			d2tk_base_free(dpugl->base);
		}
		d2tk_core_driver.free(dpugl->ctx);
		puglLeaveContext(dpugl->view, false);
	}

	if(dpugl->view)
	{
		if(puglGetVisible(dpugl->view))
		{
			puglHideWindow(dpugl->view);
		}
		puglDestroy(dpugl->view);
	}

	free(dpugl);
}

D2TK_API float
d2tk_pugl_get_scale()
{
	const char *D2TK_SCALE = getenv("D2TK_SCALE");
	const float scale = D2TK_SCALE ? atof(D2TK_SCALE) : 1.f;
	const float dpi0 = 96.f; // reference DPI we're designing for
	float dpi1 = dpi0;

#if defined(__APPLE__)
	// FIXME
#elif defined(_WIN32)
	// GetDpiForSystem/Monitor/Window is Win10 only
	HDC screen = GetDC(NULL);
	dpi1 = GetDeviceCaps(screen, LOGPIXELSX);
	ReleaseDC(NULL, screen);
#else
	Display *disp = XOpenDisplay(0);
	if(disp)
	{
		// modern X actually lies here, but proprietary nvidia
		dpi1 = XDisplayWidth(disp, 0) * 25.4f / XDisplayWidthMM(disp, 0);

		// read DPI from users's ~/.Xresources
		char *resource_string = XResourceManagerString(disp);
		XrmInitialize();
		if(resource_string)
		{
			XrmDatabase db = XrmGetStringDatabase(resource_string);
			if(db)
			{
				char *type = NULL;
				XrmValue value;

				XrmGetResource(db, "Xft.dpi", "String", &type, &value);
				if(value.addr)
				{
					dpi1 = atof(value.addr);
				}
			}
		}

		XCloseDisplay(disp);
	}
#endif

	return scale * dpi1 / dpi0;
}

D2TK_API d2tk_pugl_t *
d2tk_pugl_new(const d2tk_pugl_config_t *config, uintptr_t *widget)
{
	d2tk_pugl_t *dpugl = calloc(1, sizeof(d2tk_pugl_t));
	if(!dpugl)
	{
		goto fail;
	}

	dpugl->config = config;

	dpugl->view = puglInit(NULL, NULL);
	if(!dpugl->view)
	{
		fprintf(stderr, "puglInit failed\n");
		goto fail;
	}

	puglInitWindowClass(dpugl->view, "d2tk");
	puglInitWindowSize(dpugl->view, config->w, config->h);
	if(config->min_w && config->min_h)
	{
		puglInitWindowMinSize(dpugl->view, config->min_w, config->min_h);
	}
	if(config->parent)
	{
		puglInitWindowParent(dpugl->view, config->parent);
		puglInitTransientFor(dpugl->view, config->parent);
	}
	if(config->fixed_aspect)
	{
		puglInitWindowAspectRatio(dpugl->view, config->w, config->h,
			config->w, config->h);
	}
	puglInitResizable(dpugl->view, !config->fixed_size);
	puglSetHandle(dpugl->view, dpugl);
	puglSetEventFunc(dpugl->view, _d2tk_pugl_event_func);

#if defined(PUGL_HAVE_CAIRO)
	puglInitContextType(dpugl->view, PUGL_CAIRO_GL);
#else
	puglInitContextType(dpugl->view, PUGL_GL);
#endif
	const int stat = puglCreateWindow(dpugl->view, "d2tk");

	if(stat != 0)
	{
		fprintf(stderr, "puglCreateWindow failed\n");
		goto fail;
	}
	puglShowWindow(dpugl->view);

	if(widget)
	{
		*widget = puglGetNativeWindow(dpugl->view);
	}

#if defined(_WIN32)
	void *ctx = NULL;
#else
	puglEnterContext(dpugl->view);
	void *ctx = puglGetContext(dpugl->view);
#endif
	dpugl->ctx = d2tk_core_driver.new(config->bundle_path, ctx);
	puglLeaveContext(dpugl->view, false);

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

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

	d2tk_base_set_dimensions(dpugl->base, config->w, config->h);

	return dpugl;

fail:
	if(dpugl)
	{
		if(dpugl->view)
		{
			if(dpugl->ctx)
			{
				puglEnterContext(dpugl->view);
				d2tk_core_driver.free(dpugl->ctx);
				puglLeaveContext(dpugl->view, false);
			}

			puglDestroy(dpugl->view);
		}

		free(dpugl);
	}

	return NULL;
}

D2TK_API void
d2tk_pugl_redisplay(d2tk_pugl_t *dpugl)
{
	puglPostRedisplay(dpugl->view);
}

D2TK_API int
d2tk_pugl_resize(d2tk_pugl_t *dpugl, d2tk_coord_t w, d2tk_coord_t h)
{
	d2tk_base_set_dimensions(dpugl->base, w, h);
	puglPostRedisplay(dpugl->view);

	return 0;
}

D2TK_API d2tk_base_t *
d2tk_pugl_get_base(d2tk_pugl_t *dpugl)
{
	return dpugl->base;
}