edit: window manager and add keyhook

This commit is contained in:
loic 2024-12-13 02:53:00 +01:00
parent 46deb59efd
commit 18c4896be4
4 changed files with 66 additions and 22 deletions

1
TODO Normal file
View file

@ -0,0 +1 @@
TODO: error handling in WindowManager.cpp

View file

@ -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

View file

@ -6,7 +6,6 @@
int main() {
WindowManager window(WIDTH, HEIGHT);
window.loop();
return 0;

View file

@ -1,23 +1,60 @@
#include "windowManager/WindowManager.hpp"
#include <string.h>
#include <X11/Xutil.h>
#include <iostream>
#include <X11/Xlib.h>
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<Atom>(GeneralEvent.xclient.data.l[0]) == this->wmDelete) {
this->isWindowOpen = false;
}
} break;
}
}
}