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 tag_new(const char *name
, Layout
*layout
, double mwfact
, int nmaster
, int ncol
)
40 tag
->name
= a_strdup(name
);
43 tag
->nmaster
= nmaster
;
50 tag_append_to_screen(Tag
*tag
, int screen
)
53 tag_list_append(&globalconf
.screens
[screen
].tags
, tag
);
54 widget_invalidate_cache(screen
, WIDGET_CACHE_TAGS
);
58 tag_push_to_screen(Tag
*tag
, int screen
)
61 tag_list_push(&globalconf
.screens
[screen
].tags
, tag
);
62 widget_invalidate_cache(screen
, WIDGET_CACHE_TAGS
);
66 tag_client(Client
*c
, Tag
*t
)
68 tag_client_node_t
*tc
;
71 if(is_client_tagged(c
, t
))
74 tc
= p_new(tag_client_node_t
, 1);
77 tag_client_node_list_push(&globalconf
.tclink
, tc
);
80 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
81 globalconf
.screens
[c
->screen
].need_arrange
= True
;
85 untag_client(Client
*c
, Tag
*t
)
87 tag_client_node_t
*tc
;
89 for(tc
= globalconf
.tclink
; tc
; tc
= tc
->next
)
90 if(tc
->client
== c
&& tc
->tag
== t
)
92 tag_client_node_list_detach(&globalconf
.tclink
, tc
);
98 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
99 globalconf
.screens
[c
->screen
].need_arrange
= True
;
103 is_client_tagged(Client
*c
, Tag
*t
)
105 tag_client_node_t
*tc
;
110 for(tc
= globalconf
.tclink
; tc
; tc
= tc
->next
)
111 if(tc
->client
== c
&& tc
->tag
== t
)
118 tag_client_with_current_selected(Client
*c
)
121 VirtScreen vscreen
= globalconf
.screens
[c
->screen
];
123 for(tag
= vscreen
.tags
; tag
; tag
= tag
->next
)
127 untag_client(c
, tag
);
131 tag_client_with_rule(Client
*c
, Rule
*r
)
134 Bool matched
= False
;
138 /* check if at least one tag match */
139 for(tag
= globalconf
.screens
[c
->screen
].tags
; tag
; tag
= tag
->next
)
140 if(tag_match_rule(tag
, r
))
147 for(tag
= globalconf
.screens
[c
->screen
].tags
; tag
; tag
= tag
->next
)
148 if(tag_match_rule(tag
, r
))
151 untag_client(c
, tag
);
155 tags_get_current(int screen
)
157 Tag
*tag
, **tags
= NULL
;
160 tags
= p_new(Tag
*, n
);
161 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
164 p_realloc(&tags
, ++n
);
168 /* finish with null */
174 /** Tag selected window with tag
175 * \param screen Screen ID
176 * \param arg Tag name
177 * \ingroup ui_callback
180 uicb_client_tag(int screen
, char *arg
)
183 Tag
*tag
, *target_tag
;
184 Client
*sel
= globalconf
.focus
->client
;
191 tag_id
= atoi(arg
) - 1;
194 for(target_tag
= globalconf
.screens
[screen
].tags
; target_tag
&& tag_id
> 0;
195 target_tag
= target_tag
->next
, tag_id
--);
198 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
199 untag_client(sel
, tag
);
200 tag_client(sel
, target_tag
);
205 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
206 tag_client(sel
, tag
);
209 /** Toggle a tag on client
210 * \param screen Screen ID
211 * \param arg Tag name
212 * \ingroup ui_callback
215 uicb_client_toggletag(int screen
, char *arg
)
217 Client
*sel
= globalconf
.focus
->client
;
219 Tag
*tag
, *target_tag
;
227 for(target_tag
= globalconf
.screens
[screen
].tags
; target_tag
&& i
> 0;
228 target_tag
= target_tag
->next
, i
--);
231 if(is_client_tagged(sel
, target_tag
))
232 untag_client(sel
, target_tag
);
234 tag_client(sel
, target_tag
);
237 /* check that there's at least one tag selected for this client*/
238 for(tag
= globalconf
.screens
[screen
].tags
; tag
239 && !is_client_tagged(sel
, tag
); tag
= tag
->next
)
242 tag_client(sel
, target_tag
);
245 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
246 if(is_client_tagged(sel
, tag
))
247 tag_client(sel
, tag
);
249 untag_client(sel
, tag
);
252 /** Add a tag to viewed tags
253 * \param screen Screen ID
254 * \param arg Tag name
255 * \ingroup ui_callback
258 uicb_tag_toggleview(int screen
, char *arg
)
261 Tag
*tag
, *target_tag
;
266 for(target_tag
= globalconf
.screens
[screen
].tags
; target_tag
&& i
> 0;
267 target_tag
= target_tag
->next
, i
--);
270 tag_view(target_tag
, !target_tag
->selected
);
272 /* check that there's at least one tag selected */
273 for(tag
= globalconf
.screens
[screen
].tags
; tag
&& !tag
->selected
; tag
= tag
->next
);
275 tag_view(target_tag
, True
);
278 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
279 tag_view(tag
, !tag
->selected
);
283 tag_view_only(Tag
*target
)
289 for(tag
= globalconf
.screens
[target
->screen
].tags
; tag
; tag
= tag
->next
)
290 tag_view(tag
, False
);
292 tag_view(target
, True
);
296 tag_view_byindex(int screen
, int dindex
, Bool view
)
303 for(tag
= globalconf
.screens
[screen
].tags
; tag
&& dindex
> 0;
304 tag
= tag
->next
, dindex
--);
309 tag_view_only_byindex(int screen
, int dindex
)
316 for(tag
= globalconf
.screens
[screen
].tags
; tag
&& dindex
> 0;
317 tag
= tag
->next
, dindex
--);
322 tag_view(Tag
*tag
, Bool view
)
324 tag
->was_selected
= tag
->selected
;
325 tag
->selected
= view
;
326 ewmh_update_net_current_desktop(get_phys_screen(tag
->screen
));
327 widget_invalidate_cache(tag
->screen
, WIDGET_CACHE_TAGS
);
328 saveawesomeprops(tag
->screen
);
329 globalconf
.screens
[tag
->screen
].need_arrange
= True
;
333 * \param screen Screen ID
334 * \param arg tag to view
335 * \ingroup ui_callback
338 uicb_tag_view(int screen
, char *arg
)
343 tag_view_only_byindex(screen
, atoi(arg
) - 1);
345 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
349 /** View previously selected tags
350 * \param screen Screen ID
352 * \ingroup ui_callback
355 uicb_tag_prev_selected(int screen
, char *arg
__attribute__ ((unused
)))
359 for(tag
= globalconf
.screens
[screen
].tags
; tag
; tag
= tag
->next
)
360 tag_view(tag
, tag
->was_selected
);
364 * \param screen Screen ID
366 * \ingroup ui_callback
369 uicb_tag_viewnext(int screen
, char *arg
__attribute__ ((unused
)))
371 Tag
*tag
, **curtags
= tags_get_current(screen
);
373 tag
= tag_list_next_cycle(&globalconf
.screens
[screen
].tags
, curtags
[0]);
375 tag_view(curtags
[0], False
);
381 /** View previous tag
382 * \param screen Screen ID
384 * \ingroup ui_callback
387 uicb_tag_viewprev(int screen
, char *arg
__attribute__ ((unused
)))
389 Tag
*tag
, **curtags
= tags_get_current(screen
);
391 tag
= tag_list_prev_cycle(&globalconf
.screens
[screen
].tags
, curtags
[0]);
393 tag_view(curtags
[0], False
);
400 uicb_tag_create(int screen
, char *arg
)
407 tag
= tag_new(arg
, globalconf
.screens
[screen
].layouts
, 0.5, 1, 1);
408 tag_append_to_screen(tag
, screen
);
411 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80