2015-11-25 23:10:21 +11:00
|
|
|
#include <stdlib.h>
|
2015-11-19 14:01:22 +11:00
|
|
|
#include <wlc/wlc.h>
|
|
|
|
#include <wlc/wlc-wayland.h>
|
2016-03-17 22:05:54 +11:00
|
|
|
#include <wlc/wlc-render.h>
|
2015-11-19 14:01:22 +11:00
|
|
|
#include "wayland-desktop-shell-server-protocol.h"
|
2015-12-19 00:49:04 +11:00
|
|
|
#include "wayland-swaylock-server-protocol.h"
|
2016-09-01 22:18:37 +10:00
|
|
|
#include "sway/layout.h"
|
|
|
|
#include "sway/input_state.h"
|
|
|
|
#include "sway/extensions.h"
|
2016-12-02 13:51:07 +11:00
|
|
|
#include "sway/security.h"
|
2016-09-01 22:18:37 +10:00
|
|
|
#include "sway/ipc-server.h"
|
2015-11-19 14:01:22 +11:00
|
|
|
#include "log.h"
|
2015-11-19 14:14:57 +11:00
|
|
|
|
|
|
|
struct desktop_shell_state desktop_shell;
|
2015-11-19 14:01:22 +11:00
|
|
|
|
2015-12-20 22:49:11 +11:00
|
|
|
static struct panel_config *find_or_create_panel_config(struct wl_resource *resource) {
|
|
|
|
for (int i = 0; i < desktop_shell.panels->length; i++) {
|
|
|
|
struct panel_config *conf = desktop_shell.panels->items[i];
|
|
|
|
if (conf->wl_resource == resource) {
|
|
|
|
sway_log(L_DEBUG, "Found existing panel config for resource %p", resource);
|
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sway_log(L_DEBUG, "Creating panel config for resource %p", resource);
|
|
|
|
struct panel_config *config = calloc(1, sizeof(struct panel_config));
|
2016-12-16 10:26:53 +11:00
|
|
|
if (!config) {
|
|
|
|
sway_log(L_ERROR, "Unable to create panel config");
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-12-20 22:49:11 +11:00
|
|
|
list_add(desktop_shell.panels, config);
|
|
|
|
config->wl_resource = resource;
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2015-12-04 00:35:22 +11:00
|
|
|
void background_surface_destructor(struct wl_resource *resource) {
|
|
|
|
sway_log(L_DEBUG, "Background surface killed");
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < desktop_shell.backgrounds->length; ++i) {
|
|
|
|
struct background_config *config = desktop_shell.backgrounds->items[i];
|
2015-12-20 22:42:10 +11:00
|
|
|
if (config->wl_surface_res == resource) {
|
2015-12-04 00:35:22 +11:00
|
|
|
list_del(desktop_shell.backgrounds, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void panel_surface_destructor(struct wl_resource *resource) {
|
|
|
|
sway_log(L_DEBUG, "Panel surface killed");
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < desktop_shell.panels->length; ++i) {
|
|
|
|
struct panel_config *config = desktop_shell.panels->items[i];
|
2015-12-20 22:42:10 +11:00
|
|
|
if (config->wl_surface_res == resource) {
|
2015-12-04 00:35:22 +11:00
|
|
|
list_del(desktop_shell.panels, i);
|
|
|
|
arrange_windows(&root_container, -1, -1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-19 00:49:04 +11:00
|
|
|
void lock_surface_destructor(struct wl_resource *resource) {
|
|
|
|
sway_log(L_DEBUG, "Lock surface killed");
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < desktop_shell.lock_surfaces->length; ++i) {
|
|
|
|
struct wl_resource *surface = desktop_shell.lock_surfaces->items[i];
|
|
|
|
if (surface == resource) {
|
|
|
|
list_del(desktop_shell.lock_surfaces, i);
|
|
|
|
arrange_windows(&root_container, -1, -1);
|
|
|
|
desktop_shell.is_locked = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 14:01:22 +11:00
|
|
|
static void set_background(struct wl_client *client, struct wl_resource *resource,
|
2015-11-19 23:23:11 +11:00
|
|
|
struct wl_resource *_output, struct wl_resource *surface) {
|
2016-12-02 13:51:07 +11:00
|
|
|
pid_t pid;
|
|
|
|
wl_client_get_credentials(client, &pid, NULL, NULL);
|
|
|
|
if (!(get_feature_policy(pid) & FEATURE_BACKGROUND)) {
|
|
|
|
sway_log(L_INFO, "Denying background feature to %d", pid);
|
|
|
|
return;
|
|
|
|
}
|
2015-11-19 14:14:57 +11:00
|
|
|
wlc_handle output = wlc_handle_from_wl_output_resource(_output);
|
2015-11-19 23:23:11 +11:00
|
|
|
if (!output) {
|
2015-11-19 14:14:57 +11:00
|
|
|
return;
|
|
|
|
}
|
2015-11-19 23:23:11 +11:00
|
|
|
sway_log(L_DEBUG, "Setting surface %p as background for output %d", surface, (int)output);
|
2015-11-19 14:14:57 +11:00
|
|
|
struct background_config *config = malloc(sizeof(struct background_config));
|
2016-12-16 10:03:59 +11:00
|
|
|
if (!config) {
|
|
|
|
sway_log(L_ERROR, "Unable to allocate background config");
|
|
|
|
return;
|
|
|
|
}
|
2016-07-18 01:55:05 +10:00
|
|
|
config->client = client;
|
2015-11-19 14:14:57 +11:00
|
|
|
config->output = output;
|
2015-11-19 23:23:11 +11:00
|
|
|
config->surface = wlc_resource_from_wl_surface_resource(surface);
|
2015-12-20 22:42:10 +11:00
|
|
|
config->wl_surface_res = surface;
|
2015-11-19 14:14:57 +11:00
|
|
|
list_add(desktop_shell.backgrounds, config);
|
2015-12-04 00:35:22 +11:00
|
|
|
wl_resource_set_destructor(surface, background_surface_destructor);
|
2016-05-21 07:13:22 +10:00
|
|
|
arrange_windows(swayc_by_handle(output), -1, -1);
|
2016-03-17 22:05:54 +11:00
|
|
|
wlc_output_schedule_render(config->output);
|
2015-11-19 14:01:22 +11:00
|
|
|
}
|
|
|
|
|
2015-11-30 04:03:13 +11:00
|
|
|
static void set_panel(struct wl_client *client, struct wl_resource *resource,
|
2016-05-21 07:13:47 +10:00
|
|
|
struct wl_resource *_output, struct wl_resource *surface) {
|
2016-12-02 13:51:07 +11:00
|
|
|
pid_t pid;
|
|
|
|
wl_client_get_credentials(client, &pid, NULL, NULL);
|
|
|
|
if (!(get_feature_policy(pid) & FEATURE_PANEL)) {
|
|
|
|
sway_log(L_INFO, "Denying panel feature to %d", pid);
|
|
|
|
return;
|
|
|
|
}
|
2015-11-30 04:03:13 +11:00
|
|
|
wlc_handle output = wlc_handle_from_wl_output_resource(_output);
|
|
|
|
if (!output) {
|
|
|
|
return;
|
|
|
|
}
|
2015-12-20 22:49:11 +11:00
|
|
|
sway_log(L_DEBUG, "Setting surface %p as panel for output %d (wl_resource: %p)", surface, (int)output, resource);
|
|
|
|
struct panel_config *config = find_or_create_panel_config(resource);
|
2015-11-30 04:03:13 +11:00
|
|
|
config->output = output;
|
2016-07-05 07:53:03 +10:00
|
|
|
config->client = client;
|
2015-11-30 04:03:13 +11:00
|
|
|
config->surface = wlc_resource_from_wl_surface_resource(surface);
|
2015-12-20 22:42:10 +11:00
|
|
|
config->wl_surface_res = surface;
|
2015-12-04 00:35:22 +11:00
|
|
|
wl_resource_set_destructor(surface, panel_surface_destructor);
|
2015-11-30 04:03:13 +11:00
|
|
|
arrange_windows(&root_container, -1, -1);
|
2016-03-17 22:05:54 +11:00
|
|
|
wlc_output_schedule_render(config->output);
|
2015-11-30 04:03:13 +11:00
|
|
|
}
|
|
|
|
|
2015-12-19 00:49:04 +11:00
|
|
|
static void desktop_set_lock_surface(struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface) {
|
2015-11-30 04:03:13 +11:00
|
|
|
sway_log(L_ERROR, "desktop_set_lock_surface is not currently supported");
|
|
|
|
}
|
|
|
|
|
2015-12-19 00:49:04 +11:00
|
|
|
static void desktop_unlock(struct wl_client *client, struct wl_resource *resource) {
|
2015-11-30 04:03:13 +11:00
|
|
|
sway_log(L_ERROR, "desktop_unlock is not currently supported");
|
|
|
|
}
|
|
|
|
|
2015-12-19 00:49:04 +11:00
|
|
|
static void set_lock_surface(struct wl_client *client, struct wl_resource *resource,
|
2015-12-19 11:29:44 +11:00
|
|
|
struct wl_resource *_output, struct wl_resource *surface) {
|
2016-12-02 13:51:07 +11:00
|
|
|
pid_t pid;
|
|
|
|
wl_client_get_credentials(client, &pid, NULL, NULL);
|
|
|
|
if (!(get_feature_policy(pid) & FEATURE_LOCK)) {
|
|
|
|
sway_log(L_INFO, "Denying lock feature to %d", pid);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-19 11:29:44 +11:00
|
|
|
swayc_t *output = swayc_by_handle(wlc_handle_from_wl_output_resource(_output));
|
|
|
|
swayc_t *view = swayc_by_handle(wlc_handle_from_wl_surface_resource(surface));
|
2016-01-27 10:33:50 +11:00
|
|
|
sway_log(L_DEBUG, "Setting lock surface to %p", view);
|
2015-12-19 11:29:44 +11:00
|
|
|
if (view && output) {
|
2016-02-29 06:12:07 +11:00
|
|
|
swayc_t *workspace = output->focused;
|
|
|
|
if (!swayc_is_child_of(view, workspace)) {
|
|
|
|
move_container_to(view, workspace);
|
|
|
|
}
|
2016-09-01 22:34:52 +10:00
|
|
|
// make the view floating so it doesn't rearrange other siblings.
|
2016-02-26 00:42:48 +11:00
|
|
|
if (!view->is_floating) {
|
|
|
|
destroy_container(remove_child(view));
|
2016-02-29 06:12:07 +11:00
|
|
|
add_floating(workspace, view);
|
2015-12-19 11:29:44 +11:00
|
|
|
}
|
|
|
|
wlc_view_set_state(view->handle, WLC_BIT_FULLSCREEN, true);
|
2016-09-01 22:34:52 +10:00
|
|
|
wlc_view_bring_to_front(view->handle);
|
|
|
|
wlc_view_focus(view->handle);
|
2015-12-19 11:29:44 +11:00
|
|
|
desktop_shell.is_locked = true;
|
2016-03-05 03:10:43 +11:00
|
|
|
input_init();
|
2016-09-01 22:34:52 +10:00
|
|
|
arrange_windows(workspace, -1, -1);
|
2016-03-18 03:02:17 +11:00
|
|
|
swayc_t *focus_output = swayc_active_output();
|
|
|
|
if (focus_output == output) {
|
|
|
|
set_focused_container(view);
|
|
|
|
}
|
2016-01-27 10:33:50 +11:00
|
|
|
list_add(desktop_shell.lock_surfaces, surface);
|
|
|
|
wl_resource_set_destructor(surface, lock_surface_destructor);
|
2015-12-19 11:29:44 +11:00
|
|
|
} else {
|
|
|
|
sway_log(L_ERROR, "Attempted to set lock surface to non-view");
|
|
|
|
}
|
2015-12-19 00:49:04 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
static void unlock(struct wl_client *client, struct wl_resource *resource) {
|
|
|
|
sway_log(L_ERROR, "unlock is not currently supported");
|
2015-12-19 11:29:44 +11:00
|
|
|
// This isn't really necessary, we just unlock when the client exits.
|
2015-12-19 00:49:04 +11:00
|
|
|
}
|
|
|
|
|
2015-11-30 04:03:13 +11:00
|
|
|
static void set_grab_surface(struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface) {
|
|
|
|
sway_log(L_ERROR, "desktop_set_grab_surface is not currently supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void desktop_ready(struct wl_client *client, struct wl_resource *resource) {
|
|
|
|
// nop
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_panel_position(struct wl_client *client, struct wl_resource *resource, uint32_t position) {
|
2016-12-02 13:51:07 +11:00
|
|
|
pid_t pid;
|
|
|
|
wl_client_get_credentials(client, &pid, NULL, NULL);
|
|
|
|
if (!(get_feature_policy(pid) & FEATURE_PANEL)) {
|
|
|
|
sway_log(L_INFO, "Denying panel feature to %d", pid);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-20 22:49:11 +11:00
|
|
|
struct panel_config *config = find_or_create_panel_config(resource);
|
|
|
|
sway_log(L_DEBUG, "Panel position for wl_resource %p changed %d => %d", resource, config->panel_position, position);
|
|
|
|
config->panel_position = position;
|
2015-11-30 04:03:13 +11:00
|
|
|
arrange_windows(&root_container, -1, -1);
|
|
|
|
}
|
|
|
|
|
2015-11-19 14:01:22 +11:00
|
|
|
static struct desktop_shell_interface desktop_shell_implementation = {
|
2015-11-30 04:03:13 +11:00
|
|
|
.set_background = set_background,
|
|
|
|
.set_panel = set_panel,
|
2015-12-19 00:49:04 +11:00
|
|
|
.set_lock_surface = desktop_set_lock_surface,
|
|
|
|
.unlock = desktop_unlock,
|
2015-11-30 04:03:13 +11:00
|
|
|
.set_grab_surface = set_grab_surface,
|
|
|
|
.desktop_ready = desktop_ready,
|
|
|
|
.set_panel_position = set_panel_position
|
2015-11-19 14:01:22 +11:00
|
|
|
};
|
|
|
|
|
2015-12-19 00:49:04 +11:00
|
|
|
static struct lock_interface swaylock_implementation = {
|
|
|
|
.set_lock_surface = set_lock_surface,
|
|
|
|
.unlock = unlock
|
|
|
|
};
|
|
|
|
|
2015-11-19 14:01:22 +11:00
|
|
|
static void desktop_shell_bind(struct wl_client *client, void *data,
|
|
|
|
unsigned int version, unsigned int id) {
|
2015-11-30 04:03:13 +11:00
|
|
|
if (version > 3) {
|
2015-11-19 14:01:22 +11:00
|
|
|
// Unsupported version
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wl_resource *resource = wl_resource_create(client, &desktop_shell_interface, version, id);
|
|
|
|
if (!resource) {
|
|
|
|
wl_client_post_no_memory(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
wl_resource_set_implementation(resource, &desktop_shell_implementation, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2015-12-19 00:49:04 +11:00
|
|
|
static void swaylock_bind(struct wl_client *client, void *data,
|
|
|
|
unsigned int version, unsigned int id) {
|
|
|
|
if (version > 1) {
|
|
|
|
// Unsupported version
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wl_resource *resource = wl_resource_create(client, &lock_interface, version, id);
|
|
|
|
if (!resource) {
|
|
|
|
wl_client_post_no_memory(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
wl_resource_set_implementation(resource, &swaylock_implementation, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2015-11-19 14:01:22 +11:00
|
|
|
void register_extensions(void) {
|
2015-11-30 04:03:13 +11:00
|
|
|
wl_global_create(wlc_get_wl_display(), &desktop_shell_interface, 3, NULL, desktop_shell_bind);
|
2015-11-19 14:14:57 +11:00
|
|
|
desktop_shell.backgrounds = create_list();
|
2015-11-30 04:03:13 +11:00
|
|
|
desktop_shell.panels = create_list();
|
2015-12-19 00:49:04 +11:00
|
|
|
desktop_shell.lock_surfaces = create_list();
|
|
|
|
desktop_shell.is_locked = false;
|
2015-12-19 10:56:35 +11:00
|
|
|
wl_global_create(wlc_get_wl_display(), &lock_interface, 1, NULL, swaylock_bind);
|
2015-11-19 14:01:22 +11:00
|
|
|
}
|