aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar/status_line.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2018-03-29 15:16:12 -0400
committerLibravatar Drew DeVault <sir@cmpwn.com>2018-03-29 22:11:08 -0400
commit0d0ab7c5ce148bce841fa0682d04bc7b6c21b902 (patch)
treebc3dcefa0e306ac00d81a0cabbd6a0404d7baff6 /swaybar/status_line.c
parentIterate over workspaces backwards (diff)
downloadsway-0d0ab7c5ce148bce841fa0682d04bc7b6c21b902.tar.gz
sway-0d0ab7c5ce148bce841fa0682d04bc7b6c21b902.tar.zst
sway-0d0ab7c5ce148bce841fa0682d04bc7b6c21b902.zip
Implement status line
Does not yet support i3bar json protocol
Diffstat (limited to 'swaybar/status_line.c')
-rw-r--r--swaybar/status_line.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/swaybar/status_line.c b/swaybar/status_line.c
new file mode 100644
index 00000000..ff668c9c
--- /dev/null
+++ b/swaybar/status_line.c
@@ -0,0 +1,78 @@
1#define _POSIX_C_SOURCE
2#include <fcntl.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdio.h>
6#include <unistd.h>
7#include <wlr/util/log.h>
8#include "swaybar/config.h"
9#include "swaybar/status_line.h"
10#include "readline.h"
11
12bool handle_status_readable(struct status_line *status) {
13 char *line = read_line_buffer(status->read,
14 status->buffer, status->buffer_size);
15 switch (status->protocol) {
16 case PROTOCOL_I3BAR:
17 // TODO
18 break;
19 case PROTOCOL_TEXT:
20 status->text = line;
21 return true;
22 case PROTOCOL_UNDEF:
23 if (!line) {
24 return false;
25 }
26 if (line[0] == '{') {
27 // TODO: JSON
28 } else {
29 status->text = line;
30 status->protocol = PROTOCOL_TEXT;
31 }
32 return false;
33 }
34 return false;
35}
36
37struct status_line *status_line_init(char *cmd) {
38 struct status_line *status = calloc(1, sizeof(struct status_line));
39 status->buffer_size = 4096;
40 status->buffer = malloc(status->buffer_size);
41
42 int pipe_read_fd[2];
43 int pipe_write_fd[2];
44 if (pipe(pipe_read_fd) != 0 || pipe(pipe_write_fd) != 0) {
45 wlr_log(L_ERROR, "Unable to create pipes for status_command fork");
46 exit(1);
47 }
48
49 status->pid = fork();
50 if (status->pid == 0) {
51 dup2(pipe_read_fd[1], STDOUT_FILENO);
52 close(pipe_read_fd[0]);
53 close(pipe_read_fd[1]);
54
55 dup2(pipe_write_fd[0], STDIN_FILENO);
56 close(pipe_write_fd[0]);
57 close(pipe_write_fd[1]);
58
59 char *const _cmd[] = { "sh", "-c", cmd, NULL, };
60 execvp(_cmd[0], _cmd);
61 exit(1);
62 }
63
64 close(pipe_read_fd[1]);
65 status->read_fd = pipe_read_fd[0];
66 fcntl(status->read_fd, F_SETFL, O_NONBLOCK);
67 close(pipe_write_fd[0]);
68 status->write_fd = pipe_write_fd[1];
69 fcntl(status->write_fd, F_SETFL, O_NONBLOCK);
70
71 status->read = fdopen(status->read_fd, "r");
72 status->write = fdopen(status->write_fd, "w");
73 return status;
74}
75
76void status_line_free(struct status_line *line) {
77 free(line);
78}