aboutsummaryrefslogtreecommitdiffhomepage
path: root/hyprctl/main.cpp
blob: 9cca97926981784892b7a458a23f39460e8bd1c5 (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
#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 <unistd.h>

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

const std::string USAGE = R"#(
usage: hyprctl [command] [(opt)args]
    
    monitors
    workspaces
    clients
    activewindow
    layers
    dispatch
    keyword
    version
    reload
)#";

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

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

    const auto SERVER = gethostbyname("localhost");

    if (!SERVER) {
        std::cout << "Couldn't get host (2)";
        return;
    }

    sockaddr_in serverAddress = {0};
    serverAddress.sin_family = AF_INET;
    bcopy((char*)SERVER->h_addr, (char*)&serverAddress.sin_addr.s_addr, SERVER->h_length);

    std::ifstream socketPortStream;
    socketPortStream.open("/tmp/hypr/.socket");
    
    if (!socketPortStream.good()) {
        std::cout << "No socket port file (2a)";
        return;
    }

    std::string port = "";
    std::getline(socketPortStream, port);
    socketPortStream.close();

    int portInt = 0;
    try {
        portInt = std::stoi(port.c_str());
    } catch (...) {
        std::cout << "Port not an int?! (2b)";
        return;
    }

    if (portInt == 0) {
        std::cout << "Port not an int?! (2c)";
        return;
    }
    
    serverAddress.sin_port = portInt;

    if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, sizeof(serverAddress)) < 0) {
        std::cout << "Couldn't connect to port " << port << " (3) Is Hyprland running?";
        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 batchRequest(int argc, char** argv) {
    std::string rq = "[[BATCH]]" + std::string(argv[2]);
    
    request(rq);
}

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

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

    if (!strcmp(argv[1], "monitors")) request("monitors");
    else if (!strcmp(argv[1], "clients")) request("clients");
    else if (!strcmp(argv[1], "workspaces")) request("workspaces");
    else if (!strcmp(argv[1], "activewindow")) request("activewindow");
    else if (!strcmp(argv[1], "layers")) request("layers");
    else if (!strcmp(argv[1], "version")) request("version");
    else if (!strcmp(argv[1], "reload")) request("reload");
    else if (!strcmp(argv[1], "dispatch")) dispatchRequest(argc, argv);
    else if (!strcmp(argv[1], "keyword")) keywordRequest(argc, argv);
    else if (!strcmp(argv[1], "--batch")) batchRequest(argc, argv);
    else {
        printf("%s", USAGE.c_str());
        return 1;
    }

    return 0;
}