<commit_before>#include "window.h"
#include "container.h"
#include "node.h"
#include "display.h"
#include "space.h"
#include "tree.h"
#include "application.h"
#include "border.h"
#include "helpers.h"
#include "rules.h"
#include "serializer.h"

#include "axlib/axlib.h"

#include <cmath>

#define internal static
#define local_persist static

extern std::map<CFStringRef, space_info> WindowTree;

extern ax_state AXState;
extern ax_display *FocusedDisplay;
extern ax_application *FocusedApplication;

extern kwm_screen KWMScreen;
extern kwm_focus KWMFocus;
extern kwm_toggles KWMToggles;
extern kwm_mode KWMMode;
extern kwm_tiling KWMTiling;
extern kwm_cache KWMCache;
extern kwm_path KWMPath;
extern kwm_thread KWMThread;
extern kwm_border MarkedBorder;
extern kwm_border FocusedBorder;

EVENT_CALLBACK(Callback_AXEvent_DisplayAdded)
{
    DEBUG("AXEvent_DisplayAdded");
}

EVENT_CALLBACK(Callback_AXEvent_DisplayRemoved)
{
    DEBUG("AXEvent_DisplayRemoved");
}

EVENT_CALLBACK(Callback_AXEvent_DisplayResized)
{
    DEBUG("AXEvent_DisplayResized");
}

EVENT_CALLBACK(Callback_AXEvent_DisplayMoved)
{
    DEBUG("AXEvent_DisplayMoved");
}

EVENT_CALLBACK(Callback_AXEvent_DisplayChanged)
{
    FocusedDisplay = AXLibMainDisplay();
    printf("%d: AXEvent_DisplayChanged\n", FocusedDisplay->ArrangementID);
}

EVENT_CALLBACK(Callback_AXEvent_SpaceChanged)
{
    DEBUG("AXEvent_SpaceChanged");
    ClearBorder(&FocusedBorder);
    ax_display *Display  = AXLibMainDisplay();
    ax_space *OldSpace = Display->Space;

    FocusedDisplay = Display;
    FocusedDisplay->Space = AXLibGetActiveSpace(FocusedDisplay);
    printf("Display: CGDirectDisplayID %d, Arrangement %d\n", FocusedDisplay->ID, FocusedDisplay->ArrangementID);
    printf("Space: CGSSpaceID %d\n", FocusedDisplay->Space->ID);

    AXLibRunningApplications();
    CreateWindowNodeTree(FocusedDisplay);
    RebalanceNodeTree(FocusedDisplay);

    /* NOTE(koekeishiya): This space transition was invoked through deminiaturizing a window.
                          We have no way of passing the actual window in question, to this callback,
                          so we have marked the window through AXWindow_Minimized. We iterate through
                          all visible windows to locate the window we need. */

    /* TODO(koekeishiya): Can we simplify this somehow (?) */
    if(AXLibHasFlags(OldSpace, AXSpace_DeminimizedTransition))
    {
        AXLibClearFlags(OldSpace, AXSpace_DeminimizedTransition);
        std::vector<ax_window *> Windows = AXLibGetAllVisibleWindows();
        for(std::size_t Index = 0; Index < Windows.size(); ++Index)
        {
            ax_window *Window = Windows[Index];
            if(AXLibHasFlags(Window, AXWindow_Minimized))
            {
                AXLibClearFlags(Window, AXWindow_Minimized);
                if((AXLibIsWindowStandard(Window) || AXLibIsWindowCustom(Window)) &&
                   (!AXLibHasFlags(Window, AXWindow_Floating)))
                {
                    AddWindowToNodeTree(Display, Window->ID);
                }
            }
        }
    }

    /* NOTE(koekeishiya): If we trigger a space changed event through cmd+tab, we receive the 'didApplicationActivate'
                          notification before the 'didActiveSpaceChange' notification. If a space has not been visited
                          before, this will cause us to end up on that space with an unsynchronized focused application state.

                          Always update state of focused application and its window after a space transition. */
    FocusedApplication = AXLibGetFocusedApplication();
    FocusedApplication->Focus = AXLibGetFocusedWindow(FocusedApplication);
    UpdateBorder("focused");
}

/* TODO(koekeishiya): Is this interesting (?)
EVENT_CALLBACK(Callback_AXEvent_SpaceCreated)
{
    ax_space *Space = (ax_space *) Event->Context;
}
*/

EVENT_CALLBACK(Callback_AXEvent_ApplicationLaunched)
{
    ax_application *Application = (ax_application *) Event->Context;
    DEBUG("AXEvent_ApplicationLaunched: " << Application->Name);

    /* NOTE(koekeishiya): Some applications fail to properly create notifications upon launch.
                          Force applications to register failed notifications. */
    AXLibAddApplicationObserverNotificationFallback(Application);

    if(Application->Focus)
    {
        ApplyWindowRules(Application->Focus);
        ax_display *Display = AXLibWindowDisplay(Application->Focus);
        Assert(Display != NULL);

        if((!AXLibHasFlags(Application->Focus, AXWindow_Minimized)) &&
           (AXLibIsWindowStandard(Application->Focus) || AXLibIsWindowCustom(Application->Focus)) &&
           (!AXLibHasFlags(Application->Focus, AXWindow_Floating)))
        {
            AddWindowToNodeTree(Display, Application->Focus->ID);
        }
    }
}

EVENT_CALLBACK(Callback_AXEvent_ApplicationTerminated)
{
    DEBUG("AXEvent_ApplicationTerminated");

    /* TODO(koekeishiya): We probably want to flag every display for an update, as the application
                          in question could have had windows on several displays and spaces. */
    ax_display *Display = AXLibMainDisplay();
    Assert(Display != NULL);
    RebalanceNodeTree(Display);
}

EVENT_CALLBACK(Callback_AXEvent_ApplicationActivated)
{
    ax_application *Application = (ax_application *) Event->Context;
    DEBUG("AXEvent_ApplicationActivated: " << Application->Name);

    FocusedApplication = Application;
    UpdateBorder("focused");
}

EVENT_CALLBACK(Callback_AXEvent_WindowCreated)
{
    ax_window *Window = (ax_window *) Event->Context;
    DEBUG("AXEvent_WindowCreated: " << Window->Application->Name << " - " << Window->Name);

    ApplyWindowRules(Window);
    ax_display *Display = AXLibWindowDisplay(Window);
    if(Display)
    {
        if((!AXLibHasFlags(Window, AXWindow_Minimized)) &&
           (AXLibIsWindowStandard(Window) || AXLibIsWindowCustom(Window)) &&
           (!AXLibHasFlags(Window, AXWindow_Floating)))
        {
            AddWindowToNodeTree(Display, Window->ID);
        }
    }
}

EVENT_CALLBACK(Callback_AXEvent_WindowDestroyed)
{
    ax_window *Window = (ax_window *) Event->Context;
    DEBUG("AXEvent_WindowDestroyed: " << Window->Application->Name << " - " << Window->Name);

    ax_display *Display = AXLibWindowDisplay(Window);
    Assert(Display != NULL);
    RemoveWindowFromNodeTree(Display, Window->ID);

    if(FocusedApplication == Window->Application)
        UpdateBorder("focused");

    AXLibDestroyWindow(Window);
}

EVENT_CALLBACK(Callback_AXEvent_WindowMinimized)
{
    ax_window *Window = (ax_window *) Event->Context;
    DEBUG("AXEvent_WindowMinimized: " << Window->Application->Name << " - " << Window->Name);

    ax_display *Display = AXLibWindowDisplay(Window);
    Assert(Display != NULL);
    RemoveWindowFromNodeTree(Display, Window->ID);

    ClearBorder(&FocusedBorder);
}

EVENT_CALLBACK(Callback_AXEvent_WindowDeminimized)
{
    ax_window *Window = (ax_window *) Event->Context;
    DEBUG("AXEvent_WindowDeminimized: " << Window->Application->Name << " - " << Window->Name);<commit_msg>change assert to guard<commit_after>#include "window.h"
#include "container.h"
#include "node.h"
#include "display.h"
#include "space.h"
#include "tree.h"
#include "application.h"
#include "border.h"
#include "helpers.h"
#include "rules.h"
#include "serializer.h"

#include "axlib/axlib.h"

#include <cmath>

#define internal static
#define local_persist static

extern std::map<CFStringRef, space_info> WindowTree;

extern ax_state AXState;
extern ax_display *FocusedDisplay;
extern ax_application *FocusedApplication;

extern kwm_screen KWMScreen;
extern kwm_focus KWMFocus;
extern kwm_toggles KWMToggles;
extern kwm_mode KWMMode;
extern kwm_tiling KWMTiling;
extern kwm_cache KWMCache;
extern kwm_path KWMPath;
extern kwm_thread KWMThread;
extern kwm_border MarkedBorder;
extern kwm_border FocusedBorder;

EVENT_CALLBACK(Callback_AXEvent_DisplayAdded)
{
    DEBUG("AXEvent_DisplayAdded");
}

EVENT_CALLBACK(Callback_AXEvent_DisplayRemoved)
{
    DEBUG("AXEvent_DisplayRemoved");
}

EVENT_CALLBACK(Callback_AXEvent_DisplayResized)
{
    DEBUG("AXEvent_DisplayResized");
}

EVENT_CALLBACK(Callback_AXEvent_DisplayMoved)
{
    DEBUG("AXEvent_DisplayMoved");
}

EVENT_CALLBACK(Callback_AXEvent_DisplayChanged)
{
    FocusedDisplay = AXLibMainDisplay();
    printf("%d: AXEvent_DisplayChanged\n", FocusedDisplay->ArrangementID);
}

EVENT_CALLBACK(Callback_AXEvent_SpaceChanged)
{
    DEBUG("AXEvent_SpaceChanged");
    ClearBorder(&FocusedBorder);
    ax_display *Display  = AXLibMainDisplay();
    ax_space *OldSpace = Display->Space;

    FocusedDisplay = Display;
    FocusedDisplay->Space = AXLibGetActiveSpace(FocusedDisplay);
    printf("Display: CGDirectDisplayID %d, Arrangement %d\n", FocusedDisplay->ID, FocusedDisplay->ArrangementID);
    printf("Space: CGSSpaceID %d\n", FocusedDisplay->Space->ID);

    AXLibRunningApplications();
    CreateWindowNodeTree(FocusedDisplay);
    RebalanceNodeTree(FocusedDisplay);

    /* NOTE(koekeishiya): This space transition was invoked through deminiaturizing a window.
                          We have no way of passing the actual window in question, to this callback,
                          so we have marked the window through AXWindow_Minimized. We iterate through
                          all visible windows to locate the window we need. */

    /* TODO(koekeishiya): Can we simplify this somehow (?) */
    if(AXLibHasFlags(OldSpace, AXSpace_DeminimizedTransition))
    {
        AXLibClearFlags(OldSpace, AXSpace_DeminimizedTransition);
        std::vector<ax_window *> Windows = AXLibGetAllVisibleWindows();
        for(std::size_t Index = 0; Index < Windows.size(); ++Index)
        {
            ax_window *Window = Windows[Index];
            if(AXLibHasFlags(Window, AXWindow_Minimized))
            {
                AXLibClearFlags(Window, AXWindow_Minimized);
                if((AXLibIsWindowStandard(Window) || AXLibIsWindowCustom(Window)) &&
                   (!AXLibHasFlags(Window, AXWindow_Floating)))
                {
                    AddWindowToNodeTree(Display, Window->ID);
                }
            }
        }
    }

    /* NOTE(koekeishiya): If we trigger a space changed event through cmd+tab, we receive the 'didApplicationActivate'
                          notification before the 'didActiveSpaceChange' notification. If a space has not been visited
                          before, this will cause us to end up on that space with an unsynchronized focused application state.

                          Always update state of focused application and its window after a space transition. */
    FocusedApplication = AXLibGetFocusedApplication();
    FocusedApplication->Focus = AXLibGetFocusedWindow(FocusedApplication);
    UpdateBorder("focused");
}

/* TODO(koekeishiya): Is this interesting (?)
EVENT_CALLBACK(Callback_AXEvent_SpaceCreated)
{
    ax_space *Space = (ax_space *) Event->Context;
}
*/

EVENT_CALLBACK(Callback_AXEvent_ApplicationLaunched)
{
    ax_application *Application = (ax_application *) Event->Context;
    DEBUG("AXEvent_ApplicationLaunched: " << Application->Name);

    /* NOTE(koekeishiya): Some applications fail to properly create notifications upon launch.
                          Force applications to register failed notifications. */
    AXLibAddApplicationObserverNotificationFallback(Application);

    if(Application->Focus)
    {
        ApplyWindowRules(Application->Focus);
        ax_display *Display = AXLibWindowDisplay(Application->Focus);
        Assert(Display != NULL);

        if((!AXLibHasFlags(Application->Focus, AXWindow_Minimized)) &&
           (AXLibIsWindowStandard(Application->Focus) || AXLibIsWindowCustom(Application->Focus)) &&
           (!AXLibHasFlags(Application->Focus, AXWindow_Floating)))
        {
            AddWindowToNodeTree(Display, Application->Focus->ID);
        }
    }
}

EVENT_CALLBACK(Callback_AXEvent_ApplicationTerminated)
{
    DEBUG("AXEvent_ApplicationTerminated");

    /* TODO(koekeishiya): We probably want to flag every display for an update, as the application
                          in question could have had windows on several displays and spaces. */
    ax_display *Display = AXLibMainDisplay();
    Assert(Display != NULL);
    RebalanceNodeTree(Display);
}

EVENT_CALLBACK(Callback_AXEvent_ApplicationActivated)
{
    ax_application *Application = (ax_application *) Event->Context;
    DEBUG("AXEvent_ApplicationActivated: " << Application->Name);

    FocusedApplication = Application;
    UpdateBorder("focused");
}

EVENT_CALLBACK(Callback_AXEvent_WindowCreated)
{
    ax_window *Window = (ax_window *) Event->Context;
    DEBUG("AXEvent_WindowCreated: " << Window->Application->Name << " - " << Window->Name);

    ApplyWindowRules(Window);
    ax_display *Display = AXLibWindowDisplay(Window);
    if(Display)
    {
        if((!AXLibHasFlags(Window, AXWindow_Minimized)) &&
           (AXLibIsWindowStandard(Window) || AXLibIsWindowCustom(Window)) &&
           (!AXLibHasFlags(Window, AXWindow_Floating)))
        {
            AddWindowToNodeTree(Display, Window->ID);
        }
    }
}

EVENT_CALLBACK(Callback_AXEvent_WindowDestroyed)
{
    ax_window *Window = (ax_window *) Event->Context;
    DEBUG("AXEvent_WindowDestroyed: " << Window->Application->Name << " - " << Window->Name);

    ax_display *Display = AXLibWindowDisplay(Window);
    if(Display)
    {
        RemoveWindowFromNodeTree(Display, Window->ID);
    }

    if(FocusedApplication == Window->Application)
        UpdateBorder("focused");

    AXLibDestroyWindow(Window);
}

EVENT_CALLBACK(Callback_AXEvent_WindowMinimized)
{
    ax_window *Window = (ax_window *) Event->Context;
    DEBUG("AXEvent_WindowMinimized: " << Window->Application->Name << " - " << Window->Name);

    ax_display *Display = AXLibWindowDisplay(Window);
    Assert(Display != NULL);
    RemoveWindowFromNodeTree(Display, Window->ID);

    ClearBorder(&FocusedBorder);
}

EVENT_CALLBACK(Callback_AXEvent_WindowDeminimized)
{
    ax_window *Window = (ax_window *) Event->Context;
    DEBUG("AXEvent_WindowDeminimized: " << Window->Application->Name << " - " << Window->Name);
{'fraction_non_alphanumeric': 0.10426906333380143, 'fraction_numerical': 0.0001404297149276787, 'mean_word_length': 3.815077755240027, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 95, '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': '3774365', 'n_tokens_mistral': 4208, 'n_tokens_neox': 4049, 'n_words': 817}