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
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/io/videosources.h"
#include "gate/platforms.h"
#include "gate/results.h"
#if defined(GATE_SYS_WIN)
# if defined(GATE_SYS_WINSTORE) || defined(GATE_SYS_WIN16)
# define GATE_IO_VIDEO_NO_IMPL
# else
# define GATE_IO_VIDEO_WINAPI
# define GATE_IO_VIDEO_SCREEN_INTERFACE
# endif
#elif defined(GATE_SYS_ANDROID)
# define GATE_IO_VIDEO_ANDROID
#elif defined(GATE_SYS_LINUX)
# define GATE_IO_VIDEO_X11
# define GATE_IO_VIDEO_SCREEN_INTERFACE
#else
# define GATE_IO_VIDEO_NO_IMPL
#endif
#if defined(GATE_IO_VIDEO_SCREEN_INTERFACE)
static char const* screenctrl_get_interface_name(void* thisptr);
static void screenctrl_release(void* thisptr);
static int screenctrl_retain(void* thisptr);
static char const* screenctrl_get_id(void* thisptr);
static char const* screenctrl_get_name(void* thisptr);
static gate_intptr_t screenctrl_get_handle(void* thisptr);
static gate_size_t screenctrl_get_supported_formats(void* thisptr, gate_video_format_t* format_buffer, gate_size_t format_buffer_count);
static gate_result_t screenctrl_open(void* thisptr, gate_video_format_t const* format);
static gate_result_t screenctrl_close(void* thisptr);
static gate_result_t screenctrl_read(void* thisptr, gate_video_frame_t* frame);
static gate_result_t screenctrl_get_cursor_pos(void* thisptr, gate_int32_t* ptr_x, gate_int32_t* ptr_y);
static gate_result_t screenctrl_move_cursor(void* thisptr, gate_int32_t x, gate_int32_t y);
static gate_result_t screenctrl_change_cursor_button_state(void* thisptr, gate_int32_t x, gate_int32_t y, gate_enumint_t button_id, gate_enumint_t button_state);
static gate_result_t screenctrl_change_key_state(void* thisptr, gate_input_keycode_t key_code, gate_enumint_t key_state);
static GATE_INTERFACE_VTBL(gate_video_screencontrol) const* init_video_screencontrol_vtbl()
{
static GATE_INTERFACE_VTBL(gate_video_screencontrol) global_vtbl;
if (global_vtbl.get_interface_name == NULL)
{
GATE_INTERFACE_VTBL(gate_video_screencontrol) const local_vtbl =
{
&screenctrl_get_interface_name,
&screenctrl_release,
&screenctrl_retain,
&screenctrl_read,
&screenctrl_get_id,
&screenctrl_get_name,
&screenctrl_get_handle,
&screenctrl_get_supported_formats,
&screenctrl_open,
&screenctrl_close,
&screenctrl_get_cursor_pos,
&screenctrl_move_cursor,
&screenctrl_change_cursor_button_state,
&screenctrl_change_key_state
};
global_vtbl = local_vtbl;
}
return &global_vtbl;
}
#endif
#if defined(GATE_IO_VIDEO_WINAPI)
#include "gate/debugging.h"
#include "gate/graphics/platform/win32_gdi.h"
#if !defined(GATE_SYS_WINCE)
#define GATE_USE_NT_DESKTOP_API 1
#endif
typedef struct win32screen_class
{
GATE_INTERFACE_VTBL(gate_video_screencontrol) const* vtbl;
gate_atomic_int_t ref_counter;
unsigned x;
unsigned y;
unsigned width;
unsigned height;
unsigned bits_per_pixel;
unsigned line_len;
void* hdesk;
TCHAR deskname[128];
gate_size_t deskname_length;
HDC hscreen;
HDC hdc;
HGDIOBJ hbmp_backup;
HBITMAP hbmp;
void* bitmap_bits;
gate_size_t bitmap_bits_length;
} win32screen_t;
typedef struct
{
BITMAPINFO info;
RGBQUAD colors[256];
} BITMAPINFO_PAL;
static unsigned init_bmp_pal(RGBQUAD* pal)
{
gate_color_rgb_t gate_pal[256];
unsigned pal_len = (unsigned)gate_color_palette_rgb8_create(gate_pal);
unsigned ndx = 0;
for (; ndx != pal_len; ++ndx)
{
pal[ndx].rgbRed = gate_pal[ndx].r;
pal[ndx].rgbGreen = gate_pal[ndx].g;
pal[ndx].rgbBlue = gate_pal[ndx].b;
pal[ndx].rgbReserved = 0;
}
return pal_len;
}
static gate_bool_t win32_open_screen(win32screen_t* self)
{
gate_win32_userapi_t* const user32 = gate_win32_userapi();
gate_result_t succeeded = false;
HDC screen = NULL;
HDC mem = NULL;
HBITMAP bmp = NULL;
void* bits = NULL;
BITMAPINFO_PAL bmpinfo_pal;
BITMAPINFOHEADER* hdr = &bmpinfo_pal.info.bmiHeader;
RGBQUAD* ptr_rgb;
do
{
if (!user32) break;
screen = user32->UserGetDC(NULL);
if (!screen) break;
mem = gdi.GdiCreateCompatibleDC(screen);
if (!mem) break;
gate_mem_clear(&bmpinfo_pal, sizeof(bmpinfo_pal));
hdr->biSize = sizeof(BITMAPINFOHEADER);
hdr->biWidth = (LONG)self->width;
hdr->biHeight = (LONG)self->height;
hdr->biPlanes = 1;
hdr->biBitCount = self->bits_per_pixel;
hdr->biCompression = BI_RGB;
hdr->biSizeImage = 0;
hdr->biXPelsPerMeter = 0;
hdr->biYPelsPerMeter = 0;
hdr->biClrImportant = 0;
hdr->biClrUsed = 0;
if (self->bits_per_pixel == 1)
{
hdr->biClrUsed = 2;
ptr_rgb = &bmpinfo_pal.info.bmiColors[0];
ptr_rgb->rgbBlue = ptr_rgb->rgbGreen = ptr_rgb->rgbRed = ptr_rgb->rgbReserved = 0;
ptr_rgb = &bmpinfo_pal.info.bmiColors[1];
ptr_rgb->rgbBlue = ptr_rgb->rgbGreen = ptr_rgb->rgbRed = 255;
ptr_rgb->rgbReserved = 0;
}
else if (self->bits_per_pixel == 8)
{
hdr->biClrUsed = init_bmp_pal(&bmpinfo_pal.info.bmiColors[0]);
}
bmp = gdi.GdiCreateDIBSection(screen, &bmpinfo_pal.info, DIB_RGB_COLORS, &bits, NULL, 0);
if (NULL == bmp) break;
/* success-case: */
self->hscreen = screen;
self->hbmp_backup = gdi.GdiSelectObject(mem, (HGDIOBJ)bmp);
self->hdc = mem;
self->hbmp = bmp;
self->bitmap_bits = bits;
succeeded = true;
} while (0);
if (!succeeded)
{
if (mem) gdi.GdiDeleteDC(mem);
if (screen) user32->UserReleaseDC(NULL, screen);
}
return succeeded;
}
static void win32_close_screen(win32screen_t* self)
{
if (self->hdc)
{
gdi.GdiSelectObject(self->hdc, self->hbmp_backup);
gdi.GdiDeleteObject((HGDIOBJ)self->hbmp);
gdi.GdiDeleteDC(self->hdc);
gdi.GdiDeleteDC(self->hscreen);
self->hbmp_backup = NULL;
self->hbmp = NULL;
self->hdc = NULL;
}
if (self->hscreen)
{
self->hscreen = NULL;
}
}
#if defined(GATE_USE_NT_DESKTOP_API)
static gate_size_t win32_get_desktop_name(void* hdesk, TCHAR* desktop_name_buffer, gate_size_t desktop_name_buffer_capacity)
{
gate_win32_userapi_t* const user32 = gate_win32_userapi();
DWORD bytes_needed;
if (!user32 || !user32->UserGetUserObjectInformation ||
!user32->UserGetUserObjectInformation(hdesk, UOI_NAME, desktop_name_buffer, (DWORD)desktop_name_buffer_capacity * sizeof(TCHAR), &bytes_needed))
{
/* api not available, or failed */
return 0;
}
return bytes_needed / sizeof(TCHAR);
}
static gate_bool_t win32_open_input_desktop(void** ptr_hdesk, TCHAR* ptr_deskname, gate_size_t* ptr_deskname_length)
{
gate_win32_userapi_t* const user32 = gate_win32_userapi();
void* hdesk;
if (!user32 || !user32->UserOpenInputDesktop)
{
return false;
}
hdesk = user32->UserOpenInputDesktop(0, FALSE, GENERIC_ALL);
if (hdesk == NULL)
{
return false;
}
*ptr_deskname_length = win32_get_desktop_name(hdesk, ptr_deskname, *ptr_deskname_length);
*ptr_hdesk = hdesk;
return true;
}
static void win32_close_desktop(void** hdesk)
{
if (*hdesk)
{
gate_win32_userapi_t* const user32 = gate_win32_userapi();
user32->UserCloseDesktop((HDESK)*hdesk);
*hdesk = NULL;
}
}
static gate_bool_t win32_are_desktops_equal(void* desk1, TCHAR const* desk1name, gate_size_t desk1name_len, void* desk2, TCHAR const* desk2name, gate_size_t desk2name_len)
{
if (desk1 == desk2)
{
return true;
}
if (desk1name_len != desk2name_len)
{
return false;
}
else
{
return 0 == gate_win32_winstr_comp_range(desk1name, desk2name, desk1name_len);
}
}
static gate_bool_t win32_change_thread_desktop(void* target_desktop)
{
gate_win32_userapi_t* const user32 = gate_win32_userapi();
if (!user32 || !user32->UserSetThreadDesktop
|| !user32->UserSetThreadDesktop((HDESK)target_desktop))
{
GATE_DEBUG_TRACE("SetThreadDesktop() failed");
return false;
}
return true;
}
static gate_bool_t screenctrl_update_desktop(win32screen_t* self)
{
void* input_hdesk = NULL;
TCHAR input_deskname[128];
gate_size_t input_deskname_len = sizeof(input_deskname) / sizeof(input_deskname[0]);
gate_bool_t result = win32_open_input_desktop(&input_hdesk, input_deskname, &input_deskname_len);
if (!result)
{
GATE_DEBUG_TRACE("Failed to open input desktop");
return false;
}
result = win32_are_desktops_equal(self->hdesk, self->deskname, self->deskname_length, input_hdesk, input_deskname, input_deskname_len);
if (result)
{
/* desktops are equal, no change required */
win32_close_desktop(&input_hdesk);
return true;
}
/* switch to new desktop */
result = win32_change_thread_desktop(input_hdesk);
if (!result)
{
GATE_DEBUG_TRACE("Failed to switch thread-desktop");
win32_close_desktop(&input_hdesk);
return false;
}
/* close old desktop handle and screen objects */
win32_close_screen(self);
win32_close_desktop(&self->hdesk);
/* install new desktop handle */
self->hdesk = input_hdesk;
gate_mem_copy(&self->deskname[0], input_deskname, input_deskname_len * sizeof(TCHAR));
self->deskname_length = input_deskname_len;
self->deskname[self->deskname_length] = 0;
return true;
}
#endif /* GATE_USE_NT_DESKTOP_API */
static gate_bool_t screenctrl_is_screen_opened(win32screen_t* self)
{
return (self->hscreen != NULL) && (self->hdc != NULL) && (self->hbmp != NULL);
}
static void screenctrl_destroy(win32screen_t* self)
{
win32_close_screen(self);
#if defined(GATE_USE_NT_DESKTOP_API)
win32_close_desktop(&self->hdesk);
#endif
}
static char const* screenctrl_get_interface_name(void* thisptr)
{
return GATE_INTERFACE_NAME_VIDEO_SOURCE_SCREENCONTROL;
}
static void screenctrl_release(void* thisptr)
{
win32screen_t* const self = (win32screen_t*)thisptr;
if (gate_atomic_int_dec(&self->ref_counter) == 0)
{
screenctrl_destroy(self);
}
}
static int screenctrl_retain(void* thisptr)
{
win32screen_t* const self = (win32screen_t*)thisptr;
return gate_atomic_int_inc(&self->ref_counter);
}
static char const* screenctrl_get_id(void* thisptr)
{
return "screen";
}
static char const* screenctrl_get_name(void* thisptr)
{
return "screen";
}
static gate_intptr_t screenctrl_get_handle(void* thisptr)
{
win32screen_t* const self = (win32screen_t*)thisptr;
return (gate_intptr_t)self->hscreen;
}
#ifndef SM_CXVIRTUALSCREEN
#define SM_CXVIRTUALSCREEN 78
#endif
#ifndef SM_CYVIRTUALSCREEN
#define SM_CYVIRTUALSCREEN 79
#endif
static gate_size_t screenctrl_get_supported_formats(void* thisptr, gate_video_format_t* format_buffer, gate_size_t format_buffer_count)
{
gate_win32_userapi_t* const user32 = gate_win32_userapi();
win32screen_t* const self = (win32screen_t*)thisptr;<--- Variable 'self' is assigned a value that is never used.
int sx, sy;
if ((format_buffer_count == 0) || (format_buffer == NULL))
{
return 0;
}
sx = user32->UserGetSystemMetrics(SM_CXVIRTUALSCREEN);
sy = user32->UserGetSystemMetrics(SM_CYVIRTUALSCREEN);
if (sx == 0) sx = user32->UserGetSystemMetrics(SM_CXSCREEN);
if (sy == 0) sy = user32->UserGetSystemMetrics(SM_CYSCREEN);
format_buffer->encoding_id = 0;
format_buffer->compression = 0;
format_buffer->frames_per_second = 5.0f;
format_buffer->width = sx;
format_buffer->height = sy;
format_buffer->bits_per_pixel = 24;
return 1;
}
static gate_bool_t screenctrl_is_opened(win32screen_t* self)
{
if ((self->width == 0) || (self->height == 0) || (self->bits_per_pixel == 0))
{
return false;
}
return true;
}
static gate_result_t screenctrl_open(void* thisptr, gate_video_format_t const* format)
{
win32screen_t* const self = (win32screen_t*)thisptr;
if (screenctrl_is_opened(self))
{
return GATE_RESULT_INVALIDSTATE;
}
if ((NULL == format) || (format->width == 0) || (format->height == 0))
{
return GATE_RESULT_INVALIDARG;
}
switch (format->bits_per_pixel)
{
case 1:
case 8:
case 16:
case 24:
self->bits_per_pixel = format->bits_per_pixel;
break;
default:
self->bits_per_pixel = 24;
break;
}
self->x = 0;
self->y = 0;
self->width = format->width;
self->height = format->height;
self->line_len = ((self->bits_per_pixel * self->width) / 8 + 3) & 0xfffffffc;
#if defined(GATE_USE_NT_DESKTOP_API)
screenctrl_update_desktop(self);
#endif
return GATE_RESULT_OK;
}
static gate_result_t screenctrl_close(void* thisptr)
{
win32screen_t* const self = (win32screen_t*)thisptr;
win32_close_screen(self);
#if defined(GATE_USE_NT_DESKTOP_API)
win32_close_desktop(&self->hdesk);
#endif
self->width = 0;
self->height = 0;
self->bits_per_pixel;
self->bitmap_bits = NULL;
self->bitmap_bits_length = 0;
return GATE_RESULT_OK;
}
static gate_result_t screenctrl_read(void* thisptr, gate_video_frame_t* frame)
{
win32screen_t* const self = (win32screen_t*)thisptr;
gate_bool_t update_succeeded;
gate_rasterimage_t raster = GATE_INIT_EMPTY;
unsigned y, x;
unsigned char const* ptr_src;
unsigned char* ptr_dst;
if ((self->width == 0) || (self->height == 0))
{
return GATE_RESULT_INVALIDSTATE;
}
#if defined(GATE_USE_NT_DESKTOP_API)
update_succeeded = screenctrl_update_desktop(self);
if (!update_succeeded)
{
GATE_DEBUG_TRACE("Failed to update desktop object");
}
#endif
if (!screenctrl_is_screen_opened(self))
{
if (!win32_open_screen(self))
{
GATE_DEBUG_TRACE("Failed to open screen");
return GATE_RESULT_FAILED;
}
}
update_succeeded = gdi.GdiBitBlt(self->hdc, 0, 0, self->width, self->height, self->hscreen, self->x, self->y, SRCCOPY);
if (!update_succeeded)
{
GATE_DEBUG_TRACE("Failed to copy screen into memory buffer");
return GATE_RESULT_FAILED;
}
switch (self->bits_per_pixel)
{
case 1:
{
if (NULL == gate_rasterimage_create(&raster, GATE_IMAGE_PIXELFORMAT_PAL8, self->width, self->height, NULL))
{
return GATE_RESULT_OUTOFMEMORY;
}
for (y = 0; y != self->height; ++y)
{
ptr_src = (unsigned char const*)self->bitmap_bits + (self->height - 1 - y) * self->line_len;
ptr_dst = (unsigned char*)gate_rasterimage_get_line_ptr(&raster, y);
for (x = 0; x != self->width; ++x)
{
gate_uint8_t b = ptr_src[x / 8];
gate_uint8_t bit = x % 8;
*ptr_dst = ((b & (1 << (7 - bit))) == 0) ? 0 : 15;
++ptr_dst;
}
}
break;
}
case 8:
{
if (NULL == gate_rasterimage_create(&raster, GATE_IMAGE_PIXELFORMAT_PAL8, self->width, self->height, NULL))
{
return GATE_RESULT_OUTOFMEMORY;
}
for (y = 0; y != self->height; ++y)
{
ptr_src = (unsigned char const*)self->bitmap_bits + (self->height - 1 - y) * self->line_len;
ptr_dst = (unsigned char*)gate_rasterimage_get_line_ptr(&raster, y);
gate_mem_copy(ptr_dst, ptr_src, self->width);
}
break;
}
case 15:
case 16:
{
if (NULL == gate_rasterimage_create(&raster, GATE_IMAGE_PIXELFORMAT_RGB555, self->width, self->height, NULL))
{
return GATE_RESULT_OUTOFMEMORY;
}
for (y = 0; y != self->height; ++y)
{
ptr_src = (unsigned char const*)self->bitmap_bits + (self->height - 1 - y) * self->line_len;
ptr_dst = (unsigned char*)gate_rasterimage_get_line_ptr(&raster, y);
for (x = 0; x != self->width; ++x)
{
gate_uint8_t const b = (ptr_src[0] & 0x1f) << 3;
gate_uint8_t const g = ((ptr_src[0] >> 5) | ((ptr_src[1] & 0x03) << 3)) << 3;
gate_uint8_t const r = (ptr_src[1] & 0x7c) << 1;
unsigned const num = ((unsigned)(r >> 3)) << 10
| ((unsigned)(g >> 3)) << 5
| ((unsigned)(b >> 3));
ptr_dst[0] = (gate_uint8_t)(num & 0xff);
ptr_dst[1] = (gate_uint8_t)(num >> 8);
ptr_src += 2;
ptr_dst += 2;
}
}
break;
}
case 24:
{
if (NULL == gate_rasterimage_create(&raster, GATE_IMAGE_PIXELFORMAT_RGB24, self->width, self->height, NULL))
{
return GATE_RESULT_OUTOFMEMORY;
}
for (y = 0; y != self->height; ++y)
{
ptr_src = (unsigned char const*)self->bitmap_bits + (self->height - 1 - y) * self->line_len;
ptr_dst = (unsigned char*)gate_rasterimage_get_line_ptr(&raster, y);
for (x = 0; x != self->width; ++x)
{
/* BGR to RGB */
ptr_dst[0] = ptr_src[2];
ptr_dst[1] = ptr_src[1];
ptr_dst[2] = ptr_src[0];
ptr_src += 3;
ptr_dst += 3;
}
}
break;
}
default:
{
return GATE_RESULT_NOTSUPPORTED;
}
}
frame->format.encoding_id = 0;
frame->format.compression = 0;
frame->format.frames_per_second = 5.0f;
frame->format.width = self->width;
frame->format.height = self->height;
frame->format.bits_per_pixel = self->bits_per_pixel;
frame->record_time = 0;
gate_mem_copy(&frame->image, &raster, sizeof(raster));
return GATE_RESULT_OK;
}
static gate_result_t win32_send_input(INPUT* inp)
{
gate_win32_userapi_t* const user32 = gate_win32_userapi();
UINT result;
if (!user32)
{
return GATE_RESULT_NOTAVAILABLE;
}
if (user32->UserSendInput)
{
result = user32->UserSendInput(1, inp, sizeof(INPUT));
if (result != 1)
{
return GATE_RESULT_FAILED;
}
return GATE_RESULT_OK;
}
/* win95 NT 3.1 do not support SendInput: */
if ((inp->type == INPUT_MOUSE) && (user32->UserMouse_event))
{
user32->UserMouse_event(inp->mi.dwFlags, inp->mi.dx, inp->mi.dy, inp->mi.mouseData, inp->mi.dwExtraInfo);
return GATE_RESULT_OK;
}
if ((inp->type == INPUT_KEYBOARD) && (user32->UserKeybd_event))
{
user32->UserKeybd_event((BYTE)inp->ki.wVk, (BYTE)inp->ki.wScan, inp->ki.dwFlags, inp->ki.dwExtraInfo);
return GATE_RESULT_OK;
}
return GATE_RESULT_NOTSUPPORTED;
}
#ifndef MOUSEEVENTF_VIRTUALDESK
#define MOUSEEVENTF_VIRTUALDESK 0x4000
#endif
static void win32_prepare_mouse_input(INPUT* inp, gate_int32_t x, gate_int32_t y)
{
gate_win32_userapi_t* const user32 = gate_win32_userapi();
int screen_width;
int screen_height;
if (user32)
{
screen_width = user32->UserGetSystemMetrics(SM_CXVIRTUALSCREEN);
screen_height = user32->UserGetSystemMetrics(SM_CYVIRTUALSCREEN);
if (screen_width == 0) screen_width = user32->UserGetSystemMetrics(SM_CXSCREEN);
if (screen_height == 0) screen_height = user32->UserGetSystemMetrics(SM_CYSCREEN);
inp->mi.dwExtraInfo = user32->UserGetMessageExtraInfo();
}
else
{
screen_width = 640;
screen_height = 480;
inp->mi.dwExtraInfo = 0;
}
inp->type = INPUT_MOUSE;
inp->mi.dx = (screen_width > 0) ? (x * 65536 / screen_width) : 0;
inp->mi.dy = (screen_height > 0) ? (y * 65536 / screen_height) : 0;
inp->mi.mouseData = 0;
inp->mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK;
inp->mi.time = 0;
}
static gate_result_t screenctrl_get_cursor_pos(void* thisptr, gate_int32_t* ptr_x, gate_int32_t* ptr_y)
{
gate_win32_userapi_t* const user32 = gate_win32_userapi();
POINT pnt;
if (!user32)
{
return GATE_RESULT_FAILED;
}
if (!user32->UserGetCursorPos(&pnt))
{
return GATE_RESULT_FAILED;
}
if (ptr_x) *ptr_x = pnt.x;
if (ptr_y) *ptr_y = pnt.y;
return GATE_RESULT_OK;
}
static gate_result_t screenctrl_move_cursor(void* thisptr, gate_int32_t x, gate_int32_t y)
{
INPUT inp;
win32_prepare_mouse_input(&inp, x, y);
return win32_send_input(&inp);
}
static gate_result_t screenctrl_change_cursor_button_state(void* thisptr, gate_int32_t x, gate_int32_t y, gate_enumint_t button_id, gate_enumint_t button_state)
{
INPUT inp;
win32_prepare_mouse_input(&inp, x, y);
if (GATE_FLAG_ENABLED(button_state, GATE_VIDEO_SCREENCONTROL_STATE_DOWN))
{
/* mouse down */
if (button_id == GATE_VIDEO_SCREENCONTROL_BUTTON_LEFT) inp.mi.dwFlags |= MOUSEEVENTF_LEFTDOWN;
if (button_id == GATE_VIDEO_SCREENCONTROL_BUTTON_MIDDLE) inp.mi.dwFlags |= MOUSEEVENTF_MIDDLEDOWN;
if (button_id == GATE_VIDEO_SCREENCONTROL_BUTTON_RIGHT) inp.mi.dwFlags |= MOUSEEVENTF_RIGHTDOWN;
}
else if (GATE_FLAG_ENABLED(button_state, GATE_VIDEO_SCREENCONTROL_STATE_UP))
{
/* mouse up */
if (button_id == GATE_VIDEO_SCREENCONTROL_BUTTON_LEFT) inp.mi.dwFlags |= MOUSEEVENTF_LEFTUP;
if (button_id == GATE_VIDEO_SCREENCONTROL_BUTTON_MIDDLE) inp.mi.dwFlags |= MOUSEEVENTF_MIDDLEUP;
if (button_id == GATE_VIDEO_SCREENCONTROL_BUTTON_RIGHT) inp.mi.dwFlags |= MOUSEEVENTF_RIGHTUP;
}
return win32_send_input(&inp);
}
static gate_input_keystates_t extract_state_keys_from_key_state_flags(gate_enumint_t key_state)
{
gate_input_keystates_t value = 0;
if (GATE_FLAG_ENABLED(key_state, GATE_VIDEO_SCREENCONTROL_STATE_CTRL)) value |= GATE_KBD_KEYSTATE_CTRL;
if (GATE_FLAG_ENABLED(key_state, GATE_VIDEO_SCREENCONTROL_STATE_MENU)) value |= GATE_KBD_KEYSTATE_MENU;
if (GATE_FLAG_ENABLED(key_state, GATE_VIDEO_SCREENCONTROL_STATE_SHIFT)) value |= GATE_KBD_KEYSTATE_SHIFT;
return value;
}
static void win32_prepare_key_input(INPUT* inp, gate_input_keycode_t keycode, gate_input_keystates_t keystates, gate_bool_t down)
{
gate_win32_userapi_t* const user32 = gate_win32_userapi();
gate_input_nativekeycode_t vk = 0;
if (user32 && user32->UserGetMessageExtraInfo)
{
inp->ki.dwExtraInfo = user32->UserGetMessageExtraInfo();
}
else
{
inp->ki.dwExtraInfo = 0;
}
inp->type = INPUT_KEYBOARD;
gate_keyboard_build_native_key(keycode, keystates, &vk);
inp->ki.wVk = (WORD)vk;
inp->ki.wScan = 0;
inp->ki.dwFlags = down ? 0 : KEYEVENTF_KEYUP;
inp->ki.time = 0;
}
static gate_result_t screenctrl_change_key_state(void* thisptr, gate_input_keycode_t key_code, gate_enumint_t key_state)
{
INPUT inp;
gate_input_keystates_t state_keys = extract_state_keys_from_key_state_flags(key_state);
win32_prepare_key_input(&inp, key_code, state_keys, GATE_FLAG_ENABLED(key_state, GATE_VIDEO_SCREENCONTROL_STATE_DOWN));
return win32_send_input(&inp);
}
gate_result_t gate_video_screencontrol_create(gate_video_screencontrol_t** ptr_source)
{
gate_result_t ret = GATE_RESULT_FAILED;
win32screen_t* impl = NULL;
do
{
ret = gate_win32_gdi_init();
GATE_BREAK_IF_FAILED(ret);
impl = (win32screen_t*)gate_mem_alloc(sizeof(win32screen_t));
if (impl == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
gate_mem_clear(impl, sizeof(win32screen_t));
impl->vtbl = init_video_screencontrol_vtbl();
gate_atomic_int_init(&impl->ref_counter, 1);
if (ptr_source)
{
*ptr_source = (gate_video_screencontrol_t*)impl;
impl = NULL;
}
ret = GATE_RESULT_OK;
} while (0);
if (impl != NULL)
{
screenctrl_release(impl);
}
return ret;
}
#endif
#if defined(GATE_IO_VIDEO_X11)
#include "gate/platform/posix/xlib.h"
#include "gate/debugging.h"
typedef struct x11screen_class
{
GATE_INTERFACE_VTBL(gate_video_screencontrol) const* vtbl;
gate_atomic_int_t ref_counter;
Display* x11_display;
Window x11_root;
gate_video_format_t format;
GC x11_gc;
int x11_screen;
Colormap x11_colmap;
} x11screen_t;
static gate_result_t x11screen_init(x11screen_t* self)
{
gate_result_t result;
do
{
result = gate_xlib_load_functions();
GATE_BREAK_IF_FAILED(result);
self->x11_display = xlib.XOpenDisplay(NULL);
if (self->x11_display == NULL)
{
self->x11_display = xlib.XOpenDisplay(":0");
}
if (self->x11_display == NULL)
{
GATE_DEBUG_TRACE("Failed to open X11 display");
result = GATE_RESULT_FAILED;
break;
}
self->x11_root = xlib.XDefaultRootWindow(self->x11_display);
} while (0);
return result;
}
static void x11screen_destroy(x11screen_t* self)
{
if (self->x11_display)
{
xlib.XCloseDisplay(self->x11_display);
self->x11_display = NULL;
}
}
static char const* screenctrl_get_interface_name(void* thisptr)
{
return GATE_INTERFACE_NAME_VIDEO_SOURCE_SCREENCONTROL;
}
static void screenctrl_release(void* thisptr)
{
x11screen_t* const self = (x11screen_t*)thisptr;
if (gate_atomic_int_dec(&self->ref_counter) == 0)
{
x11screen_destroy(self);
gate_mem_dealloc(self);
}
}
static int screenctrl_retain(void* thisptr)
{
x11screen_t* const self = (x11screen_t*)thisptr;
return gate_atomic_int_inc(&self->ref_counter);
}
static char const* screenctrl_get_id(void* thisptr)
{
return "screen";
}
static char const* screenctrl_get_name(void* thisptr)
{
return "screen";
}
static gate_intptr_t screenctrl_get_handle(void* thisptr)
{
x11screen_t* const self = (x11screen_t*)thisptr;
return (gate_intptr_t)self->x11_display;
}
static gate_size_t screenctrl_get_supported_formats(void* thisptr, gate_video_format_t* format_buffer, gate_size_t format_buffer_count)
{
x11screen_t* const self = (x11screen_t*)thisptr;
XWindowAttributes attrs = GATE_INIT_EMPTY;
if ((format_buffer == NULL) || (format_buffer_count < 1))
{
GATE_DEBUG_TRACE("Not enough space in output buffer");
return 0;
}
/* get screen dimensions */
xlib.XGetWindowAttributes(self->x11_display, self->x11_root, &attrs);
format_buffer->encoding_id = 0;
format_buffer->compression = 0;
format_buffer->frames_per_second = 5;
format_buffer->width = (gate_uint32_t)attrs.width;
format_buffer->height = (gate_uint32_t)attrs.height;
format_buffer->bits_per_pixel = 32;
return 1; /* one entry was used */
}
static gate_result_t screenctrl_open(void* thisptr, gate_video_format_t const* format)
{
x11screen_t* const self = (x11screen_t*)thisptr;
if (self->x11_gc != NULL)
{
GATE_DEBUG_TRACE("Error: X11 GC is already initialized");
return GATE_RESULT_INVALIDSTATE;
}
gate_mem_copy(&self->format, format, sizeof(gate_video_format_t));
self->x11_gc = xlib.XCreateGC(self->x11_display, self->x11_root, 0, NULL);
if (!self->x11_gc)
{
GATE_DEBUG_TRACE("Error: Failed to create GC on current display");
return GATE_RESULT_FAILED;
}
self->x11_screen = xlib.XDefaultScreen(self->x11_display);
self->x11_colmap = xlib.XDefaultColormap(self->x11_display, self->x11_screen);
return GATE_RESULT_OK;
}
static gate_result_t screenctrl_close(void* thisptr)
{
x11screen_t* const self = (x11screen_t*)thisptr;
if (!self->x11_gc)
{
xlib.XFreeGC(self->x11_display, self->x11_gc);
self->x11_gc = NULL;
self->x11_screen = 0;
self->x11_colmap = 0;
}
return GATE_RESULT_OK;
}
static gate_result_t ximage32_to_raster(XImage* ptr_ximage, gate_rasterimage_t* ptr_raster)
{
unsigned char const* ptr_src_pixel = (unsigned char const*)ptr_ximage->data;
gate_color_rgb_t* ptr_dst_pixel;<--- The scope of the variable 'ptr_dst_pixel' can be reduced. [+]The scope of the variable 'ptr_dst_pixel' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
unsigned int y, x;
if (NULL == gate_rasterimage_create(ptr_raster, GATE_IMAGE_PIXELFORMAT_RGB24,
(unsigned)ptr_ximage->width, (unsigned)ptr_ximage->height, NULL))
{
return GATE_RESULT_OUTOFMEMORY;
}
for (y = 0; y != ptr_raster->height; ++y)
{
ptr_dst_pixel = (gate_color_rgb_t*)gate_rasterimage_get_line_ptr(ptr_raster, y);
for (x = 0; x != ptr_raster->width; ++x)
{
ptr_dst_pixel->b = *(ptr_src_pixel++);
ptr_dst_pixel->g = *(ptr_src_pixel++);
ptr_dst_pixel->r = *(ptr_src_pixel++);
++ptr_src_pixel; /* skip A value */
++ptr_dst_pixel;
}
}
return GATE_RESULT_OK;
}
static gate_result_t ximage16_to_raster(XImage* ptr_ximage, gate_rasterimage_t* ptr_raster)
{
unsigned char const* ptr_src_pixel = (unsigned char const*)ptr_ximage->data;
gate_color_rgb_t* ptr_dst_pixel;<--- The scope of the variable 'ptr_dst_pixel' can be reduced. [+]The scope of the variable 'ptr_dst_pixel' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
unsigned int y, x;
if (NULL == gate_rasterimage_create(ptr_raster, GATE_IMAGE_PIXELFORMAT_RGB24,
(unsigned)ptr_ximage->width, (unsigned)ptr_ximage->height, NULL))
{
return GATE_RESULT_OUTOFMEMORY;
}
for (y = 0; y != ptr_raster->height; ++y)
{
ptr_dst_pixel = (gate_color_rgb_t*)gate_rasterimage_get_line_ptr(ptr_raster, y);
for (x = 0; x != ptr_raster->width; ++x)
{
const gate_uint16_t col = (gate_uint16_t)(ptr_src_pixel[0]) | (((gate_uint16_t)ptr_src_pixel[1]) << 8);
ptr_dst_pixel->b = (gate_uint8_t)((col & 0x001F) << 3);
ptr_dst_pixel->g = (gate_uint8_t)((col & 0x07e0) >> 3);
ptr_dst_pixel->r = (gate_uint8_t)((col & 0xF800) >> 8);
ptr_src_pixel += 2;
++ptr_dst_pixel;
}
}
return GATE_RESULT_OK;
}
static gate_result_t ximage_to_raster(Display* display, Colormap colmap, XImage* ptr_ximage, gate_rasterimage_t* ptr_raster)
{
gate_color_rgb_t* ptr_dst_pixel;<--- The scope of the variable 'ptr_dst_pixel' can be reduced. [+]The scope of the variable 'ptr_dst_pixel' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
unsigned int y, x;
XColor xcol;
if (NULL == gate_rasterimage_create(ptr_raster, GATE_IMAGE_PIXELFORMAT_RGB24,
(unsigned)ptr_ximage->width, (unsigned)ptr_ximage->height, NULL))
{
return GATE_RESULT_OUTOFMEMORY;
}
for (y = 0; y != ptr_raster->height; ++y)
{
ptr_dst_pixel = (gate_color_rgb_t*)gate_rasterimage_get_line_ptr(ptr_raster, y);
for (x = 0; x != ptr_raster->width; ++x)
{
xcol.pixel = xlib.XGetPixel(ptr_ximage, (int)x, (int)y);
xlib.XQueryColor(display, colmap, &xcol);
ptr_dst_pixel->r = (gate_uint8_t)(xcol.red >> 8);
ptr_dst_pixel->g = (gate_uint8_t)(xcol.green >> 8);
ptr_dst_pixel->b = (gate_uint8_t)(xcol.blue >> 8);
++ptr_dst_pixel;
}
}
return GATE_RESULT_OK;
}
static gate_result_t screenctrl_read(void* thisptr, gate_video_frame_t* frame)
{
gate_result_t ret = GATE_RESULT_FAILED;
x11screen_t* const self = (x11screen_t*)thisptr;
XImage* ptr_ximage = NULL;
gate_rasterimage_t raster = GATE_INIT_EMPTY;
do
{
ptr_ximage = xlib.XGetImage(self->x11_display, self->x11_root,
0, 0, self->format.width, self->format.height, AllPlanes, ZPixmap);
if (ptr_ximage == NULL)
{
ret = GATE_RESULT_FAILED;
break;
}
if ((ptr_ximage->bits_per_pixel == 32) && (ptr_ximage->depth == 24))
{
ret = ximage32_to_raster(ptr_ximage, &raster);
}
else if ((ptr_ximage->bits_per_pixel == 16) && (ptr_ximage->depth == 16))
{
ret = ximage16_to_raster(ptr_ximage, &raster);
}
else
{
ret = ximage_to_raster(self->x11_display, self->x11_colmap, ptr_ximage, &raster);
}
GATE_BREAK_IF_FAILED(ret);
frame->format.bits_per_pixel = raster.bits_per_pixel;
frame->format.compression = 0;
frame->format.encoding_id = 0;
frame->format.frames_per_second = self->format.frames_per_second;
frame->format.width = raster.width;
frame->format.height = raster.height;
gate_rasterimage_duplicate(&frame->image, &raster);
frame->record_time = 0;
ret = GATE_RESULT_OK;
} while (0);
gate_rasterimage_release(&raster);
if (ptr_ximage != NULL)
{
xlib.XDestroyImage(ptr_ximage);
}
return ret;
}
static gate_result_t screenctrl_get_cursor_pos(void* thisptr, gate_int32_t* ptr_x, gate_int32_t* ptr_y)
{
gate_result_t ret = GATE_RESULT_FAILED;
x11screen_t* const self = (x11screen_t*)thisptr;
Window win, subwin;
int gx, gy, wx, wy;
unsigned int state;
if (xlib.XQueryPointer(self->x11_display, self->x11_root, &win, &subwin, &gx, &gy, &wx, &wy, &state))
{
if (ptr_x) *ptr_x = gx;
if (ptr_y) *ptr_y = gy;
return GATE_RESULT_OK;
}
return GATE_RESULT_FAILED;
}
static gate_result_t screenctrl_move_cursor(void* thisptr, gate_int32_t x, gate_int32_t y)
{
gate_result_t ret = GATE_RESULT_FAILED;
x11screen_t* const self = (x11screen_t*)thisptr;
xlib.XWarpPointer(self->x11_display, None, self->x11_root, 0, 0, 0, 0, (int)x, (int)y);
return GATE_RESULT_OK;
}
static gate_result_t screenctrl_change_cursor_button_state(void* thisptr, gate_int32_t x, gate_int32_t y, gate_enumint_t button_id, gate_enumint_t button_state)
{
/* http://www.linuxquestions.org/questions/programming-9/simulating-a-mouse-click-594576/ */
gate_result_t ret = GATE_RESULT_FAILED;
x11screen_t* const self = (x11screen_t*)thisptr;
XEvent xevt = GATE_INIT_EMPTY;
XButtonEvent* evt = &xevt.xbutton;
xlib.XWarpPointer(self->x11_display, None, self->x11_root, 0, 0, 0, 0, (int)x, (int)y);
/* find inner sub-window */
evt->type = (button_state == GATE_VIDEO_SCREENCONTROL_STATE_DOWN) ? ButtonPress : ButtonRelease;
evt->display = self->x11_display;
evt->same_screen = True;
evt->time = CurrentTime;
evt->subwindow = self->x11_root;
do
{
evt->window = evt->subwindow;
xlib.XQueryPointer(self->x11_display, evt->window, &evt->root, &evt->subwindow, &evt->x_root, &evt->y_root, &evt->x, &evt->y, &evt->state);
} while (evt->subwindow != 0);
evt->state = 0;
if (button_id == GATE_VIDEO_SCREENCONTROL_BUTTON_LEFT)
{
evt->button = Button1;
evt->state |= Button1Mask;
}
else if (button_id == GATE_VIDEO_SCREENCONTROL_BUTTON_MIDDLE)
{
evt->button = Button2;
evt->state |= Button2Mask;
}
else if (button_id == GATE_VIDEO_SCREENCONTROL_BUTTON_RIGHT)
{
evt->button = Button3;
evt->state |= Button3Mask;
}
xlib.XSendEvent(self->x11_display, PointerWindow, True, 0xfff, &xevt);
xlib.XFlush(self->x11_display);
return GATE_RESULT_OK;
}
static gate_result_t screenctrl_change_key_state(void* thisptr, gate_input_keycode_t key_code, gate_enumint_t key_state)
{
gate_result_t ret = GATE_RESULT_FAILED;
x11screen_t* const self = (x11screen_t*)thisptr;
XKeyEvent evt;
int focus_state;
Window focus_win = self->x11_root;
KeySym x11_key_sym = key_code;
gate_bool_t key_down = GATE_FLAG_ENABLED(key_state, GATE_VIDEO_SCREENCONTROL_STATE_DOWN);
unsigned int x11_state = 0;
if (key_down && GATE_FLAG_ENABLED(key_state, GATE_VIDEO_SCREENCONTROL_STATE_CTRL))
{
x11_state |= ControlMask;
}
if (key_down && GATE_FLAG_ENABLED(key_state, GATE_VIDEO_SCREENCONTROL_STATE_MENU))
{
x11_state |= Mod1Mask;
}
if (key_down && GATE_FLAG_ENABLED(key_state, GATE_VIDEO_SCREENCONTROL_STATE_SHIFT))
{
x11_state |= ShiftMask;
}
xlib.XGetInputFocus(self->x11_display, &focus_win, &focus_state);
evt.display = self->x11_display;
evt.window = focus_win;
evt.root = self->x11_root;
evt.subwindow = None;
evt.time = CurrentTime;
evt.x = 1;
evt.y = 1;
evt.x_root = 1;
evt.y_root = 1;
evt.same_screen = True;
evt.send_event = True;
evt.keycode = xlib.XKeysymToKeycode(self->x11_display, x11_key_sym);
evt.state = x11_state;
evt.type = key_down ? KeyPress : KeyRelease;
xlib.XSendEvent(evt.display, evt.window, True, KeyPressMask, (XEvent*)&evt);
return GATE_RESULT_OK;
}
gate_result_t gate_video_screencontrol_create(gate_video_screencontrol_t** ptr_source)
{
gate_result_t ret = GATE_RESULT_FAILED;
x11screen_t* impl = NULL;
do
{
impl = (x11screen_t*)gate_mem_alloc(sizeof(x11screen_t));
if (impl == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
gate_mem_clear(impl, sizeof(x11screen_t));
impl->vtbl = init_video_screencontrol_vtbl();
gate_atomic_int_init(&impl->ref_counter, 1);
ret = x11screen_init(impl);
GATE_BREAK_IF_FAILED(ret);
if (ptr_source)
{
*ptr_source = (gate_video_screencontrol_t*)impl;
impl = NULL;
}
ret = GATE_RESULT_OK;
} while (0);
if (impl != NULL)
{
screenctrl_release(impl);
}
return ret;
}
#endif
#if defined(GATE_IO_VIDEO_ANDROID)
gate_result_t gate_video_screencontrol_create(gate_video_screencontrol_t** ptr_source)
{
GATE_UNUSED_ARG(ptr_source);
/* TODO */
return GATE_RESULT_NOTSUPPORTED;
}
#endif
#if defined(GATE_IO_VIDEO_NO_IMPL)
gate_result_t gate_video_screencontrol_create(gate_video_screencontrol_t** ptr_source)
{
GATE_UNUSED_ARG(ptr_source);
return GATE_RESULT_NOTSUPPORTED;
}
#endif
|