xref: /OK3568_Linux_fs/external/xserver/hw/xquartz/X11Controller.m (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun/* X11Controller.m -- connect the IB ui, also the NSApp delegate
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * Copyright (c) 2002-2012 Apple Inc. All rights reserved.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person
6*4882a593Smuzhiyun * obtaining a copy of this software and associated documentation files
7*4882a593Smuzhiyun * (the "Software"), to deal in the Software without restriction,
8*4882a593Smuzhiyun * including without limitation the rights to use, copy, modify, merge,
9*4882a593Smuzhiyun * publish, distribute, sublicense, and/or sell copies of the Software,
10*4882a593Smuzhiyun * and to permit persons to whom the Software is furnished to do so,
11*4882a593Smuzhiyun * subject to the following conditions:
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * The above copyright notice and this permission notice shall be
14*4882a593Smuzhiyun * included in all copies or substantial portions of the Software.
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17*4882a593Smuzhiyun * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*4882a593Smuzhiyun * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19*4882a593Smuzhiyun * NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
20*4882a593Smuzhiyun * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21*4882a593Smuzhiyun * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22*4882a593Smuzhiyun * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23*4882a593Smuzhiyun * DEALINGS IN THE SOFTWARE.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * Except as contained in this notice, the name(s) of the above
26*4882a593Smuzhiyun * copyright holders shall not be used in advertising or otherwise to
27*4882a593Smuzhiyun * promote the sale, use or other dealings in this Software without
28*4882a593Smuzhiyun * prior written authorization.
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun#include "sanitizedCarbon.h"
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun#ifdef HAVE_DIX_CONFIG_H
34*4882a593Smuzhiyun#include <dix-config.h>
35*4882a593Smuzhiyun#endif
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun#import "X11Controller.h"
38*4882a593Smuzhiyun#import "X11Application.h"
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun#include "opaque.h"
41*4882a593Smuzhiyun#include "darwin.h"
42*4882a593Smuzhiyun#include "darwinEvents.h"
43*4882a593Smuzhiyun#include "quartz.h"
44*4882a593Smuzhiyun#include "quartzKeyboard.h"
45*4882a593Smuzhiyun#include <X11/extensions/applewmconst.h>
46*4882a593Smuzhiyun#include "applewmExt.h"
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun#include <stdio.h>
49*4882a593Smuzhiyun#include <unistd.h>
50*4882a593Smuzhiyun#include <fcntl.h>
51*4882a593Smuzhiyun#include <sys/types.h>
52*4882a593Smuzhiyun#include <sys/wait.h>
53*4882a593Smuzhiyun#include <asl.h>
54*4882a593Smuzhiyun#include <stdlib.h>
55*4882a593Smuzhiyun
56*4882a593Smuzhiyunextern aslclient aslc;
57*4882a593Smuzhiyunextern char *bundle_id_prefix;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun@interface X11Controller ()
60*4882a593Smuzhiyun#ifdef XQUARTZ_SPARKLE
61*4882a593Smuzhiyun@property (nonatomic, readwrite, strong) NSMenuItem *check_for_updates_item; // Programatically enabled
62*4882a593Smuzhiyun#endif
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun@property (nonatomic, readwrite, strong) NSArray *apps;
65*4882a593Smuzhiyun@property (nonatomic, readwrite, strong) NSMutableArray *table_apps;
66*4882a593Smuzhiyun@property (nonatomic, readwrite, assign) NSInteger windows_menu_nitems;
67*4882a593Smuzhiyun@property (nonatomic, readwrite, assign) int checked_window_item;
68*4882a593Smuzhiyun@property (nonatomic, readwrite, assign) x_list *pending_apps;
69*4882a593Smuzhiyun@property (nonatomic, readwrite, assign) OSX_BOOL finished_launching;
70*4882a593Smuzhiyun@end
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun@implementation X11Controller
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun- (void) awakeFromNib
75*4882a593Smuzhiyun{
76*4882a593Smuzhiyun    X11Application *xapp = NSApp;
77*4882a593Smuzhiyun    NSArray *array;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun    /* Point X11Application at ourself. */
80*4882a593Smuzhiyun    xapp.controller = self;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun    array = [xapp prefs_get_array:@PREFS_APPSMENU];
83*4882a593Smuzhiyun    if (array != nil) {
84*4882a593Smuzhiyun        int count;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun        /* convert from [TITLE1 COMMAND1 TITLE2 COMMAND2 ...]
87*4882a593Smuzhiyun           to [[TITLE1 COMMAND1] [TITLE2 COMMAND2] ...] format. */
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun        count = [array count];
90*4882a593Smuzhiyun        if (count > 0
91*4882a593Smuzhiyun            && ![[array objectAtIndex:0] isKindOfClass:[NSArray class]]) {
92*4882a593Smuzhiyun            int i;
93*4882a593Smuzhiyun            NSMutableArray *copy, *sub;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun            copy = [NSMutableArray arrayWithCapacity:(count / 2)];
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun            for (i = 0; i < count / 2; i++) {
98*4882a593Smuzhiyun                sub = [[NSMutableArray alloc] initWithCapacity:3];
99*4882a593Smuzhiyun                [sub addObject:[array objectAtIndex:i * 2]];
100*4882a593Smuzhiyun                [sub addObject:[array objectAtIndex:i * 2 + 1]];
101*4882a593Smuzhiyun                [sub addObject:@""];
102*4882a593Smuzhiyun                [copy addObject:sub];
103*4882a593Smuzhiyun                [sub release];
104*4882a593Smuzhiyun            }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun            array = copy;
107*4882a593Smuzhiyun        }
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun        [self set_apps_menu:array];
110*4882a593Smuzhiyun    }
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun    [[NSNotificationCenter defaultCenter]
113*4882a593Smuzhiyun     addObserver: self
114*4882a593Smuzhiyun        selector: @selector(apps_table_done:)
115*4882a593Smuzhiyun            name: NSWindowWillCloseNotification
116*4882a593Smuzhiyun          object: self.apps_table.window];
117*4882a593Smuzhiyun}
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun- (void) item_selected:sender
120*4882a593Smuzhiyun{
121*4882a593Smuzhiyun    [NSApp activateIgnoringOtherApps:YES];
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun    DarwinSendDDXEvent(kXquartzControllerNotify, 2,
124*4882a593Smuzhiyun                       AppleWMWindowMenuItem, [sender tag]);
125*4882a593Smuzhiyun}
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun- (void) remove_apps_menu
128*4882a593Smuzhiyun{
129*4882a593Smuzhiyun    NSMenu *menu;
130*4882a593Smuzhiyun    NSMenuItem *item;
131*4882a593Smuzhiyun    int i;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun    NSMenuItem * const apps_separator = self.apps_separator;
134*4882a593Smuzhiyun    NSMenu * const dock_apps_menu = self.dock_apps_menu;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun    if (self.apps == nil || apps_separator == nil) return;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun    menu = [apps_separator menu];
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun    if (menu != nil) {
141*4882a593Smuzhiyun        for (i = [menu numberOfItems] - 1; i >= 0; i--) {
142*4882a593Smuzhiyun            item = (NSMenuItem *)[menu itemAtIndex:i];
143*4882a593Smuzhiyun            if ([item tag] != 0)
144*4882a593Smuzhiyun                [menu removeItemAtIndex:i];
145*4882a593Smuzhiyun        }
146*4882a593Smuzhiyun    }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun    if (dock_apps_menu != nil) {
149*4882a593Smuzhiyun        for (i = [dock_apps_menu numberOfItems] - 1; i >= 0; i--) {
150*4882a593Smuzhiyun            item = (NSMenuItem *)[dock_apps_menu itemAtIndex:i];
151*4882a593Smuzhiyun            if ([item tag] != 0)
152*4882a593Smuzhiyun                [dock_apps_menu removeItemAtIndex:i];
153*4882a593Smuzhiyun        }
154*4882a593Smuzhiyun    }
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun    self.apps = nil;
157*4882a593Smuzhiyun}
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun- (void) prepend_apps_item:(NSArray *)list index:(int)i menu:(NSMenu *)menu
160*4882a593Smuzhiyun{
161*4882a593Smuzhiyun    NSString *title, *shortcut = @"";
162*4882a593Smuzhiyun    NSArray *group;
163*4882a593Smuzhiyun    NSMenuItem *item;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun    group = [list objectAtIndex:i];
166*4882a593Smuzhiyun    title = [group objectAtIndex:0];
167*4882a593Smuzhiyun    if ([group count] >= 3)
168*4882a593Smuzhiyun        shortcut = [group objectAtIndex:2];
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun    if ([title length] != 0) {
171*4882a593Smuzhiyun        item = (NSMenuItem *)[menu insertItemWithTitle:title
172*4882a593Smuzhiyun                                                action:@selector (
173*4882a593Smuzhiyun                                  app_selected:)
174*4882a593Smuzhiyun                              keyEquivalent:shortcut atIndex:0];
175*4882a593Smuzhiyun        [item setTarget:self];
176*4882a593Smuzhiyun        [item setEnabled:YES];
177*4882a593Smuzhiyun    }
178*4882a593Smuzhiyun    else {
179*4882a593Smuzhiyun        item = (NSMenuItem *)[NSMenuItem separatorItem];
180*4882a593Smuzhiyun        [menu insertItem:item atIndex:0];
181*4882a593Smuzhiyun    }
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun    [item setTag:i + 1];                  /* can't be zero, so add one */
184*4882a593Smuzhiyun}
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun- (void) install_apps_menu:(NSArray *)list
187*4882a593Smuzhiyun{
188*4882a593Smuzhiyun    NSMenu *menu;
189*4882a593Smuzhiyun    int i, count;
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun    count = [list count];
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun    NSMenuItem * const apps_separator = self.apps_separator;
194*4882a593Smuzhiyun    NSMenu * const dock_apps_menu = self.dock_apps_menu;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun    if (count == 0 || apps_separator == nil) return;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun    menu = [apps_separator menu];
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun    for (i = count - 1; i >= 0; i--) {
201*4882a593Smuzhiyun        if (menu != nil)
202*4882a593Smuzhiyun            [self prepend_apps_item:list index:i menu:menu];
203*4882a593Smuzhiyun        if (dock_apps_menu != nil)
204*4882a593Smuzhiyun            [self prepend_apps_item:list index:i menu:dock_apps_menu];
205*4882a593Smuzhiyun    }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun    self.apps = list;
208*4882a593Smuzhiyun}
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun- (void) set_window_menu:(NSArray *)list
211*4882a593Smuzhiyun{
212*4882a593Smuzhiyun    NSMenu * const menu = X11App.windowsMenu;
213*4882a593Smuzhiyun    NSMenu * const dock_menu = self.dock_menu;
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun    /* First, remove the existing items from the Window Menu */
216*4882a593Smuzhiyun    NSInteger itemsToRemove = self.windows_menu_nitems;
217*4882a593Smuzhiyun    if (itemsToRemove > 0) {
218*4882a593Smuzhiyun        NSInteger indexForRemoval = menu.numberOfItems - itemsToRemove - 1; /* we also want to remove the separator */
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun        for (NSInteger i = 0 ; i < itemsToRemove + 1 ; i++) {
221*4882a593Smuzhiyun            [menu removeItemAtIndex:indexForRemoval];
222*4882a593Smuzhiyun        }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun        for (NSInteger i = 0 ; i < itemsToRemove; i++) {
225*4882a593Smuzhiyun            [dock_menu removeItemAtIndex:0];
226*4882a593Smuzhiyun        }
227*4882a593Smuzhiyun    }
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun    NSInteger const itemsToAdd = list.count;
230*4882a593Smuzhiyun    self.windows_menu_nitems = itemsToAdd;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun    if (itemsToAdd > 0) {
233*4882a593Smuzhiyun        NSMenuItem *item;
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun        // Push a Separator
236*4882a593Smuzhiyun        [menu addItem:[NSMenuItem separatorItem]];
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun        for (NSInteger i = 0; i < itemsToAdd; i++) {
239*4882a593Smuzhiyun            NSString *name, *shortcut;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun            name = list[i][0];
242*4882a593Smuzhiyun            shortcut = list[i][1];
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun            if (windowItemModMask == 0 || windowItemModMask == -1)
245*4882a593Smuzhiyun                shortcut = @"";
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun            item = (NSMenuItem *)[menu addItemWithTitle:name
248*4882a593Smuzhiyun                                                 action:@selector(item_selected:)
249*4882a593Smuzhiyun                                          keyEquivalent:shortcut];
250*4882a593Smuzhiyun            [item setKeyEquivalentModifierMask:(NSUInteger)windowItemModMask];
251*4882a593Smuzhiyun            [item setTarget:self];
252*4882a593Smuzhiyun            [item setTag:i];
253*4882a593Smuzhiyun            [item setEnabled:YES];
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun            item = (NSMenuItem *)[dock_menu  insertItemWithTitle:name
256*4882a593Smuzhiyun                                                          action:@selector(item_selected:)
257*4882a593Smuzhiyun                                                   keyEquivalent:shortcut
258*4882a593Smuzhiyun                                                         atIndex:i];
259*4882a593Smuzhiyun            [item setKeyEquivalentModifierMask:(NSUInteger)windowItemModMask];
260*4882a593Smuzhiyun            [item setTarget:self];
261*4882a593Smuzhiyun            [item setTag:i];
262*4882a593Smuzhiyun            [item setEnabled:YES];
263*4882a593Smuzhiyun        }
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun        int const checked_window_item = self.checked_window_item;
266*4882a593Smuzhiyun        if (checked_window_item >= 0 && checked_window_item < itemsToAdd) {
267*4882a593Smuzhiyun            NSInteger first = menu.numberOfItems - itemsToAdd;
268*4882a593Smuzhiyun            item = (NSMenuItem *)[menu itemAtIndex:first + checked_window_item];
269*4882a593Smuzhiyun            [item setState:NSOnState];
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun            item = (NSMenuItem *)[dock_menu itemAtIndex:checked_window_item];
272*4882a593Smuzhiyun            [item setState:NSOnState];
273*4882a593Smuzhiyun        }
274*4882a593Smuzhiyun    }
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun    DarwinSendDDXEvent(kXquartzControllerNotify, 1, AppleWMWindowMenuNotify);
277*4882a593Smuzhiyun}
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun- (void) set_window_menu_check:(NSNumber *)nn
280*4882a593Smuzhiyun{
281*4882a593Smuzhiyun    NSMenu * const menu = X11App.windowsMenu;
282*4882a593Smuzhiyun    NSMenu * const dock_menu = self.dock_menu;
283*4882a593Smuzhiyun    NSMenuItem *item;
284*4882a593Smuzhiyun    int n = nn.intValue;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun    NSInteger const count = self.windows_menu_nitems;
287*4882a593Smuzhiyun    NSInteger const first = menu.numberOfItems - count;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun    int const checked_window_item = self.checked_window_item;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun    if (checked_window_item >= 0 && checked_window_item < count) {
292*4882a593Smuzhiyun        item = (NSMenuItem *)[menu itemAtIndex:first + checked_window_item];
293*4882a593Smuzhiyun        [item setState:NSOffState];
294*4882a593Smuzhiyun        item = (NSMenuItem *)[dock_menu itemAtIndex:checked_window_item];
295*4882a593Smuzhiyun        [item setState:NSOffState];
296*4882a593Smuzhiyun    }
297*4882a593Smuzhiyun    if (n >= 0 && n < count) {
298*4882a593Smuzhiyun        item = (NSMenuItem *)[menu itemAtIndex:first + n];
299*4882a593Smuzhiyun        [item setState:NSOnState];
300*4882a593Smuzhiyun        item = (NSMenuItem *)[dock_menu itemAtIndex:n];
301*4882a593Smuzhiyun        [item setState:NSOnState];
302*4882a593Smuzhiyun    }
303*4882a593Smuzhiyun    self.checked_window_item = n;
304*4882a593Smuzhiyun}
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun- (void) set_apps_menu:(NSArray *)list
307*4882a593Smuzhiyun{
308*4882a593Smuzhiyun    [self remove_apps_menu];
309*4882a593Smuzhiyun    [self install_apps_menu:list];
310*4882a593Smuzhiyun}
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun#ifdef XQUARTZ_SPARKLE
313*4882a593Smuzhiyun- (void) setup_sparkle
314*4882a593Smuzhiyun{
315*4882a593Smuzhiyun    if (self.check_for_updates_item)
316*4882a593Smuzhiyun        return;  // already did it...
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun    NSMenu *menu = [self.x11_about_item menu];
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun    NSMenuItem * const check_for_updates_item =
321*4882a593Smuzhiyun        [menu insertItemWithTitle:NSLocalizedString(@"Check for X11 Updates...", @"Check for X11 Updates...")
322*4882a593Smuzhiyun                           action:@selector(checkForUpdates:)
323*4882a593Smuzhiyun                    keyEquivalent:@""
324*4882a593Smuzhiyun                          atIndex:1];
325*4882a593Smuzhiyun    [check_for_updates_item setTarget:[SUUpdater sharedUpdater]];
326*4882a593Smuzhiyun    [check_for_updates_item setEnabled:YES];
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun    self.check_for_updates_item = check_for_updates_item;
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun    // Set X11Controller as the delegate for the updater.
331*4882a593Smuzhiyun    [[SUUpdater sharedUpdater] setDelegate:self];
332*4882a593Smuzhiyun}
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun// Sent immediately before installing the specified update.
335*4882a593Smuzhiyun- (void)updater:(SUUpdater *)updater willInstallUpdate:(SUAppcastItem *)update
336*4882a593Smuzhiyun{
337*4882a593Smuzhiyun    //self.can_quit = YES;
338*4882a593Smuzhiyun}
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun#endif
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun- (void) launch_client:(NSString *)filename
343*4882a593Smuzhiyun{
344*4882a593Smuzhiyun    int child1, child2 = 0;
345*4882a593Smuzhiyun    int status;
346*4882a593Smuzhiyun    const char *newargv[4];
347*4882a593Smuzhiyun    char buf[128];
348*4882a593Smuzhiyun    char *s;
349*4882a593Smuzhiyun    int stdout_pipe[2];
350*4882a593Smuzhiyun    int stderr_pipe[2];
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun    newargv[0] = [X11App prefs_get_string:@PREFS_LOGIN_SHELL default:"/bin/sh"];
353*4882a593Smuzhiyun    newargv[1] = "-c";
354*4882a593Smuzhiyun    newargv[2] = [filename UTF8String];
355*4882a593Smuzhiyun    newargv[3] = NULL;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun    s = getenv("DISPLAY");
358*4882a593Smuzhiyun    if (s == NULL || s[0] == 0) {
359*4882a593Smuzhiyun        snprintf(buf, sizeof(buf), ":%s", display);
360*4882a593Smuzhiyun        setenv("DISPLAY", buf, TRUE);
361*4882a593Smuzhiyun    }
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun    if (&asl_log_descriptor) {
364*4882a593Smuzhiyun        char *asl_sender;
365*4882a593Smuzhiyun        aslmsg amsg = asl_new(ASL_TYPE_MSG);
366*4882a593Smuzhiyun        assert(amsg);
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun        asprintf(&asl_sender, "%s.%s", bundle_id_prefix, newargv[2]);
369*4882a593Smuzhiyun        assert(asl_sender);
370*4882a593Smuzhiyun        for(s = asl_sender + strlen(bundle_id_prefix) + 1; *s; s++) {
371*4882a593Smuzhiyun            if(! ((*s >= 'a' && *s <= 'z') ||
372*4882a593Smuzhiyun                  (*s >= 'A' && *s <= 'Z') ||
373*4882a593Smuzhiyun                  (*s >= '0' && *s <= '9'))) {
374*4882a593Smuzhiyun                *s = '_';
375*4882a593Smuzhiyun            }
376*4882a593Smuzhiyun        }
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun        (void)asl_set(amsg, ASL_KEY_SENDER, asl_sender);
379*4882a593Smuzhiyun        free(asl_sender);
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun        assert(0 == pipe(stdout_pipe));
382*4882a593Smuzhiyun        fcntl(stdout_pipe[0], F_SETFD, FD_CLOEXEC);
383*4882a593Smuzhiyun        fcntl(stdout_pipe[0], F_SETFL, O_NONBLOCK);
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun        assert(0 == pipe(stderr_pipe));
386*4882a593Smuzhiyun        fcntl(stderr_pipe[0], F_SETFD, FD_CLOEXEC);
387*4882a593Smuzhiyun        fcntl(stderr_pipe[0], F_SETFL, O_NONBLOCK);
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun        asl_log_descriptor(aslc, amsg, ASL_LEVEL_INFO, stdout_pipe[0], ASL_LOG_DESCRIPTOR_READ);
390*4882a593Smuzhiyun        asl_log_descriptor(aslc, amsg, ASL_LEVEL_NOTICE, stderr_pipe[0], ASL_LOG_DESCRIPTOR_READ);
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun        asl_free(amsg);
393*4882a593Smuzhiyun    }
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun    /* Do the fork-twice trick to avoid having to reap zombies */
396*4882a593Smuzhiyun    child1 = fork();
397*4882a593Smuzhiyun    switch (child1) {
398*4882a593Smuzhiyun    case -1:                                    /* error */
399*4882a593Smuzhiyun        break;
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun    case 0:                                     /* child1 */
402*4882a593Smuzhiyun        child2 = fork();
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun        switch (child2) {
405*4882a593Smuzhiyun            int max_files, i;
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun        case -1:                                    /* error */
408*4882a593Smuzhiyun            _exit(1);
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun        case 0:                                     /* child2 */
411*4882a593Smuzhiyun            if (&asl_log_descriptor) {
412*4882a593Smuzhiyun                /* Replace our stdout/stderr */
413*4882a593Smuzhiyun                dup2(stdout_pipe[1], STDOUT_FILENO);
414*4882a593Smuzhiyun                dup2(stderr_pipe[1], STDERR_FILENO);
415*4882a593Smuzhiyun            }
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun            /* close all open files except for standard streams */
418*4882a593Smuzhiyun            max_files = sysconf(_SC_OPEN_MAX);
419*4882a593Smuzhiyun            for (i = 3; i < max_files; i++)
420*4882a593Smuzhiyun                close(i);
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun            /* ensure stdin is on /dev/null */
423*4882a593Smuzhiyun            close(0);
424*4882a593Smuzhiyun            open("/dev/null", O_RDONLY);
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun            execvp(newargv[0], (char * *const)newargv);
427*4882a593Smuzhiyun            _exit(2);
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun        default:                                    /* parent (child1) */
430*4882a593Smuzhiyun            _exit(0);
431*4882a593Smuzhiyun        }
432*4882a593Smuzhiyun        break;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun    default:                                    /* parent */
435*4882a593Smuzhiyun        waitpid(child1, &status, 0);
436*4882a593Smuzhiyun    }
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun    if (&asl_log_descriptor) {
439*4882a593Smuzhiyun        /* Close the write ends of the pipe */
440*4882a593Smuzhiyun        close(stdout_pipe[1]);
441*4882a593Smuzhiyun        close(stderr_pipe[1]);
442*4882a593Smuzhiyun    }
443*4882a593Smuzhiyun}
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun- (void) app_selected:sender
446*4882a593Smuzhiyun{
447*4882a593Smuzhiyun    int tag;
448*4882a593Smuzhiyun    NSString *item;
449*4882a593Smuzhiyun    NSArray * const apps = self.apps;
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun    tag = [sender tag] - 1;
452*4882a593Smuzhiyun    if (apps == nil || tag < 0 || tag >= [apps count])
453*4882a593Smuzhiyun        return;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun    item = [[apps objectAtIndex:tag] objectAtIndex:1];
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun    [self launch_client:item];
458*4882a593Smuzhiyun}
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun- (IBAction) apps_table_show:sender
461*4882a593Smuzhiyun{
462*4882a593Smuzhiyun    NSArray *columns;
463*4882a593Smuzhiyun    NSMutableArray *oldapps = self.table_apps;
464*4882a593Smuzhiyun    NSTableView * const apps_table = self.apps_table;
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun    NSMutableArray * const table_apps = [[NSMutableArray alloc] initWithCapacity:1];
467*4882a593Smuzhiyun    self.table_apps = table_apps;
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun    NSArray * const apps = self.apps;
470*4882a593Smuzhiyun    if (apps != nil)
471*4882a593Smuzhiyun        [table_apps addObjectsFromArray:apps];
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun    columns = [apps_table tableColumns];
474*4882a593Smuzhiyun    [[columns objectAtIndex:0] setIdentifier:@"0"];
475*4882a593Smuzhiyun    [[columns objectAtIndex:1] setIdentifier:@"1"];
476*4882a593Smuzhiyun    [[columns objectAtIndex:2] setIdentifier:@"2"];
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun    [apps_table setDataSource:self];
479*4882a593Smuzhiyun    [apps_table selectRowIndexes:[NSIndexSet indexSetWithIndex:0]
480*4882a593Smuzhiyun     byExtendingSelection:NO];
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun    [[apps_table window] makeKeyAndOrderFront:sender];
483*4882a593Smuzhiyun    [apps_table reloadData];
484*4882a593Smuzhiyun    if (oldapps != nil)
485*4882a593Smuzhiyun        [oldapps release];
486*4882a593Smuzhiyun}
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun- (IBAction) apps_table_done:sender
489*4882a593Smuzhiyun{
490*4882a593Smuzhiyun    NSMutableArray * const table_apps = self.table_apps;
491*4882a593Smuzhiyun    NSTableView * const apps_table = self.apps_table;
492*4882a593Smuzhiyun    [apps_table deselectAll:sender];    /* flush edits? */
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun    [self remove_apps_menu];
495*4882a593Smuzhiyun    [self install_apps_menu:table_apps];
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun    [NSApp prefs_set_array:@PREFS_APPSMENU value:table_apps];
498*4882a593Smuzhiyun    [NSApp prefs_synchronize];
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun    [[apps_table window] orderOut:sender];
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun    self.table_apps = nil;
503*4882a593Smuzhiyun}
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun- (IBAction) apps_table_new:sender
506*4882a593Smuzhiyun{
507*4882a593Smuzhiyun    NSMutableArray *item;
508*4882a593Smuzhiyun    NSMutableArray * const table_apps = self.table_apps;
509*4882a593Smuzhiyun    NSTableView * const apps_table = self.apps_table;
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun    int row = [apps_table selectedRow], i;
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun    if (row < 0) row = 0;
514*4882a593Smuzhiyun    else row = row + 1;
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun    i = row;
517*4882a593Smuzhiyun    if (i > [table_apps count])
518*4882a593Smuzhiyun        return;                         /* avoid exceptions */
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun    [apps_table deselectAll:sender];
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun    item = [[NSMutableArray alloc] initWithCapacity:3];
523*4882a593Smuzhiyun    [item addObject:@""];
524*4882a593Smuzhiyun    [item addObject:@""];
525*4882a593Smuzhiyun    [item addObject:@""];
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun    [table_apps insertObject:item atIndex:i];
528*4882a593Smuzhiyun    [item release];
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun    [apps_table reloadData];
531*4882a593Smuzhiyun    [apps_table selectRowIndexes:[NSIndexSet indexSetWithIndex:row]
532*4882a593Smuzhiyun     byExtendingSelection:NO];
533*4882a593Smuzhiyun}
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun- (IBAction) apps_table_duplicate:sender
536*4882a593Smuzhiyun{
537*4882a593Smuzhiyun    NSMutableArray * const table_apps = self.table_apps;
538*4882a593Smuzhiyun    NSTableView * const apps_table = self.apps_table;
539*4882a593Smuzhiyun    int row = [apps_table selectedRow], i;
540*4882a593Smuzhiyun    NSObject *item;
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun    if (row < 0) {
543*4882a593Smuzhiyun        [self apps_table_new:sender];
544*4882a593Smuzhiyun        return;
545*4882a593Smuzhiyun    }
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun    i = row;
548*4882a593Smuzhiyun    if (i > [table_apps count] - 1) return;                             /* avoid exceptions */
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun    [apps_table deselectAll:sender];
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun    item = [[table_apps objectAtIndex:i] mutableCopy];
553*4882a593Smuzhiyun    [table_apps insertObject:item atIndex:i];
554*4882a593Smuzhiyun    [item release];
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun    [apps_table reloadData];
557*4882a593Smuzhiyun    [apps_table selectRowIndexes:[NSIndexSet indexSetWithIndex:row +
558*4882a593Smuzhiyun                                  1] byExtendingSelection:NO];
559*4882a593Smuzhiyun}
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun- (IBAction) apps_table_delete:sender
562*4882a593Smuzhiyun{
563*4882a593Smuzhiyun    NSMutableArray * const table_apps = self.table_apps;
564*4882a593Smuzhiyun    NSTableView * const apps_table = self.apps_table;
565*4882a593Smuzhiyun    int row = [apps_table selectedRow];
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun    if (row >= 0) {
568*4882a593Smuzhiyun        int i = row;
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun        if (i > [table_apps count] - 1) return;                 /* avoid exceptions */
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun        [apps_table deselectAll:sender];
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun        [table_apps removeObjectAtIndex:i];
575*4882a593Smuzhiyun    }
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun    [apps_table reloadData];
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun    row = MIN(row, [table_apps count] - 1);
580*4882a593Smuzhiyun    if (row >= 0)
581*4882a593Smuzhiyun        [apps_table selectRowIndexes:[NSIndexSet indexSetWithIndex:row]
582*4882a593Smuzhiyun         byExtendingSelection:NO];
583*4882a593Smuzhiyun}
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun- (NSInteger) numberOfRowsInTableView:(NSTableView *)tableView
586*4882a593Smuzhiyun{
587*4882a593Smuzhiyun    NSMutableArray * const table_apps = self.table_apps;
588*4882a593Smuzhiyun    if (table_apps == nil) return 0;
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun    return [table_apps count];
591*4882a593Smuzhiyun}
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun- (id)             tableView:(NSTableView *)tableView
594*4882a593Smuzhiyun   objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
595*4882a593Smuzhiyun{
596*4882a593Smuzhiyun    NSMutableArray * const table_apps = self.table_apps;
597*4882a593Smuzhiyun    NSArray *item;
598*4882a593Smuzhiyun    int col;
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun    if (table_apps == nil) return nil;
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun    col = [[tableColumn identifier] intValue];
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun    item = [table_apps objectAtIndex:row];
605*4882a593Smuzhiyun    if ([item count] > col)
606*4882a593Smuzhiyun        return [item objectAtIndex:col];
607*4882a593Smuzhiyun    else
608*4882a593Smuzhiyun        return @"";
609*4882a593Smuzhiyun}
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun- (void) tableView:(NSTableView *)tableView setObjectValue:(id)object
612*4882a593Smuzhiyun    forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
613*4882a593Smuzhiyun{
614*4882a593Smuzhiyun    NSMutableArray * const table_apps = self.table_apps;
615*4882a593Smuzhiyun    NSMutableArray *item;
616*4882a593Smuzhiyun    int col;
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun    if (table_apps == nil) return;
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun    col = [[tableColumn identifier] intValue];
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun    item = [table_apps objectAtIndex:row];
623*4882a593Smuzhiyun    [item replaceObjectAtIndex:col withObject:object];
624*4882a593Smuzhiyun}
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun- (void) hide_window:sender
627*4882a593Smuzhiyun{
628*4882a593Smuzhiyun    if ([X11App x_active])
629*4882a593Smuzhiyun        DarwinSendDDXEvent(kXquartzControllerNotify, 1, AppleWMHideWindow);
630*4882a593Smuzhiyun    else
631*4882a593Smuzhiyun        NSBeep();                       /* FIXME: something here */
632*4882a593Smuzhiyun}
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun- (IBAction)bring_to_front:sender
635*4882a593Smuzhiyun{
636*4882a593Smuzhiyun    DarwinSendDDXEvent(kXquartzControllerNotify, 1, AppleWMBringAllToFront);
637*4882a593Smuzhiyun}
638*4882a593Smuzhiyun
639*4882a593Smuzhiyun- (IBAction)close_window:sender
640*4882a593Smuzhiyun{
641*4882a593Smuzhiyun    if ([X11App x_active])
642*4882a593Smuzhiyun        DarwinSendDDXEvent(kXquartzControllerNotify, 1, AppleWMCloseWindow);
643*4882a593Smuzhiyun    else
644*4882a593Smuzhiyun        [[NSApp keyWindow] performClose:sender];
645*4882a593Smuzhiyun}
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun- (IBAction)minimize_window:sender
648*4882a593Smuzhiyun{
649*4882a593Smuzhiyun    if ([X11App x_active])
650*4882a593Smuzhiyun        DarwinSendDDXEvent(kXquartzControllerNotify, 1, AppleWMMinimizeWindow);
651*4882a593Smuzhiyun    else
652*4882a593Smuzhiyun        [[NSApp keyWindow] performMiniaturize:sender];
653*4882a593Smuzhiyun}
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun- (IBAction)zoom_window:sender
656*4882a593Smuzhiyun{
657*4882a593Smuzhiyun    if ([X11App x_active])
658*4882a593Smuzhiyun        DarwinSendDDXEvent(kXquartzControllerNotify, 1, AppleWMZoomWindow);
659*4882a593Smuzhiyun    else
660*4882a593Smuzhiyun        [[NSApp keyWindow] performZoom:sender];
661*4882a593Smuzhiyun}
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun- (IBAction) next_window:sender
664*4882a593Smuzhiyun{
665*4882a593Smuzhiyun    DarwinSendDDXEvent(kXquartzControllerNotify, 1, AppleWMNextWindow);
666*4882a593Smuzhiyun}
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun- (IBAction) previous_window:sender
669*4882a593Smuzhiyun{
670*4882a593Smuzhiyun    DarwinSendDDXEvent(kXquartzControllerNotify, 1, AppleWMPreviousWindow);
671*4882a593Smuzhiyun}
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun- (IBAction) enable_fullscreen_changed:sender
674*4882a593Smuzhiyun{
675*4882a593Smuzhiyun    XQuartzRootlessDefault = !self.enable_fullscreen.intValue;
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun    [self.enable_fullscreen_menu setEnabled:!XQuartzRootlessDefault];
678*4882a593Smuzhiyun    [self.enable_fullscreen_menu_text setTextColor:XQuartzRootlessDefault ? NSColor.disabledControlTextColor : NSColor.controlTextColor];
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun    DarwinSendDDXEvent(kXquartzSetRootless, 1, XQuartzRootlessDefault);
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun    [NSApp prefs_set_boolean:@PREFS_ROOTLESS value:XQuartzRootlessDefault];
683*4882a593Smuzhiyun    [NSApp prefs_synchronize];
684*4882a593Smuzhiyun}
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun- (IBAction) toggle_fullscreen:sender
687*4882a593Smuzhiyun{
688*4882a593Smuzhiyun    DarwinSendDDXEvent(kXquartzToggleFullscreen, 0);
689*4882a593Smuzhiyun}
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun- (IBAction)prefs_changed:sender
692*4882a593Smuzhiyun{
693*4882a593Smuzhiyun    if (!sender)
694*4882a593Smuzhiyun        return;
695*4882a593Smuzhiyun
696*4882a593Smuzhiyun    if (sender == self.fake_buttons) {
697*4882a593Smuzhiyun        darwinFakeButtons = self.fake_buttons.intValue;
698*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_FAKEBUTTONS value:darwinFakeButtons];
699*4882a593Smuzhiyun    }
700*4882a593Smuzhiyun    else if (sender == self.enable_keyequivs) {
701*4882a593Smuzhiyun        XQuartzEnableKeyEquivalents = self.enable_keyequivs.intValue;
702*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_KEYEQUIVS value:
703*4882a593Smuzhiyun         XQuartzEnableKeyEquivalents];
704*4882a593Smuzhiyun    }
705*4882a593Smuzhiyun    else if (sender == self.sync_keymap) {
706*4882a593Smuzhiyun        darwinSyncKeymap = self.sync_keymap.intValue;
707*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_SYNC_KEYMAP value:darwinSyncKeymap];
708*4882a593Smuzhiyun    }
709*4882a593Smuzhiyun    else if (sender == self.enable_fullscreen_menu) {
710*4882a593Smuzhiyun        XQuartzFullscreenMenu = self.enable_fullscreen_menu.intValue;
711*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_FULLSCREEN_MENU value:
712*4882a593Smuzhiyun         XQuartzFullscreenMenu];
713*4882a593Smuzhiyun    }
714*4882a593Smuzhiyun    else if (sender == self.option_sends_alt) {
715*4882a593Smuzhiyun        BOOL prev_opt_sends_alt = XQuartzOptionSendsAlt;
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun        XQuartzOptionSendsAlt = self.option_sends_alt.intValue;
718*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_OPTION_SENDS_ALT value:
719*4882a593Smuzhiyun         XQuartzOptionSendsAlt];
720*4882a593Smuzhiyun
721*4882a593Smuzhiyun        if (prev_opt_sends_alt != XQuartzOptionSendsAlt)
722*4882a593Smuzhiyun            QuartsResyncKeymap(TRUE);
723*4882a593Smuzhiyun    }
724*4882a593Smuzhiyun    else if (sender == self.click_through) {
725*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_CLICK_THROUGH value:self.click_through.intValue];
726*4882a593Smuzhiyun    }
727*4882a593Smuzhiyun    else if (sender == self.focus_follows_mouse) {
728*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_FFM value:self.focus_follows_mouse.intValue];
729*4882a593Smuzhiyun    }
730*4882a593Smuzhiyun    else if (sender == self.focus_on_new_window) {
731*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_FOCUS_ON_NEW_WINDOW value:self.focus_on_new_window.intValue];
732*4882a593Smuzhiyun    }
733*4882a593Smuzhiyun    else if (sender == self.enable_auth) {
734*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_NO_AUTH value:!self.enable_auth.intValue];
735*4882a593Smuzhiyun    }
736*4882a593Smuzhiyun    else if (sender == self.enable_tcp) {
737*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_NO_TCP value:!self.enable_tcp.intValue];
738*4882a593Smuzhiyun    }
739*4882a593Smuzhiyun    else if (sender == self.depth) {
740*4882a593Smuzhiyun        [NSApp prefs_set_integer:@PREFS_DEPTH value:self.depth.selectedTag];
741*4882a593Smuzhiyun    }
742*4882a593Smuzhiyun    else if (sender == self.sync_pasteboard) {
743*4882a593Smuzhiyun        BOOL pbproxy_active = self.sync_pasteboard.intValue;
744*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_SYNC_PB value:pbproxy_active];
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun        [self.sync_pasteboard_to_clipboard setEnabled:pbproxy_active];
747*4882a593Smuzhiyun        [self.sync_pasteboard_to_primary setEnabled:pbproxy_active];
748*4882a593Smuzhiyun        [self.sync_clipboard_to_pasteboard setEnabled:pbproxy_active];
749*4882a593Smuzhiyun        [self.sync_primary_immediately setEnabled:pbproxy_active];
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun        // setEnabled doesn't do this...
752*4882a593Smuzhiyun        [self.sync_text1 setTextColor:pbproxy_active ? NSColor.controlTextColor : NSColor.disabledControlTextColor];
753*4882a593Smuzhiyun        [self.sync_text2 setTextColor:pbproxy_active ? NSColor.controlTextColor : NSColor.disabledControlTextColor];
754*4882a593Smuzhiyun    }
755*4882a593Smuzhiyun    else if (sender == self.sync_pasteboard_to_clipboard) {
756*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_SYNC_PB_TO_CLIPBOARD value:self.sync_pasteboard_to_clipboard.intValue];
757*4882a593Smuzhiyun    }
758*4882a593Smuzhiyun    else if (sender == self.sync_pasteboard_to_primary) {
759*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_SYNC_PB_TO_PRIMARY value:self.sync_pasteboard_to_primary.intValue];
760*4882a593Smuzhiyun    }
761*4882a593Smuzhiyun    else if (sender == self.sync_clipboard_to_pasteboard) {
762*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_SYNC_CLIPBOARD_TO_PB value:self.sync_clipboard_to_pasteboard.intValue];
763*4882a593Smuzhiyun    }
764*4882a593Smuzhiyun    else if (sender == self.sync_primary_immediately) {
765*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_SYNC_PRIMARY_ON_SELECT value:self.sync_primary_immediately.intValue];
766*4882a593Smuzhiyun    }
767*4882a593Smuzhiyun    else if (sender == self.scroll_in_device_direction) {
768*4882a593Smuzhiyun        XQuartzScrollInDeviceDirection = self.scroll_in_device_direction.intValue;
769*4882a593Smuzhiyun        [NSApp prefs_set_boolean:@PREFS_SCROLL_IN_DEV_DIRECTION value:XQuartzScrollInDeviceDirection];
770*4882a593Smuzhiyun    }
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun    [NSApp prefs_synchronize];
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun    DarwinSendDDXEvent(kXquartzReloadPreferences, 0);
775*4882a593Smuzhiyun}
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun- (IBAction) prefs_show:sender
778*4882a593Smuzhiyun{
779*4882a593Smuzhiyun    BOOL pbproxy_active =
780*4882a593Smuzhiyun        [NSApp prefs_get_boolean:@PREFS_SYNC_PB default:YES];
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun    [self.scroll_in_device_direction setIntValue:XQuartzScrollInDeviceDirection];
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun    [self.fake_buttons setIntValue:darwinFakeButtons];
785*4882a593Smuzhiyun    [self.enable_keyequivs setIntValue:XQuartzEnableKeyEquivalents];
786*4882a593Smuzhiyun    [self.sync_keymap setIntValue:darwinSyncKeymap];
787*4882a593Smuzhiyun    [self.option_sends_alt setIntValue:XQuartzOptionSendsAlt];
788*4882a593Smuzhiyun    [self.click_through setIntValue:[NSApp prefs_get_boolean:@PREFS_CLICK_THROUGH default:NO]];
789*4882a593Smuzhiyun    [self.focus_follows_mouse setIntValue:[NSApp prefs_get_boolean:@PREFS_FFM default:NO]];
790*4882a593Smuzhiyun    [self.focus_on_new_window setIntValue:[NSApp prefs_get_boolean:@PREFS_FOCUS_ON_NEW_WINDOW default:YES]];
791*4882a593Smuzhiyun
792*4882a593Smuzhiyun    [self.enable_auth setIntValue:![NSApp prefs_get_boolean:@PREFS_NO_AUTH default:NO]];
793*4882a593Smuzhiyun    [self.enable_tcp setIntValue:![NSApp prefs_get_boolean:@PREFS_NO_TCP default:NO]];
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun    [self.depth selectItemAtIndex:[self.depth indexOfItemWithTag:[NSApp prefs_get_integer:@PREFS_DEPTH default:-1]]];
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun    [self.sync_pasteboard setIntValue:pbproxy_active];
798*4882a593Smuzhiyun    [self.sync_pasteboard_to_clipboard setIntValue:[NSApp prefs_get_boolean:@PREFS_SYNC_PB_TO_CLIPBOARD default:YES]];
799*4882a593Smuzhiyun    [self.sync_pasteboard_to_primary setIntValue:[NSApp prefs_get_boolean:@PREFS_SYNC_PB_TO_PRIMARY default:YES]];
800*4882a593Smuzhiyun    [self.sync_clipboard_to_pasteboard setIntValue:[NSApp prefs_get_boolean:@PREFS_SYNC_CLIPBOARD_TO_PB default:YES]];
801*4882a593Smuzhiyun    [self.sync_primary_immediately setIntValue:[NSApp prefs_get_boolean:@PREFS_SYNC_PRIMARY_ON_SELECT default:NO]];
802*4882a593Smuzhiyun
803*4882a593Smuzhiyun    [self.sync_pasteboard_to_clipboard setEnabled:pbproxy_active];
804*4882a593Smuzhiyun    [self.sync_pasteboard_to_primary setEnabled:pbproxy_active];
805*4882a593Smuzhiyun    [self.sync_clipboard_to_pasteboard setEnabled:pbproxy_active];
806*4882a593Smuzhiyun    [self.sync_primary_immediately setEnabled:pbproxy_active];
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun    // setEnabled doesn't do this...
809*4882a593Smuzhiyun    [self.sync_text1 setTextColor:pbproxy_active ? NSColor.controlTextColor : NSColor.disabledControlTextColor];
810*4882a593Smuzhiyun    [self.sync_text2 setTextColor:pbproxy_active ? NSColor.controlTextColor : NSColor.disabledControlTextColor];
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun    [self.enable_fullscreen setIntValue:!XQuartzRootlessDefault];
813*4882a593Smuzhiyun    [self.enable_fullscreen_menu setIntValue:XQuartzFullscreenMenu];
814*4882a593Smuzhiyun    [self.enable_fullscreen_menu setEnabled:!XQuartzRootlessDefault];
815*4882a593Smuzhiyun    [self.enable_fullscreen_menu_text setTextColor:XQuartzRootlessDefault ? NSColor.disabledControlTextColor : NSColor.controlTextColor];
816*4882a593Smuzhiyun
817*4882a593Smuzhiyun    [self.prefs_panel makeKeyAndOrderFront:sender];
818*4882a593Smuzhiyun}
819*4882a593Smuzhiyun
820*4882a593Smuzhiyun- (IBAction) quit:sender
821*4882a593Smuzhiyun{
822*4882a593Smuzhiyun    DarwinSendDDXEvent(kXquartzQuit, 0);
823*4882a593Smuzhiyun}
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun- (IBAction) x11_help:sender
826*4882a593Smuzhiyun{
827*4882a593Smuzhiyun    AHLookupAnchor(CFSTR("com.apple.machelp"), CFSTR("mchlp2276"));
828*4882a593Smuzhiyun}
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun- (OSX_BOOL) validateMenuItem:(NSMenuItem *)item
831*4882a593Smuzhiyun{
832*4882a593Smuzhiyun    NSMenu *menu = [item menu];
833*4882a593Smuzhiyun    NSMenu * const dock_menu = self.dock_menu;
834*4882a593Smuzhiyun
835*4882a593Smuzhiyun    if (item == self.toggle_fullscreen_item)
836*4882a593Smuzhiyun        return !XQuartzIsRootless;
837*4882a593Smuzhiyun    else if (menu == [X11App windowsMenu] || menu == dock_menu
838*4882a593Smuzhiyun             || (menu == [self.x11_about_item menu] && [item tag] == 42))
839*4882a593Smuzhiyun        return (AppleWMSelectedEvents() & AppleWMControllerNotifyMask) != 0;
840*4882a593Smuzhiyun    else
841*4882a593Smuzhiyun        return TRUE;
842*4882a593Smuzhiyun}
843*4882a593Smuzhiyun
844*4882a593Smuzhiyun- (void) applicationDidHide:(NSNotification *)notify
845*4882a593Smuzhiyun{
846*4882a593Smuzhiyun    DarwinSendDDXEvent(kXquartzControllerNotify, 1, AppleWMHideAll);
847*4882a593Smuzhiyun
848*4882a593Smuzhiyun    /* Toggle off fullscreen mode to leave our non-default video
849*4882a593Smuzhiyun     * mode and hide our guard window.
850*4882a593Smuzhiyun     */
851*4882a593Smuzhiyun    if (!XQuartzIsRootless && XQuartzFullscreenVisible) {
852*4882a593Smuzhiyun        DarwinSendDDXEvent(kXquartzToggleFullscreen, 0);
853*4882a593Smuzhiyun    }
854*4882a593Smuzhiyun}
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun- (void) applicationDidUnhide:(NSNotification *)notify
857*4882a593Smuzhiyun{
858*4882a593Smuzhiyun    DarwinSendDDXEvent(kXquartzControllerNotify, 1, AppleWMShowAll);
859*4882a593Smuzhiyun}
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun- (NSApplicationTerminateReply) applicationShouldTerminate:sender
862*4882a593Smuzhiyun{
863*4882a593Smuzhiyun    NSString *msg;
864*4882a593Smuzhiyun    NSString *title;
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun    if (self.can_quit ||
867*4882a593Smuzhiyun        [X11App prefs_get_boolean:@PREFS_NO_QUIT_ALERT default:NO])
868*4882a593Smuzhiyun        return NSTerminateNow;
869*4882a593Smuzhiyun
870*4882a593Smuzhiyun    /* Make sure we're frontmost. */
871*4882a593Smuzhiyun    [NSApp activateIgnoringOtherApps:YES];
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun    title = NSLocalizedString(@"Do you really want to quit X11?",
874*4882a593Smuzhiyun                              @"Dialog title when quitting");
875*4882a593Smuzhiyun    msg = NSLocalizedString(
876*4882a593Smuzhiyun        @"Any open X11 applications will stop immediately, and you will lose any unsaved changes.",
877*4882a593Smuzhiyun        @"Dialog when quitting");
878*4882a593Smuzhiyun
879*4882a593Smuzhiyun    /* FIXME: safe to run the alert in here? Or should we return Later
880*4882a593Smuzhiyun     *        and then run the alert on a timer? It seems to work here, so..
881*4882a593Smuzhiyun     */
882*4882a593Smuzhiyun
883*4882a593Smuzhiyun    NSInteger result = NSRunAlertPanel(title, @"%@", NSLocalizedString(@"Quit", @""),
884*4882a593Smuzhiyun                                       NSLocalizedString(@"Cancel", @""), nil, msg);
885*4882a593Smuzhiyun    return (result == NSAlertDefaultReturn) ? NSTerminateNow : NSTerminateCancel;
886*4882a593Smuzhiyun}
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun- (void) applicationWillTerminate:(NSNotification *)aNotification _X_NORETURN
889*4882a593Smuzhiyun{
890*4882a593Smuzhiyun    [X11App prefs_synchronize];
891*4882a593Smuzhiyun
892*4882a593Smuzhiyun    /* shutdown the X server, it will exit () for us. */
893*4882a593Smuzhiyun    DarwinSendDDXEvent(kXquartzQuit, 0);
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun    /* In case it doesn't, exit anyway after 5s. */
896*4882a593Smuzhiyun    [NSThread sleepForTimeInterval:5.0];
897*4882a593Smuzhiyun
898*4882a593Smuzhiyun    exit(1);
899*4882a593Smuzhiyun}
900*4882a593Smuzhiyun
901*4882a593Smuzhiyun- (void) server_ready
902*4882a593Smuzhiyun{
903*4882a593Smuzhiyun    x_list *node;
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun    self.finished_launching = YES;
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun    for (node = self.pending_apps; node != NULL; node = node->next) {
908*4882a593Smuzhiyun        NSString *filename = node->data;
909*4882a593Smuzhiyun        [self launch_client:filename];
910*4882a593Smuzhiyun        [filename release];
911*4882a593Smuzhiyun    }
912*4882a593Smuzhiyun
913*4882a593Smuzhiyun    x_list_free(self.pending_apps);
914*4882a593Smuzhiyun    self.pending_apps = NULL;
915*4882a593Smuzhiyun}
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun- (OSX_BOOL) application:(NSApplication *)app openFile:(NSString *)filename
918*4882a593Smuzhiyun{
919*4882a593Smuzhiyun    const char *name = [filename UTF8String];
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun    if (self.finished_launching)
922*4882a593Smuzhiyun        [self launch_client:filename];
923*4882a593Smuzhiyun    else if (name[0] != ':')            /* ignore display names */
924*4882a593Smuzhiyun        self.pending_apps = x_list_prepend(self.pending_apps, [filename retain]);
925*4882a593Smuzhiyun
926*4882a593Smuzhiyun    /* FIXME: report failures. */
927*4882a593Smuzhiyun    return YES;
928*4882a593Smuzhiyun}
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun@end
931*4882a593Smuzhiyun
932*4882a593Smuzhiyunvoid
933*4882a593SmuzhiyunX11ControllerMain(int argc, char **argv, char **envp)
934*4882a593Smuzhiyun{
935*4882a593Smuzhiyun    X11ApplicationMain(argc, argv, envp);
936*4882a593Smuzhiyun}
937