module: remove module_run_context

Store abort_fd directly in the module struct instead. This then allows
us to pass the module pointer as-is to the modules' run functions.
This commit is contained in:
Daniel Eklöf 2019-01-13 15:34:59 +01:00
parent acdeff3b6e
commit 76d135e257
13 changed files with 57 additions and 77 deletions

28
bar.c
View file

@ -533,33 +533,23 @@ run(struct bar_run_context *run_ctx)
thrd_t thrd_center[bar->center.count]; thrd_t thrd_center[bar->center.count];
thrd_t thrd_right[bar->right.count]; thrd_t thrd_right[bar->right.count];
struct module_run_context run_ctx_left[bar->left.count];
struct module_run_context run_ctx_center[bar->center.count];
struct module_run_context run_ctx_right[bar->right.count];
for (size_t i = 0; i < bar->left.count; i++) { for (size_t i = 0; i < bar->left.count; i++) {
struct module_run_context *ctx = &run_ctx_left[i]; struct module *mod = bar->left.mods[i];
ctx->module = bar->left.mods[i]; mod->abort_fd = run_ctx->abort_fd;
ctx->abort_fd = run_ctx->abort_fd; thrd_create(&thrd_left[i], (int (*)(void *))bar->left.mods[i]->run, mod);
thrd_create(&thrd_left[i], (int (*)(void *))bar->left.mods[i]->run, ctx);
} }
for (size_t i = 0; i < bar->center.count; i++) { for (size_t i = 0; i < bar->center.count; i++) {
struct module_run_context *ctx = &run_ctx_center[i]; struct module *mod = bar->center.mods[i];
ctx->module = bar->center.mods[i]; mod->abort_fd = run_ctx->abort_fd;
ctx->abort_fd = run_ctx->abort_fd; thrd_create(&thrd_center[i], (int (*)(void *))bar->center.mods[i]->run, mod);
thrd_create(&thrd_center[i], (int (*)(void *))bar->center.mods[i]->run, ctx);
} }
for (size_t i = 0; i < bar->right.count; i++) { for (size_t i = 0; i < bar->right.count; i++) {
struct module_run_context *ctx = &run_ctx_right[i]; struct module *mod = bar->right.mods[i];
ctx->module = bar->right.mods[i]; mod->abort_fd = run_ctx->abort_fd;
ctx->abort_fd = run_ctx->abort_fd; thrd_create(&thrd_right[i], (int (*)(void *))bar->right.mods[i]->run, mod);
thrd_create(&thrd_right[i], (int (*)(void *))bar->right.mods[i]->run, ctx);
} }
LOG_DBG("all modules started"); LOG_DBG("all modules started");

View file

@ -22,18 +22,15 @@ struct module_info {
{NULL, false, NULL} {NULL, false, NULL}
}; };
struct module_run_context {
struct module *module;
int abort_fd;
};
struct module { struct module {
const struct bar *bar; const struct bar *bar;
int abort_fd;
mtx_t lock; mtx_t lock;
void *private; void *private;
int (*run)(struct module_run_context *ctx); int (*run)(struct module *mod);
void (*destroy)(struct module *module); void (*destroy)(struct module *module);
/* /*

View file

@ -174,9 +174,8 @@ update_state(struct module *mod, snd_mixer_elem_t *elem)
} }
static int static int
run(struct module_run_context *ctx) run(struct module *mod)
{ {
struct module *mod = ctx->module;
struct private *m = mod->private; struct private *m = mod->private;
snd_mixer_t *handle; snd_mixer_t *handle;
@ -225,7 +224,7 @@ run(struct module_run_context *ctx)
struct pollfd fds[1 + fd_count]; struct pollfd fds[1 + fd_count];
fds[0] = (struct pollfd){.fd = ctx->abort_fd, .events = POLLIN}; fds[0] = (struct pollfd){.fd = mod->abort_fd, .events = POLLIN};
snd_mixer_poll_descriptors(handle, &fds[1], fd_count); snd_mixer_poll_descriptors(handle, &fds[1], fd_count);
poll(fds, fd_count + 1, -1); poll(fds, fd_count + 1, -1);

View file

@ -138,10 +138,10 @@ initialize(struct private *m)
} }
static int static int
run(struct module_run_context *ctx) run(struct module *mod)
{ {
const struct bar *bar = ctx->module->bar; const struct bar *bar = mod->bar;
struct private *m = ctx->module->private; struct private *m = mod->private;
int current_fd = initialize(m); int current_fd = initialize(m);
if (current_fd == -1) if (current_fd == -1)
@ -167,7 +167,7 @@ run(struct module_run_context *ctx)
while (true) { while (true) {
struct pollfd fds[] = { struct pollfd fds[] = {
{.fd = ctx->abort_fd, .events = POLLIN}, {.fd = mod->abort_fd, .events = POLLIN},
{.fd = udev_monitor_get_fd(mon), .events = POLLIN}, {.fd = udev_monitor_get_fd(mon), .events = POLLIN},
}; };
poll(fds, 2, -1); poll(fds, 2, -1);
@ -184,9 +184,9 @@ run(struct module_run_context *ctx)
if (!is_us) if (!is_us)
continue; continue;
mtx_lock(&ctx->module->lock); mtx_lock(&mod->lock);
m->current_brightness = readint_from_fd(current_fd); m->current_brightness = readint_from_fd(current_fd);
mtx_unlock(&ctx->module->lock); mtx_unlock(&mod->lock);
bar->refresh(bar); bar->refresh(bar);
} }

View file

@ -242,10 +242,10 @@ update_status(struct module *mod, int capacity_fd, int energy_fd, int power_fd,
} }
static int static int
run(struct module_run_context *ctx) run(struct module *mod)
{ {
const struct bar *bar = ctx->module->bar; const struct bar *bar = mod->bar;
struct private *m = ctx->module->private; struct private *m = mod->private;
int base_dir_fd = initialize(m); int base_dir_fd = initialize(m);
if (base_dir_fd == -1) if (base_dir_fd == -1)
@ -273,12 +273,12 @@ run(struct module_run_context *ctx)
udev_monitor_filter_add_match_subsystem_devtype(mon, "power_supply", NULL); udev_monitor_filter_add_match_subsystem_devtype(mon, "power_supply", NULL);
udev_monitor_enable_receiving(mon); udev_monitor_enable_receiving(mon);
update_status(ctx->module, capacity_fd, energy_fd, power_fd, status_fd); update_status(mod, capacity_fd, energy_fd, power_fd, status_fd);
bar->refresh(bar); bar->refresh(bar);
while (true) { while (true) {
struct pollfd fds[] = { struct pollfd fds[] = {
{.fd = ctx->abort_fd, .events = POLLIN}, {.fd = mod->abort_fd, .events = POLLIN},
{.fd = udev_monitor_get_fd(mon), .events = POLLIN}, {.fd = udev_monitor_get_fd(mon), .events = POLLIN},
}; };
poll(fds, 2, m->poll_interval * 1000); poll(fds, 2, m->poll_interval * 1000);
@ -299,7 +299,7 @@ run(struct module_run_context *ctx)
continue; continue;
} }
update_status(ctx->module, capacity_fd, energy_fd, power_fd, status_fd); update_status(mod, capacity_fd, energy_fd, power_fd, status_fd);
bar->refresh(bar); bar->refresh(bar);
} }

View file

@ -51,9 +51,9 @@ content(struct module *mod)
} }
static int static int
run(struct module_run_context *ctx) run(struct module *mod)
{ {
const struct bar *bar = ctx->module->bar; const struct bar *bar = mod->bar;
bar->refresh(bar); bar->refresh(bar);
while (true) { while (true) {
@ -65,7 +65,7 @@ run(struct module_run_context *ctx)
time_t timeout = next_min - now; time_t timeout = next_min - now;
assert(timeout >= 0 && timeout <= 60); assert(timeout >= 0 && timeout <= 60);
struct pollfd fds[] = {{.fd = ctx->abort_fd, .events = POLLIN}}; struct pollfd fds[] = {{.fd = mod->abort_fd, .events = POLLIN}};
poll(fds, 1, timeout * 1000); poll(fds, 1, timeout * 1000);
if (fds[0].revents & POLLIN) if (fds[0].revents & POLLIN)

View file

@ -367,9 +367,9 @@ handle_workspace_event(struct private *m, const struct json_object *json)
} }
static int static int
run(struct module_run_context *ctx) run(struct module *mod)
{ {
struct private *m = ctx->module->private; struct private *m = mod->private;
struct sockaddr_un addr = {.sun_family = AF_UNIX}; struct sockaddr_un addr = {.sun_family = AF_UNIX};
{ {
@ -410,7 +410,7 @@ run(struct module_run_context *ctx)
while (true) { while (true) {
struct pollfd fds[] = { struct pollfd fds[] = {
{.fd = ctx->abort_fd, .events = POLLIN}, {.fd = mod->abort_fd, .events = POLLIN},
{.fd = sock, .events = POLLIN} {.fd = sock, .events = POLLIN}
}; };
@ -469,7 +469,7 @@ run(struct module_run_context *ctx)
break; break;
} }
mtx_lock(&ctx->module->lock); mtx_lock(&mod->lock);
switch (hdr->type) { switch (hdr->type) {
case I3_IPC_REPLY_TYPE_VERSION: case I3_IPC_REPLY_TYPE_VERSION:
@ -482,12 +482,12 @@ run(struct module_run_context *ctx)
case I3_IPC_REPLY_TYPE_WORKSPACES: case I3_IPC_REPLY_TYPE_WORKSPACES:
handle_get_workspaces_reply(m, json); handle_get_workspaces_reply(m, json);
ctx->module->bar->refresh(ctx->module->bar); mod->bar->refresh(mod->bar);
break; break;
case I3_IPC_EVENT_WORKSPACE: case I3_IPC_EVENT_WORKSPACE:
handle_workspace_event(m, json); handle_workspace_event(m, json);
ctx->module->bar->refresh(ctx->module->bar); mod->bar->refresh(mod->bar);
break; break;
case I3_IPC_EVENT_OUTPUT: case I3_IPC_EVENT_OUTPUT:
@ -502,7 +502,7 @@ run(struct module_run_context *ctx)
default: assert(false); default: assert(false);
} }
mtx_unlock(&ctx->module->lock); mtx_unlock(&mod->lock);
json_object_put(json); json_object_put(json);
json_tokener_free(tokener); json_tokener_free(tokener);

View file

@ -27,7 +27,7 @@ content(struct module *mod)
} }
static int static int
run(struct module_run_context *ctx) run(struct module *mod)
{ {
return 0; return 0;
} }

View file

@ -258,9 +258,8 @@ update_status(struct module *mod)
} }
static int static int
run(struct module_run_context *ctx) run(struct module *mod)
{ {
struct module *mod = ctx->module;
const struct bar *bar = mod->bar; const struct bar *bar = mod->bar;
struct private *m = mod->private; struct private *m = mod->private;
@ -289,7 +288,7 @@ run(struct module_run_context *ctx)
if (m->conn != NULL) if (m->conn != NULL)
break; break;
struct pollfd fds[] = {{.fd = ctx->abort_fd, .events = POLLIN}}; struct pollfd fds[] = {{.fd = mod->abort_fd, .events = POLLIN}};
int res = poll(fds, 1, 10 * 1000); int res = poll(fds, 1, 10 * 1000);
if (res == 1) { if (res == 1) {
@ -311,7 +310,7 @@ run(struct module_run_context *ctx)
/* Monitor for events from MPD */ /* Monitor for events from MPD */
while (true) { while (true) {
struct pollfd fds[] = { struct pollfd fds[] = {
{.fd = ctx->abort_fd, .events = POLLIN}, {.fd = mod->abort_fd, .events = POLLIN},
{.fd = mpd_connection_get_fd(m->conn), .events = POLLIN}, {.fd = mpd_connection_get_fd(m->conn), .events = POLLIN},
}; };

View file

@ -454,9 +454,8 @@ parse_reply(struct module *mod, const struct nlmsghdr *hdr, size_t len)
} }
static int static int
run(struct module_run_context *ctx) run(struct module *mod)
{ {
struct module *mod = ctx->module;
struct private *m = mod->private; struct private *m = mod->private;
m->nl_sock = netlink_connect(); m->nl_sock = netlink_connect();
@ -472,7 +471,7 @@ run(struct module_run_context *ctx)
/* Main loop */ /* Main loop */
while (true) { while (true) {
struct pollfd fds[] = { struct pollfd fds[] = {
{.fd = ctx->abort_fd, .events = POLLIN}, {.fd = mod->abort_fd, .events = POLLIN},
{.fd = m->nl_sock, .events = POLLIN} {.fd = m->nl_sock, .events = POLLIN}
}; };

View file

@ -445,9 +445,8 @@ handle_udev_event(struct module *mod, struct udev_device *dev)
} }
static int static int
run(struct module_run_context *ctx) run(struct module *mod)
{ {
struct module *mod = ctx->module;
struct private *m = mod->private; struct private *m = mod->private;
struct udev *udev = udev_new(); struct udev *udev = udev_new();
@ -503,7 +502,7 @@ run(struct module_run_context *ctx)
while (true) { while (true) {
struct pollfd fds[] = { struct pollfd fds[] = {
{.fd = ctx->abort_fd, .events = POLLIN}, {.fd = mod->abort_fd, .events = POLLIN},
{.fd = udev_monitor_get_fd(dev_mon), .events = POLLIN}, {.fd = udev_monitor_get_fd(dev_mon), .events = POLLIN},
{.fd = mount_info_fd, .events = POLLPRI}, {.fd = mount_info_fd, .events = POLLPRI},
}; };

View file

@ -285,12 +285,10 @@ register_for_events(xcb_connection_t *conn)
} }
static bool static bool
event_loop(struct module_run_context *ctx, xcb_connection_t *conn, event_loop(struct module *mod, xcb_connection_t *conn, int xkb_event_base)
int xkb_event_base)
{ {
struct module *mod = ctx->module; const struct bar *bar = mod->bar;
struct private *m = mod->private; struct private *m = mod->private;
const struct bar *bar = ctx->module->bar;
bool ret = false; bool ret = false;
bool has_error = false; bool has_error = false;
@ -300,7 +298,7 @@ event_loop(struct module_run_context *ctx, xcb_connection_t *conn,
while (!has_error) { while (!has_error) {
struct pollfd pfds[] = { struct pollfd pfds[] = {
{.fd = ctx->abort_fd, .events = POLLIN }, {.fd = mod->abort_fd, .events = POLLIN },
{.fd = xcb_fd, .events = POLLIN | POLLHUP } {.fd = xcb_fd, .events = POLLIN | POLLHUP }
}; };
@ -400,9 +398,9 @@ event_loop(struct module_run_context *ctx, xcb_connection_t *conn,
} }
static bool static bool
talk_to_xkb(struct module_run_context *ctx, xcb_connection_t *conn) talk_to_xkb(struct module *mod, xcb_connection_t *conn)
{ {
struct private *m = ctx->module->private; struct private *m = mod->private;
if (!xkb_enable(conn)) if (!xkb_enable(conn))
return false; return false;
@ -428,17 +426,17 @@ talk_to_xkb(struct module_run_context *ctx, xcb_connection_t *conn)
return false; return false;
} }
mtx_lock(&ctx->module->lock); mtx_lock(&mod->lock);
m->layouts = layouts; m->layouts = layouts;
m->current = current; m->current = current;
mtx_unlock(&ctx->module->lock); mtx_unlock(&mod->lock);
ctx->module->bar->refresh(ctx->module->bar); mod->bar->refresh(mod->bar);
return event_loop(ctx, conn, xkb_event_base); return event_loop(mod, conn, xkb_event_base);
} }
static int static int
run(struct module_run_context *ctx) run(struct module *mod)
{ {
xcb_connection_t *conn = xcb_connect(NULL, NULL); xcb_connection_t *conn = xcb_connect(NULL, NULL);
if (conn == NULL) { if (conn == NULL) {
@ -446,7 +444,7 @@ run(struct module_run_context *ctx)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
int ret = talk_to_xkb(ctx, conn) ? EXIT_SUCCESS : EXIT_FAILURE; int ret = talk_to_xkb(mod, conn) ? EXIT_SUCCESS : EXIT_FAILURE;
xcb_disconnect(conn); xcb_disconnect(conn);
return ret; return ret;

View file

@ -181,9 +181,8 @@ update_title(struct module *mod)
} }
static int static int
run(struct module_run_context *ctx) run(struct module *mod)
{ {
struct module *mod = ctx->module;
struct private *m = mod->private; struct private *m = mod->private;
m->conn = xcb_connect(NULL, NULL); m->conn = xcb_connect(NULL, NULL);
@ -221,7 +220,7 @@ run(struct module_run_context *ctx)
int xcb_fd = xcb_get_file_descriptor(m->conn); int xcb_fd = xcb_get_file_descriptor(m->conn);
while (true) { while (true) {
struct pollfd fds[] = {{.fd = ctx->abort_fd, .events = POLLIN}, struct pollfd fds[] = {{.fd = mod->abort_fd, .events = POLLIN},
{.fd = xcb_fd, .events = POLLIN}}; {.fd = xcb_fd, .events = POLLIN}};
poll(fds, 2, -1); poll(fds, 2, -1);