forked from project-repo/cagebreak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.c
223 lines (199 loc) · 5.9 KB
/
view.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2020 - 2023, project-repo and the cagebreak contributors
// SPDX-License-Identifier: MIT
#define _POSIX_C_SOURCE 200809L
#include <stdbool.h>
#include <string.h>
#include <wayland-server-core.h>
#include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_output_damage.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/util/box.h>
#include "ipc_server.h"
#include "output.h"
#include "seat.h"
#include "server.h"
#include "view.h"
#include "workspace.h"
#if CG_HAS_XWAYLAND
#include "xwayland.h"
#endif
struct cg_view *
view_get_prev_view(struct cg_view *view) {
struct cg_view *prev = NULL;
struct cg_view *it_view;
struct wl_list *it;
it = view->link.prev;
while(it != &view->link) {
if(it == &view->workspace->views) {
it = it->prev;
continue;
}
it_view = wl_container_of(it, it_view, link);
if(!view_is_visible(it_view)) {
prev = it_view;
break;
}
it = it->prev;
}
return prev;
}
bool
view_is_primary(const struct cg_view *view) {
return view->impl->is_primary(view);
}
struct cg_tile *
view_get_tile(const struct cg_view *view) {
if(view->tile != NULL && view->tile->view == view) {
return view->tile;
} else {
return NULL;
}
}
bool
view_is_visible(const struct cg_view *view) {
#if CG_HAS_XWAYLAND
if(view->type == CG_XWAYLAND_VIEW && !xwayland_view_should_manage(view)) {
return true;
}
#endif
return view_get_tile(view) != NULL;
}
void
view_activate(struct cg_view *view, bool activate) {
if(view != NULL) {
view->impl->activate(view, activate);
}
}
void
view_maximize(struct cg_view *view, struct cg_tile *tile) {
view->ox = tile->tile.x;
view->oy = tile->tile.y;
struct wlr_box box;
wlr_output_layout_get_box(view->server->output_layout,
view->workspace->output->wlr_output, &box);
wlr_scene_node_set_position(&view->scene_tree->node, view->ox + box.x,
view->oy + box.y);
view->impl->maximize(view, tile->tile.width, tile->tile.height);
view->tile = tile;
wlr_scene_node_raise_to_top(&view->scene_tree->node);
}
void
view_unmap(struct cg_view *view) {
uint32_t id = view->id;
uint32_t tile_id = 0;
uint32_t ws = view->workspace->num;
char *output_name = view->workspace->output->wlr_output->name;
int output_id = output_get_num(view->workspace->output);
pid_t pid = view->impl->get_pid(view);
/* If the view is not mapped, do nothing */
if(view->wlr_surface == NULL) {
return;
}
if(view->tile == NULL) {
tile_id = -1;
} else {
tile_id = view->tile->id;
}
wlr_scene_node_destroy(&view->scene_tree->node);
#if CG_HAS_XWAYLAND
if((view->type != CG_XWAYLAND_VIEW || xwayland_view_should_manage(view)))
#endif
{
struct cg_view *prev = view_get_prev_view(view);
if(view == view->server->seat->focused_view) {
seat_set_focus(view->server->seat, prev);
} else if(view->server->seat->seat->keyboard_state.focused_surface ==
view->wlr_surface) {
wlr_seat_keyboard_clear_focus(view->server->seat->seat);
seat_set_focus(view->server->seat,
view->server->seat->focused_view);
}
struct cg_tile *view_tile = view_get_tile(view);
if(view_tile != NULL) {
workspace_tile_update_view(view_tile, prev);
}
}
#if CG_HAS_XWAYLAND
else {
if(view->server->seat->seat->keyboard_state.focused_surface == NULL ||
view->server->seat->seat->keyboard_state.focused_surface ==
view->wlr_surface) {
seat_set_focus(view->server->seat,
view->server->seat->focused_view);
}
}
#endif
wl_list_remove(&view->link);
view->wlr_surface = NULL;
ipc_send_event(
view->workspace->server,
"{\"event_name\":\"view_unmap\",\"view_id\":%d,\"tile_id\":%d,"
"\"workspace\":%d,\"output\":\"%s\",\"output_id\":%d,\"view_pid\":%d}",
id, tile_id, ws + 1, output_name, output_id, pid);
}
void
view_map(struct cg_view *view, struct wlr_surface *surface,
struct cg_workspace *ws) {
struct cg_output *output = ws->output;
view->wlr_surface = surface;
view->scene_tree = wlr_scene_subsurface_tree_create(ws->scene, surface);
if(!view->scene_tree) {
wl_resource_post_no_memory(surface->resource);
return;
}
view->scene_tree->node.data = view;
view->workspace = ws;
#if CG_HAS_XWAYLAND
/* We shouldn't position override-redirect windows. They set
their own (x,y) coordinates in handle_wayland_surface_map. */
if(view->type == CG_XWAYLAND_VIEW && !xwayland_view_should_manage(view)) {
wl_list_insert(&ws->unmanaged_views, &view->link);
struct wlr_box box;
wlr_output_layout_get_box(view->server->output_layout,
view->workspace->output->wlr_output, &box);
wlr_scene_node_set_position(&view->scene_tree->node, view->ox + box.x,
view->oy + box.y);
} else
#endif
{
wl_list_insert(&ws->views, &view->link);
}
seat_set_focus(output->server->seat, view);
int tile_id = 0;
if(view->tile == NULL) {
tile_id = -1;
} else {
tile_id = view->tile->id;
}
ipc_send_event(
output->server,
"{\"event_name\":\"view_map\",\"view_id\":%d,\"tile_id\":%d,"
"\"workspace\":%d,\"output\":\"%s\",\"output_id\":%d,\"view_pid\":%d}",
view->id, tile_id, view->workspace->num + 1,
view->workspace->output->wlr_output->name,
output_get_num(view->workspace->output), view->impl->get_pid(view));
}
void
view_destroy(struct cg_view *view) {
struct cg_output *curr_output = view->server->curr_output;
if(view->wlr_surface != NULL) {
view_unmap(view);
}
view->impl->destroy(view);
view_activate(curr_output->workspaces[curr_output->curr_workspace]
->focused_tile->view,
true);
}
void
view_init(struct cg_view *view, enum cg_view_type type,
const struct cg_view_impl *impl, struct cg_server *server) {
view->workspace = NULL;
view->tile = NULL;
view->server = server;
view->type = type;
view->impl = impl;
view->id = server->views_curr_id;
++server->views_curr_id;
}