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 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
))
136 untag_client(c
, tag
);
139 tag_client_with_current_selected(c
);
146 get_current_tags(int screen
)
148 Tag
*tag
, **tags
= NULL
;
151 tags
= p_new(Tag
*, n
);
152 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
155 p_realloc(&tags
, ++n
);
159 /* finish with null */
165 /** Tag selected window with tag
166 * \param screen Screen ID
167 * \param arg Tag name
168 * \ingroup ui_callback
171 uicb_client_tag(int screen
, char *arg
)
174 Tag
*tag
, *target_tag
;
175 Client
*sel
= globalconf
.focus
->client
;
182 tag_id
= atoi(arg
) - 1;
185 for(target_tag
= globalconf
.screens
[screen
].tags
; target_tag
&& tag_id
> 0;
186 target_tag
= target_tag
->next
, tag_id
--);
189 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
190 untag_client(sel
, tag
);
191 tag_client(sel
, target_tag
);
196 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
197 tag_client(sel
, tag
);
199 client_saveprops(sel
);
203 /** Toggle floating state of a client
204 * \param screen Screen ID
206 * \ingroup ui_callback
209 uicb_client_togglefloating(int screen
, char *arg
__attribute__ ((unused
)))
211 Client
*sel
= globalconf
.focus
->client
;
216 sel
->isfloating
= !sel
->isfloating
;
219 client_resize(sel
, sel
->rx
, sel
->ry
, sel
->rw
, sel
->rh
, True
, False
);
221 client_resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
, True
);
223 client_saveprops(sel
);
227 /** Toggle a tag on client
228 * \param screen Screen ID
229 * \param arg Tag name
230 * \ingroup ui_callback
233 uicb_client_toggletag(int screen
, char *arg
)
235 Client
*sel
= globalconf
.focus
->client
;
237 Tag
*tag
, *target_tag
;
245 for(target_tag
= globalconf
.screens
[screen
].tags
; target_tag
&& i
> 0;
246 target_tag
= target_tag
->next
, i
--);
249 if(is_client_tagged(sel
, target_tag
))
250 untag_client(sel
, target_tag
);
252 tag_client(sel
, target_tag
);
255 /* check that there's at least one tag selected for this client*/
256 for(tag
= globalconf
.screens
[screen
].tags
; tag
257 && !is_client_tagged(sel
, tag
); tag
= tag
->next
)
260 tag_client(sel
, target_tag
);
263 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
264 if(is_client_tagged(sel
, tag
))
265 tag_client(sel
, tag
);
267 untag_client(sel
, tag
);
269 client_saveprops(sel
);
273 /** Add a tag to viewed tags
274 * \param screen Screen ID
275 * \param arg Tag name
276 * \ingroup ui_callback
279 uicb_tag_toggleview(int screen
, char *arg
)
282 Tag
*tag
, *target_tag
;
287 for(target_tag
= globalconf
.screens
[screen
].tags
; target_tag
&& i
> 0;
288 target_tag
= target_tag
->next
, i
--);
291 target_tag
->selected
= !target_tag
->selected
;
293 /* check that there's at least one tag selected */
294 for(tag
= globalconf
.screens
[screen
].tags
; tag
&& !tag
->selected
; tag
= tag
->next
);
296 target_tag
->selected
= True
;
299 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
300 tag
->selected
= !tag
->selected
;
302 saveawesomeprops(screen
);
304 ewmh_update_net_current_desktop(get_phys_screen(screen
));
308 tag_view(int screen
, int dindex
)
310 Tag
*target_tag
, *tag
;
315 for(target_tag
= globalconf
.screens
[screen
].tags
; target_tag
&& dindex
> 0;
316 target_tag
= target_tag
->next
, dindex
--);
319 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
320 tag
->selected
= False
;
321 target_tag
->selected
= True
;
323 saveawesomeprops(screen
);
325 ewmh_update_net_current_desktop(get_phys_screen(screen
));
329 * \param screen Screen ID
330 * \param arg tag to view
331 * \ingroup ui_callback
334 uicb_tag_view(int screen
, char *arg
)
339 tag_view(screen
, atoi(arg
) - 1);
342 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
343 tag
->selected
= True
;
344 saveawesomeprops(screen
);
346 ewmh_update_net_current_desktop(get_phys_screen(screen
));
350 /** View previously selected tags
351 * \param screen Screen ID
353 * \ingroup ui_callback
356 uicb_tag_prev_selected(int screen
, char *arg
__attribute__ ((unused
)))
361 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
364 tag
->selected
= tag
->was_selected
;
365 tag
->was_selected
= t
;
368 ewmh_update_net_current_desktop(get_phys_screen(screen
));
372 * \param screen Screen ID
374 * \ingroup ui_callback
377 uicb_tag_viewnext(int screen
, char *arg
__attribute__ ((unused
)))
379 Tag
**curtags
= get_current_tags(screen
);
381 if(!curtags
[0]->next
)
384 curtags
[0]->selected
= False
;
385 curtags
[0]->next
->selected
= True
;
389 saveawesomeprops(screen
);
391 ewmh_update_net_current_desktop(get_phys_screen(screen
));
394 /** View previous tag
395 * \param screen Screen ID
397 * \ingroup ui_callback
400 uicb_tag_viewprev(int screen
, char *arg
__attribute__ ((unused
)))
402 Tag
*tag
, **curtags
= get_current_tags(screen
);
404 for(tag
= globalconf
.screens
[screen
].tags
; tag
&& tag
->next
!= curtags
[0]; tag
= tag
->next
);
407 tag
->selected
= True
;
408 curtags
[0]->selected
= False
;
409 saveawesomeprops(screen
);
413 ewmh_update_net_current_desktop(get_phys_screen(screen
));
417 uicb_tag_create(int screen
, char *arg
)
424 for(last_tag
= globalconf
.screens
[screen
].tags
; last_tag
&& last_tag
->next
; last_tag
= last_tag
->next
);
425 last_tag
->next
= tag
= p_new(Tag
, 1);
426 tag
->name
= a_strdup(arg
);
427 tag
->layout
= globalconf
.screens
[screen
].layouts
;
433 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80