2021-06-06 01:26:18 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2024-02-01 06:16:31 +00:00
|
|
|
* Copyright © 2017-2024 The Looking Glass Authors
|
2021-06-06 01:26:18 +00:00
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* 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 2 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, write to the Free Software Foundation, Inc., 59
|
|
|
|
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2021-02-20 06:23:48 +00:00
|
|
|
|
|
|
|
#include "wayland.h"
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <wayland-client.h>
|
|
|
|
|
|
|
|
#include "app.h"
|
|
|
|
#include "common/debug.h"
|
2021-07-31 08:01:51 +00:00
|
|
|
#include "common/event.h"
|
2021-02-20 06:23:48 +00:00
|
|
|
|
|
|
|
// Surface-handling listeners.
|
|
|
|
|
2021-02-22 07:19:03 +00:00
|
|
|
void waylandWindowUpdateScale(void)
|
2021-02-21 05:10:08 +00:00
|
|
|
{
|
2021-08-01 01:56:47 +00:00
|
|
|
wl_fixed_t maxScale = 0;
|
2021-02-21 05:10:08 +00:00
|
|
|
struct SurfaceOutput * node;
|
|
|
|
|
|
|
|
wl_list_for_each(node, &wlWm.surfaceOutputs, link)
|
|
|
|
{
|
2021-08-01 01:56:47 +00:00
|
|
|
wl_fixed_t scale = waylandOutputGetScale(node->output);
|
2021-02-21 05:10:08 +00:00
|
|
|
if (scale > maxScale)
|
|
|
|
maxScale = scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxScale)
|
|
|
|
{
|
|
|
|
wlWm.scale = maxScale;
|
2021-08-01 01:56:47 +00:00
|
|
|
wlWm.fractionalScale = wl_fixed_from_int(wl_fixed_to_int(maxScale)) != maxScale;
|
2021-02-21 05:10:08 +00:00
|
|
|
wlWm.needsResize = true;
|
2021-08-14 23:32:33 +00:00
|
|
|
waylandCursorScaleChange();
|
2021-08-04 20:47:36 +00:00
|
|
|
app_invalidateWindow(true);
|
2021-08-01 21:51:44 +00:00
|
|
|
waylandStopWaitFrame();
|
2021-02-21 05:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wlSurfaceEnterHandler(void * data, struct wl_surface * surface, struct wl_output * output)
|
|
|
|
{
|
2021-08-15 16:18:22 +00:00
|
|
|
struct SurfaceOutput * node = malloc(sizeof(*node));
|
2022-03-06 23:13:54 +00:00
|
|
|
if (!node)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("out of memory");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-21 05:10:08 +00:00
|
|
|
node->output = output;
|
|
|
|
wl_list_insert(&wlWm.surfaceOutputs, &node->link);
|
2021-02-22 07:19:03 +00:00
|
|
|
waylandWindowUpdateScale();
|
2021-02-21 05:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wlSurfaceLeaveHandler(void * data, struct wl_surface * surface, struct wl_output * output)
|
|
|
|
{
|
|
|
|
struct SurfaceOutput * node;
|
|
|
|
wl_list_for_each(node, &wlWm.surfaceOutputs, link)
|
|
|
|
if (node->output == output)
|
|
|
|
{
|
|
|
|
wl_list_remove(&node->link);
|
|
|
|
break;
|
|
|
|
}
|
2021-02-22 07:19:03 +00:00
|
|
|
waylandWindowUpdateScale();
|
2021-02-21 05:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_surface_listener wlSurfaceListener = {
|
|
|
|
.enter = wlSurfaceEnterHandler,
|
|
|
|
.leave = wlSurfaceLeaveHandler,
|
|
|
|
};
|
|
|
|
|
2021-10-04 17:55:18 +00:00
|
|
|
bool waylandWindowInit(const char * title, bool fullscreen, bool maximize, bool borderless, bool resizable)
|
2021-02-20 06:23:48 +00:00
|
|
|
{
|
2021-08-01 01:56:47 +00:00
|
|
|
wlWm.scale = wl_fixed_from_int(1);
|
2021-02-21 05:10:08 +00:00
|
|
|
|
2021-08-01 07:06:03 +00:00
|
|
|
wlWm.frameEvent = lgCreateEvent(true, 0);
|
|
|
|
if (!wlWm.frameEvent)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize event for waitFrame");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
lgSignalEvent(wlWm.frameEvent);
|
|
|
|
|
2021-02-20 06:23:48 +00:00
|
|
|
if (!wlWm.compositor)
|
|
|
|
{
|
2021-02-27 02:58:30 +00:00
|
|
|
DEBUG_ERROR("Compositor missing wl_compositor (version 3+), will not proceed");
|
2021-02-20 06:23:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlWm.surface = wl_compositor_create_surface(wlWm.compositor);
|
|
|
|
if (!wlWm.surface)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to create wl_surface");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-21 05:10:08 +00:00
|
|
|
wl_surface_add_listener(wlWm.surface, &wlSurfaceListener, NULL);
|
|
|
|
|
2023-11-09 05:30:27 +00:00
|
|
|
if (!wlWm.desktop->shellInit(wlWm.display, wlWm.surface,
|
|
|
|
title, fullscreen, maximize, borderless, resizable))
|
2021-03-06 10:20:29 +00:00
|
|
|
return false;
|
2021-02-20 06:23:48 +00:00
|
|
|
|
|
|
|
wl_surface_commit(wlWm.surface);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void waylandWindowFree(void)
|
|
|
|
{
|
|
|
|
wl_surface_destroy(wlWm.surface);
|
2021-08-01 07:06:03 +00:00
|
|
|
lgFreeEvent(wlWm.frameEvent);
|
2021-02-20 06:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void waylandSetWindowSize(int x, int y)
|
|
|
|
{
|
2023-11-09 05:30:27 +00:00
|
|
|
wlWm.desktop->shellResize(x, y);
|
2021-02-20 06:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool waylandIsValidPointerPos(int x, int y)
|
|
|
|
{
|
2023-11-09 05:30:27 +00:00
|
|
|
int width, height;
|
|
|
|
wlWm.desktop->getSize(&width, &height);
|
|
|
|
return x >= 0 && x < width && y >= 0 && y < height;
|
2021-02-20 06:23:48 +00:00
|
|
|
}
|
2021-07-31 08:01:51 +00:00
|
|
|
|
|
|
|
static void frameHandler(void * opaque, struct wl_callback * callback, unsigned int data)
|
|
|
|
{
|
2021-08-01 07:06:03 +00:00
|
|
|
lgSignalEvent(wlWm.frameEvent);
|
2021-07-31 08:01:51 +00:00
|
|
|
wl_callback_destroy(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_callback_listener frame_listener = {
|
|
|
|
.done = frameHandler,
|
|
|
|
};
|
|
|
|
|
2021-08-03 20:49:35 +00:00
|
|
|
bool waylandWaitFrame(void)
|
2021-07-31 08:01:51 +00:00
|
|
|
{
|
2021-08-01 07:06:03 +00:00
|
|
|
lgWaitEvent(wlWm.frameEvent, TIMEOUT_INFINITE);
|
|
|
|
|
2021-07-31 08:01:51 +00:00
|
|
|
struct wl_callback * callback = wl_surface_frame(wlWm.surface);
|
|
|
|
if (callback)
|
2021-08-01 07:06:03 +00:00
|
|
|
wl_callback_add_listener(callback, &frame_listener, NULL);
|
2021-08-03 20:49:35 +00:00
|
|
|
|
|
|
|
return false;
|
2021-07-31 08:01:51 +00:00
|
|
|
}
|
2021-08-01 08:06:35 +00:00
|
|
|
|
|
|
|
void waylandSkipFrame(void)
|
|
|
|
{
|
|
|
|
// If we decided to not render, we must commit the surface so that the callback is registered.
|
|
|
|
wl_surface_commit(wlWm.surface);
|
|
|
|
}
|
|
|
|
|
2021-08-01 09:46:07 +00:00
|
|
|
void waylandStopWaitFrame(void)
|
2021-08-01 08:06:35 +00:00
|
|
|
{
|
|
|
|
lgSignalEvent(wlWm.frameEvent);
|
|
|
|
}
|