This commit is contained in:
SimonFJ20 2023-03-08 15:05:42 +01:00
parent f2f42466b0
commit 5bcfcfa400
6 changed files with 174 additions and 0 deletions

14
.clang-format Normal file
View File

@ -0,0 +1,14 @@
BasedOnStyle: WebKit
IndentWidth: 4
ColumnLimit: 80
IndentCaseLabels: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterFunction: true
SplitEmptyFunction: false
AlignAfterOpenBracket: BlockIndent
AlignOperands: AlignAfterOperator
BreakBeforeBinaryOperators: true
BinPackArguments: false
BinPackParameters: false

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.o
example
compile_flags.txt

19
Makefile Normal file
View File

@ -0,0 +1,19 @@
CFLAGS = -std=c17 -Wall -Wextra -Wpedantic -Wconversion
HEADERS = $(wildcard *.h)
all: compile_flags.txt example
example: based_gui.o example.o
gcc $^ -o $@ -lX11 -lXpm -lXext
%.o: %.c $(HEADERS)
gcc $< -c -o $@ $(CFLAGS)
clean:
rm -rf *.o wacc
compile_flags.txt:
echo -xc $(CFLAGS) | sed 's/\s\+/\n/g' > compile_flags.txt

110
based_gui.c Normal file
View File

@ -0,0 +1,110 @@
#include "based_gui.h"
#include <X11/Xlib.h>
#include <X11/extensions/Xdbe.h>
#include <X11/xpm.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
struct BasedWindow {
int a;
};
struct BasedApp {
Display* display;
Window window;
XdbeBackBuffer back_buffer;
bool has_back_buffer;
XdbeSwapInfo swap_info;
Atom wm_delete_window;
};
BasedApp* based_app_new(void)
{
BasedApp* app = malloc(sizeof(app));
XInitThreads();
app->display = XOpenDisplay(NULL);
unsigned long white = WhitePixel(app->display, DefaultScreen(app->display));
unsigned long black = BlackPixel(app->display, DefaultScreen(app->display));
app->window = XCreateSimpleWindow(
app->display,
DefaultRootWindow(app->display),
0,
0,
500,
500,
0,
white,
white
);
app->has_back_buffer = false;
// Create back buffer for double buffering
int xdbe_major_version, xdbe_minor_version;
if (XdbeQueryExtension(
app->display, &xdbe_major_version, &xdbe_minor_version
)) {
app->has_back_buffer = true;
app->back_buffer
= XdbeAllocateBackBufferName(app->display, app->window, 0);
app->swap_info
= (XdbeSwapInfo) { .swap_window = app->window, .swap_action = 0 };
}
XSelectInput(
app->display, app->window, ExposureMask | KeyPressMask | KeyReleaseMask
);
app->wm_delete_window
= XInternAtom(app->display, "WM_DELETE_WINDOW", false);
XSetWMProtocols(app->display, app->window, &app->wm_delete_window, 1);
XVisualInfo vinfo;
XMatchVisualInfo(
app->display, DefaultScreen(app->display), 32, TrueColor, &vinfo
);
XMapWindow(app->display, app->window);
return app;
}
BasedWindow* based_app_make_window(BasedApp* app)
{
(void)app;
return NULL;
}
void based_app_run(BasedApp* app)
{
XEvent event;
for (;;) {
XNextEvent(app->display, &event);
switch (event.type) {
case Expose:
if (!event.xexpose.x && !event.xexpose.y && !event.xexpose.width
&& !event.xexpose.height) {
break;
}
break;
case ClientMessage:
if ((uint64_t)event.xclient.data.l[0] == app->wm_delete_window)
return;
}
if (app->has_back_buffer)
XdbeSwapBuffers(app->display, &app->swap_info, 1);
}
}
void based_window_delete(BasedWindow* window) { free(window); }
void based_app_delete(BasedApp* app) { free(app); }

13
based_gui.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef BASED_GUI_H
#define BASED_GUI_H
typedef struct BasedWindow BasedWindow;
void based_window_delete(BasedWindow* window);
typedef struct BasedApp BasedApp;
BasedApp* based_app_new(void);
void based_app_delete(BasedApp* app);
BasedWindow* based_app_make_window(BasedApp* app);
void based_app_run(BasedApp* app);
#endif

15
example.c Normal file
View File

@ -0,0 +1,15 @@
#include "based_gui.h"
#include <stdio.h>
int main(void)
{
BasedApp* app = based_app_new();
BasedWindow* window = based_app_make_window(app);
based_app_run(app);
based_window_delete(window);
based_app_delete(app);
}