Source file b_main.ml
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
(** BOGUE *)
(** A GUI Library for Ocaml, using SDL2 *)
(** Vu Ngoc San, December 2013 -- now *)
open B_utils
open Tsdl
module Avar = B_avar
module Draw = B_draw
module E = Sdl.Event
module Layout = B_layout
module Mouse = B_mouse
module Print = B_print
module Shortcut = B_shortcut
module Sync = B_sync
module Time = B_time
module Timeout = B_timeout
module Trigger = B_trigger
module Update = B_update
module Var = B_var
module Widget = B_widget
module Window = B_window
module ISet = Set.Make(Int)
exception Exit
type board = {
mutable windows: Window.t list;
windows_house: Layout.t;
mutable mouse_focus: Layout.t option;
mutable keyboard_focus: Layout.t option;
mutable button_down: Layout.t option;
mutable shortcuts: shortcuts Var.t;
mutable shortcut_pressed: bool;
mutable mouse_alive: bool;
on_user_event : (Sdl.event -> unit) option
}
and shortcuts = board Shortcut.t
let running_board = ref None
let get_frame () = !Avar.frame
let get_layouts board =
List.map Window.get_layout board.windows
let get_mouse_focus board =
board.mouse_focus
let set_windows board windows =
board.windows <- windows;
board.windows_house.Layout.content <-
Layout.Rooms (List.map Window.get_layout windows)
let close_window window =
let layout = Window.get_layout window in
printd debug_board "** Closing window #%u (Layout %s)"
(Window.id window) (Layout.sprint_id layout);
if !Avar.alive_animations > 0
then begin
printd debug_warning "%d animation%s not stopped. We reset the counter."
!Avar.alive_animations (if !Avar.alive_animations = 1
then " was" else "s were");
Avar.alive_animations := 0
end;
List.iter Widget.remove_active_connections (Layout.get_widgets layout);
if Sdl.is_text_input_active () then Sdl.stop_text_input ();
Layout.delete_textures layout;
let canvas = Layout.get_canvas layout in
Layout.remove_canvas layout;
Draw.destroy_canvas ~bogue:window.Window.bogue canvas
let check_cemetery () =
let nzombies = List.length !Layout.cemetery in
if Layout.check_cemetery ()
then printd debug_memory "All zombies have been killed! Congratulations!"
else if !debug then begin
printd debug_memory "==> Still some living deads around. Invoking GC";
Gc.compact ();
if Layout.check_cemetery ()
then printd debug_memory "All zombies have been killed! Congratulations!"
else printd debug_memory "Percentage of killed zombies = %u%% (out of %u)."
(round (100. -. 100. *. (float (List.length !Layout.cemetery) /.
(float nzombies)))) nzombies
end
let exit_board board =
if Sync.execute 1000
then printd debug_warning "Some Sync action was queueing and hence executed \
before exiting board.";
Update.clear ();
Timeout.clear ();
check_cemetery ();
List.iter close_window board.windows;
board.mouse_focus <- None;
board.keyboard_focus <- None;
board.button_down <- None;
Draw.destroy_textures ();
Draw.check_memory ();
Trigger.flush_all ();
flush_log ()
let quit = Draw.quit
(** redisplay the layouts to the layers *)
let display board =
Trigger.(flush redraw);
List.iter (fun w ->
if not (Window.is_fresh w) then Window.render w)
board.windows
(** Render all layers and flip window buffers, only for windows with the
is_fresh=false field *)
let flip ?clear board =
List.iter (Window.flip ?clear) board.windows;
Draw.destroy_textures ()
(** Update layout of window that was resized by user *)
let resize window =
let layout = Window.get_layout window in
if Window.size window <> Layout.get_physical_size layout
then begin
printd debug_graphics "Resize window (Layout #%u)" layout.Layout.id;
Layout.resize_from_window ~flip:false layout;
Window.to_refresh window
end
let add_window board layout =
Layout.move_to_new_stack layout;
let window = Window.create layout in
Window.make_sdl_window window;
let windows = List.rev (window :: (List.rev board.windows)) in
set_windows board windows;
Sdl.show_window (Layout.window layout);
Draw.update_background (Layout.get_canvas layout);
display board;
do_option (get_mouse_focus board) Layout.set_focus;
flip board;
List.iter (Widget.wake_up (Trigger.startup_event ()))
(List.flatten (List.map Widget.connections (Layout.get_widgets layout)));
Trigger.renew_my_event ();
window
let same_window w1 w2 = Sdl.(get_window_id w1 = get_window_id w2)
(** get window (layout) by id. Not used... (layout_event can do it somehow) *)
let get_window_by_id board id =
let rec loop = function
| [] -> printd debug_error "There is no window with this id#%d" id;
List.hd board.windows;
| w::rest -> if id = Window.id w then w
else loop rest in
loop board.windows
let remove_window board window =
let windows = List.filter (fun w -> not (Window.equal window w))
board.windows in
set_windows board windows;
close_window window;
board.mouse_focus <- None;
board.keyboard_focus <- None;
board.button_down <- None;
if board.windows = [] then begin
printd debug_board "No more windows. We quit.";
raise Exit
end else if not (List.exists Window.is_shown board.windows) then begin
printd (debug_board + debug_user)
"Some windows are alive, but all windows are hidden. We quit.";
raise Exit
end
let show board =
List.iter (fun w ->
Window.show_maybe w;
Window.to_refresh w;
Draw.update_background (Window.get_canvas w)) board.windows
let remove_mouse_focus board ro =
match board.mouse_focus, ro with
| Some br, Some r -> if Layout.equal br r || not (Layout.accept_focus br)
then board.mouse_focus <- None
| Some br, _ -> if not (Layout.accept_focus br)
then board.mouse_focus <- None
| _ -> ()
let remove_keyboard_focus board ro =
match board.keyboard_focus, ro with
| Some br, Some r -> if Layout.equal br r || not (Layout.accept_focus br)
then board.keyboard_focus <- None
| Some br, _ -> if not (Layout.accept_focus br)
then board.keyboard_focus <- None
| _ -> ()
let mouse_focus_widget board =
map_option (get_mouse_focus board) Layout.widget
let keyboard_focus_widget board =
map_option board.keyboard_focus Layout.widget
let button_down_widget board =
map_option board.button_down Layout.widget
let layout_focus board =
match Sdl.get_mouse_focus () with
| None -> printd debug_board "No window with SDL mouse_focus"; None
| Some w ->
list_check_ok (fun l -> same_window (Layout.window l) w) (get_layouts board)
let top_house board room =
let top = Layout.top_house room in
list_check_ok (fun l -> Layout.(top == l)) (get_layouts board)
let window_of_event board ev =
try
let ido = match Trigger.event_kind ev with
| `Bogue_redraw ->
let wid = E.(get ev user_code) in
map_option (Layout.of_wid wid) (fun r ->
let id = Sdl.get_window_id (Layout.window r) in
printd debug_event "Redraw event window_id=%d" id;
id)
| _ -> Some (Trigger.window_id ev) in
check_option ido (fun id ->
list_check_ok (fun w -> id = Window.id w) board.windows)
with Not_found ->
printd debug_error "Search window for event %s caused an error"
(Trigger.sprint_ev ev);
None
let check_mouse_focus board =
if board.mouse_alive
then let (x,y) = Mouse.pos () in
printd debug_board "[check_mouse_focus] Mouse pos:(%u,%u)" x y;
check_option (layout_focus board) (Layout.top_focus x y)
else None
let check_mouse_hover board =
let (x,y) = Mouse.pos () in
check_option (layout_focus board) (Layout.hover x y)
let check_mouse_motion ?target board =
let open Layout in
let open Trigger in
let mf = match target with
| Some _ -> target
| None -> check_mouse_focus board in
let () = match (get_mouse_focus board), mf with
| None, None -> ()
| Some r, None ->
unset_focus r;
push_mouse_leave (r.Layout.id);
set_cursor None;
| None, Some r ->
set_focus r;
push_mouse_enter (r.Layout.id);
set_cursor (Some r);
| Some w1, Some w2 ->
if not (Widget.equal (widget w1) (widget w2))
then (set_focus w2;
unset_focus w1;
push_mouse_leave (w1.Layout.id);
push_mouse_enter (w2.Layout.id);
set_cursor (Some w2)
) in
board.mouse_focus <- mf
let dragging = ref None
let target_widget board ev =
let roomo =
if E.(get ev typ) = Trigger.mouse_enter ||
E.(get ev typ) = Trigger.mouse_leave
then let id = E.get ev Trigger.room_id in
Layout.of_id_opt ~not_found:(fun () ->
printd debug_error "The room #%u has disappeared but was pointed by \
the mouse enter/leave event" id) id
else match board.button_down with
| Some r ->
printd debug_board "Target: select button_down (%s)"
(Layout.sprint_id r);
Some r
| None ->
if Trigger.text_event ev
|| map_option board.button_down Layout.has_keyboard_focus = Some true
then (printd debug_board "Target: select keyboard widget";
board.keyboard_focus)
else (printd debug_board "Target: select mouse widget";
(get_mouse_focus board)) in
map_option roomo Layout.widget
let is_fresh board =
Layout.is_fresh board.windows_house
(** display only widgets that need to be updated *)
let update_old board =
List.iter (fun w ->
if not (Window.is_fresh w) && Draw.window_is_shown (Window.sdl_window w)
then (Window.to_refresh w;
Layout.update_old (Window.get_layout w))
else printd debug_board "Window is hidden")
board.windows
let has_anim board =
(List.fold_left (fun b w ->
let h = Layout.has_anim (Window.get_layout w) in
if h then Window.to_refresh w;
h || b ) false board.windows)
let drop board =
match board.button_down with
| None -> ()
| Some room -> begin
printd debug_board " ----> Drop";
let open Layout in
do_option !dragging (slide_to room);
dragging := None;
end
let drag board ev room =
let open Layout in
match Trigger.event_kind ev with
| `Mouse_motion when not (!dragging <> None) && Trigger.mm_pressed ev ->
dragging := Some (getx room, gety room);
follow_mouse room;
board.button_down <- Some room;
printd debug_board " ----> Drag";
None
| `Mouse_button_up when !dragging <> None -> drop board; Some ev
| `Mouse_motion when (!dragging <> None && E.(get ev mouse_motion_state) = 0l)
-> drop board; None
| _ -> Some ev
let activate board roomo =
board.button_down <- roomo;
(match board.keyboard_focus, roomo with
| Some kr, Some mr when not Layout.(kr == mr) ->
Layout.remove_keyboard_focus kr;
Layout.ask_update kr
| Some kr, None -> Layout.remove_keyboard_focus kr;
Layout.ask_update kr
| _ -> ())
let set_mouse_focus board target =
check_mouse_motion ?target board
let set_keyboard_focus board ro =
activate board ro;
board.keyboard_focus <- ro;
do_option ro (fun r ->
Layout.set_keyboard_focus r;
check_mouse_motion ~target:r board)
let tab board =
let current_room = match board.keyboard_focus with
| Some r -> r
| None -> match (get_mouse_focus board) with
| Some r -> r
| None -> match layout_focus board with
| Some l -> l
| None -> Window.get_layout (List.hd board.windows) in
let top = match top_house board current_room with
| None ->
printd (debug_board + debug_custom)
"Current keyboard focus %s has no Window..."
(Layout.sprint_id current_room);
Window.get_layout (List.hd board.windows)
| Some top ->
printd debug_custom "Current window is %s" (Layout.sprint_id top);
top in
printd debug_board "Current room #%u" current_room.Layout.id;
Layout.keyboard_focus_before_tab := Some current_room;
match Layout.next_keyboard ~top current_room with
| None -> printd debug_board " ==> No keyboard focus found !"
| Some r as ro ->
printd debug_board "Activating next keyboard focus (room #%u)" r.Layout.id;
set_keyboard_focus board ro
(** open/close the debugging window *)
let toggle_debug_window =
let window = ref None in fun board ->
match !window with
| None ->
print_endline "OPENING DEBUG WINDOW";
let debug_window = B_debug_window.create () in
let w = add_window board debug_window in
window := Some w
| Some w ->
remove_window board w;
window := None
let add_debug_shortcuts shortcuts =
shortcuts
|> Shortcut.add_ctrl (Sdl.K.d, fun _ ->
debug := not !debug;
print "Debug set to %b" !debug)
|> Shortcut.add_ctrl_shift (Sdl.K.d, toggle_debug_window)
|> Shortcut.add_ctrl_shift (Sdl.K.i, fun board ->
print "Mouse Focus Layout parents:";
print_endline Print.(option layout_up board.mouse_focus))
|> Shortcut.add_ctrl (Sdl.K.i, fun board ->
print "Hover Layout children (don't trust this):";
print_endline Print.(option layout_down (check_mouse_hover board)))
|> Shortcut.add_ctrl_shift (Sdl.K.s, fun board ->
Print.dump board.windows_house)
|> Shortcut.add_ctrl (Sdl.K.m, fun _ ->
print_endline "Garbage collecting...";
Gc.compact ();
Draw.memory_info ())
let refresh_custom_windows board =
List.iter (fun w ->
printd debug_board "BOGUE WINDOW=%b" w.Window.bogue;
if not w.Window.bogue then w.Window.is_fresh <- false)
board.windows
let check_removed board ro =
let equal br = match ro with Some r -> Layout.equal r br | None -> false in
do_option board.mouse_focus (fun r ->
if equal r || not (Layout.accept_focus r) then begin
printd debug_board "Removing mouse_focus %s."
(Layout.sprint_id r);
check_mouse_motion board
end);
do_option board.keyboard_focus (fun r ->
if equal r || not (Layout.accept_focus r) then begin
printd debug_board "Removing keyboard_focus %s."
(Layout.sprint_id r);
board.keyboard_focus <- None
end);
do_option board.button_down (fun r ->
if equal r || not (Layout.accept_focus r) then begin
printd debug_board "Removing button_down %s."
(Layout.sprint_id r);
board.button_down <- None
end)
let filter_board_events board e =
let open E in
printd debug_event "1==> Filtering event type: %s" (Trigger.sprint_ev e);
match Trigger.event_kind e with
| `Finger_motion ->
if Sdl.has_event E.finger_motion then None else Some e
| `Bogue_keyboard_focus ->
set_keyboard_focus board
(Layout.of_id_opt (get e user_code)
~not_found:(fun () ->
printd debug_error "Room #%u pointed by event %s has disappeared"
(get e user_code) (Trigger.sprint_ev e)));
Some e
| `Bogue_mouse_focus ->
printd debug_event "Require Mouse FOCUS";
set_mouse_focus board (Layout.of_id_opt (get e user_code));
Some e
| `Bogue_mouse_enter ->
printd debug_event "Mouse ENTER";
Some e
| `Bogue_mouse_leave ->
printd debug_event "Mouse LEAVE";
Some e
| `Bogue_update ->
printd debug_event "Update";
Update.execute e;
None
| `Bogue_remove_focus ->
let id = get e user_code in
printd debug_event "Remove focus from room #%i." id;
Trigger.(flush remove_focus);
let ro = Layout.of_id_unsafe id in
check_removed board ro;
None
| `Bogue_add_window ->
begin let id = get e user_code in
printd debug_event "Add window with room %i." id;
match Layout.of_id_unsafe id with
| Some r -> let w = add_window board r in
printd debug_board "New window created from room %s." (Layout.sprint_id w.layout);
None
| None -> printd debug_error "Cannot find room %i (for creating a new window)." id;
None
end
| `Render_targets_reset
| `Render_device_reset ->
printd (debug_graphics + debug_error) "TODO! Reset all textures";
Sdl.log "reset event";
None
| `SDL_POLLSENTINEL -> printd (debug_error + debug_event) "Ignoring SDL_POLLSENTINEL";
None
| _ -> Some e
let treat_layout_events board e =
let open E in
printd debug_event "2==> Treating event type: %s" (Trigger.sprint_ev e);
begin match Trigger.event_kind e with
| `Bogue_sync_action ->
printd debug_event "Sync";
if not (Sync.execute 10)
then Trigger.flush (Trigger.sync_action)
| `Key_up -> board.shortcut_pressed <- false;
| `Key_down ->
let pair = get e keyboard_keycode, get e keyboard_keymod in
if not board.shortcut_pressed
then do_option (Shortcut.find (Var.get board.shortcuts) pair)
(fun a -> board.shortcut_pressed <- true; a board)
| `Mouse_button_down
| `Finger_down ->
Trigger.button_down e;
activate board (get_mouse_focus board)
| `Mouse_button_up
| `Finger_up ->
printd debug_event "Mouse button up !";
Trigger.button_up e;
if not !Trigger.too_fast
&& (map2_option board.button_down (check_mouse_focus board) Layout.equal
= Some true
|| map_option board.button_down Layout.has_keyboard_focus = Some true)
then begin
printd debug_event "full click";
Trigger.set_full_click e;
do_option (get_mouse_focus board) (fun x ->
printd debug_board "Mouse focus: %d" x.Layout.id);
do_option board.keyboard_focus (fun x ->
printd debug_board "Keyboard focus: %d" x.Layout.id);
do_option board.button_down (fun x ->
printd debug_board "Button down on #%d" x.Layout.id;
Layout.set_keyboard_focus x);
board.keyboard_focus <- board.button_down
end
| `Mouse_wheel ->
do_option (get_mouse_focus board) (fun room ->
do_option (Layout.find_clip_house room)
(fun room ->
let list = Trigger.filter_events (fun e ->
Trigger.event_kind e <> `Mouse_wheel) in
let total = List.fold_left
(fun s ev -> s + Mouse.wheel_y ev)
(Mouse.wheel_y e) list in
printd debug_event "Total mouse wheels=%d" total;
let dy = - total in
Layout.scroll ~duration:500 dy room;
check_mouse_motion board;
Trigger.push_var_changed room.Layout.id))
| `Bogue_destroy_window ->
printd (debug_board+debug_event) "Destroy window request";
do_option (window_of_event board e) (remove_window board);
| `Window_event ->
let wid = get e window_event_id in
printd debug_event "Window event [%d]" wid;
begin
match window_event_enum wid with
| `Resized ->
printd debug_event "(ignored) Resized => to (%lu,%lu)"
(get e window_data1) (get e window_data2)
| `Size_changed ->
printd debug_event "Size_changed => Resize to (%lu,%lu)"
(get e window_data1) (get e window_data2);
do_option (window_of_event board e) resize
| `Exposed ->
printd debug_event "Exposed";
do_option (window_of_event board e) (fun w ->
let l = Window.get_layout w in
if Window.size w <> Layout.get_physical_size l
then Layout.resize_from_window ~flip:false l;
Window.to_refresh w)
| `Close ->
printd (debug_board+debug_event) "Asking window to close";
do_option (window_of_event board e) (fun w ->
if not w.Window.bogue then (Trigger.push_event e);
let action = default w.on_close (remove_window board) in
action w)
| _ as enum ->
printd debug_event "%s" (Trigger.window_event_name enum);
do_option (window_of_event board e) (fun w ->
Window.to_refresh w;
check_mouse_motion board;
)
end;
| `User_event ->
printd (debug_event + debug_board + debug_user) "User event";
do_option board.on_user_event (fun f -> f e)
| `Quit -> printd (debug_event + debug_board) "Quit event"; raise Exit
| _ -> ()
end
let filter_drag_and_drop_event board e =
match board.button_down with
| Some room -> if Layout.draggable room
then drag board e room
else Some e
| None -> printd debug_board "No board.button_down"; Some e
let treat_remaining_events _board e =
match Trigger.event_kind e with
| _ -> ()
let final_events board anim e =
match Trigger.event_kind e with
| `Mouse_motion ->
printd debug_disable "MOTION anim=%b"
anim;
if not board.mouse_alive then board.mouse_alive <- true;
if not (Trigger.mm_pressed e)
then check_mouse_motion board
| `Mouse_button_up
| `Finger_up ->
board.button_down <- None;
check_mouse_motion board
| `Window_event -> ()
| `Bogue_redraw ->
if not anim then begin
printd debug_event "Redraw";
do_option (window_of_event board e) Window.to_refresh
end
| `Bogue_mouse_at_rest ->
printd debug_event "Mouse AT REST";
| _ -> ()
let max_events = 128
let event_loop anim new_anim board =
let rec loop e count =
printd debug_event "Event loop %i" count;
let evo_layout = filter_board_events board e in
do_option evo_layout (treat_layout_events board);
let evo_widget = check_option evo_layout (filter_drag_and_drop_event board)
in
do_option evo_widget (fun ev ->
do_option (target_widget board ev) (Widget.wake_up_all ev));
do_option evo_widget (treat_remaining_events board);
final_events board new_anim e;
continue e (count + 1)
and continue e count =
if count > max_events
then begin
printd (debug_event + debug_error + debug_user)
"Too many events accumulate. Maybe your system is too slow.";
Sdl.flush_events E.first_event E.last_event
end
else if Sdl.poll_event (Some e)
then loop e count
else if anim || new_anim
then printd debug_event
"Animation: we end the event loop, %i events processed." count
else if count = 0
then loop (Trigger.wait_event ~action:Timeout.run e) count
else printd debug_event "No more events, %i events processed." count
in
let e = !Trigger.my_event in
continue e 0
let start_nop_event_fps, nop_event_fps = Time.make_fps ()
let one_step ?before_display anim (start_fps, fps) ?clear board =
let (_ : Time.t) = Timeout.run () in
let new_anim = has_anim board in
if new_anim && not anim then start_fps ();
if not new_anim && anim then check_mouse_motion board;
event_loop anim new_anim board;
do_option before_display run;
let anim = new_anim in
if anim then begin
printd debug_graphics " * Animation running...";
List.iter Window.to_refresh board.windows;
end;
if anim then fps ();
let t = Time.now () in
flip ?clear board;
printd debug_graphics "==> Rendering took %u ms" (Time.now () - t);
Avar.new_frame ();
printd debug_graphics "---------- end of loop -----------";
if not anim then
if Layout.is_fresh board.windows_house then
nop_event_fps 60
else
Thread.delay 0.005;
anim
let make_sdl_windows ?windows board =
match windows with
| None -> List.iter Window.make_sdl_window board.windows
| Some list ->
let rec loop sdl ws =
match sdl, ws with
| _, [] -> ()
| [], rest -> List.iter Window.make_sdl_window rest
| s::srest, w::wrest -> begin
Window.use_sdl_window s w;
loop srest wrest
end in
loop list board.windows
let create ?shortcuts ?(connections = []) ?on_user_event windows =
let layouts = List.map Window.get_layout windows in
let windows_house = Layout.create_win_house layouts in
let widgets =
Layout.get_widgets windows_house in
do_option (repeated Widget.equal widgets) (fun w ->
print_endline (Print.widget w);
failwith (Printf.sprintf "Widget is repeated: #%u" (Widget.id w)));
List.iter (fun c -> Widget.(add_connection c.source c)) connections;
let shortcuts = default_lazy shortcuts (lazy (Shortcut.create [])) in
let shortcuts =
(if !debug then add_debug_shortcuts shortcuts else shortcuts)
|> Shortcut.add (Sdl.K.tab, tab)
|> Shortcut.add_ctrl
(Sdl.K.l,
fun board ->
print_endline "User Redraw";
display board;
show board) in
{ windows;
windows_house;
mouse_focus = None;
keyboard_focus = None;
button_down = None;
shortcuts = Var.create shortcuts;
shortcut_pressed = false;
mouse_alive = false;
on_user_event }
let get_window_refresh_rate win =
match Sdl.get_window_display_mode (Window.sdl_window win) with
| Ok Sdl.{dm_refresh_rate = Some rate; _ } ->
printd debug_graphics "Detected refresh rate for layout %s : %i"
(Layout.sprint_id (Window.get_layout win)) rate;
Some rate
| Ok Sdl.{dm_refresh_rate = None; _ } ->
printd (debug_graphics + debug_warning)
"No refresh rate information in display mode";
None
| Error (`Msg m) ->
printd (debug_graphics + debug_warning)
"Cannot get display mode from window: %s" m;
None
let get_monitors_refresh_rate board =
List.map get_window_refresh_rate board.windows
|> List.filter (fun x -> x <> None)
|> List.map remove_option
|> List.sort Stdlib.compare
|> ISet.of_list
|> ISet.elements
let get_monitor_refresh_rate board =
match get_monitors_refresh_rate board with
| [] -> printd (debug_graphics + debug_warning)
"Could not get refresh rate information for any monitor";
None
| [r] -> Some r
| r :: rest ->
let g = List.fold_left gcd r rest in
if g >= 30 then Some g else Some (List.fold_left imin r rest)
let of_windows = create
let of_layouts ?shortcuts ?connections ?on_user_event layouts =
create ?shortcuts ?connections ?on_user_event (List.map Window.create layouts)
let of_layout ?shortcuts ?connections ?on_user_event layout =
of_layouts ?shortcuts ?connections ?on_user_event [layout]
let make ?shortcuts connections layouts =
printd (debug_user + debug_warning)
"Bogue.make is deprecated. Use Bogue.create, Bogue.of_layout, \
Bogue.of_layouts, or Bogue.of_windows instead.";
of_layouts ?shortcuts ~connections layouts
(** The main function that loops indefinitely *)
let run ?(vsync=true) ?before_display ?after_display board =
printd debug_board "==> Running board!";
if !running_board <> None
then printd (debug_board + debug_error) "Running a board while the previous one was not cleanly quit. This is a bad idea.";
running_board := Some board;
Trigger.flush_all ();
if not (Sync.is_empty ()) then Trigger.push_action ();
if not (Update.is_empty ()) then Update.push_all ();
Trigger.main_tread_id := Thread.(id (self ()));
make_sdl_windows board;
let desired_fps =
if vsync then default (get_monitor_refresh_rate board) 60
else 60 in
printd debug_graphics "Desired FPS=%u" desired_fps;
let start, fps = Time.adaptive_fps ~vsync desired_fps in
show board;
Thread.delay 0.01;
Sdl.pump_events ();
Sdl.stop_text_input ();
display board;
board.mouse_focus <- check_mouse_focus board;
printd debug_board "Has focus: %s"
(if board.mouse_focus = None then "NO" else "YES");
do_option (get_mouse_focus board) (fun l ->
Layout.set_focus l;
Trigger.push_mouse_enter (l.Layout.id));
if not (Sync.execute 50)
then Trigger.flush (Trigger.sync_action);
flip ~clear:true board;
List.iter (Widget.wake_up (Trigger.startup_event ()))
(List.flatten (List.map Widget.connections (Layout.get_widgets board.windows_house)));
Trigger.renew_my_event ();
start_nop_event_fps ();
Trigger.start_noevent_fps ();
let rec loop anim =
let anim' = one_step ?before_display ~clear:true anim (start,fps) board in
do_option after_display (fun f -> f ());
loop anim' in
let () = try
loop false
with
| Exit -> exit_board board
| e ->
let sdl_error = Sdl.get_error () in
if sdl_error <> "" then print "SDL ERROR: %s" sdl_error;
Print.dump board.windows_house;
raise e in
printd debug_board "Board exiting";
running_board := None
let get_running_board () =
!running_board
type shortcut_action = board Shortcut.action
let exit_on_escape = (Sdl.K.escape, Sdl.Kmod.none, fun (_ : board) -> raise Exit)
let shortcuts_empty () : shortcuts =
Shortcut.empty ()
let shortcuts_add map ?(keymod = Sdl.Kmod.none) keycode action : shortcuts =
Shortcut.add_map map (keycode, keymod, action)
let shortcuts_add_ctrl map keycode action : shortcuts =
Shortcut.add_ctrl (keycode, action) map
let shortcuts_add_ctrl_shift map keycode action : shortcuts =
Shortcut.add_ctrl_shift (keycode, action) map
let shortcuts_of_list list : shortcuts =
Shortcut.create list
let get_shortcut_map () =
match get_running_board () with
| None -> None
| Some b -> Some (Var.get b.shortcuts)
let get_shortcut ?map ?(keymod = Sdl.Kmod.none) keycode : shortcut_action option =
let map = default_option map (get_shortcut_map ()) in
check_option map (fun map -> Shortcut.find map (keycode, keymod))
let remove_shortcut ?board ?(keymod = Sdl.Kmod.none) keycode =
Sync.push (fun () ->
match default_option_fn board get_running_board with
| None -> printd (debug_board + debug_error)
"Cannot remove shortcut because no board was provided and no \
board is running."
| Some board ->
Var.update board.shortcuts (fun map ->
printd debug_board "Removing shortcut (code=%i, mod=%i)" keycode keymod;
Shortcut.remove (keycode, keymod) map))
let add_shortcut ?board ?(keymod = Sdl.Kmod.none) keycode action =
Sync.push (fun () ->
match default_option_fn board get_running_board with
| None -> printd (debug_board + debug_error)
"Cannot add shortcut because no board was provided and no \
board is running."
| Some board ->
Var.update board.shortcuts (fun map ->
printd debug_board "Adding shortcut (code=%i, mod=%i)" keycode keymod;
Shortcut.add_map map (keycode, keymod, action)))
type shortcut_restore = (unit -> unit) option ref
let add_one_shot_shortcut ?board ?(keymod = Sdl.Kmod.none) keycode action
(restore : shortcut_restore) =
Sync.push (fun () ->
match default_option_fn board get_running_board with
| None -> printd (debug_board + debug_error)
"Cannot add shortcut because no board was provided and no \
board is running."
| Some board ->
Var.update board.shortcuts (fun map ->
printd debug_board "Adding shortcut (code=%i, mod=%i)" keycode keymod;
restore := Some (fun () -> Var.set board.shortcuts map);
let action b =
action b;
restore:= None;
Var.set b.shortcuts map in
let map = Shortcut.remove (keycode, keymod) map in
Shortcut.add_map map (keycode, keymod, action)))
let shortcut_restore_init () : shortcut_restore = ref None
let shortcut_restore (f : shortcut_restore) = apply_option !f ()