[go: up one dir, main page]

Menu

[r664]: / tags / 0.1 / stubs.m  Maximize  Restore  History

Download this file

975 lines (803 with data), 23.4 kB

  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
/*
* stubs.c
* Remote Desktop
*
* Created by Craig Dooley on 5/10/06.
*/
// Copyright (c) 2006 Craig Dooley <xlnxminusx@gmail.com>
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// 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
// AUTHORS OR COPYRIGHT HOLDERS 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.
#import <Cocoa/Cocoa.h>
#import <sys/types.h>
#import <sys/time.h>
#import <dirent.h>
#import <stdio.h>
#import "constants.h"
#import "parse.h"
#import "types.h"
#import "proto.h"
#import "RDCView.h"
#import "RDCBitmap.h"
#import <openssl/md5.h>
#import <sys/stat.h>
#import <sys/types.h>
#import <sys/times.h>
#define UNIMPL NSLog(@"Unimplemented: %s", __func__)
#define CHECKOPCODE(x) if ((x != 12) && (x < 16)) {NSLog(@"Unimplemented opcode %d in function %s", x, __func__);}
/* Opcodes
GXclear, 0 0
GXnor, 1 !src & !dst
GXandInverted, 2 !src & dst
GXcopyInverted, 3 !src
GXandReverse, 4 src & !dst
GXinvert, 5 !dst
GXxor, 6 src ^ dst
GXnand, 7 !src | !dst
GXand, 8 src & dst
GXequiv, 9 !src ^ dst
GXnoop, 10 dst
GXorInverted, 11 !src | dst
GXcopy, 12 src
GXorReverse, 13 src | !dst
GXor, 14 src or dst
GXset 15 1
*/
static RDCBitmap *nullCursor = nil;
int ui_select(int socket) {
return 1;
}
/* Tells whether numlock is on or off */
unsigned int read_keyboard_state() {
return 0;
}
unsigned short ui_get_numlock_state(unsigned int state) {
return 0;
}
#pragma mark Colormap functions
HCOLOURMAP ui_create_colourmap(COLOURMAP * colors) {
NSMutableArray *array;
int i, color;
array = [[NSMutableArray alloc] init];
for (i = 0; i < colors->ncolours; i++) {
COLOURENTRY centry = colors->colours[i];
color = (centry.red << 16) | (centry.green << 8) | (centry.blue);
[array insertObject:[NSNumber numberWithInt:color] atIndex:i];
}
return array;
}
void ui_set_colourmap(rdcConnection conn, HCOLOURMAP map) {
RDCView *v = conn->ui;
[v setColorMap:(NSArray *)map];
}
#pragma mark Bitmap functions
HBITMAP ui_create_bitmap(rdcConnection conn, int width, int height, uint8 *data) {
RDCBitmap *bitmap = [[RDCBitmap alloc] init];
[bitmap bitmapWithData:data size:NSMakeSize(width, height) view:conn->ui];
return bitmap;
}
void ui_paint_bitmap(rdcConnection conn, int x, int y, int cx, int cy, int width, int height, uint8 * data) {
RDCBitmap *bitmap = [[RDCBitmap alloc] init];
[bitmap bitmapWithData:data size:NSMakeSize(width, height) view:conn->ui];
ui_memblt(conn, 0, x, y, cx, cy, bitmap, 0, 0);
[bitmap release];
}
void ui_memblt(rdcConnection conn, uint8 opcode, int x, int y, int cx, int cy, HBITMAP src, int srcx, int srcy) {
RDCView *v = conn->ui;
RDCBitmap *bmp = (RDCBitmap *)src;
NSRect r = NSMakeRect(x, y, cx, cy);
NSPoint p = NSMakePoint(srcx, srcy);
if (opcode != 0) {
/* Treat opcode 0 just like copy */
CHECKOPCODE(opcode);
}
[v memblt:r from:[bmp image] withOrigin:p];
[v setNeedsDisplayInRect:r];
}
void ui_destroy_bitmap(HBITMAP bmp) {
id image = bmp;
[image release];
}
# pragma mark Drawing functions
void ui_rect(rdcConnection conn, int x, int y, int cx, int cy, int colour) {
RDCView *v = conn->ui;
NSRect r = NSMakeRect(x , y, cx, cy);
[v setForeground:[v nscolorForRDCColor:colour]];
[v fillRect:r];
[v setNeedsDisplayInRect:r];
}
void ui_line(rdcConnection conn, uint8 opcode, int startx, int starty,
int endx, int endy, PEN * pen) {
RDCView *v = conn->ui;
NSPoint start = NSMakePoint(startx + 0.5, starty + 0.5);
NSPoint end = NSMakePoint(endx + 0.5, endy + 0.5);
[v setForeground:[v nscolorForRDCColor:pen->colour]];
if (opcode == 15) {
[v drawLineFrom:start to:end color:[NSColor whiteColor] width:pen->width];
return;
}
CHECKOPCODE(opcode);
/* XXX better rectangle finding for setneedsdisplay */
[v setForeground:[v nscolorForRDCColor:pen->colour]];
[v drawLineFrom:start to:end color:[v nscolorForRDCColor:pen->colour] width:pen->width];
[v setNeedsDisplay:YES];
}
#pragma mark Desktop functions
void ui_desktop_save(rdcConnection conn, uint32 offset, int x, int y, int cx, int cy) {
RDCView *v = conn->ui;
[v saveDesktop];
}
void ui_desktop_restore(rdcConnection conn, uint32 offset, int x, int y, int cx, int cy) {
RDCView *v = conn->ui;
[v restoreDesktop:NSMakeRect(x, y, cx, cy)];
[v setNeedsDisplayInRect:NSMakeRect(x, y, cx, cy)];
}
#pragma mark Text functions
HGLYPH ui_create_glyph(rdcConnection conn, int width, int height, const uint8 *data) {
RDCBitmap *image = [[RDCBitmap alloc] init];
[image glyphWithData:data size:NSMakeSize(width, height) view:conn->ui];
return image;
}
void ui_destroy_glyph(HGLYPH glyph) {
id image = glyph;
[image release];
}
static void ui_drawglyph(rdcConnection conn, int x, int y, int w, int h, RDCBitmap *glyph, int fgcolor, int bgcolor);
void ui_drawglyph(rdcConnection conn, int x, int y, int w, int h, RDCBitmap *glyph, int fgcolor, int bgcolor) {
RDCView *v = conn->ui;
NSRect r = NSMakeRect(x, y, w, h);
[v drawGlyph:glyph at:r fg:fgcolor bg:bgcolor];
[v setNeedsDisplayInRect:r];
}
#define DO_GLYPH(ttext,idx) \
{\
glyph = cache_get_font (conn, 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)\
{\
x1 = x + glyph->offset;\
y1 = y + glyph->baseline;\
ui_drawglyph(conn, x1, y1, glyph->width, glyph->height, glyph->pixmap, fgcolour, bgcolour); \
if (flags & TEXT2_IMPLICIT_X)\
x += glyph->width;\
}\
}
void ui_draw_text(rdcConnection conn, 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 = 0, j;
int xyoffset;
int x1, y1;
FONTGLYPH *glyph;
DATABLOB *entry;
RDCView *v = conn->ui;
NSRect box;
CHECKOPCODE(opcode);
if (boxx + boxcx >= [v width]) {
boxcx = [v width] - boxx;
}
[v setForeground:[v nscolorForRDCColor:bgcolour]];
if (boxcx > 1) {
box = NSMakeRect(boxx, boxy, boxcx, boxcy);
[v fillRect:box];
[v setNeedsDisplayInRect:box];
} else if (mixmode == MIX_OPAQUE) {
box = NSMakeRect(clipx, clipy, clipcx, clipcy);
[v fillRect:box];
[v setNeedsDisplayInRect:box];
}
[v setForeground:[v nscolorForRDCColor:fgcolour]];
[v setBackground:[v nscolorForRDCColor:bgcolour]];
[v startUpdate];
/* Paint text, character by character */
for (i = 0; i < length;) {
switch (text[i]) {
case 0xff:
if (i + 2 < length) {
cache_put_text(conn, 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(conn, 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;
}
}
[v stopUpdate];
}
#pragma mark Clipping functions
void ui_set_clip(rdcConnection conn, int x, int y, int cx, int cy) {
RDCView *v = conn->ui;
[v setClip:NSMakeRect(x, y, cx, cy)];
}
void ui_reset_clip(rdcConnection conn) {
RDCView *v = conn->ui;
[v resetClip];
}
void ui_bell(void) {
NSBeep();
}
#pragma mark Cursor functions
HCURSOR ui_create_cursor(rdcConnection conn, unsigned int x, unsigned int y, int width, int height, uint8 * andmask, uint8 * xormask) {
RDCBitmap *cursor = [[RDCBitmap alloc] init];
[cursor cursorWithData:andmask
alpha:xormask
size:NSMakeSize(width, height)
hotspot:NSMakePoint(x, y)
view:conn->ui];
return cursor;
}
void ui_set_null_cursor(rdcConnection conn) {
if (nullCursor == nil) {
nullCursor = ui_create_cursor(conn, 0, 0, 0, 0, NULL, NULL);
}
ui_set_cursor(nullCursor);
}
void ui_set_cursor(HCURSOR cursor) {
id c = (RDCBitmap *)cursor;
[[c cursor] set];
}
void ui_destroy_cursor(HCURSOR cursor) {
id c = (RDCBitmap *)cursor;
[c release];
}
char *
next_arg(char *src, char needle)
{
char *nextval;
char *p;
char *mvp = 0;
/* EOS */
if (*src == (char) 0x00)
return 0;
p = src;
/* skip escaped needles */
while ((nextval = strchr(p, needle)))
{
mvp = nextval - 1;
/* found backslashed needle */
if (*mvp == '\\' && (mvp > src))
{
/* move string one to the left */
while (*(mvp + 1) != (char) 0x00)
{
*mvp = *(mvp + 1);
*mvp++;
}
*mvp = (char) 0x00;
p = nextval;
}
else
{
p = nextval + 1;
break;
}
}
/* more args available */
if (nextval)
{
*nextval = (char) 0x00;
return ++nextval;
}
/* no more args after this, jump to EOS */
nextval = src + strlen(src);
return nextval;
}
void toupper_str(char *p)
{
while (*p)
{
if ((*p >= 'a') && (*p <= 'z'))
*p = toupper((int) *p);
p++;
}
}
void ui_screenblt(rdcConnection conn, uint8 opcode, int x, int y, int cx, int cy, int srcx, int srcy) {
RDCView *v = conn->ui;
NSRect src = NSMakeRect(srcx, srcy, cx, cy);
NSPoint dest = NSMakePoint(x, y);
CHECKOPCODE(opcode);
[v screenBlit:src to:dest];
[v setNeedsDisplayInRect:NSMakeRect(x, y, cx, cy)];
}
void ui_destblt(rdcConnection conn, uint8 opcode, int x, int y, int cx, int cy) {
RDCView *v = conn->ui;
NSRect r = NSMakeRect(x, y, cx, cy);
/* XXX */
if (opcode == 5) {
[v swapRect:r];
[v setNeedsDisplayInRect:r];
return;
}
if (opcode == 15) {
[v fillRect:r withColor:[NSColor whiteColor]];
[v setNeedsDisplayInRect:r];
return;
}
if (opcode != 0) {
CHECKOPCODE(opcode);
}
[v fillRect:r];
[v setNeedsDisplayInRect:r];
}
void ui_polyline(rdcConnection conn, uint8 opcode, POINT * points, int npoints, PEN *pen) {
RDCView *v = conn->ui;
CHECKOPCODE(opcode);
[v polyline:points npoints:npoints color:[v nscolorForRDCColor:pen->colour] width:pen->width];
[v setNeedsDisplay:YES];
}
void ui_polygon(rdcConnection conn, uint8 opcode, uint8 fillmode, POINT * point, int npoints, BRUSH *brush, int bgcolour, int fgcolour) {
RDCView *v = conn->ui;
NSWindingRule r;
int style;
CHECKOPCODE(opcode);
switch (fillmode) {
case ALTERNATE:
r = NSEvenOddWindingRule;
break;
case WINDING:
r = NSNonZeroWindingRule;
break;
default:
UNIMPL;
return;
}
if (brush) {
style = brush->style;
} else {
style = 0;
}
switch(style) {
case 0:
[v setForeground:[v nscolorForRDCColor:fgcolour]];
[v polygon:point npoints:npoints color:[v nscolorForRDCColor:fgcolour] winding:r];
break;
default:
UNIMPL;
break;
}
[v setNeedsDisplay:YES];
}
void ui_move_pointer(int x, int y) {
UNIMPL;
}
static const uint8 hatch_patterns[] = {
0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, /* 0 - bsHorizontal */
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, /* 1 - bsVertical */
0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, /* 2 - bsFDiagonal */
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, /* 3 - bsBDiagonal */
0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08, /* 4 - bsCross */
0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 /* 5 - bsDiagCross */
};
/* XXX Still needs origins */
void ui_patblt(rdcConnection conn, uint8 opcode, int x, int y, int cx, int cy, BRUSH * brush, int bgcolor, int fgcolor) {
RDCView *v = conn->ui;
NSRect dest = NSMakeRect(x, y, cx, cy);
NSRect glyphSize;
RDCBitmap *bitmap;
NSImage *fill;
NSColor *fillColor;
uint8 i, ipattern[8];
if(opcode == 6) {
[v swapRect:dest];
[v setNeedsDisplayInRect:dest];
return;
}
CHECKOPCODE(opcode);
switch (brush->style) {
case 0: /* Solid */
NSLog(@"fg %d bg %d opcode %d", fgcolor, bgcolor, opcode);
[v fillRect:dest withColor:[v nscolorForRDCColor:fgcolor]];
break;
case 2: /* Hatch */
glyphSize = NSMakeRect(0, 0, 8, 8);
bitmap = ui_create_glyph(conn, 8, 8, hatch_patterns + brush->pattern[0] * 8);
fill = [bitmap image];
[fill lockFocus];
[[NSGraphicsContext currentContext] setCompositingOperation:NSCompositeSourceAtop];
[[v nscolorForRDCColor:fgcolor] set];
[NSBezierPath fillRect:glyphSize];
[[NSGraphicsContext currentContext] setCompositingOperation:NSCompositeDestinationAtop];
[[v nscolorForRDCColor:bgcolor] set];
[NSBezierPath fillRect:glyphSize];
[fill unlockFocus];
fillColor = [NSColor colorWithPatternImage:fill];
[v fillRect:dest withColor:fillColor patternOrigin:NSMakePoint(brush->xorigin, brush->yorigin)];
ui_destroy_glyph(bitmap);
break;
case 3: /* Pattern */
for (i = 0; i != 8; i++) {
ipattern[7 - i] = brush->pattern[i];
}
glyphSize = NSMakeRect(0, 0, 8, 8);
bitmap = ui_create_glyph(conn, 8, 8, ipattern);
fill = [bitmap image];
[fill lockFocus];
[[NSGraphicsContext currentContext] setCompositingOperation:NSCompositeSourceAtop];
[[v nscolorForRDCColor:fgcolor] set];
[NSBezierPath fillRect:glyphSize];
[[NSGraphicsContext currentContext] setCompositingOperation:NSCompositeDestinationAtop];
[[v nscolorForRDCColor:bgcolor] set];
[NSBezierPath fillRect:glyphSize];
[fill unlockFocus];
fillColor = [NSColor colorWithPatternImage:fill];
[v fillRect:dest withColor:fillColor patternOrigin:NSMakePoint(brush->xorigin, brush->yorigin)];
ui_destroy_glyph(bitmap);
break;
default:
unimpl("brush %d\n", brush->style);
break;
}
[v setNeedsDisplayInRect:dest];
}
void *
xmalloc(int size)
{
void *mem = malloc(size);
if (mem == NULL)
{
error("xmalloc %d\n", size);
exit(1);
}
return mem;
}
/* realloc; exit if out of memory */
void *
xrealloc(void *oldmem, int size)
{
void *mem;
if (size < 1)
size = 1;
mem = realloc(oldmem, size);
if (mem == NULL)
{
error("xrealloc %d\n", size);
exit(1);
}
return mem;
}
/* free */
void
xfree(void *mem)
{
free(mem);
}
/* report an error */
void
error(char *format, ...)
{
va_list ap;
fprintf(stderr, "ERROR: ");
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
/* report a warning */
void
warning(char *format, ...)
{
va_list ap;
fprintf(stderr, "WARNING: ");
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
/* report an unimplemented protocol feature */
void
unimpl(char *format, ...)
{
va_list ap;
fprintf(stderr, "NOT IMPLEMENTED: ");
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
/* produce a hex dump */
void
hexdump(unsigned char *p, unsigned int len)
{
unsigned char *line = p;
int i, thisline, offset = 0;
while (offset < 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;
}
}
/* Generate a 32-byte random for the secure transport code. */
void
generate_random(uint8 * random)
{
struct stat st; struct tms tmsbuf;
MD5_CTX md5;
uint32 *r;
int fd, n;
/* If we have a kernel random device, try that first */
if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
|| ((fd = open("/dev/random", O_RDONLY)) != -1))
{
n = read(fd, random, 32);
close(fd);
if (n == 32)
return;
}
#ifdef EGD_SOCKET
/* As a second preference use an EGD */
if (generate_random_egd(random))
return;
#endif
/* Otherwise use whatever entropy we can gather - ideas welcome. */
r = (uint32 *) random;
r[0] = (getpid()) | (getppid() << 16);
r[1] = (getuid()) | (getgid() << 16);
r[2] = times(&tmsbuf); /* system uptime (clocks) */
gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
stat("/tmp", &st);
r[5] = st.st_atime;
r[6] = st.st_mtime;
r[7] = st.st_ctime;
/* Hash both halves with MD5 to obscure possible patterns */
MD5_Init(&md5);
MD5_Update(&md5, random, 16);
MD5_Final(random, &md5);
MD5_Update(&md5, random + 16, 16);
MD5_Final(random + 16, &md5);
}
/* Create the bitmap cache directory */
int
rd_pstcache_mkdir(void)
{
char *home;
char bmpcache_dir[256];
home = getenv("HOME");
if (home == NULL)
return False;
sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop");
if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST)
{
perror(bmpcache_dir);
return False;
}
sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop/cache");
if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST)
{
perror(bmpcache_dir);
return False;
}
return True;
}
/* open a file in the .rdesktop directory */
int
rd_open_file(char *filename)
{
char *home;
char fn[256];
int fd;
home = getenv("HOME");
if (home == NULL)
return -1;
sprintf(fn, "%s/.rdesktop/%s", home, filename);
fd = open(fn, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (fd == -1)
perror(fn);
return fd;
}
/* close file */
void
rd_close_file(int fd)
{
close(fd);
}
/* read from file*/
int
rd_read_file(int fd, void *ptr, int len)
{
return read(fd, ptr, len);
}
/* write to file */
int
rd_write_file(int fd, void *ptr, int len)
{
return write(fd, ptr, len);
}
/* move file pointer */
int
rd_lseek_file(int fd, int offset)
{
return lseek(fd, offset, SEEK_SET);
}
/* do a write lock on a file */
int
rd_lock_file(int fd, int start, int len)
{
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = start;
lock.l_len = len;
if (fcntl(fd, F_SETLK, &lock) == -1)
return False;
return True;
}
void fillDefaultConnection(rdcConnection conn) {
NSString *host = [[NSProcessInfo processInfo] hostName];
conn->tcpPort = TCP_PORT_RDP;
conn->screenWidth = 1024;
conn->screenHeight = 768;
conn->isConnected = 0;
conn->useEncryption = 1;
conn->useBitmapCompression = 1;
conn->currentStatus = 1;
conn->useRdp5 = 1;
conn->serverBpp = 16;
conn->consoleSession = 0;
conn->bitmapCache = 1;
conn->bitmapCachePersist = 0;
conn->bitmapCachePrecache = 1;
conn->polygonEllipseOrders = 1;
conn->desktopSave = 1;
conn->serverRdpVersion = 1;
conn->keyLayout = 0x409;
conn->packetNumber = 0;
conn->licenseIssued = 0;
conn->pstcacheEnumerated = 0;
conn->rdpdrClientname = NULL;
conn->ioRequest = NULL;
conn->bmpcacheLru[0] = conn->bmpcacheLru[1] = conn->bmpcacheLru[2] = NOT_SET;
conn->bmpcacheMru[0] = conn->bmpcacheMru[1] = conn->bmpcacheMru[2] = NOT_SET;
memcpy(conn->hostname, [host UTF8String], [host length] + 1);
conn->rdp5PerformanceFlags = RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG | RDP5_NO_MENUANIMATIONS;
}
void ui_resize_window(void) {
UNIMPL;
}
#define LTOA_BUFSIZE (sizeof(long) * 8 + 1)
char *
l_to_a(long N, int base)
{
char *ret;
ret = malloc(LTOA_BUFSIZE);
char *head = ret, buf[LTOA_BUFSIZE], *tail = buf + sizeof(buf);
register int divrem;
if (base < 36 || 2 > base)
base = 10;
if (N < 0)
{
*head++ = '-';
N = -N;
}
tail = buf + sizeof(buf);
*--tail = 0;
do
{
divrem = N % base;
*--tail = (divrem <= 9) ? divrem + '0' : divrem + 'a' - 10;
N /= base;
}
while (N);
strcpy(head, tail);
return ret;
}
void ui_triblt(uint8 opcode,
int x, int y, int cx, int cy,
HBITMAP src, int srcx, int srcy,
BRUSH *brush, int bgcolour, int fgcolour) {
CHECKOPCODE(opcode);
UNIMPL;
}
void ui_ellipse(rdcConnection conn,
uint8 opcode,
uint8 fillmode,
int x, int y, int cx, int cy,
BRUSH *brush, int bgcolour, int fgcolour) {
RDCView *v = conn->ui;
NSRect r = NSMakeRect(x + 0.5, y + 0.5, cx, cy);
int style;
CHECKOPCODE(opcode);
if (brush) {
style = brush->style;
} else {
style = 0;
}
switch(style) {
case 0:
[v setForeground:[v nscolorForRDCColor:fgcolour]];
[v ellipse:r color:[v nscolorForRDCColor:fgcolour]];
break;
default:
UNIMPL;
break;
}
[v setNeedsDisplay:YES];
}
void save_licence(unsigned char *data, int length) {
UNIMPL;
}
int load_licence(unsigned char **data) {
UNIMPL;
return 0;
}
void ui_clip_format_announce(uint8 *data, uint32 length) {
UNIMPL;
}
void ui_clip_handle_data(uint8 *data, uint32 length) {
UNIMPL;
}
void ui_clip_request_data(uint32 format) {
UNIMPL;
}
void ui_clip_sync(void) {
UNIMPL;
}