Проект разнесён по файлам
This commit is contained in:
342
wayland/c.c
342
wayland/c.c
@@ -6,314 +6,28 @@
|
||||
#include <stdio.h>
|
||||
#include <xkbcommon/xkbcommon.h>
|
||||
#include "xdg-shell-client-protocol.h"
|
||||
#include "input.h"
|
||||
#include "window.h"
|
||||
#include "registry.h"
|
||||
|
||||
static struct wl_compositor *compositor;
|
||||
static struct xdg_wm_base *xdg_wm_base;
|
||||
static struct wl_seat *seat;
|
||||
static struct wl_keyboard *keyboard;
|
||||
// core is now split into modules: registry, input and window
|
||||
|
||||
/* xkbcommon helpers */
|
||||
static struct xkb_context *xkb_ctx = NULL;
|
||||
static struct xkb_keymap *xkb_keymap = NULL;
|
||||
static struct xkb_state *xkb_state = NULL;
|
||||
// resize, draw and frame callbacks moved to `window.c`
|
||||
|
||||
// Всё ниже - для одного окна
|
||||
static struct xdg_surface *xdg_surface;
|
||||
static struct xdg_toplevel *xdg_toplevel;
|
||||
static struct wl_surface *wl_surface;
|
||||
static struct wl_buffer *buffer;
|
||||
static struct wl_shm *shm;
|
||||
static int16_t width = 200;
|
||||
static int16_t height = 100;
|
||||
static int8_t *data;
|
||||
static int8_t need_close = 0;
|
||||
/* Input, window and registry logic has been moved into separate modules.
|
||||
* This file now wires them together and runs the main loop. */
|
||||
|
||||
int32_t alloc_shm(size_t size)
|
||||
{
|
||||
char name[32];
|
||||
sprintf(name, "/wayland-shm-%d", getpid());
|
||||
/* Window events moved to `window.c` */
|
||||
|
||||
int32_t fd = shm_open(name, O_RDWR | O_CREAT, 0600);
|
||||
if (fd == -1)
|
||||
return fd;
|
||||
shm_unlink(name);
|
||||
ftruncate(fd, size);
|
||||
return fd;
|
||||
}
|
||||
/* frame callback now in `window.c` */
|
||||
|
||||
void resize_canvas()
|
||||
{
|
||||
size_t stride = width * 4;
|
||||
size_t size = stride * height;
|
||||
int32_t fd = alloc_shm(size);
|
||||
|
||||
data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
|
||||
buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888);
|
||||
wl_shm_pool_destroy(pool);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
void resize_new(int16_t w, int16_t h)
|
||||
{
|
||||
if (!w || !h)
|
||||
return;
|
||||
if (width != w || height != h)
|
||||
{
|
||||
if(data)
|
||||
munmap(data, width*height*4);
|
||||
width = w;
|
||||
height = h;
|
||||
resize_canvas();
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------- Keyboard handling ----------------- */
|
||||
|
||||
void keyboard_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size)
|
||||
{
|
||||
printf("keyboard: keymap format=%u size=%u\n", format, size);
|
||||
if (fd < 0)
|
||||
return;
|
||||
|
||||
/* Only support xkb v1 */
|
||||
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1)
|
||||
{
|
||||
/* nothing we can do */
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Map keymap to memory */
|
||||
char *map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (map == MAP_FAILED)
|
||||
{
|
||||
perror("mmap");
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!xkb_ctx)
|
||||
xkb_ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
||||
|
||||
/* Free previous keymap/state */
|
||||
if (xkb_state)
|
||||
{
|
||||
xkb_state_unref(xkb_state);
|
||||
xkb_state = NULL;
|
||||
}
|
||||
if (xkb_keymap)
|
||||
{
|
||||
xkb_keymap_unref(xkb_keymap);
|
||||
xkb_keymap = NULL;
|
||||
}
|
||||
|
||||
xkb_keymap = xkb_keymap_new_from_string(xkb_ctx, map, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
|
||||
if (!xkb_keymap)
|
||||
{
|
||||
fprintf(stderr, "Unable to compile xkb keymap\n");
|
||||
munmap(map, size);
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
xkb_state = xkb_state_new(xkb_keymap);
|
||||
|
||||
munmap(map, size);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
void keyboard_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys)
|
||||
{
|
||||
printf("keyboard: enter serial=%u surface=%p\n", serial, surface);
|
||||
}
|
||||
|
||||
void keyboard_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface)
|
||||
{
|
||||
printf("keyboard: leave serial=%u surface=%p\n", serial, surface);
|
||||
}
|
||||
|
||||
void keyboard_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
|
||||
{
|
||||
const char *s = (state == WL_KEYBOARD_KEY_STATE_PRESSED) ? "PRESSED" : "RELEASED";
|
||||
printf("keyboard: key serial=%u time=%u key=%u state=%s\n", serial, time, key, s);
|
||||
|
||||
/* Convert keycode to keysym using xkb */
|
||||
if (xkb_state)
|
||||
{
|
||||
/* xkb uses keycodes offset by 8 */
|
||||
xkb_keycode_t kc = (xkb_keycode_t)key + 8;
|
||||
xkb_keysym_t ks = xkb_state_key_get_one_sym(xkb_state, kc);
|
||||
if (ks != XKB_KEY_NoSymbol)
|
||||
{
|
||||
char buf[64];
|
||||
int n = xkb_keysym_to_utf8(ks, buf, sizeof(buf));
|
||||
if (n > 0)
|
||||
printf("keyboard: symbol '%s' (keysym 0x%x)\n", buf, ks);
|
||||
else
|
||||
printf("keyboard: keysym 0x%x (no UTF-8 representation)\n", ks);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void keyboard_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group)
|
||||
{
|
||||
printf("keyboard: modifiers serial=%u depressed=%u latched=%u locked=%u group=%u\n", serial, mods_depressed, mods_latched, mods_locked, group);
|
||||
if (xkb_state)
|
||||
{
|
||||
/* newer libxkbcommon uses three layout args; use group for depressed */
|
||||
xkb_state_update_mask(xkb_state, mods_depressed, mods_latched, mods_locked, group, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void keyboard_repeat_info(void *data, struct wl_keyboard *keyboard, int32_t rate, int32_t delay)
|
||||
{
|
||||
printf("keyboard: repeat rate=%d delay=%d\n", rate, delay);
|
||||
}
|
||||
|
||||
struct wl_keyboard_listener keyboard_listener = {
|
||||
.keymap = keyboard_keymap,
|
||||
.enter = keyboard_enter,
|
||||
.leave = keyboard_leave,
|
||||
.key = keyboard_key,
|
||||
.modifiers = keyboard_modifiers,
|
||||
.repeat_info = keyboard_repeat_info
|
||||
};
|
||||
|
||||
void seat_capabilities(void *data, struct wl_seat *seat, uint32_t caps)
|
||||
{
|
||||
if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !keyboard)
|
||||
{
|
||||
keyboard = wl_seat_get_keyboard(seat);
|
||||
wl_keyboard_add_listener(keyboard, &keyboard_listener, 0);
|
||||
printf("Seat reports keyboard capability - keyboard bound\n");
|
||||
}
|
||||
else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && keyboard)
|
||||
{
|
||||
wl_keyboard_destroy(keyboard);
|
||||
keyboard = NULL;
|
||||
if (xkb_state)
|
||||
{
|
||||
xkb_state_unref(xkb_state);
|
||||
xkb_state = NULL;
|
||||
}
|
||||
if (xkb_keymap)
|
||||
{
|
||||
xkb_keymap_unref(xkb_keymap);
|
||||
xkb_keymap = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void seat_name(void *data, struct wl_seat *seat, const char *name)
|
||||
{
|
||||
printf("Seat name: %s\n", name);
|
||||
}
|
||||
|
||||
struct wl_seat_listener seat_listener = {
|
||||
.capabilities = seat_capabilities,
|
||||
.name = seat_name
|
||||
};
|
||||
|
||||
int8_t c;
|
||||
void draw()
|
||||
{
|
||||
size_t stride = width * 4;
|
||||
size_t size = stride * height;
|
||||
|
||||
memset(data, c++, size);
|
||||
|
||||
wl_surface_attach(wl_surface, buffer, 0, 0);
|
||||
wl_surface_damage_buffer(wl_surface, 0, 0, width, height);
|
||||
wl_surface_commit(wl_surface);
|
||||
}
|
||||
|
||||
void xdg_surface_conf(void *data, struct xdg_surface *xdg_surface, uint32_t serial)
|
||||
{
|
||||
xdg_surface_ack_configure(xdg_surface, serial);
|
||||
if (!data)
|
||||
resize_canvas();
|
||||
|
||||
draw();
|
||||
}
|
||||
|
||||
void xdg_toplevel_conf(void *data, struct xdg_toplevel *xdg_top, int32_t w, int32_t h, struct wl_array *states)
|
||||
{
|
||||
resize_new(w, h);
|
||||
}
|
||||
|
||||
void xdg_toplevel_cls(void *data, struct xdg_toplevel *xdg_top)
|
||||
{
|
||||
need_close = 1;
|
||||
}
|
||||
|
||||
void xdg_toplevel_bounds(void *data, struct xdg_toplevel *xdg_top, int32_t w, int32_t h)
|
||||
{
|
||||
}
|
||||
|
||||
void xdg_toplevel_wm_caps(void *data, struct xdg_toplevel *xdg_top, struct wl_array *caps)
|
||||
{
|
||||
}
|
||||
|
||||
struct wl_callback_listener callback_listener;
|
||||
void frame_new(void *date, struct wl_callback *cb, uint32_t _)
|
||||
{
|
||||
wl_callback_destroy(cb);
|
||||
cb = wl_surface_frame(wl_surface);
|
||||
wl_callback_add_listener(cb, &callback_listener, 0);
|
||||
draw();
|
||||
}
|
||||
|
||||
void wm_base_ping(void *data, struct xdg_wm_base *wm_base, uint32_t serial)
|
||||
{
|
||||
xdg_wm_base_pong(wm_base, serial);
|
||||
}
|
||||
|
||||
struct xdg_wm_base_listener wm_base_listener;
|
||||
/* ping/pong moved to `window.c` */
|
||||
// Функция для прослушивания глобальных объектов
|
||||
void registry_global(void *data, struct wl_registry *reg, uint32_t name, const char *intf, uint32_t version)
|
||||
{
|
||||
printf("%u\t%s\tv:%u\n", name, intf, version);
|
||||
if (!strcmp(intf, wl_compositor_interface.name))
|
||||
{
|
||||
compositor = wl_registry_bind(reg, name, &wl_compositor_interface, version);
|
||||
}
|
||||
else if (!strcmp(intf, wl_shm_interface.name))
|
||||
{
|
||||
shm = wl_registry_bind(reg, name, &wl_shm_interface, version);
|
||||
}
|
||||
else if (!strcmp(intf, xdg_wm_base_interface.name))
|
||||
{
|
||||
xdg_wm_base = wl_registry_bind(reg, name, &xdg_wm_base_interface, version);
|
||||
xdg_wm_base_add_listener(xdg_wm_base, &wm_base_listener, 0);
|
||||
}
|
||||
else if (!strcmp(intf, wl_seat_interface.name))
|
||||
{
|
||||
seat = wl_registry_bind(reg, name, &wl_seat_interface, version);
|
||||
wl_seat_add_listener(seat, &seat_listener, 0);
|
||||
}
|
||||
}
|
||||
/* registry listener moved to `registry.c` */
|
||||
|
||||
void registry_global_remove(void *data, struct wl_registry *reg, uint32_t name) {}
|
||||
// No registry functions here; they are implemented in `registry.c`.
|
||||
|
||||
struct wl_callback_listener callback_listener = {
|
||||
.done = frame_new};
|
||||
|
||||
struct xdg_wm_base_listener wm_base_listener = {
|
||||
.ping = wm_base_ping};
|
||||
|
||||
struct xdg_toplevel_listener top_listener = {
|
||||
.configure = xdg_toplevel_conf,
|
||||
.close = xdg_toplevel_cls,
|
||||
.configure_bounds = xdg_toplevel_bounds,
|
||||
.wm_capabilities = xdg_toplevel_wm_caps};
|
||||
|
||||
struct xdg_surface_listener surface_listener = {
|
||||
.configure = xdg_surface_conf};
|
||||
|
||||
struct wl_registry_listener registry_listener = {
|
||||
.global = registry_global,
|
||||
.global_remove = registry_global_remove};
|
||||
/* listeners implemented in modules */
|
||||
|
||||
int32_t run_wayland()
|
||||
{
|
||||
@@ -321,38 +35,22 @@ int32_t run_wayland()
|
||||
if (!display)
|
||||
return -1;
|
||||
struct wl_registry *registry = wl_display_get_registry(display);
|
||||
wl_registry_add_listener(registry, ®istry_listener, 0);
|
||||
registry_add_listener(registry);
|
||||
wl_display_roundtrip(display);
|
||||
|
||||
wl_surface = wl_compositor_create_surface(compositor);
|
||||
struct wl_callback *wl_callback = wl_surface_frame(wl_surface);
|
||||
wl_callback_add_listener(wl_callback, &callback_listener, 0);
|
||||
|
||||
xdg_surface = xdg_wm_base_get_xdg_surface(xdg_wm_base, wl_surface);
|
||||
xdg_surface_add_listener(xdg_surface, &surface_listener, 0);
|
||||
xdg_toplevel = xdg_surface_get_toplevel(xdg_surface);
|
||||
xdg_toplevel_add_listener(xdg_toplevel, &top_listener, 0);
|
||||
xdg_toplevel_set_title(xdg_toplevel, "Custom client");
|
||||
wl_surface_commit(wl_surface);
|
||||
struct wayland_window win;
|
||||
if (window_init(display, &win) < 0)
|
||||
return -1;
|
||||
|
||||
while (wl_display_dispatch(display))
|
||||
{
|
||||
if (need_close)
|
||||
if (window_should_close(&win))
|
||||
{
|
||||
printf("Window closing\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (buffer)
|
||||
wl_buffer_destroy(buffer);
|
||||
xdg_toplevel_destroy(xdg_toplevel);
|
||||
xdg_surface_destroy(xdg_surface);
|
||||
wl_surface_destroy(wl_surface);
|
||||
if (keyboard)
|
||||
wl_keyboard_destroy(keyboard);
|
||||
if (seat)
|
||||
wl_seat_destroy(seat);
|
||||
window_destroy(&win);
|
||||
input_cleanup();
|
||||
wl_display_disconnect(display);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user