/* * Copyright (C) 2016 Lennart Glauer * Copyright (C) 2017 KeePassXC Team * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 or (at your option) * version 3 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #import "AppKitImpl.h" #import @implementation AppKitImpl AppKit::AppKit() { self = [[AppKitImpl alloc] init]; [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:static_cast(self) selector:@selector(didDeactivateApplicationObserver:) name:NSWorkspaceDidDeactivateApplicationNotification object:nil]; } AppKit::~AppKit() { [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:static_cast(self)]; [static_cast(self) dealloc]; } // // Update last active application property // - (void) didDeactivateApplicationObserver:(NSNotification *) notification { NSDictionary *userInfo = notification.userInfo; NSRunningApplication *app = userInfo[NSWorkspaceApplicationKey]; if (app.processIdentifier != [self ownProcessId]) { self.lastActiveApplication = app; } } // // Get process id of frontmost application (-> keyboard input) // - (pid_t) activeProcessId { return [NSWorkspace sharedWorkspace].frontmostApplication.processIdentifier; } // // Get process id of own process // - (pid_t) ownProcessId { return [NSProcessInfo processInfo].processIdentifier; } // // Activate application by process id // - (bool) activateProcess:(pid_t) pid { NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid]; return [app activateWithOptions:NSApplicationActivateIgnoringOtherApps]; } // // ------------------------- C++ Trampolines ------------------------- // pid_t AppKit::lastActiveProcessId() { return [static_cast(self) lastActiveApplication].processIdentifier; } pid_t AppKit::activeProcessId() { return [static_cast(self) activeProcessId]; } pid_t AppKit::ownProcessId() { return [static_cast(self) ownProcessId]; } bool AppKit::activateProcess(pid_t pid) { return [static_cast(self) activateProcess:pid]; } @end