wl-overlay/wayland.c

275 lines
9.4 KiB
C

/*
* This file is part of wl-overlay: show overlays with images and text in Wayland
* Copyright © 2022 Midgard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ---
*
* The code in this file is based on example code from the Wayland Book (https://wayland-book.com/).
* This code was licensed to Midgard under the MIT license in an email.
*
* Copyright (c) 2021 Drew DeVault
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>
#include <cairo/cairo.h>
#include <pango/pangocairo.h>
#include <poll.h>
#include "wlr-layer-shell-protocol.h"
#include "common.h"
#include "wayland.h"
#include "shm.h"
#include "draw.h"
/* Wayland code */
static void
wl_buffer_release(void *data, struct wl_buffer *wl_buffer)
{
/* Sent by the compositor when it's no longer using this buffer */
wl_buffer_destroy(wl_buffer);
}
static const struct wl_buffer_listener wl_buffer_listener = {
.release = wl_buffer_release,
};
static struct wl_buffer *
create_and_draw_buffer(struct client_state *state, const int width, const int height)
{
int stride = width * 4;
int size = stride * height;
int fd = allocate_shm_file(size);
if (fd == -1) {
return NULL;
}
uint32_t *data = mmap(NULL, size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
close(fd);
return NULL;
}
struct wl_shm_pool *pool = wl_shm_create_pool(state->wl_state.wl_shm, fd, size);
struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0,
width, height, stride, WL_SHM_FORMAT_ARGB8888);
wl_shm_pool_destroy(pool);
close(fd);
draw_frame_into(data, &state->user_request, width, height);
munmap(data, size);
wl_buffer_add_listener(buffer, &wl_buffer_listener, NULL);
return buffer;
}
static void
zwlr_layer_surface_configure(void *data,
struct zwlr_layer_surface_v1 *zwlr_layer_surface_v1,
uint32_t serial, uint32_t width, uint32_t height)
{
struct client_state *state = data;
zwlr_layer_surface_v1_ack_configure(zwlr_layer_surface_v1, serial);
struct wl_buffer *buffer = create_and_draw_buffer(state, width, height);
wl_surface_attach(state->wl_state.wl_surface, buffer, 0, 0);
wl_surface_commit(state->wl_state.wl_surface);
}
static const struct zwlr_layer_surface_v1_listener zwlr_layer_surface_listener = {
.configure = zwlr_layer_surface_configure,
};
static void
wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
uint32_t serial, uint32_t time, uint32_t button, uint32_t button_state)
{
struct client_state *state = data;
if (state->wl_state.wl_surface != NULL) {
wl_surface_destroy(state->wl_state.wl_surface);
state->wl_state.wl_surface = NULL;
}
}
void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
uint32_t serial, struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y) {}
void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer,
uint32_t serial, struct wl_surface *surface) {}
void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {}
void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
uint32_t time, uint32_t axis, wl_fixed_t value) {}
void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) {}
void wl_pointer_axis_source(void *data, struct wl_pointer *wl_pointer,
uint32_t axis_source) {}
void wl_pointer_axis_stop(void *data, struct wl_pointer *wl_pointer,
uint32_t time, uint32_t axis) {}
void wl_pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer,
uint32_t axis, int32_t discrete) {}
void wl_pointer_axis_value120(void *data, struct wl_pointer *wl_pointer,
uint32_t axis, int32_t value120) {}
static const struct wl_pointer_listener wl_pointer_listener = {
.button = wl_pointer_button,
.enter = wl_pointer_enter,
.leave = wl_pointer_leave,
.motion = wl_pointer_motion,
.axis = wl_pointer_axis,
.frame = wl_pointer_frame,
.axis_source = wl_pointer_axis_source,
.axis_stop = wl_pointer_axis_stop,
.axis_discrete = wl_pointer_axis_discrete,
.axis_value120 = wl_pointer_axis_value120,
};
static void
registry_global(void *data, struct wl_registry *wl_registry,
uint32_t name, const char *interface, uint32_t version)
{
struct client_state *state = data;
if (strcmp(interface, wl_shm_interface.name) == STR_EQUAL) {
state->wl_state.wl_shm = wl_registry_bind(
wl_registry, name, &wl_shm_interface, 1);
} else if (strcmp(interface, wl_compositor_interface.name) == STR_EQUAL) {
state->wl_state.wl_compositor = wl_registry_bind(
wl_registry, name, &wl_compositor_interface, 4);
} else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == STR_EQUAL) {
state->wl_state.layer_shell = wl_registry_bind(
wl_registry, name, &zwlr_layer_shell_v1_interface, 1);
} else if (strcmp(interface, wl_seat_interface.name) == STR_EQUAL) {
state->wl_state.wl_seat = wl_registry_bind(
wl_registry, name, &wl_seat_interface, 7);
}
}
static void
registry_global_remove(void *data,
struct wl_registry *wl_registry, uint32_t name)
{
/* This space deliberately left blank */
}
static const struct wl_registry_listener wl_registry_listener = {
.global = registry_global,
.global_remove = registry_global_remove,
};
#define NSEC_PER_MSEC (1000 * 1000)
long int now_milliseconds() {
struct timespec now = {0, 0};
clock_gettime(CLOCK_MONOTONIC, &now);
return (now.tv_sec * 1000) + (now.tv_nsec / NSEC_PER_MSEC);
}
void wlo_wl_main(struct client_state *state) {
state->wl_state.wl_display = wl_display_connect(NULL);
if (!state->wl_state.wl_display) {
fprintf(stderr, "Could not connect to Wayland server\n");
exit(1);
}
state->wl_state.wl_registry = wl_display_get_registry(state->wl_state.wl_display);
wl_registry_add_listener(state->wl_state.wl_registry, &wl_registry_listener, state);
wl_display_roundtrip(state->wl_state.wl_display);
if (state->wl_state.layer_shell == NULL) {
fprintf(stderr, "Wayland compositor does not support wlr-layer-shell protocol\n");
exit(1);
}
state->wl_state.wl_surface = wl_compositor_create_surface(state->wl_state.wl_compositor);
state->wl_state.zwlr_surface = zwlr_layer_shell_v1_get_layer_surface(
state->wl_state.layer_shell, state->wl_state.wl_surface, NULL, ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY, "wl-overlay");
zwlr_layer_surface_v1_set_size(state->wl_state.zwlr_surface, state->user_request.width, state->user_request.height);
zwlr_layer_surface_v1_set_anchor(state->wl_state.zwlr_surface, ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM);
zwlr_layer_surface_v1_set_margin(state->wl_state.zwlr_surface, 50, 50, 50, 50);
zwlr_layer_surface_v1_add_listener(state->wl_state.zwlr_surface, &zwlr_layer_surface_listener, state);
struct wl_pointer *pointer = wl_seat_get_pointer(state->wl_state.wl_seat);
wl_pointer_add_listener(pointer, &wl_pointer_listener, state);
wl_surface_commit(state->wl_state.wl_surface);
if (state->user_request.time >= 0) {
struct pollfd pollfd = {
.fd = wl_display_get_fd(state->wl_state.wl_display),
.events = POLLIN,
.revents = 0
};
long int now = now_milliseconds();
long int end = now + state->user_request.time;
while (now < end && state->wl_state.wl_surface != NULL) {
wl_display_flush(state->wl_state.wl_display);
int changed_fds = poll(&pollfd, 1, (int)(end - now));
if (changed_fds == -1 || (pollfd.revents & (POLLERR | POLLHUP))) {
break;
}
if (pollfd.revents & POLLIN) {
if (wl_display_dispatch(state->wl_state.wl_display) == -1) {
state->wl_state.wl_surface = NULL;
}
}
now = now_milliseconds();
}
} else {
while (wl_display_dispatch(state->wl_state.wl_display) != -1 && state->wl_state.wl_surface != NULL) {
/* This space deliberately left blank */
}
}
}