restack on max
[awesome.git] / tag.c
blobba885bf86801bcae58d6e7a66fa8a8574e708883
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 switch(r->isfloating)
126 case Tile:
127 c->isfloating = False;
128 break;
129 case Float:
130 c->isfloating = True;
131 break;
132 default:
133 break;
136 if(r->screen != RULE_NOSCREEN && r->screen != c->screen)
137 move_client_to_screen(c, r->screen, True);
139 for(tag = globalconf.screens[c->screen].tags; tag; tag = tag->next)
140 if(is_tag_match_rules(tag, r))
142 matched = True;
143 tag_client(c, tag);
145 else
146 untag_client(c, tag);
148 if(!matched)
149 tag_client_with_current_selected(c);
150 break;
155 Tag **
156 get_current_tags(int screen)
158 Tag *tag, **tags = NULL;
159 int n = 1;
161 tags = p_new(Tag *, n);
162 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
163 if(tag->selected)
165 p_realloc(&tags, ++n);
166 tags[n - 2] = tag;
169 /* finish with null */
170 tags[n - 1] = NULL;
172 return tags;
175 /** Tag selected window with tag
176 * \param screen Screen ID
177 * \param arg Tag name
178 * \ingroup ui_callback
180 void
181 uicb_client_tag(int screen, char *arg)
183 int tag_id = -1;
184 Tag *tag, *target_tag;
185 Client *sel = globalconf.focus->client;
187 if(!sel)
188 return;
190 if(arg)
192 tag_id = atoi(arg) - 1;
193 if(tag_id != -1)
195 for(target_tag = globalconf.screens[screen].tags; target_tag && tag_id > 0;
196 target_tag = target_tag->next, tag_id--);
197 if(target_tag)
199 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
200 untag_client(sel, tag);
201 tag_client(sel, target_tag);
205 else
206 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
207 tag_client(sel, tag);
209 client_saveprops(sel);
210 arrange(screen);
213 /** Toggle floating state of a client
214 * \param screen Screen ID
215 * \param arg unused
216 * \ingroup ui_callback
218 void
219 uicb_client_togglefloating(int screen, char *arg __attribute__((unused)))
221 Client *sel = globalconf.focus->client;
223 if(!sel)
224 return;
226 if((sel->isfloating = !sel->isfloating))
227 client_resize(sel, sel->f_geometry, False);
228 else if(sel->ismax)
229 client_resize(sel, sel->m_geometry, False);
231 client_saveprops(sel);
232 arrange(screen);
235 /** Toggle a tag on client
236 * \param screen Screen ID
237 * \param arg Tag name
238 * \ingroup ui_callback
240 void
241 uicb_client_toggletag(int screen, char *arg)
243 Client *sel = globalconf.focus->client;
244 int i;
245 Tag *tag, *target_tag;
247 if(!sel)
248 return;
250 if(arg)
252 i = atoi(arg) - 1;
253 for(target_tag = globalconf.screens[screen].tags; target_tag && i > 0;
254 target_tag = target_tag->next, i--);
255 if(target_tag)
257 if(is_client_tagged(sel, target_tag))
258 untag_client(sel, target_tag);
259 else
260 tag_client(sel, target_tag);
263 /* check that there's at least one tag selected for this client*/
264 for(tag = globalconf.screens[screen].tags; tag
265 && !is_client_tagged(sel, tag); tag = tag->next)
267 if(!tag)
268 tag_client(sel, target_tag);
270 else
271 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
272 if(is_client_tagged(sel, tag))
273 tag_client(sel, tag);
274 else
275 untag_client(sel, tag);
277 client_saveprops(sel);
278 arrange(screen);
281 /** Add a tag to viewed tags
282 * \param screen Screen ID
283 * \param arg Tag name
284 * \ingroup ui_callback
286 void
287 uicb_tag_toggleview(int screen, char *arg)
289 int i;
290 Tag *tag, *target_tag;
292 if(arg)
294 i = atoi(arg) - 1;
295 for(target_tag = globalconf.screens[screen].tags; target_tag && i > 0;
296 target_tag = target_tag->next, i--);
298 if(target_tag)
299 target_tag->selected = !target_tag->selected;
301 /* check that there's at least one tag selected */
302 for(tag = globalconf.screens[screen].tags; tag && !tag->selected; tag = tag->next);
303 if(!tag)
304 target_tag->selected = True;
306 else
307 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
308 tag->selected = !tag->selected;
310 saveawesomeprops(screen);
311 arrange(screen);
312 ewmh_update_net_current_desktop(get_phys_screen(screen));
315 void
316 tag_view(int screen, int dindex)
318 Tag *target_tag, *tag;
320 if(dindex < 0)
321 return;
323 for(target_tag = globalconf.screens[screen].tags; target_tag && dindex > 0;
324 target_tag = target_tag->next, dindex--);
325 if(target_tag)
327 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
328 tag->selected = False;
329 target_tag->selected = True;
331 saveawesomeprops(screen);
332 arrange(screen);
333 ewmh_update_net_current_desktop(get_phys_screen(screen));
336 /** View tag
337 * \param screen Screen ID
338 * \param arg tag to view
339 * \ingroup ui_callback
341 void
342 uicb_tag_view(int screen, char *arg)
344 Tag *tag;
346 if(arg)
347 tag_view(screen, atoi(arg) - 1);
348 else
350 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
351 tag->selected = True;
352 saveawesomeprops(screen);
353 arrange(screen);
354 ewmh_update_net_current_desktop(get_phys_screen(screen));
358 /** View previously selected tags
359 * \param screen Screen ID
360 * \param arg unused
361 * \ingroup ui_callback
363 void
364 uicb_tag_prev_selected(int screen, char *arg __attribute__ ((unused)))
366 Tag *tag;
367 Bool t;
369 for(tag = globalconf.screens[screen].tags; tag; tag = tag->next)
371 t = tag->selected;
372 tag->selected = tag->was_selected;
373 tag->was_selected = t;
375 arrange(screen);
376 ewmh_update_net_current_desktop(get_phys_screen(screen));
379 /** View next tag
380 * \param screen Screen ID
381 * \param arg unused
382 * \ingroup ui_callback
384 void
385 uicb_tag_viewnext(int screen, char *arg __attribute__ ((unused)))
387 Tag **curtags = get_current_tags(screen);
389 if(!curtags[0]->next)
390 return;
392 curtags[0]->selected = False;
393 curtags[0]->next->selected = True;
395 p_delete(&curtags);
397 saveawesomeprops(screen);
398 arrange(screen);
399 ewmh_update_net_current_desktop(get_phys_screen(screen));
402 /** View previous tag
403 * \param screen Screen ID
404 * \param arg unused
405 * \ingroup ui_callback
407 void
408 uicb_tag_viewprev(int screen, char *arg __attribute__ ((unused)))
410 Tag *tag, **curtags = get_current_tags(screen);
412 for(tag = globalconf.screens[screen].tags; tag && tag->next != curtags[0]; tag = tag->next);
413 if(tag)
415 tag->selected = True;
416 curtags[0]->selected = False;
417 saveawesomeprops(screen);
418 arrange(screen);
420 p_delete(&curtags);
421 ewmh_update_net_current_desktop(get_phys_screen(screen));
424 void
425 uicb_tag_create(int screen, char *arg)
427 Tag *last_tag, *tag;
429 if(!a_strlen(arg))
430 return;
432 for(last_tag = globalconf.screens[screen].tags; last_tag && last_tag->next; last_tag = last_tag->next);
433 last_tag->next = tag = p_new(Tag, 1);
434 tag->name = a_strdup(arg);
435 tag->layout = globalconf.screens[screen].layouts;
436 tag->mwfact = 0.5;
437 tag->nmaster = 1;
438 tag->ncol = 1;
441 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80