swaybar: rename state to bar
This commit is contained in:
parent
c6fc0033e1
commit
aa6ad09183
|
@ -10,7 +10,7 @@ add_executable(swaybar
|
|||
main.c
|
||||
config.c
|
||||
render.c
|
||||
state.c
|
||||
bar.c
|
||||
status_line.c
|
||||
ipc.c
|
||||
)
|
||||
|
|
185
swaybar/bar.c
Normal file
185
swaybar/bar.c
Normal file
|
@ -0,0 +1,185 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include "ipc-client.h"
|
||||
#include "list.h"
|
||||
#include "log.h"
|
||||
#include "ipc.h"
|
||||
#include "render.h"
|
||||
#include "config.h"
|
||||
#include "status_line.h"
|
||||
#include "bar.h"
|
||||
|
||||
static void bar_init(struct bar *bar) {
|
||||
bar->config = init_config();
|
||||
bar->status = init_status_line();
|
||||
bar->output = malloc(sizeof(struct output));
|
||||
bar->output->window = NULL;
|
||||
bar->output->registry = NULL;
|
||||
bar->output->workspaces = create_list();
|
||||
bar->output->name = NULL;
|
||||
}
|
||||
|
||||
static void spawn_status_cmd_proc(struct bar *bar) {
|
||||
if (bar->config->status_command) {
|
||||
int pipefd[2];
|
||||
pipe(pipefd);
|
||||
bar->status_command_pid = fork();
|
||||
if (bar->status_command_pid == 0) {
|
||||
close(pipefd[0]);
|
||||
dup2(pipefd[1], STDOUT_FILENO);
|
||||
close(pipefd[1]);
|
||||
char *const cmd[] = {
|
||||
"sh",
|
||||
"-c",
|
||||
bar->config->status_command,
|
||||
NULL,
|
||||
};
|
||||
execvp(cmd[0], cmd);
|
||||
return;
|
||||
}
|
||||
|
||||
close(pipefd[1]);
|
||||
bar->status_read_fd = pipefd[0];
|
||||
fcntl(bar->status_read_fd, F_SETFL, O_NONBLOCK);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id, int desired_output) {
|
||||
/* initialize bar with default values */
|
||||
bar_init(bar);
|
||||
|
||||
bar->output->registry = registry_poll();
|
||||
|
||||
if (!bar->output->registry->desktop_shell) {
|
||||
sway_abort("swaybar requires the compositor to support the desktop-shell extension.");
|
||||
}
|
||||
|
||||
/* connect to sway ipc */
|
||||
bar->ipc_socketfd = ipc_open_socket(socket_path);
|
||||
bar->ipc_event_socketfd = ipc_open_socket(socket_path);
|
||||
|
||||
ipc_bar_init(bar, desired_output, bar_id);
|
||||
|
||||
struct output_state *output = bar->output->registry->outputs->items[desired_output];
|
||||
|
||||
bar->output->window = window_setup(bar->output->registry, output->width, 30, false);
|
||||
if (!bar->output->window) {
|
||||
sway_abort("Failed to create window.");
|
||||
}
|
||||
desktop_shell_set_panel(bar->output->registry->desktop_shell, output->output, bar->output->window->surface);
|
||||
desktop_shell_set_panel_position(bar->output->registry->desktop_shell, bar->config->position);
|
||||
|
||||
/* set font */
|
||||
bar->output->window->font = bar->config->font;
|
||||
|
||||
/* set window height */
|
||||
set_window_height(bar->output->window, bar->config->height);
|
||||
|
||||
/* spawn status command */
|
||||
spawn_status_cmd_proc(bar);
|
||||
}
|
||||
|
||||
void bar_run(struct bar *bar) {
|
||||
fd_set readfds;
|
||||
int activity;
|
||||
bool dirty = true;
|
||||
|
||||
while (1) {
|
||||
if (dirty) {
|
||||
struct output *output = bar->output;
|
||||
if (window_prerender(output->window) && output->window->cairo) {
|
||||
render(output, bar->config, bar->status);
|
||||
window_render(output->window);
|
||||
if (wl_display_dispatch(output->registry->display) == -1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dirty = false;
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(bar->ipc_event_socketfd, &readfds);
|
||||
FD_SET(bar->status_read_fd, &readfds);
|
||||
|
||||
activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
|
||||
if (activity < 0) {
|
||||
sway_log(L_ERROR, "polling failed: %d", errno);
|
||||
}
|
||||
|
||||
if (FD_ISSET(bar->ipc_event_socketfd, &readfds)) {
|
||||
sway_log(L_DEBUG, "Got IPC event.");
|
||||
dirty = handle_ipc_event(bar);
|
||||
}
|
||||
|
||||
if (bar->config->status_command && FD_ISSET(bar->status_read_fd, &readfds)) {
|
||||
sway_log(L_DEBUG, "Got update from status command.");
|
||||
dirty = handle_status_line(bar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void free_workspaces(list_t *workspaces) {
|
||||
int i;
|
||||
for (i = 0; i < workspaces->length; ++i) {
|
||||
struct workspace *ws = workspaces->items[i];
|
||||
free(ws->name);
|
||||
free(ws);
|
||||
}
|
||||
list_free(workspaces);
|
||||
}
|
||||
|
||||
static void free_output(struct output *output) {
|
||||
window_teardown(output->window);
|
||||
if (output->registry) {
|
||||
registry_teardown(output->registry);
|
||||
}
|
||||
|
||||
free(output->name);
|
||||
|
||||
if (output->workspaces) {
|
||||
free_workspaces(output->workspaces);
|
||||
}
|
||||
|
||||
free(output);
|
||||
}
|
||||
|
||||
static void terminate_status_command(pid_t pid) {
|
||||
if (pid) {
|
||||
// terminate status_command process
|
||||
int ret = kill(pid, SIGTERM);
|
||||
if (ret != 0) {
|
||||
sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", pid);
|
||||
} else {
|
||||
int status;
|
||||
waitpid(pid, &status, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void bar_teardown(struct bar *bar) {
|
||||
free_config(bar->config);
|
||||
free_output(bar->output);
|
||||
free_status_line(bar->status);
|
||||
|
||||
/* close sockets/pipes */
|
||||
if (bar->status_read_fd) {
|
||||
close(bar->status_read_fd);
|
||||
}
|
||||
|
||||
if (bar->ipc_socketfd) {
|
||||
close(bar->ipc_socketfd);
|
||||
}
|
||||
|
||||
if (bar->ipc_event_socketfd) {
|
||||
close(bar->ipc_event_socketfd);
|
||||
}
|
||||
|
||||
/* terminate status command process */
|
||||
terminate_status_command(bar->status_command_pid);
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
#ifndef _SWAYBAR_STATE_H
|
||||
#define _SWAYBAR_STATE_H
|
||||
#ifndef _SWAYBAR_BAR_H
|
||||
#define _SWAYBAR_BAR_H
|
||||
|
||||
#include "client/registry.h"
|
||||
#include "client/window.h"
|
||||
#include "list.h"
|
||||
|
||||
struct swaybar_state {
|
||||
struct swaybar_config *config;
|
||||
struct bar {
|
||||
struct config *config;
|
||||
struct status_line *status;
|
||||
struct output *output;
|
||||
/* list_t *outputs; */
|
||||
|
@ -33,14 +33,14 @@ struct workspace {
|
|||
};
|
||||
|
||||
/**
|
||||
* Setup state.
|
||||
* Setup bar.
|
||||
*/
|
||||
void state_setup(struct swaybar_state *state, const char *socket_path, const char *bar_id, int desired_output);
|
||||
void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id, int desired_output);
|
||||
|
||||
/**
|
||||
* State mainloop.
|
||||
* Bar mainloop.
|
||||
*/
|
||||
void state_run(struct swaybar_state *state);
|
||||
void bar_run(struct bar *bar);
|
||||
|
||||
/**
|
||||
* free workspace list.
|
||||
|
@ -48,8 +48,8 @@ void state_run(struct swaybar_state *state);
|
|||
void free_workspaces(list_t *workspaces);
|
||||
|
||||
/**
|
||||
* Teardown state.
|
||||
* Teardown bar.
|
||||
*/
|
||||
void state_teardown(struct swaybar_state *state);
|
||||
void bar_teardown(struct bar *bar);
|
||||
|
||||
#endif /* _SWAYBAR_STATE_H */
|
||||
#endif /* _SWAYBAR_BAR_H */
|
|
@ -41,8 +41,8 @@ char *parse_font(const char *font) {
|
|||
return new_font;
|
||||
}
|
||||
|
||||
struct swaybar_config *init_config() {
|
||||
struct swaybar_config *config = calloc(1, sizeof(struct swaybar_config));
|
||||
struct config *init_config() {
|
||||
struct config *config = calloc(1, sizeof(struct config));
|
||||
config->status_command = NULL;
|
||||
config->position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM;
|
||||
config->font = strdup("monospace 10");
|
||||
|
@ -83,7 +83,7 @@ struct swaybar_config *init_config() {
|
|||
return config;
|
||||
}
|
||||
|
||||
void free_config(struct swaybar_config *config) {
|
||||
void free_config(struct config *config) {
|
||||
free(config->status_command);
|
||||
free(config->font);
|
||||
free(config->mode);
|
||||
|
|
|
@ -16,7 +16,7 @@ struct box_colors {
|
|||
/**
|
||||
* Swaybar config.
|
||||
*/
|
||||
struct swaybar_config {
|
||||
struct config {
|
||||
char *status_command;
|
||||
uint32_t position;
|
||||
char *font;
|
||||
|
@ -59,11 +59,11 @@ char *parse_font(const char *font);
|
|||
/**
|
||||
* Initialize default sway config.
|
||||
*/
|
||||
struct swaybar_config *init_config();
|
||||
struct config *init_config();
|
||||
|
||||
/**
|
||||
* Free config struct.
|
||||
*/
|
||||
void free_config(struct swaybar_config *config);
|
||||
void free_config(struct config *config);
|
||||
|
||||
#endif /* _SWAYBAR_CONFIG_H */
|
||||
|
|
|
@ -7,14 +7,14 @@
|
|||
#include "config.h"
|
||||
#include "ipc.h"
|
||||
|
||||
static void ipc_parse_config(struct swaybar_config *config, const char *payload) {
|
||||
static void ipc_parse_config(struct config *config, const char *payload) {
|
||||
json_object *bar_config = json_tokener_parse(payload);
|
||||
json_object *tray_output, *mode, *hidden_state, *position, *status_command;
|
||||
json_object *tray_output, *mode, *hidden_bar, *position, *status_command;
|
||||
json_object *font, *bar_height, *workspace_buttons, *strip_workspace_numbers;
|
||||
json_object *binding_mode_indicator, *verbose, *colors, *sep_symbol;
|
||||
json_object_object_get_ex(bar_config, "tray_output", &tray_output);
|
||||
json_object_object_get_ex(bar_config, "mode", &mode);
|
||||
json_object_object_get_ex(bar_config, "hidden_state", &hidden_state);
|
||||
json_object_object_get_ex(bar_config, "hidden_bar", &hidden_bar);
|
||||
json_object_object_get_ex(bar_config, "position", &position);
|
||||
json_object_object_get_ex(bar_config, "status_command", &status_command);
|
||||
json_object_object_get_ex(bar_config, "font", &font);
|
||||
|
@ -150,14 +150,14 @@ static void ipc_parse_config(struct swaybar_config *config, const char *payload)
|
|||
json_object_put(bar_config);
|
||||
}
|
||||
|
||||
static void ipc_update_workspaces(struct swaybar_state *state) {
|
||||
if (state->output->workspaces) {
|
||||
free_workspaces(state->output->workspaces);
|
||||
static void ipc_update_workspaces(struct bar *bar) {
|
||||
if (bar->output->workspaces) {
|
||||
free_workspaces(bar->output->workspaces);
|
||||
}
|
||||
state->output->workspaces = create_list();
|
||||
bar->output->workspaces = create_list();
|
||||
|
||||
uint32_t len = 0;
|
||||
char *res = ipc_single_command(state->ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len);
|
||||
char *res = ipc_single_command(bar->ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len);
|
||||
json_object *results = json_tokener_parse(res);
|
||||
if (!results) {
|
||||
free(res);
|
||||
|
@ -178,14 +178,14 @@ static void ipc_update_workspaces(struct swaybar_state *state) {
|
|||
json_object_object_get_ex(ws_json, "output", &out);
|
||||
json_object_object_get_ex(ws_json, "urgent", &urgent);
|
||||
|
||||
if (strcmp(json_object_get_string(out), state->output->name) == 0) {
|
||||
if (strcmp(json_object_get_string(out), bar->output->name) == 0) {
|
||||
struct workspace *ws = malloc(sizeof(struct workspace));
|
||||
ws->num = json_object_get_int(num);
|
||||
ws->name = strdup(json_object_get_string(name));
|
||||
ws->visible = json_object_get_boolean(visible);
|
||||
ws->focused = json_object_get_boolean(focused);
|
||||
ws->urgent = json_object_get_boolean(urgent);
|
||||
list_add(state->output->workspaces, ws);
|
||||
list_add(bar->output->workspaces, ws);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -193,36 +193,36 @@ static void ipc_update_workspaces(struct swaybar_state *state) {
|
|||
free(res);
|
||||
}
|
||||
|
||||
void ipc_bar_init(struct swaybar_state *state, int outputi, const char *bar_id) {
|
||||
void ipc_bar_init(struct bar *bar, int outputi, const char *bar_id) {
|
||||
uint32_t len = 0;
|
||||
char *res = ipc_single_command(state->ipc_socketfd, IPC_GET_OUTPUTS, NULL, &len);
|
||||
char *res = ipc_single_command(bar->ipc_socketfd, IPC_GET_OUTPUTS, NULL, &len);
|
||||
json_object *outputs = json_tokener_parse(res);
|
||||
json_object *info = json_object_array_get_idx(outputs, outputi);
|
||||
json_object *name;
|
||||
json_object_object_get_ex(info, "name", &name);
|
||||
state->output->name = strdup(json_object_get_string(name));
|
||||
bar->output->name = strdup(json_object_get_string(name));
|
||||
free(res);
|
||||
json_object_put(outputs);
|
||||
|
||||
len = strlen(bar_id);
|
||||
res = ipc_single_command(state->ipc_socketfd, IPC_GET_BAR_CONFIG, bar_id, &len);
|
||||
res = ipc_single_command(bar->ipc_socketfd, IPC_GET_BAR_CONFIG, bar_id, &len);
|
||||
|
||||
ipc_parse_config(state->config, res);
|
||||
ipc_parse_config(bar->config, res);
|
||||
free(res);
|
||||
|
||||
const char *subscribe_json = "[ \"workspace\", \"mode\" ]";
|
||||
len = strlen(subscribe_json);
|
||||
res = ipc_single_command(state->ipc_event_socketfd, IPC_SUBSCRIBE, subscribe_json, &len);
|
||||
res = ipc_single_command(bar->ipc_event_socketfd, IPC_SUBSCRIBE, subscribe_json, &len);
|
||||
free(res);
|
||||
|
||||
ipc_update_workspaces(state);
|
||||
ipc_update_workspaces(bar);
|
||||
}
|
||||
|
||||
bool handle_ipc_event(struct swaybar_state *state) {
|
||||
struct ipc_response *resp = ipc_recv_response(state->ipc_event_socketfd);
|
||||
bool handle_ipc_event(struct bar *bar) {
|
||||
struct ipc_response *resp = ipc_recv_response(bar->ipc_event_socketfd);
|
||||
switch (resp->type) {
|
||||
case IPC_EVENT_WORKSPACE:
|
||||
ipc_update_workspaces(state);
|
||||
ipc_update_workspaces(bar);
|
||||
break;
|
||||
case IPC_EVENT_MODE: {
|
||||
json_object *result = json_tokener_parse(resp->payload);
|
||||
|
@ -235,11 +235,11 @@ bool handle_ipc_event(struct swaybar_state *state) {
|
|||
if (json_object_object_get_ex(result, "change", &json_change)) {
|
||||
const char *change = json_object_get_string(json_change);
|
||||
|
||||
free(state->config->mode);
|
||||
free(bar->config->mode);
|
||||
if (strcmp(change, "default") == 0) {
|
||||
state->config->mode = NULL;
|
||||
bar->config->mode = NULL;
|
||||
} else {
|
||||
state->config->mode = strdup(change);
|
||||
bar->config->mode = strdup(change);
|
||||
}
|
||||
} else {
|
||||
sway_log(L_ERROR, "failed to parse response");
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
#ifndef _SWAYBAR_IPC_H
|
||||
#define _SWAYBAR_IPC_H
|
||||
|
||||
#include "state.h"
|
||||
#include "bar.h"
|
||||
|
||||
/**
|
||||
* Initialize ipc connection to sway and get sway state, outputs, bar_config.
|
||||
*/
|
||||
void ipc_bar_init(struct swaybar_state *state, int outputi, const char *bar_id);
|
||||
void ipc_bar_init(struct bar *bar, int outputi, const char *bar_id);
|
||||
|
||||
/**
|
||||
* Handle ipc event from sway.
|
||||
*/
|
||||
bool handle_ipc_event(struct swaybar_state *state);
|
||||
bool handle_ipc_event(struct bar *bar);
|
||||
|
||||
#endif /* _SWAYBAR_IPC_H */
|
||||
|
||||
|
|
|
@ -5,17 +5,18 @@
|
|||
#include <getopt.h>
|
||||
#include "ipc-client.h"
|
||||
#include "log.h"
|
||||
#include "state.h"
|
||||
#include "bar.h"
|
||||
|
||||
struct swaybar_state state;
|
||||
/* global bar state */
|
||||
struct bar swaybar;
|
||||
|
||||
void sway_terminate(void) {
|
||||
state_teardown(&state);
|
||||
bar_teardown(&swaybar);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void sig_handler(int signal) {
|
||||
state_teardown(&state);
|
||||
bar_teardown(&swaybar);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
@ -102,15 +103,15 @@ int main(int argc, char **argv) {
|
|||
|
||||
signal(SIGTERM, sig_handler);
|
||||
|
||||
state_setup(&state, socket_path, bar_id, desired_output);
|
||||
bar_setup(&swaybar, socket_path, bar_id, desired_output);
|
||||
|
||||
free(socket_path);
|
||||
free(bar_id);
|
||||
|
||||
state_run(&state);
|
||||
bar_run(&swaybar);
|
||||
|
||||
// gracefully shutdown swaybar and status_command
|
||||
state_teardown(&state);
|
||||
bar_teardown(&swaybar);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ static void render_sharp_line(cairo_t *cairo, uint32_t color, double x, double y
|
|||
}
|
||||
}
|
||||
|
||||
static void render_block(struct window *window, struct swaybar_config *config, struct status_block *block, double *x, bool edge) {
|
||||
static void render_block(struct window *window, struct config *config, struct status_block *block, double *x, bool edge) {
|
||||
int width, height, sep_width;
|
||||
get_text_size(window, &width, &height, "%s", block->full_text);
|
||||
|
||||
|
@ -203,7 +203,7 @@ static char *handle_workspace_number(bool strip_num, const char *ws_name) {
|
|||
return strdup(ws_name);
|
||||
}
|
||||
|
||||
static void render_workspace_button(struct window *window, struct swaybar_config *config, struct workspace *ws, double *x) {
|
||||
static void render_workspace_button(struct window *window, struct config *config, struct workspace *ws, double *x) {
|
||||
// strip workspace numbers if required
|
||||
char *name = handle_workspace_number(config->strip_workspace_numbers, ws->name);
|
||||
|
||||
|
@ -242,7 +242,7 @@ static void render_workspace_button(struct window *window, struct swaybar_config
|
|||
free(name);
|
||||
}
|
||||
|
||||
static void render_binding_mode_indicator(struct window *window, struct swaybar_config *config, double pos) {
|
||||
static void render_binding_mode_indicator(struct window *window, struct config *config, double pos) {
|
||||
int width, height;
|
||||
get_text_size(window, &width, &height, "%s", config->mode);
|
||||
|
||||
|
@ -264,7 +264,7 @@ static void render_binding_mode_indicator(struct window *window, struct swaybar_
|
|||
pango_printf(window, "%s", config->mode);
|
||||
}
|
||||
|
||||
void render(struct output *output, struct swaybar_config *config, struct status_line *line) {
|
||||
void render(struct output *output, struct config *config, struct status_line *line) {
|
||||
int i;
|
||||
|
||||
struct window *window = output->window;
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
#define _SWAYBAR_RENDER_H
|
||||
|
||||
#include "config.h"
|
||||
#include "state.h"
|
||||
#include "bar.h"
|
||||
|
||||
/**
|
||||
* Render swaybar.
|
||||
*/
|
||||
void render(struct output *output, struct swaybar_config *config, struct status_line *line);
|
||||
void render(struct output *output, struct config *config, struct status_line *line);
|
||||
|
||||
/**
|
||||
* Set window height and modify internal spacing accordingly.
|
||||
|
|
185
swaybar/state.c
185
swaybar/state.c
|
@ -1,185 +0,0 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include "ipc-client.h"
|
||||
#include "list.h"
|
||||
#include "log.h"
|
||||
#include "ipc.h"
|
||||
#include "render.h"
|
||||
#include "config.h"
|
||||
#include "status_line.h"
|
||||
#include "state.h"
|
||||
|
||||
static void state_init(struct swaybar_state *state) {
|
||||
state->config = init_config();
|
||||
state->status = init_status_line();
|
||||
state->output = malloc(sizeof(struct output));
|
||||
state->output->window = NULL;
|
||||
state->output->registry = NULL;
|
||||
state->output->workspaces = create_list();
|
||||
state->output->name = NULL;
|
||||
}
|
||||
|
||||
static void spawn_status_cmd_proc(struct swaybar_state *state) {
|
||||
if (state->config->status_command) {
|
||||
int pipefd[2];
|
||||
pipe(pipefd);
|
||||
state->status_command_pid = fork();
|
||||
if (state->status_command_pid == 0) {
|
||||
close(pipefd[0]);
|
||||
dup2(pipefd[1], STDOUT_FILENO);
|
||||
close(pipefd[1]);
|
||||
char *const cmd[] = {
|
||||
"sh",
|
||||
"-c",
|
||||
state->config->status_command,
|
||||
NULL,
|
||||
};
|
||||
execvp(cmd[0], cmd);
|
||||
return;
|
||||
}
|
||||
|
||||
close(pipefd[1]);
|
||||
state->status_read_fd = pipefd[0];
|
||||
fcntl(state->status_read_fd, F_SETFL, O_NONBLOCK);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void state_setup(struct swaybar_state *state, const char *socket_path, const char *bar_id, int desired_output) {
|
||||
/* initialize state with default values */
|
||||
state_init(state);
|
||||
|
||||
state->output->registry = registry_poll();
|
||||
|
||||
if (!state->output->registry->desktop_shell) {
|
||||
sway_abort("swaybar requires the compositor to support the desktop-shell extension.");
|
||||
}
|
||||
|
||||
/* connect to sway ipc */
|
||||
state->ipc_socketfd = ipc_open_socket(socket_path);
|
||||
state->ipc_event_socketfd = ipc_open_socket(socket_path);
|
||||
|
||||
ipc_bar_init(state, desired_output, bar_id);
|
||||
|
||||
struct output_state *output = state->output->registry->outputs->items[desired_output];
|
||||
|
||||
state->output->window = window_setup(state->output->registry, output->width, 30, false);
|
||||
if (!state->output->window) {
|
||||
sway_abort("Failed to create window.");
|
||||
}
|
||||
desktop_shell_set_panel(state->output->registry->desktop_shell, output->output, state->output->window->surface);
|
||||
desktop_shell_set_panel_position(state->output->registry->desktop_shell, state->config->position);
|
||||
|
||||
/* set font */
|
||||
state->output->window->font = state->config->font;
|
||||
|
||||
/* set window height */
|
||||
set_window_height(state->output->window, state->config->height);
|
||||
|
||||
/* spawn status command */
|
||||
spawn_status_cmd_proc(state);
|
||||
}
|
||||
|
||||
void state_run(struct swaybar_state *state) {
|
||||
fd_set readfds;
|
||||
int activity;
|
||||
bool dirty = true;
|
||||
|
||||
while (1) {
|
||||
if (dirty) {
|
||||
struct output *output = state->output;
|
||||
if (window_prerender(output->window) && output->window->cairo) {
|
||||
render(output, state->config, state->status);
|
||||
window_render(output->window);
|
||||
if (wl_display_dispatch(output->registry->display) == -1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dirty = false;
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(state->ipc_event_socketfd, &readfds);
|
||||
FD_SET(state->status_read_fd, &readfds);
|
||||
|
||||
activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
|
||||
if (activity < 0) {
|
||||
sway_log(L_ERROR, "polling failed: %d", errno);
|
||||
}
|
||||
|
||||
if (FD_ISSET(state->ipc_event_socketfd, &readfds)) {
|
||||
sway_log(L_DEBUG, "Got IPC event.");
|
||||
dirty = handle_ipc_event(state);
|
||||
}
|
||||
|
||||
if (state->config->status_command && FD_ISSET(state->status_read_fd, &readfds)) {
|
||||
sway_log(L_DEBUG, "Got update from status command.");
|
||||
dirty = handle_status_line(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void free_workspaces(list_t *workspaces) {
|
||||
int i;
|
||||
for (i = 0; i < workspaces->length; ++i) {
|
||||
struct workspace *ws = workspaces->items[i];
|
||||
free(ws->name);
|
||||
free(ws);
|
||||
}
|
||||
list_free(workspaces);
|
||||
}
|
||||
|
||||
static void free_output(struct output *output) {
|
||||
window_teardown(output->window);
|
||||
if (output->registry) {
|
||||
registry_teardown(output->registry);
|
||||
}
|
||||
|
||||
free(output->name);
|
||||
|
||||
if (output->workspaces) {
|
||||
free_workspaces(output->workspaces);
|
||||
}
|
||||
|
||||
free(output);
|
||||
}
|
||||
|
||||
static void terminate_status_command(pid_t pid) {
|
||||
if (pid) {
|
||||
// terminate status_command process
|
||||
int ret = kill(pid, SIGTERM);
|
||||
if (ret != 0) {
|
||||
sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", pid);
|
||||
} else {
|
||||
int status;
|
||||
waitpid(pid, &status, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void state_teardown(struct swaybar_state *state) {
|
||||
free_config(state->config);
|
||||
free_output(state->output);
|
||||
free_status_line(state->status);
|
||||
|
||||
/* close sockets/pipes */
|
||||
if (state->status_read_fd) {
|
||||
close(state->status_read_fd);
|
||||
}
|
||||
|
||||
if (state->ipc_socketfd) {
|
||||
close(state->ipc_socketfd);
|
||||
}
|
||||
|
||||
if (state->ipc_event_socketfd) {
|
||||
close(state->ipc_event_socketfd);
|
||||
}
|
||||
|
||||
/* terminate status command process */
|
||||
terminate_status_command(state->status_command_pid);
|
||||
}
|
|
@ -19,7 +19,7 @@ struct {
|
|||
char *parserpos;
|
||||
bool escape;
|
||||
int depth;
|
||||
int state[I3JSON_MAXDEPTH+1];
|
||||
int bar[I3JSON_MAXDEPTH+1];
|
||||
} i3json_state = { 0, NULL, NULL, NULL, false, 0, { I3JSON_UNKNOWN } };
|
||||
|
||||
static char line[1024];
|
||||
|
@ -48,7 +48,7 @@ static void free_status_block(void *item) {
|
|||
free(sb);
|
||||
}
|
||||
|
||||
static void parse_json(struct swaybar_state *state, const char *text) {
|
||||
static void parse_json(struct bar *bar, const char *text) {
|
||||
json_object *results = json_tokener_parse(text);
|
||||
if (!results) {
|
||||
sway_log(L_DEBUG, "Failed to parse json");
|
||||
|
@ -59,12 +59,12 @@ static void parse_json(struct swaybar_state *state, const char *text) {
|
|||
return;
|
||||
}
|
||||
|
||||
if (state->status->block_line) {
|
||||
list_foreach(state->status->block_line, free_status_block);
|
||||
list_free(state->status->block_line);
|
||||
if (bar->status->block_line) {
|
||||
list_foreach(bar->status->block_line, free_status_block);
|
||||
list_free(bar->status->block_line);
|
||||
}
|
||||
|
||||
state->status->block_line = create_list();
|
||||
bar->status->block_line = create_list();
|
||||
|
||||
int i;
|
||||
for (i = 0; i < json_object_array_length(results); ++i) {
|
||||
|
@ -108,7 +108,7 @@ static void parse_json(struct swaybar_state *state, const char *text) {
|
|||
if (color) {
|
||||
new->color = parse_color(json_object_get_string(color));
|
||||
} else {
|
||||
new->color = state->config->colors.statusline;
|
||||
new->color = bar->config->colors.statusline;
|
||||
}
|
||||
|
||||
if (min_width) {
|
||||
|
@ -188,18 +188,18 @@ static void parse_json(struct swaybar_state *state, const char *text) {
|
|||
new->border_right = 1;
|
||||
}
|
||||
|
||||
list_add(state->status->block_line, new);
|
||||
list_add(bar->status->block_line, new);
|
||||
}
|
||||
|
||||
json_object_put(results);
|
||||
}
|
||||
|
||||
// continue parsing from last parserpos
|
||||
static int i3json_parse(struct swaybar_state *st) {
|
||||
static int i3json_parse(struct bar *bar) {
|
||||
char *c = i3json_state.parserpos;
|
||||
int handled = 0;
|
||||
while (*c) {
|
||||
if (i3json_state.state[i3json_state.depth] == I3JSON_STRING) {
|
||||
if (i3json_state.bar[i3json_state.depth] == I3JSON_STRING) {
|
||||
if (!i3json_state.escape && *c == '"') {
|
||||
--i3json_state.depth;
|
||||
}
|
||||
|
@ -211,13 +211,13 @@ static int i3json_parse(struct swaybar_state *st) {
|
|||
if (i3json_state.depth > I3JSON_MAXDEPTH) {
|
||||
sway_abort("JSON too deep");
|
||||
}
|
||||
i3json_state.state[i3json_state.depth] = I3JSON_ARRAY;
|
||||
i3json_state.bar[i3json_state.depth] = I3JSON_ARRAY;
|
||||
if (i3json_state.depth == 2) {
|
||||
i3json_state.line_start = c;
|
||||
}
|
||||
break;
|
||||
case ']':
|
||||
if (i3json_state.state[i3json_state.depth] != I3JSON_ARRAY) {
|
||||
if (i3json_state.bar[i3json_state.depth] != I3JSON_ARRAY) {
|
||||
sway_abort("JSON malformed");
|
||||
}
|
||||
--i3json_state.depth;
|
||||
|
@ -225,7 +225,7 @@ static int i3json_parse(struct swaybar_state *st) {
|
|||
// c[1] is valid since c[0] != '\0'
|
||||
char p = c[1];
|
||||
c[1] = '\0';
|
||||
parse_json(st, i3json_state.line_start);
|
||||
parse_json(bar, i3json_state.line_start);
|
||||
c[1] = p;
|
||||
++handled;
|
||||
i3json_state.line_start = c+1;
|
||||
|
@ -236,7 +236,7 @@ static int i3json_parse(struct swaybar_state *st) {
|
|||
if (i3json_state.depth > I3JSON_MAXDEPTH) {
|
||||
sway_abort("JSON too deep");
|
||||
}
|
||||
i3json_state.state[i3json_state.depth] = I3JSON_STRING;
|
||||
i3json_state.bar[i3json_state.depth] = I3JSON_STRING;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -361,49 +361,49 @@ static void i3json_ensure_free(int min_free) {
|
|||
}
|
||||
|
||||
// append data and parse it.
|
||||
static int i3json_handle_data(struct swaybar_state *st, char *data) {
|
||||
static int i3json_handle_data(struct bar *bar, char *data) {
|
||||
int len = strlen(data);
|
||||
i3json_ensure_free(len);
|
||||
strcpy(i3json_state.parserpos, data);
|
||||
return i3json_parse(st);
|
||||
return i3json_parse(bar);
|
||||
}
|
||||
|
||||
// read data from fd and parse it.
|
||||
static int i3json_handle_fd(struct swaybar_state *state) {
|
||||
static int i3json_handle_fd(struct bar *bar) {
|
||||
i3json_ensure_free(10240);
|
||||
// get fresh data at the end of the buffer
|
||||
int readlen = read(state->status_read_fd, i3json_state.parserpos, 10239);
|
||||
int readlen = read(bar->status_read_fd, i3json_state.parserpos, 10239);
|
||||
if (readlen < 0) {
|
||||
return readlen;
|
||||
}
|
||||
i3json_state.parserpos[readlen] = '\0';
|
||||
return i3json_parse(state);
|
||||
return i3json_parse(bar);
|
||||
}
|
||||
|
||||
bool handle_status_line(struct swaybar_state *state) {
|
||||
bool handle_status_line(struct bar *bar) {
|
||||
bool dirty = false;
|
||||
|
||||
switch (state->status->protocol) {
|
||||
switch (bar->status->protocol) {
|
||||
case I3BAR:
|
||||
sway_log(L_DEBUG, "Got i3bar protocol.");
|
||||
if (i3json_handle_fd(state) > 0) {
|
||||
if (i3json_handle_fd(bar) > 0) {
|
||||
dirty = true;
|
||||
}
|
||||
break;
|
||||
case TEXT:
|
||||
sway_log(L_DEBUG, "Got text protocol.");
|
||||
read_line_tail(state->status_read_fd, line, sizeof(line), line_rest);
|
||||
read_line_tail(bar->status_read_fd, line, sizeof(line), line_rest);
|
||||
dirty = true;
|
||||
state->status->text_line = line;
|
||||
bar->status->text_line = line;
|
||||
break;
|
||||
case UNDEF:
|
||||
sway_log(L_DEBUG, "Detecting protocol...");
|
||||
if (read_line_tail(state->status_read_fd, line, sizeof(line), line_rest) < 0) {
|
||||
if (read_line_tail(bar->status_read_fd, line, sizeof(line), line_rest) < 0) {
|
||||
break;
|
||||
}
|
||||
dirty = true;
|
||||
state->status->text_line = line;
|
||||
state->status->protocol = TEXT;
|
||||
bar->status->text_line = line;
|
||||
bar->status->protocol = TEXT;
|
||||
if (line[0] == '{') {
|
||||
// detect i3bar json protocol
|
||||
json_object *proto = json_tokener_parse(line);
|
||||
|
@ -413,8 +413,8 @@ bool handle_status_line(struct swaybar_state *state) {
|
|||
&& json_object_get_int(version) == 1
|
||||
) {
|
||||
sway_log(L_DEBUG, "Switched to i3bar protocol.");
|
||||
state->status->protocol = I3BAR;
|
||||
i3json_handle_data(state, line_rest);
|
||||
bar->status->protocol = I3BAR;
|
||||
i3json_handle_data(bar, line_rest);
|
||||
}
|
||||
json_object_put(proto);
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include <stdbool.h>
|
||||
|
||||
#include "list.h"
|
||||
#include "state.h"
|
||||
#include "bar.h"
|
||||
|
||||
typedef enum {UNDEF, TEXT, I3BAR} command_protocol;
|
||||
|
||||
|
@ -40,7 +40,7 @@ struct status_line *init_status_line();
|
|||
/**
|
||||
* handle status line activity.
|
||||
*/
|
||||
bool handle_status_line(struct swaybar_state *st);
|
||||
bool handle_status_line(struct bar *bar);
|
||||
|
||||
/**
|
||||
* Free status line struct.
|
||||
|
|
Loading…
Reference in a new issue