summaryrefslogtreecommitdiff
path: root/src/xtoq/XtoqApplication.m
blob: 7824968684809a7c1280da27161adeddcdf905c6 (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
/*Copyright (C) 2012 Braden Wooley, Ben Huddle

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

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

#import "XtoqApplication.h"

@implementation XtoqApplication

int
XtoqApplicationMain(int argc, char** argv)
{

    XtoqController *controller;
    char *scrn;
    FILE *fp;
    const char *fifo_path = "/tmp/xtoq_fifo";
    ssize_t bytes_read;
    size_t len = 0;
    char bundle_path[PATH_MAX];
    const char *startx_path = "/opt/X11/bin/startx";
    const char *Xorg_path = "/opt/X11/bin/Xorg";
    char *xinitrc_path;
    const char *spawn[5];
    pid_t child;

    mkfifo(fifo_path, S_IRUSR | S_IWUSR);

    CFBundleRef bundle = CFBundleGetMainBundle();
    assert(bundle);

    CFURLRef resourcesURL = CFBundleCopyBundleURL(bundle);
    assert(resourcesURL);

    CFStringRef bundle_path_cfstr = CFURLCopyFileSystemPath(
        resourcesURL, kCFURLPOSIXPathStyle);
    assert(bundle_path_cfstr);

    CFStringGetCString(bundle_path_cfstr, bundle_path, PATH_MAX,
                       kCFStringEncodingUTF8);

    asprintf(&xinitrc_path,
             "%s/Contents/Resources/X11/lib/X11/xinit/xinitrc",
             bundle_path);
    assert(xinitrc_path);

    spawn[0] = startx_path;
    spawn[1] = xinitrc_path;
    spawn[2] = "--";
    spawn[3] = Xorg_path;
    spawn[4] = NULL;

    int error =
        posix_spawnp(&child, spawn[0], NULL, NULL, (char *const *)spawn,
                     environ);
    if (error) {
        NSLog(@"error with posix_spawnp");
        exit(1);
    }

    scrn = NULL;

    fp = fopen(fifo_path, "r");
    assert(fp);
#if 1
    bytes_read = getline(&scrn, &len, fp);
#else
    scrn = ":0";
#endif
    fclose(fp);

    if (bytes_read && scrn[bytes_read - 1] == '\n')
        scrn[bytes_read - 1] = '\0';

    [XtoqApplication sharedApplication];
    controller = [[XtoqController alloc] init];
    [controller setScreen:scrn];
    [NSApp setDelegate: controller];

    [NSApp run];

    return 1;
}

- (void) sendEvent:(NSEvent *)e
{
    notificationCenter = [NSNotificationCenter defaultCenter];
    NSMutableDictionary *InfoDict;
    NSPoint ns_location = [e locationInWindow];
    NSWindow *ns_window = [e window];
    NSNumber *xNum, *yNum;
    BOOL for_appkit;
    BOOL for_x;

    /* By default pass down the responder chain and to X. */
    for_appkit = YES;
    for_x = YES;

    switch ([e type]) {
    case NSLeftMouseDown:
    case NSRightMouseDown:
    case NSOtherMouseDown:
    case NSLeftMouseUp:
    case NSRightMouseUp:
    case NSOtherMouseUp:
    {
        /* We want to force sending to appkit if we're over the menu bar */
        int buttonInt = [self eventToButtonInt:e];
        NSNumber *buttonNumber = [[NSNumber alloc] initWithInt:buttonInt];

        NSPoint NSlocation = [e locationInWindow];
        NSWindow *window = [e window];
        NSRect NSframe, NSvisibleFrame;
        CGRect CGframe, CGvisibleFrame;
        CGPoint CGlocation;

        if (window != nil) {
            NSRect frame = [window frame];
            NSlocation.x += frame.origin.x;
            NSlocation.y += frame.origin.y;
        }

        NSframe = [[NSScreen mainScreen] frame];
        NSvisibleFrame = [[NSScreen mainScreen] visibleFrame];

        CGframe = CGRectMake(NSframe.origin.x, NSframe.origin.y,
                             NSframe.size.width, NSframe.size.height);
        CGvisibleFrame = CGRectMake(NSvisibleFrame.origin.x,
                                    NSvisibleFrame.origin.y,
                                    NSvisibleFrame.size.width,
                                    NSvisibleFrame.size.height);
        CGlocation = CGPointMake(NSlocation.x, NSlocation.y);

        if (CGRectContainsPoint(CGframe, CGlocation) &&
            !CGRectContainsPoint(CGvisibleFrame, CGlocation))
            for_appkit = YES;

        if (for_x) {
            if ([e type] == NSLeftMouseDown
                || [e type] == NSRightMouseDown
                || [e type] == NSOtherMouseDown) {

                CGFloat f = 100;
                NSNumber *n = [[NSNumber alloc] initWithFloat:f];
                NSMutableDictionary *threeInfoDict =
                    [[NSMutableDictionary alloc] initWithCapacity:3];
                [threeInfoDict setObject:e forKey:@"1"];
                [threeInfoDict setObject:n forKey:@"2"];
                [threeInfoDict setObject:buttonNumber forKey:@"3"];

                [notificationCenter postNotificationName:
                 @"XTOQmouseButtonDownEvent"
                 object:self
                 userInfo:threeInfoDict];
            }
            else {
                CGFloat f = 100;
                NSNumber *n = [[NSNumber alloc] initWithFloat:f];
                NSMutableDictionary *threeInfoDict =
                    [[NSMutableDictionary alloc] initWithCapacity:3];
                [threeInfoDict setObject:e forKey:@"1"];
                [threeInfoDict setObject:n forKey:@"2"];
                [threeInfoDict setObject:buttonNumber forKey:@"3"];
                [notificationCenter postNotificationName:
                 @"XTOQmouseButtonReleaseEvent"
                 object:self
                 userInfo:threeInfoDict];
            }
        }
        break;
    }

    case NSMouseMoved:
    {
        if (ns_window != nil) {
            NSRect frame = [ns_window frame];
            ns_location.x += frame.origin.x;
            ns_location.y += frame.origin.y;
        }

        xNum = [[NSNumber alloc] initWithFloat:ns_location.x];
        yNum = [[NSNumber alloc] initWithFloat:ns_location.y];

        InfoDict = [[NSMutableDictionary alloc] initWithCapacity: 2];
        [InfoDict setObject:xNum forKey: @"1"];
        [InfoDict setObject:yNum forKey: @"2"];

        [notificationCenter postNotificationName: @"MouseMovedEvent"
                                          object: self
                                        userInfo: InfoDict];
        break;
    }

    case NSKeyDown:
    case NSKeyUp:
    {

        static BOOL do_swallow = NO;
        static int swallow_keycode;

        if ([e type] == NSKeyDown) {
            /* Before that though, see if there are any global
             * shortcuts bound to it. */

            // Zero takes the place of XQuartz's
            // darwinAppKitModMask
            if (0 &[e modifierFlags]) {
                /* Override to force sending to Appkit */
                swallow_keycode = [e keyCode];
                do_swallow = YES;
                for_x = NO;
            }
            else if ([[self mainMenu] performKeyEquivalent:e]) {
                swallow_keycode = [e keyCode];
                do_swallow = YES;
                for_appkit = NO;
                for_x = NO;
            }
            else {
                /* No kit window is focused, so send it to X. */
                for_appkit = NO;
            }
            if (for_x) {
                NSDictionary * dictionary =
                    [NSDictionary dictionaryWithObject:e
                    forKey
                    :@"1"];
                [notificationCenter postNotificationName:
                 @"XTOQviewKeyDownEvent"
                 object:self
                 userInfo:dictionary];
            }
        }
        else {       /* KeyUp */
            /* If we saw a key equivalent on the down, don't pass
             * the up through to X. */
            if (do_swallow && [e keyCode] == swallow_keycode) {
                do_swallow = NO;
                for_x = NO;
            }
            if (for_x) {
                NSDictionary * dictionary =
                    [NSDictionary dictionaryWithObject:e
                    forKey
                    :@"1"];
                [notificationCenter postNotificationName:
                 @"XTOQviewKeyUpEvent"
                 object:self
                 userInfo:dictionary];
            }
        }
        break;
    }

    default:
        break;
    }
    if (for_appkit) {
        [super sendEvent:e];
    }
}

- (int) eventToButtonInt:(NSEvent *)e
{
    if ([e type] == NSLeftMouseDown ||
        [e type] == NSLeftMouseUp) {
        return 1;
    }
    else if ([e type] == NSRightMouseDown ||
             [e type] == NSRightMouseUp) {
        return 3;
    }
    else if ([e type] == NSOtherMouseDown ||
             [e type] == NSOtherMouseUp) {
        return 2;
    }
    else return -1;
}

@end