Remove usage of assert()

This commit is contained in:
Jacob 2023-08-08 20:05:15 +02:00
parent eef42559fe
commit d01d5177ad
3 changed files with 54 additions and 27 deletions

View file

@ -10,7 +10,10 @@ struct wl_buffer *create_buffer(struct state *state) {
size_t siz = stride * height; size_t siz = stride * height;
int fd = create_shm_file(siz); int fd = create_shm_file(siz);
assert(fd != -1);
if (fd == -1) {
die("create_buffer(): fd == -1");
}
void *data = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); void *data = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
@ -86,7 +89,7 @@ char *wl_clipboard(void) {
fp = popen("which wl-paste > /dev/null && wl-paste -t text/plain", "r"); fp = popen("which wl-paste > /dev/null && wl-paste -t text/plain", "r");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "spmenu: Failed to open command\n"); fprintf(stderr, "wl_clipboard(): Failed to open command\n");
return NULL; return NULL;
} }
@ -244,7 +247,7 @@ void keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard, uint32_t forma
char *map_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); char *map_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
if (map_shm == MAP_FAILED) { if (map_shm == MAP_FAILED) {
close(fd); close(fd);
die("MAP_FAILED"); die("keyboard_keymap(): MAP_FAILED");
return; return;
} }
state->xkb_keymap = xkb_keymap_new_from_string(state->xkb_context, map_shm, XKB_KEYMAP_FORMAT_TEXT_V1, 0); state->xkb_keymap = xkb_keymap_new_from_string(state->xkb_context, map_shm, XKB_KEYMAP_FORMAT_TEXT_V1, 0);
@ -480,11 +483,11 @@ void create_drawable(struct state *state) {
state->buffer = create_buffer(state); state->buffer = create_buffer(state);
if (draw == NULL) { if (draw == NULL) {
die("spmenu: draw == NULL"); die("create_drawable(): A drawable was not created.");
} }
if (state->buffer == NULL) { if (state->buffer == NULL) {
die("state->buffer == NULL"); die("create_drawable(): Buffer state->buffer could not be created.");
} }
draw_create_surface_wl(draw, state->data, state->width, state->height); draw_create_surface_wl(draw, state->data, state->width, state->height);
@ -569,13 +572,13 @@ int await_dispatch(struct state *state) {
do { do {
if (wl_display_flush(state->display) == -1 && errno != EAGAIN) { if (wl_display_flush(state->display) == -1 && errno != EAGAIN) {
fprintf(stderr, "spmenu: wl_display_flush failed: %s\n", strerror(errno)); fprintf(stderr, "await_dispatch(): wl_display_flush failed: %s\n", strerror(errno));
break; break;
} }
} while (errno == EAGAIN); } while (errno == EAGAIN);
if (poll(fds, nfds, -1) < 0) { if (poll(fds, nfds, -1) < 0) {
fprintf(stderr, "spmenu: poll failed: %s\n", strerror(errno)); fprintf(stderr, "await_dispatch(): poll failed: %s\n", strerror(errno));
break; break;
} }
@ -608,13 +611,17 @@ int init_disp(struct state *state) {
wl_registry_add_listener(registry, &registry_listener, state); wl_registry_add_listener(registry, &registry_listener, state);
wl_display_roundtrip(state->display); wl_display_roundtrip(state->display);
assert(state->compositor != NULL); if (state->compositor == NULL) {
die("init_disp(): No Wayland compositor.");
if (state->layer_shell == NULL) {
die("spmenu: Your compositor does not implement the wlr-layer-shell protocol. Run spmenu in X11 mode.");
} }
assert(state->shm != NULL); if (state->layer_shell == NULL) {
die("init_disp(): Your compositor does not implement the wlr-layer-shell protocol.");
}
if (state->shm == NULL) {
die("init_disp(): Could not create shared memory buffer.\n");
}
wl_display_roundtrip(state->display); wl_display_roundtrip(state->display);
@ -653,7 +660,7 @@ void resizeclient_wl(struct state *state) {
state->buffer = create_buffer(state); state->buffer = create_buffer(state);
if (draw == NULL) { if (draw == NULL) {
die("spmenu: draw == NULL"); die("resizeclient_wl(): Draw_t is NULL.");
} }
if (state->buffer == NULL) { if (state->buffer == NULL) {
@ -691,12 +698,19 @@ int init_keys(struct state *state) {
} }
int create_layer(struct state *state, char *name) { int create_layer(struct state *state, char *name) {
assert(state->compositor != NULL); if (state->compositor == NULL) {
die("create_layer(): No Wayland compositor.");
}
state->surface = wl_compositor_create_surface(state->compositor); state->surface = wl_compositor_create_surface(state->compositor);
assert(state->surface != NULL); if (state->surface == NULL) {
assert(state->layer_shell != NULL); die("create_layer(): Could not create surface state->surface using state->compositor");
}
if (state->layer_shell == NULL) {
die("create_layer(): Your compositor does not implement the wlr-layer-shell protocol. Run spmenu in X11 mode.");
}
wl_surface_add_listener(state->surface, &surface_listener, state); wl_surface_add_listener(state->surface, &surface_listener, state);
state->layer_surface = zwlr_layer_shell_v1_get_layer_surface( state->layer_surface = zwlr_layer_shell_v1_get_layer_surface(
@ -706,13 +720,18 @@ int create_layer(struct state *state, char *name) {
ZWLR_LAYER_SHELL_V1_LAYER_TOP, ZWLR_LAYER_SHELL_V1_LAYER_TOP,
name); name);
assert(state->layer_surface != NULL); if (state->layer_surface == NULL) {
die("create_layer(): Could not get layer_surface state->layer_surface");
}
return 0; return 0;
} }
int anchor_layer(struct state *state, int position) { int anchor_layer(struct state *state, int position) {
assert(state->layer_surface != NULL); if (state->layer_surface == NULL) {
die("anchor_layer(): Could not get layer_surface state->layer_surface");
}
zwlr_layer_surface_v1_set_anchor( zwlr_layer_surface_v1_set_anchor(
state->layer_surface, state->layer_surface,
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
@ -725,7 +744,9 @@ int anchor_layer(struct state *state, int position) {
} }
int set_layer_size(struct state *state, int32_t width, int32_t height) { int set_layer_size(struct state *state, int32_t width, int32_t height) {
assert(state->layer_surface != NULL); if (state->layer_surface != NULL) {
die("set_layer_size(): Could not get layer_surface state->layer_surface");
}
zwlr_layer_surface_v1_set_size(state->layer_surface, width, height); zwlr_layer_surface_v1_set_size(state->layer_surface, width, height);
@ -733,7 +754,9 @@ int set_layer_size(struct state *state, int32_t width, int32_t height) {
} }
int set_exclusive_zone(struct state *state, unsigned int val) { int set_exclusive_zone(struct state *state, unsigned int val) {
assert(state->layer_surface != NULL); if (state->layer_surface == NULL) {
die("set_exclusive_zone(): Could not get layer_surface state->layer_surface");
}
zwlr_layer_surface_v1_set_exclusive_zone(state->layer_surface, val); zwlr_layer_surface_v1_set_exclusive_zone(state->layer_surface, val);
@ -741,7 +764,9 @@ int set_exclusive_zone(struct state *state, unsigned int val) {
} }
int set_keyboard(struct state *state, int interactivity) { int set_keyboard(struct state *state, int interactivity) {
assert(state->layer_surface != NULL); if (state->layer_surface == NULL) {
die("set_keyboard(): Could not get layer_surface state->layer_surface");
}
zwlr_layer_surface_v1_set_keyboard_interactivity(state->layer_surface, interactivity ? true : false); zwlr_layer_surface_v1_set_keyboard_interactivity(state->layer_surface, interactivity ? true : false);
@ -749,7 +774,9 @@ int set_keyboard(struct state *state, int interactivity) {
} }
int add_layer_listener(struct state *state) { int add_layer_listener(struct state *state) {
assert(state->layer_surface != NULL); if (state->layer_surface == NULL) {
die("add_layer_listener(): Could not get layer_surface state->layer_surface");
}
zwlr_layer_surface_v1_add_listener(state->layer_surface, &layer_surface_listener, state); zwlr_layer_surface_v1_add_listener(state->layer_surface, &layer_surface_listener, state);
@ -757,8 +784,9 @@ int add_layer_listener(struct state *state) {
} }
int set_visible_layer(struct state *state) { int set_visible_layer(struct state *state) {
assert(state->layer_surface != NULL); if (state->layer_surface == NULL) {
assert(state->surface != NULL); die("set_visible_layer(): Could not get layer_surface state->layer_surface");
}
wl_surface_commit(state->surface); wl_surface_commit(state->surface);

View file

@ -8,7 +8,6 @@
#include <sys/mman.h> #include <sys/mman.h>
#include <time.h> #include <time.h>
#include <sys/timerfd.h> #include <sys/timerfd.h>
#include <assert.h>
#include <signal.h> #include <signal.h>
#include <poll.h> #include <poll.h>
#include <wayland-client.h> #include <wayland-client.h>

View file

@ -518,11 +518,11 @@ void get_mh(void) {
} }
void set_mode(void) { void set_mode(void) {
if (!type) { // no typing allowed, require normal mode if (!type) {
sp.mode = 0; sp.mode = 0;
} }
// set default mode, must be done before the event loop or keybindings will not work // set default mode
if (mode) { if (mode) {
sp.mode = 1; sp.mode = 1;
sp.allowkeys = 1; sp.allowkeys = 1;