wl-app

example app to learn the basic of wayland
Log | Files | Refs

commit a713260b2fa07792324c5b49f6216971ef52dd03
Author: mrgrouse <mrgrouse@mrgrouse.com>
Date:   Wed, 21 Oct 2026 19:22:31 -0400

initialize repository

Diffstat:
AMakefile | 30++++++++++++++++++++++++++++++
Amain.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Atypes.h | 8++++++++
3 files changed, 90 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,30 @@ +CC = clang +.DEFAULT_GOAL := wl-app + +CFLAGS = -O2 -Werror -Wall -Wextra +DEBUG_CFLAGS = -O0 -Werror -Wall -Wextra -g + +LDFLAGS = -lc +#LDFLAGS=-fPIE -static # for static linking + +SRCS = main.c +OBJS = $(SRCS:.c=.o) +BIN = wl-app + +PREFIX=/usr/local + +wl-app: o + $(CC) $(CFLAGS) $(OBJS) -o $(BIN) $(LDFLAGS) + +o: + $(CC) $(CFLAGS) -c $(SRCS) + +debug: + $(CC) $(DEBUG_CFLAGS) $(SRCS) -o $(BIN) $(LDFLAGS) + +clean: + rm -f $(BIN) $(OBJS) + +install: wl-app + cp -f $(BIN) $(PREFIX)/bin/$(BIN) + echo "$(BIN) installed at $(PREFIX)/bin/$(BIN)" diff --git a/main.c b/main.c @@ -0,0 +1,52 @@ +#include <errno.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <sys/un.h> +#include <sys/socket.h> + +#include "types.h" + +static int +wayland_display_connect(string *wsp) { + + struct sockaddr_un addr = { .sun_family = AF_UNIX }; + memcpy(addr.sun_path, wsp->data, wsp->len); // only way to assign our path to the addr .sun_path[], compiler expects length at runtime but it is variable + + int fd = socket(AF_UNIX, SOCK_STREAM, 0); + + if (fd == -1) + exit(errno); + // int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) + if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) + exit(errno); + + return fd; +} + +int +main(void) { + + string xdg_runtime_dir = { + .data = getenv("XDG_RUNTIME_DIR"), + .len = strlen(xdg_runtime_dir.data), + }; // we can keep this data for future use (temporary logs, pipewire, mpd, etc) + + string wayland_display = { + .data = getenv("WAYLAND_DISPLAY"), + .len = strlen(wayland_display.data), + }; // might be of use later idk yet + + string wl_sock_path = { + .data = strcat(strcat(xdg_runtime_dir.data, "/"), wayland_display.data), // convoluted to make it a path + .len = strlen(wl_sock_path.data), + }; + + if (wayland_display.data == NULL || xdg_runtime_dir.data == NULL) { + puts("Ensure XDG_RUNTIME_DIR and/or WAYLAND_DISPLAY are set."); + return 1; + } + + wayland_display_connect(&wl_sock_path); + return 0; +} diff --git a/types.h b/types.h @@ -0,0 +1,7 @@ +// types.h: function prototypes and structs +typedef struct { + char *data; + uint8_t len; +} string; + +static int wayland_display_connect(string *wsp); +\ No newline at end of file