-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx11vnc.c
2907 lines (2609 loc) · 82.8 KB
/
x11vnc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* xrdp: A Remote Desktop Protocol server.
*
* Copyright (C) Jay Sorg 2004-2015
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* libvnc
*
* The message definitions used in this source file can be found mostly
* in RFC6143 - "The Remote Framebuffer Protocol".
*
* The ExtendedDesktopSize encoding is reserved in RFC6143, but not
* documented there. It is documented by the RFB protocol community
* wiki currently held at https://github.com/rfbproto/rfbroto. This is
* referred to below as the "RFB community wiki"
*/
#if defined(HAVE_CONFIG_H)
#include <config_ac.h>
#endif
#include "../vnc/vnc.h"
#include "log.h"
#include "trans.h"
#include "ssl_calls.h"
#include "string_calls.h"
#include "xrdp_client_info.h"
#include <stdio.h>
#include <strings.h>
#include <inttypes.h>
typedef enum
{
X11_VNC_KEY_VALID=0x01,
X11_VNC_KEY_AUTO_REPEAT=0x02,
X11_VNC_KEY_IS_DOWN=0x04,
X11_VNC_KEY_CAPS_LOCKABLE=0x08,
X11_VNC_KEY_NUM_LOCKABLE=0x10,
X11_VNC_KEY_IS_CAPSLOCK=0x20,
X11_VNC_KEY_IS_NUMLOCK=0x40,
} X11VncKeyAttrs;
typedef enum
{
X11_VNC_KEY_RELEASED=0,
X11_VNC_KEY_PRESSED=1
} X11VncKeyDirection;
struct x11vnckey
{
X11VncKeyAttrs attrs; // X11_VNC_KEY_* per rdp keycode
uint32_t vncKeyCode; // if appropriate based on keyState
uint32_t shiftedVncKeyCode; // if appropriate based on keyState
};
static int keyAutoRepeats(const struct x11vnckey* const k)
{
return k->attrs & X11_VNC_KEY_AUTO_REPEAT;
}
static int keyIsCapsLockable(const struct x11vnckey* const k)
{
return k->attrs & X11_VNC_KEY_CAPS_LOCKABLE;
}
static int keyIsNumLockable(const struct x11vnckey* const k)
{
return k->attrs & X11_VNC_KEY_NUM_LOCKABLE;
}
static int keyIsCapsLock(const struct x11vnckey* const k)
{
return k->attrs & X11_VNC_KEY_IS_CAPSLOCK;
}
static int keyIsNumLock(const struct x11vnckey* const k)
{
return k->attrs & X11_VNC_KEY_IS_NUMLOCK;
}
static int keyIsDown(const struct x11vnckey* const k)
{
return k->attrs & X11_VNC_KEY_IS_DOWN;
}
static void setKeyDown(struct x11vnckey* const k)
{
k->attrs |= X11_VNC_KEY_IS_DOWN;
}
static void setKeyUp(struct x11vnckey* const k)
{
k->attrs &= ~X11_VNC_KEY_IS_DOWN;
}
struct x11vnc
{
struct vnc _;
// second 256 keys maps windows event with 0x100 bit set, e.g. kp /
// which has same rdp keycode as main / but 0x100 set in event
struct x11vnckey keys[2*256];
int capsLocked;
int numLocked;
};
/* Client-to-server messages */
enum c2s
{
C2S_SET_PIXEL_FORMAT = 0,
C2S_SET_ENCODINGS = 2,
C2S_FRAMEBUFFER_UPDATE_REQUEST = 3,
C2S_KEY_EVENT = 4,
C2S_POINTER_EVENT = 5,
C2S_CLIENT_CUT_TEXT = 6,
};
/* Server to client messages */
enum s2c
{
S2C_FRAMEBUFFER_UPDATE = 0,
S2C_SET_COLOUR_MAP_ENTRIES = 1,
S2C_BELL = 2,
S2C_SERVER_CUT_TEXT = 3
};
/* Encodings and pseudo-encodings
*
* The RFC uses a signed type for these. We use an unsigned type as the
* binary representation for the negative values is standardised in C
* (which it wouldn't be for an enum value)
*/
typedef uint32_t encoding_type;
#define ENC_RAW (encoding_type)0
#define ENC_COPY_RECT (encoding_type)1
#define ENC_CURSOR (encoding_type)-239
#define ENC_DESKTOP_SIZE (encoding_type)-223
#define ENC_EXTENDED_DESKTOP_SIZE (encoding_type)-308
/* Messages for ExtendedDesktopSize status code */
static const char *eds_status_msg[] =
{
/* 0 */ "No error",
/* 1 */ "Resize is administratively prohibited",
/* 2 */ "Out of resources",
/* 3 */ "Invalid screen layout",
/* others */ "Unknown code"
};
/* elements in above list */
#define EDS_STATUS_MSG_COUNT \
(sizeof(eds_status_msg) / sizeof(eds_status_msg[0]))
/* Used by enabled_encodings_mask */
enum
{
MSK_EXTENDED_DESKTOP_SIZE = (1 << 0)
};
static int
lib_mod_process_message(struct vnc *v, struct stream *s);
/******************************************************************************/
static int
lib_send_copy(struct vnc *v, struct stream *s)
{
return trans_write_copy_s(v->trans, s);
}
/******************************************************************************/
/* taken from vncauth.c */
/* performing the des3 crypt on the password so it can not be seen
on the wire
'bytes' in, contains 16 bytes server random
out, random and 'passwd' conbined */
static void
rfbEncryptBytes(char *bytes, const char *passwd)
{
char key[24];
void *des;
int len;
/* key is simply password padded with nulls */
g_memset(key, 0, sizeof(key));
len = MIN(g_strlen(passwd), 8);
g_mirror_memcpy(key, passwd, len);
des = ssl_des3_encrypt_info_create(key, 0);
ssl_des3_encrypt(des, 8, bytes, bytes);
ssl_des3_info_delete(des);
des = ssl_des3_encrypt_info_create(key, 0);
ssl_des3_encrypt(des, 8, bytes + 8, bytes + 8);
ssl_des3_info_delete(des);
}
/******************************************************************************/
/* sha1 hash 'passwd', create a string from the hash and call rfbEncryptBytes */
static void
rfbHashEncryptBytes(char *bytes, const char *passwd)
{
char passwd_hash[20];
char passwd_hash_text[40];
void *sha1;
int passwd_bytes;
/* create password hash from password */
passwd_bytes = g_strlen(passwd);
sha1 = ssl_sha1_info_create();
ssl_sha1_transform(sha1, "xrdp_vnc", 8);
ssl_sha1_transform(sha1, passwd, passwd_bytes);
ssl_sha1_transform(sha1, passwd, passwd_bytes);
ssl_sha1_complete(sha1, passwd_hash);
ssl_sha1_info_delete(sha1);
g_snprintf(passwd_hash_text, 39, "%2.2x%2.2x%2.2x%2.2x",
(tui8)passwd_hash[0], (tui8)passwd_hash[1],
(tui8)passwd_hash[2], (tui8)passwd_hash[3]);
passwd_hash_text[39] = 0;
rfbEncryptBytes(bytes, passwd_hash_text);
}
/******************************************************************************/
static int
lib_process_channel_data(struct vnc *v, int chanid, int flags, int size,
struct stream *s, int total_size)
{
int type;
int status;
int length;
int clip_bytes;
int index;
int format;
struct stream *out_s;
if (chanid == v->clip_chanid)
{
in_uint16_le(s, type);
in_uint16_le(s, status);
in_uint32_le(s, length);
LOG(LOG_LEVEL_DEBUG, "clip data type %d status %d length %d", type, status, length);
LOG_DEVEL_HEXDUMP(LOG_LEVEL_TRACE, "clipboard data", s->p, s->end - s->p);
switch (type)
{
case 2: /* CLIPRDR_FORMAT_ANNOUNCE */
LOG(LOG_LEVEL_DEBUG, "CLIPRDR_FORMAT_ANNOUNCE - "
"status %d length %d", status, length);
make_stream(out_s);
init_stream(out_s, 8192);
out_uint16_le(out_s, 3); // msg-type: CLIPRDR_FORMAT_ACK
out_uint16_le(out_s, 1); // msg-status-code: CLIPRDR_RESPONSE
out_uint32_le(out_s, 0); // null (?)
out_uint8s(out_s, 4); // pad
s_mark_end(out_s);
length = (int)(out_s->end - out_s->data);
v->server_send_to_channel(v, v->clip_chanid, out_s->data,
length, length, 3);
free_stream(out_s);
#if 0
// Send the CLIPRDR_DATA_REQUEST message to the cliprdr channel.
//
make_stream(out_s);
init_stream(out_s, 8192);
out_uint16_le(out_s, 4); // msg-type: CLIPRDR_DATA_REQUEST
out_uint16_le(out_s, 0); // msg-status-code: CLIPRDR_REQUEST
out_uint32_le(out_s, 4); // sizeof supported-format-list.
out_uint32_le(out_s, 1); // supported-format-list: CF_TEXT
out_uint8s(out_s, 0); // pad (garbage pad?)
s_mark_end(out_s);
length = (int)(out_s->end - out_s->data);
v->server_send_to_channel(v, v->clip_chanid, out_s->data,
length, length, 3);
free_stream(out_s);
#endif
break;
case 3: /* CLIPRDR_FORMAT_ACK */
LOG(LOG_LEVEL_DEBUG, "CLIPRDR_FORMAT_ACK - "
"status %d length %d", status, length);
break;
case 4: /* CLIPRDR_DATA_REQUEST */
LOG(LOG_LEVEL_DEBUG, "CLIPRDR_DATA_REQUEST - "
"status %d length %d", status, length);
format = 0;
if (length >= 4)
{
in_uint32_le(s, format);
}
/* only support CF_TEXT and CF_UNICODETEXT */
if ((format != 1) && (format != 13))
{
break;
}
make_stream(out_s);
init_stream(out_s, 8192);
out_uint16_le(out_s, 5);
out_uint16_le(out_s, 1);
if (format == 13) /* CF_UNICODETEXT */
{
out_uint32_le(out_s, v->clip_data_s->size * 2 + 2);
for (index = 0; index < v->clip_data_s->size; index++)
{
out_uint8(out_s, v->clip_data_s->data[index]);
out_uint8(out_s, 0);
}
out_uint8s(out_s, 2);
}
else if (format == 1) /* CF_TEXT */
{
out_uint32_le(out_s, v->clip_data_s->size + 1);
for (index = 0; index < v->clip_data_s->size; index++)
{
out_uint8(out_s, v->clip_data_s->data[index]);
}
out_uint8s(out_s, 1);
}
out_uint8s(out_s, 4); /* pad */
s_mark_end(out_s);
length = (int)(out_s->end - out_s->data);
v->server_send_to_channel(v, v->clip_chanid, out_s->data, length,
length, 3);
free_stream(out_s);
break;
case 5: /* CLIPRDR_DATA_RESPONSE */
LOG(LOG_LEVEL_DEBUG, "CLIPRDR_DATA_RESPONSE - "
"status %d length %d", status, length);
clip_bytes = MIN(length, 256);
// - Read the response data from the cliprdr channel, stream 's'.
// - Send the response data to the vnc server, stream 'out_s'.
//
make_stream(out_s);
// Send the RFB message type (CLIENT_CUT_TEXT) to the vnc server.
init_stream(out_s, clip_bytes + 1 + 3 + 4 + 16);
out_uint8(out_s, C2S_CLIENT_CUT_TEXT);
out_uint8s(out_s, 3); // padding
// Send the length of the cut-text to the vnc server.
out_uint32_be(out_s, clip_bytes);
// Send the cut-text (as read from 's') to the vnc server.
for (index = 0; index < clip_bytes; index++)
{
char cur_char = '\0';
in_uint8(s, cur_char); // text in from 's'
out_uint8(out_s, cur_char); // text out to 'out_s'
}
s_mark_end(out_s);
lib_send_copy(v, out_s);
free_stream(out_s);
break;
default:
{
LOG(LOG_LEVEL_DEBUG, "VNC clip information unhandled");
break;
}
}
}
else
{
LOG(LOG_LEVEL_DEBUG, "lib_process_channel_data: unknown chanid: "
"%d :(v->clip_chanid) %d", chanid, v->clip_chanid);
}
return 0;
}
/**************************************************************************//**
* Logs a debug message containing a screen layout
*
* @param lvl Level to log at
* @param source Where the layout came from
* @param layout Layout to log
*/
static void
log_screen_layout(const enum logLevels lvl, const char *source,
const struct vnc_screen_layout *layout)
{
unsigned int i;
char text[256];
size_t pos;
int res;
pos = 0;
res = g_snprintf(text, sizeof(text) - pos,
"Layout from %s (geom=%dx%d #screens=%u) :",
source, layout->total_width, layout->total_height,
layout->count);
i = 0;
while (res > 0 && (size_t)res < sizeof(text) - pos && i < layout->count)
{
pos += res;
res = g_snprintf(&text[pos], sizeof(text) - pos,
" %d:(%dx%d+%d+%d)",
layout->s[i].id,
layout->s[i].width, layout->s[i].height,
layout->s[i].x, layout->s[i].y);
++i;
}
LOG(lvl, "%s", text);
}
/**************************************************************************//**
* Compares two vnc_screen structures
*
* @param a First structure
* @param b Second structure
*
* @return Suitable for sorting structures with ID as the primary key
*/
static int cmp_vnc_screen(const struct vnc_screen *a,
const struct vnc_screen *b)
{
int result = 0;
if (a->id != b->id)
{
result = a->id - b->id;
}
else if (a->x != b->x)
{
result = a->x - b->x;
}
else if (a->y != b->y)
{
result = a->y - b->y;
}
else if (a->width != b->width)
{
result = a->width - b->width;
}
else if (a->height != b->height)
{
result = a->height - b->height;
}
return result;
}
/**************************************************************************//**
* Compares two vnc_screen_layout structures for equality
* @param a First layout
* @param b First layout
* @return != 0 if structures are equal
*/
static int vnc_screen_layouts_equal(const struct vnc_screen_layout *a,
const struct vnc_screen_layout *b)
{
unsigned int i;
int result = (a->total_width == b->total_width &&
a->total_height == b->total_height &&
a->count == b->count);
if (result)
{
for (i = 0 ; result && i < a->count ; ++i)
{
result = (cmp_vnc_screen(&a->s[i], &b->s[i]) == 0);
}
}
return result;
}
/**************************************************************************//**
* Reads an extended desktop size rectangle from the VNC server
*
* @param v VNC object
* @param [out] layout Desired layout for server
* @return != 0 for error
*
* @pre The next octet read from v->trans is the number of screens
* @pre layout is not already allocated
*
* @post if call is successful, layout->s must be freed after use.
* @post Returned structure is in increasing ID order
* @post layout->total_width is untouched
* @post layout->total_height is untouched
*/
static int
read_extended_desktop_size_rect(struct vnc *v,
struct vnc_screen_layout *layout)
{
struct stream *s;
int error;
unsigned int count;
struct vnc_screen *screens;
layout->count = 0;
layout->s = NULL;
make_stream(s);
init_stream(s, 8192);
/* Read in the current screen config */
error = trans_force_read_s(v->trans, s, 4);
if (error == 0)
{
/* Get the number of screens */
in_uint8(s, count);
in_uint8s(s, 3);
error = trans_force_read_s(v->trans, s, 16 * count);
if (error == 0)
{
screens = g_new(struct vnc_screen, count);
if (screens == NULL)
{
LOG(LOG_LEVEL_ERROR,
"VNC : Can't alloc for %d screens", count);
error = 1;
}
else
{
unsigned int i;
for (i = 0 ; i < count ; ++i)
{
in_uint32_be(s, screens[i].id);
in_uint16_be(s, screens[i].x);
in_uint16_be(s, screens[i].y);
in_uint16_be(s, screens[i].width);
in_uint16_be(s, screens[i].height);
in_uint32_be(s, screens[i].flags);
}
/* sort monitors in increasing ID order */
qsort(screens, count, sizeof(screens[0]),
(int (*)(const void *, const void *))cmp_vnc_screen);
}
}
}
free_stream(s);
if (error == 0)
{
layout->count = count;
layout->s = screens;
}
return error;
}
/**************************************************************************//**
* Sends a SetDesktopSize message
*
* @param v VNC object
* @param layout Desired layout for server
* @return != 0 for error
*
* The SetDesktopSize message is documented in the RFB community wiki
* "SetDesktopSize" section.
*/
static int
send_set_desktop_size(struct vnc *v, const struct vnc_screen_layout *layout)
{
unsigned int i;
struct stream *s;
int error;
make_stream(s);
init_stream(s, 8192);
out_uint8(s, 251);
out_uint8(s, 0);
out_uint16_be(s, layout->total_width);
out_uint16_be(s, layout->total_height);
out_uint8(s, layout->count);
out_uint8(s, 0);
for (i = 0 ; i < layout->count ; ++i)
{
out_uint32_be(s, layout->s[i].id);
out_uint16_be(s, layout->s[i].x);
out_uint16_be(s, layout->s[i].y);
out_uint16_be(s, layout->s[i].width);
out_uint16_be(s, layout->s[i].height);
out_uint32_be(s, layout->s[i].flags);
}
s_mark_end(s);
LOG(LOG_LEVEL_DEBUG, "VNC Sending SetDesktopSize");
error = lib_send_copy(v, s);
free_stream(s);
return error;
}
/**************************************************************************//**
* Sets up a single-screen vnc_screen_layout structure
*
* @param layout Structure to set up
* @param width New client width
* @param height New client height
*
* @pre layout->count must be valid
* @pre layout->s must be valid
*/
static void
set_single_screen_layout(struct vnc_screen_layout *layout,
int width, int height)
{
int id = 0;
int flags = 0;
layout->total_width = width;
layout->total_height = height;
if (layout->count == 0)
{
/* No previous layout */
layout->s = g_new(struct vnc_screen, 1);
}
else
{
/* Keep the ID and flags from the previous first screen */
id = layout->s[0].id;
flags = layout->s[0].flags;
if (layout->count > 1)
{
g_free(layout->s);
layout->s = g_new(struct vnc_screen, 1);
}
}
layout->count = 1;
layout->s[0].id = id;
layout->s[0].x = 0;
layout->s[0].y = 0;
layout->s[0].width = width;
layout->s[0].height = height;
layout->s[0].flags = flags;
}
/**************************************************************************//**
* Resize the client as a single screen
*
* @param v VNC object
* @param update_in_progress True if there's a painter update in progress
* @param width New client width
* @param height New client height
* @return != 0 for error
*
* The new client layout is recorded in v->client_layout. If the client was
* multi-screen before this call, it won't be afterwards.
*/
static int
resize_client(struct vnc *v, int update_in_progress, int width, int height)
{
int error = 0;
if (v->client_layout.count != 1 ||
v->client_layout.total_width != width ||
v->client_layout.total_height != height)
{
if (update_in_progress)
{
error = v->server_end_update(v);
}
if (error == 0)
{
error = v->server_reset(v, width, height, v->server_bpp);
if (error == 0)
{
set_single_screen_layout(&v->client_layout, width, height);
if (update_in_progress)
{
error = v->server_begin_update(v);
}
}
}
}
return error;
}
/**************************************************************************//**
* Resize the attached client from a layout
*
* @param v VNC object
* @param update_in_progress True if there's a painter update in progress
* @param layout Desired layout from server
* @return != 0 for error
*
* This has some limitations. We have no way to move multiple screens about
* on a connected client, and so we are not able to change the client unless
* we're changing to a single screeen layout.
*/
static int
resize_client_from_layout(struct vnc *v,
int update_in_progress,
const struct vnc_screen_layout *layout)
{
int error = 0;
if (!vnc_screen_layouts_equal(&v->client_layout, layout))
{
/*
* we don't have the capability to resize to anything other
* than a single screen.
*/
if (layout->count != 1)
{
LOG(LOG_LEVEL_ERROR,
"VNC Resize to %d screen(s) from %d screen(s) "
"not implemented",
v->client_layout.count, layout->count);
/* Dump some useful info, in case we get here when we don't
* need to */
log_screen_layout(LOG_LEVEL_ERROR, "OldLayout", &v->client_layout);
log_screen_layout(LOG_LEVEL_ERROR, "NewLayout", layout);
error = 1;
}
else
{
error = resize_client(v,
update_in_progress,
layout->total_width,
layout->total_height);
}
}
return error;
}
static int sendVncKey(struct vnc *v, uint32_t vncKeyCode, int pressed)
{
struct stream *s;
int error;
LOG(LOG_LEVEL_DEBUG, "send vnc key 0x%04x %d\n", vncKeyCode, pressed);
make_stream(s);
init_stream(s, 8192);
out_uint8(s, C2S_KEY_EVENT);
out_uint8(s, pressed?1:0); /* down flag */
out_uint8s(s, 2);
out_uint32_be(s, vncKeyCode);
s_mark_end(s);
error = lib_send_copy(v, s);
return error;
}
/* translate vncKey into X11 key sym to send to vnc server */
/* pre: vncKey->attrs & X11_VNC_KEY_VALID */
static uint32_t translateVncKeyToX11KeySym(
const struct x11vnckey* const vncKey,
const int shiftIsDown,
const int capsLocked,
const int numLocked)
{
const int capsLockable=keyIsCapsLockable(vncKey);
const int numLockable=keyIsNumLockable(vncKey);
LOG(LOG_LEVEL_DEBUG, "translate vnc key capslockable %d, numlockable %d, shift %d, capslock %d, numlock %d\n", capsLockable, numLockable, shiftIsDown, capsLocked, numLocked);
if (capsLockable){
return ((shiftIsDown && !capsLocked) || (!shiftIsDown && capsLocked))?
vncKey->shiftedVncKeyCode:
vncKey->vncKeyCode;
}
if (numLockable){
return ((shiftIsDown && !numLocked) || (!shiftIsDown && numLocked))?
vncKey->shiftedVncKeyCode:
vncKey->vncKeyCode;
}
return shiftIsDown?
vncKey->shiftedVncKeyCode:
vncKey->vncKeyCode;
}
/* pre: vncKey->attrs & X11_VNC_KEY_VALID */
/* returns 0 for success */
static int handleVncKeyPress(struct x11vnc* const v,
struct x11vnckey* const vncKey)
{
const struct x11vnckey* keys=v->keys;
const int shiftIsDown =
(keys[42].attrs & X11_VNC_KEY_IS_DOWN) ||
(keys[54].attrs & X11_VNC_KEY_IS_DOWN);
uint32_t x11keySym=translateVncKeyToX11KeySym(
vncKey,shiftIsDown,v->capsLocked,v->numLocked);
int status=0;
LOG(LOG_LEVEL_DEBUG, "handle vnc key 0x%04x press (shift %d, caps lock %d, num lock %d)\n", x11keySym, shiftIsDown, v->capsLocked, v->numLocked);
if (keyAutoRepeats(vncKey))
{
// rdp sends repeated key-down with no intervening key-up for
// auto-repeat, so for keys we want to auto-repeat, we ignore
// rdp key-up and instead generate a down-up pair for each rdp key-down
// this way auto-repeat does not depend on network latency
status=sendVncKey(&v->_, x11keySym, X11_VNC_KEY_PRESSED);
if (status == 0){
status=sendVncKey(&v->_, x11keySym, X11_VNC_KEY_RELEASED);
}
}
else
{
// for non-autorepeat keys, ignore rdp's repeated key-downs with
// no intervening key-up
if (!keyIsDown(vncKey))
{
status = sendVncKey(&v->_, x11keySym, X11_VNC_KEY_PRESSED);
if (status == 0){
setKeyDown(vncKey);
}
}
}
return status;
}
/* pre: vncKey->attrs & X11_VNC_KEY_VALID */
/* returns 0 for success */
static int handleVncKeyRelease(struct x11vnc* const v,
struct x11vnckey* const vncKey)
{
const struct x11vnckey* keys=v->keys;
const int shiftIsDown =
(keys[42].attrs & X11_VNC_KEY_IS_DOWN) ||
(keys[54].attrs & X11_VNC_KEY_IS_DOWN);
uint32_t x11keySym=translateVncKeyToX11KeySym(
vncKey,shiftIsDown,v->capsLocked,v->numLocked);
int status=0;
LOG(LOG_LEVEL_DEBUG, "handle vnc key 0x%04x release (shift %d, caps lock %d, num lock %d\n", x11keySym, shiftIsDown, v->capsLocked, v->numLocked);
if (keyIsCapsLock(vncKey))
{
v->capsLocked=!v->capsLocked;
}
if (keyIsNumLock(vncKey))
{
v->numLocked=!v->numLocked;
}
if (keyAutoRepeats(vncKey))
{
// rdp sends repeated key-down with no intervening key-up for
// auto-repeat, so for keys we want to auto-repeat, we ignore
// rdp key-up and instead generate a down-up pair for each rdp key-down
// this way auto-repeat does not depend on network latency
}
else
{
// for non-autorepeat keys, ignore rdp's repeated key-downs with
// no intervening key-up
if (keyIsDown(vncKey))
{
status = sendVncKey(&v->_, x11keySym, X11_VNC_KEY_RELEASED);
if (status == 0){
setKeyUp(vncKey);
}
}
}
return status;
}
int lib_mod_handle_key(struct vnc *v_, int rdpKeyCode, int rdpKeyEvent)
{
struct x11vnc* const v=(struct x11vnc*)v_;
int status=0;
const X11VncKeyDirection direction = (rdpKeyEvent & 0x8000 ?
X11_VNC_KEY_RELEASED :
X11_VNC_KEY_PRESSED);
LOG(LOG_LEVEL_DEBUG, "handle rdp key %d event %d\n", rdpKeyCode, rdpKeyEvent);
if (rdpKeyCode >= 256)
{
LOG(LOG_LEVEL_DEBUG, "rdp key code %d (>= 256) is invalid for xrdp x11vnc module", rdpKeyCode);
return 0;
}
if (rdpKeyCode < 0)
{
LOG(LOG_LEVEL_DEBUG, "rdp key code %d (< 0) is invalid for xrdp x11vnc module", rdpKeyCode);
return 0;
}
const int index=(rdpKeyEvent&0x100 ? 256:0)+rdpKeyCode;
if (! (v->keys[index].attrs & X11_VNC_KEY_VALID))
{
LOG(LOG_LEVEL_DEBUG, "rdp key code %d event %d is not mapped by xrdp x11vnc module", rdpKeyCode, rdpKeyEvent);
return 0;
}
if (direction==X11_VNC_KEY_PRESSED){
status = handleVncKeyPress(v, &v->keys[index]);
}
else{
status = handleVncKeyRelease(v, &v->keys[index]);
}
return status;
}
/*****************************************************************************/
int
lib_mod_event(struct vnc *v, int msg, long param1, long param2,
long param3, long param4)
{
struct stream *s;
int error;
int x;
int y;
int cx;
int cy;
int size;
int total_size;
int chanid;
int flags;
char *data;
error = 0;
make_stream(s);
if (msg == 0x5555) /* channel data */
{
chanid = LOWORD(param1);
flags = HIWORD(param1);
size = (int)param2;
data = (char *)param3;
total_size = (int)param4;
if ((size >= 0) && (size <= (32 * 1024)) && (data != 0))
{
init_stream(s, size);
out_uint8a(s, data, size);
s_mark_end(s);
s->p = s->data;
error = lib_process_channel_data(v, chanid, flags, size, s, total_size);
}
else
{
error = 1;
}
}
else if ((msg >= 15) && (msg <= 16)) /* key events */
{
// see lib_mod_handle_key
}
else if (msg >= 100 && msg <= 110) /* mouse events */
{
switch (msg)
{
case 100:
break; /* WM_MOUSEMOVE */
case 101:
v->mod_mouse_state &= ~1;
break; /* WM_LBUTTONUP */
case 102:
v->mod_mouse_state |= 1;
break; /* WM_LBUTTONDOWN */
case 103:
v->mod_mouse_state &= ~4;
break; /* WM_RBUTTONUP */
case 104:
v->mod_mouse_state |= 4;
break; /* WM_RBUTTONDOWN */
case 105:
v->mod_mouse_state &= ~2;
break;
case 106:
v->mod_mouse_state |= 2;
break;
case 107:
v->mod_mouse_state &= ~8;
break;
case 108:
v->mod_mouse_state |= 8;
break;
case 109:
v->mod_mouse_state &= ~16;
break;
case 110:
v->mod_mouse_state |= 16;
break;
}
init_stream(s, 8192);
out_uint8(s, C2S_POINTER_EVENT);
out_uint8(s, v->mod_mouse_state);
out_uint16_be(s, param1);
out_uint16_be(s, param2);
s_mark_end(s);
error = lib_send_copy(v, s);
}
else if (msg == 200) /* invalidate */
{
if (v->suppress_output == 0)
{
init_stream(s, 8192);
out_uint8(s, C2S_FRAMEBUFFER_UPDATE_REQUEST);
out_uint8(s, 0); /* incremental == 0 : Full contents */
x = (param1 >> 16) & 0xffff;
out_uint16_be(s, x);
y = param1 & 0xffff;