Source

Mirror / src / eventHandlers.cpp

Full commit
#include "eventHandlers.h"
#include "monitor.h"
#include "statusBar.h"

EventHandler::EventHandler(Setup *setup):
    setup(setup),
    boxUid(0),
    moveWindow(nullptr)
{
}

EventHandler::~EventHandler()
{
}

void EventHandler::handleMapRequest(EventPtr event)
{
    std::cout<< "MapRequest" << std::endl;
    auto ev = convertEvent<xcb_map_request_event_t>(event);
    setup->addWindow(ev->window);
    setup->layoutWindows();
    xcb_map_window(setup->conn, ev->window);
}

void EventHandler::handleConfigureRequest(EventPtr event)
{
    auto ev = convertEvent<xcb_configure_request_event_t>(event);
    Window &win = setup->findWindow(ev->window);
    if (win)
    {
        auto geometry = win.getGeometry();
        if(ev->value_mask & XCB_CONFIG_WINDOW_X)
            geometry.x = ev->x;
        if(ev->value_mask & XCB_CONFIG_WINDOW_Y)
            geometry.y = ev->y;
        if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
            geometry.width = ev->width;
        if(ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
            geometry.height = ev->height;

        win.setGeometry(geometry);
        setup->layoutWindows();
        return;
    }
    uint16_t mask=0;
    uint32_t values [7];
    unsigned short i = 0;
    if (ev->value_mask & XCB_CONFIG_WINDOW_X)
    {
        mask |= XCB_CONFIG_WINDOW_X;
        values[i++] = ev->x;
                                        
    }

    if (ev->value_mask & XCB_CONFIG_WINDOW_Y)
    {
        mask |= XCB_CONFIG_WINDOW_Y;
        values[i++] = ev->y;
                                                
    }

    if (ev->value_mask & XCB_CONFIG_WINDOW_WIDTH)
    {
        mask |= XCB_CONFIG_WINDOW_WIDTH;
        values[i++] = ev->width;
                                                        
    }

    if (ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
    {
        mask |= XCB_CONFIG_WINDOW_HEIGHT;
        values[i++] = ev->height;
                                                                
    }

    if (ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
    {
        mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
        values[i++] = ev->border_width;
                                                                        
    }

    if (ev->value_mask & XCB_CONFIG_WINDOW_SIBLING)
    {
        mask |= XCB_CONFIG_WINDOW_SIBLING;
        values[i++] = ev->sibling;
                                                                                
    }

    if (ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
    {
        mask |= XCB_CONFIG_WINDOW_STACK_MODE;
        values[i++] = ev->stack_mode;
                                                                                        
    }

    xcb_configure_window(setup->conn, ev->window, mask, values);
    xcb_flush(setup->conn);
                                                                        
}

void EventHandler::handleUnmapNotify(EventPtr event)
{
    std::cout << "UnmapNotify" << std::endl;
    auto ev = convertEvent<xcb_unmap_notify_event_t>(event);
    setup->removeWindow(ev->window);

    setup->layoutWindows();
}

void EventHandler::handleDestroyNotify(EventPtr event)
{
    std::cout << "DestroyNotify" << std::endl;
    auto ev = convertEvent<xcb_destroy_notify_event_t>(event);
    setup->removeWindow(ev->window);
    setup->layoutWindows();
}

void EventHandler::handleEnterNotify(EventPtr event)
{
    auto ev = convertEvent<xcb_enter_notify_event_t>(event);
    std::cerr << "got EnterNotify" << std::endl;
    setup->getCurrentMonitor().focusWindow(ev->event);
}

void EventHandler::handleButtonPress(EventPtr event)
{
    auto ev = convertEvent<xcb_button_press_event_t>(event);
    Window& win = setup->findWindow(ev->event);
    if (win)
    {
        setup->removeWindow(win);
        moveWindow = &win;
    }

}

void EventHandler::handleButtonRelease(EventPtr event)
{
    if (moveWindow)
    {
        setup->addWindow(*moveWindow);
        moveWindow = nullptr;
    }    
}

void EventHandler::handleError(EventPtr event)
{
    auto ev = convertEvent<xcb_generic_error_t>(event);
    std::string label = xcb_event_get_error_label(ev->error_code);
    std::cout << "Got an error: " << label << std::endl;
    if (label == "BadWindow")
    {
        auto err = convertEvent<xcb_value_error_t>(ev);
        std::cout << "This was the bad window: " << err->bad_value << std::endl;
    }
}