-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathui_gl.c
1797 lines (1566 loc) · 48.8 KB
/
ui_gl.c
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* meters.lv2 openGL frontend
*
* Copyright (C) 2013-2016 Robin Gareus <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//#undef HAVE_IDLE_IFACE // simulate old LV2
//#define USE_GUI_THREAD // use own thread (not idle callback), should be preferred even w/idle availale on most platforms
//#define THREADSYNC // wake up GUI thread on port-event
#ifdef XTERNAL_UI
#if defined USE_GUI_THREAD && defined _WIN32
# define INIT_PUGL_IN_THREAD
#endif
#endif
//#define TIMED_RESHAPE // resize view when idle
//#define DEBUG_RESIZE
//#define DEBUG_EXPOSURE
//#define VISIBLE_EXPOSE
//#define DEBUG_UI
/* either USE_GUI_THREAD or HAVE_IDLE_IFACE needs to be defined.
*
* if both are defined:
* the goniometer window can change size by itself..
*
* IFF HAVE_IDLE_IFACE is available we can use it to
* resize the suil-swallowed window (gtk host only), by
* making a call to gtk_window_resize of the gtk_widget_get_toplevel
* in the thread-context of the main application.
*
* without USE_GUI_THREAD but with HAVE_IDLE_IFACE:
* All plugins run single threaded in the host's process context.
*
* That will avoid various threading issues - particularly with
* libcairo < 1.12.10, libpixman < 0.30.2 and libpango < 1.32.6
* which are not threadsafe)
*
* The plugin UI launching its own thread yields generally better
* performance (the host's idle call's timing is inaccurate and
* using the idle interface will also slow down the host's UI...
*/
#if (!defined HAVE_IDLE_IFACE && !defined USE_GUI_THREAD)
#error At least one of HAVE_IDLE_IFACE or USE_GUI_THREAD must be defined.
#endif
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <pthread.h>
#include <assert.h>
#include "pugl/pugl.h"
#ifdef __APPLE__
#include "OpenGL/glu.h"
#else
#include <GL/glu.h>
#endif
#ifndef GL_BGRA
#define GL_BGRA 0x80E1 // GL_BGRA_EXT
#endif
#ifndef GL_RGBA8
#define GL_RGBA8 GL_RGBA
#endif
#ifndef GL_TEXTURE_RECTANGLE_ARB
#define GL_TEXTURE_RECTANGLE_ARB 0x84F5
#endif
#ifdef USE_GTK_RESIZE_HACK
#include <gtk/gtk.h>
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define ROBTK_MOD_SHIFT PUGL_MOD_SHIFT
#define ROBTK_MOD_CTRL PUGL_MOD_CTRL
#include "gl/posringbuf.h"
#include "robtk.h"
#ifdef WITH_SIGNATURE
# include "gpg_init.c"
#endif
static void opengl_init () {
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glDisable (GL_DEPTH_TEST);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable (GL_TEXTURE_RECTANGLE_ARB);
}
static void opengl_draw (int width, int height, unsigned char* surf_data, unsigned int texture_id) {
if (!surf_data) { return; }
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix ();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture_id);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8,
width, height, /*border*/ 0,
GL_BGRA, GL_UNSIGNED_BYTE, surf_data);
glBegin(GL_QUADS);
glTexCoord2f( 0.0f, (GLfloat) height);
glVertex2f(-1.0f, -1.0f);
glTexCoord2f((GLfloat) width, (GLfloat) height);
glVertex2f( 1.0f, -1.0f);
glTexCoord2f((GLfloat) width, 0.0f);
glVertex2f( 1.0f, 1.0f);
glTexCoord2f( 0.0f, 0.0f);
glVertex2f(-1.0f, 1.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
static void opengl_reallocate_texture (int width, int height, unsigned int* texture_id) {
glViewport (0, 0, width, height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
glClear (GL_COLOR_BUFFER_BIT);
glDeleteTextures (1, texture_id);
glGenTextures (1, texture_id);
glBindTexture (GL_TEXTURE_RECTANGLE_ARB, *texture_id);
glTexImage2D (GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8,
width, height, 0,
GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
}
static cairo_t* opengl_create_cairo_t (int width, int height, cairo_surface_t** surface, unsigned char** buffer)
{
cairo_t* cr;
const int bpp = 4;
*buffer = (unsigned char*) calloc (bpp * width * height, sizeof (unsigned char));
if (!*buffer) {
fprintf (stderr, "robtk: opengl surface out of memory.\n");
return NULL;
}
*surface = cairo_image_surface_create_for_data (*buffer,
CAIRO_FORMAT_ARGB32, width, height, bpp * width);
if (cairo_surface_status (*surface) != CAIRO_STATUS_SUCCESS) {
free (*buffer);
fprintf (stderr, "robtk: failed to create cairo surface\n");
return NULL;
}
cr = cairo_create (*surface);
if (cairo_status (cr) != CAIRO_STATUS_SUCCESS) {
free (*buffer);
fprintf (stderr, "robtk: cannot create cairo context\n");
return NULL;
}
return cr;
}
/*****************************************************************************/
#include "gl/xternalui.h"
typedef struct {
PuglView* view;
LV2UI_Resize* resize;
LV2UI_Write_Function write; // unused for now
LV2UI_Controller controller; // unused for now
PuglNativeWindow parent; // only valud during init
bool ontop;
unsigned long transient_id; // X11 window ID
struct lv2_external_ui_host *extui;
struct lv2_external_ui xternal_ui;
int width;
int height;
int xoff;
int yoff;
float xyscale;
bool gl_initialized;
#ifdef WITH_SIGNATURE
bool gpg_verified;
char gpg_data[128];
float gpg_shade;
#endif
#ifdef INIT_PUGL_IN_THREAD
int ui_initialized;
#endif
bool resize_in_progress;
bool resize_toplevel;
#ifdef USE_GUI_THREAD
int ui_queue_puglXWindow;
pthread_t thread;
int exit;
#endif
#ifdef TIMED_RESHAPE
uint64_t queue_reshape;
int queue_w;
int queue_h;
#else
bool relayout;
#endif
cairo_t* cr;
cairo_surface_t* surface;
unsigned char* surf_data;
#if __BIG_ENDIAN__
unsigned char* surf_data_be;
#endif
unsigned int texture_id;
/* top-level */
RobWidget *tl;
LV2UI_Handle ui;
/* toolkit state stuff */
volatile cairo_rectangle_t expose_area;
RobWidget *mousefocus;
RobWidget *mousehover;
posringbuf *rb;
#if (defined USE_GUI_THREAD && defined HAVE_IDLE_IFACE)
bool do_the_funky_resize;
#endif
bool queue_canvas_realloc;
void (* ui_closed)(void* controller);
bool close_ui; // used by xternalui
#if (defined USE_GUI_THREAD && defined THREADSYNC)
pthread_mutex_t msg_thread_lock;
pthread_cond_t data_ready;
#endif
void (* expose_overlay)(RobWidget* toplevel, cairo_t* cr, cairo_rectangle_t *ev);
float queue_widget_scale;
} GLrobtkLV2UI;
static const char * robtk_info(void *h) {
#ifdef WITH_SIGNATURE
GLrobtkLV2UI * self = (GLrobtkLV2UI*) h;
return self->gpg_data;
#else
return "v" VERSION;
#endif
}
static void robtk_close_self(void *h) {
GLrobtkLV2UI * self = (GLrobtkLV2UI*) h;
if (self->ui_closed) {
self->ui_closed(self);
}
}
static int robtk_open_file_dialog(void *h, const char *title) {
GLrobtkLV2UI * self = (GLrobtkLV2UI*) h;
return puglOpenFileDialog(self->view, title);
}
/*****************************************************************************/
#include PLUGIN_SOURCE
/*****************************************************************************/
#ifdef WITH_SIGNATURE
#include WITH_SIGNATURE
#endif
#ifdef ROBTKAPP
static const void*
extension_data(const char* uri)
{
return NULL;
}
static void
port_event(LV2UI_Handle handle,
uint32_t port_index,
uint32_t buffer_size,
uint32_t format,
const void* buffer)
{
}
#endif
/*****************************************************************************/
#include "gl/xternalui.c"
static void reallocate_canvas(GLrobtkLV2UI* self);
/*****************************************************************************/
/* RobWidget implementation & glue */
typedef struct {
RobWidget *rw;
cairo_rectangle_t a;
} RWArea;
#ifdef WITH_SIGNATURE
static void lc_expose (GLrobtkLV2UI * self) {
#ifdef ROBTK_UPSCALE
#if __BIG_ENDIAN__
float hw_scale = 1.0;
#else
float hw_scale = puglGetHWSurfaceScale(self->view);
#endif
#endif
assert (self->tl);
if (!self->tl) { return; }
cairo_rectangle_t expose_area;
posrb_read_clear(self->rb); // no fast-track
expose_area.x = expose_area.y = 0;
expose_area.width = self->width;
expose_area.height = self->height;
cairo_save(self->cr);
#ifdef ROBTK_UPSCALE
cairo_scale(self->cr, hw_scale, hw_scale);
#endif
self->tl->resized = TRUE; // full re-expose
self->tl->expose_event(self->tl, self->cr, &expose_area);
cairo_restore(self->cr);
expose_area.x = expose_area.y = 0;
expose_area.width = self->width;
expose_area.height = self->height;
PangoFontDescription *xfont = pango_font_description_from_string("Sans 16px");
cairo_save(self->cr);
#ifdef ROBTK_UPSCALE
cairo_scale(self->cr, hw_scale, hw_scale);
#endif
cairo_rectangle (self->cr, 0, 0, self->width, self->height);
cairo_set_operator (self->cr, CAIRO_OPERATOR_OVER);
cairo_set_source_rgba(self->cr, 0, 0, 0, .15 + self->gpg_shade);
if (self->gpg_shade < .6) self->gpg_shade += .001;
cairo_fill(self->cr);
write_text_full(self->cr, "Unregistered Version\nhttp://x42-plugins.com",
xfont, self->width * .5, self->height * .5,
self->width < 200 ? M_PI * -.5 : 0, -2, c_wht);
pango_font_description_free(xfont);
cairo_restore(self->cr);
cairo_surface_mark_dirty(self->surface);
}
#endif
static void cairo_expose(GLrobtkLV2UI * self) {
#ifdef ROBTK_UPSCALE
#if __BIG_ENDIAN__
float hw_scale = 1.0;
#else
float hw_scale = puglGetHWSurfaceScale(self->view);
#endif
#endif
if (self->expose_overlay) {
cairo_rectangle_t expose_area;
posrb_read_clear(self->rb); // no fast-track
self->tl->resized = TRUE; // full re-expose
expose_area.x = expose_area.y = 0;
expose_area.width = self->width;
expose_area.height = self->height;
cairo_save(self->cr);
#ifdef ROBTK_UPSCALE
cairo_scale(self->cr, hw_scale, hw_scale);
#endif
self->tl->expose_event(self->tl, self->cr, &expose_area);
cairo_restore(self->cr);
cairo_save(self->cr);
#ifdef ROBTK_UPSCALE
cairo_scale(self->cr, hw_scale, hw_scale);
#endif
self->expose_overlay (self->tl, self->cr, &expose_area);
cairo_restore(self->cr);
return;
}
/* FAST TRACK EXPOSE */
int qq = posrb_read_space(self->rb) / sizeof(RWArea);
bool dirty = qq > 0;
#ifdef DEBUG_FASTTRACK
/*if (qq > 0)*/ fprintf(stderr, " fast track %d events\n", qq);
#endif
cairo_rectangle_t fast_track_area = {0,0,0,0};
uint32_t fast_track_cnt = 0;
while (--qq >= 0) {
RWArea a;
posrb_read(self->rb, (uint8_t*) &a, sizeof(RWArea));
assert(a.rw);
/* skip duplicates */
if (fast_track_cnt > 0) {
if ( a.a.x+a.rw->trel.x >= fast_track_area.x
&& a.a.y+a.rw->trel.y >= fast_track_area.y
&& a.a.x+a.rw->trel.x+a.a.width <= fast_track_area.x + fast_track_area.width
&& a.a.y+a.rw->trel.y+a.a.height <= fast_track_area.y + fast_track_area.height) {
#ifdef DEBUG_FASTTRACK
fprintf(stderr, " skip fast-track event #%d (%.1f x %.1f + %.1f + %.1f)\n", fast_track_cnt,
a.a.width, a.a.height, a.a.x+a.rw->trel.x, a.a.y+a.rw->trel.y);
#endif
continue;
}
}
cairo_save(self->cr);
#ifdef ROBTK_UPSCALE
cairo_scale(self->cr, hw_scale, hw_scale);
#endif
cairo_translate(self->cr, a.rw->trel.x, a.rw->trel.y);
a.rw->expose_event(a.rw, self->cr, &a.a);
/* keep track of exposed parts */
a.a.x += a.rw->trel.x;
a.a.y += a.rw->trel.y;
#ifdef DEBUG_FASTTRACK
fprintf(stderr, " #%d (%.1f x %.1f @ %.1f + %.1f\n", fast_track_cnt,
a.a.width, a.a.height, a.a.x, a.a.y);
#endif
#ifndef FASTTRACK_INTERSECT
memcpy(&fast_track_area, &a.a, sizeof(cairo_rectangle_t));
fast_track_cnt++;
#else // intersects
if (fast_track_cnt++ == 0) {
fast_track_area.x = a.a.x;
fast_track_area.y = a.a.y;
fast_track_area.width = a.a.width;
fast_track_area.height = a.a.height;
} else {
rect_intersection(&fast_track_area, &a.a, &fast_track_area);
//rect_combine(&fast_track_area, &a.a, &fast_track_area);
}
#endif
#ifdef VISIBLE_EXPOSE
static int ftrk = 0;
static int fcol = 0;
ftrk = (ftrk + 1) %11;
fcol = (fcol + 1) %17;
cairo_rectangle (self->cr, 0, 0, a.rw->trel.width, a.rw->trel.height);
cairo_set_operator (self->cr, CAIRO_OPERATOR_OVER);
cairo_set_source_rgba(self->cr, .8, .5 + ftrk/25.0, .5 - fcol/40.0, .25 + ftrk/30.0);
cairo_fill(self->cr);
#endif
cairo_restore(self->cr);
}
if (self->expose_area.width == 0 || self->expose_area.height == 0) {
#ifdef DEBUG_EXPOSURE
fprintf(stderr, " --- NO DRAW\n");
#endif
if (dirty) {
cairo_surface_mark_dirty(self->surface);
}
return;
}
#ifdef DEBUG_EXPOSURE
fprintf(stderr, "XPS %.1f+%.1f %.1fx%.1f\n", self->expose_area.x, self->expose_area.y, self->expose_area.width, self->expose_area.height);
#endif
/* make copy and clear -- should be an atomic op when using own thread */
cairo_rectangle_t area;
area.x = self->expose_area.x;
area.y = self->expose_area.y;
area.width = self->expose_area.width;
area.height = self->expose_area.height;
self->expose_area.x = 0;
self->expose_area.y = 0;
self->expose_area.width = 0;
self->expose_area.height = 0;
#ifdef DEBUG_EXPOSURE
fprintf(stderr, "---- XPS ---\n");
#endif
// intersect exposure with toplevel
cairo_rectangle_t expose_area;
expose_area.x = MAX(0, area.x - self->tl->area.x);
expose_area.y = MAX(0, area.y - self->tl->area.y);
expose_area.width = MIN(area.x + area.width, self->tl->area.x + self->tl->area.width) - MAX(area.x, self->tl->area.x);
expose_area.height = MIN(area.y + area.height, self->tl->area.y + self->tl->area.height) - MAX(area.y, self->tl->area.y);
if (expose_area.width < 0 || expose_area.height < 0) {
fprintf(stderr, " !!! EMPTY AREA\n"); return;
}
#define XPS_NO_DRAW { fprintf(stderr, " !!! OUTSIDE DRAW %.1fx%.1f %.1f+%.1f %.1fx%.1f\n", area.x, area.y, self->tl->area.x, self->tl->area.y, self->tl->area.width, self->tl->area.height); return; }
#if 1
if (area.x > self->tl->area.x + self->tl->area.width) XPS_NO_DRAW
if (area.y > self->tl->area.y + self->tl->area.height) XPS_NO_DRAW
if (area.x < self->tl->area.x) XPS_NO_DRAW
if (area.y < self->tl->area.y) XPS_NO_DRAW
#endif
cairo_save(self->cr);
#ifdef ROBTK_UPSCALE
cairo_scale(self->cr, hw_scale, hw_scale);
#endif
self->tl->expose_event(self->tl, self->cr, &expose_area);
cairo_restore(self->cr);
#ifdef VISIBLE_EXPOSE
static int move = 0;
static int hueh = 0;
move = (move + 1) %10;
hueh = (hueh + 1) %13;
cairo_rectangle (self->cr, expose_area.x, expose_area.y, expose_area.width, expose_area.height);
cairo_set_operator (self->cr, CAIRO_OPERATOR_OVER);
cairo_set_source_rgba(self->cr, .7 + hueh / 50.0, .3, .5 - hueh/30, .25 + move/20.0);
cairo_fill(self->cr);
#endif
cairo_surface_mark_dirty(self->surface);
}
static void queue_draw_full(RobWidget *rw) {
GLrobtkLV2UI * const self =
(GLrobtkLV2UI*) robwidget_get_toplevel_handle(rw);
if (!self || !self->view) {
rw->redraw_pending = true;
return;
}
#ifdef DEBUG_EXPOSURE
fprintf(stderr, "~~ queue_draw_full '%s'\n", ROBWIDGET_NAME(rw));
#endif
self->expose_area.x = 0;
self->expose_area.y = 0;
self->expose_area.width = self->width;
self->expose_area.height = self->height;
puglPostRedisplay(self->view);
}
static void queue_draw_area(RobWidget *rw, int x, int y, int width, int height) {
GLrobtkLV2UI * self =
(GLrobtkLV2UI*) robwidget_get_toplevel_handle(rw);
if (!self || !self->view) {
rw->redraw_pending = true;
return;
}
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x + width > rw->area.width) width = rw->area.width - x;
if (y + height > rw->area.height) height = rw->area.height - y;
if (self->expose_area.width == 0 || self->expose_area.height == 0) {
RobTkBtnEvent ev; ev.x = x; ev.y = y;
#ifdef DEBUG_EXPOSURE
fprintf(stderr, "Q PARTIAL NEW '%s': ", ROBWIDGET_NAME(rw));
#endif
offset_traverse_from_child(rw, &ev);
#ifdef DEBUG_EXPOSURE
fprintf(stderr, "Q PARTIAL -> %d+%d -> %d+%d (%dx%d)\n", x, y, ev.x, ev.y, width, height);
#endif
self->expose_area.x = ev.x;
self->expose_area.y = ev.y;
self->expose_area.width = width;
self->expose_area.height = height;
} else {
RobTkBtnEvent ev; ev.x = x; ev.y = y;
#ifdef DEBUG_EXPOSURE
fprintf(stderr, "Q PARTIAL AMEND '%s': ", ROBWIDGET_NAME(rw));
#endif
offset_traverse_from_child(rw, &ev);
#ifdef DEBUG_EXPOSURE
fprintf(stderr, "Q PARTIAL -> %d+%d -> %d+%d (%dx%d)\n", x, y, ev.x, ev.y, width, height);
#endif
cairo_rectangle_t r;
r.x = ev.x; r.y = ev.y;
r.width = width; r.height = height;
rect_combine((cairo_rectangle_t*) &self->expose_area, &r, (cairo_rectangle_t*) &self->expose_area);
}
puglPostRedisplay(self->view);
}
static void queue_draw(RobWidget *rw) {
#ifdef DEBUG_EXPOSURE
fprintf(stderr, "FULL widget -> ");
#endif
queue_draw_area(rw, 0, 0, rw->area.width, rw->area.height);
}
static void queue_tiny_rect(RobWidget *rw, cairo_rectangle_t *a) {
if (!rw->cached_position) {
rw->redraw_pending = true;
queue_draw(rw);
return;
}
GLrobtkLV2UI * self =
(GLrobtkLV2UI*) robwidget_get_toplevel_handle(rw);// XXX cache ?!
if (!self || !self->view) {
rw->redraw_pending = true;
return;
}
RWArea b;
b.rw = rw;
memcpy(&b.a, a, sizeof(cairo_rectangle_t));
if (posrb_write(self->rb, (uint8_t*) &b, sizeof(RWArea))<0) {
queue_draw_area(rw, a->x, a->y, a->width, a->height);
}
puglPostRedisplay(self->view);
}
static void queue_tiny_area(RobWidget *rw, float x, float y, float w, float h) {
cairo_rectangle_t a;
a.x = x; a.width = w;
a.y = y - 1;
a.height = h + 1;
queue_tiny_rect(rw, &a);
}
static void robwidget_layout(GLrobtkLV2UI * const self, bool setsize, bool init) {
RobWidget * rw = self->tl;
#ifdef DEBUG_RESIZE
printf("robwidget_layout(%s) setsize:%s init:%s\n", ROBWIDGET_NAME(self->tl),
setsize ? "true" : "false",
init ? "true" : "false"
);
#endif
int oldw = self->width;
int oldh = self->height;
bool size_changed = FALSE;
int nox, noy;
rtoplevel_scale (self->tl, self->tl->widget_scale);
self->tl->size_request(self->tl, &nox, &noy);
if (!init && rw->size_limit) {
self->tl->size_limit(self->tl, &self->width, &self->height);
if (oldw != self->width || oldh != self->height) {
size_changed = TRUE;
}
} else if (setsize) {
if (oldw != nox || oldh != noy) {
size_changed = TRUE;
}
self->width = nox;
self->height = noy;
} else if (nox > self->width || noy > self->height) {
enum LVGLResize rsz = plugin_scale_mode(self->ui);
if (rsz == LVGL_ZOOM_TO_ASPECT || rsz == LVGL_LAYOUT_TO_FIT) {
puglUpdateGeometryConstraints(self->view, nox, noy, rsz == LVGL_ZOOM_TO_ASPECT);
return;
}
fprintf(stderr, "WINDOW IS SMALLER THAN MINIMUM SIZE! %d > %d h: %d > %d\n",
nox, self->width, noy, self->height);
if (nox > self->width) self->width = nox;
if (noy > self->height) self->height = noy;
} else if (nox < self->width || noy < self->height) {
enum LVGLResize rsz = plugin_scale_mode(self->ui);
if (rsz == LVGL_ZOOM_TO_ASPECT || rsz == LVGL_LAYOUT_TO_FIT) {
puglUpdateGeometryConstraints(self->view, nox, noy, rsz == LVGL_ZOOM_TO_ASPECT);
}
}
if (rw->size_allocate) {
self->tl->size_allocate(rw, self->width, self->height);
}
rtoplevel_cache(rw, TRUE);
if (init) {
return;
}
if (setsize && size_changed) {
self->resize_in_progress = TRUE;
puglPostResize(self->view);
} else {
queue_draw_full(rw);
}
}
// called by a widget if size changes
static void resize_self(RobWidget *rw) {
GLrobtkLV2UI * const self =
(GLrobtkLV2UI*) robwidget_get_toplevel_handle(rw);
if (!self || !self->view) { return; }
#ifdef DEBUG_RESIZE
printf("resize_self(%s)\n", ROBWIDGET_NAME(rw));
#endif
robwidget_layout(self, TRUE, FALSE);
}
static void resize_toplevel(RobWidget *rw, int w, int h) {
GLrobtkLV2UI * const self =
(GLrobtkLV2UI*) robwidget_get_toplevel_handle(rw);
if (!self || !self->view) { return; }
self->width = w;
self->height = h;
resize_self(rw);
self->resize_in_progress = TRUE;
self->resize_toplevel = TRUE;
puglPostResize(self->view);
}
static void relayout_toplevel(RobWidget *rw) {
GLrobtkLV2UI * const self =
(GLrobtkLV2UI*) robwidget_get_toplevel_handle(rw);
if (!self || !self->view) { return; }
#ifdef TIMED_RESHAPE
if (!self->resize_in_progress) {
self->queue_w = self->width;
self->queue_h = self->height;
self->resize_in_progress = TRUE;
self->queue_reshape = 1;
}
#else
self->relayout = TRUE;
#endif
puglPostRedisplay(self->view);
}
/*****************************************************************************/
/* UI scaling */
static void robtk_queue_scale_change (RobWidget *rw, const float ws) {
GLrobtkLV2UI * const self =
(GLrobtkLV2UI*) robwidget_get_toplevel_handle(rw);
self->queue_widget_scale = ws;
queue_draw (rw);
}
static void set_toplevel_expose_overlay(RobWidget *rw, void (*expose_event)(struct _robwidget*, cairo_t*, cairo_rectangle_t *)){
GLrobtkLV2UI * const self = (GLrobtkLV2UI*) robwidget_get_toplevel_handle(rw);
self->expose_overlay = expose_event;
rw->resized = TRUE; //full re-expose
queue_draw (rw);
}
static void robtk_expose_ui_scale (RobWidget* handle, cairo_t* cr, cairo_rectangle_t *ev) {
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
cairo_rectangle (cr, ev->x, ev->y, ev->width, ev->height);
cairo_set_source_rgba (cr, 0, 0, 0, .6);
cairo_fill (cr);
// ui-scale buttons
const int nbtn_col = 4;
const int nbtn_row = 2;
float bt_w = ev->width / (float)(nbtn_col * 2 + 1);
float bt_h = ev->height / (float)(nbtn_row * 2 + 1);
static const char scales[8][8] = { "100%", "110%", "115%", "120%", "125%", "150%", "175%", "200%" };
PangoFontDescription *font = pango_font_description_from_string("Sans 24px");
write_text_full (cr, "GUI Scaling", font, floor(ev->width * .5), floor(bt_h * .5), 0, 2, c_wht);
pango_font_description_free (font);
font = pango_font_description_from_string("Sans 14px");
for (int y = 0; y < nbtn_row; ++y) {
for (int x = 0; x < nbtn_col; ++x) {
float x0 = floor ((1 + 2 * x) * bt_w);
float y0 = floor ((1 + 2 * y) * bt_h);
rounded_rectangle (cr, x0, y0, floor (bt_w), floor (bt_h), 8);
CairoSetSouerceRGBA(c_wht);
cairo_set_line_width(cr, 1.5);
cairo_stroke_preserve (cr);
cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
cairo_fill (cr);
int pos = x + y * nbtn_col;
write_text_full (cr, scales[pos], font, floor(x0 + bt_w * .5), floor(y0 + bt_h * .5), 0, 2, c_wht);
}
}
pango_font_description_free (font);
}
static bool robtk_event_ui_scale (RobWidget* rw, RobTkBtnEvent *ev) {
const int nbtn_col = 4;
const int nbtn_row = 2;
float bt_w = rw->area.width / (float)(nbtn_col * 2 + 1);
float bt_h = rw->area.height / (float)(nbtn_row * 2 + 1);
int xp = floor (ev->x / bt_w);
int yp = floor (ev->y / bt_h);
if ((xp & 1) == 0 || (yp & 1) == 0) {
return FALSE;
}
const int pos = (xp - 1) / 2 + nbtn_col * (yp - 1) / 2;
if (pos < 0 || pos >= nbtn_col * nbtn_row) {
// possible rounding-error, bottom right corner
return FALSE;
}
static const float scales[8] = { 1.0, 1.1, 1.15, 1.20, 1.25, 1.50, 1.75, 2.0 };
robtk_queue_scale_change (rw, scales [pos]);
return TRUE;
}
static RobWidget* robtk_tl_mousedown (RobWidget* rw, RobTkBtnEvent *ev) {
if (rw->block_events) {
if (robtk_event_ui_scale (rw, ev)) {
rw->block_events = FALSE;
set_toplevel_expose_overlay (rw, NULL);
}
return NULL;
}
RobWidget *rv = rcontainer_mousedown (rw, ev);
if (rv) return rv;
if (ev->button != 3) {
return NULL;
}
RobWidget * c = decend_into_widget_tree(rw, ev->x, ev->y);
if (c && c->mousedown) return NULL;
rw->block_events = TRUE;
set_toplevel_expose_overlay (rw, &robtk_expose_ui_scale);
return NULL;
}
static void robwidget_toplevel_enable_scaling (RobWidget* rw) {
assert (rw->parent == rw);
assert (rw->mousedown == &rcontainer_mousedown);
robwidget_set_mousedown (rw, robtk_tl_mousedown);
}
/*****************************************************************************/
/* helper functions */
static uint64_t microtime(float offset) {
struct timespec now;
rtk_clock_gettime(&now);
now.tv_nsec += 1000000000 * offset;
while (now.tv_nsec >= 1000000000) {
now.tv_nsec -= 1000000000;
now.tv_sec += 1;
}
return now.tv_sec * 1000 + now.tv_nsec / 1000000;
}
static void myusleep(uint32_t usec) {
#ifdef _WIN32
Sleep(usec / 1000);
#else
struct timespec slp;
slp.tv_sec = usec / 1000000;
slp.tv_nsec = (usec % 1000000) * 1000;
nanosleep(&slp, NULL);
#endif
}
/*****************************************************************************/
static void reallocate_canvas(GLrobtkLV2UI* self) {
#if __BIG_ENDIAN__
float hw_scale = 1.0;
#else
float hw_scale = puglGetHWSurfaceScale(self->view);
#endif
#ifdef DEBUG_RESIZE
printf("reallocate_canvas to %d x %d\n", self->width, self->height);
#endif
self->queue_canvas_realloc = false;
if (self->cr) {
free (self->surf_data);
#if __BIG_ENDIAN__
free (self->surf_data_be);
#endif
cairo_destroy (self->cr);
}
opengl_reallocate_texture(hw_scale * self->width, hw_scale * self->height, &self->texture_id);
if (self->surface) {
cairo_surface_destroy (self->surface);
self->surface = NULL;
}
self->cr = opengl_create_cairo_t(hw_scale * self->width, hw_scale * self->height, &self->surface, &self->surf_data);
#if __BIG_ENDIAN__
self->surf_data_be = (unsigned char*) calloc (4 * self->width * self->height, sizeof (unsigned char));
#endif
/* clear top window */
cairo_save(self->cr);
cairo_set_source_rgba (self->cr, .0, .0, .0, 1.0);
cairo_set_operator (self->cr, CAIRO_OPERATOR_SOURCE);
cairo_rectangle (self->cr, 0, 0, hw_scale * self->width, hw_scale * self->height);
cairo_fill (self->cr);
cairo_restore(self->cr);
}
static void
onRealReshape(PuglView* view, int width, int height)
{
GLrobtkLV2UI* self = (GLrobtkLV2UI*)puglGetHandle(view);
self->resize_in_progress = FALSE;
self->resize_toplevel = FALSE;
#ifdef DEBUG_RESIZE
printf("onRealReshape (%s) %dx%d\n",
ROBWIDGET_NAME(self->tl), width, height);
#endif
switch(plugin_scale_mode(self->ui)) {
case LVGL_LAYOUT_TO_FIT:
self->xoff = 0; self->yoff = 0; self->xyscale = 1.0;
#ifdef DEBUG_RESIZE
printf("onRealReshape pre-layout %dx%d -> %dx%d\n",
self->width, self->height, width, height);
#endif
self->width = width;
self->height = height;
robwidget_layout(self, FALSE, FALSE);
self->width = self->tl->area.width;
self->height = self->tl->area.height;
#ifdef DEBUG_RESIZE
printf("onRealReshape post-layout %dx%d\n",
self->width, self->height);
#endif
reallocate_canvas(self);
// fall-thru to scale
case LVGL_ZOOM_TO_ASPECT:
if (self->queue_canvas_realloc) {
reallocate_canvas(self);
}
rtoplevel_cache(self->tl, TRUE); // redraw background
if (self->width == width && self->height == height) {
self->xoff = 0; self->yoff = 0; self->xyscale = 1.0;
glViewport (0, 0, self->width, self->height);
} else
{
reallocate_canvas(self);
const float gl_aspect = width / (float) height;
const float cl_aspect = self->width / (float) self->height;
if (gl_aspect > cl_aspect) {
self->xyscale = (float) self->height / (float) height;
self->xoff = (width - self->width / self->xyscale)/2;
self->yoff = (height - self->height / self->xyscale)/2;
} else {
self->xoff = 0;
self->xyscale = (float) self->width / (float) width;
self->xoff = (width - self->width / self->xyscale)/2;
self->yoff = (height - self->height / self->xyscale)/2;
}
glViewport (self->xoff, self->yoff, self->width / self->xyscale, self->height / self->xyscale);
}
break;
case LVGL_CENTER:
{
self->xyscale = 1.0;
self->xoff = (width - self->width)/2;
self->yoff = (height - self->height)/2;
glViewport (self->xoff, self->yoff, self->width, self->height);
}
break;
case LVGL_TOP_LEFT: