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>
33 extern AwesomeConf globalconf
;
36 detach_tagclientlink(TagClientLink
*tc
)
40 if(globalconf
.tclink
== tc
)
41 globalconf
.tclink
= tc
->next
;
44 for(tmp
= globalconf
.tclink
; tmp
&& tmp
->next
!= tc
; tmp
= tmp
->next
);
52 tag_client(Client
*c
, Tag
*t
)
54 TagClientLink
*tc
, *new_tc
;
57 if(is_client_tagged(c
, t
))
60 new_tc
= p_new(TagClientLink
, 1);
62 if(!globalconf
.tclink
)
63 globalconf
.tclink
= new_tc
;
66 for(tc
= globalconf
.tclink
; tc
->next
; tc
= tc
->next
);
73 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
77 untag_client(Client
*c
, Tag
*t
)
81 for(tc
= globalconf
.tclink
; tc
; tc
= tc
->next
)
82 if(tc
->client
== c
&& tc
->tag
== t
)
84 detach_tagclientlink(tc
);
88 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
92 is_client_tagged(Client
*c
, Tag
*t
)
99 for(tc
= globalconf
.tclink
; tc
; tc
= tc
->next
)
100 if(tc
->client
== c
&& tc
->tag
== t
)
107 tag_client_with_current_selected(Client
*c
)
110 VirtScreen vscreen
= globalconf
.screens
[c
->screen
];
112 for(tag
= vscreen
.tags
; tag
; tag
= tag
->next
)
116 untag_client(c
, tag
);
120 tag_client_with_rules(Client
*c
)
124 Bool matched
= False
;
126 for(r
= globalconf
.rules
; r
; r
= r
->next
)
127 if(client_match_rule(c
, r
))
129 switch(r
->isfloating
)
132 c
->isfloating
= False
;
135 c
->isfloating
= True
;
141 if(r
->screen
!= RULE_NOSCREEN
&& r
->screen
!= c
->screen
)
142 move_client_to_screen(c
, r
->screen
, True
);
144 for(tag
= globalconf
.screens
[c
->screen
].tags
; tag
; tag
= tag
->next
)
145 if(is_tag_match_rules(tag
, r
))
151 untag_client(c
, tag
);
154 tag_client_with_current_selected(c
);
161 get_current_tags(int screen
)
163 Tag
*tag
, **tags
= NULL
;
166 tags
= p_new(Tag
*, n
);
167 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
170 p_realloc(&tags
, ++n
);
174 /* finish with null */
180 /** Tag selected window with tag
181 * \param screen Screen ID
182 * \param arg Tag name
183 * \ingroup ui_callback
186 uicb_client_tag(int screen
, char *arg
)
189 Tag
*tag
, *target_tag
;
190 Client
*sel
= globalconf
.focus
->client
;
197 tag_id
= atoi(arg
) - 1;
200 for(target_tag
= globalconf
.screens
[screen
].tags
; target_tag
&& tag_id
> 0;
201 target_tag
= target_tag
->next
, tag_id
--);
204 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
205 untag_client(sel
, tag
);
206 tag_client(sel
, target_tag
);
211 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
212 tag_client(sel
, tag
);
214 client_saveprops(sel
);
218 /** Toggle a tag on client
219 * \param screen Screen ID
220 * \param arg Tag name
221 * \ingroup ui_callback
224 uicb_client_toggletag(int screen
, char *arg
)
226 Client
*sel
= globalconf
.focus
->client
;
228 Tag
*tag
, *target_tag
;
236 for(target_tag
= globalconf
.screens
[screen
].tags
; target_tag
&& i
> 0;
237 target_tag
= target_tag
->next
, i
--);
240 if(is_client_tagged(sel
, target_tag
))
241 untag_client(sel
, target_tag
);
243 tag_client(sel
, target_tag
);
246 /* check that there's at least one tag selected for this client*/
247 for(tag
= globalconf
.screens
[screen
].tags
; tag
248 && !is_client_tagged(sel
, tag
); tag
= tag
->next
)
251 tag_client(sel
, target_tag
);
254 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
255 if(is_client_tagged(sel
, tag
))
256 tag_client(sel
, tag
);
258 untag_client(sel
, tag
);
260 client_saveprops(sel
);
264 /** Add a tag to viewed tags
265 * \param screen Screen ID
266 * \param arg Tag name
267 * \ingroup ui_callback
270 uicb_tag_toggleview(int screen
, char *arg
)
273 Tag
*tag
, *target_tag
;
278 for(target_tag
= globalconf
.screens
[screen
].tags
; target_tag
&& i
> 0;
279 target_tag
= target_tag
->next
, i
--);
282 target_tag
->selected
= !target_tag
->selected
;
284 /* check that there's at least one tag selected */
285 for(tag
= globalconf
.screens
[screen
].tags
; tag
&& !tag
->selected
; tag
= tag
->next
);
287 target_tag
->selected
= True
;
290 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
291 tag
->selected
= !tag
->selected
;
293 saveawesomeprops(screen
);
295 ewmh_update_net_current_desktop(get_phys_screen(screen
));
296 widget_invalidate_cache(screen
, WIDGET_CACHE_TAGS
);
300 tag_view(int screen
, int dindex
)
302 Tag
*target_tag
, *tag
;
307 for(target_tag
= globalconf
.screens
[screen
].tags
; target_tag
&& dindex
> 0;
308 target_tag
= target_tag
->next
, dindex
--);
311 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
312 tag
->selected
= False
;
313 target_tag
->selected
= True
;
315 saveawesomeprops(screen
);
317 ewmh_update_net_current_desktop(get_phys_screen(screen
));
318 widget_invalidate_cache(screen
, WIDGET_CACHE_TAGS
);
322 * \param screen Screen ID
323 * \param arg tag to view
324 * \ingroup ui_callback
327 uicb_tag_view(int screen
, char *arg
)
332 tag_view(screen
, atoi(arg
) - 1);
335 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
336 tag
->selected
= True
;
337 saveawesomeprops(screen
);
339 ewmh_update_net_current_desktop(get_phys_screen(screen
));
340 widget_invalidate_cache(screen
, WIDGET_CACHE_TAGS
);
344 /** View previously selected tags
345 * \param screen Screen ID
347 * \ingroup ui_callback
350 uicb_tag_prev_selected(int screen
, char *arg
__attribute__ ((unused
)))
355 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
358 tag
->selected
= tag
->was_selected
;
359 tag
->was_selected
= t
;
362 ewmh_update_net_current_desktop(get_phys_screen(screen
));
363 widget_invalidate_cache(screen
, WIDGET_CACHE_TAGS
);
367 * \param screen Screen ID
369 * \ingroup ui_callback
372 uicb_tag_viewnext(int screen
, char *arg
__attribute__ ((unused
)))
374 Tag
**curtags
= get_current_tags(screen
);
376 if(!curtags
[0]->next
)
379 curtags
[0]->selected
= False
;
380 curtags
[0]->next
->selected
= True
;
384 saveawesomeprops(screen
);
386 ewmh_update_net_current_desktop(get_phys_screen(screen
));
387 widget_invalidate_cache(screen
, WIDGET_CACHE_TAGS
);
390 /** View previous tag
391 * \param screen Screen ID
393 * \ingroup ui_callback
396 uicb_tag_viewprev(int screen
, char *arg
__attribute__ ((unused
)))
398 Tag
*tag
, **curtags
= get_current_tags(screen
);
400 for(tag
= globalconf
.screens
[screen
].tags
; tag
&& tag
->next
!= curtags
[0]; tag
= tag
->next
);
403 tag
->selected
= True
;
404 curtags
[0]->selected
= False
;
405 saveawesomeprops(screen
);
409 ewmh_update_net_current_desktop(get_phys_screen(screen
));
410 widget_invalidate_cache(screen
, WIDGET_CACHE_TAGS
);
414 uicb_tag_create(int screen
, char *arg
)
421 for(last_tag
= globalconf
.screens
[screen
].tags
; last_tag
&& last_tag
->next
; last_tag
= last_tag
->next
);
422 last_tag
->next
= tag
= p_new(Tag
, 1);
423 tag
->name
= a_strdup(arg
);
424 tag
->layout
= globalconf
.screens
[screen
].layouts
;
428 widget_invalidate_cache(screen
, WIDGET_CACHE_TAGS
);
431 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80