<commit_before>#include "ffmouse.h"

static const std::string OSASCRIPT_START = "osascript -e 'tell application \"System Events\" to tell process \"";
static const std::string OSASCRIPT_MID = "\" to perform action \"AXRaise\" of (first window whose name contains \"";
static const std::string OSASCRIPT_END = "\")'";

static pid_t pid;
static ProcessSerialNumber psn;
static std::vector<app_info> window_lst;
static int window_lst_focus_index;
static std::string window_lst_focus_name = "<no focus>";;

void detect_window_below_cursor()
{
    window_lst.clear();

    CFArrayRef osx_window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
    if(osx_window_list)
    {
        CFIndex osx_window_count = CFArrayGetCount(osx_window_list);
        for(CFIndex i = 0; i < osx_window_count; ++i)
        {
            CFDictionaryRef elem = (CFDictionaryRef)CFArrayGetValueAtIndex(osx_window_list, i);
            window_lst.push_back(app_info());
            CFDictionaryApplyFunction(elem, get_window_info, NULL);
        }

        CGEventRef event = CGEventCreate(NULL);
        CGPoint cursor = CGEventGetLocation(event);
        CFRelease(event);
        
        std::cout << "Mouse Pos: " << cursor.x << ", " << cursor.y << std::endl;
        for(int i = 0; i < window_lst.size(); ++i)
        {
            if(window_lst[i].layer == 0)
            {
                if(cursor.x >= window_lst[i].x && cursor.x <= window_lst[i].x + window_lst[i].width
                        && cursor.y >= window_lst[i].y && cursor.y <= window_lst[i].y + window_lst[i].height)
                {
                    if(window_lst_focus_name != window_lst[i].name || pid != window_lst[i].pid)
                    {
                        window_lst_focus_name = window_lst[i].name;
                        window_lst_focus_index = i;

                        // Strip single quotes and remaining part if found,
                        // because  it breaks applescript syntax
                        std::string win_title = window_lst[window_lst_focus_index].name;
                        std::size_t pos = win_title.find("'");
                        if(pos != std::string::npos)
                            win_title.erase(pos);

                        std::string applescript_cmd = OSASCRIPT_START +
                            window_lst[window_lst_focus_index].owner + OSASCRIPT_MID +
                            win_title + OSASCRIPT_END;
                        system(applescript_cmd.c_str());

                        if(pid != window_lst[i].pid)
                        {
                            pid = window_lst[i].pid;
                            GetProcessForPID(pid, &psn);
                        }
                        SetFrontProcessWithOptions(&psn, kSetFrontProcessFrontWindowOnly);
                    }
                    break;
                }

                /*
                std::cout << "Owner: " << window_lst[i].owner << std::endl;
                std::cout << "Name: " << window_lst[i].name << std::endl;
                std::cout << "PID: " << window_lst[i].pid << std::endl;
                std::cout << "Layer: " << window_lst[i].layer << std::endl;
                std::cout << "X: " << window_lst[i].x << std::endl;
                std::cout << "Y: " << window_lst[i].y << std::endl;
                std::cout << "Width: " << window_lst[i].width << std::endl;
                std::cout << "Height: " << window_lst[i].height << std::endl;
                */
            }
        }
        std::cout << "Keyboard focus: " << pid << std::endl;
    }
}

CGEventRef cgevent_callback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
    if(type != kCGEventKeyDown && type != kCGEventKeyUp && type != kCGEventMouseMoved)<commit_msg>remove autoraise<commit_after>#include "ffmouse.h"

static const std::string OSASCRIPT_START = "osascript -e 'tell application \"System Events\" to tell process \"";
static const std::string OSASCRIPT_MID = "\" to perform action \"AXRaise\" of (first window whose name contains \"";
static const std::string OSASCRIPT_END = "\")'";

static pid_t pid;
static ProcessSerialNumber psn;
static std::vector<app_info> window_lst;
static int window_lst_focus_index;
static std::string window_lst_focus_name = "<no focus>";;

void detect_window_below_cursor()
{
    window_lst.clear();

    CFArrayRef osx_window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
    if(osx_window_list)
    {
        CFIndex osx_window_count = CFArrayGetCount(osx_window_list);
        for(CFIndex i = 0; i < osx_window_count; ++i)
        {
            CFDictionaryRef elem = (CFDictionaryRef)CFArrayGetValueAtIndex(osx_window_list, i);
            window_lst.push_back(app_info());
            CFDictionaryApplyFunction(elem, get_window_info, NULL);
        }

        CGEventRef event = CGEventCreate(NULL);
        CGPoint cursor = CGEventGetLocation(event);
        CFRelease(event);
        
        std::cout << "Mouse Pos: " << cursor.x << ", " << cursor.y << std::endl;
        for(int i = 0; i < window_lst.size(); ++i)
        {
            if(window_lst[i].layer == 0)
            {
                if(cursor.x >= window_lst[i].x && cursor.x <= window_lst[i].x + window_lst[i].width
                        && cursor.y >= window_lst[i].y && cursor.y <= window_lst[i].y + window_lst[i].height)
                {
                    if(window_lst_focus_name != window_lst[i].name || pid != window_lst[i].pid)
                    {
                        window_lst_focus_name = window_lst[i].name;
                        window_lst_focus_index = i;

                        // Strip single quotes and remaining part if found,
                        // because  it breaks applescript syntax
                        std::string win_title = window_lst[window_lst_focus_index].name;
                        std::size_t pos = win_title.find("'");
                        if(pos != std::string::npos)
                            win_title.erase(pos);

                        std::string applescript_cmd = OSASCRIPT_START +
                            window_lst[window_lst_focus_index].owner + OSASCRIPT_MID +
                            win_title + OSASCRIPT_END;
                        system(applescript_cmd.c_str());

                        if(pid != window_lst[i].pid)
                        {
                            pid = window_lst[i].pid;
                            GetProcessForPID(pid, &psn);
                        }
                    }
                    break;
                }

                /*
                std::cout << "Owner: " << window_lst[i].owner << std::endl;
                std::cout << "Name: " << window_lst[i].name << std::endl;
                std::cout << "PID: " << window_lst[i].pid << std::endl;
                std::cout << "Layer: " << window_lst[i].layer << std::endl;
                std::cout << "X: " << window_lst[i].x << std::endl;
                std::cout << "Y: " << window_lst[i].y << std::endl;
                std::cout << "Width: " << window_lst[i].width << std::endl;
                std::cout << "Height: " << window_lst[i].height << std::endl;
                */
            }
        }
        std::cout << "Keyboard focus: " << pid << std::endl;
    }
}

CGEventRef cgevent_callback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
    if(type != kCGEventKeyDown && type != kCGEventKeyUp && type != kCGEventMouseMoved)
{'fraction_non_alphanumeric': 0.12591719077568134, 'fraction_numerical': 0.0007861635220125787, 'mean_word_length': 1.8652402402402402, 'pattern_counts': {'":': 0, '<': 143, '<?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': '5764503', 'n_tokens_mistral': 2308, 'n_tokens_neox': 2169, 'n_words': 410}