mirror of
https://github.com/blazblue547-afk/aero3.git
synced 2026-12-02 14:24:17 -05:00
584 lines
24 KiB
C
584 lines
24 KiB
C
/* aerowm — a small wlroots-based Wayland compositor for AeroOS.
|
|||
|
|
*
|
||
|
|
* Adds, on top of a tinywl-style xdg-shell compositor:
|
||
|
|
* - a full-screen Frutiger Aero wallpaper (cover-scaled)
|
||
|
|
* - a pre-blurred copy of the wallpaper used as a frosted backdrop
|
||
|
|
* - a glassmorphism home UI (top bar + welcome card + dock) drawn with cairo
|
||
|
|
* - a live clock that re-renders the glass UI each minute
|
||
|
|
* - autostart of the lofi player + user session
|
||
|
|
*
|
||
|
|
* Targets wlroots 0.18. Build with meson (see ../meson.build).
|
||
|
|
*/
|
||
|
|
#define _POSIX_C_SOURCE 200809L
|
||
|
|
#include <assert.h>
|
||
|
|
#include <getopt.h>
|
||
|
|
#include <stdbool.h>
|
||
|
|
#include <stdint.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <string.h>
|
||
|
|
#include <time.h>
|
||
|
|
#include <unistd.h>
|
||
|
|
|
||
|
|
#include <wayland-server-core.h>
|
||
|
|
#include <wlr/backend.h>
|
||
|
|
#include <wlr/interfaces/wlr_buffer.h>
|
||
|
|
#include <wlr/render/allocator.h>
|
||
|
|
#include <wlr/render/wlr_renderer.h>
|
||
|
|
#include <wlr/types/wlr_cursor.h>
|
||
|
|
#include <wlr/types/wlr_compositor.h>
|
||
|
|
#include <wlr/types/wlr_data_device.h>
|
||
|
|
#include <wlr/types/wlr_input_device.h>
|
||
|
|
#include <wlr/types/wlr_keyboard.h>
|
||
|
|
#include <wlr/types/wlr_output.h>
|
||
|
|
#include <wlr/types/wlr_output_layout.h>
|
||
|
|
#include <wlr/types/wlr_pointer.h>
|
||
|
|
#include <wlr/types/wlr_scene.h>
|
||
|
|
#include <wlr/types/wlr_seat.h>
|
||
|
|
#include <wlr/types/wlr_subcompositor.h>
|
||
|
|
#include <wlr/types/wlr_xcursor_manager.h>
|
||
|
|
#include <wlr/types/wlr_xdg_shell.h>
|
||
|
|
#include <wlr/util/log.h>
|
||
|
|
#include <xkbcommon/xkbcommon.h>
|
||
|
|
|
||
|
|
#include "image.h"
|
||
|
|
#include "blur.h"
|
||
|
|
#include "panel.h"
|
||
|
|
|
||
|
|
/* ----------------------------------------------------------------- aero_buffer
|
||
|
|
* Minimal wlr_buffer backed by a malloc'd ARGB8888 image, so cairo-rendered
|
||
|
|
* UI and the wallpaper can be placed directly into the wlroots scene graph.
|
||
|
|
*/
|
||
|
|
struct aero_buffer {
|
||
|
|
struct wlr_buffer base;
|
||
|
|
uint32_t *data; /* ARGB8888, owned */
|
||
|
|
size_t stride; /* bytes */
|
||
|
|
};
|
||
|
|
|
||
|
|
static void aero_buffer_destroy(struct wlr_buffer *b) {
|
||
|
|
struct aero_buffer *buf = wl_container_of(b, buf, base);
|
||
|
|
free(buf->data);
|
||
|
|
free(buf);
|
||
|
|
}
|
||
|
|
static bool aero_buffer_begin_ptr(struct wlr_buffer *b, uint32_t flags,
|
||
|
|
void **data, uint32_t *format, size_t *stride) {
|
||
|
|
struct aero_buffer *buf = wl_container_of(b, buf, base);
|
||
|
|
if (flags & WLR_BUFFER_DATA_PTR_ACCESS_WRITE) return false;
|
||
|
|
*data = buf->data; *stride = buf->stride;
|
||
|
|
*format = 0x34325241; /* DRM_FORMAT_ARGB8888 */
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
static void aero_buffer_end_ptr(struct wlr_buffer *b) { (void)b; }
|
||
|
|
|
||
|
|
static const struct wlr_buffer_impl aero_buffer_impl = {
|
||
|
|
.destroy = aero_buffer_destroy,
|
||
|
|
.begin_data_ptr_access = aero_buffer_begin_ptr,
|
||
|
|
.end_data_ptr_access = aero_buffer_end_ptr,
|
||
|
|
};
|
||
|
|
|
||
|
|
/* Wrap an aero_image (consumes its data) into a wlr_buffer. */
|
||
|
|
static struct aero_buffer *aero_buffer_from_image(struct aero_image *img) {
|
||
|
|
struct aero_buffer *buf = calloc(1, sizeof(*buf));
|
||
|
|
if (!buf) return NULL;
|
||
|
|
wlr_buffer_init(&buf->base, &aero_buffer_impl, img->width, img->height);
|
||
|
|
buf->data = img->data; /* take ownership */
|
||
|
|
buf->stride = (size_t)img->stride * 4;
|
||
|
|
img->data = NULL;
|
||
|
|
return buf;
|
||
|
|
}
|
||
|
|
|
||
|
|
/* ----------------------------------------------------------------- server */
|
||
|
|
enum cursor_mode { CURSOR_PASSTHROUGH, CURSOR_MOVE, CURSOR_RESIZE };
|
||
|
|
|
||
|
|
struct aero_server {
|
||
|
|
struct wl_display *wl_display;
|
||
|
|
struct wlr_backend *backend;
|
||
|
|
struct wlr_renderer *renderer;
|
||
|
|
struct wlr_allocator *allocator;
|
||
|
|
struct wlr_scene *scene;
|
||
|
|
struct wlr_scene_output_layout *scene_layout;
|
||
|
|
|
||
|
|
struct wlr_xdg_shell *xdg_shell;
|
||
|
|
struct wl_listener new_xdg_toplevel;
|
||
|
|
struct wl_list toplevels;
|
||
|
|
|
||
|
|
struct wlr_cursor *cursor;
|
||
|
|
struct wlr_xcursor_manager *cursor_mgr;
|
||
|
|
struct wl_listener cursor_motion, cursor_motion_absolute;
|
||
|
|
struct wl_listener cursor_button, cursor_axis, cursor_frame;
|
||
|
|
enum cursor_mode cursor_mode;
|
||
|
|
|
||
|
|
struct wlr_seat *seat;
|
||
|
|
struct wl_listener new_input, request_cursor, request_set_selection;
|
||
|
|
struct wl_list keyboards;
|
||
|
|
|
||
|
|
struct wlr_output_layout *output_layout;
|
||
|
|
struct wl_list outputs;
|
||
|
|
struct wl_listener new_output;
|
||
|
|
|
||
|
|
/* glassmorphism layers */
|
||
|
|
struct wlr_scene_tree *layer_bg; /* sharp wallpaper */
|
||
|
|
struct wlr_scene_tree *layer_apps; /* client windows */
|
||
|
|
struct wlr_scene_tree *layer_ui; /* glass home UI */
|
||
|
|
struct aero_image wallpaper; /* loaded source */
|
||
|
|
struct aero_image blurred_full; /* per-output blurred backdrop */
|
||
|
|
struct wlr_scene_buffer *ui_node;
|
||
|
|
int ui_w, ui_h;
|
||
|
|
struct wl_event_source *clock_timer;
|
||
|
|
};
|
||
|
|
|
||
|
|
struct aero_output {
|
||
|
|
struct wl_list link;
|
||
|
|
struct aero_server *server;
|
||
|
|
struct wlr_output *wlr_output;
|
||
|
|
struct wl_listener frame, destroy;
|
||
|
|
};
|
||
|
|
|
||
|
|
struct aero_toplevel {
|
||
|
|
struct wl_list link;
|
||
|
|
struct aero_server *server;
|
||
|
|
struct wlr_xdg_toplevel *xdg_toplevel;
|
||
|
|
struct wlr_scene_tree *scene_tree;
|
||
|
|
struct wl_listener map, unmap, commit, destroy;
|
||
|
|
struct wl_listener request_move, request_resize;
|
||
|
|
};
|
||
|
|
|
||
|
|
struct aero_keyboard {
|
||
|
|
struct wl_list link;
|
||
|
|
struct aero_server *server;
|
||
|
|
struct wlr_keyboard *wlr_keyboard;
|
||
|
|
struct wl_listener modifiers, key, destroy;
|
||
|
|
};
|
||
|
|
|
||
|
|
/* ----------------------------------------------------------------- helpers */
|
||
|
|
static void spawn(const char *cmd) {
|
||
|
|
if (fork() == 0) {
|
||
|
|
setsid();
|
||
|
|
execl("/bin/sh", "/bin/sh", "-c", cmd, (void *)NULL);
|
||
|
|
_exit(127);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static void focus_toplevel(struct aero_toplevel *toplevel) {
|
||
|
|
if (!toplevel) return;
|
||
|
|
struct aero_server *server = toplevel->server;
|
||
|
|
struct wlr_seat *seat = server->seat;
|
||
|
|
struct wlr_surface *surface = toplevel->xdg_toplevel->base->surface;
|
||
|
|
struct wlr_surface *prev = seat->keyboard_state.focused_surface;
|
||
|
|
if (prev == surface) return;
|
||
|
|
if (prev) {
|
||
|
|
struct wlr_xdg_toplevel *pt = wlr_xdg_toplevel_try_from_wlr_surface(prev);
|
||
|
|
if (pt) wlr_xdg_toplevel_set_activated(pt, false);
|
||
|
|
}
|
||
|
|
struct wlr_keyboard *kb = wlr_seat_get_keyboard(seat);
|
||
|
|
wlr_scene_node_raise_to_top(&toplevel->scene_tree->node);
|
||
|
|
wl_list_remove(&toplevel->link);
|
||
|
|
wl_list_insert(&server->toplevels, &toplevel->link);
|
||
|
|
wlr_xdg_toplevel_set_activated(toplevel->xdg_toplevel, true);
|
||
|
|
if (kb)
|
||
|
|
wlr_seat_keyboard_notify_enter(seat, surface, kb->keycodes,
|
||
|
|
kb->num_keycodes, &kb->modifiers);
|
||
|
|
}
|
||
|
|
|
||
|
|
/* -------------------------------------------------- glass UI build/refresh */
|
||
|
|
static void refresh_clock(struct aero_server *s);
|
||
|
|
|
||
|
|
static void build_ui_for_output(struct aero_server *s, int W, int H) {
|
||
|
|
/* (Re)build the blurred backdrop + glass UI sized to the output. */
|
||
|
|
if (s->blurred_full.data) aero_image_free(&s->blurred_full);
|
||
|
|
|
||
|
|
struct aero_image cover;
|
||
|
|
if (aero_image_cover(&s->wallpaper, W, H, &cover) != 0) return;
|
||
|
|
|
||
|
|
/* background: sharp wallpaper */
|
||
|
|
struct aero_buffer *bgbuf = aero_buffer_from_image(&cover); /* consumes cover */
|
||
|
|
if (bgbuf) {
|
||
|
|
struct wlr_scene_buffer *bg =
|
||
|
|
wlr_scene_buffer_create(s->layer_bg, &bgbuf->base);
|
||
|
|
wlr_scene_node_set_position(&bg->node, 0, 0);
|
||
|
|
wlr_buffer_drop(&bgbuf->base);
|
||
|
|
}
|
||
|
|
|
||
|
|
/* frosted backdrop: blurred cover (re-make cover since consumed) */
|
||
|
|
struct aero_image cover2;
|
||
|
|
if (aero_image_cover(&s->wallpaper, W, H, &cover2) == 0) {
|
||
|
|
s->blurred_full.width = cover2.width;
|
||
|
|
s->blurred_full.height = cover2.height;
|
||
|
|
s->blurred_full.stride = cover2.stride;
|
||
|
|
s->blurred_full.data = aero_blur_copy(cover2.data, cover2.width,
|
||
|
|
cover2.height, cover2.stride, 14, 3);
|
||
|
|
aero_image_free(&cover2);
|
||
|
|
}
|
||
|
|
|
||
|
|
s->ui_w = W; s->ui_h = H;
|
||
|
|
refresh_clock(s);
|
||
|
|
}
|
||
|
|
|
||
|
|
static int clock_tick(void *data) {
|
||
|
|
struct aero_server *s = data;
|
||
|
|
refresh_clock(s);
|
||
|
|
/* re-arm the same timer (every 30s) instead of leaking a new source */
|
||
|
|
if (s->clock_timer)
|
||
|
|
wl_event_source_timer_update(s->clock_timer, 30000);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void refresh_clock(struct aero_server *s) {
|
||
|
|
if (!s->blurred_full.data || s->ui_w == 0) return;
|
||
|
|
char ct[64], dt[64];
|
||
|
|
time_t t = time(NULL);
|
||
|
|
struct tm tmv; localtime_r(&t, &tmv);
|
||
|
|
strftime(ct, sizeof ct, "<b>%H:%M</b>", &tmv);
|
||
|
|
strftime(dt, sizeof dt, "%A, %d %B %Y", &tmv);
|
||
|
|
|
||
|
|
struct aero_image ui;
|
||
|
|
if (aero_panel_render(&s->blurred_full, s->ui_w, s->ui_h, ct, dt, &ui) != 0)
|
||
|
|
return;
|
||
|
|
struct aero_buffer *uibuf = aero_buffer_from_image(&ui); /* consumes ui */
|
||
|
|
if (!uibuf) return;
|
||
|
|
if (s->ui_node) {
|
||
|
|
wlr_scene_buffer_set_buffer(s->ui_node, &uibuf->base);
|
||
|
|
} else {
|
||
|
|
s->ui_node = wlr_scene_buffer_create(s->layer_ui, &uibuf->base);
|
||
|
|
wlr_scene_node_set_position(&s->ui_node->node, 0, 0);
|
||
|
|
}
|
||
|
|
wlr_buffer_drop(&uibuf->base);
|
||
|
|
}
|
||
|
|
|
||
|
|
/* ----------------------------------------------------------------- output */
|
||
|
|
static void output_frame(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_output *output = wl_container_of(listener, output, frame);
|
||
|
|
struct wlr_scene *scene = output->server->scene;
|
||
|
|
struct wlr_scene_output *so =
|
||
|
|
wlr_scene_get_scene_output(scene, output->wlr_output);
|
||
|
|
wlr_scene_output_commit(so, NULL);
|
||
|
|
struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now);
|
||
|
|
wlr_scene_output_send_frame_done(so, &now);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void output_destroy(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_output *output = wl_container_of(listener, output, destroy);
|
||
|
|
wl_list_remove(&output->frame.link);
|
||
|
|
wl_list_remove(&output->destroy.link);
|
||
|
|
wl_list_remove(&output->link);
|
||
|
|
free(output);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void server_new_output(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_server *server = wl_container_of(listener, server, new_output);
|
||
|
|
struct wlr_output *wlr_output = data;
|
||
|
|
wlr_output_init_render(wlr_output, server->allocator, server->renderer);
|
||
|
|
|
||
|
|
struct wlr_output_state state;
|
||
|
|
wlr_output_state_init(&state);
|
||
|
|
wlr_output_state_set_enabled(&state, true);
|
||
|
|
struct wlr_output_mode *mode = wlr_output_preferred_mode(wlr_output);
|
||
|
|
if (mode) wlr_output_state_set_mode(&state, mode);
|
||
|
|
wlr_output_commit_state(wlr_output, &state);
|
||
|
|
wlr_output_state_finish(&state);
|
||
|
|
|
||
|
|
struct aero_output *output = calloc(1, sizeof(*output));
|
||
|
|
output->wlr_output = wlr_output;
|
||
|
|
output->server = server;
|
||
|
|
output->frame.notify = output_frame;
|
||
|
|
wl_signal_add(&wlr_output->events.frame, &output->frame);
|
||
|
|
output->destroy.notify = output_destroy;
|
||
|
|
wl_signal_add(&wlr_output->events.destroy, &output->destroy);
|
||
|
|
wl_list_insert(&server->outputs, &output->link);
|
||
|
|
|
||
|
|
struct wlr_output_layout_output *lo =
|
||
|
|
wlr_output_layout_add_auto(server->output_layout, wlr_output);
|
||
|
|
struct wlr_scene_output *scene_output =
|
||
|
|
wlr_scene_output_create(server->scene, wlr_output);
|
||
|
|
wlr_scene_output_layout_add_output(server->scene_layout, lo, scene_output);
|
||
|
|
|
||
|
|
/* Build the Aero home UI sized to this output. */
|
||
|
|
int W = wlr_output->width, H = wlr_output->height;
|
||
|
|
if (W > 0 && H > 0) build_ui_for_output(server, W, H);
|
||
|
|
}
|
||
|
|
|
||
|
|
/* ----------------------------------------------------------------- xdg shell */
|
||
|
|
static void xdg_toplevel_map(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_toplevel *t = wl_container_of(listener, t, map);
|
||
|
|
wl_list_insert(&t->server->toplevels, &t->link);
|
||
|
|
focus_toplevel(t);
|
||
|
|
}
|
||
|
|
static void xdg_toplevel_unmap(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_toplevel *t = wl_container_of(listener, t, unmap);
|
||
|
|
wl_list_remove(&t->link);
|
||
|
|
}
|
||
|
|
static void xdg_toplevel_commit(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_toplevel *t = wl_container_of(listener, t, commit);
|
||
|
|
if (t->xdg_toplevel->base->initial_commit)
|
||
|
|
wlr_xdg_toplevel_set_size(t->xdg_toplevel, 0, 0);
|
||
|
|
}
|
||
|
|
static void xdg_toplevel_destroy(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_toplevel *t = wl_container_of(listener, t, destroy);
|
||
|
|
wl_list_remove(&t->map.link);
|
||
|
|
wl_list_remove(&t->unmap.link);
|
||
|
|
wl_list_remove(&t->commit.link);
|
||
|
|
wl_list_remove(&t->destroy.link);
|
||
|
|
free(t);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void server_new_xdg_toplevel(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_server *server = wl_container_of(listener, server, new_xdg_toplevel);
|
||
|
|
struct wlr_xdg_toplevel *xdg_toplevel = data;
|
||
|
|
|
||
|
|
struct aero_toplevel *t = calloc(1, sizeof(*t));
|
||
|
|
t->server = server;
|
||
|
|
t->xdg_toplevel = xdg_toplevel;
|
||
|
|
/* client windows live in the apps layer (above wallpaper, below glass UI) */
|
||
|
|
t->scene_tree = wlr_scene_xdg_surface_create(server->layer_apps,
|
||
|
|
xdg_toplevel->base);
|
||
|
|
t->scene_tree->node.data = t;
|
||
|
|
xdg_toplevel->base->data = t->scene_tree;
|
||
|
|
|
||
|
|
t->map.notify = xdg_toplevel_map; wl_signal_add(&xdg_toplevel->base->surface->events.map, &t->map);
|
||
|
|
t->unmap.notify = xdg_toplevel_unmap; wl_signal_add(&xdg_toplevel->base->surface->events.unmap, &t->unmap);
|
||
|
|
t->commit.notify = xdg_toplevel_commit; wl_signal_add(&xdg_toplevel->base->surface->events.commit, &t->commit);
|
||
|
|
t->destroy.notify = xdg_toplevel_destroy; wl_signal_add(&xdg_toplevel->events.destroy, &t->destroy);
|
||
|
|
}
|
||
|
|
|
||
|
|
/* ----------------------------------------------------------------- input */
|
||
|
|
static void keyboard_handle_modifiers(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_keyboard *kb = wl_container_of(listener, kb, modifiers);
|
||
|
|
wlr_seat_set_keyboard(kb->server->seat, kb->wlr_keyboard);
|
||
|
|
wlr_seat_keyboard_notify_modifiers(kb->server->seat,
|
||
|
|
&kb->wlr_keyboard->modifiers);
|
||
|
|
}
|
||
|
|
|
||
|
|
static bool handle_keybinding(struct aero_server *server, xkb_keysym_t sym) {
|
||
|
|
switch (sym) {
|
||
|
|
case XKB_KEY_Escape: /* Super+Esc quits compositor */
|
||
|
|
wl_display_terminate(server->wl_display);
|
||
|
|
return true;
|
||
|
|
case XKB_KEY_Return: /* Super+Return: terminal */
|
||
|
|
spawn("foot || alacritty || xterm");
|
||
|
|
return true;
|
||
|
|
case XKB_KEY_d: /* Super+d: launcher */
|
||
|
|
spawn("wofi --show drun || fuzzel || bemenu-run");
|
||
|
|
return true;
|
||
|
|
case XKB_KEY_m: /* Super+m: toggle music */
|
||
|
|
spawn("systemctl --user is-active aero-lofi && systemctl --user stop aero-lofi || systemctl --user start aero-lofi");
|
||
|
|
return true;
|
||
|
|
case XKB_KEY_q: { /* Super+q: close focused window */
|
||
|
|
struct aero_toplevel *t;
|
||
|
|
if (!wl_list_empty(&server->toplevels)) {
|
||
|
|
t = wl_container_of(server->toplevels.next, t, link);
|
||
|
|
wlr_xdg_toplevel_send_close(t->xdg_toplevel);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
default: return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static void keyboard_handle_key(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_keyboard *kb = wl_container_of(listener, kb, key);
|
||
|
|
struct aero_server *server = kb->server;
|
||
|
|
struct wlr_keyboard_key_event *event = data;
|
||
|
|
uint32_t keycode = event->keycode + 8;
|
||
|
|
const xkb_keysym_t *syms;
|
||
|
|
int nsyms = xkb_state_key_get_syms(kb->wlr_keyboard->xkb_state, keycode, &syms);
|
||
|
|
bool handled = false;
|
||
|
|
uint32_t mods = wlr_keyboard_get_modifiers(kb->wlr_keyboard);
|
||
|
|
if ((mods & WLR_MODIFIER_LOGO) && event->state == WL_KEYBOARD_KEY_STATE_PRESSED)
|
||
|
|
for (int i = 0; i < nsyms; i++)
|
||
|
|
handled |= handle_keybinding(server, syms[i]);
|
||
|
|
if (!handled) {
|
||
|
|
wlr_seat_set_keyboard(server->seat, kb->wlr_keyboard);
|
||
|
|
wlr_seat_keyboard_notify_key(server->seat, event->time_msec,
|
||
|
|
event->keycode, event->state);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static void keyboard_handle_destroy(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_keyboard *kb = wl_container_of(listener, kb, destroy);
|
||
|
|
wl_list_remove(&kb->modifiers.link);
|
||
|
|
wl_list_remove(&kb->key.link);
|
||
|
|
wl_list_remove(&kb->destroy.link);
|
||
|
|
wl_list_remove(&kb->link);
|
||
|
|
free(kb);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void server_new_keyboard(struct aero_server *server,
|
||
|
|
struct wlr_input_device *device) {
|
||
|
|
struct wlr_keyboard *wlr_keyboard = wlr_keyboard_from_input_device(device);
|
||
|
|
struct aero_keyboard *kb = calloc(1, sizeof(*kb));
|
||
|
|
kb->server = server; kb->wlr_keyboard = wlr_keyboard;
|
||
|
|
|
||
|
|
struct xkb_context *ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
||
|
|
struct xkb_keymap *keymap = xkb_keymap_new_from_names(ctx, NULL,
|
||
|
|
XKB_KEYMAP_COMPILE_NO_FLAGS);
|
||
|
|
wlr_keyboard_set_keymap(wlr_keyboard, keymap);
|
||
|
|
xkb_keymap_unref(keymap);
|
||
|
|
xkb_context_unref(ctx);
|
||
|
|
wlr_keyboard_set_repeat_info(wlr_keyboard, 25, 600);
|
||
|
|
|
||
|
|
kb->modifiers.notify = keyboard_handle_modifiers;
|
||
|
|
wl_signal_add(&wlr_keyboard->events.modifiers, &kb->modifiers);
|
||
|
|
kb->key.notify = keyboard_handle_key;
|
||
|
|
wl_signal_add(&wlr_keyboard->events.key, &kb->key);
|
||
|
|
kb->destroy.notify = keyboard_handle_destroy;
|
||
|
|
wl_signal_add(&device->events.destroy, &kb->destroy);
|
||
|
|
|
||
|
|
wlr_seat_set_keyboard(server->seat, wlr_keyboard);
|
||
|
|
wl_list_insert(&server->keyboards, &kb->link);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void server_new_input(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_server *server = wl_container_of(listener, server, new_input);
|
||
|
|
struct wlr_input_device *device = data;
|
||
|
|
if (device->type == WLR_INPUT_DEVICE_KEYBOARD)
|
||
|
|
server_new_keyboard(server, device);
|
||
|
|
else if (device->type == WLR_INPUT_DEVICE_POINTER)
|
||
|
|
wlr_cursor_attach_input_device(server->cursor, device);
|
||
|
|
uint32_t caps = WL_SEAT_CAPABILITY_POINTER;
|
||
|
|
if (!wl_list_empty(&server->keyboards)) caps |= WL_SEAT_CAPABILITY_KEYBOARD;
|
||
|
|
wlr_seat_set_capabilities(server->seat, caps);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void cursor_motion_abs(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_server *server = wl_container_of(listener, server, cursor_motion_absolute);
|
||
|
|
struct wlr_pointer_motion_absolute_event *e = data;
|
||
|
|
wlr_cursor_warp_absolute(server->cursor, &e->pointer->base, e->x, e->y);
|
||
|
|
wlr_seat_pointer_notify_frame(server->seat);
|
||
|
|
}
|
||
|
|
static void cursor_motion(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_server *server = wl_container_of(listener, server, cursor_motion);
|
||
|
|
struct wlr_pointer_motion_event *e = data;
|
||
|
|
wlr_cursor_move(server->cursor, &e->pointer->base, e->delta_x, e->delta_y);
|
||
|
|
wlr_seat_pointer_notify_frame(server->seat);
|
||
|
|
}
|
||
|
|
static void cursor_button(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_server *server = wl_container_of(listener, server, cursor_button);
|
||
|
|
struct wlr_pointer_button_event *e = data;
|
||
|
|
wlr_seat_pointer_notify_button(server->seat, e->time_msec, e->button, e->state);
|
||
|
|
}
|
||
|
|
static void cursor_axis(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_server *server = wl_container_of(listener, server, cursor_axis);
|
||
|
|
struct wlr_pointer_axis_event *e = data;
|
||
|
|
wlr_seat_pointer_notify_axis(server->seat, e->time_msec, e->orientation,
|
||
|
|
e->delta, e->delta_discrete, e->source, e->relative_direction);
|
||
|
|
}
|
||
|
|
static void cursor_frame(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_server *server = wl_container_of(listener, server, cursor_frame);
|
||
|
|
wlr_seat_pointer_notify_frame(server->seat);
|
||
|
|
}
|
||
|
|
static void seat_request_cursor(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_server *server = wl_container_of(listener, server, request_cursor);
|
||
|
|
struct wlr_seat_pointer_request_set_cursor_event *e = data;
|
||
|
|
if (e->seat_client == server->seat->pointer_state.focused_client)
|
||
|
|
wlr_cursor_set_surface(server->cursor, e->surface, e->hotspot_x, e->hotspot_y);
|
||
|
|
}
|
||
|
|
static void seat_request_selection(struct wl_listener *listener, void *data) {
|
||
|
|
struct aero_server *server = wl_container_of(listener, server, request_set_selection);
|
||
|
|
struct wlr_seat_request_set_selection_event *e = data;
|
||
|
|
wlr_seat_set_selection(server->seat, e->source, e->serial);
|
||
|
|
}
|
||
|
|
|
||
|
|
/* ----------------------------------------------------------------- main */
|
||
|
|
int main(int argc, char *argv[]) {
|
||
|
|
wlr_log_init(WLR_INFO, NULL);
|
||
|
|
const char *startup_cmd = NULL;
|
||
|
|
int c;
|
||
|
|
while ((c = getopt(argc, argv, "s:h")) != -1) {
|
||
|
|
if (c == 's') startup_cmd = optarg;
|
||
|
|
else { printf("usage: %s [-s startup-command]\n", argv[0]); return 0; }
|
||
|
|
}
|
||
|
|
|
||
|
|
struct aero_server server = {0};
|
||
|
|
server.wl_display = wl_display_create();
|
||
|
|
server.backend = wlr_backend_autocreate(
|
||
|
|
wl_display_get_event_loop(server.wl_display), NULL);
|
||
|
|
server.renderer = wlr_renderer_autocreate(server.backend);
|
||
|
|
wlr_renderer_init_wl_display(server.renderer, server.wl_display);
|
||
|
|
server.allocator = wlr_allocator_autocreate(server.backend, server.renderer);
|
||
|
|
|
||
|
|
wlr_compositor_create(server.wl_display, 5, server.renderer);
|
||
|
|
wlr_subcompositor_create(server.wl_display);
|
||
|
|
wlr_data_device_manager_create(server.wl_display);
|
||
|
|
|
||
|
|
server.output_layout = wlr_output_layout_create(server.wl_display);
|
||
|
|
wl_list_init(&server.outputs);
|
||
|
|
server.new_output.notify = server_new_output;
|
||
|
|
wl_signal_add(&server.backend->events.new_output, &server.new_output);
|
||
|
|
|
||
|
|
server.scene = wlr_scene_create();
|
||
|
|
server.scene_layout = wlr_scene_attach_output_layout(server.scene,
|
||
|
|
server.output_layout);
|
||
|
|
/* three stacked layers: wallpaper < apps < glass UI */
|
||
|
|
server.layer_bg = wlr_scene_tree_create(&server.scene->tree);
|
||
|
|
server.layer_apps = wlr_scene_tree_create(&server.scene->tree);
|
||
|
|
server.layer_ui = wlr_scene_tree_create(&server.scene->tree);
|
||
|
|
|
||
|
|
/* load the wallpaper */
|
||
|
|
const char *wp = getenv("AEROWM_WALLPAPER");
|
||
|
|
if (!wp) wp = "/usr/share/backgrounds/aeros/frutiger-aero.png";
|
||
|
|
if (aero_image_load_png(wp, &server.wallpaper) != 0)
|
||
|
|
wlr_log(WLR_ERROR, "could not load wallpaper %s", wp);
|
||
|
|
|
||
|
|
wl_list_init(&server.toplevels);
|
||
|
|
server.xdg_shell = wlr_xdg_shell_create(server.wl_display, 3);
|
||
|
|
server.new_xdg_toplevel.notify = server_new_xdg_toplevel;
|
||
|
|
wl_signal_add(&server.xdg_shell->events.new_toplevel, &server.new_xdg_toplevel);
|
||
|
|
|
||
|
|
server.cursor = wlr_cursor_create();
|
||
|
|
wlr_cursor_attach_output_layout(server.cursor, server.output_layout);
|
||
|
|
server.cursor_mgr = wlr_xcursor_manager_create(NULL, 24);
|
||
|
|
server.cursor_mode = CURSOR_PASSTHROUGH;
|
||
|
|
server.cursor_motion.notify = cursor_motion;
|
||
|
|
wl_signal_add(&server.cursor->events.motion, &server.cursor_motion);
|
||
|
|
server.cursor_motion_absolute.notify = cursor_motion_abs;
|
||
|
|
wl_signal_add(&server.cursor->events.motion_absolute, &server.cursor_motion_absolute);
|
||
|
|
server.cursor_button.notify = cursor_button;
|
||
|
|
wl_signal_add(&server.cursor->events.button, &server.cursor_button);
|
||
|
|
server.cursor_axis.notify = cursor_axis;
|
||
|
|
wl_signal_add(&server.cursor->events.axis, &server.cursor_axis);
|
||
|
|
server.cursor_frame.notify = cursor_frame;
|
||
|
|
wl_signal_add(&server.cursor->events.frame, &server.cursor_frame);
|
||
|
|
|
||
|
|
wl_list_init(&server.keyboards);
|
||
|
|
server.new_input.notify = server_new_input;
|
||
|
|
wl_signal_add(&server.backend->events.new_input, &server.new_input);
|
||
|
|
server.seat = wlr_seat_create(server.wl_display, "seat0");
|
||
|
|
server.request_cursor.notify = seat_request_cursor;
|
||
|
|
wl_signal_add(&server.seat->events.request_set_cursor, &server.request_cursor);
|
||
|
|
server.request_set_selection.notify = seat_request_selection;
|
||
|
|
wl_signal_add(&server.seat->events.request_set_selection, &server.request_set_selection);
|
||
|
|
|
||
|
|
const char *socket = wl_display_add_socket_auto(server.wl_display);
|
||
|
|
if (!socket) { wlr_backend_destroy(server.backend); return 1; }
|
||
|
|
if (!wlr_backend_start(server.backend)) {
|
||
|
|
wlr_backend_destroy(server.backend);
|
||
|
|
wl_display_destroy(server.wl_display);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
setenv("WAYLAND_DISPLAY", socket, true);
|
||
|
|
wlr_log(WLR_INFO, "AeroOS compositor running on WAYLAND_DISPLAY=%s", socket);
|
||
|
|
|
||
|
|
/* clock updater */
|
||
|
|
struct wl_event_source *timer =
|
||
|
|
wl_event_loop_add_timer(wl_display_get_event_loop(server.wl_display),
|
||
|
|
clock_tick, &server);
|
||
|
|
server.clock_timer = timer;
|
||
|
|
wl_event_source_timer_update(timer, 1000);
|
||
|
|
|
||
|
|
/* autostart: lofi + optional session command */
|
||
|
|
spawn("systemctl --user start aero-lofi.service 2>/dev/null || "
|
||
|
|
"aero-lofi &");
|
||
|
|
if (startup_cmd) spawn(startup_cmd);
|
||
|
|
|
||
|
|
wl_display_run(server.wl_display);
|
||
|
|
|
||
|
|
wl_display_destroy_clients(server.wl_display);
|
||
|
|
wlr_scene_node_destroy(&server.scene->tree.node);
|
||
|
|
wlr_allocator_destroy(server.allocator);
|
||
|
|
wlr_renderer_destroy(server.renderer);
|
||
|
|
wlr_backend_destroy(server.backend);
|
||
|
|
wl_display_destroy(server.wl_display);
|
||
|
|
aero_image_free(&server.wallpaper);
|
||
|
|
aero_image_free(&server.blurred_full);
|
||
|
|
return 0;
|
||
|
|
}
|