-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathhyprland.cpp
More file actions
162 lines (142 loc) · 5.07 KB
/
hyprland.cpp
File metadata and controls
162 lines (142 loc) · 5.07 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
// Display images inside a terminal
// Copyright (C) 2023 JustKidding
//
// 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 3 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, see <https://www.gnu.org/licenses/>.
#include "hyprland.hpp"
#include "os.hpp"
#include "tmux.hpp"
#include "util/socket.hpp"
#include <filesystem>
#include <fmt/format.h>
#include <nlohmann/json.hpp>
#include <range/v3/all.hpp>
using njson = nlohmann::json;
namespace fs = std::filesystem;
HyprlandSocket::HyprlandSocket(const std::string_view signature)
: logger(spdlog::get("wayland"))
{
const auto socket_base_dir = os::getenv("XDG_RUNTIME_DIR").value_or("/tmp");
const auto socket_rel_path = fmt::format("hypr/{}/.socket.sock", signature);
socket_path = fmt::format("{}/{}", socket_base_dir, socket_rel_path);
// XDG_RUNTIME_DIR set but hyprland < 0.40
if (!fs::exists(socket_path)) {
socket_path = fmt::format("/tmp/{}", socket_rel_path);
}
logger->info("Using hyprland socket {}", socket_path);
const auto active = request_result("j/activewindow");
address = active.at("address");
set_active_monitor();
}
void HyprlandSocket::set_active_monitor()
{
const auto monitors = request_result("j/monitors");
for (const auto &monitor : monitors) {
bool focused = monitor.at("focused");
if (focused) {
output_name = monitor.at("name");
output_scale = monitor.at("scale");
break;
}
}
}
auto HyprlandSocket::get_focused_output_name() -> std::string
{
return output_name;
}
auto HyprlandSocket::request_result(const std::string_view payload) -> nlohmann::json
{
const UnixSocket socket{socket_path};
socket.write(payload.data(), payload.size());
const std::string result = socket.read_until_empty();
return njson::parse(result);
}
void HyprlandSocket::request(const std::string_view payload)
{
const UnixSocket socket{socket_path};
logger->debug("Running socket command {}", payload);
socket.write(payload.data(), payload.length());
}
auto HyprlandSocket::get_active_window() -> nlohmann::json
{
// recalculate address in case it changed
if (tmux::is_used()) {
const auto active = request_result("j/activewindow");
address = active.at("address");
}
const auto clients = request_result("j/clients");
const auto client = ranges::find_if(clients, [this](const njson &json) { return json.at("address") == address; });
if (client == clients.end()) {
throw std::runtime_error("Active window not found");
}
return *client;
}
auto HyprlandSocket::get_window_info() -> struct WaylandWindowGeometry {
const auto terminal = get_active_window();
const auto &sizes = terminal.at("size");
const auto &coords = terminal.at("at");
return {
.width = sizes.at(0),
.height = sizes.at(1),
.x = coords.at(0),
.y = coords.at(1),
};
}
void HyprlandSocket::initial_setup(const std::string_view appid)
{
disable_focus(appid);
enable_floating(appid);
remove_borders(appid);
remove_rounding(appid);
}
void HyprlandSocket::remove_rounding(const std::string_view appid)
{
const auto payload = fmt::format("/keyword windowrulev2 rounding 0,title:{}", appid);
request(payload);
}
void HyprlandSocket::disable_focus(const std::string_view appid)
{
const auto payload = fmt::format("/keyword windowrulev2 nofocus,title:{}", appid);
request(payload);
}
void HyprlandSocket::enable_floating(const std::string_view appid)
{
const auto payload = fmt::format("/keyword windowrulev2 float,title:{}", appid);
request(payload);
}
void HyprlandSocket::remove_borders(const std::string_view appid)
{
const auto payload = fmt::format("/keyword windowrulev2 noborder,title:{}", appid);
request(payload);
}
void HyprlandSocket::change_workspace(const std::string_view appid, int workspaceid)
{
const auto payload = fmt::format("/dispatch movetoworkspacesilent {},title:{}", workspaceid, appid);
request(payload);
}
void HyprlandSocket::move_window(const std::string_view appid, int xcoord, int ycoord)
{
auto terminal = get_active_window();
const auto &workspace = terminal.at("workspace");
const int workspaceid = workspace.at("id");
change_workspace(appid, workspaceid);
int res_x = xcoord;
int res_y = ycoord;
if (output_scale > 1.0F) {
const int offset = 10;
res_x = res_x / 2 + offset;
res_y = res_y / 2 + offset;
}
const auto payload = fmt::format("/dispatch movewindowpixel exact {} {},title:{}", res_x, res_y, appid);
request(payload);
}