diff --git a/TODO b/TODO new file mode 100644 index 0000000..7f64f49 --- /dev/null +++ b/TODO @@ -0,0 +1 @@ +TODO: error handling in WindowManager.cpp \ No newline at end of file diff --git a/includes/windowManager/WindowManager.hpp b/includes/windowManager/WindowManager.hpp index 0e6a93b..ab79756 100644 --- a/includes/windowManager/WindowManager.hpp +++ b/includes/windowManager/WindowManager.hpp @@ -12,14 +12,21 @@ public: void loop(); private: - Display *_display; - int _screen; - Window root; - Window win; - XEvent event; - int _width; - int _height; - + int WindowX; + int WindowY; + int WindowWidth; + int WindowHeight; + int BorderWidth; + int WindowDepth; + int WindowClass; + Visual *WindowVisual; + int AttributeValueMask; + XSetWindowAttributes WindowAttributes; + Window MainWindow; + Display *MainDisplay; + Window RootWindow; + Atom wmDelete; + bool isWindowOpen; }; #endif //NOISE_GENERATOR_WINDOWMANAGER_HPP diff --git a/srcs/main.cpp b/srcs/main.cpp index b541fb4..f2c4bcd 100644 --- a/srcs/main.cpp +++ b/srcs/main.cpp @@ -6,7 +6,6 @@ int main() { WindowManager window(WIDTH, HEIGHT); - window.loop(); return 0; diff --git a/srcs/windowManager/WindowManager.cpp b/srcs/windowManager/WindowManager.cpp index 3f4287d..44bb210 100644 --- a/srcs/windowManager/WindowManager.cpp +++ b/srcs/windowManager/WindowManager.cpp @@ -1,23 +1,60 @@ #include "windowManager/WindowManager.hpp" +#include +#include +#include +#include -WindowManager::WindowManager(int width, int height) : _width(width), _height(height) { - _display = XOpenDisplay(NULL); - if (NULL == _display) errx(2, "unable to open display"); - _screen = DefaultScreen(_display); - root = RootWindow(_display, _screen); - win = XCreateSimpleWindow(_display, root, 0, 0, _width, _height, 10, - 0, 0x0f0f0f); +WindowManager::WindowManager(int width, int height) : + WindowX(0), WindowY(0), + WindowWidth(width), WindowHeight(height), + BorderWidth(0), + WindowDepth(CopyFromParent), + WindowClass(CopyFromParent), + WindowVisual(CopyFromParent), + AttributeValueMask(CWBackPixel | CWEventMask) +{ + MainDisplay = XOpenDisplay(0); + RootWindow = XDefaultRootWindow(MainDisplay); + bzero(&WindowAttributes, sizeof(XSetWindowAttributes)); + WindowAttributes.background_pixel = 0xffafe9af; + WindowAttributes.event_mask = StructureNotifyMask | KeyPressMask | KeyReleaseMask | ExposureMask; - XMapWindow(_display, win); - XFlush(_display); + MainWindow = XCreateWindow(MainDisplay, RootWindow, + WindowX, WindowY, WindowWidth, WindowHeight, + BorderWidth, WindowDepth, WindowClass, WindowVisual, + AttributeValueMask, &WindowAttributes); + XMapWindow(MainDisplay, MainWindow); + wmDelete = XInternAtom(MainDisplay, "WM_DELETE_WINDOW", false); + XSetWMProtocols(MainDisplay, MainWindow, &wmDelete, 1); + + isWindowOpen = true; } WindowManager::~WindowManager() { - XUnmapWindow(_display, win); - XDestroyWindow(_display, win); - XCloseDisplay(_display); + XUnmapWindow(this->MainDisplay, this->MainWindow); + XDestroyWindow(this->MainDisplay, this->MainWindow); + XCloseDisplay(this->MainDisplay); } void WindowManager::loop() { - while (XNextEvent(_display, &event) != 0); + while (isWindowOpen) { + XEvent GeneralEvent = {}; + XNextEvent(this->MainDisplay, &GeneralEvent); + + switch(GeneralEvent.type) { + case KeyPress: + case KeyRelease: + { + XKeyPressedEvent *event = (XKeyPressedEvent *)&GeneralEvent; + if (event->keycode == XKeysymToKeycode(this->MainDisplay, XK_Escape)) { + this->isWindowOpen = false; + } + } break; + case ClientMessage: { + if (static_cast(GeneralEvent.xclient.data.l[0]) == this->wmDelete) { + this->isWindowOpen = false; + } + } break; + } + } }