summaryrefslogtreecommitdiffstats
path: root/uirdesktop/uimain.c
blob: 9f9bcac730e5f98d6d4ef1823bdef32d368962f1 (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
/* -*- c-basic-offset: 8 -*-
   rdesktop: A Remote Desktop Protocol client.
   Main ui file
   Copyright (C) Jay Sorg 2006

   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.
*/

#include "rdesktop.h"
#include "bsops.h"
#include "uimain.h"

char g_username[256] = "";
char g_hostname[256] = "";
char g_servername[256] = "";
char g_password[256] = "";
char g_shell[256] = "";
char g_directory[256] = "";
char g_domain[256] = "";
char * g_bs = 0; /* created in hardware file, used in bsops.c */
int g_bs_Bpp = 4;
int g_bs_bpp = 32;
BOOL g_desktop_save = False; /* desktop save order */
BOOL g_polygon_ellipse_orders = False; /* polygon / ellipse orders */
BOOL g_bitmap_compression = True;
uint32 g_rdp5_performanceflags =
  RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG | RDP5_NO_MENUANIMATIONS;
BOOL g_bitmap_cache_persist_enable = False;
BOOL g_bitmap_cache_precache = True;
BOOL g_bitmap_cache = True;
BOOL g_encryption = True;
int g_server_depth = 8;
BOOL g_use_rdp5 = False;
int g_width = 800;
int g_height = 600;
uint32 g_keylayout = 0x409; /* Defaults to US keyboard layout */
int g_keyboard_type = 0x4; /* Defaults to US keyboard layout */
int g_keyboard_subtype = 0x0; /* Defaults to US keyboard layout */
int g_keyboard_functionkeys = 0xc; /* Defaults to US keyboard layout */
BOOL g_console_session = False;

/* can't be static, hardware file or bsops need these */
int g_tcp_sck = 0;
int pal_entries[256];

/* Session Directory redirection */
BOOL g_redirect = False;
char g_redirect_server[64];
char g_redirect_domain[16];
char g_redirect_password[64];
char g_redirect_username[64];
char g_redirect_cookie[128];
uint32 g_redirect_flags = 0;

extern int g_tcp_port_rdp;

static int g_deactivated = 0;
static uint32 g_ext_disc_reason = 0;

struct bitmap
{
  uint8 * data;
  uint32 width;
  uint32 height;
};

/* in ui specific file eg win32.c, qt.c, dfb.c, ... */
int
mi_create_window(void);
void
mi_invalidate(int x, int y, int cx, int cy);
int
mi_create_bs(void);
int
mi_main_loop(void);
void
mi_error(char * msg);
void
mi_warning(char * msg);
void
ui_invalidate(int x, int y, int cx, int cy);
void
mi_paint_rect(char * data, int width, int height, int x, int y, int cx, int cy);
void
mi_begin_update(void);
void
mi_end_update(void);
void
mi_fill_rect(int x, int y, int cx, int cy, int colour);
void
mi_screen_copy(int x, int y, int cx, int cy, int srcx, int srcy);
void
mi_set_clip(int x, int y, int cx, int cy);
void
mi_reset_clip(void);
void
mi_line(int x1, int y1, int x2, int y2, int colour);
mi_create_cursor(unsigned int x, unsigned int y,
                 int width, int height,
                 unsigned char * andmask, unsigned char * xormask);
void
mi_destroy_cursor(void * cursor);
void
mi_set_cursor(void * cursor);
void
mi_set_null_cursor(void);
int
mi_read_keyboard_state(void);

/*****************************************************************************/
/* put part of the screen from the backing store to the display */
void
ui_invalidate(int x, int y, int cx, int cy)
{
  if (cx < 1 || cy < 1 || g_bs == 0)
  {
    return;
  }
  if (bs_warp_coords(&x, &y, &cx, &cy, 0, 0))
  {
    mi_invalidate(x, y, cx, cy);
  }
/*
  if (bs_warp_coords(&x, &y, &cx, &cy, 0, 0))
  {
    cx = (cx + 3) & ~3;
    data = (char *) xmalloc(cx * cy * 4);
    bs_copy_box(data, x, y, cx, cy, cx * ((g_server_depth + 7) / 8));
    mi_paint_rect(data, cx, cy, x, y, cx, cy);
    xfree(data);
  }
*/
}

/*****************************************************************************/
static int
convert_colour(int in_colour)
{
  int r;
  int g;
  int b;

  if (g_server_depth == 8)
  {
    r = (pal_entries[in_colour & 0xff] & 0xff0000) >> 16;
    g = (pal_entries[in_colour & 0xff] & 0xff00) >> 8;
    b = pal_entries[in_colour & 0xff] & 0xff;
  }
  else if (g_server_depth == 15)
  {
    SPLIT_COLOUR15(in_colour, r, g, b);
  }  
  else if (g_server_depth == 16)
  {
    SPLIT_COLOUR16(in_colour, r, g, b);
  }
  else if (g_server_depth == 24)
  {
    SPLIT_COLOUR32(in_colour, r, g, b);
  }
  if (g_bs_bpp == 32)
  {
    MAKE_COLOUR32(in_colour, r, g, b);
  }
  return in_colour;  
}

/*****************************************************************************/
void
ui_bell(void)
{
}

/*****************************************************************************/
int
ui_select(int in)
{
  if (g_tcp_sck == 0)
  {
    g_tcp_sck = in;
  }
  return 1;
}

/*****************************************************************************/
void *
ui_create_cursor(uint32 x, uint32 y,
                 int width, int height,
                 uint8 * andmask, uint8 * xormask)
{
  int i;
  int j;
  char am[32 * 4];
  char xm[32 * 4];

  if (width != 32 || height != 32)
  {
    return 0;
  }
  memset(am, 0, 32 * 4);
  memset(xm, 0, 32 * 4);
  for (i = 0; i < 32; i++)
  {
    for (j = 0; j < 32; j++)
    {
      if (bs_is_pixel_on(andmask, j, i, 32, 1))
      {
        bs_set_pixel_on(am, j, 31 - i, 32, 1, 1);
      }
      if (bs_is_pixel_on(xormask, j, i, 32, 24))
      {
        bs_set_pixel_on(xm, j, 31 - i, 32, 1, 1);
      }
    }
  }
  return (void *) mi_create_cursor(x, y, width, height, am, xm);
}

/*****************************************************************************/
void
ui_destroy_cursor(void * cursor)
{
  mi_destroy_cursor(cursor);
}

/*****************************************************************************/
void
ui_set_cursor(void * cursor)
{
  mi_set_cursor(cursor);
}

/*****************************************************************************/
void
ui_set_null_cursor(void)
{
  mi_set_null_cursor();
}

/*****************************************************************************/
void *
ui_create_glyph(int width, int height, uint8 * data)
{
  int i;
  int j;
  char * glyph_data;
  struct bitmap * the_glyph;

  glyph_data = (char *) xmalloc(width * height);
  memset(glyph_data, 0, width * height);
  the_glyph = (struct bitmap *) xmalloc(sizeof(struct bitmap));
  memset(the_glyph, 0, sizeof(struct bitmap));
  the_glyph->width = width;
  the_glyph->height = height;
  the_glyph->data = glyph_data;
  for (i = 0; i < height; i++)
  {
    for (j = 0; j < width; j++)
    {
      if (bs_is_pixel_on(data, j, i, width, 1))
      {
        bs_set_pixel_on(glyph_data, j, i, width, 8, 255);
      }
    }
  }
  return the_glyph;
}

/*****************************************************************************/
void
ui_destroy_glyph(void * glyph)
{
  struct bitmap * the_glyph;

  the_glyph = glyph;
  if (the_glyph != 0)
  {
    xfree(the_glyph->data);
  }
  xfree(the_glyph);
}

/*****************************************************************************/
void *
ui_create_bitmap(int width, int height, uint8 * data)
{
#if 0
  struct bitmap * b;
  int size;

  size = width * height * ((g_server_depth + 7) / 8);
  b = (struct bitmap *) xmalloc(sizeof(struct bitmap));
  b->data = (uint8 *) xmalloc(size);
  memcpy(b->data, data, size);
  b->width = width;
  b->height = height;
  return b;
#endif
  return 0;
}

/*****************************************************************************/
void *
ui_create_bitmap_ex(int width, int height, uint8 * data, int data_size,
                    int compressed)
{
  struct bitmap * b;
  int size;
  int i;
  int j;
  int pixel;
  int red;
  int green;
  int blue;
  unsigned short * s16;
  unsigned char * s8;
  unsigned int * d32;

  size = width * height * 4;
  b = (struct bitmap *) xmalloc(sizeof(struct bitmap));
  b->data = (uint8 *) xmalloc(size);
  if (compressed)
  {
    bitmap_decompress_ex(b->data, width, height, data, data_size,
                         g_server_depth, 32);
  }
  else
  {
    if (g_server_depth == 16 && g_bs_bpp == 32)
    {
      for (i = 0; i < height; i++)
      {
        s16 = ((unsigned short *) data) + (i * width);
        d32 = ((unsigned int *) b->data) + (height - i - 1) * width;
        for (j = 0; j < width; j++)
        {
          pixel = *s16;
          s16++;
          SPLIT_COLOUR16(pixel, red, green, blue);
          MAKE_COLOUR32(pixel, red, green, blue);
          *d32 = pixel;
          d32++;
        }
      }
    }  
    else if (g_server_depth == 24 && g_bs_bpp == 32)
    {
      for (i = 0; i < height; i++)
      {
        s8 = ((unsigned char *) data) + (i * width * 3);
        d32 = ((unsigned int *) b->data) + (height - i - 1) * width;
        for (j = 0; j < width; j++)
        {
          blue = *s8;
          s8++;
          green = *s8;
          s8++;
          red = *s8;
          s8++;
          MAKE_COLOUR32(pixel, red, green, blue);
          *d32 = pixel;
          d32++;
        }
      }
    }  
  }
  b->width = width;
  b->height = height;
  return b;
}

/*****************************************************************************/
void
ui_destroy_bitmap(void * bmp)
{
  struct bitmap * b;

  b = (struct bitmap *) bmp;
  if (b != 0)
  {
    xfree(b->data);
  }
  xfree(b);
}

/*****************************************************************************/
void
ui_paint_bitmap(int x, int y, int cx, int cy,
                int width, int height, uint8 * data)
{
/*  struct bitmap b;

  b.width = width;
  b.height = height;
  b.data = data;
  ui_memblt(12, x, y, cx, cy, &b, 0, 0); */
}

/*****************************************************************************/
void
ui_paint_bitmap_ex(int x, int y, int cx, int cy,
                   int width, int height, uint8 * data, int data_size,
                   int compressed)
{
  struct bitmap * b;

  b = ui_create_bitmap_ex(width, height, data, data_size, compressed);
  ui_memblt(12, x, y, cx, cy, b, 0, 0);
  ui_destroy_bitmap(b);
}

/*****************************************************************************/
void
ui_set_clip(int x, int y, int cx, int cy)
{
  bs_set_clip(x, y, cx, cy);
  mi_set_clip(x, y, cx, cy);
}

/*****************************************************************************/
void
ui_reset_clip(void)
{
  bs_reset_clip();
  mi_reset_clip();
}

/*****************************************************************************/
void *
ui_create_colourmap(COLOURMAP * colours)
{
  int i;
  int n;

  n = MIN(256, colours->ncolours);
  memset(pal_entries, 0, sizeof(pal_entries));
  for (i = 0; i < n; i++)
  {
    pal_entries[i] = (colours->colours[i].red << 16) |
                     (colours->colours[i].green << 8) |
                     colours->colours[i].blue;
  }
  return 0;
}

/*****************************************************************************/
void
ui_set_colourmap(void * map)
{
}

/*****************************************************************************/
/* don't convert colour here */
static void
draw_glyph(int x, int y, void * glyph, int fgcolor)
{
  struct bitmap * b;

  b = glyph;
  bs_draw_glyph(x, y, b->data, b->width, b->height, fgcolor);
}

/*****************************************************************************/
#define DO_GLYPH(ttext,idx) \
{ \
  glyph = cache_get_font(font, ttext[idx]); \
  if (!(flags & TEXT2_IMPLICIT_X)) \
  { \
    xyoffset = ttext[++idx]; \
    if (xyoffset & 0x80) \
    { \
      if (flags & TEXT2_VERTICAL) \
      { \
        y += ttext[idx + 1] | (ttext[idx + 2] << 8); \
      } \
      else \
      { \
        x += ttext[idx + 1] | (ttext[idx + 2] << 8); \
      } \
      idx += 2; \
    } \
    else \
    { \
      if (flags & TEXT2_VERTICAL) \
      { \
        y += xyoffset; \
      } \
      else \
      { \
        x += xyoffset; \
      } \
    } \
  } \
  if (glyph != NULL) \
  { \
    draw_glyph(x + glyph->offset, y + glyph->baseline, glyph->pixmap, \
               fgcolour); \
    if (flags & TEXT2_IMPLICIT_X) \
    { \
      x += glyph->width; \
    } \
  } \
}

/*****************************************************************************/
void
ui_draw_text(uint8 font, uint8 flags, uint8 opcode, int mixmode,
             int x, int y,
             int clipx, int clipy, int clipcx, int clipcy,
             int boxx, int boxy, int boxcx, int boxcy, BRUSH * brush,
             int bgcolour, int fgcolour, uint8 * text, uint8 length)
{
  int i;
  int j;
  int xyoffset;
  DATABLOB * entry;
  FONTGLYPH * glyph;

  fgcolour = convert_colour(fgcolour);
  bgcolour = convert_colour(bgcolour);
  if (boxx + boxcx > g_width)
  {
    boxcx = g_width - boxx;
  }
  if (boxcx > 1)
  {
    bs_rect(boxx, boxy, boxcx, boxcy, bgcolour, 0xc);
  }
  else
  {
    if (mixmode == MIX_OPAQUE)
    {
      bs_rect(clipx, clipy, clipcx, clipcy, bgcolour, 0xc);
    }
  }
  /* Paint text, character by character */
  for (i = 0; i < length;)
  {
    switch (text[i])
    {
      case 0xff:
        if (i + 2 < length)
        {
          cache_put_text(text[i + 1], text, text[i + 2]);
        }
        else
        {
          error("this shouldn't be happening\n");
          exit(1);
        }
        /* this will move pointer from start to first character after */
        /* FF command */
        length -= i + 3;
        text = &(text[i + 3]);
        i = 0;
        break;
      case 0xfe:
        entry = cache_get_text(text[i + 1]);
        if (entry != NULL)
        {
          if ((((uint8 *) (entry->data))[1] == 0) &&
                              (!(flags & TEXT2_IMPLICIT_X)))
          {
            if (flags & TEXT2_VERTICAL)
            {
              y += text[i + 2];
            }
            else
            {
              x += text[i + 2];
            }
          }
          for (j = 0; j < entry->size; j++)
          {
            DO_GLYPH(((uint8 *) (entry->data)), j);
          }
        }
        if (i + 2 < length)
        {
          i += 3;
        }
        else
        {
          i += 2;
        }
        length -= i;
        /* this will move pointer from start to first character after */
        /* FE command */
        text = &(text[i]);
        i = 0;
        break;
      default:
        DO_GLYPH(text, i);
        i++;
        break;
    }
  }
  if (boxcx > 1)
  {
    ui_invalidate(boxx, boxy, boxcx, boxcy);
  }
  else
  {
    ui_invalidate(clipx, clipy, clipcx, clipcy);
  }
}

/*****************************************************************************/
void
ui_line(uint8 opcode, int startx, int starty, int endx, int endy,
        PEN * pen)
{
  int x;
  int y;
  int cx;
  int cy;
  int colour;

  colour = convert_colour(pen->colour);

  bs_line(opcode, startx, starty, endx, endy, pen->width, pen->style,
          colour);
  if (pen->style == 0 && pen->width < 2 && opcode == 12)
  {
    mi_line(startx, starty, endx, endy, colour);
  }
  else
  {
    x = MIN(startx, endx);
    y = MIN(starty, endy);
    cx = (MAX(startx, endx) + 1) - x;
    cy = (MAX(starty, endy) + 1) - y;
    ui_invalidate(x, y, cx, cy);
  }
}

/*****************************************************************************/
void
ui_triblt(uint8 opcode, int x, int y, int cx, int cy,
          void * src, int srcx, int srcy,
          BRUSH* brush, int bgcolour, int fgcolour)
{
  /* not used */
}

/*****************************************************************************/
void
ui_memblt(uint8 opcode, int x, int y, int cx, int cy,
          void * src, int srcx, int srcy)
{
  struct bitmap* b;

  b = (struct bitmap *) src;
  bs_memblt(opcode, x, y, cx, cy, b->data, b->width, b->height,
            srcx, srcy);
  ui_invalidate(x, y, cx, cy);
}

/*****************************************************************************/
void
ui_desktop_restore(uint32 offset, int x, int y, int cx, int cy)
{
}

/*****************************************************************************/
void
ui_desktop_save(uint32 offset, int x, int y, int cx, int cy)
{
}

/*****************************************************************************/
void
ui_rect(int x, int y, int cx, int cy, int colour)
{
  colour = convert_colour(colour);
  bs_rect(x, y, cx, cy, colour, 12);
  mi_fill_rect(x, y, cx, cy, colour);
}

/*****************************************************************************/
void
ui_screenblt(uint8 opcode, int x, int y, int cx, int cy,
             int srcx, int srcy)
{
  bs_screenblt(opcode, x, y, cx, cy, srcx, srcy);
  if (opcode == 12)
  {
    mi_screen_copy(x, y, cx, cy, srcx, srcy);
  }
  else
  {
    ui_invalidate(x, y, cx, cy);
  }
}

/*****************************************************************************/
void
ui_patblt(uint8 opcode, int x, int y, int cx, int cy,
          BRUSH * brush, int bgcolour, int fgcolour)
{
  bgcolour = convert_colour(bgcolour);
  fgcolour = convert_colour(fgcolour);
  bs_patblt(opcode, x, y, cx, cy, brush->style, brush->pattern,
            brush->xorigin, brush->yorigin, bgcolour, fgcolour);
  ui_invalidate(x, y, cx, cy);
}

/*****************************************************************************/
void
ui_destblt(uint8 opcode, int x, int y, int cx, int cy)
{
  bs_rect(x, y, cx, cy, 0, opcode);
  ui_invalidate(x, y, cx, cy);
  /* todo */
}

/*****************************************************************************/
void
ui_move_pointer(int x, int y)
{
}

/*****************************************************************************/
uint16
ui_get_numlock_state(uint32 state)
{
  return (uint16) state;
}

/*****************************************************************************/
/* get the num, caps, and scroll lock state */
/* scroll lock is 1, num lock is 2 and caps lock is 4 */
/* just returning 0, the hardware specific file is responsable for this */
uint32
read_keyboard_state(void)
{
  return (uint32) mi_read_keyboard_state();
}

/*****************************************************************************/
void
ui_set_modifier_state(int code)

{

  //error("%8.8x", code);

  rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, (uint16) code, 0);

}

/*****************************************************************************/
void
ui_resize_window(void)
{
}

/*****************************************************************************/
void
ui_begin_update(void)
{
  mi_begin_update();
}

/*****************************************************************************/
void
ui_end_update(void)
{
  mi_end_update();
}

/*****************************************************************************/
void
ui_polygon(uint8 opcode, uint8 fillmode, POINT * point, int npoints,
           BRUSH * brush, int bgcolour, int fgcolour)
{
  /* not used */
}

/*****************************************************************************/
void
ui_polyline(uint8 opcode, POINT * points, int npoints, PEN * pen)
{
  int i, x, y, dx, dy;
  if (npoints > 0)
  {
    x = points[0].x;
    y = points[0].y;
    for (i = 1; i < npoints; i++)
    {
      dx = points[i].x;
      dy = points[i].y;
      ui_line(opcode, x, y, x + dx, y + dy, pen);
      x = x + dx;
      y = y + dy;
    }
  }
}

/*****************************************************************************/
void
ui_ellipse(uint8 opcode, uint8 fillmode,
           int x, int y, int cx, int cy,
           BRUSH * brush, int bgcolour, int fgcolour)
{
  /* not used */
}

/*****************************************************************************/
/* get a 32 byte random */
void
generate_random(uint8 * random)
{
  int i;

  rand();
  rand();
  for (i = 0; i < 32; i++)
  {
    random[i] = rand() >> 16; /* higher bits are more random */
  }
}

/*****************************************************************************/
void
save_licence(uint8 * data, int length)
{
}

/*****************************************************************************/
int
load_licence(uint8 ** data)
{
  return 0;
}

/*****************************************************************************/
void *
xrealloc(void * in, int size)
{
  if (size < 1)
  {
    size = 1;
  }
  return realloc(in, size);
}

/*****************************************************************************/
void *
xmalloc(int size)
{
  if (size < 1)
  {
    size = 1;
  }
  return malloc(size);
}

/*****************************************************************************/
void
xfree(void * in)
{
  if (in != 0)
  {
    free(in);
  }
}

/*****************************************************************************/
char *
xstrdup(const char * s)
{
  int len;
  char * p;

  if (s == 0)
  {
    return 0;
  }
  len = strlen(s);
  p = (char *) xmalloc(len + 1);
  strcpy(p, s);
  return p;
}

/*****************************************************************************/
void
warning(char * format, ...)
{
  va_list ap;
  char text[512];
  char text1[512];

  sprintf(text1, "WARNING: ");
  va_start(ap, format);
  vsprintf(text, format, ap);
  va_end(ap);
  strcat(text1, text);
  mi_warning(text1);
}

/*****************************************************************************/
void
unimpl(char * format, ...)
{
  va_list ap;
  char text[512];
  char text1[512];

  sprintf(text1, "UNIMPL: ");
  va_start(ap, format);
  vsprintf(text, format, ap);
  va_end(ap);
  strcat(text1, text);
  mi_warning(text1);
}

/*****************************************************************************/
void
error(char * format, ...)
{
  va_list ap;
  char text[512];
  char text1[512];

  sprintf(text1, "ERROR: ");
  va_start(ap, format);
  vsprintf(text, format, ap);
  va_end(ap);
  strcat(text1, text);
  mi_error(text1);
}

/*****************************************************************************/
BOOL
rd_pstcache_mkdir(void)
{
  return 0;
}

/*****************************************************************************/
int
rd_open_file(char * filename)
{
  return 0;
}

/*****************************************************************************/
void
rd_close_file(int fd)
{
  return;
}

/*****************************************************************************/
int
rd_read_file(int fd, void * ptr, int len)
{
  return 0;
}

/*****************************************************************************/
int
rd_write_file(int fd, void * ptr, int len)
{
  return 0;
}

/*****************************************************************************/
int
rd_lseek_file(int fd, int offset)
{
  return 0;
}

/*****************************************************************************/
BOOL
rd_lock_file(int fd, int start, int len)
{
  return False;
}


/*****************************************************************************/
void
ui_mouse_move(int x, int y)
{
  rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_MOVE, (uint16) x, (uint16) y);
}


/*****************************************************************************/
void
ui_mouse_button(int button, int x, int y, int down)
{
  uint16 flags;

  flags = 0;
  if (down)
  {
    flags |= MOUSE_FLAG_DOWN;
  }
  switch (button)
  {
    case 1:
      flags |= MOUSE_FLAG_BUTTON1;
      break;
    case 2:
      flags |= MOUSE_FLAG_BUTTON2;
      break;
    case 3:
      flags |= MOUSE_FLAG_BUTTON3;
      break;
    case 4:
      flags |= MOUSE_FLAG_BUTTON4;
      break;
    case 5:
      flags |= MOUSE_FLAG_BUTTON5;
      break;
  }
  rdp_send_input(0, RDP_INPUT_MOUSE, flags, (uint16) x, (uint16) y);
}


/*****************************************************************************/
void
ui_key_down(int key, int ext)

{
  rdp_send_input(0, RDP_INPUT_SCANCODE, (uint16) (RDP_KEYPRESS | ext),
                 (uint16) key, 0);
}

/*****************************************************************************/
void
ui_key_up(int key, int ext)
{
  rdp_send_input(0, RDP_INPUT_SCANCODE, (uint16) (RDP_KEYRELEASE | ext),
                 (uint16) key, 0);
}

/*****************************************************************************/
/* returns boolean, non zero is good */
int
ui_read_wire(void)
{
  return rdp_loop(&g_deactivated, &g_ext_disc_reason);
}

/*****************************************************************************/
/* called after the command line parameters are processed */
/* returns boolean, non zero is ok */
int
ui_main(void)
{
  uint32 flags;

  /* try to connect */
  flags = RDP_LOGON_NORMAL;
  if (g_password[0] != 0)
  {
    flags |= RDP_LOGON_AUTO;
  }
  if (!rdp_connect(g_servername, flags, g_domain, g_password,
                   g_shell, g_directory))
  {
    return 0;
  }
  /* create the window */
  if (!mi_create_window())
  {
    error("mi_create_window failed\r\n");
    return 0;
  }
  /* create backingstore stuff for use in bsops.c */
  if (!mi_create_bs())
  {
    error("mi_create_bs failed\r\n");
    return 0;
  }
  /* init backingstore */
  bs_init();
  /* if all ok, enter main loop */
  return mi_main_loop();
}

/*****************************************************************************/
/* produce a hex dump */
void
hexdump(uint8 * p, uint32 len)
{
  uint8 * line = p;
  int i, thisline, offset = 0;

  while (offset < (int)len)
  {
    printf("%04x ", offset);
    thisline = len - offset;
    if (thisline > 16)
      thisline = 16;

    for (i = 0; i < thisline; i++)
      printf("%02x ", line[i]);

    for (; i < 16; i++)
      printf("   ");

    for (i = 0; i < thisline; i++)
      printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');

    printf("\n");
    offset += thisline;
    line += thisline;
  }
}