<commit_before>/** @file */
#include "events.h"

/**
 * Sets up some member variables, and binds the mouse and keyboard shortcuts.
 * @param shared The shared window manager data
 * @param clients The collection of clients
 * @param keyboard The list of keyboard shortcuts
 * @param randr_offset The X event offset for RandR events
*/
XEvents::XEvents(WMShared &shared, ClientManager &clients, KeyboardConfig &keyboard,
        int randr_offset) :
    m_shared(shared), m_clients(clients), m_randroffset(randr_offset), m_keyboard(keyboard)
{
    int buttons[] = {MOVE_BUTTON, RESIZE_BUTTON, LAUNCH_BUTTON , -1};

    for (int *button = &buttons[0]; *button != -1; button++)
    {
        XGrabButton(m_shared.display, *button, BUTTON_MASK, m_shared.root, true, 
                ButtonPressMask,GrabModeAsync, GrabModeAsync, None, None);
    }

    KeySym keys[] = {m_keyboard.client_next_desktop, m_keyboard.client_prev_desktop,
        m_keyboard.next_desktop, m_keyboard.prev_desktop, m_keyboard.toggle_stick,
        m_keyboard.iconify, m_keyboard.maximize, m_keyboard.request_close, 
        m_keyboard.force_close, m_keyboard.snap_top, m_keyboard.snap_bottom,
        m_keyboard.snap_left, m_keyboard.snap_right, m_keyboard.layer_above,
        m_keyboard.layer_below, m_keyboard.layer_top, m_keyboard.layer_bottom,
        m_keyboard.exit_wm, m_keyboard.layer1, m_keyboard.layer2, m_keyboard.layer3,
        m_keyboard.layer4, m_keyboard.layer5, m_keyboard.layer6, m_keyboard.layer7,
        m_keyboard.layer8, m_keyboard.layer9, NoSymbol};

    for (KeySym *key = &keys[0]; *key != NoSymbol; key++)
    {
        int keycode = XKeysymToKeycode(m_shared.display, *key);
        XGrabKey(m_shared.display, keycode, BUTTON_MASK, m_shared.root, true, 
                GrabModeAsync, GrabModeAsync);
    }
};

/**
 * Processes all events until the user terminates SmallWM.
 */
void XEvents::run()
{
    m_done = false;
    while (!m_done)
    {
        XNextEvent(m_shared.display, &m_event);

        if (m_event.type == m_randroffset + RRNotify)
            handle_rrnotify();

        if (m_event.type == KeyPress)
            handle_keypress();

        if (m_event.type == ButtonPress)
            handle_buttonpress();

        if (m_event.type == ButtonRelease)
            handle_buttonrelease();

        if (m_event.type == MotionNotify)
            handle_motionnotify();

        if (m_event.type == MapNotify)
            handle_mapnotify();

        if (m_event.type == Expose)
            handle_expose();

        if (m_event.type == ConfigureNotify)
            handle_configurenotify();

        if (m_event.type == DestroyNotify)
            handle_destroynotify();
    }
}

/**
 * Handles keyboard presses, which trigger keyboard shortcuts.
 */
void XEvents::handle_keypress()
{
    int nkeys;
    KeySym *keysym = NULL;
    keysym = XGetKeyboardMapping(m_shared.display, m_event.xkey.keycode, 1, &nkeys);

    Window client = m_event.xkey.subwindow;
    if (*keysym == m_keyboard.client_next_desktop)
        m_clients.to_next_desktop(client);

    if (*keysym == m_keyboard.client_prev_desktop)
        m_clients.to_prev_desktop(client);

    if (*keysym == m_keyboard.next_desktop)
        m_clients.next_desktop();

    if (*keysym == m_keyboard.prev_desktop)
        m_clients.prev_desktop();

    if (*keysym == m_keyboard.toggle_stick)
        m_clients.flip_sticky_flag(client);

    if (*keysym == m_keyboard.iconify)
        m_clients.state_transition(client, CS_ICON);

    if (*keysym == m_keyboard.maximize)
        m_clients.maximize(client);

    if (*keysym == m_keyboard.request_close)
        m_clients.close(client);

    // Note that m_clients.destroy() is not usable here, since it must be called
    // _after_ the window has been removed
    if (*keysym == m_keyboard.force_close)
        XDestroyWindow(m_shared.display, client);

    if (*keysym == m_keyboard.snap_top)
        m_clients.snap(client, SNAP_TOP);

    if (*keysym == m_keyboard.snap_bottom)
        m_clients.snap(client, SNAP_BOTTOM);

    if (*keysym == m_keyboard.snap_left)
        m_clients.snap(client, SNAP_LEFT);

    if (*keysym == m_keyboard.snap_right)
        m_clients.snap(client, SNAP_RIGHT);

    if (*keysym == m_keyboard.layer_above)
        m_clients.raise_layer(client);

    if (*keysym == m_keyboard.layer_below)
        m_clients.lower_layer(client);

    if (*keysym == m_keyboard.layer_top)
        m_clients.set_layer(client, MAX_LAYER);

    if (*keysym == m_keyboard.layer_bottom)
        m_clients.set_layer(client, MIN_LAYER);

    if (*keysym == m_keyboard.exit_wm)
        m_done = true;

    if (*keysym == m_keyboard.layer1)<commit_msg>Added a 'fallback' for event handlers.<commit_after>/** @file */
#include "events.h"

/**
 * Sets up some member variables, and binds the mouse and keyboard shortcuts.
 * @param shared The shared window manager data
 * @param clients The collection of clients
 * @param keyboard The list of keyboard shortcuts
 * @param randr_offset The X event offset for RandR events
*/
XEvents::XEvents(WMShared &shared, ClientManager &clients, KeyboardConfig &keyboard,
        int randr_offset) :
    m_shared(shared), m_clients(clients), m_randroffset(randr_offset), m_keyboard(keyboard)
{
    int buttons[] = {MOVE_BUTTON, RESIZE_BUTTON, LAUNCH_BUTTON , -1};

    for (int *button = &buttons[0]; *button != -1; button++)
    {
        XGrabButton(m_shared.display, *button, BUTTON_MASK, m_shared.root, true, 
                ButtonPressMask,GrabModeAsync, GrabModeAsync, None, None);
    }

    KeySym keys[] = {m_keyboard.client_next_desktop, m_keyboard.client_prev_desktop,
        m_keyboard.next_desktop, m_keyboard.prev_desktop, m_keyboard.toggle_stick,
        m_keyboard.iconify, m_keyboard.maximize, m_keyboard.request_close, 
        m_keyboard.force_close, m_keyboard.snap_top, m_keyboard.snap_bottom,
        m_keyboard.snap_left, m_keyboard.snap_right, m_keyboard.layer_above,
        m_keyboard.layer_below, m_keyboard.layer_top, m_keyboard.layer_bottom,
        m_keyboard.exit_wm, m_keyboard.layer1, m_keyboard.layer2, m_keyboard.layer3,
        m_keyboard.layer4, m_keyboard.layer5, m_keyboard.layer6, m_keyboard.layer7,
        m_keyboard.layer8, m_keyboard.layer9, NoSymbol};

    for (KeySym *key = &keys[0]; *key != NoSymbol; key++)
    {
        int keycode = XKeysymToKeycode(m_shared.display, *key);
        XGrabKey(m_shared.display, keycode, BUTTON_MASK, m_shared.root, true, 
                GrabModeAsync, GrabModeAsync);
    }
};

/**
 * Processes all events until the user terminates SmallWM.
 */
void XEvents::run()
{
    m_done = false;
    while (!m_done)
    {
        XNextEvent(m_shared.display, &m_event);

        if (m_event.type == m_randroffset + RRNotify)
            handle_rrnotify();

        if (m_event.type == KeyPress)
            handle_keypress();

        if (m_event.type == ButtonPress)
            handle_buttonpress();

        if (m_event.type == ButtonRelease)
            handle_buttonrelease();

        if (m_event.type == MotionNotify)
            handle_motionnotify();

        if (m_event.type == MapNotify)
            handle_mapnotify();

        if (m_event.type == Expose)
            handle_expose();

        if (m_event.type == ConfigureNotify)
            handle_configurenotify();

        if (m_event.type == DestroyNotify)
            handle_destroynotify();
    }
}

/**
 * Handles keyboard presses, which trigger keyboard shortcuts.
 */
void XEvents::handle_keypress()
{
    int nkeys;
    KeySym *keysym = NULL;
    keysym = XGetKeyboardMapping(m_shared.display, m_event.xkey.keycode, 1, &nkeys);

    Window client = m_event.xkey.subwindow;
    if (client == None)
        client = m_event.xkey.window;

    if (*keysym == m_keyboard.client_next_desktop)
        m_clients.to_next_desktop(client);

    if (*keysym == m_keyboard.client_prev_desktop)
        m_clients.to_prev_desktop(client);

    if (*keysym == m_keyboard.next_desktop)
        m_clients.next_desktop();

    if (*keysym == m_keyboard.prev_desktop)
        m_clients.prev_desktop();

    if (*keysym == m_keyboard.toggle_stick)
        m_clients.flip_sticky_flag(client);

    if (*keysym == m_keyboard.iconify)
        m_clients.state_transition(client, CS_ICON);

    if (*keysym == m_keyboard.maximize)
        m_clients.maximize(client);

    if (*keysym == m_keyboard.request_close)
        m_clients.close(client);

    // Note that m_clients.destroy() is not usable here, since it must be called
    // _after_ the window has been removed
    if (*keysym == m_keyboard.force_close)
    {
        // Make sure that the window isn't an icon before we nuke it
        if (m_clients.get_icon_of_icon(client))
            return;

        XDestroyWindow(m_shared.display, client);
    }

    if (*keysym == m_keyboard.snap_top)
        m_clients.snap(client, SNAP_TOP);

    if (*keysym == m_keyboard.snap_bottom)
        m_clients.snap(client, SNAP_BOTTOM);

    if (*keysym == m_keyboard.snap_left)
        m_clients.snap(client, SNAP_LEFT);

    if (*keysym == m_keyboard.snap_right)
        m_clients.snap(client, SNAP_RIGHT);

    if (*keysym == m_keyboard.layer_above)
        m_clients.raise_layer(client);

    if (*keysym == m_keyboard.layer_below)
        m_clients.lower_layer(client);

    if (*keysym == m_keyboard.layer_top)
        m_clients.set_layer(client, MAX_LAYER);

    if (*keysym == m_keyboard.layer_bottom)
        m_clients.set_layer(client, MIN_LAYER);

    if (*keysym == m_keyboard.exit_wm)
        m_done = true;

    if (*keysym == m_keyboard.layer1)
{'fraction_non_alphanumeric': 0.11081024786502812, 'fraction_numerical': 0.003124349093938763, 'mean_word_length': 3.081172970675733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2431824', 'n_tokens_mistral': 3344, 'n_tokens_neox': 3226, 'n_words': 632}