glonkers/glonkers.c
2026-03-12 14:03:37 +01:00

116 lines
3.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <signal.h>
#include <stdint.h>
#include <wayland-client.h>
#include <wayland-util.h>
#include <pthread.h>
#include <sys/mman.h>
#include "renderer.h"
#include "wayland.h"
#include <string.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
void print_help(FILE *channel, char *bin_path) {
fprintf(channel, "Usage: %s [OPTION]... [PATH]\n", bin_path);
}
/// What is the argument parser currently reading?
enum list_state {
list_none,
list_ouptut,
list_texture,
};
int main(int argc, char *argv[]) {
char *shader_path = NULL;
int output_type = OUTPUT_WINDOW;
enum list_state list_state = list_none;
char **output_list = NULL;
char **texture_list = NULL;
int output_count = 0;
int texture_count = 0;
// syntax: --window for a normal window or --layer for a wallpaper
for (int i = 1; i < argc; i++) {
if (strcmp("--help", argv[i]) == 0) {
print_help(stdout, argv[0]);
return 0;
}
else if (strcmp("--window", argv[i]) == 0) {
output_type = OUTPUT_WINDOW;
list_state = list_none;
}
else if (strcmp("--layer", argv[i]) == 0) {
output_type = OUTPUT_LAYER;
list_state = list_none;
}
else if (strcmp("--output", argv[i]) == 0) {
output_count = 0;
output_list = &argv[i + 1];
list_state = list_ouptut;
}
else if (strcmp("--texture", argv[i]) == 0) {
texture_count = 0;
texture_list = &argv[i + 1];
list_state = list_texture;
}
else if (strcmp("--", argv[i]) == 0) {
list_state = list_none;
}
else if (list_state == list_ouptut) {
output_count++;
}
else if (list_state == list_texture) {
texture_count++;
if (texture_count > 4) {
fprintf(stderr, "maximum number of textures is 4\n");
return 1;
}
}
else if (list_state == list_none) {
// path to fragment shader
if (shader_path != NULL) {
fprintf(stderr, "Tried supplying '%s' as a shader file while one has already been selected.\n", argv[i]);
return 1;
}
shader_path = argv[i];
}
}
if (shader_path == NULL) {
fprintf(stderr, "Need to supply path to shader!\n");
print_help(stderr, argv[0]);
return 1;
}
struct client_state *state = wayland_init(output_type, output_list, output_count);
bool renderer_initialized = false;
Renderer renderer;
bool running = true;
while (running) {
struct event event = wait_for_event(state);
if (event.type == EVENT_DRAW) {
int width = event.data.draw.width;
int height = event.data.draw.height;
double time = event.data.draw.time / 1000.0;
if (!renderer_initialized) {
renderer = new_renderer(texture_list, texture_count);
renderer_initialized = true;
}
render(&renderer, width, height, time, shader_path, 0);
swap_buffers(state, event.data.draw.surface);
}
if (event.type == EVENT_NONE) {
}
if (event.type == EVENT_EXIT) {
running = false;
}
}
return 0;
}