Files
zswm/src/main.c
2025-08-08 22:53:33 +08:00

132 lines
3.1 KiB
C

#include <glib.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wordexp.h>
#include "config.h"
#include "types.h"
#include "utils.h"
#include "xcb.h"
static void init_monitors(void);
static wallpaper_config_t *parse_wallpaper_config(const char **wallpapers);
static void free_wallpaper_config(wallpaper_config_t *wallpaper_config);
static void cleanup(void);
static monitor_t *monitors = NULL;
static wallpaper_config_t *wallpaper_config = NULL;
void init_monitors(void) {
monitor_rect_t *mr = detect_monitor_rect();
uint32_t i = 0;
while (mr) {
monitor_t *m = ecalloc(1, sizeof(monitor_t));
m->monitor_x = mr->x;
m->monitor_y = mr->y;
m->monitor_width = mr->width;
m->monitor_height = mr->height;
m->index = i;
printf("mon[%u]: x: %d, y: %d, w: %u, h: %u\n", i, mr->x, mr->y, mr->width,
mr->height);
if (!i) monitors = m;
i++;
monitor_rect_t *next_mr = mr->next;
free(mr);
mr = next_mr;
}
}
wallpaper_config_t *parse_wallpaper_config(const char **wallpapers) {
if (!wallpapers) return NULL;
wallpaper_config_t *wc = ecalloc(1, sizeof(wallpaper_config_t));
GStrvBuilder *builder = g_strv_builder_new();
for (int i = 0; wallpapers && wallpapers[i]; i++) {
wordexp_t p;
if (wordexp(wallpapers[i], &p, 0)) continue;
for (size_t i = 0; i < p.we_wordc; i++) {
size_t len = strlen(p.we_wordv[i]) + 1;
char *realpath = ecalloc(len, sizeof(char));
strncpy(realpath, p.we_wordv[i], len);
g_strstrip(realpath);
if (file_exist(realpath)) g_strv_builder_add(builder, realpath);
free(realpath);
}
wordfree(&p);
}
wc->path_list = g_strv_builder_unref_to_strv(builder);
wc->count = g_strv_length(wc->path_list);
if (wc->count) return wc;
free_wallpaper_config(wc);
return NULL;
}
void free_wallpaper_config(wallpaper_config_t *wallpaper_config) {
if (!wallpaper_config) return;
g_strfreev(wallpaper_config->path_list);
free(wallpaper_config);
}
void cleanup(void) {
clean_xcb();
free_wallpaper_config(wallpaper_config);
wallpaper_config = NULL;
monitor_t *m = monitors;
client_t *c = m->clients;
while (m) {
while (c) {
client_t *tc = c->next;
free(c);
c = tc;
}
monitor_t *tm = m->next;
free(m);
m = tm;
}
monitors = NULL;
}
int main(int argc, char *argv[]) {
if (has_other_wm_running()) {
die("another window manager is already running");
}
wallpaper_config = parse_wallpaper_config(wallpager_list);
if (wallpaper_config) {
printf("wallpaper count: %u\n", wallpaper_config->count);
for (uint32_t i = 0; i < wallpaper_config->count; i++) {
printf("wallpaper[%02u]: %s\n", i, wallpaper_config->path_list[i]);
}
}
init_monitors();
window_list_t *window_list = scan_window_list();
if (window_list) {
for (uint32_t i = 0; i < window_list->count; i++) {
printf("window[%u]: 0x%x\n", i, window_list->list[i]);
}
} else {
printf("no window\n");
}
free_window_list(window_list);
cleanup();
return 0;
}