[go: up one dir, main page]

Menu

[r664]: / tags / 0.3 / RDCView.m  Maximize  Restore  History

Download this file

553 lines (458 with data), 15.9 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
//
// RDCView.m
// Remote Desktop
//
// Created by Craig Dooley on 4/25/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 "RDCView.h"
#import "RDCKeyboard.h"
#import "RDCBitmap.h"
#import "RDInstance.h"
#import "constants.h"
#import "scancodes.h"
@interface RDCView (Private)
-(void)send_modifiers:(NSEvent *)ev enable:(BOOL)en;
@end
@implementation RDCView
#pragma mark NSView functions
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
back = [[NSImage alloc] initWithSize:frame.size];
[self resetClip];
[back lockFocus];
[[NSColor blackColor] set];
[NSBezierPath fillRect:frame];
[back unlockFocus];
cursor = [[NSCursor arrowCursor] retain];
[self addCursorRect:[self visibleRect] cursor:cursor];
auxiliaryViews = [[NSMutableArray alloc] init];
[attributes setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
}
return self;
}
-(void)setController:(RDInstance *)instance {
controller = instance;
bitdepth = [instance conn]->serverBpp;
}
-(BOOL)acceptsFirstResponder {
return YES;
}
-(BOOL)wantsDefaultClipping {
return NO;
}
- (void)drawRect:(NSRect)rect {
int nRects, i;
const NSRect* rects;
[self getRectsBeingDrawn:&rects count:&nRects];
for (i = 0; i < nRects; i++)
[back drawInRect:rects[i] fromRect:rects[i] operation:NSCompositeCopy fraction:1.0f];
}
-(BOOL)isFlipped {
return YES;
}
-(BOOL)isOpaque {
return YES;
}
-(void)resetCursorRects {
[self discardCursorRects];
NSRect r = [self visibleRect];
[self addCursorRect:r cursor:cursor];
}
#pragma mark NSObject functions
-(void)dealloc {
[cursor release];
[auxiliaryViews release];
[super dealloc];
}
#pragma mark Remote Desktop methods
-(void)startUpdate {
[back lockFocus];
}
-(void)stopUpdate {
[back unlockFocus];
}
-(void)ellipse:(NSRect)r color:(NSColor *)c {
[back lockFocus];
NSRectClip(clipRect);
[c set];
[[NSBezierPath bezierPathWithOvalInRect:r] fill];
[back unlockFocus];
}
-(void)polygon:(POINT *)points npoints:(int)nPoints color:(NSColor *)c
winding:(NSWindingRule)winding {
NSBezierPath *bp = [NSBezierPath bezierPath];
int i;
[bp moveToPoint:NSMakePoint(points[0].x + 0.5, points[0].y + 0.5)];
for (i = 1; i < nPoints; i++) {
[bp relativeLineToPoint:NSMakePoint(points[i].x, points[i].y)];
}
[bp closePath];
[back lockFocus];
NSRectClip(clipRect);
[c set];
[bp fill];
[back unlockFocus];
}
-(void)polyline:(POINT *)points npoints:(int)nPoints color:(NSColor *)c width:(int)w {
NSBezierPath *bp = [NSBezierPath bezierPath];
int i;
[bp moveToPoint:NSMakePoint(points[0].x + 0.5, points[0].y + 0.5)];
for (i = 1; i < nPoints; i++) {
[bp relativeLineToPoint:NSMakePoint(points[i].x, points[i].y)];
}
[bp setLineWidth:w];
[back lockFocus];
NSRectClip(clipRect);
[c set];
[bp stroke];
[back unlockFocus];
}
-(void)fillRect:(NSRect)rect {
[self fillRect:rect withColor:foregroundColor patternOrigin:NSZeroPoint];
}
-(void)fillRect:(NSRect)rect withColor:(NSColor *) color {
[self fillRect:rect withColor:color patternOrigin:NSZeroPoint];
}
-(void)fillRect:(NSRect)rect withColor:(NSColor *) color patternOrigin:(NSPoint)origin {
[back lockFocus];
NSRectClip(clipRect);
[color set];
[[NSGraphicsContext currentContext] setPatternPhase:origin];
[NSBezierPath fillRect:rect];
[back unlockFocus];
}
-(void)memblt:(NSRect)to from:(NSImage *)image withOrigin:(NSPoint)origin {
[back lockFocus];
NSRectClip(clipRect);
[image drawInRect:to
fromRect:NSMakeRect(origin.x, origin.y, to.size.width, to.size.height)
operation:NSCompositeCopy
fraction:1.0];
[back unlockFocus];
}
-(NSColor *)nscolorForRDCColor:(int)col {
int r, g, b, t;
if (bitdepth == 8) {
t = [[colorMap objectAtIndex:col] intValue];
r = (t >> 16) & 0xff;
g = (t >> 8) & 0xff;
b = t & 0xff;
} else if (bitdepth == 16) {
r = ((col >> 8) & 0xf8) | ((col >> 13) & 0x7);
g = ((col >> 3) & 0xfc) | ((col >> 9) & 0x3);
b =((col << 3) & 0xf8) | ((col >> 2) & 0x7);
} else if (bitdepth == 24) {
r = (col >> 16) & 0xff;
g = (col >> 8) & 0xff;
b = col & 0xff;
} else {
NSLog(@"Bitdepth = %d", bitdepth);
r = g = b = 0;
}
return [NSColor colorWithDeviceRed:(float)r / 255.0
green:(float)g / 255.0
blue:(float)b / 255.0
alpha:1.0];
}
-(void)screenBlit:(NSRect)from to:(NSPoint)to {
[back lockFocus];
NSRectClip(clipRect);
NSCopyBits(nil, from, to);
[back unlockFocus];
}
-(void)drawLineFrom:(NSPoint)start to:(NSPoint)end color:(NSColor *)color width:(int)width {
NSBezierPath *bp = [NSBezierPath bezierPath];
[back lockFocus];
NSRectClip(clipRect);
[color set];
[bp moveToPoint:start];
[bp lineToPoint:end];
[bp setLineWidth:width];
[bp stroke];
[back unlockFocus];
}
-(void)drawGlyph:(RDCBitmap *)glyph at:(NSRect)r fg:(int)fgcolor bg:(int)bgcolor {
NSColor *fg, *bg;
fg = [self nscolorForRDCColor:fgcolor];
bg = [self nscolorForRDCColor:bgcolor];
NSImage *image = [glyph image];
if (! [[glyph color] isEqual:fg]) {
[image lockFocus];
[[NSGraphicsContext currentContext] setCompositingOperation:NSCompositeSourceAtop];
[fg setFill];
[NSBezierPath fillRect:NSMakeRect(0, 0, [image size].width, [image size].height)];
[image unlockFocus];
[glyph setColor:fg];
}
NSRectClip(clipRect);
[image drawInRect:r
fromRect:NSMakeRect(0, 0, r.size.width, r.size.height)
operation:NSCompositeSourceOver
fraction:1.0];
}
-(void)setClip:(NSRect)r {
clipRect = r;
}
-(void)resetClip {
NSRect r = NSZeroRect;
r.size = [back size];
clipRect = r;
}
#pragma mark Event Handling Methods
-(void)keyDown:(NSEvent *)ev {
int key = [ev keyCode];
unsigned char scancode = [[RDCKeyboard shared] scancodeForKeycode:key];
// [self set_remote_modifiers:ev];
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYPRESS param1:scancode param2:0];
// [self restore_remote_modifiers:ev];
}
-(void)keyUp:(NSEvent *)ev {
int key = [ev keyCode];
unsigned char scancode = [[RDCKeyboard shared] scancodeForKeycode:key];
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYRELEASE param1:scancode param2:0];
}
-(void)flagsChanged:(NSEvent *)ev {
[self send_modifiers:ev enable:YES];
}
-(void)send_modifiers:(NSEvent *)ev enable:(BOOL)en {
int newflags = [ev modifierFlags];
int flag = newflags ^ modifiers;
// Caps lock only gets sent once, so send double send it for each event
if (flag & NSAlphaShiftKeyMask) {
if (newflags & NSAlphaShiftKeyMask) {
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYRELEASE param1:0x3a param2:0];
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYPRESS param1:0x3a param2:0];
} else {
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYRELEASE param1:0x3a param2:0];
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYPRESS param1:0x3a param2:0];
}
}
if (flag & NSShiftKeyMask) {
if (newflags & NSShiftKeyMask) {
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYPRESS param1:SCANCODE_CHAR_LSHIFT param2:0];
} else {
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYRELEASE param1:SCANCODE_CHAR_LSHIFT param2:0];
}
}
if (flag & NSControlKeyMask) {
if (newflags & NSControlKeyMask) {
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYPRESS param1:SCANCODE_CHAR_LCTRL param2:0];
} else {
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYRELEASE param1:SCANCODE_CHAR_LCTRL param2:0];
}
}
if (flag & NSAlternateKeyMask) {
if (newflags & NSAlternateKeyMask) {
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYPRESS param1:SCANCODE_CHAR_LALT param2:0];
} else {
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYRELEASE param1:SCANCODE_CHAR_LALT param2:0];
}
}
if (flag & NSCommandKeyMask) {
if (newflags & NSCommandKeyMask) {
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYPRESS param1:SCANCODE_CHAR_LWIN param2:0];
} else {
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYRELEASE param1:SCANCODE_CHAR_LWIN param2:0];
}
}
modifiers = newflags;
}
-(void)set_remote_modifiers:(NSEvent *)ev {
[self send_modifiers:ev enable:YES];
}
-(void)restore_remote_modifiers:(NSEvent *)ev {
[self send_modifiers:ev enable:NO];
}
-(void)mouseDown:(NSEvent *)ev {
int flags = [ev modifierFlags];
if ((flags & NSShiftKeyMask) && (flags & NSControlKeyMask)) {
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYRELEASE param1:SCANCODE_CHAR_LSHIFT param2:0];
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYRELEASE param1:SCANCODE_CHAR_LCTRL param2:0];
[self rightMouseDown:ev];
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYPRESS param1:SCANCODE_CHAR_LSHIFT param2:0];
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYPRESS param1:SCANCODE_CHAR_LCTRL param2:0];
return;
}
mouseLoc = [self convertPoint:[ev locationInWindow] fromView:nil];
[controller sendInput:RDP_INPUT_MOUSE flags:MOUSE_FLAG_DOWN | MOUSE_FLAG_BUTTON1 param1:(int)mouseLoc.x param2:(int)mouseLoc.y];
}
-(void)mouseUp:(NSEvent *)ev {
int flags = [ev modifierFlags];
if ((flags & NSShiftKeyMask) && (flags & NSControlKeyMask)) {
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYRELEASE param1:SCANCODE_CHAR_LSHIFT param2:0];
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYRELEASE param1:SCANCODE_CHAR_LCTRL param2:0];
[self rightMouseUp:ev];
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYPRESS param1:SCANCODE_CHAR_LSHIFT param2:0];
[controller sendInput:RDP_INPUT_SCANCODE flags:RDP_KEYPRESS param1:SCANCODE_CHAR_LCTRL param2:0];
return;
}
mouseLoc = [self convertPoint:[ev locationInWindow] fromView:nil];
[controller sendInput:RDP_INPUT_MOUSE flags:MOUSE_FLAG_BUTTON1 param1:(int)mouseLoc.x param2:(int)mouseLoc.y];
}
-(void)rightMouseDown:(NSEvent *)ev {
mouseLoc = [self convertPoint:[ev locationInWindow] fromView:nil];
[controller sendInput:RDP_INPUT_MOUSE flags:MOUSE_FLAG_DOWN | MOUSE_FLAG_BUTTON2 param1:(int)mouseLoc.x param2:(int)mouseLoc.y];
}
-(void)rightMouseUp:(NSEvent *)ev {
mouseLoc = [self convertPoint:[ev locationInWindow] fromView:nil];
[controller sendInput:RDP_INPUT_MOUSE flags:MOUSE_FLAG_BUTTON2 param1:(int)mouseLoc.x param2:(int)mouseLoc.y];
}
-(void)scrollWheel:(NSEvent *)ev {
if ([ev deltaY] > 0) {
[controller sendInput:RDP_INPUT_MOUSE flags:MOUSE_FLAG_DOWN | MOUSE_FLAG_BUTTON4 param1:(int)mouseLoc.x param2:(int)mouseLoc.y];
[controller sendInput:RDP_INPUT_MOUSE flags:MOUSE_FLAG_BUTTON4 param1:(int)mouseLoc.x param2:(int)mouseLoc.y];
} else if ([ev deltaY] < 0) {
[controller sendInput:RDP_INPUT_MOUSE flags:MOUSE_FLAG_DOWN | MOUSE_FLAG_BUTTON5 param1:(int)mouseLoc.x param2:(int)mouseLoc.y];
[controller sendInput:RDP_INPUT_MOUSE flags:MOUSE_FLAG_BUTTON5 param1:(int)mouseLoc.x param2:(int)mouseLoc.y];
}
}
-(BOOL)checkMouseInBounds:(NSEvent *)ev {
NSRect frame = [self frame];
mouseLoc = [self convertPoint:[ev locationInWindow] fromView:nil];
if ((mouseLoc.x < frame.origin.x) || (mouseLoc.x > frame.size.width) ||
(mouseLoc.y < frame.origin.y) || (mouseLoc.y > frame.size.height)) {
return NO;
}
return YES;
}
-(void)mouseDragged:(NSEvent *)ev {
[self mouseMoved:ev];
}
-(void)mouseMoved:(NSEvent *)ev {
if ([self checkMouseInBounds:ev]) {
[controller sendInput:RDP_INPUT_MOUSE flags:MOUSE_FLAG_MOVE param1:(int)mouseLoc.x param2:(int)mouseLoc.y];
}
}
-(void)mouseEntered:(NSEvent *)ev {
NSLog(@"Mouse entered!");
}
-(void)mouseExited:(NSEvent *)ev {
NSLog(@"Mouse exited");
}
#pragma mark Accessor Methods
-(void)setForeground:(NSColor *)color {
[color retain];
[foregroundColor release];
foregroundColor = color;
}
-(void)setBackground:(NSColor *)color {
[color retain];
[backgroundColor release];
backgroundColor = color;
}
-(int)bitsPerPixel {
return bitdepth;
}
-(void)setFrameSize:(NSSize)size {
[back release];
back = [[NSImage alloc] initWithSize:size];
[super setFrameSize:size];
}
-(int)width {
return [self frame].size.width;
}
-(int)height {
return [self frame].size.height;
}
-(NSArray *)colorMap {
return colorMap;
}
-(int)setColorMap:(NSArray *)map {
[map retain];
[colorMap release];
colorMap = map;
return 0;
}
-(void)swapRect:(NSRect)r {
[back lockFocus];
NSRectClip(clipRect);
CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(context);
CGContextSetBlendMode(context, kCGBlendModeDifference);
CGContextSetRGBFillColor (context, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect (context, CGRectMake(r.origin.x, r.origin.y, r.size.width, r.size.height));
CGContextFlush(context);
CGContextRestoreGState (context);
[back unlockFocus];
}
-(int)rgbForRDCColor:(int)col r:(unsigned char *)r g:(unsigned char *)g b:(unsigned char *)b {
if (bitdepth == 8) {
int t = [[colorMap objectAtIndex:col] intValue];
*r = (t >> 16) & 0xff;
*g = (t >> 8) & 0xff;
*b = t & 0xff;
} else if (bitdepth == 16) {
*r = ((col >> 8) & 0xf8) | ((col >> 13) & 0x7);
*g = ((col >> 3) & 0xfc) | ((col >> 9) & 0x3);
*b = ((col << 3) & 0xf8) | ((col >> 2) & 0x7);
} else if (bitdepth == 24) {
*r = (col >> 16) & 0xff;
*g = (col >> 8) & 0xff;
*b = col & 0xff;
} else {
NSLog(@"Attempting to convert color in unknown bitdepth");
}
return 0;
}
-(void)setBitdepth:(int)depth {
bitdepth = depth;
}
-(void)setCursor:(NSCursor *)cur {
[cur retain];
[cursor release];
cursor = cur;
[[self window] invalidateCursorRectsForView:self];
// this is a hack because the above call isn't always reliable...
[[self window] resetCursorRects];
}
-(void)setNeedsDisplayInRectAsValue:(NSValue *)rectValue {
[self setNeedsDisplayInRect:[rectValue rectValue]];
}
#pragma mark Auxiliary view functions
-(void)addAuxiliaryView:(NSView *)view {
[auxiliaryViews addObject:view];
}
-(NSView *)primaryAuxiliaryView {
return [auxiliaryViews objectAtIndex:0];
}
-(void)paintAuxiliaryViews {
NSImage *img;
NSRect bounds, thumbSize;
bounds.origin = NSMakePoint(0.0,0.0);
thumbSize.origin = NSMakePoint(0.0,0.0);
NSEnumerator *enumerator = [auxiliaryViews objectEnumerator];
id aView;
while ( (aView = [enumerator nextObject]) ) {
if ([aView tag] != 0) {
img = [aView image];
[img lockFocus];
bounds.size = [back size];
thumbSize.size = [aView frame].size;
// using High *kills* performance on all but fast machines,
// and doesn't make it look a ton better
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationLow];
[back drawInRect:thumbSize fromRect:bounds operation:NSCompositeCopy fraction:1.0];
[img unlockFocus];
[aView performSelectorOnMainThread:@selector(setNeedsDisplay:)
withObject:[NSNumber numberWithBool:YES] waitUntilDone:NO];
}
}
}
@end