mirror of
https://cagebreak.project-repo.co/cagebreak.git
synced 2026-08-22 13:34:18 +02:00
Apply clang-format
This commit is contained in:
parent
e98378bffd
commit
816283cdb1
5 changed files with 193 additions and 122 deletions
|
|
@ -47,6 +47,7 @@
|
|||
#endif
|
||||
|
||||
#include "idle_inhibit_v1.h"
|
||||
#include "ipc_server.h"
|
||||
#include "keybinding.h"
|
||||
#include "message.h"
|
||||
#include "output.h"
|
||||
|
|
@ -55,7 +56,6 @@
|
|||
#include "server.h"
|
||||
#include "view.h"
|
||||
#include "xdg_shell.h"
|
||||
#include "ipc_server.h"
|
||||
#if CG_HAS_XWAYLAND
|
||||
#include "xwayland.h"
|
||||
#endif
|
||||
|
|
@ -290,7 +290,8 @@ main(int argc, char *argv[]) {
|
|||
server.modes[1] = strdup("root");
|
||||
server.modes[2] = strdup("resize");
|
||||
server.modes[3] = NULL;
|
||||
if(server.modes[0] == NULL || server.modes[1] == NULL || server.modes[2] == NULL) {
|
||||
if(server.modes[0] == NULL || server.modes[1] == NULL ||
|
||||
server.modes[2] == NULL) {
|
||||
wlr_log(WLR_ERROR, "Error allocating default modes");
|
||||
return 1;
|
||||
}
|
||||
|
|
|
|||
141
ipc_server.c
141
ipc_server.c
|
|
@ -10,21 +10,22 @@
|
|||
#define _POSIX_C_SOURCE 200112L
|
||||
|
||||
#include "ipc_server.h"
|
||||
#include "server.h"
|
||||
#include "parse.h"
|
||||
#include "message.h"
|
||||
#include "parse.h"
|
||||
#include "server.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <wlr/util/log.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void handle_display_destroy(struct wl_listener *listener, void *data) {
|
||||
struct cg_ipc_handle *ipc = wl_container_of(listener, ipc,display_destroy);
|
||||
if (ipc->event_source != NULL) {
|
||||
static void
|
||||
handle_display_destroy(struct wl_listener *listener, void *data) {
|
||||
struct cg_ipc_handle *ipc = wl_container_of(listener, ipc, display_destroy);
|
||||
if(ipc->event_source != NULL) {
|
||||
wl_event_source_remove(ipc->event_source);
|
||||
}
|
||||
close(ipc->socket);
|
||||
|
|
@ -40,18 +41,19 @@ static void handle_display_destroy(struct wl_listener *listener, void *data) {
|
|||
wl_list_remove(&ipc->display_destroy.link);
|
||||
}
|
||||
|
||||
int ipc_init(struct cg_server *server) {
|
||||
int
|
||||
ipc_init(struct cg_server *server) {
|
||||
struct cg_ipc_handle *ipc = &server->ipc;
|
||||
ipc->socket = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (ipc->socket == -1) {
|
||||
if(ipc->socket == -1) {
|
||||
wlr_log(WLR_ERROR, "Unable to create IPC socket");
|
||||
return -1;
|
||||
}
|
||||
if (fcntl(ipc->socket, F_SETFD, FD_CLOEXEC) == -1) {
|
||||
wlr_log(WLR_ERROR,"Unable to set CLOEXEC on IPC socket");
|
||||
if(fcntl(ipc->socket, F_SETFD, FD_CLOEXEC) == -1) {
|
||||
wlr_log(WLR_ERROR, "Unable to set CLOEXEC on IPC socket");
|
||||
return -1;
|
||||
}
|
||||
if (fcntl(ipc->socket, F_SETFL, O_NONBLOCK) == -1) {
|
||||
if(fcntl(ipc->socket, F_SETFL, O_NONBLOCK) == -1) {
|
||||
wlr_log(WLR_ERROR, "Unable to set NONBLOCK on IPC socket");
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -63,29 +65,32 @@ int ipc_init(struct cg_server *server) {
|
|||
return -1;
|
||||
}
|
||||
|
||||
ipc->sockaddr->sun_family=AF_UNIX;
|
||||
ipc->sockaddr->sun_family = AF_UNIX;
|
||||
int max_path_size = sizeof(ipc->sockaddr->sun_path);
|
||||
const char *sockdir=getenv("XDG_RUNTIME_DIR");
|
||||
const char *sockdir = getenv("XDG_RUNTIME_DIR");
|
||||
if(sockdir == NULL) {
|
||||
sockdir = "/tmp";
|
||||
}
|
||||
|
||||
if (max_path_size <= snprintf(ipc->sockaddr->sun_path, max_path_size,
|
||||
"%s/cagebreak-ipc.%i.%i.sock", sockdir, getuid(), getpid())) {
|
||||
wlr_log(WLR_ERROR,"Unable to write socket path to ipc->sockaddr->sun_path. Path too long");
|
||||
if(max_path_size <= snprintf(ipc->sockaddr->sun_path, max_path_size,
|
||||
"%s/cagebreak-ipc.%i.%i.sock", sockdir,
|
||||
getuid(), getpid())) {
|
||||
wlr_log(WLR_ERROR, "Unable to write socket path to "
|
||||
"ipc->sockaddr->sun_path. Path too long");
|
||||
free(ipc->sockaddr);
|
||||
return -1;
|
||||
}
|
||||
|
||||
unlink(ipc->sockaddr->sun_path);
|
||||
|
||||
if (bind(ipc->socket, (struct sockaddr *)ipc->sockaddr, sizeof(*ipc->sockaddr)) == -1) {
|
||||
if(bind(ipc->socket, (struct sockaddr *)ipc->sockaddr,
|
||||
sizeof(*ipc->sockaddr)) == -1) {
|
||||
wlr_log(WLR_ERROR, "Unable to bind IPC socket");
|
||||
free(ipc->sockaddr);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (listen(ipc->socket, 3) == -1) {
|
||||
if(listen(ipc->socket, 3) == -1) {
|
||||
wlr_log(WLR_ERROR, "Unable to listen on IPC socket");
|
||||
free(ipc->sockaddr);
|
||||
return -1;
|
||||
|
|
@ -98,13 +103,15 @@ int ipc_init(struct cg_server *server) {
|
|||
ipc->display_destroy.notify = handle_display_destroy;
|
||||
wl_display_add_destroy_listener(server->wl_display, &ipc->display_destroy);
|
||||
|
||||
ipc->event_source = wl_event_loop_add_fd(server->event_loop, ipc->socket,
|
||||
WL_EVENT_READABLE, ipc_handle_connection, server);
|
||||
ipc->event_source =
|
||||
wl_event_loop_add_fd(server->event_loop, ipc->socket, WL_EVENT_READABLE,
|
||||
ipc_handle_connection, server);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ipc_handle_connection(int fd, uint32_t mask, void *data) {
|
||||
(void) fd;
|
||||
int
|
||||
ipc_handle_connection(int fd, uint32_t mask, void *data) {
|
||||
(void)fd;
|
||||
struct cg_server *server = data;
|
||||
struct cg_ipc_handle *ipc = &server->ipc;
|
||||
if(mask != WL_EVENT_READABLE) {
|
||||
|
|
@ -113,45 +120,46 @@ int ipc_handle_connection(int fd, uint32_t mask, void *data) {
|
|||
}
|
||||
|
||||
int client_fd = accept(ipc->socket, NULL, NULL);
|
||||
if (client_fd == -1) {
|
||||
if(client_fd == -1) {
|
||||
wlr_log(WLR_ERROR, "Unable to accept IPC client connection");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int flags;
|
||||
if ((flags = fcntl(client_fd, F_GETFD)) == -1
|
||||
|| fcntl(client_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
|
||||
if((flags = fcntl(client_fd, F_GETFD)) == -1 ||
|
||||
fcntl(client_fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
|
||||
wlr_log(WLR_ERROR, "Unable to set CLOEXEC on IPC client socket");
|
||||
close(client_fd);
|
||||
return 0;
|
||||
}
|
||||
if ((flags = fcntl(client_fd, F_GETFL)) == -1
|
||||
|| fcntl(client_fd, F_SETFL, flags|O_NONBLOCK) == -1) {
|
||||
if((flags = fcntl(client_fd, F_GETFL)) == -1 ||
|
||||
fcntl(client_fd, F_SETFL, flags | O_NONBLOCK) == -1) {
|
||||
wlr_log(WLR_ERROR, "Unable to set NONBLOCK on IPC client socket");
|
||||
close(client_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct cg_ipc_client *client = malloc(sizeof(struct cg_ipc_client));
|
||||
if (!client) {
|
||||
if(!client) {
|
||||
wlr_log(WLR_ERROR, "Unable to allocate ipc client");
|
||||
close(client_fd);
|
||||
return 0;
|
||||
}
|
||||
// +1 for \n and +1 for \0
|
||||
client->read_buffer = malloc(sizeof(char)*(MAX_LINE_SIZE+2));
|
||||
client->read_buffer = malloc(sizeof(char) * (MAX_LINE_SIZE + 2));
|
||||
client->read_buf_len = 0;
|
||||
client->read_discard = 0;
|
||||
client->server = server;
|
||||
client->fd = client_fd;
|
||||
client->event_source = wl_event_loop_add_fd(server->event_loop,
|
||||
client_fd, WL_EVENT_READABLE, ipc_client_handle_readable, client);
|
||||
client->event_source =
|
||||
wl_event_loop_add_fd(server->event_loop, client_fd, WL_EVENT_READABLE,
|
||||
ipc_client_handle_readable, client);
|
||||
client->writable_event_source = NULL;
|
||||
|
||||
client->write_buffer_size = 128;
|
||||
client->write_buffer_len = 0;
|
||||
client->write_buffer = malloc(client->write_buffer_size);
|
||||
if (!client->write_buffer) {
|
||||
if(!client->write_buffer) {
|
||||
wlr_log(WLR_ERROR, "Unable to allocate ipc client write buffer");
|
||||
close(client_fd);
|
||||
return 0;
|
||||
|
|
@ -161,52 +169,58 @@ int ipc_handle_connection(int fd, uint32_t mask, void *data) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) {
|
||||
int
|
||||
ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) {
|
||||
struct cg_ipc_client *client = data;
|
||||
|
||||
if (mask & WL_EVENT_ERROR) {
|
||||
if(mask & WL_EVENT_ERROR) {
|
||||
wlr_log(WLR_ERROR, "IPC Client socket error, removing client");
|
||||
ipc_client_disconnect(client);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (mask & WL_EVENT_HANGUP) {
|
||||
if(mask & WL_EVENT_HANGUP) {
|
||||
ipc_client_disconnect(client);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int read_available;
|
||||
if (ioctl(client_fd, FIONREAD, &read_available) < 0) {
|
||||
if(ioctl(client_fd, FIONREAD, &read_available) < 0) {
|
||||
wlr_log(WLR_ERROR, "Unable to read IPC socket buffer size");
|
||||
ipc_client_disconnect(client);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int read_size = read_available < MAX_LINE_SIZE+1-client->read_buf_len ? read_available:MAX_LINE_SIZE+1-client->read_buf_len;
|
||||
int read_size = read_available < MAX_LINE_SIZE + 1 - client->read_buf_len
|
||||
? read_available
|
||||
: MAX_LINE_SIZE + 1 - client->read_buf_len;
|
||||
// Append to buffer
|
||||
ssize_t received = recv(client_fd, client->read_buffer+client->read_buf_len, read_size, 0);
|
||||
if (received == -1) {
|
||||
ssize_t received = recv(
|
||||
client_fd, client->read_buffer + client->read_buf_len, read_size, 0);
|
||||
if(received == -1) {
|
||||
wlr_log(WLR_ERROR, "Unable to receive data from IPC client");
|
||||
ipc_client_disconnect(client);
|
||||
return 0;
|
||||
}
|
||||
client->read_buf_len+=received;
|
||||
client->read_buf_len += received;
|
||||
|
||||
ipc_client_handle_command(client);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ipc_client_disconnect(struct cg_ipc_client *client) {
|
||||
if (client == NULL) {
|
||||
wlr_log(WLR_ERROR, "Client \"NULL\" was passed to ipc_client_disconnect");
|
||||
void
|
||||
ipc_client_disconnect(struct cg_ipc_client *client) {
|
||||
if(client == NULL) {
|
||||
wlr_log(WLR_ERROR,
|
||||
"Client \"NULL\" was passed to ipc_client_disconnect");
|
||||
return;
|
||||
}
|
||||
|
||||
shutdown(client->fd, SHUT_RDWR);
|
||||
|
||||
wl_event_source_remove(client->event_source);
|
||||
if (client->writable_event_source) {
|
||||
if(client->writable_event_source) {
|
||||
wl_event_source_remove(client->writable_event_source);
|
||||
}
|
||||
wl_list_remove(&client->link);
|
||||
|
|
@ -216,43 +230,48 @@ void ipc_client_disconnect(struct cg_ipc_client *client) {
|
|||
free(client);
|
||||
}
|
||||
|
||||
void ipc_client_handle_command(struct cg_ipc_client *client) {
|
||||
if (client == NULL) {
|
||||
wlr_log(WLR_ERROR, "Client \"NULL\" was passed to ipc_client_handle_command");
|
||||
void
|
||||
ipc_client_handle_command(struct cg_ipc_client *client) {
|
||||
if(client == NULL) {
|
||||
wlr_log(WLR_ERROR,
|
||||
"Client \"NULL\" was passed to ipc_client_handle_command");
|
||||
return;
|
||||
}
|
||||
client->read_buffer[client->read_buf_len]='\0';
|
||||
client->read_buffer[client->read_buf_len] = '\0';
|
||||
char *nl_pos;
|
||||
uint32_t offset=0;
|
||||
while((nl_pos = strchr(client->read_buffer+offset,'\n')) != NULL){
|
||||
uint32_t offset = 0;
|
||||
while((nl_pos = strchr(client->read_buffer + offset, '\n')) != NULL) {
|
||||
if(client->read_discard) {
|
||||
client->read_discard = 0;
|
||||
} else {
|
||||
*nl_pos='\0';
|
||||
char *line=client->read_buffer+offset;
|
||||
*nl_pos = '\0';
|
||||
char *line = client->read_buffer + offset;
|
||||
if(*line != '\0' && *line != '#') {
|
||||
message_clear(client->server->curr_output);
|
||||
char *errstr;
|
||||
if(parse_rc_line(client->server, line, &errstr) != 0) {
|
||||
if(errstr != NULL) {
|
||||
message_printf(client->server->curr_output, "%s", errstr);
|
||||
wlr_log(WLR_ERROR, "%s",errstr);
|
||||
message_printf(client->server->curr_output, "%s",
|
||||
errstr);
|
||||
wlr_log(WLR_ERROR, "%s", errstr);
|
||||
free(errstr);
|
||||
}
|
||||
wlr_log(WLR_ERROR, "Error parsing input from IPC socket");
|
||||
}
|
||||
}
|
||||
}
|
||||
offset=(nl_pos-client->read_buffer)+1;
|
||||
offset = (nl_pos - client->read_buffer) + 1;
|
||||
}
|
||||
if(offset == 0) {
|
||||
wlr_log(WLR_ERROR, "Line received was longer that %d, discarding it",MAX_LINE_SIZE);
|
||||
wlr_log(WLR_ERROR, "Line received was longer that %d, discarding it",
|
||||
MAX_LINE_SIZE);
|
||||
client->read_buf_len = 0;
|
||||
client->read_discard = 1;
|
||||
return;
|
||||
}
|
||||
if(offset<client->read_buf_len) {
|
||||
memmove(client->read_buffer, client->read_buffer+offset, client->read_buf_len-offset);
|
||||
if(offset < client->read_buf_len) {
|
||||
memmove(client->read_buffer, client->read_buffer + offset,
|
||||
client->read_buf_len - offset);
|
||||
}
|
||||
client->read_buf_len-=offset;
|
||||
client->read_buf_len -= offset;
|
||||
}
|
||||
|
|
|
|||
24
ipc_server.h
24
ipc_server.h
|
|
@ -3,8 +3,8 @@
|
|||
|
||||
#include "config.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <inttypes.h>
|
||||
#include <sys/types.h>
|
||||
#include <wayland-server-core.h>
|
||||
|
||||
struct cg_server;
|
||||
|
|
@ -21,7 +21,7 @@ struct cg_ipc_client {
|
|||
char *write_buffer;
|
||||
// The following is for storing data between event_loop calls
|
||||
uint16_t read_buf_len;
|
||||
uint8_t read_discard;// 1 if the current line is to be discarded
|
||||
uint8_t read_discard; // 1 if the current line is to be discarded
|
||||
char *read_buffer;
|
||||
};
|
||||
|
||||
|
|
@ -33,12 +33,18 @@ struct cg_ipc_handle {
|
|||
struct sockaddr_un *sockaddr;
|
||||
};
|
||||
|
||||
int ipc_init(struct cg_server *server);
|
||||
int ipc_handle_connection(int fd, uint32_t mask, void *data);
|
||||
int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data);
|
||||
//int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data);
|
||||
void ipc_client_disconnect(struct cg_ipc_client *client);
|
||||
void ipc_client_handle_command(struct cg_ipc_client *client);
|
||||
//bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length);
|
||||
int
|
||||
ipc_init(struct cg_server *server);
|
||||
int
|
||||
ipc_handle_connection(int fd, uint32_t mask, void *data);
|
||||
int
|
||||
ipc_client_handle_readable(int client_fd, uint32_t mask, void *data);
|
||||
// int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data);
|
||||
void
|
||||
ipc_client_disconnect(struct cg_ipc_client *client);
|
||||
void
|
||||
ipc_client_handle_command(struct cg_ipc_client *client);
|
||||
// bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t
|
||||
// payload_length);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
143
parse.c
143
parse.c
|
|
@ -11,34 +11,35 @@
|
|||
#include "server.h"
|
||||
#include "workspace.h"
|
||||
|
||||
char *malloc_vsprintf(const char *fmt, va_list ap) {
|
||||
char *
|
||||
malloc_vsprintf(const char *fmt, va_list ap) {
|
||||
va_list ap2;
|
||||
va_copy(ap2,ap);
|
||||
va_copy(ap2, ap);
|
||||
int len = vsnprintf(NULL, 0, fmt, ap);
|
||||
char *ret=malloc(sizeof(char)*(len+1));
|
||||
vsnprintf(ret, len+1, fmt, ap2);
|
||||
char *ret = malloc(sizeof(char) * (len + 1));
|
||||
vsnprintf(ret, len + 1, fmt, ap2);
|
||||
va_end(ap2);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *log_error(const char *fmt, ...) {
|
||||
char *
|
||||
log_error(const char *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args,fmt);
|
||||
char *ret=malloc_vsprintf(fmt, args);
|
||||
va_start(args, fmt);
|
||||
char *ret = malloc_vsprintf(fmt, args);
|
||||
if(ret != NULL) {
|
||||
wlr_log(WLR_ERROR, "%s", ret);
|
||||
}
|
||||
va_end(args);
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
/* parses a key definition (e.g. "S-Tab") and sets key and modifiers in
|
||||
* keybinding respectivly */
|
||||
int
|
||||
parse_key(struct keybinding *keybinding, const char *key_def,char **errstr) {
|
||||
parse_key(struct keybinding *keybinding, const char *key_def, char **errstr) {
|
||||
if(key_def == NULL) {
|
||||
*errstr=log_error("Expected key definition, got nothing.");
|
||||
*errstr = log_error("Expected key definition, got nothing.");
|
||||
return -1;
|
||||
}
|
||||
keybinding->modifiers = 0;
|
||||
|
|
@ -66,14 +67,14 @@ parse_key(struct keybinding *keybinding, const char *key_def,char **errstr) {
|
|||
keybinding->modifiers |= WLR_MODIFIER_MOD5;
|
||||
break;
|
||||
default:
|
||||
*errstr=log_error("Unknown modifier \"%c\"", key_def[0]);
|
||||
*errstr = log_error("Unknown modifier \"%c\"", key_def[0]);
|
||||
return -1;
|
||||
}
|
||||
key_def += 2;
|
||||
}
|
||||
xkb_keysym_t keysym = xkb_keysym_from_name(key_def, XKB_KEYSYM_NO_FLAGS);
|
||||
if(keysym == XKB_KEY_NoSymbol) {
|
||||
*errstr=log_error("Could not convert key \"%s\" to keysym.", key_def);
|
||||
*errstr = log_error("Could not convert key \"%s\" to keysym.", key_def);
|
||||
return -1;
|
||||
}
|
||||
keybinding->key = keysym;
|
||||
|
|
@ -90,7 +91,8 @@ struct keybinding *
|
|||
parse_keybinding(struct cg_server *server, char **saveptr, char **errstr) {
|
||||
struct keybinding *keybinding = malloc(sizeof(struct keybinding));
|
||||
if(keybinding == NULL) {
|
||||
*errstr=log_error("Failed to allocate memory for keybinding in parse_keybinding");
|
||||
*errstr = log_error(
|
||||
"Failed to allocate memory for keybinding in parse_keybinding");
|
||||
return NULL;
|
||||
}
|
||||
char *key = strtok_r(NULL, " ", saveptr);
|
||||
|
|
@ -121,12 +123,13 @@ struct keybinding *
|
|||
parse_definekey(struct cg_server *server, char **saveptr, char **errstr) {
|
||||
char *mode = strtok_r(NULL, " ", saveptr);
|
||||
if(mode == NULL) {
|
||||
*errstr=log_error("Too few arguments to \"definekey\". Expected mode");
|
||||
*errstr =
|
||||
log_error("Too few arguments to \"definekey\". Expected mode");
|
||||
return NULL;
|
||||
}
|
||||
int mode_idx = get_mode_index_from_name(server->modes, mode);
|
||||
if(mode_idx == -1) {
|
||||
*errstr=log_error("Unknown mode \"%s\"", mode);
|
||||
*errstr = log_error("Unknown mode \"%s\"", mode);
|
||||
return NULL;
|
||||
}
|
||||
struct keybinding *keybinding = parse_keybinding(server, saveptr, errstr);
|
||||
|
|
@ -139,13 +142,16 @@ parse_definekey(struct cg_server *server, char **saveptr, char **errstr) {
|
|||
}
|
||||
|
||||
int
|
||||
parse_background(struct cg_server *server, float *color, char **saveptr, char **errstr) {
|
||||
parse_background(struct cg_server *server, float *color, char **saveptr,
|
||||
char **errstr) {
|
||||
/* Read rgb numbers */
|
||||
for(unsigned int i = 0; i < 3; ++i) {
|
||||
char *nstr = strtok_r(NULL, " \n", saveptr);
|
||||
int nstrlen;
|
||||
if(nstr == NULL || (nstrlen = strlen(nstr)) == 0) {
|
||||
*errstr=log_error("Expected three space-separated numbers (rgb) for background color setting. Got %d.", i);
|
||||
*errstr = log_error("Expected three space-separated numbers (rgb) "
|
||||
"for background color setting. Got %d.",
|
||||
i);
|
||||
return -1;
|
||||
}
|
||||
if(nstr[nstrlen - 1] == '\n') {
|
||||
|
|
@ -155,11 +161,15 @@ parse_background(struct cg_server *server, float *color, char **saveptr, char **
|
|||
char *endptr = NULL;
|
||||
float nval = strtof(nstr, &endptr);
|
||||
if(endptr != nstr + nstrlen) {
|
||||
*errstr=log_error("Could not parse number \"%s\" for background color setting.", nstr);
|
||||
*errstr = log_error(
|
||||
"Could not parse number \"%s\" for background color setting.",
|
||||
nstr);
|
||||
return -1;
|
||||
}
|
||||
if(nval < 0 || nval > 1) {
|
||||
*errstr=log_error("Expected a number between 0 and 1 for setting of background color. Got %f.", nval);
|
||||
*errstr = log_error("Expected a number between 0 and 1 for setting "
|
||||
"of background color. Got %f.",
|
||||
nval);
|
||||
return -1;
|
||||
}
|
||||
color[i] = nval;
|
||||
|
|
@ -171,7 +181,8 @@ struct keybinding *
|
|||
parse_escape(char **saveptr, char **errstr) {
|
||||
struct keybinding *keybinding = malloc(sizeof(struct keybinding));
|
||||
if(keybinding == NULL) {
|
||||
*errstr=log_error("Failed to allocate memory for keybinding in parse_escape");
|
||||
*errstr = log_error(
|
||||
"Failed to allocate memory for keybinding in parse_escape");
|
||||
return NULL;
|
||||
}
|
||||
char *key = strtok_r(NULL, " ", saveptr);
|
||||
|
|
@ -191,7 +202,7 @@ char *
|
|||
parse_definemode(char **saveptr, char **errstr) {
|
||||
char *mode = strtok_r(NULL, " ", saveptr);
|
||||
if(mode == NULL) {
|
||||
*errstr=log_error("Expected mode to succeed \"definemode\" keyword.");
|
||||
*errstr = log_error("Expected mode to succeed \"definemode\" keyword.");
|
||||
return NULL;
|
||||
}
|
||||
return strdup(mode);
|
||||
|
|
@ -201,12 +212,14 @@ int
|
|||
parse_workspaces(char **saveptr, char **errstr) {
|
||||
char *nws_str = strtok_r(NULL, " ", saveptr);
|
||||
if(nws_str == NULL) {
|
||||
*errstr=log_error("Expected argument for \"workspaces\" command, got none.");
|
||||
*errstr = log_error(
|
||||
"Expected argument for \"workspaces\" command, got none.");
|
||||
return -1;
|
||||
}
|
||||
long nws = strtol(nws_str, NULL, 10);
|
||||
if(!(1 <= nws && nws <= 30)) {
|
||||
*errstr=log_error("More than 30 workspaces are not supported. Received %li", nws);
|
||||
*errstr = log_error(
|
||||
"More than 30 workspaces are not supported. Received %li", nws);
|
||||
return -1;
|
||||
}
|
||||
return nws;
|
||||
|
|
@ -249,62 +262,81 @@ parse_float(char **saveptr, const char *delim) {
|
|||
}
|
||||
|
||||
int
|
||||
parse_output_config(struct wl_list *config_list, char **saveptr, char **errstr) {
|
||||
parse_output_config(struct wl_list *config_list, char **saveptr,
|
||||
char **errstr) {
|
||||
struct cg_output_config *cfg = malloc(sizeof(struct cg_output_config));
|
||||
if(cfg == NULL) {
|
||||
*errstr=log_error("Failed to allocate memory for output configuration");
|
||||
*errstr =
|
||||
log_error("Failed to allocate memory for output configuration");
|
||||
goto error;
|
||||
}
|
||||
char *name = strtok_r(NULL, " ", saveptr);
|
||||
if(name == NULL) {
|
||||
*errstr=log_error("Expected name of output to be configured, got none");
|
||||
*errstr =
|
||||
log_error("Expected name of output to be configured, got none");
|
||||
goto error;
|
||||
}
|
||||
char *pos_str = strtok_r(NULL, " ", saveptr);
|
||||
if(pos_str == NULL || strcmp(pos_str, "pos") != 0) {
|
||||
*errstr=log_error("Expected keyword \"pos\" in output configuration for output %s", name);
|
||||
*errstr = log_error(
|
||||
"Expected keyword \"pos\" in output configuration for output %s",
|
||||
name);
|
||||
goto error;
|
||||
}
|
||||
|
||||
cfg->pos.x = parse_uint(saveptr, " ");
|
||||
if(cfg->pos.x < 0) {
|
||||
*errstr=log_error("Error parsing x coordinate of output configuration for output %s", name);
|
||||
*errstr = log_error(
|
||||
"Error parsing x coordinate of output configuration for output %s",
|
||||
name);
|
||||
goto error;
|
||||
}
|
||||
|
||||
cfg->pos.y = parse_uint(saveptr, " ");
|
||||
if(cfg->pos.y < 0) {
|
||||
*errstr=log_error("Error parsing y coordinate of output configuration for output %s", name);
|
||||
*errstr = log_error(
|
||||
"Error parsing y coordinate of output configuration for output %s",
|
||||
name);
|
||||
goto error;
|
||||
}
|
||||
|
||||
char *res_str = strtok_r(NULL, " ", saveptr);
|
||||
if(res_str == NULL || strcmp(res_str, "res") != 0) {
|
||||
*errstr=log_error("Expected keyword \"res\" in output configuration for output %s", name);
|
||||
*errstr = log_error(
|
||||
"Expected keyword \"res\" in output configuration for output %s",
|
||||
name);
|
||||
goto error;
|
||||
}
|
||||
|
||||
cfg->pos.width = parse_uint(saveptr, "x");
|
||||
if(cfg->pos.width <= 0) {
|
||||
*errstr=log_error("Error parsing width of output configuration for output %s (hint: width must be larger than 0)", name);
|
||||
*errstr = log_error("Error parsing width of output configuration for "
|
||||
"output %s (hint: width must be larger than 0)",
|
||||
name);
|
||||
goto error;
|
||||
}
|
||||
|
||||
cfg->pos.height = parse_uint(saveptr, " ");
|
||||
if(cfg->pos.height <= 0) {
|
||||
*errstr=log_error("Error parsing height of output configuration for output %s (hint: height must e larger than 0)", name);
|
||||
*errstr = log_error("Error parsing height of output configuration for "
|
||||
"output %s (hint: height must e larger than 0)",
|
||||
name);
|
||||
goto error;
|
||||
}
|
||||
|
||||
char *rate_str = strtok_r(NULL, " ", saveptr);
|
||||
if(rate_str == NULL || strcmp(rate_str, "rate") != 0) {
|
||||
*errstr=log_error("Expected keyword \"rate\" in output configuration for output %s", name);
|
||||
*errstr = log_error(
|
||||
"Expected keyword \"rate\" in output configuration for output %s",
|
||||
name);
|
||||
goto error;
|
||||
}
|
||||
|
||||
cfg->refresh_rate = parse_float(saveptr, " ");
|
||||
if(cfg->refresh_rate <= 0.0) {
|
||||
*errstr=log_error("Error parsing refresh rate of output configuration for output %s", name);
|
||||
*errstr = log_error(
|
||||
"Error parsing refresh rate of output configuration for output %s",
|
||||
name);
|
||||
goto error;
|
||||
}
|
||||
|
||||
|
|
@ -329,10 +361,10 @@ error:
|
|||
|
||||
int
|
||||
parse_command(struct cg_server *server, struct keybinding *keybinding,
|
||||
char *saveptr, char** errstr) {
|
||||
char *saveptr, char **errstr) {
|
||||
char *action = strtok_r(NULL, " ", &saveptr);
|
||||
if(action == NULL) {
|
||||
*errstr=log_error("Expexted an action to parse, got none.");
|
||||
*errstr = log_error("Expexted an action to parse, got none.");
|
||||
return -1;
|
||||
}
|
||||
keybinding->data = (union keybinding_params){.c = NULL};
|
||||
|
|
@ -369,7 +401,8 @@ parse_command(struct cg_server *server, struct keybinding *keybinding,
|
|||
} else if(strcmp(action, "exec") == 0) {
|
||||
keybinding->action = KEYBINDING_RUN_COMMAND;
|
||||
if(saveptr == NULL) {
|
||||
*errstr=log_error("Not enough paramaters to \"exec\". Expected " "string to execute.");
|
||||
*errstr = log_error("Not enough paramaters to \"exec\". Expected "
|
||||
"string to execute.");
|
||||
return -1;
|
||||
}
|
||||
keybinding->data.c = strdup(saveptr);
|
||||
|
|
@ -389,13 +422,16 @@ parse_command(struct cg_server *server, struct keybinding *keybinding,
|
|||
keybinding->action = KEYBINDING_SWITCH_WORKSPACE;
|
||||
char *nws_str = strtok_r(NULL, " ", &saveptr);
|
||||
if(nws_str == NULL) {
|
||||
*errstr=log_error("Expected argument for \"workspace\" action, got none.");
|
||||
*errstr = log_error(
|
||||
"Expected argument for \"workspace\" action, got none.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
long ws = strtol(nws_str, NULL, 10);
|
||||
if(ws < 1) {
|
||||
*errstr=log_error("Workspace number must be an integer number larger or equal to 1. Got %ld", ws);
|
||||
*errstr = log_error("Workspace number must be an integer number "
|
||||
"larger or equal to 1. Got %ld",
|
||||
ws);
|
||||
return -1;
|
||||
}
|
||||
keybinding->data.u = ws - 1;
|
||||
|
|
@ -403,13 +439,16 @@ parse_command(struct cg_server *server, struct keybinding *keybinding,
|
|||
keybinding->action = KEYBINDING_MOVE_VIEW_TO_WORKSPACE;
|
||||
char *nws_str = strtok_r(NULL, " ", &saveptr);
|
||||
if(nws_str == NULL) {
|
||||
*errstr=log_error("Expected argument for \"workspace\" action, got none.");
|
||||
*errstr = log_error(
|
||||
"Expected argument for \"workspace\" action, got none.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
long ws = strtol(nws_str, NULL, 10);
|
||||
if(ws < 1) {
|
||||
*errstr=log_error("Workspace number must be an integer larger or equal to 1. Got %ld", ws);
|
||||
*errstr = log_error("Workspace number must be an integer larger or "
|
||||
"equal to 1. Got %ld",
|
||||
ws);
|
||||
return -1;
|
||||
}
|
||||
keybinding->data.u = ws - 1;
|
||||
|
|
@ -435,7 +474,8 @@ parse_command(struct cg_server *server, struct keybinding *keybinding,
|
|||
keybinding->action = KEYBINDING_CHANGE_TTY;
|
||||
char *ntty = strtok_r(NULL, " ", &saveptr);
|
||||
if(ntty == NULL) {
|
||||
*errstr=log_error("Expected argument for \"switchvt\" command, got none.");
|
||||
*errstr = log_error(
|
||||
"Expected argument for \"switchvt\" command, got none.");
|
||||
return -1;
|
||||
}
|
||||
long tty = strtol(ntty, NULL, 10);
|
||||
|
|
@ -444,12 +484,13 @@ parse_command(struct cg_server *server, struct keybinding *keybinding,
|
|||
keybinding->action = KEYBINDING_SWITCH_MODE;
|
||||
char *mode = strtok_r(NULL, " ", &saveptr);
|
||||
if(mode == NULL) {
|
||||
*errstr=log_error("Expected mode after \"switch_mode\". Got nothing.");
|
||||
*errstr =
|
||||
log_error("Expected mode after \"switch_mode\". Got nothing.");
|
||||
return -1;
|
||||
}
|
||||
int mode_idx = get_mode_index_from_name(server->modes, mode);
|
||||
if(mode_idx == -1) {
|
||||
*errstr=log_error("Unknown mode \"%s\" for switch_mode", mode);
|
||||
*errstr = log_error("Unknown mode \"%s\" for switch_mode", mode);
|
||||
return -1;
|
||||
}
|
||||
keybinding->data.u = (unsigned int)mode_idx;
|
||||
|
|
@ -457,12 +498,14 @@ parse_command(struct cg_server *server, struct keybinding *keybinding,
|
|||
keybinding->action = KEYBINDING_SWITCH_DEFAULT_MODE;
|
||||
char *mode = strtok_r(NULL, " ", &saveptr);
|
||||
if(mode == NULL) {
|
||||
*errstr=log_error("Expected mode after \"switch_default_mode\". Got nothing.");
|
||||
*errstr = log_error(
|
||||
"Expected mode after \"switch_default_mode\". Got nothing.");
|
||||
return -1;
|
||||
}
|
||||
int mode_idx = get_mode_index_from_name(server->modes, mode);
|
||||
if(mode_idx == -1) {
|
||||
*errstr=log_error("Unknown mode \"%s\" for switch_default_mode", mode);
|
||||
*errstr =
|
||||
log_error("Unknown mode \"%s\" for switch_default_mode", mode);
|
||||
return -1;
|
||||
}
|
||||
keybinding->data.u = (unsigned int)mode_idx;
|
||||
|
|
@ -480,7 +523,8 @@ parse_command(struct cg_server *server, struct keybinding *keybinding,
|
|||
}
|
||||
} else if(strcmp(action, "background") == 0) {
|
||||
keybinding->action = KEYBINDING_BACKGROUND;
|
||||
if(parse_background(server, keybinding->data.color, &saveptr, errstr) != 0) {
|
||||
if(parse_background(server, keybinding->data.color, &saveptr, errstr) !=
|
||||
0) {
|
||||
return -1;
|
||||
}
|
||||
} else if(strcmp(action, "escape") == 0) {
|
||||
|
|
@ -506,7 +550,7 @@ parse_command(struct cg_server *server, struct keybinding *keybinding,
|
|||
return -1;
|
||||
}
|
||||
} else {
|
||||
*errstr=log_error("Error, unsupported action \"%s\".", action);
|
||||
*errstr = log_error("Error, unsupported action \"%s\".", action);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
|
|
@ -518,7 +562,8 @@ parse_rc_line(struct cg_server *server, char *line, char **errstr) {
|
|||
|
||||
struct keybinding *keybinding = malloc(sizeof(struct keybinding));
|
||||
if(keybinding == NULL) {
|
||||
*errstr=log_error("Failed to allocate memory for temporary keybinding struct.");
|
||||
*errstr = log_error(
|
||||
"Failed to allocate memory for temporary keybinding struct.");
|
||||
free(keybinding);
|
||||
free(saveptr);
|
||||
return -1;
|
||||
|
|
|
|||
2
parse.h
2
parse.h
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#define PARSE_H
|
||||
|
||||
# define MAX_LINE_SIZE 256
|
||||
#define MAX_LINE_SIZE 256
|
||||
|
||||
struct cg_server;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue