aboutsummaryrefslogtreecommitdiffhomepage
path: root/hyprctl/main.cpp
blob: 820e9fe99706145129c7da7b5bc31dc84560e738 (plain)
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
#include <ctype.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include <ranges>
#include <algorithm>

#include <iostream>
#include <string>
#include <fstream>
#include <string>
#include <deque>

const std::string USAGE = R"#(usage: hyprctl [(opt)flags] [command] [(opt)args]
    
commands:
    monitors
    workspaces
    clients
    activewindow
    layers
    devices
    dispatch
    keyword
    version
    kill
    splash
    hyprpaper
    reload
    setcursor
    getoption
    
flags:
    -j -> output in JSON
    --batch -> execute a batch of commands, separated by ';'
)#";

void request(std::string arg) {
    const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);

    if (SERVERSOCKET < 0) {
        std::cout << "Couldn't open a socket (1)";
        return;
    }


    // get the instance signature
    auto instanceSig = getenv("HYPRLAND_INSTANCE_SIGNATURE");

    if (!instanceSig) {
        std::cout << "HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)";
        return;
    }

    std::string instanceSigStr = std::string(instanceSig);

    sockaddr_un serverAddress = {0};
    serverAddress.sun_family = AF_UNIX;

    std::string socketPath = "/tmp/hypr/" + instanceSigStr + "/.socket.sock";

    strcpy(serverAddress.sun_path, socketPath.c_str());

    if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, SUN_LEN(&serverAddress)) < 0) {
        std::cout << "Couldn't connect to " << socketPath << ". (3)";
        return;
    }

    auto sizeWritten = write(SERVERSOCKET, arg.c_str(), arg.length());

    if (sizeWritten < 0) {
        std::cout << "Couldn't write (4)";
        return;
    }

    char buffer[8192] = {0};

    sizeWritten = read(SERVERSOCKET, buffer, 8192);

    if (sizeWritten < 0) {
        std::cout << "Couldn't read (5)";
        return;
    }

    close(SERVERSOCKET);

    std::cout << std::string(buffer);
}

void requestHyprpaper(std::string arg) {
    const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);

    if (SERVERSOCKET < 0) {
        std::cout << "Couldn't open a socket (1)";
        return;
    }

    // get the instance signature
    auto instanceSig = getenv("HYPRLAND_INSTANCE_SIGNATURE");

    if (!instanceSig) {
        std::cout << "HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)";
        return;
    }

    std::string instanceSigStr = std::string(instanceSig);

    sockaddr_un serverAddress = {0};
    serverAddress.sun_family = AF_UNIX;

    std::string socketPath = "/tmp/hypr/" + instanceSigStr + "/.hyprpaper.sock";

    strcpy(serverAddress.sun_path, socketPath.c_str());

    if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, SUN_LEN(&serverAddress)) < 0) {
        std::cout << "Couldn't connect to " << socketPath << ". (3)";
        return;
    }

    auto sizeWritten = write(SERVERSOCKET, arg.c_str(), arg.length());

    if (sizeWritten < 0) {
        std::cout << "Couldn't write (4)";
        return;
    }

    char buffer[8192] = {0};

    sizeWritten = read(SERVERSOCKET, buffer, 8192);

    if (sizeWritten < 0) {
        std::cout << "Couldn't read (5)";
        return;
    }

    close(SERVERSOCKET);

    std::cout << std::string(buffer);
}

void dispatchRequest(int argc, char** argv) {

    if (argc < 4) {
        std::cout << "dispatch requires 2 params";
        return;
    }

    std::string rq = "/dispatch " + std::string(argv[2]) + " " + std::string(argv[3]);

    request(rq);
}

void keywordRequest(int argc, char** argv) {
    if (argc < 4) {
        std::cout << "keyword requires 2 params";
        return;
    }

    std::string rq = "keyword " + std::string(argv[2]) + " " + std::string(argv[3]);

    request(rq);
}

void hyprpaperRequest(int argc, char** argv) {
    if (argc < 4) {
        std::cout << "hyprpaper requires 2 params";
        return;
    }

    std::string rq = std::string(argv[2]) + " " + std::string(argv[3]);

    requestHyprpaper(rq);
}

void setcursorRequest(int argc, char** argv) {
    if (argc < 4) {
        std::cout << "setcursor requires 2 params";
        return;
    }

    std::string rq = "setcursor " + std::string(argv[2]) + " " + std::string(argv[3]);

    request(rq);
}

void batchRequest(std::string arg) {
    std::string rq = "[[BATCH]]" + arg.substr(arg.find_first_of(" ") + 1);
    
    request(rq);
}

std::deque<std::string> splitArgs(int argc, char** argv) {
    std::deque<std::string> result;

    for (auto i = 1 /* skip the executable */; i < argc; ++i)
        result.push_back(std::string(argv[i]));

    return result;
}

bool isNumber(const std::string& str, bool allowfloat) {
    return std::ranges::all_of(str.begin(), str.end(), [&](char c) { return isdigit(c) != 0 || c == '-' || (allowfloat && c == '.'); });
}

int main(int argc, char** argv) {
    int bflag = 0, sflag = 0, index, c;

    if (argc < 2) {
        printf("%s\n", USAGE.c_str());
        return 1;
    }

    std::string fullRequest = "";
    std::string fullArgs = "";
    const auto ARGS = splitArgs(argc, argv);

    for (auto i = 0; i < ARGS.size(); ++i) {
        if (ARGS[i][0] == '-' && !isNumber(ARGS[i], true) /* For stuff like -2 */) {
            // parse
            if (ARGS[i] == "-j" && !fullArgs.contains("j")) {
                fullArgs += "j";
            } else if (ARGS[i] == "--batch") {
                fullRequest = "--batch ";
            } else {
                printf("%s\n", USAGE.c_str());
                return 1;
            }

            continue;
        }

        fullRequest += ARGS[i] + " ";
    }

    if (fullRequest.empty()) {
        printf("%s\n", USAGE.c_str());
        return 1;
    }

    fullRequest.pop_back(); // remove trailing space

    fullRequest = fullArgs + "/" + fullRequest;

    if (fullRequest.contains("/--batch")) batchRequest(fullRequest);
    else if (fullRequest.contains("/monitors")) request(fullRequest);
    else if (fullRequest.contains("/clients")) request(fullRequest);
    else if (fullRequest.contains("/workspaces")) request(fullRequest);
    else if (fullRequest.contains("/activewindow")) request(fullRequest);
    else if (fullRequest.contains("/layers")) request(fullRequest);
    else if (fullRequest.contains("/version")) request(fullRequest);
    else if (fullRequest.contains("/kill")) request(fullRequest);
    else if (fullRequest.contains("/splash")) request(fullRequest);
    else if (fullRequest.contains("/devices")) request(fullRequest);
    else if (fullRequest.contains("/reload")) request(fullRequest);
    else if (fullRequest.contains("/getoption")) request(fullRequest);
    else if (fullRequest.contains("/setcursor")) setcursorRequest(argc, argv);
    else if (fullRequest.contains("/dispatch")) dispatchRequest(argc, argv);
    else if (fullRequest.contains("/keyword")) keywordRequest(argc, argv);
    else if (fullRequest.contains("/hyprpaper")) hyprpaperRequest(argc, argv);
    else if (fullRequest.contains("/--help")) printf("%s", USAGE.c_str());
    else {
        printf("%s\n", USAGE.c_str());
        return 1;
    }

    printf("\n");
    return 0;
}