Update
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 4m10s

This commit is contained in:
2024-08-05 13:35:39 -04:00
parent d9ee8ebedc
commit 7bc90af3d5
2 changed files with 250 additions and 243 deletions

View File

@@ -1,6 +1,7 @@
#include <iostream>
#include <rewindow/types/window.h>
#include <jlog/jlog.hpp>
#include <GL/gl.h>
Vector2 mouse_pos;
@@ -19,6 +20,8 @@ class MyWindow : public ReWindow::RWindow {
void OnKeyDown(const ReWindow::KeyDownEvent& e) override {}
void OnRefresh(float elapsed) override {
glClearColor(255, 0, 0, 255);
glClear(GL_COLOR_BUFFER_BIT);
glSwapBuffers();
auto pos = getCursorPos();
//std::cout << pos.x << ", " << pos.y << std::endl;

View File

@@ -85,11 +85,11 @@ void RWindow::pollEvents() {
if (xev.type == ClientMessage)
DEBUG(std::format("Recieved event '{}'", "ClientMessage"));
if (xev.xclient.message_type == XInternAtom(display, "WM_PROTOCOLS", False) && static_cast<Atom>(xev.xclient.data.l[0]) == wmDeleteWindow) {
destroyWindow();
system("xset r on");
exit(0);
}
if (xev.xclient.message_type == XInternAtom(display, "WM_PROTOCOLS", False) && static_cast<Atom>(xev.xclient.data.l[0]) == wmDeleteWindow) {
destroyWindow();
system("xset r on");
exit(0);
}
if (xev.type == FocusIn) {
DEBUG(std::format("Recieved event '{}'", "FocusIn"));
@@ -103,251 +103,255 @@ void RWindow::pollEvents() {
if (xev.type == FocusOut) {
DEBUG(std::format("Recieved event '{}'", "FocusOut"));
XAutoRepeatOn(display);
//focusLost.Invoke();
RWindowEvent event {};
OnFocusLost(event);
OnFocusLostEvent(event);
setFlag(RWindowFlags::IN_FOCUS, false);
}
if (xev.type == KeyRelease) {
DEBUG(std::format("Recieved event '{}'", "KeyRelease"));
auto scancode = (X11Scancode) xev.xkey.keycode;
auto key = GetKeyFromX11Scancode(scancode);
OnKeyUpEvent(key);
OnKeyUp(key);
liftKey(key);
}
if (xev.type == KeyPress) {
DEBUG(std::format("Recieved event '{}'", "KeyPress"));
auto scancode = (X11Scancode) xev.xkey.keycode;
auto key = GetKeyFromX11Scancode(scancode);
OnKeyDownEvent(key);
OnKeyDown(key);
pressKey(key);
//eventLog.push_back(eventData);
}
if (xev.type == ButtonRelease) {
DEBUG(std::format("Recieved event '{}'", "ButtonRelease"));
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
auto eventData = MouseButtonUpEvent();
eventData.Button = button;
OnMouseButtonUpEvent(eventData);
OnMouseButtonUp(eventData);
}
if (xev.type == ButtonPress) {
DEBUG(std::format("Recieved event '{}'", "ButtonPress"));
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
auto eventData = MouseButtonDownEvent(button);
eventData.Button = button;
OnMouseButtonDownEvent(eventData);
OnMouseButtonDown(eventData);
}
if (xev.type == Expose)
{
DEBUG(std::format("Recieved event '{}'", "Expose"));
}
// NOTE: This event is functionally useless, as it only informs of the very beginning and end of a mouse movement.
if (xev.type == MotionNotify)
{
DEBUG(std::format("Recieved event '{}'", "MotionNotify"));
}
if (xev.type == ResizeRequest) {
DEBUG(std::format("Recieved event '{}'", "ResizeRequest"));
auto eventData = WindowResizeRequestEvent();
eventData.Size = {(float)xev.xresizerequest.width, (float)xev.xresizerequest.height};
glViewport(0, 0, (GLsizei)xev.xresizerequest.width, (GLsizei)xev.xresizerequest.height);
lastKnownWindowSize = eventData.Size;
OnResizeRequest(eventData);
OnResizeRequestEvent(eventData);
}
}
previousKeyboard = currentKeyboard;
}
// Might make the window go off the screen on some window managers.
void RWindow::setSize(int newWidth, int newHeight) {
if (!getFlag(RWindowFlags::RESIZABLE)) return;
this->width = newWidth;
this->height = newHeight;
XResizeWindow(display, window, width, height);
glViewport(0, 0, width, height);
DEBUG(std::format("Set size for window '{}'. width={} height={}", this->title, newWidth, newHeight));
}
Vector2 RWindow::getCursorPos() const {
Window root = XDefaultRootWindow(display);
Window root_return;
Window child_return;
int root_x_ret;
int root_y_ret;
int win_x_ret;
int win_y_ret;
uint32_t mask_return;
unsigned m;
bool mouseAvailable = XQueryPointer(display, window, &root_return, &child_return, &root_x_ret, &root_y_ret, &win_x_ret, &win_y_ret, &mask_return);
if (mouseAvailable) {
// TODO: process retrieved mouse coordinates
// TODO: normalize coordinates from displaySpace to windowSpace
// TODO: fire mouse movement event
//std::cout << win_x_ret << ", " << win_y_ret << std::endl;
// TODO: Compensate for height of window TitleBar + window border width
float window_border_width = 2;
float window_titlebar_height = 18;
Vector2 mouse_coords_raw = {(float)win_x_ret+window_border_width, (float)win_y_ret+window_titlebar_height};
auto window_pos = getPos();
return mouse_coords_raw - window_pos;
}
return Vector2::Zero;
}
// TODO: implement integer vector2/3 types
Vector2 RWindow::getSize() const {
XGetWindowAttributes(display,window,&windowAttributes);
return {(float)windowAttributes.width, (float)windowAttributes.height};
}
Vector2 RWindow::getLastKnownResize() const
{
return lastKnownWindowSize;
}
// TODO: implement integer vector2/3 types
Vector2 RWindow::getPos() const {
XGetWindowAttributes(display,window,&windowAttributes);
return {(float)windowAttributes.x, (float)windowAttributes.y};
}
void RWindow::setPos(int x, int y) {
XMoveWindow(display, window, x, y);
}
void RWindow::setPos(const Vector2& pos) {
this->setPos(pos.x, pos.y);
}
void RWindow::glSwapBuffers() {
glXSwapBuffers(display,window);
}
bool RWindow::isResizable() const {
return this->getFlag(RWindowFlags::RESIZABLE);
}
void RWindow::fullscreen() {
DEBUG(std::format("Fullscreening window '{}'", this->title));
fullscreenmode = true;
XEvent xev;
Atom wm_state = XInternAtom(display, "_NET_WM_STATE", true);
Atom wm_fullscreen = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", true);
XChangeProperty(display, window, wm_state, XA_ATOM, 32, PropModeReplace, (unsigned char *)&wm_fullscreen, 1);
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.window = window;
xev.xclient.message_type = wm_state;
xev.xclient.format = 32;
xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
xev.xclient.data.l[1] = fullscreenmode;
xev.xclient.data.l[2] = 0;
XSendEvent(display, DefaultRootWindow(display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
DEBUG(std::format("Fullscreened window '{}'", this->title));
}
void RWindow::restoreFromFullscreen() {
DEBUG(std::format("Restoring window '{}' from fullscreen", this->title));
fullscreenmode = false;
XEvent xev;
Atom wm_state = XInternAtom(display, "_NET_WM_STATE", False);
Atom fullscreen = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", False);
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.window = window;
xev.xclient.message_type = wm_state;
xev.xclient.format = 32;
xev.xclient.data.l[0] = 0; // _NET_WM_STATE_REMOVE
xev.xclient.data.l[1] = fullscreenmode;
xev.xclient.data.l[2] = 0;
XSendEvent(display, DefaultRootWindow(display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
DEBUG(std::format("Restored window '{}' from fullscreen", this->title));
}
void RWindow::setVsyncEnabled(bool b) {
PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT;
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
glXSwapIntervalEXT(display, window, b);
}
bool RWindow::isFullscreen() const {
return fullscreenmode;
}
void RWindow::setCursorStyle(CursorStyle style) const {
u32 x11_cursor_resolved_enum = static_cast<u32>(style.X11Cursor);
Cursor c = XCreateFontCursor(display, x11_cursor_resolved_enum);
XDefineCursor(display, window, c);
}
void RWindow::Open() {
xSetWindowAttributes.border_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.background_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.override_redirect = True;
xSetWindowAttributes.event_mask = ExposureMask;
//setVsyncEnabled(vsync);
if (renderer == RenderingAPI::OPENGL) {
GLint glAttributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
visual = glXChooseVisual(display, defaultScreen, glAttributes);
glContext = glXCreateContext(display, visual, nullptr, GL_TRUE);
XAutoRepeatOn(display);
//focusLost.Invoke();
RWindowEvent event {};
OnFocusLost(event);
OnFocusLostEvent(event);
setFlag(RWindowFlags::IN_FOCUS, false);
}
xSetWindowAttributes.colormap = XCreateColormap(display, RootWindow(display, defaultScreen), visual->visual, AllocNone);
if (xev.type == KeyRelease) {
DEBUG(std::format("Recieved event '{}'", "KeyRelease"));
auto scancode = (X11Scancode) xev.xkey.keycode;
auto key = GetKeyFromX11Scancode(scancode);
OnKeyUpEvent(key);
OnKeyUp(key);
liftKey(key);
}
window = XCreateWindow(display, RootWindow(display, defaultScreen), 0, 0, width, height, 0, visual->depth,
InputOutput, visual->visual, CWBackPixel | CWColormap | CWBorderPixel | NoEventMask,
&xSetWindowAttributes);
// Set window to floating because fucking tiling WMs
windowTypeAtom = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
windowTypeUtilityAtom = XInternAtom(display, "_NET_WM_WINDOW_TYPE_UTILITY", False);
XChangeProperty(display, window, windowTypeAtom, XA_ATOM, 32, PropModeReplace,
if (xev.type == KeyPress) {
DEBUG(std::format("Recieved event '{}'", "KeyPress"));
auto scancode = (X11Scancode) xev.xkey.keycode;
auto key = GetKeyFromX11Scancode(scancode);
OnKeyDownEvent(key);
OnKeyDown(key);
pressKey(key);
//eventLog.push_back(eventData);
}
if (xev.type == ButtonRelease) {
DEBUG(std::format("Recieved event '{}'", "ButtonRelease"));
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
auto eventData = MouseButtonUpEvent();
eventData.Button = button;
OnMouseButtonUpEvent(eventData);
OnMouseButtonUp(eventData);
}
if (xev.type == ButtonPress) {
DEBUG(std::format("Recieved event '{}'", "ButtonPress"));
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
auto eventData = MouseButtonDownEvent(button);
eventData.Button = button;
OnMouseButtonDownEvent(eventData);
OnMouseButtonDown(eventData);
}
if (xev.type == Expose)
{
DEBUG(std::format("Recieved event '{}'", "Expose"));
}
// NOTE: This event is functionally useless, as it only informs of the very beginning and end of a mouse movement.
if (xev.type == MotionNotify)
{
DEBUG(std::format("Recieved event '{}'", "MotionNotify"));
}
if (xev.type == ResizeRequest) {
DEBUG(std::format("Recieved event '{}'", "ResizeRequest"));
this->width = xev.xresizerequest.width;
this->height = xev.xresizerequest.height;
auto eventData = WindowResizeRequestEvent();
eventData.Size = {(float)xev.xresizerequest.width, (float)xev.xresizerequest.height};
lastKnownWindowSize = eventData.Size;
OnResizeRequest(eventData);
OnResizeRequestEvent(eventData);
glViewport(0, 0, (GLsizei)xev.xresizerequest.width, (GLsizei)xev.xresizerequest.height);
}
}
previousKeyboard = currentKeyboard;
}
// Might make the window go off the screen on some window managers.
void RWindow::setSize(int newWidth, int newHeight) {
if (!getFlag(RWindowFlags::RESIZABLE)) return;
this->width = newWidth;
this->height = newHeight;
XResizeWindow(display, window, newWidth, newHeight);
XFlush(display);
glViewport(0, 0, width, height);
DEBUG(std::format("Set size for window '{}'. width={} height={}", this->title, newWidth, newHeight));
}
Vector2 RWindow::getCursorPos() const {
Window root = XDefaultRootWindow(display);
Window root_return;
Window child_return;
int root_x_ret;
int root_y_ret;
int win_x_ret;
int win_y_ret;
uint32_t mask_return;
unsigned m;
bool mouseAvailable = XQueryPointer(display, window, &root_return, &child_return, &root_x_ret, &root_y_ret, &win_x_ret, &win_y_ret, &mask_return);
if (mouseAvailable) {
// TODO: process retrieved mouse coordinates
// TODO: normalize coordinates from displaySpace to windowSpace
// TODO: fire mouse movement event
//std::cout << win_x_ret << ", " << win_y_ret << std::endl;
// TODO: Compensate for height of window TitleBar + window border width
float window_border_width = 2;
float window_titlebar_height = 18;
Vector2 mouse_coords_raw = {(float)win_x_ret+window_border_width, (float)win_y_ret+window_titlebar_height};
auto window_pos = getPos();
return mouse_coords_raw - window_pos;
}
return Vector2::Zero;
}
// TODO: implement integer vector2/3 types
Vector2 RWindow::getSize() const {
XGetWindowAttributes(display,window,&windowAttributes);
return {(float)windowAttributes.width, (float)windowAttributes.height};
}
Vector2 RWindow::getLastKnownResize() const
{
return lastKnownWindowSize;
}
// TODO: implement integer vector2/3 types
Vector2 RWindow::getPos() const {
XGetWindowAttributes(display,window,&windowAttributes);
return {(float)windowAttributes.x, (float)windowAttributes.y};
}
void RWindow::setPos(int x, int y) {
XMoveWindow(display, window, x, y);
}
void RWindow::setPos(const Vector2& pos) {
this->setPos(pos.x, pos.y);
}
void RWindow::glSwapBuffers() {
glXSwapBuffers(display,window);
}
bool RWindow::isResizable() const {
return this->getFlag(RWindowFlags::RESIZABLE);
}
void RWindow::fullscreen() {
DEBUG(std::format("Fullscreening window '{}'", this->title));
fullscreenmode = true;
XEvent xev;
Atom wm_state = XInternAtom(display, "_NET_WM_STATE", true);
Atom wm_fullscreen = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", true);
XChangeProperty(display, window, wm_state, XA_ATOM, 32, PropModeReplace, (unsigned char *)&wm_fullscreen, 1);
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.window = window;
xev.xclient.message_type = wm_state;
xev.xclient.format = 32;
xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
xev.xclient.data.l[1] = fullscreenmode;
xev.xclient.data.l[2] = 0;
XSendEvent(display, DefaultRootWindow(display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
DEBUG(std::format("Fullscreened window '{}'", this->title));
}
void RWindow::restoreFromFullscreen() {
DEBUG(std::format("Restoring window '{}' from fullscreen", this->title));
fullscreenmode = false;
XEvent xev;
Atom wm_state = XInternAtom(display, "_NET_WM_STATE", False);
Atom fullscreen = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", False);
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.window = window;
xev.xclient.message_type = wm_state;
xev.xclient.format = 32;
xev.xclient.data.l[0] = 0; // _NET_WM_STATE_REMOVE
xev.xclient.data.l[1] = fullscreenmode;
xev.xclient.data.l[2] = 0;
XSendEvent(display, DefaultRootWindow(display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
DEBUG(std::format("Restored window '{}' from fullscreen", this->title));
}
void RWindow::setVsyncEnabled(bool b) {
PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT;
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
glXSwapIntervalEXT(display, window, b);
}
bool RWindow::isFullscreen() const {
return fullscreenmode;
}
void RWindow::setCursorStyle(CursorStyle style) const {
u32 x11_cursor_resolved_enum = static_cast<u32>(style.X11Cursor);
Cursor c = XCreateFontCursor(display, x11_cursor_resolved_enum);
XDefineCursor(display, window, c);
}
void RWindow::Open() {
xSetWindowAttributes.border_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.background_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.override_redirect = True;
xSetWindowAttributes.event_mask = ExposureMask;
//setVsyncEnabled(vsync);
if (renderer == RenderingAPI::OPENGL) {
GLint glAttributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
visual = glXChooseVisual(display, defaultScreen, glAttributes);
glContext = glXCreateContext(display, visual, nullptr, GL_TRUE);
}
xSetWindowAttributes.colormap = XCreateColormap(display, RootWindow(display, defaultScreen), visual->visual, AllocNone);
window = XCreateWindow(display, RootWindow(display, defaultScreen), 0, 0, width, height, 0, visual->depth,
InputOutput, visual->visual, CWBackPixel | CWColormap | CWBorderPixel | NoEventMask,
&xSetWindowAttributes);
// Set window to floating because fucking tiling WMs
windowTypeAtom = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
windowTypeUtilityAtom = XInternAtom(display, "_NET_WM_WINDOW_TYPE_UTILITY", False);
XChangeProperty(display, window, windowTypeAtom, XA_ATOM, 32, PropModeReplace,
(unsigned char *)&windowTypeUtilityAtom, 1);
//
XSelectInput(display, window,
ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
PointerMotionMask |
PointerMotionHintMask | FocusChangeMask | StructureNotifyMask | SubstructureRedirectMask |
SubstructureNotifyMask | CWColormap | ResizeRequest | ResizeRedirectMask);
XMapWindow(display, window);
XStoreName(display, window, title.c_str());
wmDeleteWindow = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &wmDeleteWindow, 1);
//
XSelectInput(display, window,
ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
PointerMotionMask |
PointerMotionHintMask | FocusChangeMask | StructureNotifyMask | SubstructureRedirectMask |
SubstructureNotifyMask | CWColormap | ResizeRequest);
XMapWindow(display, window);
XStoreName(display, window, title.c_str());
wmDeleteWindow = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &wmDeleteWindow, 1);
if (renderer == RenderingAPI::OPENGL)
glXMakeCurrent(display, window, glContext);
open = true;
}
if (renderer == RenderingAPI::OPENGL)
glXMakeCurrent(display, window, glContext);
open = true;
}
void RWindow::setTitle(const std::string &title) {
this->title = title;
XStoreName(display, window, title.c_str());
}
void RWindow::setTitle(const std::string &title) {
this->title = title;
XStoreName(display, window, title.c_str());
}
// TODO: Implement MouseButton map
bool RWindow::isMouseButtonDown(MouseButton button) const {
return false;
}
// TODO: Implement MouseButton map
bool RWindow::isMouseButtonDown(MouseButton button) const {
return false;
}