Merge branch 'i3-empty-tag'

Closes #139
This commit is contained in:
Daniel Eklöf 2021-12-20 17:25:00 +01:00
commit 375c7ca89c
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
3 changed files with 42 additions and 5 deletions

View file

@ -10,9 +10,12 @@
## Unreleased ## Unreleased
### Added ### Added
* ramp: can now have custom min and max values * ramp: can now have custom min and max values
(https://codeberg.org/dnkl/yambar/issues/103). (https://codeberg.org/dnkl/yambar/issues/103).
* border: new decoration. * border: new decoration.
* i3/sway: new boolean tag: `empty`
(https://codeberg.org/dnkl/yambar/issues/139).
### Changed ### Changed

View file

@ -35,6 +35,9 @@ with the _application_ and _title_ tags to replace the X11-only
| urgent | urgent
: bool : bool
: True if the workspace has the urgent flag set : True if the workspace has the urgent flag set
| empty
: bool
: True if the workspace is empty
| state | state
: string : string
: One of *urgent*, *focused*, *unfocused* or *invisible* (note: : One of *urgent*, *focused*, *unfocused* or *invisible* (note:

View file

@ -37,6 +37,7 @@ struct workspace {
bool visible; bool visible;
bool focused; bool focused;
bool urgent; bool urgent;
bool empty;
struct { struct {
unsigned id; unsigned id;
@ -85,14 +86,17 @@ static bool
workspace_from_json(const struct json_object *json, struct workspace *ws) workspace_from_json(const struct json_object *json, struct workspace *ws)
{ {
/* Always present */ /* Always present */
struct json_object *name, *output; struct json_object *name, *output, *focus;
if (!json_object_object_get_ex(json, "name", &name) || if (!json_object_object_get_ex(json, "name", &name) ||
!json_object_object_get_ex(json, "output", &output)) !json_object_object_get_ex(json, "output", &output) ||
!json_object_object_get_ex(json, "focus", &focus))
{ {
LOG_ERR("workspace reply/event without 'name' and/or 'output' property"); LOG_ERR("workspace reply/event without 'name' and/or 'output', "
"and/or 'focus' properties");
return false; return false;
} }
/* Optional */ /* Optional */
struct json_object *visible = NULL, *focused = NULL, *urgent = NULL; struct json_object *visible = NULL, *focused = NULL, *urgent = NULL;
json_object_object_get_ex(json, "visible", &visible); json_object_object_get_ex(json, "visible", &visible);
@ -101,6 +105,9 @@ workspace_from_json(const struct json_object *json, struct workspace *ws)
const char *name_as_string = json_object_get_string(name); const char *name_as_string = json_object_get_string(name);
const size_t node_count = json_object_array_length(focus);
const bool is_empty = node_count == 0;
*ws = (struct workspace) { *ws = (struct workspace) {
.name = strdup(name_as_string), .name = strdup(name_as_string),
.name_as_int = workspace_name_as_int(name_as_string), .name_as_int = workspace_name_as_int(name_as_string),
@ -109,6 +116,7 @@ workspace_from_json(const struct json_object *json, struct workspace *ws)
.visible = json_object_get_boolean(visible), .visible = json_object_get_boolean(visible),
.focused = json_object_get_boolean(focused), .focused = json_object_get_boolean(focused),
.urgent = json_object_get_boolean(urgent), .urgent = json_object_get_boolean(urgent),
.empty = is_empty,
.window = {.title = NULL, .pid = -1}, .window = {.title = NULL, .pid = -1},
}; };
@ -353,6 +361,7 @@ handle_workspace_event(int type, const struct json_object *json, void *_mod)
else { else {
workspace_free(ws); workspace_free(ws);
ws->name = strdup(current_name); ws->name = strdup(current_name);
ws->empty = true;
assert(ws->persistent); assert(ws->persistent);
} }
} }
@ -425,11 +434,12 @@ handle_window_event(int type, const struct json_object *json, void *_mod)
} }
const char *change_str = json_object_get_string(change); const char *change_str = json_object_get_string(change);
bool is_new = strcmp(change_str, "new") == 0;
bool is_focus = strcmp(change_str, "focus") == 0; bool is_focus = strcmp(change_str, "focus") == 0;
bool is_close = strcmp(change_str, "close") == 0; bool is_close = strcmp(change_str, "close") == 0;
bool is_title = strcmp(change_str, "title") == 0; bool is_title = strcmp(change_str, "title") == 0;
if (!is_focus && !is_close && !is_title) if (!is_new && !is_focus && !is_close && !is_title)
return true; return true;
mtx_lock(&mod->lock); mtx_lock(&mod->lock);
@ -454,12 +464,19 @@ handle_window_event(int type, const struct json_object *json, void *_mod)
ws->window.title = ws->window.application = NULL; ws->window.title = ws->window.application = NULL;
ws->window.pid = -1; ws->window.pid = -1;
/* May not be true, but e.g. a subsequent “focus” event will
* reset it... */
ws->empty = true;
m->dirty = true; m->dirty = true;
mtx_unlock(&mod->lock); mtx_unlock(&mod->lock);
return true; return true;
} }
/* Non-close event - thus workspace cannot be empty */
ws->empty = false;
struct json_object *container, *id, *name; struct json_object *container, *id, *name;
if (!json_object_object_get_ex(json, "container", &container) || if (!json_object_object_get_ex(json, "container", &container) ||
!json_object_object_get_ex(container, "id", &id) || !json_object_object_get_ex(container, "id", &id) ||
@ -602,6 +619,7 @@ run(struct module *mod)
.name = strdup(name_as_string), .name = strdup(name_as_string),
.name_as_int = workspace_name_as_int(name_as_string), .name_as_int = workspace_name_as_int(name_as_string),
.persistent = true, .persistent = true,
.empty = true,
}; };
workspace_add(m, ws); workspace_add(m, ws);
} }
@ -695,12 +713,25 @@ content(struct module *mod)
ws->visible ? ws->focused ? "focused" : "unfocused" : ws->visible ? ws->focused ? "focused" : "unfocused" :
"invisible"; "invisible";
LOG_DBG("%s: visible=%s, focused=%s, urgent=%s, empty=%s, state=%s, "
"application=%s, title=%s, mode=%s",
ws->name,
ws->visible ? "yes" : "no",
ws->focused ? "yes" : "no",
ws->urgent ? "yes" : "no",
ws->empty ? "yes" : "no",
state,
ws->window.application,
ws->window.title,
m->mode);
struct tag_set tags = { struct tag_set tags = {
.tags = (struct tag *[]){ .tags = (struct tag *[]){
tag_new_string(mod, "name", ws->name), tag_new_string(mod, "name", ws->name),
tag_new_bool(mod, "visible", ws->visible), tag_new_bool(mod, "visible", ws->visible),
tag_new_bool(mod, "focused", ws->focused), tag_new_bool(mod, "focused", ws->focused),
tag_new_bool(mod, "urgent", ws->urgent), tag_new_bool(mod, "urgent", ws->urgent),
tag_new_bool(mod, "empty", ws->empty),
tag_new_string(mod, "state", state), tag_new_string(mod, "state", state),
tag_new_string(mod, "application", ws->window.application), tag_new_string(mod, "application", ws->window.application),
@ -708,7 +739,7 @@ content(struct module *mod)
tag_new_string(mod, "mode", m->mode), tag_new_string(mod, "mode", m->mode),
}, },
.count = 8, .count = 9,
}; };
if (ws->focused) { if (ws->focused) {