]> git.mdlowis.com Git - proto/labwc.git/commitdiff
view: introduce view_matches_query()
authorConsus <consus@ftml.net>
Mon, 28 Aug 2023 15:55:29 +0000 (18:55 +0300)
committerJohan Malm <johanmalm@users.noreply.github.com>
Sun, 1 Oct 2023 13:17:29 +0000 (14:17 +0100)
Add new function view_matches_query() that will be required by If and
ForEach actions in the future.

include/view.h
src/view.c

index 4a408a84fcb737ec92ccf1a68f72bd2ad1a7750c..7046c1153187d2f921cef99ac21a7cb9e64529d9 100644 (file)
@@ -172,6 +172,12 @@ struct view {
        struct wl_listener set_title;
 };
 
+struct view_query {
+       struct wl_list link;
+       char *identifier;
+       char *title;
+};
+
 struct xdg_toplevel_view {
        struct view base;
        struct wlr_xdg_surface *xdg_surface;
@@ -194,6 +200,22 @@ enum lab_view_criteria {
  */
 struct view *view_from_wlr_surface(struct wlr_surface *surface);
 
+/**
+ * view_query_free() - Free a given view query
+ * @query: Query to be freed.
+ */
+void view_query_free(struct view_query *view);
+
+/**
+ * view_matches_query() - Check if view matches the given criteria
+ * @view: View to checked.
+ * @query: Criteria to match against.
+ *
+ * Returns true if %view matches all of the criteria given in %query, false
+ * otherwise.
+ */
+bool view_matches_query(struct view *view, struct view_query *query);
+
 /**
  * for_each_view() - iterate over all views which match criteria
  * @view: Iterator.
index 743c9290dd9128e6ff1dc58e1841142648ff5d68..6185d9177009f4963a21681b37434a980d52de53 100644 (file)
@@ -2,6 +2,7 @@
 #include <assert.h>
 #include <stdio.h>
 #include <strings.h>
+#include "common/match.h"
 #include "common/mem.h"
 #include "common/scene-helpers.h"
 #include "labwc.h"
@@ -51,6 +52,36 @@ view_from_wlr_surface(struct wlr_surface *surface)
        return NULL;
 }
 
+void
+view_query_free(struct view_query *query)
+{
+       wl_list_remove(&query->link);
+       free(query->identifier);
+       free(query->title);
+       free(query);
+}
+
+bool
+view_matches_query(struct view *view, struct view_query *query)
+{
+       bool match = true;
+       bool empty = true;
+
+       const char *identifier = view_get_string_prop(view, "app_id");
+       if (match && query->identifier) {
+               empty = false;
+               match &= match_glob(query->identifier, identifier);
+       }
+
+       const char *title = view_get_string_prop(view, "title");
+       if (match && query->title) {
+               empty = false;
+               match &= match_glob(query->title, title);
+       }
+
+       return !empty && match;
+}
+
 static bool
 matches_criteria(struct view *view, enum lab_view_criteria criteria)
 {