<commit_before>#include "window.h"
#include "container.h"
#include "node.h"
#include "display.h"
#include "space.h"
#include "tree.h"
#include "border.h"
#include "helpers.h"
#include "rules.h"
#include "serializer.h"
#include "cursor.h"
#include "scratchpad.h"
#include "axlib/axlib.h"

#include <cmath>

#define internal static
#define local_persist static

extern std::map<const char *, space_info> WindowTree;
extern ax_state AXState;
extern ax_display *FocusedDisplay;
extern ax_application *FocusedApplication;
extern ax_window *MarkedWindow;

extern kwm_settings KWMSettings;
extern kwm_path KWMPath;
extern kwm_thread KWMThread;
extern kwm_border MarkedBorder;
extern kwm_border FocusedBorder;

internal void
DrawFocusedBorder(ax_display *Display)
{
    if((Display) &&
       (Display->Space->Type == kCGSSpaceUser))
    {
        UpdateBorder("focused");
    }
}

internal void
FloatNonResizable(ax_window *Window)
{
    if(Window)
    {
        if((KWMSettings.FloatNonResizable) &&
           (!AXLibHasFlags(Window, AXWindow_Resizable)))
        {
            AXLibAddFlags(Window, AXWindow_Floating);
        }
    }
}

internal void
StandbyOnFloat(ax_window *Window)
{
    if(Window)
    {
        if((KWMSettings.StandbyOnFloat) &&
           (KWMSettings.Focus != FocusModeDisabled))
        {
            if(AXLibHasFlags(Window, AXWindow_Floating))
                KWMSettings.Focus = FocusModeStandby;
            else
                KWMSettings.Focus = FocusModeAutoraise;
        }
    }
}

internal void
TileWindow(ax_display *Display, ax_window *Window)
{
    if(Window)
    {
        if((!AXLibHasFlags(Window, AXWindow_Minimized)) &&
           (AXLibIsWindowStandard(Window) || AXLibIsWindowCustom(Window)) &&
           (!AXLibHasFlags(Window, AXWindow_Floating)))
        {
            AddWindowToNodeTree(Display, Window->ID);
        }
    }
}

/* TODO(koekeishiya): Event context is a pointer to the new display. */
EVENT_CALLBACK(Callback_AXEvent_DisplayAdded)
{
    DEBUG("AXEvent_DisplayAdded");
}

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

/* NOTE(koekeishiya): Event context is a pointer to the resized display. */
EVENT_CALLBACK(Callback_AXEvent_DisplayResized)
{
    ax_display *Display = (ax_display *) Event->Context;
    DEBUG("AXEvent_DisplayResized");

    std::map<CGSSpaceID, ax_space>::iterator It;
    for(It = Display->Spaces.begin(); It != Display->Spaces.end(); ++It)
    {
        ax_space *Space = &It->second;
        space_info *SpaceInfo = &WindowTree[Space->Identifier];
        if(Space == Display->Space)
            UpdateSpaceOfDisplay(Display, SpaceInfo);
        else
            SpaceInfo->NeedsUpdate = true;
    }
}

/* NOTE(koekeishiya): Event context is a pointer to the moved display. */
EVENT_CALLBACK(Callback_AXEvent_DisplayMoved)
{
    ax_display *Display = (ax_display *) Event->Context;
    DEBUG("AXEvent_DisplayMoved");

    std::map<CGSSpaceID, ax_space>::iterator It;
    for(It = Display->Spaces.begin(); It != Display->Spaces.end(); ++It)
    {
        ax_space *Space = &It->second;
        space_info *SpaceInfo = &WindowTree[Space->Identifier];
        if(Space == Display->Space)
            UpdateSpaceOfDisplay(Display, SpaceInfo);
        else
            SpaceInfo->NeedsUpdate = true;
    }
}

/* NOTE(koekeishiya): Event context is NULL. */
EVENT_CALLBACK(Callback_AXEvent_DisplayChanged)
{
    FocusedDisplay = AXLibMainDisplay();
    printf("%d: AXEvent_DisplayChanged\n", FocusedDisplay->ArrangementID);

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

internal void
AddMinimizedWindowsToTree(ax_display *Display)
{
    std::vector<ax_window *> Windows = AXLibGetAllVisibleWindows();<commit_msg>duplicate code<commit_after>#include "window.h"
#include "container.h"
#include "node.h"
#include "display.h"
#include "space.h"
#include "tree.h"
#include "border.h"
#include "helpers.h"
#include "rules.h"
#include "serializer.h"
#include "cursor.h"
#include "scratchpad.h"
#include "axlib/axlib.h"

#include <cmath>

#define internal static
#define local_persist static

extern std::map<const char *, space_info> WindowTree;
extern ax_state AXState;
extern ax_display *FocusedDisplay;
extern ax_application *FocusedApplication;
extern ax_window *MarkedWindow;

extern kwm_settings KWMSettings;
extern kwm_path KWMPath;
extern kwm_thread KWMThread;
extern kwm_border MarkedBorder;
extern kwm_border FocusedBorder;

internal void
DrawFocusedBorder(ax_display *Display)
{
    if((Display) &&
       (Display->Space->Type == kCGSSpaceUser))
    {
        UpdateBorder("focused");
    }
}

internal void
FloatNonResizable(ax_window *Window)
{
    if(Window)
    {
        if((KWMSettings.FloatNonResizable) &&
           (!AXLibHasFlags(Window, AXWindow_Resizable)))
        {
            AXLibAddFlags(Window, AXWindow_Floating);
        }
    }
}

internal void
StandbyOnFloat(ax_window *Window)
{
    if(Window)
    {
        if((KWMSettings.StandbyOnFloat) &&
           (KWMSettings.Focus != FocusModeDisabled))
        {
            if(AXLibHasFlags(Window, AXWindow_Floating))
                KWMSettings.Focus = FocusModeStandby;
            else
                KWMSettings.Focus = FocusModeAutoraise;
        }
    }
}

internal void
TileWindow(ax_display *Display, ax_window *Window)
{
    if(Window)
    {
        if((!AXLibHasFlags(Window, AXWindow_Minimized)) &&
           (AXLibIsWindowStandard(Window) || AXLibIsWindowCustom(Window)) &&
           (!AXLibHasFlags(Window, AXWindow_Floating)))
        {
            AddWindowToNodeTree(Display, Window->ID);
        }
    }
}

/* TODO(koekeishiya): Event context is a pointer to the new display. */
EVENT_CALLBACK(Callback_AXEvent_DisplayAdded)
{
    DEBUG("AXEvent_DisplayAdded");
}

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

internal inline void
ResizeDisplay(ax_display *Display)
{
    std::map<CGSSpaceID, ax_space>::iterator It;
    for(It = Display->Spaces.begin(); It != Display->Spaces.end(); ++It)
    {
        ax_space *Space = &It->second;
        space_info *SpaceInfo = &WindowTree[Space->Identifier];
        if(Space == Display->Space)
            UpdateSpaceOfDisplay(Display, SpaceInfo);
        else
            SpaceInfo->NeedsUpdate = true;
    }
}

/* NOTE(koekeishiya): Event context is a pointer to the resized display. */
EVENT_CALLBACK(Callback_AXEvent_DisplayResized)
{
    ax_display *Display = (ax_display *) Event->Context;
    DEBUG("AXEvent_DisplayResized");
    ResizeDisplay(Display);
}

/* NOTE(koekeishiya): Event context is a pointer to the moved display. */
EVENT_CALLBACK(Callback_AXEvent_DisplayMoved)
{
    ax_display *Display = (ax_display *) Event->Context;
    DEBUG("AXEvent_DisplayMoved");
    ResizeDisplay(Display);
}

/* NOTE(koekeishiya): Event context is NULL. */
EVENT_CALLBACK(Callback_AXEvent_DisplayChanged)
{
    FocusedDisplay = AXLibMainDisplay();
    printf("%d: AXEvent_DisplayChanged\n", FocusedDisplay->ArrangementID);

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

internal void
AddMinimizedWindowsToTree(ax_display *Display)
{
    std::vector<ax_window *> Windows = AXLibGetAllVisibleWindows();
{'fraction_non_alphanumeric': 0.1168141592920354, 'fraction_numerical': 0.0, 'mean_word_length': 3.748545572074984, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 42, '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': '3045575', 'n_tokens_mistral': 2385, 'n_tokens_neox': 2341, 'n_words': 432}