Skip to content

Commit

Permalink
Sync OS X's and XMonad's focused window
Browse files Browse the repository at this point in the history
  • Loading branch information
puffnfresh committed Oct 7, 2012
1 parent e8935bc commit 8a47b06
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions OSXMonad/Core.hs
Expand Up @@ -31,6 +31,10 @@ updateWindow :: Window -> IO ()
updateWindow window = do
with window setWindow

focusWindow :: Window -> IO ()
focusWindow window = do
with window setWindowFocused

rectangleWindow :: Rectangle -> Window -> Window
rectangleWindow (Rectangle x y w h) win =
win { pos = pos', size = size' }
Expand Down Expand Up @@ -92,11 +96,14 @@ tile' context = do
(rectangles, _) <- C.runLayout (S.workspace . S.current $ ws) rect

let namedWindowsById = zip wids namedWindows
focusedWindow = S.peek newStack >>= flip lookup namedWindowsById
windows' = Maybe.catMaybes
$ map (\(i, r) ->
fmap (rectangleWindow r) (lookup i namedWindowsById)
) rectangles

maybe (return ()) (XM.io . focusWindow) focusedWindow

if null namedWindows
then return ()
else XM.io $ mapM_ updateWindow windows'
Expand Down
3 changes: 3 additions & 0 deletions OSXMonad/Window.hsc
Expand Up @@ -15,6 +15,9 @@ type ArchCFloat = CFloat
foreign import ccall "ApplicationServices/ApplicationServices.h AXAPIEnabled"
axAPIEnabled :: IO Bool

foreign import ccall "utils.h setWindowFocused"
setWindowFocused :: Ptr Window -> IO ()

foreign import ccall "utils.h setWindow"
setWindow :: Ptr Window -> IO ()

Expand Down
1 change: 1 addition & 0 deletions src/utils.h
Expand Up @@ -30,6 +30,7 @@ Event globalEvent;
int getWindows(Windows *);
void freeWindows(Windows *);
void setWindow(Window *);
void setWindowFocused(Window *);
void getFrame(CGPoint *, CGSize *);
void setupEventCallback();
void collectEvent();
Expand Down
8 changes: 8 additions & 0 deletions src/utils.m
Expand Up @@ -70,6 +70,14 @@ void setWindow(Window *window) {
setWindowSize(window->size, window->uiElement);
}

void setWindowFocused(Window *window) {
AXUIElementSetAttributeValue(window->uiElement, kAXMainAttribute, kCFBooleanTrue);

AXUIElementRef application;
AXUIElementCopyAttributeValue(window->uiElement, kAXParentAttribute, &application);
AXUIElementSetAttributeValue(application, kAXFrontmostAttribute, kCFBooleanTrue);
}

void addWindows(CFArrayRef windows, Windows *context, int *count) {
int j;
for(j = 0; j < CFArrayGetCount(windows) && *count < WINDOWS_ELEMENTS_LENGTH; j++) {
Expand Down

0 comments on commit 8a47b06

Please sign in to comment.