awesome-client send now everything in one message
[awesome.git] / focus.c
blobe52ed23820175a1d37602651d00dfdea7e45872b
1 /*
2 * focus.c - focus management
4 * Copyright © 2007-2008 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 "focus.h"
25 #include "client.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, int nindex)
101 FocusList *fl;
102 Tag **tags;
103 int i = 0;
105 for(fl = globalconf.focus; fl; fl = fl->prev)
106 if(fl->client && !fl->client->skip)
107 for(tags = t; *tags; tags++)
108 if(is_client_tagged(fl->client, *tags))
110 if(i == nindex)
111 return fl->client;
112 else
113 i--;
116 return NULL;
119 Client *
120 focus_get_current_client(int screen)
122 Tag **curtags = get_current_tags(screen);
123 Client *sel = focus_get_latest_client_for_tags(curtags, 0);
124 p_delete(&curtags);
126 return sel;
129 /** Jump in focus history stack
130 * \param screen Screen ID
131 * \param arg Integer argument
132 * \ingroup ui_callback
134 void
135 uicb_focus_history(int screen, char *arg)
137 int i;
138 Tag **curtags;
139 Client *c;
141 if(arg)
143 i = atoi(arg);
145 if(i < 0)
147 curtags = get_current_tags(screen);
148 c = focus_get_latest_client_for_tags(curtags, i);
149 p_delete(&curtags);
150 if(c)
151 focus(c, True, screen);
156 void
157 uicb_focus_client_byname(int screen, char *arg)
159 Client *c;
160 Tag **curtags, **tag;
162 if(arg)
164 curtags = get_current_tags(screen);
165 if((c = get_client_byname(globalconf.clients, arg)))
166 for(tag = curtags; *tag; tag++)
167 if(is_client_tagged(c, *tag))
168 focus(c, True, screen);
169 p_delete(&curtags);
173 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80