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.
23 #include <X11/Xutil.h>
32 extern AwesomeConf globalconf
;
35 detach_tagclientlink(TagClientLink
*tc
)
39 if(globalconf
.tclink
== tc
)
40 globalconf
.tclink
= tc
->next
;
43 for(tmp
= globalconf
.tclink
; tmp
&& tmp
->next
!= tc
; tmp
= tmp
->next
);
51 tag_client(Client
*c
, Tag
*t
)
53 TagClientLink
*tc
, *new_tc
;
56 if(is_client_tagged(c
, t
))
59 new_tc
= p_new(TagClientLink
, 1);
61 if(!globalconf
.tclink
)
62 globalconf
.tclink
= new_tc
;
65 for(tc
= globalconf
.tclink
; tc
->next
; tc
= tc
->next
);
74 untag_client(Client
*c
, Tag
*t
)
78 for(tc
= globalconf
.tclink
; tc
; tc
= tc
->next
)
79 if(tc
->client
== c
&& tc
->tag
== t
)
81 detach_tagclientlink(tc
);
87 is_client_tagged(Client
*c
, Tag
*t
)
94 for(tc
= globalconf
.tclink
; tc
; tc
= tc
->next
)
95 if(tc
->client
== c
&& tc
->tag
== t
)
102 tag_client_with_current_selected(Client
*c
)
105 VirtScreen vscreen
= globalconf
.screens
[c
->screen
];
107 for(tag
= vscreen
.tags
; tag
; tag
= tag
->next
)
111 untag_client(c
, tag
);
115 tag_client_with_rules(Client
*c
)
119 Bool matched
= False
;
121 for(r
= globalconf
.rules
; r
; r
= r
->next
)
122 if(client_match_rule(c
, r
))
124 switch(r
->isfloating
)
127 c
->isfloating
= False
;
130 c
->isfloating
= True
;
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
))
146 untag_client(c
, tag
);
149 tag_client_with_current_selected(c
);
156 get_current_tags(int screen
)
158 Tag
*tag
, **tags
= NULL
;
161 tags
= p_new(Tag
*, n
);
162 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
165 p_realloc(&tags
, ++n
);
169 /* finish with null */
175 /** Tag selected window with tag
176 * \param screen Screen ID
177 * \param arg Tag name
178 * \ingroup ui_callback
181 uicb_client_tag(int screen
, char *arg
)
184 Tag
*tag
, *target_tag
;
185 Client
*sel
= globalconf
.focus
->client
;
192 tag_id
= atoi(arg
) - 1;
195 for(target_tag
= globalconf
.screens
[screen
].tags
; target_tag
&& tag_id
> 0;
196 target_tag
= target_tag
->next
, tag_id
--);
199 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
200 untag_client(sel
, tag
);
201 tag_client(sel
, target_tag
);
206 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
207 tag_client(sel
, tag
);
209 client_saveprops(sel
);
213 /** Toggle floating state of a client
214 * \param screen Screen ID
216 * \ingroup ui_callback
219 uicb_client_togglefloating(int screen
, char *arg
__attribute__((unused
)))
221 Client
*sel
= globalconf
.focus
->client
;
226 if((sel
->isfloating
= !sel
->isfloating
))
227 client_resize(sel
, sel
->f_geometry
, True
);
229 client_saveprops(sel
);
233 /** Toggle a tag on client
234 * \param screen Screen ID
235 * \param arg Tag name
236 * \ingroup ui_callback
239 uicb_client_toggletag(int screen
, char *arg
)
241 Client
*sel
= globalconf
.focus
->client
;
243 Tag
*tag
, *target_tag
;
251 for(target_tag
= globalconf
.screens
[screen
].tags
; target_tag
&& i
> 0;
252 target_tag
= target_tag
->next
, i
--);
255 if(is_client_tagged(sel
, target_tag
))
256 untag_client(sel
, target_tag
);
258 tag_client(sel
, target_tag
);
261 /* check that there's at least one tag selected for this client*/
262 for(tag
= globalconf
.screens
[screen
].tags
; tag
263 && !is_client_tagged(sel
, tag
); tag
= tag
->next
)
266 tag_client(sel
, target_tag
);
269 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
270 if(is_client_tagged(sel
, tag
))
271 tag_client(sel
, tag
);
273 untag_client(sel
, tag
);
275 client_saveprops(sel
);
279 /** Add a tag to viewed tags
280 * \param screen Screen ID
281 * \param arg Tag name
282 * \ingroup ui_callback
285 uicb_tag_toggleview(int screen
, char *arg
)
288 Tag
*tag
, *target_tag
;
293 for(target_tag
= globalconf
.screens
[screen
].tags
; target_tag
&& i
> 0;
294 target_tag
= target_tag
->next
, i
--);
297 target_tag
->selected
= !target_tag
->selected
;
299 /* check that there's at least one tag selected */
300 for(tag
= globalconf
.screens
[screen
].tags
; tag
&& !tag
->selected
; tag
= tag
->next
);
302 target_tag
->selected
= True
;
305 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
306 tag
->selected
= !tag
->selected
;
308 saveawesomeprops(screen
);
310 ewmh_update_net_current_desktop(get_phys_screen(screen
));
314 tag_view(int screen
, int dindex
)
316 Tag
*target_tag
, *tag
;
321 for(target_tag
= globalconf
.screens
[screen
].tags
; target_tag
&& dindex
> 0;
322 target_tag
= target_tag
->next
, dindex
--);
325 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
326 tag
->selected
= False
;
327 target_tag
->selected
= True
;
329 saveawesomeprops(screen
);
331 ewmh_update_net_current_desktop(get_phys_screen(screen
));
335 * \param screen Screen ID
336 * \param arg tag to view
337 * \ingroup ui_callback
340 uicb_tag_view(int screen
, char *arg
)
345 tag_view(screen
, atoi(arg
) - 1);
348 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
349 tag
->selected
= True
;
350 saveawesomeprops(screen
);
352 ewmh_update_net_current_desktop(get_phys_screen(screen
));
356 /** View previously selected tags
357 * \param screen Screen ID
359 * \ingroup ui_callback
362 uicb_tag_prev_selected(int screen
, char *arg
__attribute__ ((unused
)))
367 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
370 tag
->selected
= tag
->was_selected
;
371 tag
->was_selected
= t
;
374 ewmh_update_net_current_desktop(get_phys_screen(screen
));
378 * \param screen Screen ID
380 * \ingroup ui_callback
383 uicb_tag_viewnext(int screen
, char *arg
__attribute__ ((unused
)))
385 Tag
**curtags
= get_current_tags(screen
);
387 if(!curtags
[0]->next
)
390 curtags
[0]->selected
= False
;
391 curtags
[0]->next
->selected
= True
;
395 saveawesomeprops(screen
);
397 ewmh_update_net_current_desktop(get_phys_screen(screen
));
400 /** View previous tag
401 * \param screen Screen ID
403 * \ingroup ui_callback
406 uicb_tag_viewprev(int screen
, char *arg
__attribute__ ((unused
)))
408 Tag
*tag
, **curtags
= get_current_tags(screen
);
410 for(tag
= globalconf
.screens
[screen
].tags
; tag
&& tag
->next
!= curtags
[0]; tag
= tag
->next
);
413 tag
->selected
= True
;
414 curtags
[0]->selected
= False
;
415 saveawesomeprops(screen
);
419 ewmh_update_net_current_desktop(get_phys_screen(screen
));
423 uicb_tag_create(int screen
, char *arg
)
430 for(last_tag
= globalconf
.screens
[screen
].tags
; last_tag
&& last_tag
->next
; last_tag
= last_tag
->next
);
431 last_tag
->next
= tag
= p_new(Tag
, 1);
432 tag
->name
= a_strdup(arg
);
433 tag
->layout
= globalconf
.screens
[screen
].layouts
;
439 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80