Skip to content

Commit

Permalink
Allow MacPlatformWindow to take an OS X window number.
Browse files Browse the repository at this point in the history
  • Loading branch information
rygwdn committed Jun 13, 2014
1 parent 3f7d1b0 commit 65c6059
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/platform/mac/macplatformwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class MacPlatformWindow : public PlatformWindow

private:
// Don't allow copies
Q_DISABLE_COPY(MacPlatformWindow);
Q_DISABLE_COPY(MacPlatformWindow)

long int m_windowNumber;
NSWindow *m_window;
Expand Down
39 changes: 34 additions & 5 deletions src/platform/mac/macplatformwindow.mm
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ void delayedPaste(int64_t delayInMS, uint tries, NSWindow *window, NSRunningAppl
});
}

pid_t getPidForWid(WId find_wid) {
// Build a set of "normal" windows. This is necessary as "NSWindowList" gets things like the
// menubar (which can be "owned" by various apps).
NSArray *array = (__bridge NSArray*) CGWindowListCopyWindowInfo(kCGWindowListOptionAll | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
for (NSDictionary* dict in array) {
long int pid = [(NSNumber*)[dict objectForKey:@"kCGWindowOwnerPID"] longValue];
unsigned long int wid = (unsigned long) [(NSNumber*)[dict objectForKey:@"kCGWindowNumber"] longValue];

if (wid == find_wid) {
return pid;
}
}

return 0;
}


long int getTopWindow(pid_t process_pid) {
// Build a set of "normal" windows. This is necessary as "NSWindowList" gets things like the
// menubar (which can be "owned" by various apps).
Expand Down Expand Up @@ -123,15 +140,17 @@ long int getTopWindow(pid_t process_pid) {
}

QString getTitleFromWid(long int wid) {
QString title;

if (wid < 0) {
return QString();
return title;
}

uint32_t windowid[1] = {wid};
CFArrayRef windowArray = CFArrayCreate ( NULL, (const void **)windowid, 1 ,NULL);
NSArray *array = (__bridge NSArray*) CGWindowListCreateDescriptionFromArray(windowArray);

QString title;
// Should only be one
for (NSDictionary* dict in array) {
title = QString::fromNSString([dict objectForKey:@"kCGWindowName"]);
}
Expand All @@ -144,6 +163,7 @@ QString getTitleFromWid(long int wid) {
MacPlatformWindow::MacPlatformWindow(NSRunningApplication *runningApp):
m_windowNumber(-1)
, m_window(0)
, m_runningApplication(0)
{
if (runningApp) {
m_runningApplication = runningApp;
Expand All @@ -157,14 +177,23 @@ QString getTitleFromWid(long int wid) {

MacPlatformWindow::MacPlatformWindow(WId wid):
m_windowNumber(-1)
, m_window(0)
, m_runningApplication(0)
{
NSView *view = objc_cast<NSView>((id)wid);
if (view) {
// Try using wid as an actual window ID
pid_t pid = getPidForWid(wid);
if (pid != 0) {
m_runningApplication = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
m_windowNumber = wid;
// This will return 'nil' unless this process owns the window
m_window = [NSApp windowWithWindowNumber: wid];
} else if (NSView *view = objc_cast<NSView>((id)wid)) {
// If given a view, its ours
m_runningApplication = [NSRunningApplication currentApplication];
m_window = [view window];
[m_runningApplication retain];
[m_window retain];
m_windowNumber = [m_window windowNumber];
COPYQ_LOG("Created platform window for copyq");
} else {
log("Failed to convert WId to window", LogWarning);
Expand Down Expand Up @@ -226,7 +255,7 @@ QString getTitleFromWid(long int wid) {
//[m_runningApplication unhide];

COPYQ_LOG(QString("Raise running app."));
[m_runningApplication activateWithOptions:0];
[m_runningApplication activateWithOptions:NSApplicationActivateIgnoringOtherApps];
} else {
::log(QString("Tried to raise unknown window."), LogWarning);
}
Expand Down

0 comments on commit 65c6059

Please sign in to comment.