windows that start in Withdrawstate are skipped in focus list
[awesome.git] / focus.c
blob5c6e926f9fe0d4a5d3c1eae28ec0d32fb0586158
1 /*
2 * focus.c - focus 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 "util.h"
23 #include "tag.h"
24 #include "layout.h"
25 #include "focus.h"
27 extern AwesomeConf globalconf;
29 static FocusList *
30 focus_get_node_by_client(Client *c)
32 FocusList *fl;
34 for(fl = globalconf.focus; fl; fl = fl->prev)
35 if(fl->client == c)
36 return fl;
38 return NULL;
41 static FocusList *
42 focus_detach_node(FocusList *fl)
44 FocusList *tmp;
46 if(globalconf.focus == fl)
47 globalconf.focus = fl->prev;
48 else
50 for(tmp = globalconf.focus; tmp && tmp->prev != fl; tmp = tmp->prev);
51 tmp->prev = fl->prev;
54 return fl;
57 static FocusList *
58 focus_attach_node(FocusList *fl)
60 FocusList *old_head;
62 old_head = globalconf.focus;
63 globalconf.focus = fl;
64 fl->prev = old_head;
66 return fl;
69 void
70 focus_add_client(Client *c)
72 FocusList *new_fh;
74 /* if we don't find this node, create a new one */
75 if(!(new_fh = focus_get_node_by_client(c)))
77 new_fh = p_new(FocusList, 1);
78 new_fh->client = c;
80 else /* if we've got a node, detach it */
81 focus_detach_node(new_fh);
83 focus_attach_node(new_fh);
86 void
87 focus_delete_client(Client *c)
89 FocusList *target = focus_get_node_by_client(c);
91 if(target)
93 focus_detach_node(target);
94 p_delete(&target);
98 static Client *
99 focus_get_latest_client_for_tags(Tag **t)
101 FocusList *fl;
102 Tag **tags;
104 for(fl = globalconf.focus; fl; fl = fl->prev)
105 for(tags = t; *tags; tags++)
106 if(is_client_tagged(fl->client, *t))
107 return fl->client;
109 return NULL;
112 Client *
113 focus_get_current_client(int screen)
115 Tag **curtags = get_current_tags(screen);
116 Client *sel = focus_get_latest_client_for_tags(curtags);
117 p_delete(&curtags);
119 return sel;
122 /** Jump in focus history stack
123 * \param screen Screen ID
124 * \param arg Integer argument
125 * \ingroup ui_callback
127 void
128 uicb_focus_history(int screen, char *arg)
130 int i;
131 FocusList *fl = globalconf.focus;
132 Tag **curtags, **tag;
134 if(arg)
136 i = atoi(arg);
138 if(i < 0)
140 curtags = get_current_tags(screen);
141 for(; fl && i < 0; fl = fl->prev)
142 for(tag = curtags; *tag; tag++)
143 if(is_client_tagged(fl->client, *tag))
144 i++;
145 p_delete(&curtags);
146 if(fl)
147 focus(fl->client, True, screen);
152 void
153 uicb_focus_client_byname(int screen, char *arg)
155 Client *c;
156 Tag **curtags, **tag;
158 if(arg)
160 curtags = get_current_tags(screen);
161 if((c = get_client_byname(globalconf.clients, arg)))
162 for(tag = curtags; *tag; tag++)
163 if(is_client_tagged(c, *tag))
164 focus(c, True, screen);
165 p_delete(&curtags);
169 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80