parent
							
								
									718502c815
								
							
						
					
					
						commit
						0d0ab7c5ce
					
				| @ -1,61 +1,30 @@ | ||||
| #ifndef _SWAYBAR_STATUS_LINE_H | ||||
| #define _SWAYBAR_STATUS_LINE_H | ||||
| 
 | ||||
| #include <stdint.h> | ||||
| #include <stdio.h> | ||||
| #include <stdbool.h> | ||||
| 
 | ||||
| #include "list.h" | ||||
| #include "bar.h" | ||||
| 
 | ||||
| typedef enum {UNDEF, TEXT, I3BAR} command_protocol; | ||||
| enum status_protocol { | ||||
| 	PROTOCOL_UNDEF, | ||||
| 	PROTOCOL_TEXT, | ||||
| 	PROTOCOL_I3BAR, | ||||
| }; | ||||
| 
 | ||||
| struct status_line { | ||||
| 	list_t *block_line; | ||||
| 	const char *text_line; | ||||
| 	command_protocol protocol; | ||||
| 	bool click_events; | ||||
| }; | ||||
| 	pid_t pid; | ||||
| 	int read_fd, write_fd; | ||||
| 	FILE *read, *write; | ||||
| 
 | ||||
| struct status_block { | ||||
| 	char *full_text, *short_text, *align; | ||||
| 	bool urgent; | ||||
| 	uint32_t color; | ||||
| 	int min_width; | ||||
| 	char *name, *instance; | ||||
| 	bool separator; | ||||
| 	int separator_block_width; | ||||
| 	bool markup; | ||||
| 	// Airblader features
 | ||||
| 	uint32_t background; | ||||
| 	uint32_t border; | ||||
| 	int border_top; | ||||
| 	int border_bottom; | ||||
| 	int border_left; | ||||
| 	int border_right; | ||||
| 	enum status_protocol protocol; | ||||
| 	const char *text; | ||||
| 
 | ||||
| 	// Set during rendering
 | ||||
| 	int x; | ||||
| 	int width; | ||||
| 	char *buffer; | ||||
| 	size_t buffer_size; | ||||
| }; | ||||
| 
 | ||||
| /**
 | ||||
|  * Initialize status line struct. | ||||
|  */ | ||||
| struct status_line *init_status_line(); | ||||
| 
 | ||||
| /**
 | ||||
|  * handle status line activity. | ||||
|  */ | ||||
| bool handle_status_line(struct bar *bar); | ||||
| 
 | ||||
| /**
 | ||||
|  * Handle mouse clicks. | ||||
|  */ | ||||
| bool status_line_mouse_event(struct bar *bar, int x, int y, uint32_t button); | ||||
| 
 | ||||
| /**
 | ||||
|  * Free status line struct. | ||||
|  */ | ||||
| void free_status_line(struct status_line *line); | ||||
| struct status_line *status_line_init(char *cmd); | ||||
| void status_line_free(struct status_line *status); | ||||
| bool handle_status_readable(struct status_line *status); | ||||
| 
 | ||||
| #endif /* _SWAYBAR_STATUS_LINE_H */ | ||||
| #endif | ||||
|  | ||||
| @ -0,0 +1,78 @@ | ||||
| #define _POSIX_C_SOURCE | ||||
| #include <fcntl.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #include <stdio.h> | ||||
| #include <unistd.h> | ||||
| #include <wlr/util/log.h> | ||||
| #include "swaybar/config.h" | ||||
| #include "swaybar/status_line.h" | ||||
| #include "readline.h" | ||||
| 
 | ||||
| bool handle_status_readable(struct status_line *status) { | ||||
| 	char *line = read_line_buffer(status->read, | ||||
| 			status->buffer, status->buffer_size); | ||||
| 	switch (status->protocol) { | ||||
| 	case PROTOCOL_I3BAR: | ||||
| 		// TODO
 | ||||
| 		break; | ||||
| 	case PROTOCOL_TEXT: | ||||
| 		status->text = line; | ||||
| 		return true; | ||||
| 	case PROTOCOL_UNDEF: | ||||
| 		if (!line) { | ||||
| 			return false; | ||||
| 		} | ||||
| 		if (line[0] == '{') { | ||||
| 			// TODO: JSON
 | ||||
| 		} else { | ||||
| 			status->text = line; | ||||
| 			status->protocol = PROTOCOL_TEXT; | ||||
| 		} | ||||
| 		return false; | ||||
| 	} | ||||
| 	return false; | ||||
| } | ||||
| 
 | ||||
| struct status_line *status_line_init(char *cmd) { | ||||
| 	struct status_line *status = calloc(1, sizeof(struct status_line)); | ||||
| 	status->buffer_size = 4096; | ||||
| 	status->buffer = malloc(status->buffer_size); | ||||
| 
 | ||||
| 	int pipe_read_fd[2]; | ||||
| 	int pipe_write_fd[2]; | ||||
| 	if (pipe(pipe_read_fd) != 0 || pipe(pipe_write_fd) != 0) { | ||||
| 		wlr_log(L_ERROR, "Unable to create pipes for status_command fork"); | ||||
| 		exit(1); | ||||
| 	} | ||||
| 
 | ||||
| 	status->pid = fork(); | ||||
| 	if (status->pid == 0) { | ||||
| 		dup2(pipe_read_fd[1], STDOUT_FILENO); | ||||
| 		close(pipe_read_fd[0]); | ||||
| 		close(pipe_read_fd[1]); | ||||
| 
 | ||||
| 		dup2(pipe_write_fd[0], STDIN_FILENO); | ||||
| 		close(pipe_write_fd[0]); | ||||
| 		close(pipe_write_fd[1]); | ||||
| 
 | ||||
| 		char *const _cmd[] = { "sh", "-c", cmd, NULL, }; | ||||
| 		execvp(_cmd[0], _cmd); | ||||
| 		exit(1); | ||||
| 	} | ||||
| 
 | ||||
| 	close(pipe_read_fd[1]); | ||||
| 	status->read_fd = pipe_read_fd[0]; | ||||
| 	fcntl(status->read_fd, F_SETFL, O_NONBLOCK); | ||||
| 	close(pipe_write_fd[0]); | ||||
| 	status->write_fd = pipe_write_fd[1]; | ||||
| 	fcntl(status->write_fd, F_SETFL, O_NONBLOCK); | ||||
| 
 | ||||
| 	status->read = fdopen(status->read_fd, "r"); | ||||
| 	status->write = fdopen(status->write_fd, "w"); | ||||
| 	return status; | ||||
| } | ||||
| 
 | ||||
| void status_line_free(struct status_line *line) { | ||||
| 	free(line); | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue