tag: all tag constructors must now be passed a module 'owner'

This commit is contained in:
Daniel Eklöf 2018-12-28 12:40:41 +01:00
parent 8d94202057
commit 904f9ff8e1
9 changed files with 58 additions and 42 deletions

View file

@ -44,8 +44,9 @@ content(struct module *mod)
mtx_lock(&mod->lock);
struct tag_set tags = {
.tags = (struct tag *[]){
tag_new_int_range("brightness", m->current_brightness, 0, m->max_brightness),
tag_new_int("max_brightness", m->max_brightness),
tag_new_int_range(mod, "brightness", m->current_brightness,
0, m->max_brightness),
tag_new_int(mod, "max_brightness", m->max_brightness),
},
.count = 2,
};

View file

@ -79,16 +79,16 @@ content(struct module *mod)
struct tag_set tags = {
.tags = (struct tag *[]){
tag_new_string("name", m->battery),
tag_new_string("manufacturer", m->manufacturer),
tag_new_string("model", m->model),
tag_new_string("state",
tag_new_string(mod, "name", m->battery),
tag_new_string(mod, "manufacturer", m->manufacturer),
tag_new_string(mod, "model", m->model),
tag_new_string(mod, "state",
m->state == STATE_FULL ? "full" :
m->state == STATE_CHARGING ? "charging" :
m->state == STATE_DISCHARGING ? "discharging" :
"unknown"),
tag_new_int_range("capacity", m->capacity, 0, 100),
tag_new_string("estimate", estimate),
tag_new_int_range(mod, "capacity", m->capacity, 0, 100),
tag_new_string(mod, "estimate", estimate),
},
.count = 6,
};

View file

@ -34,8 +34,8 @@ content(struct module *mod)
strftime(date_str, sizeof(date_str), "%e %b", tm);
struct tag_set tags = {
.tags = (struct tag *[]){tag_new_string("time", time_str),
tag_new_string("date", date_str)},
.tags = (struct tag *[]){tag_new_string(mod, "time", time_str),
tag_new_string(mod, "date", date_str)},
.count = 2,
};

View file

@ -559,11 +559,11 @@ content(struct module *mod)
struct tag_set tags = {
.tags = (struct tag *[]){
tag_new_string("name", ws->name),
tag_new_bool("visible", ws->visible),
tag_new_bool("focused", ws->focused),
tag_new_bool("urgent", ws->urgent),
tag_new_string("state", state),
tag_new_string(mod, "name", ws->name),
tag_new_bool(mod, "visible", ws->visible),
tag_new_bool(mod, "focused", ws->focused),
tag_new_bool(mod, "urgent", ws->urgent),
tag_new_string(mod, "state", state),
},
.count = 5,
};

View file

@ -88,13 +88,13 @@ content(struct module *mod)
struct tag_set tags = {
.tags = (struct tag *[]){
tag_new_string("state", state_str),
tag_new_string("album", m->album),
tag_new_string("artist", m->artist),
tag_new_string("title", m->title),
tag_new_string("pos", pos),
tag_new_string("end", end),
tag_new_int("duration", m->duration),
tag_new_string(mod, "state", state_str),
tag_new_string(mod, "album", m->album),
tag_new_string(mod, "artist", m->artist),
tag_new_string(mod, "title", m->title),
tag_new_string(mod, "pos", pos),
tag_new_string(mod, "end", end),
tag_new_int(mod, "duration", m->duration),
tag_new_int_realtime(
"elapsed", m->elapsed, 0, m->duration,
m->state == STATE_PLAY ? TAG_REALTIME_SECONDS : TAG_REALTIME_NONE),

View file

@ -58,8 +58,8 @@ content(struct module *mod)
struct tag_set tags = {
.tags = (struct tag *[]){
tag_new_string("name", m->layouts.layouts[m->current].name),
tag_new_string("symbol", m->layouts.layouts[m->current].symbol)},
tag_new_string(mod, "name", m->layouts.layouts[m->current].name),
tag_new_string(mod, "symbol", m->layouts.layouts[m->current].symbol)},
.count = 2,
};

View file

@ -272,8 +272,9 @@ content(struct module *mod)
mtx_lock(&mod->lock);
struct tag_set tags = {
.tags = (struct tag *[]){
tag_new_string("application", m->application ? m->application : ""),
tag_new_string("title", m->title ? m->title : "")},
tag_new_string(mod, "application", m->application),
tag_new_string(mod, "title", m->title),
},
.count = 2,
};
mtx_unlock(&mod->lock);

26
tag.c
View file

@ -4,6 +4,8 @@
#include <string.h>
#include <stdio.h>
#include "module.h"
struct private {
char *name;
union {
@ -204,20 +206,21 @@ string_as_float(const struct tag *tag)
}
struct tag *
tag_new_int(const char *name, long value)
tag_new_int(struct module *owner, const char *name, long value)
{
return tag_new_int_range(name, value, value, value);
return tag_new_int_range(owner, name, value, value, value);
}
struct tag *
tag_new_int_range(const char *name, long value, long min, long max)
tag_new_int_range(struct module *owner, const char *name, long value,
long min, long max)
{
return tag_new_int_realtime(name, value, min, max, TAG_REALTIME_NONE);
return tag_new_int_realtime(owner, name, value, min, max, TAG_REALTIME_NONE);
}
struct tag *
tag_new_int_realtime(const char *name, long value, long min, long max,
enum tag_realtime_unit unit)
tag_new_int_realtime(struct module *owner, const char *name, long value,
long min, long max, enum tag_realtime_unit unit)
{
struct private *priv = malloc(sizeof(*priv));
priv->name = strdup(name);
@ -228,11 +231,13 @@ tag_new_int_realtime(const char *name, long value, long min, long max,
struct tag *tag = malloc(sizeof(*tag));
tag->private = priv;
tag->owner = owner;
tag->destroy = &destroy_int_and_float;
tag->name = &tag_name;
tag->min = &int_min;
tag->max = &int_max;
tag->realtime = &int_realtime;
tag->refresh_in = &int_refresh_in;
tag->as_string = &int_as_string;
tag->as_int = &int_as_int;
tag->as_bool = &int_as_bool;
@ -241,7 +246,7 @@ tag_new_int_realtime(const char *name, long value, long min, long max,
}
struct tag *
tag_new_bool(const char *name, bool value)
tag_new_bool(struct module *owner, const char *name, bool value)
{
struct private *priv = malloc(sizeof(*priv));
priv->name = strdup(name);
@ -249,6 +254,7 @@ tag_new_bool(const char *name, bool value)
struct tag *tag = malloc(sizeof(*tag));
tag->private = priv;
tag->owner = owner;
tag->destroy = &destroy_int_and_float;
tag->name = &tag_name;
tag->min = &unimpl_min_max;
@ -262,7 +268,7 @@ tag_new_bool(const char *name, bool value)
}
struct tag *
tag_new_float(const char *name, double value)
tag_new_float(struct module *owner, const char *name, double value)
{
struct private *priv = malloc(sizeof(*priv));
priv->name = strdup(name);
@ -270,6 +276,7 @@ tag_new_float(const char *name, double value)
struct tag *tag = malloc(sizeof(*tag));
tag->private = priv;
tag->owner = owner;
tag->destroy = &destroy_int_and_float;
tag->name = &tag_name;
tag->min = &unimpl_min_max;
@ -283,7 +290,7 @@ tag_new_float(const char *name, double value)
}
struct tag *
tag_new_string(const char *name, const char *value)
tag_new_string(struct module *owner, const char *name, const char *value)
{
struct private *priv = malloc(sizeof(*priv));
priv->name = strdup(name);
@ -291,6 +298,7 @@ tag_new_string(const char *name, const char *value)
struct tag *tag = malloc(sizeof(*tag));
tag->private = priv;
tag->owner = owner;
tag->destroy = &destroy_string;
tag->name = &tag_name;
tag->min = &unimpl_min_max;

20
tag.h
View file

@ -9,8 +9,11 @@ enum tag_realtime_unit {
TAG_REALTIME_SECONDS
};
struct module;
struct tag {
void *private;
struct module *owner;
void (*destroy)(struct tag *tag);
const char *(*name)(const struct tag *tag);
@ -29,13 +32,16 @@ struct tag_set {
size_t count;
};
struct tag *tag_new_int(const char *name, long value);
struct tag *tag_new_int_range(const char *name, long value, long min, long max);
struct tag *tag_new_int_realtime(const char *name, long value, long min,
long max, enum tag_realtime_unit unit);
struct tag *tag_new_bool(const char *name, bool value);
struct tag *tag_new_float(const char *name, double value);
struct tag *tag_new_string(const char *name, const char *value);
struct tag *tag_new_int(struct module *owner, const char *name, long value);
struct tag *tag_new_int_range(
struct module *owner, const char *name, long value, long min, long max);
struct tag *tag_new_int_realtime(
struct module *owner, const char *name, long value, long min,
long max, enum tag_realtime_unit unit);
struct tag *tag_new_bool(struct module *owner, const char *name, bool value);
struct tag *tag_new_float(struct module *owner, const char *name, double value);
struct tag *tag_new_string(
struct module *owner, const char *name, const char *value);
const struct tag *tag_for_name(const struct tag_set *set, const char *name);
void tag_set_destroy(struct tag_set *set);