summaryrefslogtreecommitdiffstats
path: root/vnc/vnc.c
blob: dffe0891f53cb3be9a2f40a65c48f2dda9125753 (plain)
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
/*
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   xrdp: A Remote Desktop Protocol server.
   Copyright (C) Jay Sorg 2004

   libvnc

*/

#include "vnc.h"

/******************************************************************************/
int lib_mod_event(int handle, int msg, int param1, int param2)
{
  struct vnc* v;
  struct stream* s;
  int key;

  make_stream(s);
  v = (struct vnc*)handle;
  if (msg >= 15 && msg <= 16)
  {
    key = 0;
    if (param2 == 0xffff)
    {
      key = param1;
    }
    else
    {
      switch (param1)
      {
        case 0x0001: key = 0xff1b; break; /* ecs */
        case 0x000e: key = 0xff08; break; /* backspace */
        case 0x000f: key = 0xff09; break; /* tab */
        case 0x001c: key = 0xff0d; break; /* enter */
        /* left-right control */
        case 0x001d: key = (param2 & 0x0100) ? 0xffe3 : 0xffe4; break;
        case 0x002a: key = 0xffe1; break; /* left shift */
        case 0x0036: key = 0xffe2; break; /* right shift */
        /* left-right alt */
        case 0x0038: key = (param2 & 0x0100) ? 0xffe9 : 0xffea; break;
        case 0x003b: key = 0xffbe; break; /* F1 */
        case 0x003c: key = 0xffbf; break; /* F2 */
        case 0x003d: key = 0xffc0; break; /* F3 */
        case 0x003e: key = 0xffc1; break; /* F4 */
        case 0x003f: key = 0xffc2; break; /* F5 */
        case 0x0040: key = 0xffc3; break; /* F6 */
        case 0x0041: key = 0xffc4; break; /* F7 */
        case 0x0042: key = 0xffc5; break; /* F8 */
        case 0x0043: key = 0xffc6; break; /* F9 */
        case 0x0044: key = 0xffc7; break; /* F10 */
        case 0x0047: key = 0xff50; break; /* home */
        case 0x0048: key = 0xff52; break; /* up arrow */
        case 0x0049: key = 0xff55; break; /* page up */
        case 0x004b: key = 0xff51; break; /* left arrow */
        case 0x004d: key = 0xff53; break; /* right arrow */
        case 0x004f: key = 0xff57; break; /* end */
        case 0x0050: key = 0xff54; break; /* down arrow */
        case 0x0051: key = 0xff56; break; /* page down */
        case 0x0052: key = 0xff63; break; /* insert */
        case 0x0053: key = 0xffff; break; /* delete */
        case 0x0057: key = 0xffc8; break; /* F11 */
        case 0x0058: key = 0xffc9; break; /* F12 */
      }
    }
    if (key > 0)
    {
      init_stream(s, 8192);
      out_uint8(s, 4);
      out_uint8(s, msg == 15); /* down flag */
      out_uint8s(s, 2);
      out_uint32_be(s, key);
      if (g_tcp_force_send(v->sck, s->data, 8) != 0)
      {
        free_stream(s);
        return 1;
      }
    }
  }
  else if (msg >= 100 && msg <= 110)
  {
    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, 5);
    out_uint8(s, v->mod_mouse_state);
    out_uint16_be(s, param1);
    out_uint16_be(s, param2);
    if (g_tcp_force_send(v->sck, s->data, 6) != 0)
    {
      free_stream(s);
      return 1;
    }
  }
  free_stream(s);
  return 0;
}

//******************************************************************************
int get_pixel_safe(char* data, int x, int y, int width, int height, int bpp)
{
  int start;
  int shift;

  if (x < 0)
    return 0;
  if (y < 0)
    return 0;
  if (x >= width)
    return 0;
  if (y >= height)
    return 0;
  if (bpp == 1)
  {
    width = (width + 7) / 8;
    start = (y * width) + x / 8;
    shift = x % 8;
    return (data[start] & (0x80 >> shift)) != 0;
  }
  else if (bpp == 4)
  {
    width = (width + 1) / 2;
    start = y * width + x / 2;
    shift = x % 2;
    if (shift == 0)
      return (data[start] & 0xf0) >> 4;
    else
      return data[start] & 0x0f;
  }
  else if (bpp == 8)
  {
    return *(((unsigned char*)data) + (y * width + x));
  }
  else if (bpp == 15 || bpp == 16)
  {
    return *(((unsigned short*)data) + (y * width + x));
  }
  return 0;
}

/******************************************************************************/
void set_pixel_safe(char* data, int x, int y, int width, int height, int bpp,
                    int pixel)
{
  int start;
  int shift;

  if (x < 0)
    return;
  if (y < 0)
    return;
  if (x >= width)
    return;
  if (y >= height)
    return;
  if (bpp == 1)
  {
    width = (width + 7) / 8;
    start = (y * width) + x / 8;
    shift = x % 8;
    if (pixel & 1)
      data[start] = data[start] | (0x80 >> shift);
    else
      data[start] = data[start] & ~(0x80 >> shift);
  }
  else if (bpp == 15 || bpp == 16)
  {
    *(((unsigned short*)data) + (y * width + x)) = pixel;
  }
  else if (bpp == 24)
  {
    *(data + (3 * (y * width + x)) + 0) = pixel >> 0;
    *(data + (3 * (y * width + x)) + 1) = pixel >> 8;
    *(data + (3 * (y * width + x)) + 2) = pixel >> 16;
  }
}

/******************************************************************************/
int split_color(int pixel, int* r, int* g, int* b, int bpp, int* palette)
{
  if (bpp == 8)
  {
    if (pixel >= 0 && pixel < 256 && palette != 0)
    {
      *r = (palette[pixel] >> 16) & 0xff;
      *g = (palette[pixel] >> 8) & 0xff;
      *b = (palette[pixel] >> 0) & 0xff;
    }
  }
  else if (bpp == 16)
  {
    *r = ((pixel >> 8) & 0xf8) | ((pixel >> 13) & 0x7);
    *g = ((pixel >> 3) & 0xfc) | ((pixel >> 9) & 0x3);
    *b = ((pixel << 3) & 0xf8) | ((pixel >> 2) & 0x7);
  }
  return 0;
}

/******************************************************************************/
int make_color(int r, int g, int b, int bpp)
{
  if (bpp == 24)
  {
    return (r << 16) | (g << 8) | b;
  }
  return 0;
}

/******************************************************************************/
int lib_framebuffer_update(struct vnc* v)
{
  char* data;
  char* d1;
  char* d2;
  char cursor_data[32 * (32 * 3)];
  char cursor_mask[32 * (32 / 8)];
  int num_recs;
  int i;
  int j;
  int k;
  int x;
  int y;
  int cx;
  int cy;
  int srcx;
  int srcy;
  int encoding;
  int Bpp;
  int pixel;
  int r;
  int g;
  int b;
  struct stream* s;

  Bpp = (v->mod_bpp + 7) / 8;
  make_stream(s);
  init_stream(s, 8192);
  if (g_tcp_force_recv(v->sck, s->data, 3) != 0)
  {
    free_stream(s);
    return 1;
  }
  in_uint8s(s, 1);
  in_uint16_be(s, num_recs);
  for (i = 0; i < num_recs; i++)
  {
    init_stream(s, 8192);
    if (g_tcp_force_recv(v->sck, s->data, 12) != 0)
    {
      free_stream(s);
      return 1;
    }
    in_uint16_be(s, x);
    in_uint16_be(s, y);
    in_uint16_be(s, cx);
    in_uint16_be(s, cy);
    in_uint32_be(s, encoding);
    if (encoding == 0) /* raw */
    {
      data = (char*)g_malloc(cx * cy * Bpp, 0);
      if (g_tcp_force_recv(v->sck, data, cx * cy * Bpp) != 0)
      {
        g_free(data);
        free_stream(s);
        return 1;
      }
      v->server_begin_update((int)v);
      v->server_paint_rect((int)v, x, y, cx, cy, data);
      v->server_end_update((int)v);
      g_free(data);
    }
    else if (encoding == 1) /* copy rect */
    {
      init_stream(s, 8192);
      if (g_tcp_force_recv(v->sck, s->data, 4) != 0)
      {
        free_stream(s);
        return 1;
      }
      in_uint16_be(s, srcx);
      in_uint16_be(s, srcy);
      v->server_begin_update((int)v);
      v->server_screen_blt((int)v, x, y, cx, cy, srcx, srcy);
      v->server_end_update((int)v);
    }
    else if (encoding == 0xffffff11) /* cursor */
    {
      g_memset(cursor_data, 0, 32 * (32 * 3));
      g_memset(cursor_mask, 0, 32 * (32 / 8));
      init_stream(s, 8192);
      if (g_tcp_force_recv(v->sck, s->data,
                           cx * cy * Bpp + ((cx + 7) / 8) * cy) != 0)
      {
        free_stream(s);
        return 1;
      }
      in_uint8p(s, d1, cx * cy * Bpp);
      in_uint8p(s, d2, ((cx + 7) / 8) * cy);
      for (j = 0; j < 32; j++)
      {
        for (k = 0; k < 32; k++)
        {
          pixel = get_pixel_safe(d2, k, 31 - j, cx, cy, 1);
          set_pixel_safe(cursor_mask, k, j, 32, 32, 1, !pixel);
          if (pixel)
          {
            pixel = get_pixel_safe(d1, k, 31 - j, cx, cy, v->mod_bpp);
            split_color(pixel, &r, &g, &b, v->mod_bpp, v->palette);
            pixel = make_color(r, g, b, 24);
            set_pixel_safe(cursor_data, k, j, 32, 32, 24, pixel);
          }
        }
      }
      v->server_set_cursor((int)v, x, y, cursor_data, cursor_mask);
    }
  }
  /* FrambufferUpdateRequest */
  init_stream(s, 8192);
  out_uint8(s, 3);
  out_uint8(s, 1);
  out_uint16_be(s, 0);
  out_uint16_be(s, 0);
  out_uint16_be(s, v->mod_width);
  out_uint16_be(s, v->mod_height);
  if (g_tcp_force_send(v->sck, s->data, 10) != 0)
  {
    free_stream(s);
    return 1;
  }
  free_stream(s);
  return 0;
}

/******************************************************************************/
int lib_clip_data(struct vnc* v)
{
  struct stream* s;
  int size;

  make_stream(s);
  init_stream(s, 8192);
  if (g_tcp_force_recv(v->sck, s->data, 7) != 0)
  {
    free_stream(s);
    return 1;
  }
  in_uint8s(s, 3);
  in_uint32_be(s, size);
  init_stream(s, 8192);
  if (g_tcp_force_recv(v->sck, s->data, size) != 0)
  {
    free_stream(s);
    return 1;
  }
  free_stream(s);
  return 0;
}

/******************************************************************************/
int lib_palette_update(struct vnc* v)
{
  struct stream* s;
  int first_color;
  int num_colors;
  int i;
  int r;
  int g;
  int b;

  make_stream(s);
  init_stream(s, 8192);
  if (g_tcp_force_recv(v->sck, s->data, 5) != 0)
  {
    free_stream(s);
    return 1;
  }
  in_uint8s(s, 1);
  in_uint16_be(s, first_color);
  in_uint16_be(s, num_colors);
  init_stream(s, 8192);
  if (g_tcp_force_recv(v->sck, s->data, num_colors * 6) != 0)
  {
    free_stream(s);
    return 1;
  }
  for (i = 0; i < num_colors; i++)
  {
    in_uint16_be(s, r);
    in_uint16_be(s, g);
    in_uint16_be(s, b);
    r = r >> 8;
    g = g >> 8;
    b = b >> 8;
    v->palette[first_color + i] = (r << 16) | (g << 8) | b;
  }
  v->server_begin_update((int)v);
  v->server_palette((int)v, v->palette);
  v->server_end_update((int)v);
  free_stream(s);
  return 0;
}

/******************************************************************************/
int lib_mod_signal(int handle)
{
  struct vnc* v;
  char type;

  v = (struct vnc*)handle;
  if (g_tcp_force_recv(v->sck, &type, 1) != 0)
  {
    return 1;
  }
  if (type == 0) /* framebuffer update */
  {
    if (lib_framebuffer_update(v) != 0)
    {
      return 1;
    }
  }
  else if (type == 1) /* palette */
  {
    if (lib_palette_update(v) != 0)
    {
      return 1;
    }
  }
  else if (type == 3) /* clipboard */
  {
    if (lib_clip_data(v) != 0)
    {
      return 1;
    }
  }
  else
  {
    g_printf("unknown in lib_mod_signal %d\n\r", type);
  }
  return 0;
}

/******************************************************************************/
int lib_mod_start(int handle, int w, int h, int bpp)
{
  struct vnc* v;

  v = (struct vnc*)handle;
  v->server_begin_update(handle);
  v->server_fill_rect(handle, 0, 0, w, h, 0);
  v->server_end_update(handle);
  v->server_width = w;
  v->server_height = h;
  v->server_bpp = bpp;
  return 0;
}

/******************************************************************************/
int lib_mod_connect(int handle, char* ip, char* port,
                    char* username, char* password)
{
  char cursor_data[32 * (32 * 3)];
  char cursor_mask[32 * (32 / 8)];
  struct vnc* v;
  struct stream* s;
  struct stream* pixel_format;
  int error;
  int i;

  make_stream(s);
  make_stream(pixel_format);
  v = (struct vnc*)handle;
  v->sck = g_tcp_socket();
  error = g_tcp_connect(v->sck, ip, port);
  if (error == 0)
  {
    g_tcp_set_non_blocking(v->sck);
    /* protocal version */
    init_stream(s, 8192);
    g_tcp_force_recv(v->sck, s->data, 12);
    g_tcp_force_send(v->sck, "RFB 003.003\n", 12);
    /* sec type */
    init_stream(s, 8192);
    g_tcp_force_recv(v->sck, s->data, 4);
    in_uint32_be(s, i);
    if (i == 2) /* dec the password and the server random */
    {
      init_stream(s, 8192);
      g_tcp_force_recv(v->sck, s->data, 16);
      rfbEncryptBytes((unsigned char*)s->data, password);
      g_tcp_force_send(v->sck, s->data, 16);
    }
    else
      error = 1;
  }
  if (error == 0)
  {
    /* sec result */
    init_stream(s, 8192);
    g_tcp_force_recv(v->sck, s->data, 4);
    in_uint32_be(s, i);
    if (i != 0)
      error = 2;
  }
  if (error == 0)
  {
    init_stream(s, 8192);
    s->data[0] = 1;
    g_tcp_force_send(v->sck, s->data, 1); /* share flag */
    g_tcp_force_recv(v->sck, s->data, 4); /* server init */
    in_uint16_be(s, v->mod_width);
    in_uint16_be(s, v->mod_height);
    init_stream(pixel_format, 8192);
    g_tcp_force_recv(v->sck, pixel_format->data, 16);
    in_uint8(pixel_format, v->mod_bpp);
    init_stream(s, 8192);
    g_tcp_force_recv(v->sck, s->data, 4); /* name len */
    in_uint32_be(s, i);
    if (i > 255 || i < 0)
      error = 3;
    else
    {
      g_tcp_force_recv(v->sck, v->mod_name, i);
      v->mod_name[i] = 0;
    }
    /* should be connected */
  }
  if (error == 0)
  {
    /* SetPixelFormat */
    init_stream(s, 8192);
    out_uint8(s, 0);
    out_uint8(s, 0);
    out_uint8(s, 0);
    out_uint8(s, 0);
    init_stream(pixel_format, 8192);
    if (v->mod_bpp == 8)
    {
      out_uint8(pixel_format, 8); /* bits per pixel */
      out_uint8(pixel_format, 8); /* depth */
      out_uint8(pixel_format, 0); /* big endian */
      out_uint8(pixel_format, 0); /* true color flag */
      out_uint16_be(pixel_format, 0); /* red max */
      out_uint16_be(pixel_format, 0); /* green max */
      out_uint16_be(pixel_format, 0); /* blue max */
      out_uint8(pixel_format, 0); /* red shift */
      out_uint8(pixel_format, 0); /* green shift */
      out_uint8(pixel_format, 0); /* blue shift */
      out_uint8s(pixel_format, 3); /* pad */
    }
    out_uint8a(s, pixel_format->data, 16);
    g_tcp_force_send(v->sck, s->data, 20);
    /* SetEncodings */
    init_stream(s, 8192);
    out_uint8(s, 2);
    out_uint8(s, 0);
    out_uint16_be(s, 3);
    out_uint32_be(s, 0); /* raw */
    out_uint32_be(s, 1); /* copy rect */
    out_uint32_be(s, 0xffffff11); /* cursor */
    g_tcp_force_send(v->sck, s->data, 4 + 3 * 4);
    /* FrambufferUpdateRequest */
    init_stream(s, 8192);
    out_uint8(s, 3);
    out_uint8(s, 0);
    out_uint16_be(s, 0);
    out_uint16_be(s, 0);
    out_uint16_be(s, v->mod_width);
    out_uint16_be(s, v->mod_height);
    g_tcp_force_send(v->sck, s->data, 10);
  }
  if (error == 0)
  {
    v->server_error_popup((int)v, "hi", "Hi");
    if (v->server_bpp != v->mod_bpp)
      error = 4;
  }
  /* set almost null cursor */
  g_memset(cursor_data, 0, 32 * (32 * 3));
  g_memset(cursor_data + (32 * (32 * 3) - 1 * 32 * 3), 0xff, 9);
  g_memset(cursor_data + (32 * (32 * 3) - 2 * 32 * 3), 0xff, 9);
  g_memset(cursor_data + (32 * (32 * 3) - 3 * 32 * 3), 0xff, 9);
  g_memset(cursor_mask, 0xff, 32 * (32 / 8));
  v->server_set_cursor((int)v, 0, 0, cursor_data, cursor_mask);
  free_stream(s);
  free_stream(pixel_format);
  return error;
}

/******************************************************************************/
int lib_mod_invalidate(int handle, int x, int y, int cx, int cy)
{
  struct vnc* v;
  struct stream* s;

  make_stream(s);
  v = (struct vnc*)handle;
  /* FrambufferUpdateRequest */
  init_stream(s, 8192);
  out_uint8(s, 3);
  out_uint8(s, 0);
  out_uint16_be(s, x);
  out_uint16_be(s, y);
  out_uint16_be(s, cx);
  out_uint16_be(s, cy);
  g_tcp_force_send(v->sck, s->data, 10);
  free_stream(s);
  return 0;
}

/******************************************************************************/
int mod_init()
{
  struct vnc* v;

  v = (struct vnc*)g_malloc(sizeof(struct vnc), 1);
  /* set client functions */
  v->size = sizeof(struct vnc);
  v->handle = (int)v;
  v->mod_connect = lib_mod_connect;
  v->mod_start = lib_mod_start;
  v->mod_event = lib_mod_event;
  v->mod_signal = lib_mod_signal;
  v->mod_invalidate = lib_mod_invalidate;
  return (int)v;
}

/******************************************************************************/
int mod_exit(int handle)
{
  struct vnc* v;

  if (handle == 0)
    return 0;
  v = (struct vnc*)handle;
  g_tcp_close(v->sck);
  g_free(v);
  return 0;
}