-
Notifications
You must be signed in to change notification settings - Fork 2
/
sway-musli.c
executable file
·281 lines (238 loc) · 8.23 KB
/
sway-musli.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <linux/wireless.h>
#define IW_INTERFACE "wlan0"
#define MAX_BUF 100
#define OUTPUT_BUF_SIZE 100
// status-line.c
// - Print a line containing the WIFI SSID, keyboard layout, battery capacity,
// battery status, and time.
// - Made for Sway.
// - Depends on swaymsg (for keyboard layout) and iwctl (SSID).
// - Example:
// MyWiFi | Qwerty | 80% - Charging | Sun 2021-08-22 12:00:00
void read_file(const char *filename, char *buffer, size_t buffer_size) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Cannot open file %s\n", filename);
exit(1);
}
fgets(buffer, buffer_size - 1, fp);
buffer[strcspn(buffer, "\n")] = 0; // remove newline
fclose(fp);
}
char* get_time_string() {
static char time_str[25]; // "aaa yyyy-mm-dd hh:mm:ss"
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(time_str, sizeof(time_str), "%a %Y-%m-%d %H:%M:%S", timeinfo);
return time_str;
}
// adapted from https://stackoverflow.com/a/19733653/21567639
void extract_wifi_ssid(char *buffer, size_t buffer_size) {
static int sockfd = -1;
char * id;
id = (char *)malloc(sizeof(char)*IW_ESSID_MAX_SIZE+1);
struct iwreq wreq;
memset(&wreq, 0, sizeof(struct iwreq));
wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
sprintf(wreq.ifr_name, IW_INTERFACE);
// initialize socket if it hasn't been initialized yet
if (sockfd == -1) {
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
exit(1);
}
}
wreq.u.essid.pointer = id;
if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
fprintf(stderr, "Get ESSID ioctl failed \n");
exit(2);
}
strncpy(buffer, (char *)wreq.u.essid.pointer, buffer_size);
buffer[buffer_size - 1] = 0;
// if buffer is empty, set "Not Connected" instead.
if (strlen(buffer) == 0) {
strncpy(buffer, "Not Connected", buffer_size);
buffer[buffer_size - 1] = 0;
}
free(id);
}
void send_sway_command(int sockfd, uint32_t command_type, const char *command) {
const char *magic = "i3-ipc"; // Sway IPC magic string
uint32_t magic_length = strlen(magic);
uint32_t command_size = strlen(command);
size_t message_size = magic_length + sizeof(command_size) + sizeof(command_type) + command_size;
// Allocate buffer for the entire message
char *message = malloc(message_size);
if (message == NULL) {
perror("malloc failed");
exit(1);
}
// Copy the magic string, command size, command type, and command into the buffer
char *buffer_ptr = message;
memcpy(buffer_ptr, magic, magic_length);
buffer_ptr += magic_length;
memcpy(buffer_ptr, &command_size, sizeof(command_size));
buffer_ptr += sizeof(command_size);
memcpy(buffer_ptr, &command_type, sizeof(command_type));
buffer_ptr += sizeof(command_type);
memcpy(buffer_ptr, command, command_size);
if (write(sockfd, message, message_size) != message_size) {
perror("write failed");
exit(1);
}
// Free the allocated buffer
free(message);
}
// A simple function to extract the keyboard layout from the swaymsg command output.
void extract_keyboard_layout(char *buffer, size_t buffer_size) {
static int sockfd = -1;
struct sockaddr_un addr;
static const char *socket_path;
const char *get_inputs_command = "get_inputs";
char read_buffer[1024];
int found = 0;
// get socket path on first run
if (socket_path == NULL) {
socket_path = getenv("SWAYSOCK");
// If not found, try I3SOCK, else bail out
if (socket_path == NULL) {
socket_path = getenv("I3SOCK");
if (socket_path == NULL) {
fprintf(stderr, "SWAYSOCK or I3SOCK not set\n");
exit(1);
}
}
}
if (sockfd == -1) {
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("socket");
exit(1);
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
perror("connect");
close(sockfd);
exit(1);
}
}
// Send "get_inputs" command to Sway
const uint32_t get_inputs_command_type = 100; // IPC command type for "get_inputs"
send_sway_command(sockfd, get_inputs_command_type, get_inputs_command);
// Read Sway's response header
size_t prefix_size = strlen("i3-ipc") + sizeof(uint32_t) + sizeof(uint32_t);
ssize_t num_read = read(sockfd, read_buffer, prefix_size);
if (num_read == -1) {
perror("read");
close(sockfd);
exit(1);
}
// Read Sway's response
num_read = read(sockfd, read_buffer, sizeof(read_buffer));
if (num_read == -1) {
perror("read");
close(sockfd);
exit(1);
}
char *start = strstr(read_buffer, "xkb_active_layout_name");
if (start) {
char *start_quote = strchr(start, '(');
char *end_quote = strchr(start_quote, ')');
if (start_quote && end_quote && start_quote < end_quote) {
size_t len = end_quote - start_quote - 1;
if (len < buffer_size) {
strncpy(buffer, start_quote + 1, len);
buffer[len] = 0; // Null-terminate buffer.
// if value is "US", change to "Qwerty"
if (strcmp(buffer, "US") == 0) {
strncpy(buffer, "Qwerty", buffer_size);
buffer[buffer_size - 1] = 0;
}
found = 1;
}
}
}
// flush rest of response
while (num_read == sizeof(read_buffer)) {
num_read = read(sockfd, read_buffer, sizeof(read_buffer));
}
if (!found) {
strncpy(buffer, "Unknown", buffer_size);
buffer[buffer_size - 1] = 0;
}
}
void print() {
char wifi[MAX_BUF];
char keyboard[MAX_BUF];
char batcap[MAX_BUF];
char batstat[MAX_BUF];
static char last_message[MAX_BUF];
char message[MAX_BUF];
extract_wifi_ssid(wifi, sizeof(wifi));
extract_keyboard_layout(keyboard, sizeof(keyboard));
read_file("/sys/class/power_supply/BAT0/capacity", batcap, sizeof(batcap));
read_file("/sys/class/power_supply/BAT0/status", batstat, sizeof(batstat));
// only print if it changed since last time
sprintf(message, "%s | %s | %s%% - %s | %s", wifi, keyboard, batcap, batstat, get_time_string());
if (strcmp(message, last_message) == 0) {
return;
}
strncpy(last_message, message, sizeof(last_message));
last_message[sizeof(last_message) - 1] = 0;
printf("%s\n", message);
fflush(stdout);
}
void usage() {
printf("Usage: sway-musli [-1|--once] [-f|--fps <FPS>]\n");
printf(" - Print a stream of status lines to be used with swaybar.\n");
printf(" - If passed -1 or --once, print once and exit.\n");
printf(" - If passed -f or --fps, print at most <FPS> times per second.\n");
printf(" - Default is 30.\n");
printf(" - Example sway config:\n");
printf(" ...\n");
printf(" bar {\n");
printf(" status_command sway-musli\n");
printf(" }\n");
printf(" ...\n");
printf(" # Note: Make sure sway-musli is in your PATH.\n");
}
int main(int argc, char *argv[]) {
// print usage on -h or --help
if (argc == 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) {
usage();
return 0;
}
// if passed argument -1 or --once, print once and exit
if (argc == 2 && (strcmp(argv[1], "-1") == 0 || strcmp(argv[1], "--once") == 0)) {
print();
return 0;
}
// parse fps
int fps = 30;
if (argc == 3 && (strcmp(argv[1], "-f") == 0 || strcmp(argv[1], "--fps") == 0)) {
fps = atoi(argv[2]);
argc = 1;
}
// on any other argument, print usage and exit
if (argc != 1) {
usage();
return 1;
}
while (1) {
print();
// run at provided FPS
usleep(1000000 / fps);
}
}