Merge pull request #23 from taiyu-len/master

moving things around + statics + forking + exec cleanup + fixed cmd_focus return + keep exec programs out of logs
This commit is contained in:
Drew DeVault 2015-08-13 11:00:00 -04:00
commit 94e81fd64c
7 changed files with 163 additions and 129 deletions

View File

@ -18,7 +18,7 @@ struct modifier_key {
uint32_t mod;
};
struct modifier_key modifiers[] = {
static struct modifier_key modifiers[] = {
{ XKB_MOD_NAME_SHIFT, WLC_BIT_MOD_SHIFT },
{ XKB_MOD_NAME_CAPS, WLC_BIT_MOD_CAPS },
{ XKB_MOD_NAME_CTRL, WLC_BIT_MOD_CTRL },
@ -29,11 +29,46 @@ struct modifier_key modifiers[] = {
{ "Mod5", WLC_BIT_MOD_MOD5 },
};
bool cmd_bindsym(struct sway_config *config, int argc, char **argv) {
if (argc < 2) {
sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc);
return false;
enum expected_args {
EXPECTED_MORE_THAN,
EXPECTED_LESS_THAN,
EXPECTED_EQUAL_TO
};
static bool checkarg(int argc, char *name, enum expected_args type, int val) {
switch (type) {
case EXPECTED_MORE_THAN:
if (argc > val) {
return true;
}
sway_log(L_ERROR, "Invalid %s command."
"(expected more then %d argument%s, got %d",
name, val, (char*[2]){"s", ""}[argc==1], argc);
break;
case EXPECTED_LESS_THAN:
if (argc < val) {
return true;
};
sway_log(L_ERROR, "Invalid %s command."
"(expected less then %d argument%s, got %d",
name, val, (char*[2]){"s", ""}[argc==1], argc);
break;
case EXPECTED_EQUAL_TO:
if (argc == val) {
return true;
};
sway_log(L_ERROR, "Invalid %s command."
"(expected %d arguments, got %d", name, val, argc);
break;
}
return false;
}
static bool cmd_bindsym(struct sway_config *config, int argc, char **argv) {
if (!checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1)) {
return false;
};
struct sway_binding *binding = malloc(sizeof(struct sway_binding));
binding->keys = create_list();
@ -73,46 +108,43 @@ bool cmd_bindsym(struct sway_config *config, int argc, char **argv) {
return true;
}
bool cmd_exec(struct sway_config *config, int argc, char **argv) {
if (argc < 1) {
sway_log(L_ERROR, "Invalid exec command (expected at least 1 argument, got %d)", argc);
static bool cmd_exec_always(struct sway_config *config, int argc, char **argv) {
if (!checkarg(argc, "exec_always", EXPECTED_MORE_THAN, 0)) {
return false;
}
pid_t pid = fork();
/* Failed to fork */
if (pid < 0) {
sway_log(L_ERROR, "exec command failed, sway did not fork");
return false;
}
/* Child process */
if (pid == 0) {
char *args = join_args(argv, argc);
sway_log(L_DEBUG, "Executing %s", args);
execl("/bin/sh", "sh", "-c", args, (char *)NULL);
/* Execl doesnt return unless failure */
sway_log(L_ERROR, "could not find /bin/sh");
free(args);
exit(-1);
}
/* Parent */
return true;
}
static bool cmd_exec(struct sway_config *config, int argc, char **argv) {
if (config->reloading) {
sway_log(L_DEBUG, "Ignoring exec %s due to reload", join_args(argv, argc));
char *args = join_args(argv, argc);
sway_log(L_DEBUG, "Ignoring exec %s due to reload", args);
free(args);
return true;
}
if (fork() == 0) {
char *args = join_args(argv, argc);
sway_log(L_DEBUG, "Executing %s", args);
execl("/bin/sh", "sh", "-c", args, (char *)NULL);
free(args);
exit(0);
}
return true;
return cmd_exec_always(config, argc, argv);
}
bool cmd_exec_always(struct sway_config *config, int argc, char **argv) {
if (argc < 1) {
sway_log(L_ERROR, "Invalid exec_always command (expected at least 1 argument, got %d)", argc);
return false;
}
if (fork() == 0) {
char *args = join_args(argv, argc);
sway_log(L_DEBUG, "Executing %s", args);
execl("/bin/sh", "sh", "-c", args, (char *)NULL);
free(args);
exit(0);
}
return true;
}
bool cmd_exit(struct sway_config *config, int argc, char **argv) {
if (argc != 0) {
sway_log(L_ERROR, "Invalid exit command (expected 1 arguments, got %d)", argc);
static bool cmd_exit(struct sway_config *config, int argc, char **argv) {
if (!checkarg(argc, "exit", EXPECTED_EQUAL_TO, 0)) {
return false;
}
// TODO: Some kind of clean up is probably in order
@ -120,9 +152,8 @@ bool cmd_exit(struct sway_config *config, int argc, char **argv) {
return true;
}
bool cmd_focus(struct sway_config *config, int argc, char **argv) {
if (argc != 1) {
sway_log(L_ERROR, "Invalid focus command (expected 1 arguments, got %d)", argc);
static bool cmd_focus(struct sway_config *config, int argc, char **argv) {
if (!checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1)) {
return false;
}
if (strcasecmp(argv[0], "left") == 0) {
@ -139,9 +170,8 @@ bool cmd_focus(struct sway_config *config, int argc, char **argv) {
return true;
}
bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char **argv) {
if (argc != 1) {
sway_log(L_ERROR, "Invalid focus_follows_mouse command (expected 1 arguments, got %d)", argc);
static bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char **argv) {
if (!checkarg(argc, "focus_follows_mouse", EXPECTED_EQUAL_TO, 1)) {
return false;
}
@ -149,9 +179,8 @@ bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char **argv)
return true;
}
bool cmd_layout(struct sway_config *config, int argc, char **argv) {
if (argc < 1) {
sway_log(L_ERROR, "Invalid layout command (expected at least 1 argument, got %d)", argc);
static bool cmd_layout(struct sway_config *config, int argc, char **argv) {
if (!checkarg(argc, "layout", EXPECTED_MORE_THAN, 0)) {
return false;
}
swayc_t *parent = get_focused_container(&root_container);
@ -174,9 +203,8 @@ bool cmd_layout(struct sway_config *config, int argc, char **argv) {
return true;
}
bool cmd_reload(struct sway_config *config, int argc, char **argv) {
if (argc != 0) {
sway_log(L_ERROR, "Invalid reload command (expected 0 arguments, got %d)", argc);
static bool cmd_reload(struct sway_config *config, int argc, char **argv) {
if (!checkarg(argc, "reload", EXPECTED_EQUAL_TO, 0)) {
return false;
}
if (!load_config()) {
@ -186,9 +214,8 @@ bool cmd_reload(struct sway_config *config, int argc, char **argv) {
return true;
}
bool cmd_set(struct sway_config *config, int argc, char **argv) {
if (argc != 2) {
sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc);
static bool cmd_set(struct sway_config *config, int argc, char **argv) {
if (!checkarg(argc, "set", EXPECTED_EQUAL_TO, 2)) {
return false;
}
struct sway_variable *var = malloc(sizeof(struct sway_variable));
@ -200,9 +227,11 @@ bool cmd_set(struct sway_config *config, int argc, char **argv) {
return true;
}
bool _do_split(struct sway_config *config, int argc, char **argv, int layout) {
if (argc != 0) {
sway_log(L_ERROR, "Invalid splitv command (expected 0 arguments, got %d)", argc);
static bool _do_split(struct sway_config *config, int argc, char **argv, int layout) {
char *name = layout == L_VERT ? "splitv":
layout == L_HORIZ ? "splith":
"split";
if (!checkarg(argc, name, EXPECTED_EQUAL_TO, 0)) {
return false;
}
swayc_t *focused = get_focused_container(&root_container);
@ -225,20 +254,18 @@ bool _do_split(struct sway_config *config, int argc, char **argv, int layout) {
return true;
}
bool cmd_splitv(struct sway_config *config, int argc, char **argv) {
static bool cmd_splitv(struct sway_config *config, int argc, char **argv) {
return _do_split(config, argc, argv, L_VERT);
}
bool cmd_splith(struct sway_config *config, int argc, char **argv) {
static bool cmd_splith(struct sway_config *config, int argc, char **argv) {
return _do_split(config, argc, argv, L_HORIZ);
}
bool cmd_log_colors(struct sway_config *config, int argc, char **argv) {
if (argc != 1) {
sway_log(L_ERROR, "Invalid log_colors command (expected 1 argument, got %d)", argc);
static bool cmd_log_colors(struct sway_config *config, int argc, char **argv) {
if (!checkarg(argc, "log_colors", EXPECTED_EQUAL_TO, 1)) {
return false;
}
if (strcasecmp(argv[0], "no") != 0 && strcasecmp(argv[0], "yes") != 0) {
sway_log(L_ERROR, "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]);
return false;
@ -248,9 +275,8 @@ bool cmd_log_colors(struct sway_config *config, int argc, char **argv) {
return true;
}
bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) {
if (argc != 1) {
sway_log(L_ERROR, "Invalid fullscreen command (expected 1 arguments, got %d)", argc);
static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) {
if (!checkarg(argc, "fullscreen", EXPECTED_EQUAL_TO, 0)) {
return false;
}
@ -262,9 +288,8 @@ bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) {
return true;
}
bool cmd_workspace(struct sway_config *config, int argc, char **argv) {
if (argc != 1) {
sway_log(L_ERROR, "Invalid workspace command (expected 1 arguments, got %d)", argc);
static bool cmd_workspace(struct sway_config *config, int argc, char **argv) {
if (!checkarg(argc, "workspace", EXPECTED_EQUAL_TO, 1)) {
return false;
}
@ -278,7 +303,7 @@ bool cmd_workspace(struct sway_config *config, int argc, char **argv) {
}
/* Keep alphabetized */
struct cmd_handler handlers[] = {
static struct cmd_handler handlers[] = {
{ "bindsym", cmd_bindsym },
{ "exec", cmd_exec },
{ "exec_always", cmd_exec_always },
@ -295,7 +320,7 @@ struct cmd_handler handlers[] = {
{ "workspace", cmd_workspace }
};
char **split_directive(char *line, int *argc) {
static char **split_directive(char *line, int *argc) {
const char *delimiters = " ";
*argc = 0;
while (isspace(*line) && *line) ++line;
@ -347,13 +372,13 @@ char **split_directive(char *line, int *argc) {
return parts;
}
int handler_compare(const void *_a, const void *_b) {
static int handler_compare(const void *_a, const void *_b) {
const struct cmd_handler *a = _a;
const struct cmd_handler *b = _b;
return strcasecmp(a->command, b->command);
}
struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *line) {
static struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *line) {
struct cmd_handler d = { .command=line };
struct cmd_handler *res = bsearch(&d, handlers, l, sizeof(struct cmd_handler), handler_compare);
return res;
@ -395,7 +420,7 @@ bool handle_command(struct sway_config *config, char *exec) {
sway_log(L_ERROR, "Command failed: %s", cmd);
}
}
if(ptr) {
if (ptr) {
free(cmd);
}
return exec_success;

View File

@ -9,16 +9,16 @@
#include "commands.h"
#include "handlers.h"
bool handle_output_created(wlc_handle output) {
static bool handle_output_created(wlc_handle output) {
add_output(output);
return true;
}
void handle_output_destroyed(wlc_handle output) {
static void handle_output_destroyed(wlc_handle output) {
destroy_output(output);
}
void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to) {
static void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to) {
sway_log(L_DEBUG, "Output %d resolution changed to %d x %d", output, to->w, to->h);
swayc_t *c = get_swayc_for_handle(output, &root_container);
if (!c) return;
@ -27,7 +27,7 @@ void handle_output_resolution_change(wlc_handle output, const struct wlc_size *f
arrange_windows(&root_container, -1, -1);
}
void handle_output_focused(wlc_handle output, bool focus) {
static void handle_output_focused(wlc_handle output, bool focus) {
swayc_t *c = get_swayc_for_handle(output, &root_container);
if (!c) return;
if (focus) {
@ -36,27 +36,26 @@ void handle_output_focused(wlc_handle output, bool focus) {
}
}
bool handle_view_created(wlc_handle view) {
static bool handle_view_created(wlc_handle view) {
add_view(view);
return true;
}
void handle_view_destroyed(wlc_handle view) {
static void handle_view_destroyed(wlc_handle view) {
sway_log(L_DEBUG, "Destroying window %d", view);
destroy_view(get_swayc_for_handle(view, &root_container));
return true;
}
void handle_view_focus(wlc_handle view, bool focus) {
static void handle_view_focus(wlc_handle view, bool focus) {
return;
}
void handle_view_geometry_request(wlc_handle view, const struct wlc_geometry* geometry) {
static void handle_view_geometry_request(wlc_handle view, const struct wlc_geometry* geometry) {
// deny that shit
}
bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers
static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers
*modifiers, uint32_t key, uint32_t sym, enum wlc_key_state state) {
enum { QSIZE = 32 };
static uint8_t head = 0;
@ -133,7 +132,7 @@ bool pointer_test(swayc_t *view, void *_origin) {
struct wlc_origin mouse_origin;
bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct wlc_origin *origin) {
static bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct wlc_origin *origin) {
mouse_origin = *origin;
if (!config->focus_follows_mouse) {
return true;
@ -148,7 +147,7 @@ bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct wlc_orig
return true;
}
bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers,
static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers,
uint32_t button, enum wlc_button_state state) {
if (state == WLC_BUTTON_STATE_PRESSED) {
swayc_t *c = find_container(&root_container, pointer_test, &mouse_origin);
@ -163,3 +162,29 @@ bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modi
}
return true;
}
struct wlc_interface interface = {
.output = {
.created = handle_output_created,
.destroyed = handle_output_destroyed,
.resolution = handle_output_resolution_change,
.focus = handle_output_focused
},
.view = {
.created = handle_view_created,
.destroyed = handle_view_destroyed,
.focus = handle_view_focus,
.request = {
.geometry = handle_view_geometry_request
}
},
.keyboard = {
.key = handle_key
},
.pointer = {
.motion = handle_pointer_motion,
.button = handle_pointer_button
}
};

View File

@ -4,21 +4,6 @@
#include <stdbool.h>
#include <wlc/wlc.h>
bool handle_output_created(wlc_handle output);
void handle_output_destroyed(wlc_handle output);
void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to);
void handle_output_focused(wlc_handle output, bool focus);
bool handle_view_created(wlc_handle view);
void handle_view_destroyed(wlc_handle view);
void handle_view_focus(wlc_handle view, bool focus);
void handle_view_geometry_request(wlc_handle view, const struct wlc_geometry* geometry);
bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers
*modifiers, uint32_t key, uint32_t sym, enum wlc_key_state state);
bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct wlc_origin *origin);
bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers,
uint32_t button, enum wlc_button_state state);
extern struct wlc_interface interface;
#endif

View File

@ -2,6 +2,8 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int colored = 1;
int v = 0;
@ -15,6 +17,16 @@ const char *verbosity_colors[] = {
void init_log(int verbosity) {
v = verbosity;
/* set FD_CLOEXEC flag to prevent programs called with exec to write into
* logs */
int i, flag;
int fd[] = { STDOUT_FILENO, STDIN_FILENO, STDERR_FILENO };
for (i = 0; i < 3; ++i) {
flag = fcntl(fd[i], F_GETFD);
if (flag != -1) {
fcntl(fd[i], F_SETFD, flag | FD_CLOEXEC);
}
}
}
void sway_log_colors(int mode) {

View File

@ -2,40 +2,22 @@
#include <stdlib.h>
#include <stdbool.h>
#include <wlc/wlc.h>
#include <sys/wait.h>
#include <signal.h>
#include "layout.h"
#include "config.h"
#include "log.h"
#include "handlers.h"
static void sigchld_handle(int signal);
int main(int argc, char **argv) {
init_log(L_DEBUG); // TODO: Control this with command line arg
init_layout();
static struct wlc_interface interface = {
.output = {
.created = handle_output_created,
.destroyed = handle_output_destroyed,
.resolution = handle_output_resolution_change,
.focus = handle_output_focused
},
.view = {
.created = handle_view_created,
.destroyed = handle_view_destroyed,
.focus = handle_view_focus,
.request = {
.geometry = handle_view_geometry_request
}
},
.keyboard = {
.key = handle_key
},
.pointer = {
.motion = handle_pointer_motion,
.button = handle_pointer_button
}
/* Signal handling */
signal(SIGCHLD, sigchld_handle);
};
setenv("WLC_DIM", "0", 0);
if (!wlc_init(&interface, argc, argv)) {
@ -50,3 +32,8 @@ int main(int argc, char **argv) {
wlc_run();
return 0;
}
static void sigchld_handle(int signal) {
(void) signal;
while (waitpid((pid_t)-1, 0, WNOHANG) > 0);
}

View File

@ -5,7 +5,7 @@
#include "layout.h"
#include "movement.h"
int move_focus(enum movement_direction direction) {
bool move_focus(enum movement_direction direction) {
swayc_t *current = get_focused_container(&root_container);
swayc_t *parent = current->parent;
@ -14,12 +14,12 @@ int move_focus(enum movement_direction direction) {
parent = parent->parent;
if (parent->type == C_ROOT) {
sway_log(L_DEBUG, "Focus cannot move to parent");
return 1;
return false;
} else {
sway_log(L_DEBUG, "Moving focus away from %p", current);
unfocus_all(parent);
focus_view(parent);
return 0;
return true;
}
}
@ -56,7 +56,7 @@ int move_focus(enum movement_direction direction) {
} else {
unfocus_all(&root_container);
focus_view(parent->children->items[desired]);
return 0;
return true;
}
}
if (!can_move) {
@ -65,7 +65,7 @@ int move_focus(enum movement_direction direction) {
parent = parent->parent;
if (parent->type == C_ROOT) {
// Nothing we can do
return 1;
return false;
}
}
}

View File

@ -12,6 +12,6 @@ enum movement_direction {
MOVE_PARENT
};
int move_focus(enum movement_direction direction);
bool move_focus(enum movement_direction direction);
#endif