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_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++) {
struct module_run_context *ctx = &run_ctx_left[i];
struct module *mod = bar->left.mods[i];
ctx->module = bar->left.mods[i];
ctx->abort_fd = run_ctx->abort_fd;
thrd_create(&thrd_left[i], (int (*)(void *))bar->left.mods[i]->run, ctx);
mod->abort_fd = run_ctx->abort_fd;
thrd_create(&thrd_left[i], (int (*)(void *))bar->left.mods[i]->run, mod);
}
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];
ctx->abort_fd = run_ctx->abort_fd;
thrd_create(&thrd_center[i], (int (*)(void *))bar->center.mods[i]->run, ctx);
mod->abort_fd = run_ctx->abort_fd;
thrd_create(&thrd_center[i], (int (*)(void *))bar->center.mods[i]->run, mod);
}
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];
ctx->abort_fd = run_ctx->abort_fd;
thrd_create(&thrd_right[i], (int (*)(void *))bar->right.mods[i]->run, ctx);
mod->abort_fd = run_ctx->abort_fd;
thrd_create(&thrd_right[i], (int (*)(void *))bar->right.mods[i]->run, mod);
}
LOG_DBG("all modules started");

View file

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

View file

@ -174,9 +174,8 @@ update_state(struct module *mod, snd_mixer_elem_t *elem)
}
static int
run(struct module_run_context *ctx)
run(struct module *mod)
{
struct module *mod = ctx->module;
struct private *m = mod->private;
snd_mixer_t *handle;
@ -225,7 +224,7 @@ run(struct module_run_context *ctx)
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);
poll(fds, fd_count + 1, -1);

View file

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

View file

@ -51,9 +51,9 @@ content(struct module *mod)
}
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);
while (true) {
@ -65,7 +65,7 @@ run(struct module_run_context *ctx)
time_t timeout = next_min - now;
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);
if (fds[0].revents & POLLIN)

View file

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

View file

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

View file

@ -258,9 +258,8 @@ update_status(struct module *mod)
}
static int
run(struct module_run_context *ctx)
run(struct module *mod)
{
struct module *mod = ctx->module;
const struct bar *bar = mod->bar;
struct private *m = mod->private;
@ -289,7 +288,7 @@ run(struct module_run_context *ctx)
if (m->conn != NULL)
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);
if (res == 1) {
@ -311,7 +310,7 @@ run(struct module_run_context *ctx)
/* Monitor for events from MPD */
while (true) {
struct pollfd fds[] = {
{.fd = ctx->abort_fd, .events = POLLIN},
{.fd = mod->abort_fd, .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
run(struct module_run_context *ctx)
run(struct module *mod)
{
struct module *mod = ctx->module;
struct private *m = mod->private;
m->nl_sock = netlink_connect();
@ -472,7 +471,7 @@ run(struct module_run_context *ctx)
/* Main loop */
while (true) {
struct pollfd fds[] = {
{.fd = ctx->abort_fd, .events = POLLIN},
{.fd = mod->abort_fd, .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
run(struct module_run_context *ctx)
run(struct module *mod)
{
struct module *mod = ctx->module;
struct private *m = mod->private;
struct udev *udev = udev_new();
@ -503,7 +502,7 @@ run(struct module_run_context *ctx)
while (true) {
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 = mount_info_fd, .events = POLLPRI},
};

View file

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

View file

@ -181,9 +181,8 @@ update_title(struct module *mod)
}
static int
run(struct module_run_context *ctx)
run(struct module *mod)
{
struct module *mod = ctx->module;
struct private *m = mod->private;
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);
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}};
poll(fds, 2, -1);