-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathplx.c
More file actions
1537 lines (1305 loc) · 49.5 KB
/
plx.c
File metadata and controls
1537 lines (1305 loc) · 49.5 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
/* MIT License, Copyright (c) 2023-2026 Barnabás Zahorán, see LICENSE
*
* plxlib - X11 library bindings to SWI-Prolog for plwm
*
*/
#include <stdio.h>
#include <X11/X.h>
#include <X11/Xutil.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xproto.h>
#include <X11/Xft/Xft.h>
#include <X11/extensions/Xrandr.h>
#include <SWI-Prolog.h>
#define Q(x) #x
#define QUOTE(x) Q(x)
#define GET_MACRO(_1,_2,NAME,...) NAME
#define PL_TRY1(plcall) PL_TRY2(plcall, {})
#define PL_TRY2(plcall, cleanup) \
if (!plcall) { \
cleanup; \
return (foreign_t)PL_warning("%s: " QUOTE((plcall)) " failed!", __func__); \
}
#define PL_TRY(...) GET_MACRO(__VA_ARGS__, PL_TRY2, PL_TRY1,)(__VA_ARGS__)
/* Bindings to swipl predicates */
static foreign_t x_open_display(term_t display_name, term_t display);
static foreign_t x_close_display(term_t display);
static foreign_t x_set_error_handler(term_t to_dummy);
static foreign_t x_set_close_down_mode(term_t display, term_t close_mode);
static foreign_t x_create_font_cursor(term_t display, term_t shape, term_t cursor);
static foreign_t x_define_cursor(term_t display, term_t w, term_t cursor);
static foreign_t x_free_cursor(term_t display, term_t cursor);
static foreign_t x_grab_key(term_t display, term_t keycode, term_t modifiers, term_t grab_window,
term_t owner_events, term_t pointer_mode, term_t keyboard_mode);
static foreign_t x_grab_button(term_t display, term_t button, term_t modifiers, term_t grab_window,
term_t owner_events, term_t event_mask, term_t pointer_mode,
term_t keyboard_mode, term_t confine_to, term_t cursor);
static foreign_t x_grab_pointer(term_t display, term_t grab_window, term_t owner_events, term_t event_mask,
term_t pointer_mode, term_t keyboard_mode, term_t confine_to, term_t cursor, term_t time);
static foreign_t x_grab_server(term_t display);
static foreign_t x_ungrab_key(term_t display, term_t keycode, term_t modifiers, term_t grab_window);
static foreign_t x_ungrab_button(term_t display, term_t button, term_t modifiers, term_t grab_window);
static foreign_t x_ungrab_pointer(term_t display, term_t time);
static foreign_t x_ungrab_server(term_t display);
static foreign_t x_keysym_to_keycode(term_t display, term_t keysym, term_t keycode);
static foreign_t x_string_to_keysym(term_t string, term_t keysym);
static foreign_t x_next_event(term_t display, term_t event_return);
static foreign_t x_send_event(term_t display, term_t w, term_t propagate, term_t event_mask, term_t event_send);
static foreign_t x_raise_window(term_t display, term_t w);
static foreign_t x_get_window_attributes(term_t display, term_t w, term_t window_attributes_return, term_t status);
static foreign_t x_move_resize_window(term_t display, term_t w, term_t x, term_t y, term_t width, term_t height);
static foreign_t x_change_window_attributes(term_t display, term_t w, term_t valuemask, term_t event_mask);
static foreign_t x_select_input(term_t display, term_t w, term_t event_mask);
static foreign_t x_map_window(term_t display, term_t w);
static foreign_t x_configure_window(term_t display, term_t w, term_t value_mask, term_t x, term_t y, term_t width,
term_t height, term_t border_width, term_t sibling, term_t stack_mode);
static foreign_t x_set_window_border(term_t display, term_t w, term_t border_pixel);
static foreign_t x_set_input_focus(term_t display, term_t focus, term_t revert_to, term_t time);
static foreign_t x_kill_client(term_t display, term_t resource);
static foreign_t x_sync(term_t display, term_t discard);
static foreign_t x_intern_atom(term_t display, term_t atom_name, term_t only_if_exists, term_t atom);
static foreign_t x_get_class_hint(term_t display, term_t w, term_t res_name, term_t res_class);
static foreign_t x_change_property(term_t display, term_t w, term_t property, term_t atom, term_t format,
term_t mode, term_t data, term_t nelements);
static foreign_t x_delete_property(term_t display, term_t w, term_t property);
static foreign_t x_utf8_text_list_to_text_property(term_t display, term_t list, term_t count, term_t style,
term_t text_prop_return);
static foreign_t x_get_text_property(term_t display, term_t w, term_t text, term_t property, term_t status);
static foreign_t x_set_text_property(term_t display, term_t w, term_t text_prop, term_t property);
static foreign_t x_create_simple_window(term_t display, term_t parent, term_t x, term_t y, term_t width,
term_t height, term_t border_width, term_t border, term_t background, term_t w);
static foreign_t x_get_transient_for_hint(term_t display, term_t w, term_t prop_window_return, term_t status);
static foreign_t x_get_window_property(term_t display, term_t w, term_t property, term_t delete, term_t req_type,
term_t prop_return);
static foreign_t x_get_wm_protocols(term_t display, term_t w, term_t protocols_return, term_t count_return);
static foreign_t x_get_wm_normal_hints(term_t display, term_t w, term_t hints_return, term_t status);
static foreign_t x_warp_pointer(term_t display, term_t src_w, term_t dest_w, term_t src_x, term_t src_y,
term_t src_width, term_t src_height, term_t dest_x, term_t dest_y);
static foreign_t default_root_window(term_t display, term_t w);
static foreign_t default_screen(term_t display, term_t screen);
static foreign_t default_visual(term_t display, term_t screen_number, term_t visual);
static foreign_t default_colormap(term_t display, term_t screen_number, term_t colormap);
/* Xft */
static foreign_t xft_color_alloc_name(term_t display, term_t visual, term_t cmap, term_t name, term_t result);
/* XRandR */
static foreign_t xrr_query_extension(term_t dpy, term_t event_base_return, term_t error_base_return);
static foreign_t xrr_select_input(term_t dpy, term_t window, term_t mask);
static foreign_t xrr_get_screen_resources(term_t dpy, term_t window, term_t screen_resources, term_t outputs);
static foreign_t xrr_free_screen_resources(term_t resources);
static foreign_t xrr_get_output_info(term_t dpy, term_t resources, term_t output_index, term_t output_info);
static foreign_t xrr_get_crtc_info(term_t dpy, term_t resources, term_t crtc, term_t crtc_info);
static foreign_t create_configure_event(term_t display, term_t w, term_t configure_event);
static foreign_t create_clientmessage_event(term_t w, term_t message_type, term_t format, term_t datal0, term_t datal1,
term_t clientmessage);
static foreign_t c_free(term_t ptr);
/* Helpers */
static int build_list(term_t dst, term_t *src, size_t size);
static int xerror(Display __attribute__((unused)) *dpy, const XErrorEvent *ee); /* copied from dwm */
static int xerrordummy(const Display *dpy, const XErrorEvent *ee); /* copied from dwm */
static int rr_event_base = -1;
#pragma GCC diagnostic ignored "-Wpedantic" /* ISO C forbids initialization between function pointer and 'void *' */
#pragma GCC diagnostic push /* but pl_function_t is void* which we must use to pass our callbacks */
static PL_extension predicates[] = {
/* functor name arity C-callback flags remarks */
{ "x_open_display" , 2, x_open_display , 0 }, /* pass "" for XOpenDisplay(NULL) */
{ "x_close_display" , 1, x_close_display , 0 },
{ "x_set_error_handler" , 1, x_set_error_handler , 0 }, /* it sets xerror() or xerrordummy() */
{ "x_set_close_down_mode" , 2, x_set_close_down_mode , 0 },
{ "x_create_font_cursor" , 3, x_create_font_cursor , 0 },
{ "x_define_cursor" , 3, x_define_cursor , 0 },
{ "x_free_cursor" , 2, x_free_cursor , 0 },
{ "x_grab_key" , 7, x_grab_key , 0 },
{ "x_grab_button" , 10, x_grab_button , 0 },
{ "x_grab_pointer" , 9, x_grab_pointer , 0 }, /* Unused */
{ "x_grab_server" , 1, x_grab_server , 0 },
{ "x_ungrab_key" , 4, x_ungrab_key , 0 },
{ "x_ungrab_button" , 4, x_ungrab_button , 0 },
{ "x_ungrab_pointer" , 2, x_ungrab_pointer , 0 }, /* Unused */
{ "x_ungrab_server" , 1, x_ungrab_server , 0 },
{ "x_keysym_to_keycode" , 3, x_keysym_to_keycode , 0 },
{ "x_string_to_keysym" , 2, x_string_to_keysym , 0 },
{ "x_next_event" , 2, x_next_event , 0 },
{ "x_send_event" , 5, x_send_event , 0 },
{ "x_raise_window" , 2, x_raise_window , 0 },
{ "x_get_window_attributes" , 4, x_get_window_attributes , 0 },
{ "x_move_resize_window" , 6, x_move_resize_window , 0 },
{ "x_change_window_attributes", 4, x_change_window_attributes , 0 }, /* Only sets event_mask for now! */
{ "x_select_input" , 3, x_select_input , 0 },
{ "x_map_window" , 2, x_map_window , 0 },
{ "x_configure_window" , 10, x_configure_window , 0 },
{ "x_set_window_border" , 3, x_set_window_border , 0 },
{ "x_set_input_focus" , 4, x_set_input_focus , 0 },
{ "x_kill_client" , 2, x_kill_client , 0 },
{ "x_sync" , 2, x_sync , 0 },
{ "x_intern_atom" , 4, x_intern_atom , 0 },
{ "x_get_class_hint" , 4, x_get_class_hint , 0 },
{ "x_change_property" , 8, x_change_property , 0 },
{ "x_delete_property" , 3, x_delete_property , 0 },
{ "x_utf8_text_list_to_text_property", 5, x_utf8_text_list_to_text_property, 0 },
{ "x_get_text_property" , 5, x_get_text_property , 0 },
{ "x_set_text_property" , 4, x_set_text_property , 0 },
{ "x_create_simple_window" , 10, x_create_simple_window , 0 },
{ "x_get_transient_for_hint" , 4, x_get_transient_for_hint , 0 },
{ "x_get_window_property" , 6, x_get_window_property , 0 }, /* 4th, 5th, 8th-11th args are omitted */
{ "x_get_wm_protocols" , 4, x_get_wm_protocols , 0 },
{ "x_get_wm_normal_hints" , 4, x_get_wm_normal_hints , 0 }, /* supplied_return arg is ignored */
{ "x_warp_pointer" , 9, x_warp_pointer , 0 }, /* Unused */
{ "default_root_window" , 2, default_root_window , 0 },
{ "default_screen" , 2, default_screen , 0 },
{ "default_visual" , 3, default_visual , 0 },
{ "default_colormap" , 3, default_colormap , 0 },
/* Xft */
{ "xft_color_alloc_name" , 5, xft_color_alloc_name , 0 },
/* XRandR */
{ "xrr_query_extension" , 3, xrr_query_extension , 0 },
{ "xrr_select_input" , 3, xrr_select_input , 0 },
{ "xrr_get_screen_resources" , 4, xrr_get_screen_resources , 0 }, /* also returns outputs directly */
{ "xrr_free_screen_resources" , 1, xrr_free_screen_resources , 0 },
{ "xrr_get_output_info" , 4, xrr_get_output_info , 0 }, /* 3rd arg is output idx, last is list attrs */
{ "xrr_get_crtc_info" , 4, xrr_get_crtc_info , 0 }, /* returns list of useful fields only */
{ "create_configure_event" , 3, create_configure_event , 0 }, /* must be de-allocated with c_free! */
{ "create_clientmessage_event", 6, create_clientmessage_event , 0 }, /* must be de-allocated with c_free! */
{ "c_free" , 1, c_free , 0 },
{ NULL , 0, NULL , 0 }
};
#pragma GCC diagnostic pop
install_t
// cppcheck-suppress unusedFunction
install(void)
{
PL_register_extensions(predicates);
}
static foreign_t
x_open_display(term_t display_name, term_t display)
{
char *dname;
size_t len;
Display *dp;
PL_TRY(PL_get_string(display_name, &dname, &len));
if (!(dp = XOpenDisplay(len == 1 ? NULL : dname))) {
return (foreign_t)PL_warning("x_open_display: XOpenDisplay() failed!");
}
PL_TRY(PL_unify_pointer(display, dp));
return TRUE;
}
static foreign_t
x_close_display(term_t display)
{
Display *dp;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
XCloseDisplay(dp);
return TRUE;
}
static foreign_t
x_set_error_handler(term_t to_dummy)
{
Bool dummy;
PL_TRY(PL_get_bool_ex(to_dummy, &dummy));
if (dummy) {
XSetErrorHandler((XErrorHandler)xerrordummy);
}
else {
XSetErrorHandler((XErrorHandler)xerror);
}
return TRUE;
}
static foreign_t
x_set_close_down_mode(term_t display, term_t close_mode)
{
Display *dp;
int closemode;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_integer_ex(close_mode, &closemode));
XSetCloseDownMode(dp, closemode);
return TRUE;
}
static foreign_t
x_create_font_cursor(term_t display, term_t shape, term_t cursor)
{
Display *dp;
int shp;
Cursor curs;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_integer_ex(shape, &shp));
curs = XCreateFontCursor(dp, (unsigned)shp);
PL_TRY(PL_unify_uint64(cursor, curs));
return TRUE;
}
static foreign_t
x_define_cursor(term_t display, term_t w, term_t cursor)
{
Display *dp;
Window win;
Cursor curs;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(w, &win));
PL_TRY(PL_get_uint64_ex(cursor, &curs));
XDefineCursor(dp, win, curs);
return TRUE;
}
static foreign_t
x_free_cursor(term_t display, term_t cursor)
{
Display *dp;
Cursor curs;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(cursor, &curs));
XFreeCursor(dp, curs);
return TRUE;
}
static foreign_t
x_grab_key(term_t display, term_t keycode, term_t modifiers, term_t grab_window,
term_t owner_events, term_t pointer_mode, term_t keyboard_mode)
{
Display *dp;
int kcode, mods, pmode, kmode;
Window win;
Bool oevents;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_integer_ex(keycode, &kcode));
PL_TRY(PL_get_integer_ex(modifiers, &mods));
PL_TRY(PL_get_uint64_ex(grab_window, &win));
PL_TRY(PL_get_bool_ex(owner_events, &oevents));
PL_TRY(PL_get_integer_ex(pointer_mode, &pmode));
PL_TRY(PL_get_integer_ex(keyboard_mode, &kmode));
XGrabKey(dp, kcode, (unsigned)mods, win, oevents, pmode, kmode);
return TRUE;
}
static foreign_t
x_grab_button(term_t display, term_t button, term_t modifiers, term_t grab_window,
term_t owner_events, term_t event_mask, term_t pointer_mode,
term_t keyboard_mode, term_t confine_to, term_t cursor)
{
Display *dp;
int btn, mods, emask, pmode, kmode;
Window win, confto, crsr;
Bool oevents;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_integer_ex(button, &btn));
PL_TRY(PL_get_integer_ex(modifiers, &mods));
PL_TRY(PL_get_uint64_ex(grab_window, &win));
PL_TRY(PL_get_bool_ex(owner_events, &oevents));
PL_TRY(PL_get_integer_ex(event_mask, &emask));
PL_TRY(PL_get_integer_ex(pointer_mode, &pmode));
PL_TRY(PL_get_integer_ex(keyboard_mode, &kmode));
PL_TRY(PL_get_uint64_ex(confine_to, &confto));
PL_TRY(PL_get_uint64_ex(cursor, &crsr));
XGrabButton(dp, (unsigned)btn, (unsigned)mods, win, oevents, (unsigned)emask, pmode, kmode, confto, crsr);
return TRUE;
}
static foreign_t
x_grab_pointer(term_t display, term_t grab_window, term_t owner_events, term_t event_mask,
term_t pointer_mode, term_t keyboard_mode, term_t confine_to, term_t cursor, term_t time)
{
Display *dp;
Window gwin, confto;
Bool oevents;
int emask, ptrmode, kbmode;
Cursor csr;
Time t;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(grab_window, &gwin));
PL_TRY(PL_get_bool_ex(owner_events, &oevents));
PL_TRY(PL_get_integer_ex(event_mask, &emask));
PL_TRY(PL_get_integer_ex(pointer_mode, &ptrmode));
PL_TRY(PL_get_integer_ex(keyboard_mode, &kbmode));
PL_TRY(PL_get_uint64_ex(confine_to, &confto));
PL_TRY(PL_get_uint64_ex(cursor, &csr));
PL_TRY(PL_get_uint64_ex(time, &t));
XGrabPointer(dp, gwin, oevents, (unsigned)emask, ptrmode, kbmode, confto, csr, t);
return TRUE;
}
static foreign_t
x_grab_server(term_t display)
{
Display *dp;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
XGrabServer(dp);
return TRUE;
}
static foreign_t
x_ungrab_key(term_t display, term_t keycode, term_t modifiers, term_t grab_window)
{
Display *dp;
int kcode, mods;
Window win;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_integer_ex(keycode, &kcode));
PL_TRY(PL_get_integer_ex(modifiers, &mods));
PL_TRY(PL_get_uint64_ex(grab_window, &win));
XUngrabKey(dp, kcode, (unsigned)mods, win);
return TRUE;
}
static foreign_t
x_ungrab_button(term_t display, term_t button, term_t modifiers, term_t grab_window)
{
Display *dp;
int btn, mods;
Window win;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_integer_ex(button, &btn));
PL_TRY(PL_get_integer_ex(modifiers, &mods));
PL_TRY(PL_get_uint64_ex(grab_window, &win));
XUngrabButton(dp, (unsigned)btn, (unsigned)mods, win);
return TRUE;
}
static foreign_t
x_ungrab_pointer(term_t display, term_t time)
{
Display *dp;
Time t;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(time, &t));
XUngrabPointer(dp, t);
return TRUE;
}
static foreign_t
x_ungrab_server(term_t display)
{
Display *dp;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
XUngrabServer(dp);
return TRUE;
}
static foreign_t
x_keysym_to_keycode(term_t display, term_t keysym, term_t keycode)
{
Display *dp;
uint64_t ksym;
KeyCode kcode;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(keysym, &ksym));
kcode = XKeysymToKeycode(dp, ksym);
PL_TRY(PL_unify_integer(keycode, kcode));
return TRUE;
}
static foreign_t
x_string_to_keysym(term_t string, term_t keysym)
{
char *str;
size_t len;
uint64_t ksym;
PL_TRY(PL_get_string_chars(string, &str, &len));
ksym = XStringToKeysym(str);
PL_TRY(PL_unify_uint64(keysym, ksym));
return TRUE;
}
static foreign_t
x_next_event(term_t display, term_t event_return)
{
Display *dp;
XEvent ev;
term_t subts[18];
term_t list;
int stcnt;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
XNextEvent(dp, &ev);
if (ev.type == MapRequest) {
stcnt = 7;
for (int i = 0; i < stcnt; ++i) { subts[i] = PL_new_term_ref(); }
PL_TRY(PL_put_atom_chars(subts[0], "maprequest"));
PL_TRY(PL_put_integer(subts[1], ev.xmaprequest.type ));
PL_TRY(PL_put_uint64 (subts[2], ev.xmaprequest.serial ));
PL_TRY(PL_put_bool (subts[3], ev.xmaprequest.send_event));
PL_TRY(PL_put_pointer(subts[4], ev.xmaprequest.display ));
PL_TRY(PL_put_uint64 (subts[5], ev.xmaprequest.parent ));
PL_TRY(PL_put_uint64 (subts[6], ev.xmaprequest.window ));
}
else if (ev.type == UnmapNotify) {
stcnt = 8;
for (int i = 0; i < stcnt; ++i) { subts[i] = PL_new_term_ref(); }
PL_TRY(PL_put_atom_chars(subts[0], "unmapnotify"));
PL_TRY(PL_put_integer(subts[1], ev.xunmap.type ));
PL_TRY(PL_put_uint64 (subts[2], ev.xunmap.serial ));
PL_TRY(PL_put_bool (subts[3], ev.xunmap.send_event ));
PL_TRY(PL_put_pointer(subts[4], ev.xunmap.display ));
PL_TRY(PL_put_uint64 (subts[5], ev.xunmap.event ));
PL_TRY(PL_put_uint64 (subts[6], ev.xunmap.window ));
PL_TRY(PL_put_bool (subts[7], ev.xunmap.from_configure));
}
else if (ev.type == DestroyNotify) {
stcnt = 7;
for (int i = 0; i < stcnt; ++i) { subts[i] = PL_new_term_ref(); }
PL_TRY(PL_put_atom_chars(subts[0], "destroynotify"));
PL_TRY(PL_put_integer(subts[1], ev.xdestroywindow.type ));
PL_TRY(PL_put_uint64 (subts[2], ev.xdestroywindow.serial ));
PL_TRY(PL_put_bool (subts[3], ev.xdestroywindow.send_event));
PL_TRY(PL_put_pointer(subts[4], ev.xdestroywindow.display ));
PL_TRY(PL_put_uint64 (subts[5], ev.xdestroywindow.event ));
PL_TRY(PL_put_uint64 (subts[6], ev.xdestroywindow.window ));
}
else if (ev.type == EnterNotify) {
stcnt = 18;
for (int i = 0; i < stcnt; ++i) { subts[i] = PL_new_term_ref(); }
PL_TRY(PL_put_atom_chars(subts[0], "enternotify"));
PL_TRY(PL_put_integer(subts[ 1], ev.xcrossing.type ));
PL_TRY(PL_put_uint64 (subts[ 2], ev.xcrossing.serial ));
PL_TRY(PL_put_bool (subts[ 3], ev.xcrossing.send_event ));
PL_TRY(PL_put_pointer(subts[ 4], ev.xcrossing.display ));
PL_TRY(PL_put_uint64 (subts[ 5], ev.xcrossing.window ));
PL_TRY(PL_put_uint64 (subts[ 6], ev.xcrossing.root ));
PL_TRY(PL_put_uint64 (subts[ 7], ev.xcrossing.subwindow ));
PL_TRY(PL_put_uint64 (subts[ 8], ev.xcrossing.time ));
PL_TRY(PL_put_integer(subts[ 9], ev.xcrossing.x ));
PL_TRY(PL_put_integer(subts[10], ev.xcrossing.y ));
PL_TRY(PL_put_integer(subts[11], ev.xcrossing.x_root ));
PL_TRY(PL_put_integer(subts[12], ev.xcrossing.y_root ));
PL_TRY(PL_put_integer(subts[13], ev.xcrossing.mode ));
PL_TRY(PL_put_integer(subts[14], ev.xcrossing.detail ));
PL_TRY(PL_put_bool (subts[15], ev.xcrossing.same_screen));
PL_TRY(PL_put_bool (subts[16], ev.xcrossing.focus ));
PL_TRY(PL_put_integer(subts[17], ev.xcrossing.state ));
}
else if (ev.type == PropertyNotify) {
stcnt = 8;
for (int i = 0; i < stcnt; ++i) { subts[i] = PL_new_term_ref(); }
PL_TRY(PL_put_atom_chars(subts[0], "propertynotify"));
PL_TRY(PL_put_uint64 (subts[1], ev.xproperty.serial ));
PL_TRY(PL_put_bool (subts[2], ev.xproperty.send_event));
PL_TRY(PL_put_pointer(subts[3], ev.xproperty.display ));
PL_TRY(PL_put_uint64 (subts[4], ev.xproperty.window ));
PL_TRY(PL_put_uint64 (subts[5], ev.xproperty.atom ));
PL_TRY(PL_put_uint64 (subts[6], ev.xproperty.time ));
PL_TRY(PL_put_integer(subts[7], ev.xproperty.state ));
}
else if (ev.type == ClientMessage) {
stcnt = 11;
for (int i = 0; i < stcnt; ++i) { subts[i] = PL_new_term_ref(); }
PL_TRY(PL_put_atom_chars(subts[0], "clientmessage"));
PL_TRY(PL_put_integer(subts[ 1], ev.xclient.type ));
PL_TRY(PL_put_uint64 (subts[ 2], ev.xclient.serial ));
PL_TRY(PL_put_bool (subts[ 3], ev.xclient.send_event ));
PL_TRY(PL_put_pointer(subts[ 4], ev.xclient.display ));
PL_TRY(PL_put_uint64 (subts[ 5], ev.xclient.window ));
PL_TRY(PL_put_uint64 (subts[ 6], ev.xclient.message_type ));
PL_TRY(PL_put_integer(subts[ 7], ev.xclient.format ));
PL_TRY(PL_put_uint64 (subts[ 8], (unsigned)ev.xclient.data.l[0])); /* we are lazy and only handle */
PL_TRY(PL_put_uint64 (subts[ 9], (unsigned)ev.xclient.data.l[1])); /* the data were are interested in */
PL_TRY(PL_put_uint64 (subts[10], (unsigned)ev.xclient.data.l[2]));
}
else if (ev.type == ConfigureNotify) {
stcnt = 14;
for (int i = 0; i < stcnt; ++i) { subts[i] = PL_new_term_ref(); }
PL_TRY(PL_put_atom_chars(subts[0], "configurenotify"));
PL_TRY(PL_put_integer(subts[ 1], ev.xconfigure.type ));
PL_TRY(PL_put_uint64 (subts[ 2], ev.xconfigure.serial ));
PL_TRY(PL_put_bool (subts[ 3], ev.xconfigure.send_event ));
PL_TRY(PL_put_pointer(subts[ 4], ev.xconfigure.display ));
PL_TRY(PL_put_uint64 (subts[ 5], ev.xconfigure.event ));
PL_TRY(PL_put_uint64 (subts[ 6], ev.xconfigure.window ));
PL_TRY(PL_put_integer(subts[ 7], ev.xconfigure.x ));
PL_TRY(PL_put_integer(subts[ 8], ev.xconfigure.y ));
PL_TRY(PL_put_integer(subts[ 9], ev.xconfigure.width ));
PL_TRY(PL_put_integer(subts[10], ev.xconfigure.height ));
PL_TRY(PL_put_integer(subts[11], ev.xconfigure.border_width ));
PL_TRY(PL_put_uint64 (subts[12], ev.xconfigure.above ));
PL_TRY(PL_put_bool (subts[13], ev.xconfigure.override_redirect));
}
/* XRandR */
else if (ev.type == rr_event_base + RRScreenChangeNotify) {
stcnt = 1;
subts[0] = PL_new_term_ref();
PL_TRY(PL_put_atom_chars(subts[0], "rrscreenchangenotify"));
}
else {
/* rest of the event types are very similar */
stcnt = 15;
for (int i = 0; i < stcnt; ++i) { subts[i] = PL_new_term_ref(); }
switch (ev.type) {
case KeyPress:
PL_TRY(PL_put_atom_chars(subts[0], "keypress"));
PL_TRY(PL_put_integer (subts[13], ev.xkey.keycode));
break;
case KeyRelease:
PL_TRY(PL_put_atom_chars(subts[0], "keyrelease"));
PL_TRY(PL_put_integer (subts[13], ev.xkey.keycode));
break;
case ButtonPress:
PL_TRY(PL_put_atom_chars(subts[0], "buttonpress"));
PL_TRY(PL_put_integer (subts[13], ev.xbutton.button));
break;
case ButtonRelease:
PL_TRY(PL_put_atom_chars(subts[0], "buttonrelease"));
PL_TRY(PL_put_integer (subts[13], ev.xbutton.button));
break;
case MotionNotify:
PL_TRY(PL_put_atom_chars(subts[0], "motionnotify"));
PL_TRY(PL_put_integer (subts[13], ev.xmotion.is_hint));
break;
default:
PL_TRY(PL_unify_atom_chars(event_return, "unsupported_event"));
return TRUE;
}
/* common fields */
PL_TRY(PL_put_uint64 (subts[ 1], ev.xkey.serial ));
PL_TRY(PL_put_bool (subts[ 2], ev.xkey.send_event ));
PL_TRY(PL_put_pointer(subts[ 3], ev.xkey.display ));
PL_TRY(PL_put_uint64 (subts[ 4], ev.xkey.window ));
PL_TRY(PL_put_uint64 (subts[ 5], ev.xkey.root ));
PL_TRY(PL_put_uint64 (subts[ 6], ev.xkey.subwindow ));
PL_TRY(PL_put_uint64 (subts[ 7], ev.xkey.time ));
PL_TRY(PL_put_integer(subts[ 8], ev.xkey.x ));
PL_TRY(PL_put_integer(subts[ 9], ev.xkey.y ));
PL_TRY(PL_put_integer(subts[10], ev.xkey.x_root ));
PL_TRY(PL_put_integer(subts[11], ev.xkey.y_root ));
PL_TRY(PL_put_integer(subts[12], ev.xkey.state ));
PL_TRY(PL_put_bool (subts[14], ev.xkey.same_screen));
}
list = PL_new_term_ref();
build_list(list, subts, (size_t)stcnt);
PL_TRY(PL_unify(list, event_return));
return TRUE;
}
static foreign_t
x_send_event(term_t display, term_t w, term_t propagate, term_t event_mask, term_t event_send)
{
Display *dp;
Window win;
Bool propag;
long emask;
XEvent *esend;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(w, &win));
PL_TRY(PL_get_bool_ex(propagate, &propag));
PL_TRY(PL_get_long_ex(event_mask, &emask));
PL_TRY(PL_get_pointer_ex(event_send, (void**)&esend));
XSendEvent(dp, win, propag, emask, esend);
return TRUE;
}
static foreign_t
x_raise_window(term_t display, term_t w)
{
Display *dp;
Window win;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(w, &win));
XRaiseWindow(dp, w);
return TRUE;
}
static foreign_t
x_get_window_attributes(term_t display, term_t w, term_t window_attributes_return, term_t status)
{
/*
Note: this doesn't extract all win attributes, only the ones needed!
x, y, width and height for now
*/
Display *dp;
Window win;
XWindowAttributes winattrs;
term_t subts[4];
term_t list = PL_new_term_ref();
Status st;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(w, &win));
st = XGetWindowAttributes(dp, win, &winattrs);
for (int i = 0; i < 4; ++i) {
subts[i] = PL_new_term_ref();
}
PL_TRY(PL_put_integer(subts[0], winattrs.x));
PL_TRY(PL_put_integer(subts[1], winattrs.y));
PL_TRY(PL_put_integer(subts[2], winattrs.width));
PL_TRY(PL_put_integer(subts[3], winattrs.height));
build_list(list, subts, 4);
PL_TRY(PL_unify(list, window_attributes_return));
PL_TRY(PL_unify_integer(status, st));
return TRUE;
}
static foreign_t
x_move_resize_window(term_t display, term_t w, term_t x, term_t y, term_t width, term_t height)
{
Display *dp;
Window win;
int wx, wy, wwidth, wheight;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(w, &win));
PL_TRY(PL_get_integer_ex(x, &wx));
PL_TRY(PL_get_integer_ex(y, &wy));
PL_TRY(PL_get_integer_ex(width, &wwidth));
PL_TRY(PL_get_integer_ex(height, &wheight));
XMoveResizeWindow(dp, win, wx, wy, (unsigned)wwidth, (unsigned)wheight);
return TRUE;
}
static foreign_t
x_change_window_attributes(term_t display, term_t w, term_t valuemask, term_t event_mask)
{
Display *dp;
Window win, vmask;
long emask;
XSetWindowAttributes wa;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(w, &win));
PL_TRY(PL_get_uint64_ex(valuemask, &vmask));
PL_TRY(PL_get_long_ex(event_mask, &emask));
wa.event_mask = emask;
XChangeWindowAttributes(dp, win, vmask, &wa);
return TRUE;
}
static foreign_t
x_select_input(term_t display, term_t w, term_t event_mask)
{
Display *dp;
long emask;
Window win;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_long_ex(event_mask, &emask));
PL_TRY(PL_get_uint64_ex(w, &win));
XSelectInput(dp, win, emask);
return TRUE;
}
static foreign_t
x_map_window(term_t display, term_t w)
{
Display *dp;
Window win;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(w, &win));
XMapWindow(dp, win);
return TRUE;
}
static foreign_t
x_configure_window(term_t display, term_t w, term_t value_mask, term_t x, term_t y, term_t width,
term_t height, term_t border_width, term_t sibling, term_t stack_mode)
{
Display *dp;
Window win;
int vmask, wx, wy, wwidth, wheight, bw, stackm;
Window sib;
XWindowChanges wc;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(w, &win));
PL_TRY(PL_get_integer_ex(value_mask, &vmask));
PL_TRY(PL_get_integer_ex(x, &wx));
PL_TRY(PL_get_integer_ex(y, &wy));
PL_TRY(PL_get_integer_ex(width, &wwidth));
PL_TRY(PL_get_integer_ex(height, &wheight));
PL_TRY(PL_get_integer_ex(border_width, &bw));
PL_TRY(PL_get_uint64_ex(sibling, &sib));
PL_TRY(PL_get_integer_ex(stack_mode, &stackm));
wc.x = wx; wc.y = wy; wc.width = wwidth; wc.height = wheight;
wc.border_width = bw; wc.sibling = sib; wc.stack_mode = stackm;
XConfigureWindow(dp, win, (unsigned)vmask, &wc);
return TRUE;
}
static foreign_t
x_set_window_border(term_t display, term_t w, term_t border_pixel)
{
Display *dp;
Window win;
unsigned long bp;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(w, &win));
PL_TRY(PL_get_uint64_ex(border_pixel, &bp));
XSetWindowBorder(dp, win, bp);
return TRUE;
}
static foreign_t
x_set_input_focus(term_t display, term_t focus, term_t revert_to, term_t time)
{
Display *dp;
Window win, tim;
int revto;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(focus, &win));
PL_TRY(PL_get_integer_ex(revert_to, &revto));
PL_TRY(PL_get_uint64_ex(time, &tim));
XSetInputFocus(dp, win, revto, tim);
return TRUE;
}
static foreign_t
x_kill_client(term_t display, term_t resource)
{
Display *dp;
uint64_t res;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(resource, &res));
XKillClient(dp, res);
return TRUE;
}
static foreign_t
x_sync(term_t display, term_t discard)
{
Display *dp;
Bool discrd;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_bool_ex(discard, &discrd));
XSync(dp, discrd);
return TRUE;
}
static foreign_t
x_intern_atom(term_t display, term_t atom_name, term_t only_if_exists, term_t atom)
{
Display *dp;
char *aname; size_t len;
Bool ifexists;
Atom a;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_string_chars(atom_name, &aname, &len));
PL_TRY(PL_get_bool_ex(only_if_exists, &ifexists));
a = XInternAtom(dp, aname, ifexists);
PL_TRY(PL_unify_uint64(atom, a));
return TRUE;
}
static foreign_t
x_get_class_hint(term_t display, term_t w, term_t res_name, term_t res_class)
{
Display *dp;
Window win;
XClassHint ch = { NULL, NULL };
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(w, &win));
if (XGetClassHint(dp, win, &ch)) {
PL_TRY(PL_unify_string_chars(res_name , ch.res_name ? ch.res_name : ""));
PL_TRY(PL_unify_string_chars(res_class, ch.res_class ? ch.res_class : ""));
} else {
return FALSE;
}
if (ch.res_class) XFree(ch.res_class);
if (ch.res_name) XFree(ch.res_name);
return TRUE;
}
static foreign_t
x_change_property(term_t display, term_t w, term_t property, term_t atom, term_t format,
term_t mode, term_t data, term_t nelements)
{
Display *dp;
Window win;
Atom prop, a;
int fmt, md, nelem;
term_t head = PL_new_term_ref();
term_t list = PL_copy_term_ref(data);
uint64_t *alist;
char *str; size_t len; int i = 0;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(w, &win));
PL_TRY(PL_get_uint64_ex(property, &prop));
PL_TRY(PL_get_uint64_ex(atom, &a));
PL_TRY(PL_get_integer_ex(format, &fmt));
PL_TRY(PL_get_integer_ex(mode, &md));
PL_TRY(PL_get_integer_ex(nelements, &nelem));
if (PL_is_string(data)) { /* handle the UTF8_STRING case separately */
PL_TRY(PL_get_string_chars(data, &str, &len));
XChangeProperty(dp, win, prop, a, fmt, md, (unsigned char *)str, (int)len);
return TRUE;
}
alist = malloc((size_t)nelem * sizeof(*alist));
while (PL_get_list(list, head, list)) {
if (!PL_get_uint64_ex(head, &alist[i++])) {
free(alist);
fprintf(stderr, "x_change_property: PL_get_uint64_ex() on 'data[%d]' failed!", i);
return FALSE;
}
}
XChangeProperty(dp, win, prop, a, fmt, md, (unsigned char *)alist, nelem);
free(alist);
return TRUE;
}
static foreign_t
x_delete_property(term_t display, term_t w, term_t property)
{
Display *dp;
Window win;
Atom prop;
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(w, &win));
PL_TRY(PL_get_uint64_ex(property, &prop));
XDeleteProperty(dp, win, prop);
return TRUE;
}
static foreign_t
x_utf8_text_list_to_text_property(term_t display, term_t list, term_t count, term_t style, term_t text_prop_return)
{
Display *dp;
char **strs;
int cnt, sty, i = 0;
XTextProperty *tprop = NULL;
term_t head = PL_new_term_ref();
term_t tlist = PL_copy_term_ref(list);
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_integer_ex(count, &cnt));
PL_TRY(PL_get_integer_ex(style, &sty));
tprop = malloc(sizeof(*tprop));
strs = malloc((size_t)cnt * sizeof(*strs));
while (PL_get_list(tlist, head, tlist)) {
if (!PL_get_chars(head, &strs[i++], CVT_ALL|REP_UTF8)) {
free(strs); free(tprop);
fprintf(stderr, "x_utf8_text_list_to_text_property: PL_get_chars() on 'list[%d]' failed!", i);
return FALSE;
}
}
Xutf8TextListToTextProperty(dp, strs, cnt, (XICCEncodingStyle)sty, tprop);
free(strs);
/* Note: tprop must be freed with c_free/1 after no longer needed! */
PL_TRY(PL_unify_pointer(text_prop_return, tprop));
return TRUE;
}
static foreign_t
x_get_text_property(term_t display, term_t w, term_t text, term_t property, term_t status)
{
Display *dp;
Window win;
XTextProperty tprop;
Atom prop;
Status st;
static char stext[256];
PL_TRY(PL_get_pointer_ex(display, (void**)&dp));
PL_TRY(PL_get_uint64_ex(w, &win));
PL_TRY(PL_get_uint64_ex(property, &prop));
st = XGetTextProperty(dp, win, &tprop, prop);
if (st != 0) {
strncpy(stext, (char*)tprop.value, sizeof(stext)-1);
}
else {
stext[0] = '\0';
}
PL_TRY(PL_unify_integer(status, st));
PL_TRY(PL_unify_string_chars(text, stext));
return TRUE;
}
static foreign_t
x_set_text_property(term_t display, term_t w, term_t text_prop, term_t property)