Skip to content

Commit

Permalink
LInux: Window Resize Support
Browse files Browse the repository at this point in the history
  • Loading branch information
CardealRusso authored Oct 19, 2024
1 parent 3d2f3b0 commit 239783f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 0 additions & 2 deletions examples/mousebuttons-c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
* - Closes a window
* ============================================================ */
static int run() {
uint32_t buf[W * H];
struct fenster f = {
.title = "Mouse Buttons",
.width = W,
.height = H,
.buf = buf,
};
fenster_open(&f);
while (fenster_loop(&f) == 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/fenster/fenster.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

struct fenster {
const char *title;
const int width;
const int height;
int width;
int height;
uint32_t *buf;
int keys[256]; // keys are mostly ASCII, but arrows are 17..20
int modkeys[4]; // ctrl, shift, alt, meta
Expand Down
18 changes: 18 additions & 0 deletions src/fenster/fenster_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ static Atom wmDeleteWindow;
// clang-format on

FENSTER_API int fenster_open(struct fenster *f) {
f->buf = (uint32_t*)malloc(f->width * f->height * sizeof(uint32_t));
f->dpy = XOpenDisplay(NULL);
int screen = DefaultScreen(f->dpy);
f->w = XCreateSimpleWindow(f->dpy, RootWindow(f->dpy, screen), 0, 0, f->width,
Expand Down Expand Up @@ -39,6 +40,23 @@ FENSTER_API int fenster_loop(struct fenster *f) {
while (XPending(f->dpy)) {
XNextEvent(f->dpy, &ev);
switch (ev.type) {
case ConfigureNotify: {
if (ev.xconfigure.width != f->width || ev.xconfigure.height != f->height) {
uint32_t *new_buf = realloc(f->buf, ev.xconfigure.width * ev.xconfigure.height * sizeof(uint32_t));
if (!new_buf) break;

f->img->data = NULL;
XDestroyImage(f->img);

f->buf = new_buf;
f->width = ev.xconfigure.width;
f->height = ev.xconfigure.height;
f->buf_size = f->width * f->height * sizeof(uint32_t);

f->img = XCreateImage(f->dpy, DefaultVisual(f->dpy, 0), 24, ZPixmap, 0,
(char *)f->buf, f->width, f->height, 32, 0);
}
} break;
case ClientMessage:
if ((Atom)ev.xclient.data.l[0] == wmDeleteWindow) {
return 1;
Expand Down

0 comments on commit 239783f

Please sign in to comment.