-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
304 lines (231 loc) · 8.33 KB
/
Copy pathutils.cpp
File metadata and controls
304 lines (231 loc) · 8.33 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <fcntl.h>
#include <fstream>
#include <linux/input-event-codes.h>
#include <linux/prctl.h>
#include <sys/prctl.h>
#include <unistd.h>
#include <sys/wait.h>
#include "utils.hpp"
#include "layer.hpp"
#include "toplevel.hpp"
std::tuple<yawc_toplevel*, yawc_input_on_surface>
utils::desktop_toplevel_at(yawc_server* server, double lx, double ly)
{
double sx, sy;
if(server->drag.running){
wlr_scene_node_set_enabled(&server->drag.icons->node, false);
}
struct wlr_scene_node* node = wlr_scene_node_at(&server->scene->tree.node, lx, ly, &sx, &sy);
if(server->drag.running){ // in case we're grabbing an icon we cant have the drag icon annoying us
wlr_scene_node_set_enabled(&server->drag.icons->node, true);
}
if (node == NULL || node->type != WLR_SCENE_NODE_BUFFER) {
return {};
}
struct wlr_scene_buffer* scene_buffer = wlr_scene_buffer_from_node(node);
struct wlr_scene_surface* scene_surface = wlr_scene_surface_try_from_buffer(scene_buffer);
if (!scene_surface) {
return {};
}
yawc_input_on_surface out;
out.surface = scene_surface->surface;
out.x = sx;
out.y = sy;
struct wlr_scene_tree* tree = node->parent;
while (tree != NULL && tree->node.data == NULL) {
tree = tree->node.parent;
}
if (tree == NULL) {
return std::make_tuple(nullptr, out);
}
auto desc = scene_descriptor_try_get(&tree->node, YAWC_SCENE_DESC_VIEW);
if(!desc){
return std::make_tuple(nullptr, out);
}
return std::make_tuple(static_cast<struct yawc_toplevel*>(desc->parent), out);
}
std::tuple<wlr_scene_node*, yawc_input_on_node>
utils::desktop_node_at(yawc_server* server, double lx, double ly)
{
double sx, sy;
struct wlr_scene_node* node = wlr_scene_node_at(&server->scene->tree.node, lx, ly, &sx, &sy);
if (!node) {
return std::make_tuple(nullptr, yawc_input_on_node{0,0});
}
yawc_input_on_node out;
out.x = sx;
out.y = sy;
return std::make_tuple(node, out);
}
struct yawc_toplevel *utils::previous_toplevel(struct yawc_server *server){
if(wl_list_empty(&server->toplevels)){
return nullptr;
}
struct yawc_toplevel *prev_toplevel = wl_container_of(server->toplevels.next, prev_toplevel, link);
if (!prev_toplevel->mapped) {
return nullptr;
}
return prev_toplevel;
}
void utils::focus_toplevel(struct yawc_toplevel* toplevel)
{
if (toplevel == nullptr) {
return;
}
struct yawc_server* server = toplevel->server;
struct wlr_seat* seat = server->seat;
struct wlr_surface* prev_surface = seat->keyboard_state.focused_surface;
struct wlr_surface* surface = toplevel->xdg_toplevel->base->surface;
if (prev_surface == surface) {
return;
}
server->constrain_cursor(nullptr);
if (!wl_list_empty(&server->toplevels)) {
struct yawc_toplevel *prev_toplevel = wl_container_of(server->toplevels.next, prev_toplevel, link);
if (prev_toplevel != toplevel && prev_toplevel->mapped) {
if(prev_toplevel->fullscreen){ //the idea is simple, if is a fullscreen window, alt tab should let other windows stack on top
wlr_scene_node_reparent(&prev_toplevel->scene_tree->node, server->layers.normal);
}
prev_toplevel->activate(false);
}
}
if(toplevel->scene_tree){
wlr_scene_node_raise_to_top(&toplevel->scene_tree->node);
if(toplevel->fullscreen){
wlr_scene_node_reparent(&toplevel->scene_tree->node, server->layers.fullscreen);
}
}
wl_list_remove(&toplevel->link);
wl_list_insert(&server->toplevels, &toplevel->link);
toplevel->activate(true);
server->set_focus_layer(nullptr);
server->set_focus_surface(surface);
struct wlr_pointer_constraint_v1 *req = wlr_pointer_constraints_v1_constraint_for_surface(
server->pointer_constraints, surface, server->seat);
server->constrain_cursor(req);
}
bool utils::toplevel_not_empty(struct yawc_toplevel* toplevel)
{
if (!toplevel || !toplevel->scene_tree) {
return false;
}
return toplevel->xdg_toplevel != nullptr &&
toplevel->xdg_toplevel->base != nullptr;
}
struct yawc_output*
utils::get_output_of_toplevel(struct yawc_toplevel* toplevel)
{
double closest_x, closest_y;
struct wlr_output *output = nullptr;
auto geometry = toplevel->xdg_toplevel->base->geometry;
int global_x, global_y;
wlr_scene_node_coords(&toplevel->scene_tree->node, &global_x, &global_y);
wlr_output_layout_closest_point(toplevel->server->output_layout, output,
global_x + geometry.x + geometry.width / 2.,
global_y + geometry.y + geometry.height / 2.,
&closest_x, &closest_y);
auto *wlr_output = wlr_output_layout_output_at(toplevel->server->output_layout, closest_x, closest_y);
if (!wlr_output) {
return nullptr;
}
struct yawc_output* out;
wl_list_for_each(out, &toplevel->server->outputs, link)
{
if (out->wlr_output == wlr_output) {
return out;
}
}
return nullptr;
}
bool utils::pointer_pressed(struct wlr_pointer_button_event *event){
return event->state == wl_pointer_button_state::WL_POINTER_BUTTON_STATE_PRESSED && event->button == BTN_LEFT;
}
void utils::wake_up_from_idle(struct yawc_server *server){
wlr_idle_notifier_v1_notify_activity(server->idle_notify_manager, server->seat);
}
struct wlr_layer_surface_v1 *utils::toplevel_layer_surface_from_surface(struct wlr_surface *surface){
struct wlr_layer_surface_v1 *layer;
do {
if (!surface) {
return nullptr;
}
if ((layer = wlr_layer_surface_v1_try_from_wlr_surface(surface))) {
return layer;
}
if (wlr_subsurface_try_from_wlr_surface(surface)) {
surface = wlr_surface_get_root_surface(surface);
continue;
}
struct wlr_xdg_surface *xdg_surface = NULL;
if ((xdg_surface = wlr_xdg_surface_try_from_wlr_surface(surface)) &&
xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP && xdg_surface->popup != NULL) {
if (!xdg_surface->popup->parent) {
return NULL;
}
surface = wlr_surface_get_root_surface(xdg_surface->popup->parent);
continue;
}
return nullptr;
} while (true);
}
struct wlr_box utils::get_usable_area_of_output(struct yawc_output *output){
auto *server = output->server;
struct wlr_box box;
wlr_output_layout_get_box(server->output_layout, output->wlr_output, &box);
struct yawc_layer_surface *layer;
wl_list_for_each(layer, &output->layer_surfaces, link) {
struct wlr_layer_surface_v1 *surface = layer->layer_surface;
if (!surface->surface->mapped || surface->current.exclusive_zone <= 0) {
continue;
}
int zone = surface->current.exclusive_zone;
uint32_t anchor = surface->current.anchor;
if ((anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) &&
!(anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM)) {
box.y += zone;
box.height -= zone;
}
else if ((anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM) &&
!(anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP)) {
box.height -= zone;
}
else if ((anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT) &&
!(anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT)) {
box.x += zone;
box.width -= zone;
}
else if ((anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT) &&
!(anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT)) {
box.width -= zone;
}
}
return box;
}
void utils::exec(const char *cmd){
pid_t pid = fork();
if (pid == 0) {
sigset_t set;
sigemptyset(&set);
sigprocmask(SIG_SETMASK, &set, NULL);
if (fork() == 0) {
execl("/bin/sh", "sh", "-c", cmd, (void*)NULL);
_exit(0);
}
_exit(0);
} else if (pid > 0) {
waitpid(pid, NULL, 0);
}
}
uint64_t utils::hash_file_fnv1a(const std::string& path) {
std::ifstream file(path, std::ios::binary);
if (!file) return 0;
uint64_t hash = 0xcbf29ce484222325;
char buffer[4096];
while (file.read(buffer, sizeof(buffer)) || file.gcount()) {
for (std::streamsize i = 0; i < file.gcount(); ++i) {
hash ^= static_cast<uint8_t>(buffer[i]);
hash *= 0x100000001b3;
}
}
return hash;
}