module: remove module_expose_context

This commit is contained in:
Daniel Eklöf 2019-01-13 14:52:28 +01:00
parent 9276724113
commit 33cba8b0ba
3 changed files with 65 additions and 83 deletions

105
bar.c
View file

@ -43,17 +43,17 @@ struct private {
struct {
struct module **mods;
struct module_expose_context *exps;
struct exposable **exps;
size_t count;
} left;
struct {
struct module **mods;
struct module_expose_context *exps;
struct exposable **exps;
size_t count;
} center;
struct {
struct module **mods;
struct module_expose_context *exps;
struct exposable **exps;
size_t count;
} right;
@ -89,21 +89,21 @@ calculate_widths(const struct private *b, int *left, int *center, int *right)
*right = 0;
for (size_t i = 0; i < b->left.count; i++) {
struct module_expose_context *e = &b->left.exps[i];
assert(e->exposable != NULL);
*left += b->left_spacing + e->exposable->width + b->right_spacing;
struct exposable *e = b->left.exps[i];
assert(e != NULL);
*left += b->left_spacing + e->width + b->right_spacing;
}
for (size_t i = 0; i < b->center.count; i++) {
struct module_expose_context *e = &b->center.exps[i];
assert(e->exposable != NULL);
*center += b->left_spacing + e->exposable->width + b->right_spacing;
struct exposable *e = b->center.exps[i];
assert(e != NULL);
*center += b->left_spacing + e->width + b->right_spacing;
}
for (size_t i = 0; i < b->right.count; i++) {
struct module_expose_context *e = &b->right.exps[i];
assert(e->exposable != NULL);
*right += b->left_spacing + e->exposable->width + b->right_spacing;
struct exposable *e = b->right.exps[i];
assert(e != NULL);
*right += b->left_spacing + e->width + b->right_spacing;
}
/* No spacing on the edges (that's what the margins are for) */
@ -143,32 +143,32 @@ expose(const struct bar *_bar)
for (size_t i = 0; i < bar->left.count; i++) {
struct module *m = bar->left.mods[i];
struct module_expose_context *e = &bar->left.exps[i];
struct exposable *e = bar->left.exps[i];
if (e->exposable != NULL)
if (e != NULL)
m->end_expose(m, e);
*e = m->begin_expose(m);
bar->left.exps[i] = m->begin_expose(m);
}
for (size_t i = 0; i < bar->center.count; i++) {
struct module *m = bar->center.mods[i];
struct module_expose_context *e = &bar->center.exps[i];
struct exposable *e = bar->center.exps[i];
if (e->exposable != NULL)
if (e != NULL)
m->end_expose(m, e);
*e = m->begin_expose(m);
bar->center.exps[i] = m->begin_expose(m);
}
for (size_t i = 0; i < bar->right.count; i++) {
struct module *m = bar->right.mods[i];
struct module_expose_context *e = &bar->right.exps[i];
struct exposable *e = bar->right.exps[i];
if (e->exposable != NULL)
if (e != NULL)
m->end_expose(m, e);
*e = m->begin_expose(m);
bar->right.exps[i] = m->begin_expose(m);
}
int left_width, center_width, right_width;
@ -178,17 +178,17 @@ expose(const struct bar *_bar)
int x = bar->border.width + bar->left_margin - bar->left_spacing;
for (size_t i = 0; i < bar->left.count; i++) {
const struct module *m = bar->left.mods[i];
const struct module_expose_context *e = &bar->left.exps[i];
const struct exposable *e = bar->left.exps[i];
m->expose(m, e, bar->cairo, x + bar->left_spacing, y, bar->height);
x += bar->left_spacing + e->exposable->width + bar->right_spacing;
x += bar->left_spacing + e->width + bar->right_spacing;
}
x = bar->width / 2 - center_width / 2 - bar->left_spacing;
for (size_t i = 0; i < bar->center.count; i++) {
const struct module *m = bar->center.mods[i];
const struct module_expose_context *e = &bar->center.exps[i];
const struct exposable *e = bar->center.exps[i];
m->expose(m, e, bar->cairo, x + bar->left_spacing, y, bar->height);
x += bar->left_spacing + e->exposable->width + bar->right_spacing;
x += bar->left_spacing + e->width + bar->right_spacing;
}
x = bar->width - (
@ -199,9 +199,9 @@ expose(const struct bar *_bar)
for (size_t i = 0; i < bar->right.count; i++) {
const struct module *m = bar->right.mods[i];
const struct module_expose_context *e = &bar->right.exps[i];
const struct exposable *e = bar->right.exps[i];
m->expose(m, e, bar->cairo, x + bar->left_spacing, y, bar->height);
x += bar->left_spacing + e->exposable->width + bar->right_spacing;
x += bar->left_spacing + e->width + bar->right_spacing;
}
cairo_surface_flush(bar->cairo_surface);
@ -276,47 +276,44 @@ on_mouse(struct bar *bar, enum mouse_event event, int x, int y)
int mx = b->border.width + b->left_margin - b->left_spacing;
for (size_t i = 0; i < b->left.count; i++) {
const struct module_expose_context *e = &b->left.exps[i];
struct exposable *e = b->left.exps[i];
mx += b->left_spacing;
if (x >= mx && x < mx + e->exposable->width) {
assert(e->exposable != NULL);
if (e->exposable->on_mouse != NULL)
e->exposable->on_mouse(e->exposable, bar, event, x - mx, y);
if (x >= mx && x < mx + e->width) {
if (e->on_mouse != NULL)
e->on_mouse(e, bar, event, x - mx, y);
return;
}
mx += e->exposable->width + b->right_spacing;
mx += e->width + b->right_spacing;
}
mx = b->width / 2 - center_width / 2 - b->left_spacing;
for (size_t i = 0; i < b->center.count; i++) {
const struct module_expose_context *e = &b->center.exps[i];
struct exposable *e = b->center.exps[i];
mx += b->left_spacing;
if (x >= mx && x < mx + e->exposable->width) {
assert(e->exposable != NULL);
if (e->exposable->on_mouse != NULL)
e->exposable->on_mouse(e->exposable, bar, event, x - mx, y);
if (x >= mx && x < mx + e->width) {
if (e->on_mouse != NULL)
e->on_mouse(e, bar, event, x - mx, y);
return;
}
mx += e->exposable->width + b->right_spacing;
mx += e->width + b->right_spacing;
}
mx = b->width - (right_width + b->left_spacing + b->right_margin + b->border.width);
for (size_t i = 0; i < b->right.count; i++) {
const struct module_expose_context *e = &b->right.exps[i];
struct exposable *e = b->right.exps[i];
mx += b->left_spacing;
if (x >= mx && x < mx + e->exposable->width) {
assert(e->exposable != NULL);
if (e->exposable->on_mouse != NULL)
e->exposable->on_mouse(e->exposable, bar, event, x - mx, y);
if (x >= mx && x < mx + e->width) {
if (e->on_mouse != NULL)
e->on_mouse(e, bar, event, x - mx, y);
return;
}
mx += e->exposable->width + b->right_spacing;
mx += e->width + b->right_spacing;
}
set_cursor(bar, "left_ptr");
@ -678,25 +675,25 @@ run(struct bar_run_context *run_ctx)
for (size_t i = 0; i < bar->left.count; i++) {
struct module *m = bar->left.mods[i];
struct module_expose_context *e = &bar->left.exps[i];
struct exposable *e = bar->left.exps[i];
if (e->exposable != NULL)
if (e != NULL)
m->end_expose(m, e);
m->destroy(m);
}
for (size_t i = 0; i < bar->center.count; i++) {
struct module *m = bar->center.mods[i];
struct module_expose_context *e = &bar->center.exps[i];
struct exposable *e = bar->center.exps[i];
if (e->exposable != NULL)
if (e != NULL)
m->end_expose(m, e);
m->destroy(m);
}
for (size_t i = 0; i < bar->right.count; i++) {
struct module *m = bar->right.mods[i];
struct module_expose_context *e = &bar->right.exps[i];
struct exposable *e = bar->right.exps[i];
if (e->exposable != NULL)
if (e != NULL)
m->end_expose(m, e);
m->destroy(m);
}
@ -771,15 +768,15 @@ bar_new(const struct bar_config *config)
for (size_t i = 0; i < priv->left.count; i++) {
priv->left.mods[i] = config->left.mods[i];
priv->left.exps[i].exposable = NULL;
priv->left.exps[i] = NULL;
}
for (size_t i = 0; i < priv->center.count; i++) {
priv->center.mods[i] = config->center.mods[i];
priv->center.exps[i].exposable = NULL;
priv->center.exps[i] = NULL;
}
for (size_t i = 0; i < priv->right.count; i++) {
priv->right.mods[i] = config->right.mods[i];
priv->right.exps[i].exposable = NULL;
priv->right.exps[i] = NULL;
}
struct bar *bar = malloc(sizeof(*bar));

View file

@ -37,29 +37,23 @@ module_signal_ready(struct module_run_context *ctx)
write(ctx->ready_fd, &(uint64_t){1}, sizeof(uint64_t));
}
struct module_expose_context
struct exposable *
module_default_begin_expose(struct module *mod)
{
struct exposable *e = mod->content(mod);
e->begin_expose(e);
return (struct module_expose_context){
.exposable = e,
.private = NULL,
};
return e;
}
void
module_default_expose(const struct module *mod,
const struct module_expose_context *ctx, cairo_t *cr,
int x, int y, int height)
module_default_expose(const struct module *mod, const struct exposable *exposable,
cairo_t *cr, int x, int y, int height)
{
ctx->exposable->expose(ctx->exposable, cr, x, y, height);
exposable->expose(exposable, cr, x, y, height);
}
void
module_default_end_expose(const struct module *mod,
struct module_expose_context *ctx)
module_default_end_expose(const struct module *mod, struct exposable *exposable)
{
ctx->exposable->destroy(ctx->exposable);
exposable->destroy(exposable);
}

View file

@ -28,11 +28,6 @@ struct module_run_context {
int abort_fd;
};
struct module_expose_context {
struct exposable *exposable;
void *private;
};
struct module {
const struct bar *bar;
mtx_t lock;
@ -72,11 +67,10 @@ struct module {
* (module_default_*) is good enough. In this case, implement
* 'content()' instead (see above).
*/
struct module_expose_context (*begin_expose)(struct module *mod);
void (*expose)(const struct module *mod,
const struct module_expose_context *ctx,
struct exposable *(*begin_expose)(struct module *mod);
void (*expose)(const struct module *mod, const struct exposable *exposable,
cairo_t *cr, int x, int y, int height);
void (*end_expose)(const struct module *mod, struct module_expose_context *ctx);
void (*end_expose)(const struct module *mod, struct exposable *exosable);
};
@ -85,13 +79,10 @@ void module_signal_ready(struct module_run_context *ctx);
void module_default_destroy(struct module *mod);
struct module_expose_context module_default_begin_expose(
struct module *mod);
struct exposable *module_default_begin_expose(struct module *mod);
void module_default_expose(
const struct module *mod,
const struct module_expose_context *ctx, cairo_t *cr,
int x, int y, int height);
const struct module *mod, const struct exposable *exposbale,
cairo_t *cr, int x, int y, int height);
void module_default_end_expose(
const struct module *mod, struct module_expose_context *ctx);
void module_default_end_expose(const struct module *mod, struct exposable *exposable);