use Area to store window geoms
[awesome.git] / tag.c
blob1d8e7c6d4f9e7850f7a544c4b6d97657196ce27b
1 /*
2 * tag.c - tag management
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
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 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <stdio.h>
23 #include <X11/Xutil.h>
25 #include "screen.h"
26 #include "tag.h"
27 #include "util.h"
28 #include "rules.h"
29 #include "client.h"
30 #include "ewmh.h"
32 extern AwesomeConf globalconf;
34 static void
35 detach_tagclientlink(TagClientLink *tc)
37 TagClientLink *tmp;
39 if(globalconf.tclink == tc)
40 globalconf.tclink = tc->next;
41 else
43 for(tmp = globalconf.tclink; tmp && tmp->next != tc; tmp = tmp->next);
44 tmp->next = tc->next;
47 p_delete(&tc);
50 void
51 tag_client(Client *c, Tag *t)
53 TagClientLink *tc, *new_tc;
55 /* don't tag twice */
56 if(is_client_tagged(c, t))
57 return;
59 new_tc = p_new(TagClientLink, 1);
61 if(!globalconf.tclink)
62 globalconf.tclink = new_tc;
63 else
65 for(tc = globalconf.tclink; tc->next; tc = tc->next);
66 tc->next = new_tc;
69 new_tc->client = c;
70 new_tc->tag = t;
73 void
74 untag_client(Client *c, Tag *t)
76 TagClientLink *tc;
78 for(tc = globalconf.tclink; tc; tc = tc->next)
79 if(tc->client == c && tc->tag == t)
81 detach_tagclientlink(tc);
82 break;
86 Bool
87 is_client_tagged(Client *c, Tag *t)
89 TagClientLink *tc;
91 if(!c)
92 return False;
94 for(tc = globalconf.tclink; tc; tc = tc->next)
95 if(tc->client == c && tc->tag == t)
96 return True;
98 return False;
101 void
102 tag_client_with_current_selected(Client *c)
104 Tag *tag;
105 VirtScreen vscreen = globalconf.screens[c->screen];
107 for(tag = vscreen.tags; tag; tag = tag->next)
108 if(tag->selected)
109 tag_client(c, tag);
110 else
111 untag_client(c, tag);
114 void
115 tag_client_with_rules(Client *c)
117 Rule *r;
118 Tag *tag;
119 Bool matched = False;
121 for(r = globalconf.rules; r; r = r->next)
122 if(client_match_rule(c, r))
124 c->isfloating = r->isfloating;
126 if(r->screen != RULE_NOSCREEN && r->screen != c->screen)
127 move_client_to_screen(c, r->screen, True);
129 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
130 if(is_tag_match_rules(tag, r))
132 matched = True;
133 tag_client(c, tag);
135 else
136 untag_client(c, tag);
138 if(!matched)
139 tag_client_with_current_selected(c);
140 break;
145 Tag **
146 get_current_tags(int screen)
148 Tag *tag, **tags = NULL;
149 int n = 1;
151 tags = p_new(Tag *, n);
152 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
153 if(tag->selected)
155 p_realloc(&tags, ++n);
156 tags[n - 2] = tag;
159 /* finish with null */
160 tags[n - 1] = NULL;
162 return tags;
165 /** Tag selected window with tag
166 * \param screen Screen ID
167 * \param arg Tag name
168 * \ingroup ui_callback
170 void
171 uicb_client_tag(int screen, char *arg)
173 int tag_id = -1;
174 Tag *tag, *target_tag;
175 Client *sel = globalconf.focus->client;
177 if(!sel)
178 return;
180 if(arg)
182 tag_id = atoi(arg) - 1;
183 if(tag_id != -1)
185 for(target_tag = globalconf.screens[screen].tags; target_tag && tag_id > 0;
186 target_tag = target_tag->next, tag_id--);
187 if(target_tag)
189 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
190 untag_client(sel, tag);
191 tag_client(sel, target_tag);
195 else
196 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
197 tag_client(sel, tag);
199 client_saveprops(sel);
200 arrange(screen);
203 /** Toggle floating state of a client
204 * \param screen Screen ID
205 * \param arg unused
206 * \ingroup ui_callback
208 void
209 uicb_client_togglefloating(int screen, char *arg __attribute__ ((unused)))
211 Client *sel = globalconf.focus->client;
213 if(!sel)
214 return;
216 sel->isfloating = !sel->isfloating;
218 if (arg == NULL)
219 client_resize(sel, sel->f_geometry.x, sel->f_geometry.y,
220 sel->f_geometry.width, sel->f_geometry.height, True, False);
221 else
222 client_resize(sel, sel->geometry.x, sel->geometry.y,
223 sel->geometry.width, sel->geometry.height, True, True);
225 client_saveprops(sel);
226 arrange(screen);
229 /** Toggle a tag on client
230 * \param screen Screen ID
231 * \param arg Tag name
232 * \ingroup ui_callback
234 void
235 uicb_client_toggletag(int screen, char *arg)
237 Client *sel = globalconf.focus->client;
238 int i;
239 Tag *tag, *target_tag;
241 if(!sel)
242 return;
244 if(arg)
246 i = atoi(arg) - 1;
247 for(target_tag = globalconf.screens[screen].tags; target_tag && i > 0;
248 target_tag = target_tag->next, i--);
249 if(target_tag)
251 if(is_client_tagged(sel, target_tag))
252 untag_client(sel, target_tag);
253 else
254 tag_client(sel, target_tag);
257 /* check that there's at least one tag selected for this client*/
258 for(tag = globalconf.screens[screen].tags; tag
259 && !is_client_tagged(sel, tag); tag = tag->next)
261 if(!tag)
262 tag_client(sel, target_tag);
264 else
265 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
266 if(is_client_tagged(sel, tag))
267 tag_client(sel, tag);
268 else
269 untag_client(sel, tag);
271 client_saveprops(sel);
272 arrange(screen);
275 /** Add a tag to viewed tags
276 * \param screen Screen ID
277 * \param arg Tag name
278 * \ingroup ui_callback
280 void
281 uicb_tag_toggleview(int screen, char *arg)
283 int i;
284 Tag *tag, *target_tag;
286 if(arg)
288 i = atoi(arg) - 1;
289 for(target_tag = globalconf.screens[screen].tags; target_tag && i > 0;
290 target_tag = target_tag->next, i--);
292 if(target_tag)
293 target_tag->selected = !target_tag->selected;
295 /* check that there's at least one tag selected */
296 for(tag = globalconf.screens[screen].tags; tag && !tag->selected; tag = tag->next);
297 if(!tag)
298 target_tag->selected = True;
300 else
301 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
302 tag->selected = !tag->selected;
304 saveawesomeprops(screen);
305 arrange(screen);
306 ewmh_update_net_current_desktop(get_phys_screen(screen));
309 void
310 tag_view(int screen, int dindex)
312 Tag *target_tag, *tag;
314 if(dindex < 0)
315 return;
317 for(target_tag = globalconf.screens[screen].tags; target_tag && dindex > 0;
318 target_tag = target_tag->next, dindex--);
319 if(target_tag)
321 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
322 tag->selected = False;
323 target_tag->selected = True;
325 saveawesomeprops(screen);
326 arrange(screen);
327 ewmh_update_net_current_desktop(get_phys_screen(screen));
330 /** View tag
331 * \param screen Screen ID
332 * \param arg tag to view
333 * \ingroup ui_callback
335 void
336 uicb_tag_view(int screen, char *arg)
338 Tag *tag;
340 if(arg)
341 tag_view(screen, atoi(arg) - 1);
342 else
344 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
345 tag->selected = True;
346 saveawesomeprops(screen);
347 arrange(screen);
348 ewmh_update_net_current_desktop(get_phys_screen(screen));
352 /** View previously selected tags
353 * \param screen Screen ID
354 * \param arg unused
355 * \ingroup ui_callback
357 void
358 uicb_tag_prev_selected(int screen, char *arg __attribute__ ((unused)))
360 Tag *tag;
361 Bool t;
363 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
365 t = tag->selected;
366 tag->selected = tag->was_selected;
367 tag->was_selected = t;
369 arrange(screen);
370 ewmh_update_net_current_desktop(get_phys_screen(screen));
373 /** View next tag
374 * \param screen Screen ID
375 * \param arg unused
376 * \ingroup ui_callback
378 void
379 uicb_tag_viewnext(int screen, char *arg __attribute__ ((unused)))
381 Tag **curtags = get_current_tags(screen);
383 if(!curtags[0]->next)
384 return;
386 curtags[0]->selected = False;
387 curtags[0]->next->selected = True;
389 p_delete(&curtags);
391 saveawesomeprops(screen);
392 arrange(screen);
393 ewmh_update_net_current_desktop(get_phys_screen(screen));
396 /** View previous tag
397 * \param screen Screen ID
398 * \param arg unused
399 * \ingroup ui_callback
401 void
402 uicb_tag_viewprev(int screen, char *arg __attribute__ ((unused)))
404 Tag *tag, **curtags = get_current_tags(screen);
406 for(tag = globalconf.screens[screen].tags; tag && tag->next != curtags[0]; tag = tag->next);
407 if(tag)
409 tag->selected = True;
410 curtags[0]->selected = False;
411 saveawesomeprops(screen);
412 arrange(screen);
414 p_delete(&curtags);
415 ewmh_update_net_current_desktop(get_phys_screen(screen));
418 void
419 uicb_tag_create(int screen, char *arg)
421 Tag *last_tag, *tag;
423 if(!a_strlen(arg))
424 return;
426 for(last_tag = globalconf.screens[screen].tags; last_tag && last_tag->next; last_tag = last_tag->next);
427 last_tag->next = tag = p_new(Tag, 1);
428 tag->name = a_strdup(arg);
429 tag->layout = globalconf.screens[screen].layouts;
430 tag->mwfact = 0.5;
431 tag->nmaster = 1;
432 tag->ncol = 1;
435 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80