2021-06-06 01:26:18 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2021-08-04 09:48:32 +00:00
|
|
|
* Copyright © 2017-2021 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-01 21:51:44 +00:00
|
|
|
app_invalidateWindow();
|
|
|
|
waylandStopWaitFrame();
|
2021-02-21 05:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wlSurfaceEnterHandler(void * data, struct wl_surface * surface, struct wl_output * output)
|
|
|
|
{
|
|
|
|
struct SurfaceOutput * node = malloc(sizeof(struct SurfaceOutput));
|
|
|
|
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-02-20 06:23:48 +00:00
|
|
|
bool waylandWindowInit(const char * title, bool fullscreen, bool maximize, bool borderless)
|
|
|
|
{
|
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);
|
|
|
|
|
2021-03-06 10:20:29 +00:00
|
|
|
if (!waylandShellInit(title, fullscreen, maximize, borderless))
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
// FIXME: implement.
|
|
|
|
}
|
|
|
|
|
|
|
|
bool waylandIsValidPointerPos(int x, int y)
|
|
|
|
{
|
|
|
|
return x >= 0 && x < wlWm.width && y >= 0 && y < wlWm.height;
|
|
|
|
}
|
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);
|
|
|
|
}
|