summaryrefslogtreecommitdiff
path: root/src/xtoq/XtoqController.m
blob: a9c38261d6ec22930c18fb695844215292bfa035 (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

/*Copyright (C) 2012 Aaron Skomra, Ben Huddle, Braden Wooley
 
 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.
 */

/** 
 *  AppController.m
 *  xcwm
 *
 * 
 *  This is the controller for the Popup to retreive the display number
 *  from the user.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#import "XtoqController.h"

#define WINDOWBAR 22

@implementation XtoqController

- (id) init {
    self = [super init];
    if (self) {
        referenceToSelf = self;
    }
    return self;
}

- (int) xserverToOSX:(int)yValue windowHeight:(int)windowH {
    
    int height = [[NSScreen mainScreen] frame].size.height;    
    return height - windowH + yValue;
    
}

- (int) osxToXserver:(int)yValue windowHeight:(int)windowH {
    
    int height = [[NSScreen mainScreen] frame].size.height;    
    return height - yValue;
    
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  
    // setup X connection and get the initial image from the server
    rootContext = xcwm_init(screen);
    
    [[NSGraphicsContext currentContext]
    setImageInterpolation:NSImageInterpolationHigh];
    
    xcwmWindow = [[XtoqWindow alloc] 
                  initWithContentRect: NSMakeRect(rootContext->x, rootContext->y, 
                                                  rootContext->width, rootContext->height)
                            styleMask: (NSTitledWindowMask |
                                        NSClosableWindowMask |
                                        NSMiniaturizableWindowMask |
                                        NSResizableWindowMask)
                              backing: NSBackingStoreBuffered
                                defer: YES];

	[xcwmWindow setContext: rootContext];
	rootContext->local_data = xcwmWindow;
    // Make the menu
    [self makeMenu];

    // FIXME: Since this is the root window, should be getting width
    // and height from the size of the root window, not hard-coded
    // values.
    imageRec = NSMakeRect(0, 0, 1028,768);//[image getWidth], [image getHeight]);
    // create a view, init'ing it with our rect
    ourView = [[XtoqView alloc] initWithFrame:imageRec];
	[ourView setContext: rootContext];

    // add view to its window
    [xcwmWindow setContentView: ourView];  
    
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];    
    
    // Register for the key down notifications from the view
    [nc addObserver: self
		   selector: @selector(keyDownInView:)
		       name: @"XTOQviewKeyDownEvent"
		     object: nil];
    
    [nc addObserver: self
		   selector: @selector(keyUpInView:)
		       name: @"XTOQviewKeyUpEvent"
		     object: nil];
    
    [nc addObserver: self
		   selector: @selector(mouseButtonDownInView:)
		       name: @"XTOQmouseButtonDownEvent"
		     object: nil];
    
    [nc addObserver: self
		   selector: @selector(mouseButtonReleaseInView:)
		       name: @"XTOQmouseButtonReleaseEvent" 
		     object: nil];

    [nc addObserver: self
           selector: @selector(mouseMovedInApp:)
               name: @"MouseMovedEvent" 
             object: nil];

    // register for destroy event
    [nc addObserver: self
		   selector: @selector(destroy:)
		       name: @"XTOQdestroyTheWindow"
		     object: nil];

    [nc addObserver:self 
		   selector:@selector(windowDidMove:) 
		       name:NSWindowDidMoveNotification 
		     object:nil];

    // regester for window resize notification
    [nc addObserver:self 
		   selector:@selector(windowDidResize:) 
		       name:NSWindowDidResizeNotification 
		     object:nil];

    xcwmDispatchQueue = dispatch_queue_create("xcwm.dispatch.queue", NULL);

    // Start the event loop and set the handler function
	xcwm_start_event_loop(rootContext, (void *) eventHandler);    
}

- (void)applicationWillTerminate:(NSNotification *)aNotification
{
    xcwm_close();
    
    const char *spawn[4];
    pid_t child;

    // FIXME: This clobbers all instantances of the xserver. Not what
    // we want to do.
    spawn[0] = "/usr/bin/killall";
    spawn[1] = "-9";
    spawn[2] = "Xorg";
    spawn[3] = NULL;
     
    posix_spawn(&child, spawn[0], NULL, NULL, (char * const*)spawn, environ);
}

- (void) applicationWillFinishLaunching: (NSNotification *) aNotification
{
}

- (void) mouseMovedInApp: (NSNotification *) aNotification {

    //CGFloat heightFloat;
    NSDictionary *mouseMoveInfo = [aNotification userInfo];
    NSEvent * event = [mouseMoveInfo objectForKey: @"1"];
    NSNumber *xNum =  [mouseMoveInfo objectForKey: @"2"];
    NSNumber *yNum =  [mouseMoveInfo objectForKey: @"3"];
    
    int height = [[NSScreen mainScreen] frame].size.height;
        
    dispatch_async(xcwmDispatchQueue, 
                   ^{ xcwm_mouse_motion (rootContext,
                                         [xNum intValue], 
                                         //Converting OSX coordinates to X11
                                         height - WINDOWBAR - [yNum intValue], 
                                         (int)[event windowNumber],
                                         0);;});
}

- (void) keyDownInView: (NSNotification *) aNotification
{   
    NSDictionary *keyInfo = [aNotification userInfo];
    // note this keyInfo is the key in <key, value> not the key pressed
    NSEvent * event = [keyInfo objectForKey: @"1"];
    unsigned short aChar = [event keyCode];
    NSString* charNSString = [event characters]; 
    const char* charcharstar = [charNSString UTF8String];

    NSLog(@"%s pressed", charcharstar);
    dispatch_async(xcwmDispatchQueue, 
                   ^{ xcwm_key_press(rootContext, 
                                     (int)[event windowNumber],
                                     aChar + 8) ;});
}

- (void) keyUpInView: (NSNotification *) aNotification
{   
    NSDictionary *keyInfo = [aNotification userInfo];
    // note this keyInfo is the key in <key, value> not the key pressed
    NSEvent * event = [keyInfo objectForKey: @"1"];
    unsigned short aChar = [event keyCode];

    dispatch_async(xcwmDispatchQueue, 
                   ^{ xcwm_key_release(rootContext, 
                                     (int)[event windowNumber],
                                     aChar + 8) ;});
}
 

// on this side all I have is a xcwm_context , on the library side I need
// to turn that into a real context 
- (void) mouseButtonDownInView: (NSNotification *) aNotification
{
    CGFloat heightFloat;
    NSDictionary *mouseDownInfo = [aNotification userInfo];
    // NSLog(@"Controller Got a XTOQmouseButtonDownEvent");
    NSEvent * event = [mouseDownInfo objectForKey: @"1"];

    NSNumber * heightAsNumber =  [NSNumber alloc];
    heightAsNumber = [mouseDownInfo objectForKey: @"2"];
    heightFloat = [heightAsNumber floatValue];
    
    NSNumber * mouseButton = [NSNumber alloc];
    mouseButton = [mouseDownInfo objectForKey:@"3"];
    int buttonInt = [mouseButton intValue];
    //NSLog(@"Mouse Info: %@", [mouseDownInfo objectForKey: @"2"]);
    
    dispatch_async(xcwmDispatchQueue, 
                   ^{ xcwm_button_press (rootContext,
                                         0,
                                         0, 
                                         (int)[event windowNumber],
                                         buttonInt);;});
}

// on this side all I have is a xcwm_context , on the library side I need
// to turn that into a real context 
- (void) mouseButtonReleaseInView: (NSNotification *) aNotification
{
    CGFloat heightFloat;
    NSDictionary *mouseReleaseInfo = [aNotification userInfo];
    // NSLog(@"Controller Got a XTOQmouseButtonDownEvent");
    NSEvent * event = [mouseReleaseInfo objectForKey: @"1"];
    //NSRect bnd = NSMakeRect(0,0,512,386);
    NSNumber * heightAsNumber =  [NSNumber alloc];
    heightAsNumber = [mouseReleaseInfo objectForKey: @"2"];
    heightFloat = [heightAsNumber floatValue];
    //NSLog(@"Mouse Info: %@", [mouseDownInfo objectForKey: @"2"]);
    
    NSNumber * mouseButton = [NSNumber alloc];
    mouseButton = [mouseReleaseInfo objectForKey:@"3"];
    int buttonInt = [mouseButton intValue];
    
    dispatch_async(xcwmDispatchQueue, 
                   ^{ xcwm_button_release (rootContext,
                                           0,
                                           0,
                                           (int)[event windowNumber],
                                           buttonInt);;});
}


- (void) setScreen:(char *)scrn {
    free(screen);
    screen = strdup(scrn);
    setenv("DISPLAY", screen, 1);
}

- (void) makeMenu {
    // Create menu  
    NSMenu *menubar;
    NSMenuItem *appMenuItem;
    NSMenu *appMenu;
    NSString *appName;
    NSString *aboutTitle;
    NSMenuItem *aboutMenuItem;
    NSString *quitTitle;
    NSMenuItem *quitMenuItem;

    menubar = [[NSMenu new] autorelease];
    appMenuItem = [[NSMenuItem new] autorelease];
    
    appMenu = [[NSMenu new] autorelease];
    appName = [[NSProcessInfo processInfo] processName];

    // Xtoq -> About
    aboutTitle = [@"About " stringByAppendingString:appName];
    aboutMenuItem = [[NSMenuItem alloc] initWithTitle:aboutTitle 
                                                           action:NULL 
                                                    keyEquivalent:@"a"];
    [appMenu addItem:aboutMenuItem];
    [appMenuItem setSubmenu:appMenu];
    
    // Xtoq -> Quit
    quitTitle = [@"Quit " stringByAppendingString:appName];
    quitMenuItem = [[NSMenuItem alloc] initWithTitle:quitTitle 
                                                          action:@selector(terminate:) 
                                                   keyEquivalent:@"q"];
    [appMenu addItem:quitMenuItem];
    [appMenuItem setSubmenu:appMenu];
    
    // Menu under Applications
    NSMenu *startXMenu = [[NSMenu new] autorelease];
    NSMenuItem *startXApps = [[NSMenuItem new] autorelease];
    [startXMenu setTitle:@"Applications"];
    
    // Run Xeyes
    NSString *xTitle;
    NSMenuItem *xeyesMenuItem;
    xTitle = @"Run Xeyes";
    xeyesMenuItem = [[NSMenuItem alloc] initWithTitle:xTitle 
                                               action:@selector(runXeyes:) 
                                        keyEquivalent:@""];
    [startXMenu addItem:xeyesMenuItem];
    [startXApps setSubmenu:startXMenu];
    
    // Run Xclock
    NSMenuItem *xclockMenuItem;
    xTitle = @"Run Xclock";
    xclockMenuItem = [[NSMenuItem alloc] initWithTitle:xTitle 
                                                action:@selector(runXclock:)
                                         keyEquivalent:@""];
    [startXMenu addItem:xclockMenuItem];
    [startXApps setSubmenu:startXMenu];
    
    // Run Xlogo
    NSMenuItem *xlogoMenuItem;
    xTitle = @"Run Xlogo";
    xlogoMenuItem = [[NSMenuItem alloc] initWithTitle:xTitle 
                                               action:@selector(runXlogo:)
                                        keyEquivalent:@""];
    [startXMenu addItem:xlogoMenuItem];
    [startXApps setSubmenu:startXMenu];
    
    // Run Xterm
    NSMenuItem *xtermMenuItem;
    xTitle = @"Run Xterm";
    xtermMenuItem = [[NSMenuItem alloc] initWithTitle:xTitle 
                                               action:@selector(runXterm:)
                                        keyEquivalent:@""];
    [startXMenu addItem:xtermMenuItem];
    [startXApps setSubmenu:startXMenu];
    
    // Run Xman
    NSMenuItem *xmanMenuItem;
    xTitle = @"Run Xman";
    xmanMenuItem = [[NSMenuItem alloc] initWithTitle:xTitle
                                              action:@selector(runXman:)
                                       keyEquivalent:@""];
    [startXMenu addItem:xmanMenuItem];
    [startXApps setSubmenu:startXMenu];
    
    // Adding all the menu items to the main menu for XtoQ.
    [menubar addItem:appMenuItem];
    [menubar addItem:startXApps];
    [NSApp setMainMenu:menubar];
}

- (void) launch_client:(NSString *)filename {
    int status;
    pid_t child;
    const char *file_name = [filename UTF8String];
    const char *newargv[6];
	char *exec_path;
    
    // Xman Special case
    if ([filename isEqualToString:@"xman"]) {
        const char *manpath = "/opt/local/share/man:/usr/share/man:/usr/local/share/man:/opt/X11/share/man:/usr/X11/man:/usr/local/git/share/man";        
        setenv("MANPATH", manpath, 1);
    }
    
	// FIXME: Doesn't seem like we should be relying on an absolute path,
	// but just sending the executable name doesn't always work.
	asprintf(&exec_path, "/usr/X11/bin/%s", file_name);
    newargv[0] = exec_path;
    newargv[1] = "-display";
    newargv[2] = screen;
    
    if ([filename isEqualToString:@"xclock"]) {
        newargv[3] = "-update";
        newargv[4] = "1";
        newargv[5] = NULL;
    }
    else {
        newargv[3] = NULL;
    }
    
    status = posix_spawnp(&child, newargv[0], NULL, NULL,
						  (char * const *) newargv, environ);
    if(status) {
        NSLog(@"Error spawning file for launch.");
    }
}

- (void) runXeyes:(id) sender {
    [self launch_client:@"xeyes"];
}
- (void) runXclock:(id) sender {
    [self launch_client:@"xclock"];
}
- (void) runXlogo:(id) sender {
    [self launch_client:@"xlogo"];    
}
- (void) runXterm:(id) sender {
    [self launch_client:@"xterm"];    
}
-(void) runXman:(id) sender {
    [self launch_client:@"xman"];
}

// create a new window 
- (void) createNewWindow: (xcwm_context_t *) windowContext {
    
    XtoqWindow   *newWindow;
    XtoqView     *newView;
    xcwm_image_t *xcbImage;
    XtoqImageRep *imageRep;  

    int y = [self xserverToOSX:windowContext->y windowHeight:windowContext->height];
    
    newWindow =  [[XtoqWindow alloc] 
                  initWithContentRect: NSMakeRect(windowContext->x, y, 
                                                  windowContext->width, windowContext->height)
                  styleMask: (NSTitledWindowMask |
                              NSClosableWindowMask |
                              NSMiniaturizableWindowMask |
                              NSResizableWindowMask)
                  backing: NSBackingStoreBuffered
                  defer: YES];
    
    // save context in window
    [newWindow setContext:windowContext];
    
    // save the newWindow pointer into the context
    windowContext->local_data = (id)newWindow;
    
    // get image to darw
    xcbImage = xcwm_get_image(windowContext);
    imageRep = [[XtoqImageRep alloc] initWithData:xcbImage x: 0 y: 0];
    
    // draw the image into a rect
    NSRect imgRec = NSMakeRect(0, 0, [imageRep getWidth], [imageRep getHeight]);
    
    // create a view, init'ing it with our rect
    newView = [[XtoqView alloc] initWithFrame:imgRec];
	[newView setContext:windowContext];
    
    // add view to its window
    [newWindow setContentView: newView ];
    
    // set title
    NSString *winTitle;
    winTitle = [NSString stringWithCString:windowContext->name encoding:NSUTF8StringEncoding];
    [newWindow setTitle:winTitle];
    
    //shows the window
    [newWindow makeKeyAndOrderFront:self];
    
}

- (void) destroyWindow:(xcwm_context_t *) windowContext {
    // set the window to be closed
    XtoqWindow *destWindow = windowContext->local_data;
    //close window
    [destWindow close];
}

- (void) destroy:(NSNotification *) aNotification {    
    
    NSDictionary *contextInfo = [aNotification userInfo];    
    XtoqWindow *aWindow = [contextInfo objectForKey: @"1"];
    xcwm_context_t *theContext = [aWindow getContext];
    
    //use dispatch_async() to handle the actual close 
      dispatch_async(xcwmDispatchQueue, ^{
          NSLog(@"Call xcwm_request_close(theContext)");
          xcwm_request_close(theContext);
      });
}

- (void) updateImage:(xcwm_context_t *) windowContext
{
    float  y_transformed;
	NSRect newDamageRect;

    y_transformed =( windowContext->height - windowContext->damaged_y - windowContext->damaged_height)/1.0; 
	newDamageRect = NSMakeRect(windowContext->damaged_x,
							   y_transformed,
							   windowContext->damaged_width,
							   windowContext->damaged_height);
	XtoqView *localView = (XtoqView *)[(XtoqWindow *)windowContext->local_data contentView];
    [ localView setPartialImage:newDamageRect];
}

- (void) windowDidMove:(NSNotification*)notification {
    [self reshape];
}

- (void) windowDidResize:(NSNotification*)notification {
    [self reshape];
}

- (void) reshape {
    
  XtoqWindow *moveWindow = (XtoqWindow *)[NSApp mainWindow];
    
    if (moveWindow != nil) {        
        xcwm_context_t *moveContext = [moveWindow getContext];        
        NSRect moveFrame = [moveWindow frame];
        int x = (int)moveFrame.origin.x;
        int y = [self osxToXserver:(int)moveFrame.origin.y
					  windowHeight:moveContext->height] - WINDOWBAR;
        int width = (int)moveFrame.size.width;
        int height = (int)moveFrame.size.height - WINDOWBAR;

        xcwm_configure_window(moveContext, x, y - height, height, width);
		[[moveWindow contentView] setNeedsDisplay: YES];
    }    
}

@end

void eventHandler (xcwm_event_t *event)
{
    xcwm_context_t *context = xcwm_event_get_context(event);
    if (xcwm_event_get_type(event) == XTOQ_DAMAGE) {
	  [referenceToSelf updateImage: context];
    } else if (xcwm_event_get_type(event) == XTOQ_CREATE) {
        NSLog(@"Window was created");
        [referenceToSelf createNewWindow: context];
    } else if (xcwm_event_get_type(event) == XTOQ_DESTROY) {
        NSLog(@"Window was destroyed");
        [referenceToSelf destroyWindow: context];
    } else { 
        NSLog(@"Hey I'm Not damage!"); 
    }
    free(event);
}