switch to get_current_tags() in event.c
[awesome.git] / focus.c
blobf101a24857a42f98e14d8b2e54cddad6d416bf22
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 *fh;
34 for(fh = globalconf.focus; fh; fh = fh->prev)
35 if(fh->client == c)
36 return fh;
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 *fc = focus_get_node_by_client(c), *target;
90 if (fc)
92 target = focus_detach_node(fc);
93 p_delete(&target);
97 Client *
98 focus_get_latest_client_for_tag(int screen, Tag *t)
100 FocusList *fl;
102 for(fl = globalconf.focus; fl; fl = fl->prev)
103 if(is_client_tagged(fl->client, t, screen))
104 return fl->client;
106 return NULL;
109 /** Jump in focus history stack
110 * \param screen Screen ID
111 * \param arg Integer argument
112 * \ingroup ui_callback
114 void
115 uicb_focus_history(int screen, char *arg)
117 int i;
118 FocusList *fl = globalconf.focus;
119 Tag **curtags, **tag;
121 if(arg)
123 i = atoi(arg);
125 if(i < 0)
127 curtags = get_current_tags(screen);
128 for(; fl && i < 0; fl = fl->prev)
129 for(tag = curtags; *tag; tag++)
130 if(is_client_tagged(fl->client, *tag, screen))
131 i++;
132 p_delete(&curtags);
133 if(fl)
134 focus(fl->client, True, screen);
139 void
140 uicb_focus_client_byname(int screen, char *arg)
142 Client *c;
143 Tag **curtags, **tag;
145 if(arg)
147 curtags = get_current_tags(screen);
148 if((c = get_client_byname(globalconf.clients, arg)))
149 for(tag = curtags; *tag; tag++)
150 if(is_client_tagged(c, *tag, screen))
151 focus(c, True, screen);
152 p_delete(&curtags);
156 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80