aboutsummaryrefslogtreecommitdiffstats
path: root/sway/desktop/wl_shell.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/desktop/wl_shell.c')
-rw-r--r--sway/desktop/wl_shell.c131
1 files changed, 131 insertions, 0 deletions
diff --git a/sway/desktop/wl_shell.c b/sway/desktop/wl_shell.c
new file mode 100644
index 00000000..b63c220c
--- /dev/null
+++ b/sway/desktop/wl_shell.c
@@ -0,0 +1,131 @@
1#define _POSIX_C_SOURCE 199309L
2#include <stdbool.h>
3#include <stdlib.h>
4#include <wayland-server.h>
5#include <wlr/types/wlr_wl_shell.h>
6#include "sway/tree/container.h"
7#include "sway/tree/layout.h"
8#include "sway/server.h"
9#include "sway/tree/view.h"
10#include "sway/input/seat.h"
11#include "sway/input/input-manager.h"
12#include "log.h"
13
14static struct sway_wl_shell_view *wl_shell_view_from_view(
15 struct sway_view *view) {
16 if (!sway_assert(view->type == SWAY_VIEW_WL_SHELL,
17 "Expected wl_shell view")) {
18 return NULL;
19 }
20 return (struct sway_wl_shell_view *)view;
21}
22
23static const char *get_prop(struct sway_view *view, enum sway_view_prop prop) {
24 if (wl_shell_view_from_view(view) == NULL) {
25 return NULL;
26 }
27 switch (prop) {
28 case VIEW_PROP_TITLE:
29 return view->wlr_wl_shell_surface->title;
30 case VIEW_PROP_CLASS:
31 return view->wlr_wl_shell_surface->class;
32 default:
33 return NULL;
34 }
35}
36
37static void configure(struct sway_view *view, double ox, double oy, int width,
38 int height) {
39 struct sway_wl_shell_view *wl_shell_view = wl_shell_view_from_view(view);
40 if (wl_shell_view == NULL) {
41 return;
42 }
43 view_update_position(view, ox, oy);
44 wl_shell_view->pending_width = width;
45 wl_shell_view->pending_height = height;
46 wlr_wl_shell_surface_configure(view->wlr_wl_shell_surface, 0, width, height);
47}
48
49static void _close(struct sway_view *view) {
50 if (wl_shell_view_from_view(view) == NULL) {
51 return;
52 }
53
54 wl_client_destroy(view->wlr_wl_shell_surface->client);
55}
56
57static void destroy(struct sway_view *view) {
58 struct sway_wl_shell_view *wl_shell_view = wl_shell_view_from_view(view);
59 if (wl_shell_view == NULL) {
60 return;
61 }
62 wl_list_remove(&wl_shell_view->commit.link);
63 wl_list_remove(&wl_shell_view->destroy.link);
64 free(wl_shell_view);
65}
66
67static const struct sway_view_impl view_impl = {
68 .get_prop = get_prop,
69 .configure = configure,
70 .close = _close,
71 .destroy = destroy,
72};
73
74static void handle_commit(struct wl_listener *listener, void *data) {
75 struct sway_wl_shell_view *wl_shell_view =
76 wl_container_of(listener, wl_shell_view, commit);
77 struct sway_view *view = &wl_shell_view->view;
78 // NOTE: We intentionally discard the view's desired width here
79 // TODO: Let floating views do whatever
80 view_update_size(view, wl_shell_view->pending_width,
81 wl_shell_view->pending_height);
82 view_damage(view, false);
83}
84
85static void handle_destroy(struct wl_listener *listener, void *data) {
86 struct sway_wl_shell_view *wl_shell_view =
87 wl_container_of(listener, wl_shell_view, destroy);
88 view_destroy(&wl_shell_view->view);
89}
90
91void handle_wl_shell_surface(struct wl_listener *listener, void *data) {
92 struct sway_server *server = wl_container_of(listener, server,
93 wl_shell_surface);
94 struct wlr_wl_shell_surface *shell_surface = data;
95
96 if (shell_surface->state == WLR_WL_SHELL_SURFACE_STATE_POPUP) {
97 // popups don't get views
98 wlr_log(L_DEBUG, "New wl_shell popup");
99 return;
100 }
101
102 // TODO: make transient windows floating
103
104 wlr_log(L_DEBUG, "New wl_shell toplevel title='%s' app_id='%s'",
105 shell_surface->title, shell_surface->class);
106 wlr_wl_shell_surface_ping(shell_surface);
107
108 struct sway_wl_shell_view *wl_shell_view =
109 calloc(1, sizeof(struct sway_wl_shell_view));
110 if (!sway_assert(wl_shell_view, "Failed to allocate view")) {
111 return;
112 }
113
114 view_init(&wl_shell_view->view, SWAY_VIEW_WL_SHELL, &view_impl);
115 wl_shell_view->view.wlr_wl_shell_surface = shell_surface;
116
117 // TODO:
118 // - Wire up listeners
119 // - Look up pid and open on appropriate workspace
120 // - Set new view to maximized so it behaves nicely
121 // - Criteria
122
123 wl_shell_view->commit.notify = handle_commit;
124 wl_signal_add(&shell_surface->surface->events.commit,
125 &wl_shell_view->commit);
126
127 wl_shell_view->destroy.notify = handle_destroy;
128 wl_signal_add(&shell_surface->events.destroy, &wl_shell_view->destroy);
129
130 view_map(&wl_shell_view->view, shell_surface->surface);
131}