aboutsummaryrefslogtreecommitdiff
path: root/SDL2_vnc.c
blob: 6afedfcf8ae60225620ff24bfd142b8345840025 (plain) (blame)
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
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>

#include <SDL2/SDL.h>

#include "keysymdef.h"

#include "SDL2_vnc.h"

#define VNC_INITIAL_BUFSIZE 64;

typedef unsigned int uint;

typedef enum {
    RFB_33 = 3,
    RFB_37 = 7,
    RFB_38 = 8,
    RFB_OTHER
} RFB_version;

#define RFB_33_STR "RFB 003.003\n"
#define RFB_37_STR "RFB 003.007\n"
#define RFB_38_STR "RFB 003.008\n"

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define warn(...) fprintf(stderr, __VA_ARGS__)

uint16_t swap_endianness_u16b(uint16_t x) {
    uint16_t a = x;
    uint16_t b = 0;

    uint8_t *pa = (uint8_t *) &a;
    uint8_t *pb = (uint8_t *) &b;

    pb[0] = pa[1];
    pb[1] = pa[0];

    return b;
}

uint32_t swap_endianness_u32b(uint32_t x) {
    uint32_t a = x;
    uint32_t b = 0;

    uint8_t *pa = (uint8_t *) &a;
    uint8_t *pb = (uint8_t *) &b;

    pb[0] = pa[3];
    pb[1] = pa[2];
    pb[2] = pa[1];
    pb[3] = pa[0];

    return b;
}

int32_t swap_endianness_32b(int32_t x) {
    int32_t a = x;
    int32_t b = 0;

    int8_t *pa = (int8_t *) &a;
    int8_t *pb = (int8_t *) &b;

    pb[0] = pa[3];
    pb[1] = pa[2];
    pb[2] = pa[1];
    pb[3] = pa[0];

    return b;
}

int init_vnc_buffer(vnc_buf *buffer) {
    buffer->size = VNC_INITIAL_BUFSIZE;
    buffer->data = malloc(buffer->size);

    return buffer->data == NULL;
}

int create_socket() {
    int sock = socket(AF_INET, SOCK_STREAM, 0);

    struct timeval t;
    t.tv_sec = 0;
    t.tv_usec = 0;
    setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(t));

    return sock;
}

int vnc_connect(SDL_vnc *vnc, char *host, uint port) {
    struct sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_port = htons(port);
    inet_pton(AF_INET, host, &address.sin_addr);
    return connect(vnc->socket, (struct sockaddr *) &address, sizeof(address));
}

int resize_vnc_buffer(vnc_buf buffer, size_t n) {
    buffer.data = realloc(buffer.data, n);

    return !buffer.data;
}

int assure_buffer_size(vnc_buf buffer, size_t n) {
    if (n > buffer.size) {
        return resize_vnc_buffer(buffer, n);
    }

    return 0;
}

int from_server(int socket, void *buffer, size_t n, bool spin) {

    size_t left_to_read = n;
    void *needle = buffer;

    while (left_to_read > 0) {
        ssize_t bytes_read = recv(socket, needle, left_to_read, 0);

        if (bytes_read < 0) {
            return -1;
        }

        if (!spin && bytes_read == 0) {
            return n - left_to_read;
        }

        left_to_read -= bytes_read;
        needle += bytes_read;
    }

    return n;
}

int server_to_vnc_buffer(SDL_vnc *vnc, size_t n, bool spin) {
    assure_buffer_size(vnc->buffer, n);
    return from_server(vnc->socket, vnc->buffer.data, n, spin);
}

int assure_scratch_buffer_size(SDL_vnc *vnc, size_t w, size_t h) {
    vnc_server_details details = vnc->server_details;

    if (vnc->scratch_buffer && vnc->scratch_buffer->w == w &&
            vnc->scratch_buffer->h == h) {
        return 0;
    }

    if (vnc->scratch_buffer) {
        SDL_FreeSurface(vnc->scratch_buffer);
    }

    vnc->scratch_buffer = SDL_CreateRGBSurface(0, w, h, details.fmt.bpp,
            details.fmt.red_max << details.fmt.red_shift,
            details.fmt.green_max << details.fmt.green_shift,
            details.fmt.blue_max << details.fmt.blue_shift,
            0);
    SDL_SetSurfaceBlendMode(vnc->scratch_buffer, SDL_BLENDMODE_NONE);

    return 0;
}

int server_to_scratch_buffer(SDL_vnc *vnc, size_t w, size_t h, bool spin) {
    assure_scratch_buffer_size(vnc, w, h);

    size_t pixel_data_size = w * h * vnc->server_details.fmt.bpp / 8;

    if (SDL_MUSTLOCK(vnc->scratch_buffer)) {
        SDL_LockSurface(vnc->scratch_buffer);
    }

    from_server(vnc->socket, vnc->scratch_buffer->pixels, pixel_data_size,
            spin);

    if (SDL_MUSTLOCK(vnc->scratch_buffer)) {
        SDL_UnlockSurface(vnc->scratch_buffer);
    }

    return 0;
}

int to_server(int socket, void *data, size_t n) {
    return send(socket, data, n, 0);
}

RFB_version deduce_version(char *str) {
    if (!strncmp(str, RFB_33_STR, 12)) {
        return RFB_33;

    } else if (!strncmp(str, RFB_37_STR, 12)) {
        return RFB_37;

    } else if (!strncmp(str, RFB_38_STR, 12)) {
        return RFB_38;

    } else {
        return RFB_OTHER;
    }
}

char *rfb_ver_to_str(RFB_version ver) {
    switch (ver) {
        case RFB_33:
            return RFB_33_STR;

        case RFB_37:
            return RFB_37_STR;

        case RFB_38:
            return RFB_38_STR;

        default: // including RFB_OTHER
            return RFB_33_STR;
    }
}

RFB_version get_server_version(SDL_vnc *vnc) {
    char protocol_string[12];
    from_server(vnc->socket, protocol_string, 12, true);

    RFB_version ver = deduce_version(protocol_string);

    return ver;
}

int send_client_version(SDL_vnc *vnc, RFB_version ver) {
    char *ver_str = rfb_ver_to_str(ver);
    return to_server(vnc->socket, ver_str, 12);
}

typedef enum {
    RFB_SECURITY_INVALID = 0,
    RFB_SECURITY_NONE = 1,
    RFB_SECURITY_VNC_AUTH = 2
} RFB_security_protocol;

bool look_for_no_auth(uint8_t *options, size_t n) {
    for (uint i = 0; i < n; i++) {
        if (options[i] == RFB_SECURITY_NONE) {
            return true;
        }
    }

    return false;
}

int negotiate_security_33(SDL_vnc *vnc) {
    return SDL_VNC_ERROR_UNIMPLEMENTED;
}

int negotiate_security_37(SDL_vnc *vnc) {
    return SDL_VNC_ERROR_UNIMPLEMENTED;
}

int negotiate_security_38(SDL_vnc *vnc) {
    uint8_t security_protocol_count;
    from_server(vnc->socket, &security_protocol_count, 1, true);

    if (!security_protocol_count) {
        return SDL_VNC_ERROR_SERVER_DISCONNECT;
    }

    server_to_vnc_buffer(vnc, security_protocol_count, true);

    bool no_security_supported =
        look_for_no_auth(vnc->buffer.data, security_protocol_count);

    if (!no_security_supported) {
        return SDL_VNC_ERROR_UNSUPPORTED_SECURITY_PROTOCOLS;
    }

    RFB_security_protocol no_sec = RFB_SECURITY_NONE;
    to_server(vnc->socket, &no_sec, 1);

    uint32_t security_handshake_error;
    from_server(vnc->socket, &security_handshake_error, 4, true);

    if (security_handshake_error) {
        return SDL_VNC_ERROR_SECURITY_HANDSHAKE_FAILED;
    }

    return OK;
}

int negotiate_security(SDL_vnc *vnc, RFB_version ver) {
    switch (ver) {
        case RFB_33:
            return negotiate_security_33(vnc);

        case RFB_37:
            return negotiate_security_37(vnc);

        case RFB_38:
            return negotiate_security_38(vnc);

        default:
            return negotiate_security_33(vnc);
    }
}

int client_initialisation(SDL_vnc *vnc) {
    uint8_t shared_flag = 0;
    to_server(vnc->socket, &shared_flag, 1);
    return 0;
}

int server_initialisation(SDL_vnc *vnc) {
    server_to_vnc_buffer(vnc, 24, true);

    uint16_t *framebuffer_info = (uint16_t *) vnc->buffer.data;
    vnc->server_details.w = swap_endianness_u16b(framebuffer_info[0]);
    vnc->server_details.h = swap_endianness_u16b(framebuffer_info[1]);

    uint8_t *server_pixel_info = (uint8_t *) &framebuffer_info[2];
    vnc->server_details.fmt.bpp = server_pixel_info[0];
    vnc->server_details.fmt.depth = server_pixel_info[1];
    vnc->server_details.fmt.is_big_endian = server_pixel_info[2];
    vnc->server_details.fmt.is_true_colour = server_pixel_info[3];

    // always in big endian order
    uint16_t *server_colour_maxima = (uint16_t *) &server_pixel_info[4];
    vnc->server_details.fmt.red_max = swap_endianness_u16b(server_colour_maxima[0]);
    vnc->server_details.fmt.green_max = swap_endianness_u16b(server_colour_maxima[1]);
    vnc->server_details.fmt.blue_max = swap_endianness_u16b(server_colour_maxima[2]);

    uint8_t *server_colour_shifts = (uint8_t *) &server_colour_maxima[3];
    vnc->server_details.fmt.red_shift = server_colour_shifts[0];
    vnc->server_details.fmt.green_shift = server_colour_shifts[1];
    vnc->server_details.fmt.blue_shift = server_colour_shifts[2];

    char *padding = (char *) &server_colour_shifts[3];
    padding += 3;

    uint32_t *server_name_info = (uint32_t *) padding;
    vnc->server_details.name_length = swap_endianness_u32b(server_name_info[0]);

    if (vnc->server_details.name_length) {

        vnc->server_details.name = malloc(vnc->server_details.name_length + 1);
        from_server(vnc->socket, vnc->server_details.name,
                vnc->server_details.name_length, true);
        vnc->server_details.name[vnc->server_details.name_length] = '\0';

    } else {
        vnc->server_details.name = NULL;
    }

    printf("server '%s':\n"
            "  pixel depth: %u (%u bpp)\n"
            "  true colour %s\n"
            "  %s pixel values\n"
            "  red:   %x << %2u\n"
            "  green: %x << %2u\n"
            "  blue:  %x << %2u\n",
            vnc->server_details.name ?  vnc->server_details.name : "UNNAMED",
            vnc->server_details.fmt.depth,
            vnc->server_details.fmt.bpp,
            vnc->server_details.fmt.is_true_colour ? "enabled" : "disabled",
            vnc->server_details.fmt.is_big_endian ? "big-endian" : "little-endian",
            vnc->server_details.fmt.red_max, vnc->server_details.fmt.red_shift,
            vnc->server_details.fmt.green_max, vnc->server_details.fmt.green_shift,
            vnc->server_details.fmt.blue_max, vnc->server_details.fmt.blue_shift);

    return 0;
}

SDL_Surface *create_surface(vnc_server_details *details) {
    return SDL_CreateRGBSurface(0, details->w, details->h, details->fmt.bpp,
            details->fmt.red_max << details->fmt.red_shift,
            details->fmt.green_max << details->fmt.green_shift,
            details->fmt.blue_max << details->fmt.blue_shift,
            0);
}

typedef enum {
    FRAME_BUFFER_UPDATE = 0,
    SET_COLOUR_MAP_ENTRIES = 1,
    BELL = 2,
    SERVER_CUT_TEXT = 3
} server_msg_t;

typedef enum {
    RAW = 0,
    COPY_RECT = 1,
    RRE = 2,
    HEXTILE = 5,
    TRLE = 15,
    ZRLE = 16,
    PSEUDO_CURSOR = -239,
    PSEUDO_DESKTOP_SIZE = -223
} RFB_encoding_type;

typedef struct {
    SDL_Rect r;
    RFB_encoding_type e;
} SDL_vnc_rect_header;

int set_encodings(SDL_vnc *vnc, RFB_encoding_type *encodings, uint n) {

    /*
     * Message size is:
     * - 2 bytes for message type;
     * - 1 byte padding;
     * - 2 bytes for number of encodings;
     * - 4-byte-wide encoding ID per encoding.
     */
    size_t msg_size = 4 + n * 4;

    assure_buffer_size(vnc->buffer, msg_size);

    uint8_t *msg = (uint8_t *) vnc->buffer.data;
    *msg++ = 2; // ID of SetEncoding message
    msg++;

    uint16_t *encoding_count = (uint16_t *) msg;
    *encoding_count++ = swap_endianness_u16b(n);

    int32_t *encoding_ids = (int32_t *) encoding_count;
    for (uint i = 0; i < n; i++) {
        *encoding_ids++ = swap_endianness_32b(encodings[i]);
    }

    return to_server(vnc->socket, vnc->buffer.data, msg_size);
}

int raw_from_server(SDL_vnc *vnc, SDL_vnc_rect_header *header) {
    size_t data_size =
        header->r.w * header->r.h * vnc->server_details.fmt.bpp / 8;

    server_to_scratch_buffer(vnc, header->r.w, header->r.h, true);

    return SDL_BlitSurface(vnc->scratch_buffer, NULL, vnc->surface, &header->r);
}

int copy_rect_from_server(SDL_vnc *vnc, SDL_vnc_rect_header *header) {
    server_to_vnc_buffer(vnc, 4, true);
    uint16_t *src_info = (uint16_t *) vnc->buffer.data;

    SDL_Rect src;
    src.x = swap_endianness_u16b(*src_info++);
    src.y = swap_endianness_u16b(*src_info++);
    src.w = header->r.w;
    src.h = header->r.h;

    return SDL_BlitSurface(vnc->surface, &src, vnc->surface, &header->r);
}

int rect_from_server(SDL_vnc *vnc, SDL_vnc_rect_header *header) {
    server_to_vnc_buffer(vnc, 12, true);

    uint16_t *rect_info = (uint16_t *) vnc->buffer.data;
    header->r.x = swap_endianness_u16b(*rect_info++);
    header->r.y = swap_endianness_u16b(*rect_info++);
    header->r.w = swap_endianness_u16b(*rect_info++);
    header->r.h = swap_endianness_u16b(*rect_info++);

    header->e = swap_endianness_32b(*((int32_t *) (rect_info)));

    switch (header->e) {
        case RAW:
            return raw_from_server(vnc, header);

        case COPY_RECT:
            return copy_rect_from_server(vnc, header);

        default:
            warn("unknown encoding method %i\n", header->e);
            return 0;
    }
}

int frame_buffer_update(SDL_vnc *vnc) {
    char buf[3];
    from_server(vnc->socket, buf, 3, true);
    uint16_t rect_count = swap_endianness_u16b(*((uint16_t *) (buf + 1)));

    debug("receiving framebuffer update of %u rectangles\n", rect_count);

    for (uint i = 0; i < rect_count; i++) {
        SDL_vnc_rect_header header;
        rect_from_server(vnc, &header);
    }
}

int framebuffer_update_request(int socket, bool incremental, uint16_t x,
        uint16_t y, uint16_t w, uint16_t h) {

    debug("sending framebuffer update request\n");

    char msg[10];

    uint8_t *msg_as_8b = (uint8_t *) msg;
    *msg_as_8b++ = 3;
    *msg_as_8b++ = incremental;

    uint16_t *msg_as_16b = (uint16_t *) msg_as_8b;
    *msg_as_16b++ = x;
    *msg_as_16b++ = y;
    *msg_as_16b++ = w;
    *msg_as_16b++ = h;

    return to_server(socket, msg, 10);
}

int resize_colour_map(vnc_colour_map *colour_map, size_t n) {
    colour_map->data = realloc(colour_map->data, n * sizeof (colour_map_entry));

    return !colour_map->data;
}

int assure_colour_map_size(vnc_colour_map *colour_map, size_t n) {
    if (colour_map->size < n) {
        return resize_colour_map(colour_map, n);
    }

    return 0;
}

int set_colour_map_entries(SDL_vnc *vnc) {
    server_to_vnc_buffer(vnc, 5, true);

    uint16_t *buf = (uint16_t *) (vnc->buffer.data + 1);
    uint16_t first_colour_index = *buf++;
    uint16_t number_of_colours = *buf++;
    uint16_t colour_index_end = first_colour_index + number_of_colours;

    debug("updating colours %u-%u in colour map\n", first_colour_index,
            colour_index_end - 1);

    assure_colour_map_size(&vnc->colour_map, colour_index_end);

    for (uint i = first_colour_index; i < colour_index_end; i++) {
        server_to_vnc_buffer(vnc, 6, true);

        uint16_t *colours = (uint16_t *) vnc->buffer.data;
        vnc->colour_map.data[i].r = *colours++;
        vnc->colour_map.data[i].g = *colours++;
        vnc->colour_map.data[i].b = *colours++;
    }

    return 0;
}

int update_loop(void *data) {
    SDL_vnc *vnc = data;

    while (vnc->thread) {
        server_msg_t msg;
        from_server(vnc->socket, &msg, 1, true);

        switch (msg) {
            case FRAME_BUFFER_UPDATE:
                frame_buffer_update(vnc);
                break;

            case SET_COLOUR_MAP_ENTRIES:
                set_colour_map_entries(vnc);
                break;

            //case BELL:
            //    //server_bell(vnc);
            //    break;

            //case SERVER_CUT_TEXT:
            //    //server_cut_text(vnc);
            //    break;

            default:
                printf("unknown msg code %u\n", msg);
                break;
        }

        framebuffer_update_request(vnc->socket, true, 0, 0,
                vnc->server_details.w, vnc->server_details.h);

        SDL_Delay(1000 / vnc->fps);
    }
}

SDL_Thread *create_update_thread(SDL_vnc *vnc) {
    return SDL_CreateThread(update_loop, "RFB Listener", vnc);
}

int handshake(SDL_vnc *vnc) {
    RFB_version server_version = get_server_version(vnc);
    RFB_version client_version =
        server_version == RFB_OTHER ? RFB_33 : server_version;

    send_client_version(vnc, client_version);

    negotiate_security(vnc, client_version);

    client_initialisation(vnc);
    server_initialisation(vnc);
}

int send_initial_framebuffer_update_request(SDL_vnc *vnc) {
    return framebuffer_update_request(vnc->socket, false, 0, 0,
            vnc->server_details.w, vnc->server_details.h);
}

SDL_vnc_result init_vnc_connection(SDL_vnc *vnc, char *host, uint port,
        uint fps) {

    int res;

    vnc->fps = fps;
    vnc->scratch_buffer = NULL;

    res = init_vnc_buffer(&vnc->buffer);
    if (res) {
        return SDL_VNC_ERROR_OOM;
    }

    vnc->socket = create_socket();
    if (vnc->socket <= 0) {
        return SDL_VNC_ERROR_COULD_NOT_CREATE_SOCKET;
    }

    res = vnc_connect(vnc, host, port);
    if (res) {
        return SDL_VNC_ERROR_COULD_NOT_CONNECT;
    }

    handshake(vnc);

    RFB_encoding_type encodings[] = {
        COPY_RECT, RAW
    };
    set_encodings(vnc, encodings, 2);
    send_initial_framebuffer_update_request(vnc);

    vnc->surface = create_surface(&vnc->server_details);
    vnc->thread = create_update_thread(vnc);

    return OK;
}

int wait_on_vnc_connection(SDL_vnc *vnc) {
    SDL_WaitThread(vnc->thread, NULL);
    return 0;
}

uint32_t translate_key(SDL_KeyCode key, bool shift) {
    switch (key) {
        case SDLK_0: return XK_0;
        case SDLK_1: return XK_1;
        case SDLK_2: return XK_2;
        case SDLK_3: return XK_3;
        case SDLK_4: return XK_4;
        case SDLK_5: return XK_5;
        case SDLK_6: return XK_6;
        case SDLK_7: return XK_7;
        case SDLK_8: return XK_8;
        case SDLK_9: return XK_9;

        case SDLK_F1: return XK_F1;
        case SDLK_F2: return XK_F2;
        case SDLK_F3: return XK_F3;
        case SDLK_F4: return XK_F4;
        case SDLK_F5: return XK_F5;
        case SDLK_F6: return XK_F6;
        case SDLK_F7: return XK_F7;
        case SDLK_F8: return XK_F8;
        case SDLK_F9: return XK_F9;
        case SDLK_F10: return XK_F10;
        case SDLK_F11: return XK_F11;
        case SDLK_F12: return XK_F12;
        case SDLK_F13: return XK_F13;
        case SDLK_F14: return XK_F14;
        case SDLK_F15: return XK_F15;
        case SDLK_F16: return XK_F16;
        case SDLK_F17: return XK_F17;
        case SDLK_F18: return XK_F18;
        case SDLK_F19: return XK_F19;
        case SDLK_F20: return XK_F20;
        case SDLK_F21: return XK_F21;
        case SDLK_F22: return XK_F22;
        case SDLK_F23: return XK_F23;
        case SDLK_F24: return XK_F24;

        case SDLK_a: return shift ? XK_A : XK_a;
        case SDLK_b: return shift ? XK_B : XK_b;
        case SDLK_c: return shift ? XK_C : XK_c;
        case SDLK_d: return shift ? XK_D : XK_d;
        case SDLK_e: return shift ? XK_E : XK_e;
        case SDLK_f: return shift ? XK_F : XK_f;
        case SDLK_g: return shift ? XK_G : XK_g;
        case SDLK_h: return shift ? XK_H : XK_h;
        case SDLK_i: return shift ? XK_I : XK_i;
        case SDLK_j: return shift ? XK_J : XK_j;
        case SDLK_k: return shift ? XK_K : XK_k;
        case SDLK_l: return shift ? XK_L : XK_l;
        case SDLK_m: return shift ? XK_M : XK_m;
        case SDLK_n: return shift ? XK_N : XK_n;
        case SDLK_o: return shift ? XK_O : XK_o;
        case SDLK_p: return shift ? XK_P : XK_p;
        case SDLK_q: return shift ? XK_Q : XK_q;
        case SDLK_r: return shift ? XK_R : XK_r;
        case SDLK_s: return shift ? XK_S : XK_s;
        case SDLK_t: return shift ? XK_T : XK_t;
        case SDLK_u: return shift ? XK_U : XK_u;
        case SDLK_v: return shift ? XK_V : XK_v;
        case SDLK_w: return shift ? XK_W : XK_w;
        case SDLK_x: return shift ? XK_X : XK_x;
        case SDLK_y: return shift ? XK_Y : XK_y;
        case SDLK_z: return shift ? XK_Z : XK_z;

        case SDLK_SPACE: return XK_space;
        case SDLK_QUOTE: return XK_apostrophe;
        case SDLK_BACKSLASH: return XK_backslash;
        case SDLK_COMMA: return XK_comma;
        case SDLK_AMPERSAND: return XK_ampersand;
        case SDLK_ASTERISK: return XK_asterisk;
        case SDLK_AT: return XK_at;
        case SDLK_CARET: return XK_asciicircum;
        case SDLK_COLON: return XK_colon;
        case SDLK_DOLLAR: return XK_dollar;
        case SDLK_EXCLAIM: return XK_exclam;
        case SDLK_GREATER: return XK_greater;
        case SDLK_HASH: return XK_numbersign;
        case SDLK_LEFTPAREN: return XK_parenleft;
        case SDLK_LESS: return XK_less;
        case SDLK_PERCENT: return XK_percent;
        case SDLK_PLUS: return XK_plus;
        case SDLK_QUESTION: return XK_question;
        case SDLK_QUOTEDBL: return XK_quotedbl;
        case SDLK_RIGHTPAREN: return XK_parenright;
        case SDLK_UNDERSCORE: return XK_underscore;
        case SDLK_EQUALS: return XK_equal;
        case SDLK_BACKQUOTE: return XK_grave;
        case SDLK_LEFTBRACKET: return XK_bracketleft;
        case SDLK_RIGHTBRACKET: return XK_bracketright;
        case SDLK_PERIOD: return XK_period;
        case SDLK_SEMICOLON: return XK_semicolon;
        case SDLK_SLASH: return XK_slash;

        case SDLK_BACKSPACE: return XK_BackSpace;
        case SDLK_CAPSLOCK: return XK_Caps_Lock;
        case SDLK_NUMLOCKCLEAR: return XK_Num_Lock;
        case SDLK_SCROLLLOCK: return XK_Scroll_Lock;
        case SDLK_DELETE: return XK_Delete;
        case SDLK_INSERT: return XK_Insert;
        case SDLK_ESCAPE: return XK_Escape;
        case SDLK_EXECUTE: return XK_Execute;
        case SDLK_FIND: return XK_Find;
        case SDLK_HELP: return XK_Help;
        case SDLK_AGAIN: return XK_Redo;
        case SDLK_UNDO: return XK_Undo;
        case SDLK_MENU: return XK_Menu;
        case SDLK_RETURN: return XK_Return;
        case SDLK_SEPARATOR: return XK_KP_Separator;
        case SDLK_STOP: return XK_Cancel;
        case SDLK_SYSREQ: return XK_Sys_Req;
        case SDLK_TAB: return XK_Tab;

        case SDLK_LALT: return XK_Alt_L;
        case SDLK_RALT: return XK_Alt_R;
        case SDLK_LCTRL: return XK_Control_L;
        case SDLK_RCTRL: return XK_Control_R;
        case SDLK_LGUI: return XK_Meta_L;
        case SDLK_RGUI: return XK_Meta_R;
        case SDLK_LSHIFT: return XK_Shift_L;
        case SDLK_RSHIFT: return XK_Shift_R;

        case SDLK_KP_0: return XK_KP_0;
        case SDLK_KP_1: return XK_KP_1;
        case SDLK_KP_2: return XK_KP_2;
        case SDLK_KP_3: return XK_KP_3;
        case SDLK_KP_4: return XK_KP_4;
        case SDLK_KP_5: return XK_KP_5;
        case SDLK_KP_6: return XK_KP_6;
        case SDLK_KP_7: return XK_KP_7;
        case SDLK_KP_8: return XK_KP_8;
        case SDLK_KP_9: return XK_KP_9;

        case SDLK_KP_A: return XK_a;
        case SDLK_KP_B: return XK_b;
        case SDLK_KP_C: return XK_c;
        case SDLK_KP_D: return XK_d;
        case SDLK_KP_E: return XK_e;
        case SDLK_KP_F: return XK_f;

        case SDLK_KP_AMPERSAND: return XK_ampersand;
        case SDLK_KP_AT: return XK_at;
        case SDLK_KP_COLON: return XK_colon;
        case SDLK_KP_COMMA: return XK_comma;
        case SDLK_KP_EXCLAM: return XK_exclam;
        case SDLK_KP_GREATER: return XK_greater;
        case SDLK_KP_LESS: return XK_less;
        case SDLK_KP_HASH: return XK_numbersign;
        case SDLK_KP_LEFTBRACE: return XK_braceleft;
        case SDLK_KP_RIGHTBRACE: return XK_braceright;
        case SDLK_KP_LEFTPAREN: return XK_parenleft;
        case SDLK_KP_RIGHTPAREN: return XK_parenright;
        case SDLK_KP_PERCENT: return XK_percent;
        case SDLK_KP_PERIOD: return XK_period;
        case SDLK_KP_PLUSMINUS: return XK_plusminus;
        case SDLK_KP_POWER: return XK_asciicircum;
        case SDLK_KP_VERTICALBAR: return XK_bar;

        case SDLK_KP_MINUS: return XK_KP_Subtract;
        case SDLK_KP_MULTIPLY: return XK_KP_Multiply;
        case SDLK_KP_DECIMAL: return XK_KP_Decimal;
        case SDLK_KP_DIVIDE: return XK_KP_Divide;
        case SDLK_KP_EQUALS: return XK_KP_Equal;
        case SDLK_KP_PLUS: return XK_KP_Add;

        case SDLK_KP_SPACE: return XK_KP_Space;
        case SDLK_KP_TAB: return XK_KP_Tab;
        case SDLK_KP_BACKSPACE: return XK_BackSpace;
        case SDLK_KP_CLEAR: return XK_Clear;
        case SDLK_KP_CLEARENTRY: return XK_Clear;
        case SDLK_KP_ENTER: return XK_KP_Enter;

        case SDLK_CURRENCYUNIT: return XK_currency;
        case SDLK_DECIMALSEPARATOR: return XK_decimalpoint;

        case SDLK_SELECT: return XK_Select;
        case SDLK_CRSEL: return XK_3270_CursorSelect;
        case SDLK_EXSEL: return XK_3270_ExSelect;
        case SDLK_PRINTSCREEN: return XK_3270_PrintScreen;

        case SDLK_UP: return XK_Up;
        case SDLK_DOWN: return XK_Down;

        case SDLK_HOME: return XK_Home;
        case SDLK_END: return XK_End;
        case SDLK_PAGEDOWN: return XK_Page_Down;
        case SDLK_PAGEUP: return XK_Page_Up;
        case SDLK_PRIOR: return XK_Prior;

        case SDLK_COPY: return XK_3270_Copy;
        case SDLK_PAUSE: return XK_Pause;

        case SDLK_CANCEL: return XK_Cancel;
        case SDLK_CLEAR: return XK_Clear;

        case SDLK_ALTERASE: return XK_3270_EraseInput;

        case SDLK_UNKNOWN:

        case SDLK_EJECT:

        case SDLK_OPER:
        case SDLK_OUT:

        case SDLK_CURRENCYSUBUNIT:
        case SDLK_KP_00:
        case SDLK_KP_000:
        case SDLK_KP_BINARY:
        case SDLK_KP_HEXADECIMAL:
        case SDLK_KP_OCTAL:
        case SDLK_KP_DBLAMPERSAND:
        case SDLK_KP_EQUALSAS400:
        case SDLK_KP_XOR:

        case SDLK_THOUSANDSSEPARATOR:

        case SDLK_KP_MEMADD:
        case SDLK_KP_MEMCLEAR:
        case SDLK_KP_MEMDIVIDE:
        case SDLK_KP_MEMMULTIPLY:
        case SDLK_KP_MEMRECALL:
        case SDLK_KP_MEMSTORE:
        case SDLK_KP_MEMSUBTRACT:

        case SDLK_CLEARAGAIN:
        case SDLK_CUT:

        case SDLK_COMPUTER:
        case SDLK_CALCULATOR:
        case SDLK_DISPLAYSWITCH:
        case SDLK_MAIL: 
        case SDLK_MEDIASELECT:
        case SDLK_WWW:

        case SDLK_POWER:
        case SDLK_SLEEP:

        case SDLK_AUDIOMUTE:
        case SDLK_AUDIONEXT:
        case SDLK_AUDIOPLAY:
        case SDLK_AUDIOPREV:
        case SDLK_AUDIOSTOP:

        case SDLK_AC_BACK:
        case SDLK_AC_BOOKMARKS:
        case SDLK_AC_FORWARD:
        case SDLK_AC_SEARCH:
        case SDLK_AC_REFRESH:
        case SDLK_AC_STOP:
        case SDLK_AC_HOME:

        case SDLK_BRIGHTNESSUP:
        case SDLK_BRIGHTNESSDOWN:
        case SDLK_VOLUMEUP:
        case SDLK_VOLUMEDOWN:

        case SDLK_KBDILLUMDOWN:
        case SDLK_KBDILLUMTOGGLE:
        case SDLK_KBDILLUMUP:

        case SDLK_APPLICATION:
        default:
            return XK_VoidSymbol;
    }
}

int pointer_event(SDL_vnc *vnc, uint32_t buttons, uint16_t x, uint16_t y,
        int32_t mw_x, int32_t mw_y) {

    uint8_t button_mask = 0;
    button_mask |= (buttons & SDL_BUTTON_LMASK) ? (1 << 0) : 0; // LMB
    button_mask |= (buttons & SDL_BUTTON_MMASK) ? (1 << 1) : 0; // MMB
    button_mask |= (buttons & SDL_BUTTON_RMASK) ? (1 << 2) : 0; // RMB

    button_mask |= (mw_y > 0) ? (1 << 3) : 0; // MW up
    button_mask |= (mw_y < 0) ? (1 << 4) : 0; // MW down
    button_mask |= (mw_x < 0) ? (1 << 5) : 0; // MW left
    button_mask |= (mw_x > 0) ? (1 << 6) : 0; // MW right

    debug("buttons: %x\nbutton mask: %x\n", buttons, button_mask);

    char buf[6];

    uint8_t *msg = (uint8_t *) buf;
    *msg++ = 5;
    *msg++ = button_mask;

    uint16_t *pos = (uint16_t *) msg;
    *pos++ = swap_endianness_u16b(x);
    *pos++ = swap_endianness_u16b(y);

    to_server(vnc->socket, buf, 6);
}

int key_event(SDL_vnc *vnc, bool pressed, SDL_Keysym sym) {
    char buf[8];
    SDL_Keycode key = sym.sym;

    bool shift = sym.mod & KMOD_SHIFT;

    uint8_t *msg = (uint8_t *) buf;
    *msg++ = 4;
    *msg++ = pressed;
    msg += 2;

    uint32_t *key_p = (uint32_t *) msg;
    *key_p = swap_endianness_u32b(translate_key(key, shift));

    to_server(vnc->socket, buf, 8);
}