1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 client_set.c for the Openbox window manager
4 Copyright (c) 2011 Dana Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
19 #include "client_set.h"
29 static void client_destroyed(ObClient *client, ObClientSet *set)
31 g_hash_table_remove(set->h, &client->window);
34 ObClientSet* client_set_empty(void)
38 set = g_slice_new(ObClientSet);
39 set->h = g_hash_table_new(g_int_hash, g_int_equal);
40 client_add_destroy_notify((ObClientCallback)client_destroyed, set);
44 ObClientSet* client_set_single(void)
49 c = event_current_target();
51 set = client_set_empty();
52 g_hash_table_insert(set->h, &c->window, c);
56 /*! Returns a new set of clients with all possible client in it.*/
57 ObClientSet* client_set_all(void)
62 if (!client_list) return NULL;
63 set = client_set_empty();
64 for (it = client_list; it; it = g_list_next(it)) {
65 ObClient *c = it->data;
66 g_hash_table_insert(set->h, &c->window, c);
71 void client_set_destroy(ObClientSet *set)
73 client_remove_destroy_notify_data((ObClientCallback)client_destroyed, set);
74 g_hash_table_destroy(set->h);
75 g_slice_free(ObClientSet, set);
78 static void foreach_union(gpointer k, gpointer v, gpointer u)
81 g_hash_table_insert(set, k, v); /* add everything in the other set */
84 /* Returns a new set which contains all clients in either @a or @b. The sets
85 @a and @b are considered freed once passed to this function.
87 ObClientSet* client_set_union(ObClientSet *a, ObClientSet *b)
89 g_return_val_if_fail(a != NULL, NULL);
90 g_return_val_if_fail(b != NULL, NULL);
92 g_hash_table_foreach(b->h, foreach_union, a->h);
93 client_set_destroy(b);
97 static gboolean foreach_intersection(gpointer k, gpointer v, gpointer u)
100 return !g_hash_table_lookup(set, k); /* remove if not in the other set */
103 /* Returns a new set which contains all clients in both @a and @b. The sets
104 @a and @b are considered freed once passed to this function.
106 ObClientSet* client_set_intersection(ObClientSet *a, ObClientSet *b)
108 g_return_val_if_fail(a != NULL, NULL);
109 g_return_val_if_fail(b != NULL, NULL);
111 g_hash_table_foreach_remove(a->h, foreach_intersection, b->h);
112 client_set_destroy(b);
116 static gboolean foreach_reduce(gpointer k, gpointer v, gpointer u)
119 ObClientSetReduceFunc f = u;
123 ObClientSet* client_set_reduce(ObClientSet *set, ObClientSetReduceFunc f)
125 g_return_val_if_fail(set != NULL, NULL);
126 g_return_val_if_fail(f != NULL, NULL);
128 g_hash_table_foreach_remove(set->h, foreach_reduce, f);
132 ObClientSet* client_set_expand(ObClientSet *set, ObClientSetExpandFunc f)
136 g_return_val_if_fail(set != NULL, NULL);
137 g_return_val_if_fail(f != NULL, NULL);
139 for (it = client_list; it; it = g_list_next(it)) {
140 ObClient *c = it->data;
141 if (!g_hash_table_lookup(set->h, &c->window) && f(c))
142 g_hash_table_insert(set->h, &c->window, c);