<commit_before>#import "Cocoa/Cocoa.h"
#import "types.h"
#include "notifications.h"

extern void UpdateActiveSpace();
extern screen_info *GetDisplayOfWindow(window_info *Window);
extern bool GetWindowFocusedByOSX(AXUIElementRef *WindowRef);
extern void SetKwmFocus(AXUIElementRef WindowRef);
extern void GiveFocusToScreen(unsigned int ScreenIndex, tree_node *Focus, bool Mouse, bool UpdateFocus);
extern window_info *GetWindowByID(int WindowID);
extern int GetWindowIDFromRef(AXUIElementRef WindowRef);
extern space_info *GetActiveSpaceOfScreen(screen_info *Screen);
extern tree_node *GetTreeNodeFromWindowIDOrLinkNode(tree_node *RootNode, int WindowID);

extern kwm_focus KWMFocus;
extern kwm_screen KWMScreen;
extern kwm_thread KWMThread;

@interface MDWorkspaceWatcher : NSObject {
}
- (id)init;
@end

@implementation MDWorkspaceWatcher
- (id)init
{
    if ((self = [super init]))
    {
       [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
                selector:@selector(activeSpaceDidChange:)
                name:NSWorkspaceActiveSpaceDidChangeNotification
                object:nil];

       [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
                selector:@selector(didActivateApplication:)
                name:NSWorkspaceDidActivateApplicationNotification
                object:nil];
    }

    return self;
}

- (void)dealloc
{
    [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
    [super dealloc];
}

- (void)activeSpaceDidChange:(NSNotification *)notification
{
    UpdateActiveSpace();
}

- (void)didActivateApplication:(NSNotification *)notification
{
    pthread_mutex_lock(&KWMThread.Lock);

    pid_t ProcessID = [[notification.userInfo objectForKey:NSWorkspaceApplicationKey] processIdentifier];
    if(ProcessID != -1)
    {
        window_info *Window = KWMFocus.Window;
        screen_info *ScreenOfWindow = GetDisplayOfWindow(Window);
        if((Window && Window->PID != ProcessID) || !Window)
        {
            AXUIElementRef OSXWindowRef;
            if(GetWindowFocusedByOSX(&OSXWindowRef))
            {
                window_info *OSXWindow = GetWindowByID(GetWindowIDFromRef(OSXWindowRef));
                screen_info *Screen = GetDisplayOfWindow(OSXWindow);
                if(Window && Screen)
                {
                    space_info *Space = GetActiveSpaceOfScreen(Screen);
                    tree_node *TreeNode = GetTreeNodeFromWindowIDOrLinkNode(Space->RootNode, OSXWindow->WID);
                    if(TreeNode)
                    {
                        if(ScreenOfWindow && ScreenOfWindow != Screen)
                            GiveFocusToScreen(Screen->ID, NULL, false, false);

                        SetKwmFocus(OSXWindowRef);
                        if(Screen == ScreenOfWindow)
                            KWMFocus.InsertionPoint = KWMFocus.Cache;
                    }
                }
            }
        }
    }

    pthread_mutex_unlock(&KWMThread.Lock);
}

@end
<commit_msg>check the correct window variable<commit_after>#import "Cocoa/Cocoa.h"
#import "types.h"
#include "notifications.h"

extern void UpdateActiveSpace();
extern screen_info *GetDisplayOfWindow(window_info *Window);
extern bool GetWindowFocusedByOSX(AXUIElementRef *WindowRef);
extern void SetKwmFocus(AXUIElementRef WindowRef);
extern void GiveFocusToScreen(unsigned int ScreenIndex, tree_node *Focus, bool Mouse, bool UpdateFocus);
extern window_info *GetWindowByID(int WindowID);
extern int GetWindowIDFromRef(AXUIElementRef WindowRef);
extern space_info *GetActiveSpaceOfScreen(screen_info *Screen);
extern tree_node *GetTreeNodeFromWindowIDOrLinkNode(tree_node *RootNode, int WindowID);

extern kwm_focus KWMFocus;
extern kwm_screen KWMScreen;
extern kwm_thread KWMThread;

@interface MDWorkspaceWatcher : NSObject {
}
- (id)init;
@end

@implementation MDWorkspaceWatcher
- (id)init
{
    if ((self = [super init]))
    {
       [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
                selector:@selector(activeSpaceDidChange:)
                name:NSWorkspaceActiveSpaceDidChangeNotification
                object:nil];

       [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
                selector:@selector(didActivateApplication:)
                name:NSWorkspaceDidActivateApplicationNotification
                object:nil];
    }

    return self;
}

- (void)dealloc
{
    [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
    [super dealloc];
}

- (void)activeSpaceDidChange:(NSNotification *)notification
{
    UpdateActiveSpace();
}

- (void)didActivateApplication:(NSNotification *)notification
{
    pthread_mutex_lock(&KWMThread.Lock);

    pid_t ProcessID = [[notification.userInfo objectForKey:NSWorkspaceApplicationKey] processIdentifier];
    if(ProcessID != -1)
    {
        window_info *Window = KWMFocus.Window;
        screen_info *ScreenOfWindow = GetDisplayOfWindow(Window);
        if((Window && Window->PID != ProcessID) || !Window)
        {
            AXUIElementRef OSXWindowRef;
            if(GetWindowFocusedByOSX(&OSXWindowRef))
            {
                window_info *OSXWindow = GetWindowByID(GetWindowIDFromRef(OSXWindowRef));
                screen_info *OSXScreen = GetDisplayOfWindow(OSXWindow);
                if(OSXWindow && OSXScreen)
                {
                    space_info *OSXSpace = GetActiveSpaceOfScreen(OSXScreen);
                    tree_node *TreeNode = GetTreeNodeFromWindowIDOrLinkNode(OSXSpace->RootNode, OSXWindow->WID);
                    if(TreeNode)
                    {
                        GiveFocusToScreen(OSXScreen->ID, NULL, false, false);
                        SetKwmFocus(OSXWindowRef);
                        if(OSXScreen == ScreenOfWindow)
                            KWMFocus.InsertionPoint = KWMFocus.Cache;
                    }
                }
            }
        }
    }

    pthread_mutex_unlock(&KWMThread.Lock);
}

@end

{'fraction_non_alphanumeric': 0.08888888888888889, 'fraction_numerical': 0.0003316749585406302, 'mean_word_length': 2.9086195722618275, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2831902', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1659, 'n_words': 301}