-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGtk.c
1036 lines (809 loc) · 27.1 KB
/
Gtk.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
/* This file is part of the Elliott 803 emulator.
Copyright © 2020 Peter Onion
See LICENCE file.
*/
// gtk interface
#define _GNU_SOURCE
#define G_LOG_USE_STRUCTURED
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <EGL/egl.h>
#include <assert.h>
#include <cglm/cglm.h>
#include "Gtk.h"
#include "Gles.h"
#include "3D.h"
#include "ObjLoader.h"
#include "Keyboard.h"
#include "Hands.h"
#include "Wiring.h"
#include "Logging.h"
#include "Common.h"
extern void FrontOffset2(HandInfo *hand);
static GtkBuilder *builder = NULL;
static GError *err = NULL;
static GtkWidget *mainWindow;
GtkWidget *eventBox;
GdkCursor *blankCursor,*savedCursor;
GtkWidget *splashWindow;
GtkWidget *splashImage;
static EGLDisplay eglDisplay;
static EGLSurface eglSurface;
static EGLContext eglContext;
static int windowWidth,windowHeight;
static GString *shaderPath = NULL;
static gboolean shiftPressed = FALSE;
static gboolean controlPressed = FALSE;
static GdkSeat *seat = NULL;
static GdkWindow *cursorWindow = NULL;
static gfloat grabedAtX,grabedAtY;
static gfloat pointerAtX,pointerAtY;
static gboolean justGrabbed = FALSE;
static gboolean navigating = FALSE;
static gdouble MouseX,MouseY;
/* Widget event handler prototypes */
gboolean
on_quitButton_clicked (__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEvent *event,
__attribute__((unused)) gpointer user_data);
gboolean
on_ResetViewButton_clicked (__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEvent *event,
__attribute__((unused)) gpointer user_data);
gboolean
on_MainsOnButton_clicked (__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEvent *event,
__attribute__((unused)) gpointer user_data);
gboolean
on_MainsOffButton_clicked (__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEvent *event,
__attribute__((unused)) gpointer user_data);
void on_eventBox_realize(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) gpointer data);
gboolean
on_eventBox_draw(__attribute__((unused)) GtkWidget *eventBox2,
__attribute__((unused)) cairo_t *cr,
__attribute__((unused)) gpointer data);
gboolean
on_eventBox_motion_notify_event(__attribute__((unused)) GtkWidget *widget,
GdkEventMotion *event,
__attribute__((unused)) gpointer user_data);
gboolean on_eventBox_key_press_event(__attribute__((unused))GtkWidget *widget,
GdkEvent *event,
__attribute__((unused))gpointer user_data);
gboolean on_eventBox_key_release_event(__attribute__((unused))GtkWidget *widget,
GdkEvent *event,
__attribute__((unused))gpointer user_data);
gboolean
on_eventBox_button_press_event(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEventButton *event,
__attribute__((unused)) gpointer user_data);
gboolean
on_eventBox_button_release_event(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEventButton *event,
__attribute__((unused)) gpointer user_data);
gboolean
on_eventBox_enter_notify_event(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEvent *event,
__attribute__((unused)) gpointer user_data);
gboolean
on_ResetViewButton_clicked (__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEvent *event,
__attribute__((unused)) gpointer user_data)
{
GdkEventKey ek;
GdkDisplay *display;
GtkAllocation allocation;
// Reposition camera.
ResetScene();
// Reset hand states and positions
LeftHandInfo = ResetLeft;
RightHandInfo = ResetRight;
// Reset the MVP
gtk_widget_get_allocation(eventBox,&allocation);
UpdateMVP(allocation.width,allocation.height);
// simulate the end of tracking by calling
// on_eventBox_key_release_event to reset everything
display = gdk_display_get_default();
seat = gdk_display_get_default_seat(display);
ek.keyval = 0;
shiftPressed = FALSE;
controlPressed = FALSE;
on_eventBox_key_release_event(eventBox,(GdkEvent *) &ek,NULL);
// Turn the cursor off
gdk_window_set_cursor(gtk_widget_get_window(eventBox),blankCursor);
return GDK_EVENT_PROPAGATE ;
}
// These two are needed until there is a mains switch in the 3d space.
gboolean
on_MainsOnButton_clicked (__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEvent *event,
__attribute__((unused)) gpointer user_data)
{
wiring(MAINS_SUPPLY_ON,0);
return GDK_EVENT_PROPAGATE ;
}
gboolean
on_MainsOffButton_clicked (__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEvent *event,
__attribute__((unused)) gpointer user_data)
{
wiring(MAINS_SUPPLY_OFF,0);
return GDK_EVENT_PROPAGATE ;
}
gboolean
on_quitButton_clicked (__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEvent *event,
__attribute__((unused)) gpointer user_data)
{
gtk_main_quit();
return FALSE;
}
// Seems an event box doesn't automatically get focus when the mouse enters it, so
// do it explicitly.
gboolean
on_eventBox_enter_notify_event(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEvent *event,
__attribute__((unused)) gpointer user_data)
{
//printf("%s called\n",__FUNCTION__);
gtk_widget_grab_focus(widget);
return FALSE;
}
extern gboolean MOUSE_WARPED;
gboolean
on_eventBox_motion_notify_event(__attribute__((unused)) GtkWidget *widget,
GdkEventMotion *event,
__attribute__((unused)) gpointer user_data)
{
GdkEventMotion *em;
static gfloat lastx,lasty;
static gboolean yTopWarped = FALSE;
static gboolean yBottomWarped = FALSE;
static gboolean xLeftWarped = FALSE;
static gboolean xRightWarped = FALSE;
gfloat mouseMoveLR,mouseMoveUD;
gfloat X_root,Y_root;
GtkAllocation allocation;
static gfloat pointerx,pointery;
em = (GdkEventMotion *) event;
// For mouse position debugging only
MouseX = em->x;
MouseY = em->y;
X_root = (gfloat) em->x_root;
Y_root = (gfloat) em->y_root;
pointerAtX = X_root;
pointerAtY = Y_root;
// Sometimes after warping the mouse there is still an event in the queue after the
// one that caused the warp and before the event cause by the warp. A simple solution is to ignore
// first event after a warp.
if(MOUSE_WARPED)
{
//printf("**** MOTION AFTER WARP (%f,%f) ******\n",MouseX,MouseY);
MOUSE_WARPED = FALSE;
return GDK_EVENT_STOP ;
}
if((pointerx == pointerAtX) && (pointery == pointerAtY))
{
//printf("%s NO MOTION\n",__FUNCTION__);
return GDK_EVENT_STOP ;
}
pointerx = pointerAtX;
pointery = pointerAtY;
gtk_widget_get_allocation(widget,&allocation);
if(justGrabbed)
{
lastx = X_root;
lasty = Y_root;
justGrabbed = FALSE;
navigating = TRUE;
}
else
{
if(navigating)
{
// When navigating (rather than moving a hand) when the mouse reaches an edge
// of the event box it is warped back to the center. THis is unseen by the
// user as the mouse cursor is hidden.
if(!yTopWarped && !yBottomWarped && !xLeftWarped && !xRightWarped)
{
mouseMoveLR = X_root - lastx;
mouseMoveUD = Y_root - lasty;
lastx = X_root;
lasty = Y_root;
UpdateUser(mouseMoveLR,mouseMoveUD,shiftPressed,controlPressed);
}
if(em->x < 0.0)
{
if(!xLeftWarped)
{
X_root += (gfloat)allocation.width * 0.5f;
gdk_device_warp (em->device,
gdk_screen_get_default(),
(gint) X_root,
(gint) Y_root);
lastx = X_root;
xLeftWarped = TRUE;
}
}
else
xLeftWarped = FALSE;
if(em->x > (gdouble)allocation.width)
{
if(!xRightWarped)
{
X_root -= (gfloat)allocation.width * 0.5f;
gdk_device_warp (em->device,
gdk_screen_get_default(),
(gint) X_root,
(gint) Y_root);
lastx = X_root;
xRightWarped = TRUE;
}
}
else
xRightWarped = FALSE;
if(em->y < 0.0)
{
if(!yTopWarped)
{
Y_root += (float)allocation.height * 0.5f;
gdk_device_warp (em->device,
gdk_screen_get_default(),
(gint) X_root,
(gint) Y_root);
lasty = Y_root;
yTopWarped = TRUE;
}
}
else
yTopWarped = FALSE;
if(em->y > (gdouble)allocation.height)
{
if(!yBottomWarped)
{
Y_root -= (gfloat)allocation.height * 0.5f;
gdk_device_warp (em->device,
gdk_screen_get_default(),
(gint) X_root,
(gint) Y_root);
lasty = Y_root;
yBottomWarped = TRUE;
}
}
else
yBottomWarped = FALSE;
}
else
{ // Following a hand
GLuint depth;
GLfloat z,y,x;
vec4 MouseXY = {(float)em->x,(float)em->y,0.0f,0.0f};
eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
depth = getDepth(&allocation,(GLint)em->x,(GLint)(allocation.height-em->y));
// Unproject the mouse coordinates and the depth
z = (GLfloat) (depth / 4294967296.0);
x = (GLfloat) em->x;
y = (GLfloat) (allocation.height - em->y);
glm_unprojecti((vec3){x,y,z},mvpMatrixInv,(vec4){0.0,0.0,(GLfloat)allocation.width,
(GLfloat) allocation.height},XwiresXYZ);
// Tell relevent device about the cursor and mouse location.
// Only one for now
PointerOverKeyboard(XwiresXYZ,MouseXY,event->time);
}
}
return GDK_EVENT_STOP ;
}
gboolean on_eventBox_key_press_event(__attribute__((unused))GtkWidget *widget,
GdkEvent *event,
__attribute__((unused))gpointer user_data)
{
GdkEventKey *ek;
gboolean grab = FALSE;
ek = (GdkEventKey *)event;
switch(ek->keyval)
{
case GDK_KEY_Shift_L:
//printf("Pressed GDK_KEY_Shift_L\n");
grab = shiftPressed = TRUE;
break;
case GDK_KEY_Control_L:
//printf("Pressed GDK_KEY_Control_L\n");
grab = controlPressed = TRUE;
break;
case GDK_KEY_q:
gtk_main_quit();
break;
case GDK_KEY_z:
if((!shiftPressed) && (!controlPressed))
{
HandInfo *tmp;
enum WgZones zone;
// Don't swap hands if
// Inactive hand has been pushed off the edge or
// Inactive hand is off screnn
if(( !((InactiveHand->InZone == OFF_PISTE) &&
(InactiveHand->Pushed != NOT_PUSHED)) ) &&
(InactiveHand->Trackable))
{
if(InactiveHand->Pushed != NOT_PUSHED)
{
glm_vec4_add(InactiveHand->PushedOffsetXYZ,
InactiveHand->FingerAtXYZ,
InactiveHand->FingerAtXYZ);
glm_vec4_zero(InactiveHand->PushedOffsetXYZ);
InactiveHand->Pushed = NOT_PUSHED;
zone = XYZtoZone(InactiveHand->FingerAtXYZ);
if( (( InactiveHand->LeftHand) && (zone == WG_FRONT_RIGHT_INNER)) ||
((!InactiveHand->LeftHand) && (zone == WG_FRONT_LEFT_INNER )) )
zone = WG_OPERATE;
InactiveHand->InZone = zone;
}
warpMouseToXYZ(InactiveHand->FingerAtXYZ);
tmp = ActiveHand;
ActiveHand = InactiveHand;
InactiveHand = tmp;
InactiveHand->SnapState = 0;
}
}
break;
default:
break;
}
if(grab && !seat)
{
GdkDisplay *display;
GdkWindow *window;
window = gtk_widget_get_window(eventBox);
display = gdk_display_get_default();
seat = gdk_display_get_default_seat(display);
grabedAtX = pointerAtX; // = eb->x_root;
grabedAtY = pointerAtY; // = eb->y_root;
justGrabbed = TRUE;
cursorWindow = window;
gdk_seat_grab (seat,
window,
GDK_SEAT_CAPABILITY_POINTER|GDK_SEAT_CAPABILITY_KEYBOARD,
FALSE, //gboolean owner_events,
NULL, //GdkCursor *cursor,
NULL, //const GdkEvent *event,
NULL, //GdkSeatGrabPrepareFunc prepare_func,
NULL //gpointer prepare_func_data);
);
}
return GDK_EVENT_PROPAGATE ;
}
gboolean on_eventBox_key_release_event(__attribute__((unused)) GtkWidget *widget,
GdkEvent *event,
__attribute__((unused)) gpointer user_data)
{
GdkEventKey *ek;
ek = (GdkEventKey *)event;
switch(ek->keyval)
{
case GDK_KEY_Shift_L:
//printf("Released GDK_KEY_Shift_L\n");
shiftPressed = FALSE;
break;
case GDK_KEY_Control_L:
//printf("Released GDK_KEY_Control_L\n");
controlPressed = FALSE;
break;
default:
break;
}
if((!shiftPressed) && (!controlPressed))
{
GtkAllocation allocation;
// Update the draawing used for getting depths.
gtk_widget_get_allocation(widget,&allocation);
eglMakeCurrent (eglDisplay, eglSurface, eglSurface, eglContext);
DrawKeyboardForDepthTracking(&allocation);
}
if(!shiftPressed && !controlPressed && seat!=NULL) // i.e. if(released)
{
vec3 ActiveMouseXYZ,InactiveMouseXYZ;
int ox,oy;
GdkWindow *win;
GtkAllocation allocation;
int flags;
HandInfo *tmp;
gdk_seat_ungrab(seat);
win = gtk_widget_get_parent_window (eventBox);
gdk_window_get_origin (win,&ox,&oy);
gtk_widget_get_allocation(eventBox,&allocation);
// If the original Finger position is still on the screen, move the mouse pointer
// back to the same XYZ position
glm_project( ActiveHand->FingerAtXYZ,mvpMatrix,(vec4){0.0,0.0,(GLfloat)allocation.width,
(GLfloat) allocation.height},ActiveMouseXYZ);
// Round mouse to nearest integer values to stop mouse drift.
ActiveMouseXYZ[0] = roundf(ActiveMouseXYZ[0]);
ActiveMouseXYZ[1] = roundf(ActiveMouseXYZ[1]);
ActiveMouseXYZ[2] = roundf(ActiveMouseXYZ[2]);
glm_project( InactiveHand->FingerAtXYZ,mvpMatrix,(vec4){0.0,0.0,(GLfloat)allocation.width,
(GLfloat) allocation.height},InactiveMouseXYZ);
// Round mouse to nearest integer values to stop mouse drift.
InactiveMouseXYZ[0] = roundf(InactiveMouseXYZ[0]);
InactiveMouseXYZ[1] = roundf(InactiveMouseXYZ[1]);
InactiveMouseXYZ[2] = roundf(InactiveMouseXYZ[2]);
flags = 0;
if((ActiveMouseXYZ[0] >= 0.0f) && (ActiveMouseXYZ[0] <= (GLfloat)allocation.width) &&
(ActiveMouseXYZ[1] >= 0.0f) && (ActiveMouseXYZ[1] <= (GLfloat) allocation.height)) flags |= 1;
if((InactiveMouseXYZ[0] >= 0.0f) && (InactiveMouseXYZ[0] <= (GLfloat)allocation.width) &&
(InactiveMouseXYZ[1] >= 0.0f) && (InactiveMouseXYZ[1] <= (GLfloat) allocation.height)) flags |= 2;
g_debug("flags = %d",flags);
switch(flags)
{
case 0:
ActiveHand->Trackable = FALSE;
InactiveHand->Trackable = FALSE;
// Put cursor in the middle of the window
gdk_device_warp (gdk_seat_get_pointer(seat),
gdk_screen_get_default(),
ox + (allocation.width / 2),
oy + (allocation.height / 2));
break;
case 1:
ActiveHand->Trackable = TRUE;
InactiveHand->Trackable = FALSE;
// Put cursor at active hand
gdk_device_warp (gdk_seat_get_pointer(seat),
gdk_screen_get_default(),
ox + (gint) ActiveMouseXYZ[0],
oy + allocation.height - (gint) ActiveMouseXYZ[1]);
break;
case 2:
tmp = ActiveHand;
ActiveHand = InactiveHand;
InactiveHand = tmp;
ActiveHand->Trackable = TRUE;
InactiveHand->Trackable = FALSE;
// Put cursor at inactive hand
gdk_device_warp (gdk_seat_get_pointer(seat),
gdk_screen_get_default(),
ox + (gint) InactiveMouseXYZ[0],
oy + allocation.height - (gint) InactiveMouseXYZ[1]);
break;
case 3:
ActiveHand->Trackable = TRUE;
InactiveHand->Trackable = TRUE;
// Put cursor at active hand
gdk_device_warp (gdk_seat_get_pointer(seat),
gdk_screen_get_default(),
ox + (gint) ActiveMouseXYZ[0],
oy + allocation.height - (gint) ActiveMouseXYZ[1]);
break;
}
seat = NULL;
justGrabbed = FALSE;
navigating = FALSE;
}
return GDK_EVENT_PROPAGATE ;
}
static gboolean
on_eventBox_button_press_eventNew(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEventButton *event,
__attribute__((unused)) gpointer user_data)
{
enum WgZones zone;
// Don't act on any presses while navigating
if((!shiftPressed) && (!controlPressed))
{
HandInfo *tmp;
// New swapping method
if(((event->button == 1) && ( InactiveHand->LeftHand) ) ||
((event->button == 3) && ( ActiveHand->LeftHand)))
{
// Don't swap hands if
// Inactive hand has been pushed off the edge or
// Inactive hand is off screnn
if(( !((InactiveHand->InZone == OFF_PISTE) && (InactiveHand->Pushed != NOT_PUSHED)) ) &&
(InactiveHand->Trackable))
{
if(InactiveHand->Pushed != NOT_PUSHED)
{
glm_vec4_add(InactiveHand->PushedOffsetXYZ,InactiveHand->FingerAtXYZ, InactiveHand->FingerAtXYZ);
glm_vec4_zero(InactiveHand->PushedOffsetXYZ);
InactiveHand->Pushed = NOT_PUSHED;
zone = XYZtoZone(InactiveHand->FingerAtXYZ);
if( (( InactiveHand->LeftHand) && (zone == WG_FRONT_RIGHT_INNER)) ||
((!InactiveHand->LeftHand) && (zone == WG_FRONT_LEFT_INNER )) )
zone = WG_OPERATE;
InactiveHand->InZone = zone;
}
warpMouseToXYZ(InactiveHand->FingerAtXYZ);
tmp = ActiveHand;
ActiveHand = InactiveHand;
InactiveHand = tmp;
// Leave snap state as is so still snapped when swapped back
//InactiveHand->SnapState = 0;
}
return GDK_EVENT_STOP;
}
}
printf("ActiveHand->NearestButton=%p ActiveHand->SnapState=%d\n",
ActiveHand->NearestButton,ActiveHand->SnapState);
if( (ActiveHand->NearestButton != NULL) && (ActiveHand->SnapState != 0))
{
ActiveHand->fingersPressed = 1;
if( (ActiveHand->NearestButton->objectId != 70) && (ActiveHand->NearestButton->handler != NULL))
{
(ActiveHand->NearestButton->handler)(ActiveHand->NearestButton,event->type == GDK_BUTTON_PRESS,event->time);
}
}
return GDK_EVENT_PROPAGATE ;
}
static gboolean
on_eventBox_button_release_eventNew(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEventButton *event,
__attribute__((unused)) gpointer user_data)
{
//if(event->button == 3) return GDK_EVENT_PROPAGATE ;
ActiveHand->fingersPressed = 0;
if(ActiveHand->NearestButton != NULL)
{
if(ActiveHand->NearestButton->handler != NULL)
{
(ActiveHand->NearestButton->handler)(ActiveHand->NearestButton,event->type == GDK_BUTTON_PRESS,event->time);
}
warpMouseToXYZ(ActiveHand->FingerAtXYZ);
}
return GDK_EVENT_PROPAGATE ;
}
static gboolean
on_eventBox_button_press_eventOld(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEventButton *event,
__attribute__((unused)) gpointer user_data)
{
enum WgZones zone;
// Don't allow hand swap while navigating
if((event->button == 3) && (!shiftPressed) && (!controlPressed))
{
HandInfo *tmp;
// Don't swap hands if
// Inactive hand has been pushed off the edge or
// Inactive hand is off screnn
if(( !((InactiveHand->InZone == OFF_PISTE) && (InactiveHand->Pushed != NOT_PUSHED)) ) &&
(InactiveHand->Trackable))
{
if(InactiveHand->Pushed != NOT_PUSHED)
{
glm_vec4_add(InactiveHand->PushedOffsetXYZ,InactiveHand->FingerAtXYZ, InactiveHand->FingerAtXYZ);
glm_vec4_zero(InactiveHand->PushedOffsetXYZ);
InactiveHand->Pushed = NOT_PUSHED;
zone = XYZtoZone(InactiveHand->FingerAtXYZ);
if( (( InactiveHand->LeftHand) && (zone == WG_FRONT_RIGHT_INNER)) ||
((!InactiveHand->LeftHand) && (zone == WG_FRONT_LEFT_INNER )) )
zone = WG_OPERATE;
InactiveHand->InZone = zone;
}
warpMouseToXYZ(InactiveHand->FingerAtXYZ);
tmp = ActiveHand;
ActiveHand = InactiveHand;
InactiveHand = tmp;
// Leave snap state as is so still snapped when swapped back
//InactiveHand->SnapState = 0;
}
return GDK_EVENT_STOP;
}
if( (ActiveHand->NearestButton != NULL) && (ActiveHand->SnapState != 0))
{
ActiveHand->fingersPressed = 1;
if( (ActiveHand->NearestButton->objectId != 70) && (ActiveHand->NearestButton->handler != NULL))
{
(ActiveHand->NearestButton->handler)(ActiveHand->NearestButton,event->type == GDK_BUTTON_PRESS,event->time);
}
}
return GDK_EVENT_PROPAGATE ;
}
static gboolean
on_eventBox_button_release_eventOld(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEventButton *event,
__attribute__((unused)) gpointer user_data)
{
if(event->button == 3) return GDK_EVENT_PROPAGATE ;
ActiveHand->fingersPressed = 0;
if(ActiveHand->NearestButton != NULL)
{
if(ActiveHand->NearestButton->handler != NULL)
{
(ActiveHand->NearestButton->handler)(ActiveHand->NearestButton,event->type == GDK_BUTTON_PRESS,event->time);
}
warpMouseToXYZ(ActiveHand->FingerAtXYZ);
}
return GDK_EVENT_PROPAGATE ;
}
/*
Old : Use right mouse click to swap active hand
Use left mouse click to press buttons
New: If left hand active, left click presses and right click swaps hands
If Right hand active, right click presses and left clcik swaps hands
*/
gboolean
on_eventBox_button_press_event(GtkWidget *widget,
GdkEventButton *event,
gpointer user_data)
{
gboolean ret;
if(oldHandSwap)
{
ret = on_eventBox_button_press_eventOld(widget,event,user_data);
}
else
{
ret = on_eventBox_button_press_eventNew(widget,event,user_data);
}
return ret;
}
gboolean
on_eventBox_button_release_event(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEventButton *event,
__attribute__((unused)) gpointer user_data)
{
gboolean ret;
if(oldHandSwap)
{
ret = on_eventBox_button_release_eventOld(widget,event,user_data);
}
else
{
ret = on_eventBox_button_release_eventNew(widget,event,user_data);
}
return ret;
}
// Lots happens here ! EGL and OpenGLES are intiailised.
void on_eventBox_realize(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) gpointer data)
{
EGLBoolean result;
EGLint num_config;
GdkWindow *gdkWindow;
Window xWindow;
GtkAllocation allocation;
static const EGLint attribute_list[] =
{
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE,16,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_NONE
};
static const EGLint context_attributes[] =
{
EGL_CONTEXT_CLIENT_VERSION, 3,
EGL_NONE
};
EGLConfig config;
// Recover the xwindows window id from the gtk widget
gdkWindow = gtk_widget_get_window (widget);
xWindow = gdk_x11_window_get_xid (gdkWindow);
// get an EGL display connection
eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
assert(eglDisplay != EGL_NO_DISPLAY);
// initialize the EGL display connection
result = eglInitialize(eglDisplay, NULL, NULL);
assert(EGL_FALSE != result);
// get an appropriate EGL frame buffer configuration
result = eglChooseConfig(eglDisplay, attribute_list, &config, 1, &num_config);
assert(EGL_FALSE != result);
// get an appropriate EGL frame buffer configuration
result = eglBindAPI(EGL_OPENGL_ES_API);
assert(EGL_FALSE != result);
// create an EGL rendering context
eglContext = eglCreateContext(eglDisplay, config, EGL_NO_CONTEXT, context_attributes);
assert(eglContext!=EGL_NO_CONTEXT);
gtk_widget_get_allocation(widget,&allocation);
windowWidth = allocation.width;
windowHeight = allocation.height;
eglSurface = eglCreateWindowSurface( eglDisplay, config, xWindow, NULL );
assert(eglSurface != EGL_NO_SURFACE);
//eglSurfaceAttrib(eglDisplay,eglSurface,EGL_SWAP_BEHAVIOR,EGL_BUFFER_DESTROYED);
// connect the context to the surface
result = eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
assert(EGL_FALSE != result);
// Display update rate is set by timer callback.
eglSwapInterval(eglDisplay,0); // 1 = 60Hz 2 = 30Hz
GlesInit(shaderPath,windowWidth,windowHeight);
// Assume all blender files loaded so ...
loadTextures2();
UpdateMVP(allocation.width , allocation.height);
DrawKeyboardForDepthTracking(&allocation);
}
gboolean
on_eventBox_draw(__attribute__((unused)) GtkWidget *eventBox2,
__attribute__((unused)) cairo_t *cr,
__attribute__((unused)) gpointer data)
{
GtkAllocation allocation;
eglMakeCurrent (eglDisplay, eglSurface, eglSurface, eglContext);
gtk_widget_get_allocation(eventBox2,&allocation);
UpdateMVP(allocation.width , allocation.height);
GlesDraw(allocation.width , allocation.height);
eglSwapBuffers(eglDisplay,eglSurface);
return GDK_EVENT_PROPAGATE ;
}
void UpdateScreen(void)
{
gtk_widget_queue_draw(eventBox);
}
static gboolean timerTick(__attribute__((unused)) gpointer user_data)
{
KeyboardTimerTick2();
UpdateScreen();
return G_SOURCE_CONTINUE;
}
// Helper
static GObject *gtk_builder_get_object_CHECKed(GtkBuilder *_builder,const gchar *name)
{
GObject *gotten;
gotten = gtk_builder_get_object (_builder,name);
if(gotten == NULL)
{
g_error("FAILED TO GET (%s)\n",name);
}
return gotten;
}
// Macro for geting widgets from the builder
#define GETWIDGET(name,type) name=type(gtk_builder_get_object_CHECKed (builder,#name))
gboolean GtkInit(GString *sharedPath,int *argc,char ***argv)
{
GString *gladeFileName;
shaderPath = g_string_new(sharedPath->str);
g_string_append(shaderPath,"shaders/");
// Normal GTK application start up
gtk_init (argc, argv);
// Create a builder
builder = gtk_builder_new();
// And read gui definition from a file
gladeFileName = g_string_new(sharedPath->str);
g_string_append(gladeFileName,"generic900x900.glade");
if( gtk_builder_add_from_file (builder,gladeFileName->str , &err) == 0)
{
g_info("gtk_builder_add_from_file (%s) FAILED (%s)\n",gladeFileName->str,err->message);
g_string_free(gladeFileName,TRUE);
return(FALSE);
}
g_string_free(gladeFileName,TRUE);
GETWIDGET(mainWindow,GTK_WIDGET);
GETWIDGET(eventBox,GTK_WIDGET);
GETWIDGET(splashWindow,GTK_WIDGET);
GETWIDGET(splashImage,GTK_WIDGET);
{
GString *splashPath;
splashPath = g_string_new(sharedPath->str);
g_string_append(splashPath,"graphics/803splash.png");
gtk_image_set_from_file(GTK_IMAGE(splashImage),splashPath->str);
g_string_free(splashPath,TRUE);
}
return TRUE;
}
void showCursor(void)
{
gdk_window_set_cursor(gtk_widget_get_window(eventBox),savedCursor);
}
void hideCursor(void)
{
gdk_window_set_cursor(gtk_widget_get_window(eventBox),blankCursor);