-
-
Notifications
You must be signed in to change notification settings - Fork 304
Expand file tree
/
Copy pathapp.c
More file actions
1101 lines (910 loc) · 24.2 KB
/
app.c
File metadata and controls
1101 lines (910 loc) · 24.2 KB
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
/**
* Looking Glass
* Copyright © 2017-2022 The Looking Glass Authors
* https://looking-glass.io
*
* 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 of the License, 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., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "app.h"
#include "main.h"
#include "core.h"
#include "util.h"
#include "clipboard.h"
#include "render_queue.h"
#include "kb.h"
#include "common/debug.h"
#include "common/stringutils.h"
#include "interface/overlay.h"
#include "overlays.h"
#include "cimgui.h"
#include <stdarg.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
bool app_isRunning(void)
{
return
g_state.state == APP_STATE_RUNNING ||
g_state.state == APP_STATE_RESTART;
}
bool app_isCaptureMode(void)
{
return g_cursor.grab;
}
bool app_isCaptureOnlyMode(void)
{
return g_params.captureInputOnly;
}
bool app_isFormatValid(void)
{
return g_state.formatValid;
}
bool app_isOverlayMode(void)
{
if (g_state.overlayInput)
return true;
bool result = false;
struct Overlay * overlay;
ll_lock(g_state.overlays);
ll_forEachNL(g_state.overlays, item, overlay)
{
if (overlay->ops->needs_overlay && overlay->ops->needs_overlay(overlay))
{
result = true;
break;
}
}
ll_unlock(g_state.overlays);
return result;
}
void app_updateCursorPos(double x, double y)
{
g_cursor.pos.x = x;
g_cursor.pos.y = y;
g_cursor.valid = true;
g_state.io->MousePos = (ImVec2) { x, y };
}
void app_handleFocusEvent(bool focused)
{
g_state.focused = focused;
// release any imgui buttons/keys if we lost focus
if (!focused && app_isOverlayMode())
core_resetOverlayInputState();
if (!core_inputEnabled())
{
if (!focused && g_params.minimizeOnFocusLoss && app_getFullscreen())
g_state.ds->minimize();
return;
}
if (!focused)
{
core_setGrabQuiet(false);
core_setCursorInView(false);
if (g_params.releaseKeysOnFocusLoss)
for (int key = 0; key < KEY_MAX; key++)
if (g_state.keyDown[key])
app_handleKeyRelease(key, 0);
g_state.escapeActive = false;
if (!g_params.showCursorDot)
g_state.ds->setPointer(LG_POINTER_NONE);
if (g_params.minimizeOnFocusLoss)
g_state.ds->minimize();
}
g_cursor.realign = true;
g_state.ds->realignPointer();
}
void app_handleEnterEvent(bool entered)
{
if (entered)
{
g_cursor.inWindow = true;
if (!core_inputEnabled())
return;
g_cursor.realign = true;
}
else
{
g_cursor.inWindow = false;
core_setCursorInView(false);
// stop the user being able to drag windows off the screen and work around
// the mouse button release being missed due to not being in capture mode.
if (app_isOverlayMode())
{
g_state.io->MouseDown[ImGuiMouseButton_Left ] = false;
g_state.io->MouseDown[ImGuiMouseButton_Right ] = false;
g_state.io->MouseDown[ImGuiMouseButton_Middle] = false;
}
if (!core_inputEnabled())
return;
if (!g_params.alwaysShowCursor)
g_cursor.draw = false;
g_cursor.redraw = true;
}
}
void app_clipboardRelease(void)
{
if (!g_params.clipboardToVM)
return;
purespice_clipboardRelease();
}
void app_clipboardNotifyTypes(const LG_ClipboardData types[], int count)
{
if (!g_params.clipboardToVM)
return;
if (count == 0)
{
purespice_clipboardRelease();
return;
}
PSDataType conv[count];
for(int i = 0; i < count; ++i)
conv[i] = cb_lgTypeToSpiceType(types[i]);
purespice_clipboardGrab(conv, count);
}
void app_clipboardNotifySize(const LG_ClipboardData type, size_t size)
{
if (!g_params.clipboardToVM)
return;
if (type == LG_CLIPBOARD_DATA_NONE)
{
purespice_clipboardRelease();
return;
}
g_state.cbType = cb_lgTypeToSpiceType(type);
g_state.cbChunked = size > 0;
g_state.cbXfer = size;
purespice_clipboardDataStart(g_state.cbType, size);
}
void app_clipboardData(const LG_ClipboardData type, uint8_t * data, size_t size)
{
if (!g_params.clipboardToVM)
return;
if (g_state.cbChunked && size > g_state.cbXfer)
{
DEBUG_ERROR("refusing to send more then cbXfer bytes for chunked xfer");
size = g_state.cbXfer;
}
if (!g_state.cbChunked)
purespice_clipboardDataStart(g_state.cbType, size);
purespice_clipboardData(g_state.cbType, data, (uint32_t)size);
g_state.cbXfer -= size;
}
void app_clipboardRequest(const LG_ClipboardReplyFn replyFn, void * opaque)
{
if (!g_params.clipboardToLocal)
return;
struct CBRequest * cbr = malloc(sizeof(*cbr));
if (!cbr)
{
DEBUG_ERROR("out of memory");
return;
}
cbr->type = g_state.cbType;
cbr->replyFn = replyFn;
cbr->opaque = opaque;
ll_push(g_state.cbRequestList, cbr);
purespice_clipboardRequest(g_state.cbType);
}
static int mapSpiceToImGuiButton(uint32_t button)
{
switch (button)
{
case 1: // SPICE_MOUSE_BUTTON_LEFT
return ImGuiMouseButton_Left;
case 2: // SPICE_MOUSE_BUTTON_MIDDLE
return ImGuiMouseButton_Middle;
case 3: // SPICE_MOUSE_BUTTON_RIGHT
return ImGuiMouseButton_Right;
}
return -1;
}
void app_handleButtonPress(int button)
{
g_cursor.buttons |= (1U << button);
if (app_isOverlayMode())
{
int igButton = mapSpiceToImGuiButton(button);
if (igButton != -1)
g_state.io->MouseDown[igButton] = true;
return;
}
if (!core_inputEnabled() || !g_cursor.inView)
return;
if (!purespice_mousePress(button))
DEBUG_ERROR("app_handleButtonPress: failed to send message");
}
void app_handleButtonRelease(int button)
{
g_cursor.buttons &= ~(1U << button);
if (app_isOverlayMode())
{
int igButton = mapSpiceToImGuiButton(button);
if (igButton != -1)
g_state.io->MouseDown[igButton] = false;
return;
}
if (!core_inputEnabled())
return;
if (!purespice_mouseRelease(button))
DEBUG_ERROR("app_handleButtonRelease: failed to send message");
}
void app_handleWheelMotion(double motion)
{
if (app_isOverlayMode())
g_state.io->MouseWheel -= motion;
}
void app_handleKeyPress(int sc, int charcode)
{
if (!app_isOverlayMode() || !g_state.io->WantCaptureKeyboard)
{
if (sc == g_params.escapeKey && !g_state.escapeActive)
{
g_state.escapeActive = true;
g_state.escapeTime = microtime();
g_state.escapeAction = -1;
return;
}
if (g_state.escapeActive)
{
g_state.escapeAction = sc;
KeybindHandle handle;
ll_forEachNL(g_state.bindings, item, handle)
{
if ((handle->sc && handle->sc == sc ) ||
(handle->charcode && handle->charcode == charcode))
{
handle->callback(sc, handle->opaque);
break;
}
}
return;
}
}
if (app_isOverlayMode())
{
if (sc == KEY_ESC)
app_setOverlay(false);
else
g_state.io->KeysDown[sc] = true;
return;
}
if (!core_inputEnabled())
return;
if (g_params.ignoreWindowsKeys && (sc == KEY_LEFTMETA || sc == KEY_RIGHTMETA))
return;
if (!g_state.keyDown[sc])
{
uint32_t ps2 = linux_to_ps2[sc];
if (!ps2)
return;
if (purespice_keyDown(ps2))
g_state.keyDown[sc] = true;
else
{
DEBUG_ERROR("app_handleKeyPress: failed to send message");
return;
}
}
}
void app_handleKeyRelease(int sc, int charcode)
{
if (g_state.escapeActive)
{
if (g_state.escapeAction == -1)
{
if (!g_state.escapeHelp && g_params.useSpiceInput &&
!app_isOverlayMode())
core_setGrab(!g_cursor.grab);
}
if (sc == g_params.escapeKey)
g_state.escapeActive = false;
}
if (app_isOverlayMode())
{
g_state.io->KeysDown[sc] = false;
return;
}
if (!core_inputEnabled())
return;
// avoid sending key up events when we didn't send a down
if (!g_state.keyDown[sc])
return;
if (g_params.ignoreWindowsKeys && (sc == KEY_LEFTMETA || sc == KEY_RIGHTMETA))
return;
uint32_t ps2 = linux_to_ps2[sc];
if (!ps2)
return;
if (purespice_keyUp(ps2))
g_state.keyDown[sc] = false;
else
{
DEBUG_ERROR("app_handleKeyRelease: failed to send message");
return;
}
}
void app_handleKeyboardTyped(const char * typed)
{
ImGuiIO_AddInputCharactersUTF8(g_state.io, typed);
}
void app_handleKeyboardModifiers(bool ctrl, bool shift, bool alt, bool super)
{
g_state.modCtrl = ctrl;
g_state.modShift = shift;
g_state.modAlt = alt;
g_state.modSuper = super;
}
void app_handleKeyboardLEDs(bool numLock, bool capsLock, bool scrollLock)
{
if (!core_inputEnabled())
return;
uint32_t modifiers =
(scrollLock ? 1 /* SPICE_SCROLL_LOCK_MODIFIER */ : 0) |
(numLock ? 2 /* SPICE_NUM_LOCK_MODIFIER */ : 0) |
(capsLock ? 4 /* SPICE_CAPS_LOCK_MODIFIER */ : 0);
if (!purespice_keyModifiers(modifiers))
DEBUG_ERROR("app_handleKeyboardLEDs: failed to send message");
}
void app_handleMouseRelative(double normx, double normy,
double rawx, double rawy)
{
if (app_isOverlayMode())
return;
if (g_cursor.grab)
{
if (g_params.rawMouse)
core_handleMouseGrabbed(rawx, rawy);
else
core_handleMouseGrabbed(normx, normy);
}
else
if (g_cursor.inWindow)
core_handleMouseNormal(normx, normy);
}
// On some display servers normal cursor logic does not work due to the lack of
// cursor warp support. Instead, we attempt a best-effort emulation which works
// with a 1:1 mouse movement patch applied in the guest. For anything fancy, use
// capture mode.
void app_handleMouseBasic(void)
{
/* do not pass mouse events to the guest if we do not have focus */
if (!g_cursor.guest.valid || !g_state.haveSrcSize || !g_state.focused ||
app_isOverlayMode())
return;
if (!core_inputEnabled())
return;
const bool inView =
g_cursor.pos.x >= g_state.dstRect.x &&
g_cursor.pos.x < g_state.dstRect.x + g_state.dstRect.w &&
g_cursor.pos.y >= g_state.dstRect.y &&
g_cursor.pos.y < g_state.dstRect.y + g_state.dstRect.h;
core_setCursorInView(inView);
/* translate the current position to guest coordinate space */
struct DoublePoint guest;
util_localCurToGuest(&guest);
int x = (int) round(util_clamp(guest.x, 0, g_state.srcSize.x) -
g_cursor.projected.x);
int y = (int) round(util_clamp(guest.y, 0, g_state.srcSize.y) -
g_cursor.projected.y);
if (!x && !y)
return;
g_cursor.projected.x += x;
g_cursor.projected.y += y;
if (!purespice_mouseMotion(x, y))
DEBUG_ERROR("failed to send mouse motion message");
}
void app_resyncMouseBasic(void)
{
if (!g_cursor.guest.valid)
return;
g_cursor.projected.x = g_cursor.guest.x + g_cursor.guest.hx;
g_cursor.projected.y = g_cursor.guest.y + g_cursor.guest.hy;
}
void app_updateWindowPos(int x, int y)
{
g_state.windowPos.x = x;
g_state.windowPos.y = y;
}
void app_handleResizeEvent(int w, int h, double scale, const struct Border border)
{
memcpy(&g_state.border, &border, sizeof(border));
/* don't do anything else if the window dimensions have not changed */
if (g_state.windowW == w && g_state.windowH == h && g_state.windowScale == scale)
return;
g_state.windowW = w;
g_state.windowH = h;
g_state.windowCX = w / 2;
g_state.windowCY = h / 2;
g_state.windowScale = scale;
core_updatePositionInfo();
if (core_inputEnabled())
{
/* if the window is moved/resized causing a loss of focus while grabbed, it
* makes it impossible to re-focus the window, so we quietly re-enter
* capture if we were already in it */
if (g_cursor.grab)
{
core_setGrabQuiet(false);
core_setGrabQuiet(true);
}
core_alignToGuest();
}
}
void app_invalidateWindow(bool full)
{
if (full)
atomic_store(&g_state.invalidateWindow, true);
if (g_state.dsInitialized && g_state.jitRender && g_state.ds->stopWaitFrame)
g_state.ds->stopWaitFrame();
lgSignalEvent(g_state.frameEvent);
}
void app_handleCloseEvent(void)
{
if (!g_params.ignoreQuit || !g_cursor.inView)
g_state.state = APP_STATE_SHUTDOWN;
}
void app_handleRenderEvent(const uint64_t timeUs)
{
bool invalidate = false;
if (!g_state.escapeActive)
{
if (g_state.escapeHelp)
{
g_state.escapeHelp = false;
invalidate = true;
}
}
else
{
if (!g_state.escapeHelp && timeUs - g_state.escapeTime > g_params.helpMenuDelayUs)
{
g_state.escapeHelp = true;
invalidate = true;
}
}
if (invalidate)
app_invalidateWindow(false);
}
void app_setFullscreen(bool fs)
{
g_state.ds->setFullscreen(fs);
}
bool app_getFullscreen(void)
{
return g_state.ds->getFullscreen();
}
bool app_getProp(LG_DSProperty prop, void * ret)
{
return g_state.ds->getProp(prop, ret);
}
#ifdef ENABLE_EGL
EGLDisplay app_getEGLDisplay(void)
{
return g_state.ds->getEGLDisplay();
}
EGLNativeWindowType app_getEGLNativeWindow(void)
{
return g_state.ds->getEGLNativeWindow();
}
void app_eglSwapBuffers(EGLDisplay display, EGLSurface surface, const struct Rect * damage, int count)
{
g_state.ds->eglSwapBuffers(display, surface, damage, count);
}
#endif
#ifdef ENABLE_OPENGL
LG_DSGLContext app_glCreateContext(void)
{
return g_state.ds->glCreateContext();
}
void app_glDeleteContext(LG_DSGLContext context)
{
g_state.ds->glDeleteContext(context);
}
void app_glMakeCurrent(LG_DSGLContext context)
{
g_state.ds->glMakeCurrent(context);
}
void app_glSetSwapInterval(int interval)
{
g_state.ds->glSetSwapInterval(interval);
}
void app_glSwapBuffers(void)
{
g_state.ds->glSwapBuffers();
}
#endif
void app_alert(LG_MsgAlert type, const char * fmt, ...)
{
if (!g_state.lgr || !g_params.showAlerts)
return;
va_list args;
va_start(args, fmt);
overlayAlert_show(type, fmt, args);
va_end(args);
}
MsgBoxHandle app_msgBox(const char * caption, const char * fmt, ...)
{
va_list args;
va_start(args, fmt);
MsgBoxHandle handle = overlayMsg_show(caption, NULL, NULL, fmt, args);
va_end(args);
core_updateOverlayState();
return handle;
}
MsgBoxHandle app_confirmMsgBox(const char * caption,
MsgBoxConfirmCallback callback, void * opaque, const char * fmt, ...)
{
va_list args;
va_start(args, fmt);
MsgBoxHandle handle = overlayMsg_show(caption, callback, opaque, fmt, args);
va_end(args);
core_updateOverlayState();
return handle;
}
void app_msgBoxClose(MsgBoxHandle handle)
{
if (!handle)
return;
overlayMsg_close(handle);
}
void app_showRecord(bool show)
{
overlayStatus_set(LG_USER_STATUS_RECORDING, show);
}
KeybindHandle app_registerKeybind(int sc, int charcode, KeybindFn callback,
void * opaque, const char * description)
{
if (charcode != 0 && sc != 0)
{
DEBUG_ERROR("invalid keybind, one of scancode or charcode must be 0");
return NULL;
}
if (charcode && islower(charcode))
{
DEBUG_ERROR("invalid keybind, charcode must be uppercase");
return NULL;
}
KeybindHandle handle;
// don't allow duplicate binds
ll_forEachNL(g_state.bindings, item, handle)
{
if ((sc && handle->sc == sc ) ||
(charcode && handle->charcode == charcode))
{
DEBUG_INFO("Key already bound");
return NULL;
}
}
handle = malloc(sizeof(*handle));
if (!handle)
{
DEBUG_ERROR("out of memory");
return NULL;
}
handle->sc = sc;
handle->charcode = charcode;
handle->callback = callback;
handle->description = description;
handle->opaque = opaque;
ll_push(g_state.bindings, handle);
return handle;
}
void app_releaseKeybind(KeybindHandle * handle)
{
if (!*handle)
return;
ll_removeData(g_state.bindings, *handle);
free(*handle);
*handle = NULL;
}
void app_releaseAllKeybinds(void)
{
KeybindHandle * handle;
while(ll_shift(g_state.bindings, (void **)&handle))
free(handle);
}
GraphHandle app_registerGraph(const char * name, RingBuffer buffer,
float min, float max, GraphFormatFn formatFn)
{
return overlayGraph_register(name, buffer, min, max, formatFn);
}
void app_unregisterGraph(GraphHandle handle)
{
overlayGraph_unregister(handle);
}
void app_invalidateGraph(GraphHandle handle)
{
overlayGraph_invalidate(handle);
}
void app_registerOverlay(const struct LG_OverlayOps * ops, const void * params)
{
ASSERT_LG_OVERLAY_VALID(ops);
struct Overlay * overlay = malloc(sizeof(*overlay));
if (!overlay)
{
DEBUG_ERROR("out of ram");
return;
}
overlay->ops = ops;
overlay->params = params;
overlay->udata = NULL;
overlay->lastRectCount = 0;
ll_push(g_state.overlays, overlay);
if (ops->earlyInit)
ops->earlyInit();
}
void app_initOverlays(void)
{
struct Overlay * overlay;
ll_lock(g_state.overlays);
ll_forEachNL(g_state.overlays, item, overlay)
{
DEBUG_ASSERT(overlay->ops);
if (!overlay->ops->init(&overlay->udata, overlay->params))
{
DEBUG_ERROR("Overlay `%s` failed to initialize", overlay->ops->name);
overlay->ops = NULL;
}
}
ll_unlock(g_state.overlays);
}
static inline void mergeRect(struct Rect * dest, const struct Rect * a, const struct Rect * b)
{
int x2 = max(a->x + a->w, b->x + b->w);
int y2 = max(a->y + a->h, b->y + b->h);
dest->x = min(a->x, b->x);
dest->y = min(a->y, b->y);
dest->w = x2 - dest->x;
dest->h = y2 - dest->y;
}
static inline LG_DSPointer mapImGuiCursor(ImGuiMouseCursor cursor)
{
switch (cursor)
{
case ImGuiMouseCursor_None:
return LG_POINTER_NONE;
case ImGuiMouseCursor_Arrow:
return LG_POINTER_ARROW;
case ImGuiMouseCursor_TextInput:
return LG_POINTER_INPUT;
case ImGuiMouseCursor_ResizeAll:
return LG_POINTER_MOVE;
case ImGuiMouseCursor_ResizeNS:
return LG_POINTER_RESIZE_NS;
case ImGuiMouseCursor_ResizeEW:
return LG_POINTER_RESIZE_EW;
case ImGuiMouseCursor_ResizeNESW:
return LG_POINTER_RESIZE_NESW;
case ImGuiMouseCursor_ResizeNWSE:
return LG_POINTER_RESIZE_NWSE;
case ImGuiMouseCursor_Hand:
return LG_POINTER_HAND;
case ImGuiMouseCursor_NotAllowed:
return LG_POINTER_NOT_ALLOWED;
default:
return LG_POINTER_ARROW;
}
}
bool app_overlayNeedsRender(void)
{
if (app_isOverlayMode())
return true;
bool result = false;
struct Overlay * overlay;
ll_lock(g_state.overlays);
ll_forEachNL(g_state.overlays, item, overlay)
{
if (!overlay->ops->needs_render)
continue;
if (overlay->ops->needs_render(overlay->udata, false))
{
result = true;
break;
}
}
ll_unlock(g_state.overlays);
return result;
}
int app_renderOverlay(struct Rect * rects, int maxRects)
{
int totalRects = 0;
bool totalDamage = false;
struct Overlay * overlay;
struct Rect buffer[MAX_OVERLAY_RECTS];
g_state.io->KeyCtrl = g_state.modCtrl;
g_state.io->KeyShift = g_state.modShift;
g_state.io->KeyAlt = g_state.modAlt;
g_state.io->KeySuper = g_state.modSuper;
uint64_t now = nanotime();
g_state.io->DeltaTime = (now - g_state.lastImGuiFrame) * 1e-9f;
g_state.lastImGuiFrame = now;
render_again:
igNewFrame();
const bool overlayMode = app_isOverlayMode();
if (overlayMode && g_params.overlayDim)
{
totalDamage = true;
ImDrawList_AddRectFilled(igGetBackgroundDrawList_Nil(), (ImVec2) { 0.0f , 0.0f },
g_state.io->DisplaySize,
igGetColorU32_Col(ImGuiCol_ModalWindowDimBg, 1.0f),
0, 0);
}
const bool msgModal = overlayMsg_modal();
// render the overlays
ll_lock(g_state.overlays);
ll_forEachNL(g_state.overlays, item, overlay)
{
if (msgModal && overlay->ops != &LGOverlayMsg)
continue;
const int written =
overlay->ops->render(overlay->udata, overlayMode,
buffer, MAX_OVERLAY_RECTS);
for (int i = 0; i < written; ++i)
{
buffer[i].x *= g_state.windowScale;
buffer[i].y *= g_state.windowScale;
buffer[i].w *= g_state.windowScale;
buffer[i].h *= g_state.windowScale;
}
// It is an error to run out of rectangles, because we will not be able to
// correctly calculate the damage of the next frame.
DEBUG_ASSERT(written >= 0);
const int toAdd = max(written, overlay->lastRectCount);
totalDamage |= toAdd > maxRects;
if (!totalDamage && toAdd)
{
int i = 0;
for (; i < overlay->lastRectCount && i < written; ++i)
mergeRect(rects + i, buffer + i, overlay->lastRects + i);
// only one of the following memcpys will copy non-zero bytes.
memcpy(rects + i, buffer + i, (written - i) * sizeof(struct Rect));
memcpy(rects + i, overlay->lastRects + i, (overlay->lastRectCount - i) * sizeof(struct Rect));
rects += toAdd;
totalRects += toAdd;
maxRects -= toAdd;
}
memcpy(overlay->lastRects, buffer, sizeof(struct Rect) * written);
overlay->lastRectCount = written;
}
ll_unlock(g_state.overlays);
if (overlayMode)
{
ImGuiMouseCursor cursor = igGetMouseCursor();
if (cursor != g_state.cursorLast)
{
g_state.ds->setPointer(mapImGuiCursor(cursor));
g_state.cursorLast = cursor;
}
}
igRender();
/* imgui requires two passes to calculate the bounding box of auto sized
* windows, this is by design
* ref: https://github.com/ocornut/imgui/issues/2158#issuecomment-434223618
*/
if (g_state.renderImGuiTwice)
{
g_state.renderImGuiTwice = false;
goto render_again;
}
return totalDamage ? -1 : totalRects;
}
void app_freeOverlays(void)
{
struct Overlay * overlay;
while(ll_shift(g_state.overlays, (void **)&overlay))
{
overlay->ops->free(overlay->udata);
free(overlay);
}
}
void app_setOverlay(bool enable)
{
if (g_state.overlayInput == enable)
return;
g_state.overlayInput = enable;
core_updateOverlayState();
}
void app_overlayConfigRegister(const char * title,
void (*callback)(void * udata, int * id), void * udata)
{
overlayConfig_register(title, callback, udata);
}
void app_overlayConfigRegisterTab(const char * title,
void (*callback)(void * udata, int * id), void * udata)
{
overlayConfig_registerTab(title, callback, udata);
}