|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 Lennart Glauer <mail@lennart-glauer.de> |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 2 or (at your option) |
| 7 | + * version 3 of the License. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +#import "AppKitImpl.h" |
| 19 | + |
| 20 | +#import <AppKit/NSWorkspace.h> |
| 21 | + |
| 22 | +@implementation AppKitImpl |
| 23 | + |
| 24 | +AppKit::AppKit() |
| 25 | +{ |
| 26 | + self = [[AppKitImpl alloc] init]; |
| 27 | + [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:static_cast<id>(self) |
| 28 | + selector:@selector(didDeactivateApplicationObserver:) |
| 29 | + name:NSWorkspaceDidDeactivateApplicationNotification |
| 30 | + object:nil]; |
| 31 | +} |
| 32 | + |
| 33 | +AppKit::~AppKit() |
| 34 | +{ |
| 35 | + [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:static_cast<id>(self)]; |
| 36 | + [static_cast<id>(self) dealloc]; |
| 37 | +} |
| 38 | + |
| 39 | +// |
| 40 | +// Update last active application property |
| 41 | +// |
| 42 | +- (void) didDeactivateApplicationObserver:(NSNotification *) notification |
| 43 | +{ |
| 44 | + NSDictionary *userInfo = notification.userInfo; |
| 45 | + NSRunningApplication *app = userInfo[NSWorkspaceApplicationKey]; |
| 46 | + |
| 47 | + if (app.processIdentifier != [self ownProcessId]) { |
| 48 | + self.lastActiveApplication = app; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// |
| 53 | +// Get process id of frontmost application (-> keyboard input) |
| 54 | +// |
| 55 | +- (pid_t) activeProcessId |
| 56 | +{ |
| 57 | + return [NSWorkspace sharedWorkspace].frontmostApplication.processIdentifier; |
| 58 | +} |
| 59 | + |
| 60 | +// |
| 61 | +// Get process id of own process |
| 62 | +// |
| 63 | +- (pid_t) ownProcessId |
| 64 | +{ |
| 65 | + return [NSProcessInfo processInfo].processIdentifier; |
| 66 | +} |
| 67 | + |
| 68 | +// |
| 69 | +// Activate application by process id |
| 70 | +// |
| 71 | +- (bool) activateProcess:(pid_t) pid |
| 72 | +{ |
| 73 | + NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid]; |
| 74 | + |
| 75 | + return [app activateWithOptions:NSApplicationActivateIgnoringOtherApps]; |
| 76 | +} |
| 77 | + |
| 78 | +// |
| 79 | +// ------------------------- C++ Trampolines ------------------------- |
| 80 | +// |
| 81 | + |
| 82 | +pid_t AppKit::lastActiveProcessId() |
| 83 | +{ |
| 84 | + return [static_cast<id>(self) lastActiveApplication].processIdentifier; |
| 85 | +} |
| 86 | + |
| 87 | +pid_t AppKit::activeProcessId() |
| 88 | +{ |
| 89 | + return [static_cast<id>(self) activeProcessId]; |
| 90 | +} |
| 91 | + |
| 92 | +pid_t AppKit::ownProcessId() |
| 93 | +{ |
| 94 | + return [static_cast<id>(self) ownProcessId]; |
| 95 | +} |
| 96 | + |
| 97 | +bool AppKit::activateProcess(pid_t pid) |
| 98 | +{ |
| 99 | + return [static_cast<id>(self) activateProcess:pid]; |
| 100 | +} |
| 101 | + |
| 102 | +@end |
0 commit comments