From 04c039d64a06097935378ff7eea792d8a2be32c9 Mon Sep 17 00:00:00 2001 From: IllusionMan1212 Date: Sun, 26 Nov 2023 01:29:17 +0200 Subject: [PATCH] non-resizable window and always floating window --- main.c | 3 ++- todo.txt | 2 -- ui.c | 10 ++++++++-- ui.h | 2 ++ x11.c | 25 +++++++++++++++++++++++++ x11.h | 2 ++ 6 files changed, 39 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index 56e1bf4..006be0b 100644 --- a/main.c +++ b/main.c @@ -122,11 +122,12 @@ int main(int argc, char *argv[]) { } Color clear_color = {0.2f, 0.15f, 0.5f, 1.f}; - int res = init_zephr(font_path, title, (Size){800, 600}, &clear_color); + int res = init_zephr(font_path, title, (Size){900, 900}, &clear_color); if (res != 0) { printf("[ERROR]: could not initialize zephr\n"); return 1; } + make_window_non_resizable(); srand(time(NULL)); diff --git a/todo.txt b/todo.txt index 2e824db..529fde6 100644 --- a/todo.txt +++ b/todo.txt @@ -1,8 +1,6 @@ - "premade" (but also customizable) ui elements (buttons, text boxes, etc) with instanced rendering - maybe force the window to be a specific resolution (for cudoku only? or all games?) - border decorations for shapes (maybe. or maybe only for ui elements) -- remove the hardcoded 1920 and 1080 values when applying relative pixel constraints -and replace them with the screen's currently active resolution text rendering improvements - [ ] unicode support (allows for non-ascii characters) diff --git a/ui.c b/ui.c index 155d114..88f3a05 100644 --- a/ui.c +++ b/ui.c @@ -241,6 +241,11 @@ int init_ui(const char* font_path, Size window_size) { return 0; } +// Has to be called after calling init_zephr() +void make_window_non_resizable() { + x11_make_window_non_resizable(display, window, zephr_context.window_size.width, zephr_context.window_size.height); +} + int init_zephr(const char* font_path, const char* window_title, Size window_size, Color *clear_color) { int res = audio_init(); if (res != 0) { @@ -263,6 +268,7 @@ int init_zephr(const char* font_path, const char* window_title, Size window_size zephr_context.clear_color = (Color){0.0, 0.0, 0.0, 1.0}; } + get_screen_size(display, &zephr_context.screen_size.width, &zephr_context.screen_size.height); start_internal_timer(); return 0; @@ -421,7 +427,7 @@ void set_width_constraint(UIConstraints *constraints, float value, UIConstraint constraints->width = (value * zephr_context.window_size.width); break; case UI_CONSTRAINT_RELATIVE_PIXELS: - constraints->width = zephr_context.window_size.width / 1920.f * value; + constraints->width = zephr_context.window_size.width / (float)zephr_context.screen_size.width * value; break; case UI_CONSTRAINT_ASPECT_RATIO: constraints->width = constraints->height * value; @@ -438,7 +444,7 @@ void set_height_constraint(UIConstraints *constraints, float value, UIConstraint constraints->height = (value * zephr_context.window_size.height); break; case UI_CONSTRAINT_RELATIVE_PIXELS: - constraints->height = zephr_context.window_size.height / 1080.f * value; + constraints->height = zephr_context.window_size.height / (float)zephr_context.screen_size.height * value; break; case UI_CONSTRAINT_ASPECT_RATIO: constraints->height = constraints->width * value; diff --git a/ui.h b/ui.h index 48f8383..8a581e5 100644 --- a/ui.h +++ b/ui.h @@ -32,6 +32,7 @@ typedef struct Text { typedef struct Context { bool should_quit; Color clear_color; + Size screen_size; Size window_size; ZephFont font; Text texts; @@ -68,6 +69,7 @@ typedef struct UIConstraints { int init_zephr(const char* font_path, const char* window_title, Size window_size, Color *clear_color); void deinit_zephr(); +void make_window_non_resizable(); bool zephr_should_quit(); void zephr_swap_buffers(); Size zephr_get_window_size(); diff --git a/x11.c b/x11.c index 800740f..37e17d3 100644 --- a/x11.c +++ b/x11.c @@ -2,6 +2,7 @@ #include #include +#include #include static bool fullscreen = false; @@ -38,6 +39,12 @@ int init_x11(Display **display, Window *window, const char* title, int window_wi DefaultDepth(*display, screen), InputOutput, visual, CWColormap | CWEventMask, &attributes); + // hints to the WM that the window is a dialog window which makes it a floating + // window in titling WMs (only tested on DWM though) + Atom net_wm_window_type = XInternAtom(*display, "_NET_WM_WINDOW_TYPE", False); + Atom net_wm_window_type_dialog = XInternAtom(*display, "_NET_WM_WINDOW_TYPE_DIALOG", False); + XChangeProperty(*display, *window, net_wm_window_type, XA_ATOM, 32, PropModeReplace, (unsigned char*)&net_wm_window_type_dialog, 1); + XMapWindow(*display, *window); XStoreName(*display, *window, title); @@ -160,3 +167,21 @@ void toggle_fullscreen(Display *display, Window window) { fullscreen = !fullscreen; } + +void get_screen_size(Display *display, int *width, int *height) { + Screen *screen = DefaultScreenOfDisplay(display); + + *width = screen->width; + *height = screen->height; +} + +void x11_make_window_non_resizable(Display *display, Window window, int width, int height) { + XSizeHints *size_hints = XAllocSizeHints(); + size_hints->flags = PMinSize | PMaxSize; + size_hints->min_width = width; + size_hints->min_height = height; + size_hints->max_width = width; + size_hints->max_height = height; + XSetWMNormalHints(display, window, size_hints); + XFree(size_hints); +} diff --git a/x11.h b/x11.h index caa9c6d..6c3d737 100644 --- a/x11.h +++ b/x11.h @@ -6,3 +6,5 @@ int init_x11(Display **display, Window *window, const char* title, int window_wi void close_x11(Display **display, Window *window); void resize_x11_window(Display *display, Window window); void toggle_fullscreen(Display *display, Window window); +void get_screen_size(Display *display, int *width, int *height); +void x11_make_window_non_resizable(Display *display, Window window, int width, int height);