Source

Mirror / src / main.cpp

#include <iostream>
#include <functional>
#include <future>

#include <xcb/xcb.h>
#include <xcb/xcb_keysyms.h>
#include <X11/keysym.h>

#include "event.h"
#include "setup.h"
#include "monitor.h"
#include "eventHandlers.h"
#include "keyHandlers.h"
#include "statusBar.h"


int main()
{
    try
    {
        Setup setup;
        EventDispatcher eventDispatch(setup.conn);
        EventHandler handler(&setup);
        KeyHandler keyHandler(&setup);
        KeyCallback commandCallback;
        KeyCallback interactCallback;
        eventDispatch.setHandler("ConfigureRequest", std::bind(&EventHandler::handleConfigureRequest, &handler, std::placeholders::_1));
        eventDispatch.setHandler("MapRequest", std::bind(&EventHandler::handleMapRequest, &handler, std::placeholders::_1));
        eventDispatch.setHandler("DestroyNotify", std::bind(&EventHandler::handleDestroyNotify, &handler, std::placeholders::_1));
        eventDispatch.setHandler("UnmapNotify", std::bind(&EventHandler::handleUnmapNotify, &handler, std::placeholders::_1));
        eventDispatch.setHandler("EnterNotify", std::bind(&EventHandler::handleEnterNotify, &handler, std::placeholders::_1));
        eventDispatch.setHandler("error", std::bind(&EventHandler::handleError, &handler, std::placeholders::_1));
        eventDispatch.setHandler("MappingNotify", std::bind(&KeyHandler::handleMappingNotify, &keyHandler, std::placeholders::_1));
        eventDispatch.setHandler("KeyPress", std::bind(&KeyHandler::handleKeyPress, &keyHandler, std::placeholders::_1));
        eventDispatch.setHandler("KeyRelease", std::bind(&KeyHandler::handleKeyRelease, &keyHandler, std::placeholders::_1));
        eventDispatch.setHandler("Expose", [&](EventPtr event){ 
            for (auto& statusBar : setup.statusBars)
                statusBar.draw();
             });
        
        eventDispatch.addLoopCallback([&]{
            for (auto& statusBar : setup.statusBars)
            {
                statusBar.update();
            }
                });

        commandCallback = [&]{
            for (auto& statusBar: setup.statusBars)
                statusBar.setText("Command"); 
            
            keyHandler.ungrabKey(0, XK_Super_L, Press);
            keyHandler.grabKey(0, XK_i, Press, interactCallback);
            keyHandler.grabKey(0, XK_x, Press, [&]{
                Monitor& monitor = setup.getCurrentMonitor();
                std::cerr << "currentTag = " << monitor.currentTag << std::endl;
                monitor.tagList.at(monitor.currentTag).windowContainer.killFocused();
            });
            keyHandler.grabKey(0, XK_Right, Press, [&] {setup.nextTag();});
            keyHandler.grabKey(0, XK_Left, Press, [&]{setup.prevTag();});
            keyHandler.grabKey(0, XK_q, Press, [&]{eventDispatch.stop();});
            keyHandler.grabKey(0, XK_o, Press, [&]{
                interactCallback();
                setup.launchApp("dmenu-launch");});
            xcb_grab_keyboard(setup.conn, true, setup.root, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
            //xcb_grab_pointer()
        };

        interactCallback = [&]{
            for (auto& statusBar : setup.statusBars)
                statusBar.setText("Interact");
            keyHandler.ungrabKey(0, XK_i, Press);
            keyHandler.ungrabKey(0, XK_q, Press);
            keyHandler.ungrabKey(0, XK_x, Press);
            keyHandler.ungrabKey(0, XK_o, Press);
            keyHandler.ungrabKey(0, XK_Left, Press);
            keyHandler.ungrabKey(0, XK_Right, Press);
            keyHandler.grabKey(0, XK_Super_L, Press, commandCallback);
            xcb_ungrab_keyboard(setup.conn, XCB_CURRENT_TIME);
        };

        commandCallback();
        std::cout << "Done setup" << std::endl;
        eventDispatch.eventLoop();

    }
    catch (OtherWindowManager &ex)
    {
        std::cout << "There is another window manager running; I can't do anything. Sorry!" << std::endl;
        return 0;
    }
   return 0;

}