/**
* MIT/X11 License
* Modified (c) 2017 Quentin “Sardem FF7” Glidic
*
* 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.
*/
#define G_LOG_DOMAIN "Wayland"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <locale.h>
#include <linux/input-event-codes.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <cairo.h>
#include <wayland-client.h>
#include <wayland-cursor.h>
#include <libgwater-wayland.h>
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-compose.h>
#include "xkb.h"
#include "view.h"
#include "unstable/launcher-menu/launcher-menu-unstable-v1-client-protocol.h"
#include "wayland.h"
#include "display.h"
typedef struct _display_buffer_pool wayland_buffer_pool;
typedef struct {
wayland_stuff *context;
uint32_t global_name;
struct wl_output *output;
int32_t scale;
} wayland_output;
typedef struct {
wayland_buffer_pool *pool;
struct wl_buffer *buffer;
uint8_t *data;
gboolean released;
} wayland_buffer;
struct _display_buffer_pool {
wayland_stuff *context;
uint8_t *data;
size_t size;
int32_t width;
int32_t height;
gboolean to_free;
wayland_buffer *buffers;
};
static wayland_stuff wayland_;
wayland_stuff *wayland = &wayland_;
static const cairo_user_data_key_t wayland_cairo_surface_user_data;
static void
wayland_buffer_cleanup(wayland_buffer_pool *self)
{
if ( ! self->to_free )
return;
size_t i, count = 0;
for ( i = 0 ; i < wayland->buffer_count ; ++i )
{
if ( ( self->buffers[i].released ) && ( self->buffers[i].buffer != NULL ) )
{
wl_buffer_destroy(self->buffers[i].buffer);
self->buffers[i].buffer = NULL;
}
if ( self->buffers[i].buffer == NULL )
++count;
}
if ( count < wayland->buffer_count )
return;
munmap(self->data, self->size);
g_free(self);
}
static void
wayland_buffer_release(void *data, struct wl_buffer *buffer)
{
wayland_buffer_pool *self = data;
size_t i;
for ( i = 0 ; i < wayland->buffer_count ; ++i )
{
if ( self->buffers[i].buffer == buffer )
self->buffers[i].released = TRUE;
}
wayland_buffer_cleanup(self);
}
static const struct wl_buffer_listener wayland_buffer_listener = {
wayland_buffer_release
};
wayland_buffer_pool *
display_buffer_pool_new(gint width, gint height)
{
struct wl_shm_pool *wl_pool;
struct wl_buffer *buffer;
int fd;
uint8_t *data;
width *= wayland->scale;
height *= wayland->scale;
int32_t stride;
size_t size;
size_t pool_size;
stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
size = stride * height;
pool_size = size * wayland->buffer_count;
gchar filename[PATH_MAX];
g_snprintf(filename, PATH_MAX,