summaryrefslogtreecommitdiffstats
path: root/xorg/X11R7.6/rdp/rdpup.c
blob: c0845a2e60b85f9c57c7b489aa4b9c6e8ad593e1 (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
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
/*
Copyright 2005-2012 Jay Sorg

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

#include "rdp.h"

#if 1
#define DEBUG_OUT_UP(arg)
#else
#define DEBUG_OUT_UP(arg) ErrorF arg
#endif

static int g_listen_sck = 0;
static int g_sck = 0;
static int g_sck_closed = 0;
static int g_connected = 0;

static int g_dis_listen_sck = 0;
//static int g_dis_sck = 0;
//static int g_dis_sck_closed = 0;
//static int g_dis_connected = 0;

static int g_begin = 0;
static struct stream* g_out_s = 0;
static struct stream* g_in_s = 0;
static int g_button_mask = 0;
static int g_cursor_x = 0;
static int g_cursor_y = 0;
static OsTimerPtr g_timer = 0;
static int g_scheduled = 0;
static int g_count = 0;
extern ScreenPtr g_pScreen; /* from rdpmain.c */
extern int g_Bpp; /* from rdpmain.c */
extern int g_Bpp_mask; /* from rdpmain.c */
extern rdpScreenInfoRec g_rdpScreen; /* from rdpmain.c */

/* true is to use unix domain socket */
extern int g_use_uds; /* in rdpmain.c */
extern char g_uds_data[]; /* in rdpmain.c */

/*
0 GXclear,        0
1 GXnor,          DPon
2 GXandInverted,  DPna
3 GXcopyInverted, Pn
4 GXandReverse,   PDna
5 GXinvert,       Dn
6 GXxor,          DPx
7 GXnand,         DPan
8 GXand,          DPa
9 GXequiv,        DPxn
a GXnoop,         D
b GXorInverted,   DPno
c GXcopy,         P
d GXorReverse,   PDno
e GXor,          DPo
f GXset          1
*/

static int g_rdp_opcodes[16] =
{
  0x00, /* GXclear        0x0 0 */
  0x88, /* GXand          0x1 src AND dst */
  0x44, /* GXandReverse   0x2 src AND NOT dst */
  0xcc, /* GXcopy         0x3 src */
  0x22, /* GXandInverted  0x4 NOT src AND dst */
  0xaa, /* GXnoop         0x5 dst */
  0x66, /* GXxor          0x6 src XOR dst */
  0xee, /* GXor           0x7 src OR dst */
  0x11, /* GXnor          0x8 NOT src AND NOT dst */
  0x99, /* GXequiv        0x9 NOT src XOR dst */
  0x55, /* GXinvert       0xa NOT dst */
  0xdd, /* GXorReverse    0xb src OR NOT dst */
  0x33, /* GXcopyInverted 0xc NOT src */
  0xbb, /* GXorInverted   0xd NOT src OR dst */
  0x77, /* GXnand         0xe NOT src OR NOT dst */
  0xff  /* GXset          0xf 1 */
};

/*****************************************************************************/
/* returns error */
static int
rdpup_send(char* data, int len)
{
  int sent;

  DEBUG_OUT_UP(("rdpup_send - sending %d bytes\n", len));
  if (g_sck_closed)
  {
    return 1;
  }
  while (len > 0)
  {
    sent = g_tcp_send(g_sck, data, len, 0);
    if (sent == -1)
    {
      if (g_tcp_last_error_would_block(g_sck))
      {
        g_sleep(1);
      }
      else
      {
        RemoveEnabledDevice(g_sck);
        g_connected = 0;
        g_tcp_close(g_sck);
        g_sck = 0;
        g_sck_closed = 1;
        return 1;
      }
    }
    else if (sent == 0)
    {
      RemoveEnabledDevice(g_sck);
      g_connected = 0;
      g_tcp_close(g_sck);
      g_sck = 0;
      g_sck_closed = 1;
      return 1;
    }
    else
    {
      data += sent;
      len -= sent;
    }
  }
  return 0;
}

/******************************************************************************/
static int
rdpup_send_msg(struct stream* s)
{
  int len;
  int rv;

  rv = 1;
  if (s != 0)
  {
    len = (int)(s->end - s->data);
    if (len > s->size)
    {
      rdpLog("overrun error len %d count %d\n", len, g_count);
    }
    s_pop_layer(s, iso_hdr);
    out_uint16_le(s, 1);
    out_uint16_le(s, g_count);
    out_uint32_le(s, len - 8);
    rv = rdpup_send(s->data, len);
  }
  if (rv != 0)
  {
    rdpLog("error in rdpup_send_msg\n");
  }
  return rv;
}

/******************************************************************************/
static CARD32
rdpDeferredUpdateCallback(OsTimerPtr timer, CARD32 now, pointer arg)
{
  if (g_connected && g_begin)
  {
    DEBUG_OUT_UP(("end %d\n", g_count));
    out_uint16_le(g_out_s, 2);
    g_count++;
    s_mark_end(g_out_s);
    rdpup_send_msg(g_out_s);
  }
  g_count = 0;
  g_begin = 0;
  g_scheduled = 0;
  return 0;
}

/******************************************************************************/
static void
rdpScheduleDeferredUpdate(void)
{
  if (!g_scheduled)
  {
    g_scheduled = 1;
    g_timer = TimerSet(g_timer, 0, 40, rdpDeferredUpdateCallback, 0);
  }
}

/******************************************************************************/
/* returns error */
static int
rdpup_recv(char* data, int len)
{
  int rcvd;

  if (g_sck_closed)
  {
    return 1;
  }
  while (len > 0)
  {
    rcvd = g_tcp_recv(g_sck, data, len, 0);
    if (rcvd == -1)
    {
      if (g_tcp_last_error_would_block(g_sck))
      {
        g_sleep(1);
      }
      else
      {
        RemoveEnabledDevice(g_sck);
        g_connected = 0;
        g_tcp_close(g_sck);
        g_sck = 0;
        g_sck_closed = 1;
        return 1;
      }
    }
    else if (rcvd == 0)
    {
      RemoveEnabledDevice(g_sck);
      g_connected = 0;
      g_tcp_close(g_sck);
      g_sck = 0;
      g_sck_closed = 1;
      return 1;
    }
    else
    {
      data += rcvd;
      len -= rcvd;
    }
  }
  return 0;
}

/******************************************************************************/
static int
rdpup_recv_msg(struct stream* s)
{
  int len;
  int rv;

  rv = 1;
  if (s != 0)
  {
    init_stream(s, 4);
    rv = rdpup_recv(s->data, 4);
    if (rv == 0)
    {
      in_uint32_le(s, len);
      if (len > 3)
      {
        init_stream(s, len);
        rv = rdpup_recv(s->data, len - 4);
      }
    }
  }
  if (rv != 0)
  {
    rdpLog("error in rdpup_recv_msg\n");
  }
  return rv;
}

/******************************************************************************/
/*
    this from miScreenInit
    pScreen->mmWidth = (xsize * 254 + dpix * 5) / (dpix * 10);
    pScreen->mmHeight = (ysize * 254 + dpiy * 5) / (dpiy * 10);
*/
static int
process_screen_size_msg(int width, int height, int bpp)
{
  RRScreenSizePtr pSize;
  int mmwidth;
  int mmheight;
  Bool ok;

  ErrorF("process_screen_size_msg: set width %d height %d bpp %d\n",
         width, height, bpp);
  g_rdpScreen.rdp_width = width;
  g_rdpScreen.rdp_height = height;
  g_rdpScreen.rdp_bpp = bpp;
  if (bpp < 15)
  {
    g_rdpScreen.rdp_Bpp = 1;
    g_rdpScreen.rdp_Bpp_mask = 0xff;
  }
  else if (bpp == 15)
  {
    g_rdpScreen.rdp_Bpp = 2;
    g_rdpScreen.rdp_Bpp_mask = 0x7fff;
  }
  else if (bpp == 16)
  {
    g_rdpScreen.rdp_Bpp = 2;
    g_rdpScreen.rdp_Bpp_mask = 0xffff;
  }
  else if (bpp > 16)
  {
    g_rdpScreen.rdp_Bpp = 4;
    g_rdpScreen.rdp_Bpp_mask = 0xffffff;
  }
  mmwidth = PixelToMM(width);
  mmheight = PixelToMM(height);

  pSize = RRRegisterSize(g_pScreen, width, height, mmwidth, mmheight);
  RRSetCurrentConfig(g_pScreen, RR_Rotate_0, 0, pSize);
  if ((g_rdpScreen.width != width) || (g_rdpScreen.height != height))
  {
    ErrorF("  calling RRScreenSizeSet\n");
    ok = RRScreenSizeSet(g_pScreen, width, height, mmwidth, mmheight);
    ErrorF("  RRScreenSizeSet ok=[%d]\n", ok);
  }
  return 0;
}

/******************************************************************************/
static int
l_bound_by(int val, int low, int high)
{
  if (val > high)
  {
    val = high;
  }
  if (val < low)
  {
    val = low;
  }
  return val;
}

/******************************************************************************/
static int
rdpup_send_caps(void)
{
  struct stream* ls;
  int len;
  int rv;
  int cap_count;
  int cap_bytes;

  make_stream(ls);
  init_stream(ls, 8192);
  s_push_layer(ls, iso_hdr, 8);

  cap_count = 0;
  cap_bytes = 0;

#if 0
  out_uint16_le(ls, 0);
  out_uint16_le(ls, 4);
  cap_count++;
  cap_bytes += 4;

  out_uint16_le(ls, 1);
  out_uint16_le(ls, 4);
  cap_count++;
  cap_bytes += 4;
#endif

  s_mark_end(ls);
  len = (int)(ls->end - ls->data);
  s_pop_layer(ls, iso_hdr);
  out_uint16_le(ls, 2); /* caps */
  out_uint16_le(ls, cap_count); /* num caps */
  out_uint32_le(ls, cap_bytes); /* caps len after header */

  rv = rdpup_send(ls->data, len);
  if (rv != 0)
  {
    ErrorF("rdpup_send_caps: rdpup_send failed\n");
  }
  free_stream(ls);
  return rv;
}

/******************************************************************************/
static int
process_version_msg(int param1, int param2, int param3, int param4)
{
  ErrorF("process_version_msg: version %d %d %d %d\n", param1, param2,
         param3, param4);
  if ((param1 > 0) || (param2 > 0) || (param3 > 0) || (param4 > 0))
  {
    rdpup_send_caps();
  }
  return 0;
}

/******************************************************************************/
static int
rdpup_process_msg(struct stream* s)
{
  int msg_type;
  int msg;
  int param1;
  int param2;
  int param3;
  int param4;

  in_uint16_le(s, msg_type);
  if (msg_type == 103)
  {
    in_uint32_le(s, msg);
    in_uint32_le(s, param1);
    in_uint32_le(s, param2);
    in_uint32_le(s, param3);
    in_uint32_le(s, param4);
    DEBUG_OUT_UP(("rdpup_process_msg - msg %d param1 %d param2 %d param3 %d \
param4 %d\n", msg, param1, param2, param3, param4));
    switch (msg)
    {
      case 15: /* key down */
      case 16: /* key up */
        KbdAddEvent(msg == 15, param1, param2, param3, param4);
        break;
      case 17: /* from RDP_INPUT_SYNCHRONIZE */
        KbdSync(param1);
        break;
      case 100:
        /* without the minus 2, strange things happen when dragging
           past the width or height */
        g_cursor_x = l_bound_by(param1, 0, g_rdpScreen.width - 2);
        g_cursor_y = l_bound_by(param2, 0, g_rdpScreen.height - 2);
        PtrAddEvent(g_button_mask, g_cursor_x, g_cursor_y);
        break;
      case 101:
        g_button_mask = g_button_mask & (~1);
        PtrAddEvent(g_button_mask, g_cursor_x, g_cursor_y);
        break;
      case 102:
        g_button_mask = g_button_mask | 1;
        PtrAddEvent(g_button_mask, g_cursor_x, g_cursor_y);
        break;
      case 103:
        g_button_mask = g_button_mask & (~4);
        PtrAddEvent(g_button_mask, g_cursor_x, g_cursor_y);
        break;
      case 104:
        g_button_mask = g_button_mask | 4;
        PtrAddEvent(g_button_mask, g_cursor_x, g_cursor_y);
        break;
      case 105:
        g_button_mask = g_button_mask & (~2);
        PtrAddEvent(g_button_mask, g_cursor_x, g_cursor_y);
        break;
      case 106:
        g_button_mask = g_button_mask | 2;
        PtrAddEvent(g_button_mask, g_cursor_x, g_cursor_y);
        break;
      case 107:
        g_button_mask = g_button_mask & (~8);
        PtrAddEvent(g_button_mask, g_cursor_x, g_cursor_y);
        break;
      case 108:
        g_button_mask = g_button_mask | 8;
        PtrAddEvent(g_button_mask, g_cursor_x, g_cursor_y);
        break;
      case 109:
        g_button_mask = g_button_mask & (~16);
        PtrAddEvent(g_button_mask, g_cursor_x, g_cursor_y);
        break;
      case 110:
        g_button_mask = g_button_mask | 16;
        PtrAddEvent(g_button_mask, g_cursor_x, g_cursor_y);
        break;
      case 200:
        rdpup_begin_update();
        rdpup_send_area((param1 >> 16) & 0xffff, param1 & 0xffff,
                        (param2 >> 16) & 0xffff, param2 & 0xffff);
        rdpup_end_update();
        break;
      case 300:
        process_screen_size_msg(param1, param2, param3);
        break;
      case 301:
        process_version_msg(param1, param2, param3, param4);
        break;
    }
  }
  else
  {
    rdpLog("unknown message type in rdpup_process_msg\n");
  }
  return 0;
}

/******************************************************************************/
int
rdpup_init(void)
{
  char text[256];
  int i;

  if (!g_directory_exist("/tmp/.xrdp"))
  {
    if (!g_create_dir("/tmp/.xrdp"))
    {
      ErrorF("rdpup_init: g_create_dir failed\n");
      return 0;
    }
    g_chmod_hex("/tmp/.xrdp", 0x1777);
  }
  i = atoi(display);
  if (i < 1)
  {
    return 0;
  }
  if (g_in_s == 0)
  {
    make_stream(g_in_s);
    init_stream(g_in_s, 8192);
  }
  if (g_out_s == 0)
  {
    make_stream(g_out_s);
    init_stream(g_out_s, 8192 * g_Bpp + 100);
  }
  if (g_use_uds)
  {
    g_sprintf(g_uds_data, "/tmp/.xrdp/xrdp_display_%s", display);
    if (g_listen_sck == 0)
    {
      g_listen_sck = g_tcp_local_socket_stream();
      if (g_tcp_local_bind(g_listen_sck, g_uds_data) != 0)
      {
        ErrorF("rdpup_init: g_tcp_local_bind failed\n");
        return 0;
      }
      g_tcp_listen(g_listen_sck);
      AddEnabledDevice(g_listen_sck);
    }
  }
  else
  {
    g_sprintf(text, "62%2.2d", i);
    if (g_listen_sck == 0)
    {
      g_listen_sck = g_tcp_socket();
      if (g_tcp_bind(g_listen_sck, text) != 0)
      {
        return 0;
      }
      g_tcp_listen(g_listen_sck);
      AddEnabledDevice(g_listen_sck);
    }
  }
  g_dis_listen_sck = g_tcp_local_socket_dgram();
  if (g_dis_listen_sck != 0)
  {
    g_sprintf(text, "/tmp/.xrdp/xrdp_disconnect_display_%s", display);
    if (g_tcp_local_bind(g_dis_listen_sck, text) == 0)
    {
      AddEnabledDevice(g_dis_listen_sck);
    }
    else
    {
      rdpLog("g_tcp_local_bind failed [%s]\n", text);
    }
  }
  return 1;
}

/******************************************************************************/
int
rdpup_check(void)
{
  int sel;
  int new_sck;
  char buf[8];

  sel = g_tcp_select(g_listen_sck, g_sck, g_dis_listen_sck);
  if (sel & 1)
  {
    new_sck = g_tcp_accept(g_listen_sck);
    if (new_sck == -1)
    {
    }
    else
    {
      if (g_sck != 0)
      {
        /* should maybe ask is user wants to allow here with timeout */
        rdpLog("replacing connection, already got a connection\n");
        RemoveEnabledDevice(g_sck);
        g_connected = 0;
        g_tcp_close(g_sck);
        g_sck = 0;
        g_sck_closed = 1;
      }
      rdpLog("got a connection\n");
      g_sck = new_sck;
      g_tcp_set_non_blocking(g_sck);
      g_tcp_set_no_delay(g_sck);
      g_connected = 1;
      g_sck_closed = 0;
      g_begin = 0;
      AddEnabledDevice(g_sck);
    }
  }
  if (sel & 2)
  {
    if (rdpup_recv_msg(g_in_s) == 0)
    {
      rdpup_process_msg(g_in_s);
    }
  }
  if (sel & 4)
  {
    if (g_tcp_recv(g_dis_listen_sck, buf, 4, 0) > 0)
    {
      if (g_sck != 0)
      {
        rdpLog("disconnecting session via user request\n");
        RemoveEnabledDevice(g_sck);
        g_connected = 0;
        g_tcp_close(g_sck);
        g_sck = 0;
        g_sck_closed = 1;
      }
    }
  }
  return 0;
}

/******************************************************************************/
int
rdpup_begin_update(void)
{
  if (g_connected)
  {
    if (g_begin)
    {
      return 0;
    }
    init_stream(g_out_s, 0);
    s_push_layer(g_out_s, iso_hdr, 8);
    out_uint16_le(g_out_s, 1);
    DEBUG_OUT_UP(("begin %d\n", g_count));
    g_begin = 1;
    g_count = 1;
  }
  return 0;
}

/******************************************************************************/
int
rdpup_end_update(void)
{
  if (g_connected && g_begin)
  {
    rdpScheduleDeferredUpdate();
  }
  return 0;
}

/******************************************************************************/
int
rdpup_pre_check(int in_size)
{
  if (!g_begin)
  {
    rdpup_begin_update();
  }
  if ((g_out_s->p - g_out_s->data) > (g_out_s->size - (in_size + 20)))
  {
    s_mark_end(g_out_s);
    rdpup_send_msg(g_out_s);
    g_count = 0;
    init_stream(g_out_s, 0);
    s_push_layer(g_out_s, iso_hdr, 8);
  }
  return 0;
}

/******************************************************************************/
int
rdpup_fill_rect(short x, short y, int cx, int cy)
{
  if (g_connected)
  {
    DEBUG_OUT_UP(("  rdpup_fill_rect\n"));
    rdpup_pre_check(10);
    out_uint16_le(g_out_s, 3);
    g_count++;
    out_uint16_le(g_out_s, x);
    out_uint16_le(g_out_s, y);
    out_uint16_le(g_out_s, cx);
    out_uint16_le(g_out_s, cy);
  }
  return 0;
}

/******************************************************************************/
int
rdpup_screen_blt(short x, short y, int cx, int cy, short srcx, short srcy)
{
  if (g_connected)
  {
    DEBUG_OUT_UP(("  rdpup_screen_blt\n"));
    rdpup_pre_check(14);
    out_uint16_le(g_out_s, 4);
    g_count++;
    out_uint16_le(g_out_s, x);
    out_uint16_le(g_out_s, y);
    out_uint16_le(g_out_s, cx);
    out_uint16_le(g_out_s, cy);
    out_uint16_le(g_out_s, srcx);
    out_uint16_le(g_out_s, srcy);
  }
  return 0;
}

/******************************************************************************/
int
rdpup_set_clip(short x, short y, int cx, int cy)
{
  if (g_connected)
  {
    DEBUG_OUT_UP(("  rdpup_set_clip\n"));
    rdpup_pre_check(10);
    out_uint16_le(g_out_s, 10);
    g_count++;
    out_uint16_le(g_out_s, x);
    out_uint16_le(g_out_s, y);
    out_uint16_le(g_out_s, cx);
    out_uint16_le(g_out_s, cy);
  }
  return 0;
}

/******************************************************************************/
int
rdpup_reset_clip(void)
{
  if (g_connected)
  {
    DEBUG_OUT_UP(("  rdpup_reset_clip\n"));
    rdpup_pre_check(2);
    out_uint16_le(g_out_s, 11);
    g_count++;
  }
  return 0;
}

#define COLOR8(r, g, b) \
  ((((r) >> 5) << 0)  | (((g) >> 5) << 3) | (((b) >> 6) << 6))
#define COLOR15(r, g, b) \
  ((((r) >> 3) << 10) | (((g) >> 3) << 5) | (((b) >> 3) << 0))
#define COLOR16(r, g, b) \
  ((((r) >> 3) << 11) | (((g) >> 2) << 5) | (((b) >> 3) << 0))
#define COLOR24(r, g, b) \
  ((((r) >> 0) << 0)  | (((g) >> 0) << 8) | (((b) >> 0) << 16))
#define SPLITCOLOR32(r, g, b, c) \
{ \
  r = ((c) >> 16) & 0xff; \
  g = ((c) >> 8) & 0xff; \
  b = (c) & 0xff; \
}

int
convert_pixel(int in_pixel)
{
  int red;
  int green;
  int blue;
  int rv;

  rv = 0;
  if (g_rdpScreen.depth == 24)
  {
    if (g_rdpScreen.rdp_bpp == 24)
    {
      rv = in_pixel;
      SPLITCOLOR32(red, green, blue, rv);
      rv = COLOR24(red, green, blue);
    }
    else if (g_rdpScreen.rdp_bpp == 16)
    {
      rv = in_pixel;
      SPLITCOLOR32(red, green, blue, rv);
      rv = COLOR16(red, green, blue);
    }
    else if (g_rdpScreen.rdp_bpp == 15)
    {
      rv = in_pixel;
      SPLITCOLOR32(red, green, blue, rv);
      rv = COLOR15(red, green, blue);
    }
    else if (g_rdpScreen.rdp_bpp == 8)
    {
      rv = in_pixel;
      SPLITCOLOR32(red, green, blue, rv);
      rv = COLOR8(red, green, blue);
    }
  }
  else if (g_rdpScreen.depth == g_rdpScreen.rdp_bpp)
  {
    return in_pixel;
  }
  return rv;
}

int
convert_pixels(void* src, void* dst, int num_pixels)
{
  unsigned int pixel;
  unsigned int red;
  unsigned int green;
  unsigned int blue;
  unsigned int* src32;
  unsigned int* dst32;
  unsigned short* dst16;
  unsigned char* dst8;
  int index;

  if (g_rdpScreen.depth == g_rdpScreen.rdp_bpp)
  {
    memcpy(dst, src, num_pixels * g_Bpp);
    return 0;
  }
  if (g_rdpScreen.depth == 24)
  {
    src32 = (unsigned int*)src;
    if (g_rdpScreen.rdp_bpp == 24)
    {
      dst32 = (unsigned int*)dst;
      for (index = 0; index < num_pixels; index++)
      {
        pixel = *src32;
        *dst32 = pixel;
        dst32++;
        src32++;
      }
    }
    else if (g_rdpScreen.rdp_bpp == 16)
    {
      dst16 = (unsigned short*)dst;
      for (index = 0; index < num_pixels; index++)
      {
        pixel = *src32;
        SPLITCOLOR32(red, green, blue, pixel);
        pixel = COLOR16(red, green, blue);
        *dst16 = pixel;
        dst16++;
        src32++;
      }
    }
    else if (g_rdpScreen.rdp_bpp == 15)
    {
      dst16 = (unsigned short*)dst;
      for (index = 0; index < num_pixels; index++)
      {
        pixel = *src32;
        SPLITCOLOR32(red, green, blue, pixel);
        pixel = COLOR15(red, green, blue);
        *dst16 = pixel;
        dst16++;
        src32++;
      }
    }
    else if (g_rdpScreen.rdp_bpp == 8)
    {
      dst8 = (unsigned char*)dst;
      for (index = 0; index < num_pixels; index++)
      {
        pixel = *src32;
        SPLITCOLOR32(red, green, blue, pixel);
        pixel = COLOR8(red, green, blue);
        *dst8 = pixel;
        dst8++;
        src32++;
      }
    }
  }
  return 0;
}

/******************************************************************************/
int
rdpup_set_fgcolor(int fgcolor)
{
  if (g_connected)
  {
    DEBUG_OUT_UP(("  rdpup_set_fgcolor\n"));
    rdpup_pre_check(6);
    out_uint16_le(g_out_s, 12);
    g_count++;
    fgcolor = fgcolor & g_Bpp_mask;
    fgcolor = convert_pixel(fgcolor) & g_rdpScreen.rdp_Bpp_mask;
    out_uint32_le(g_out_s, fgcolor);
  }
  return 0;
}

/******************************************************************************/
int
rdpup_set_bgcolor(int bgcolor)
{
  if (g_connected)
  {
    DEBUG_OUT_UP(("  rdpup_set_bgcolor\n"));
    rdpup_pre_check(6);
    out_uint16_le(g_out_s, 13);
    g_count++;
    bgcolor = bgcolor & g_Bpp_mask;
    bgcolor = convert_pixel(bgcolor) & g_rdpScreen.rdp_Bpp_mask;
    out_uint32_le(g_out_s, bgcolor);
  }
  return 0;
}

/******************************************************************************/
int
rdpup_set_opcode(int opcode)
{
  if (g_connected)
  {
    DEBUG_OUT_UP(("  rdpup_set_opcode\n"));
    rdpup_pre_check(4);
    out_uint16_le(g_out_s, 14);
    g_count++;
    out_uint16_le(g_out_s, g_rdp_opcodes[opcode & 0xf]);
  }
  return 0;
}

/******************************************************************************/
int
rdpup_set_pen(int style, int width)
{
  if (g_connected)
  {
    DEBUG_OUT_UP(("  rdpup_set_pen\n"));
    rdpup_pre_check(6);
    out_uint16_le(g_out_s, 17);
    g_count++;
    out_uint16_le(g_out_s, style);
    out_uint16_le(g_out_s, width);
  }
  return 0;
}

/******************************************************************************/
int
rdpup_draw_line(short x1, short y1, short x2, short y2)
{
  if (g_connected)
  {
    DEBUG_OUT_UP(("  rdpup_draw_line\n"));
    rdpup_pre_check(10);
    out_uint16_le(g_out_s, 18);
    g_count++;
    out_uint16_le(g_out_s, x1);
    out_uint16_le(g_out_s, y1);
    out_uint16_le(g_out_s, x2);
    out_uint16_le(g_out_s, y2);
  }
  return 0;
}

/******************************************************************************/
int
rdpup_set_cursor(short x, short y, char* cur_data, char* cur_mask)
{
  if (g_connected)
  {
    DEBUG_OUT_UP(("  rdpup_set_cursor\n"));
    rdpup_pre_check(6 + 32 * (32 * 3) + 32 * (32 / 8));
    out_uint16_le(g_out_s, 19);
    g_count++;
    x = MAX(0, x);
    x = MIN(31, x);
    y = MAX(0, y);
    y = MIN(31, y);
    out_uint16_le(g_out_s, x);
    out_uint16_le(g_out_s, y);
    out_uint8a(g_out_s, cur_data, 32 * (32 * 3));
    out_uint8a(g_out_s, cur_mask, 32 * (32 / 8));
  }
  return 0;
}

/******************************************************************************/
static int
get_single_color(int x, int y, int w, int h)
{
  int rv;
  int i;
  int j;
  int p;
  unsigned char* i8;
  unsigned short* i16;
  unsigned int* i32;

  p = 0;
  rv = -1;
  if (g_Bpp == 1)
  {
    for (i = 0; i < h; i++)
    {
      i8 = (unsigned char*)(g_rdpScreen.pfbMemory +
               ((y + i) * g_rdpScreen.paddedWidthInBytes) + (x * g_Bpp));
      if (i == 0)
      {
        p = *i8;
      }
      for (j = 0; j < w; j++)
      {
        if (i8[j] != p)
        {
          return -1;
        }
      }
    }
    rv = p;
  }
  else if (g_Bpp == 2)
  {
    for (i = 0; i < h; i++)
    {
      i16 = (unsigned short*)(g_rdpScreen.pfbMemory +
               ((y + i) * g_rdpScreen.paddedWidthInBytes) + (x * g_Bpp));
      if (i == 0)
      {
        p = *i16;
      }
      for (j = 0; j < w; j++)
      {
        if (i16[j] != p)
        {
          return -1;
        }
      }
    }
    rv = p;
  }
  else if (g_Bpp == 4)
  {
    for (i = 0; i < h; i++)
    {
      i32 = (unsigned int*)(g_rdpScreen.pfbMemory +
               ((y + i) * g_rdpScreen.paddedWidthInBytes) + (x * g_Bpp));
      if (i == 0)
      {
        p = *i32;
      }
      for (j = 0; j < w; j++)
      {
        if (i32[j] != p)
        {
          return -1;
        }
      }
    }
    rv = p;
  }
  return rv;
}

/******************************************************************************/
/* split the bitmap up into 64 x 64 pixel areas */
void
rdpup_send_area(int x, int y, int w, int h)
{
  char* s;
  int i;
  int single_color;
  int lx;
  int ly;
  int lh;
  int lw;

  if (x >= g_rdpScreen.width)
  {
    return;
  }
  if (y >= g_rdpScreen.height)
  {
    return;
  }
  if (x < 0)
  {
    w += x;
    x = 0;
  }
  if (y < 0)
  {
    h += y;
    y = 0;
  }
  if (w <= 0)
  {
    return;
  }
  if (h <= 0)
  {
    return;
  }
  if (x + w > g_rdpScreen.width)
  {
    w = g_rdpScreen.width - x;
  }
  if (y + h > g_rdpScreen.height)
  {
    h = g_rdpScreen.height - y;
  }
  DEBUG_OUT_UP(("%d\n", w * h));
  if (g_connected && g_begin)
  {
    DEBUG_OUT_UP(("  rdpup_send_area\n"));
    ly = y;
    while (ly < y + h)
    {
      lx = x;
      while (lx < x + w)
      {
        lw = MIN(64, (x + w) - lx);
        lh = MIN(64, (y + h) - ly);
        single_color = get_single_color(lx, ly, lw, lh);
        if (single_color != -1)
        {
          DEBUG_OUT_UP(("%d sending single color\n", g_count));
          rdpup_set_fgcolor(single_color);
          rdpup_fill_rect(lx, ly, lw, lh);
        }
        else
        {
          rdpup_pre_check(lw * lh * g_rdpScreen.rdp_Bpp + 42);
          out_uint16_le(g_out_s, 5);
          g_count++;
          out_uint16_le(g_out_s, lx);
          out_uint16_le(g_out_s, ly);
          out_uint16_le(g_out_s, lw);
          out_uint16_le(g_out_s, lh);
          out_uint32_le(g_out_s, lw * lh * g_rdpScreen.rdp_Bpp);
          for (i = 0; i < lh; i++)
          {
            s = (g_rdpScreen.pfbMemory +
                  ((ly + i) * g_rdpScreen.paddedWidthInBytes) + (lx * g_Bpp));
            convert_pixels(s, g_out_s->p, lw);
            g_out_s->p += lw * g_rdpScreen.rdp_Bpp;
          }
          out_uint16_le(g_out_s, lw);
          out_uint16_le(g_out_s, lh);
          out_uint16_le(g_out_s, 0);
          out_uint16_le(g_out_s, 0);
        }
        lx += 64;
      }
      ly += 64;
    }
  }
}