detect clone screens (FS#42)
[awesome.git] / focus.c
blobd3df95a6eb5e79627e6f24eea2e02aac4dcf0177
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 "common/util.h"
23 #include "tag.h"
24 #include "focus.h"
25 #include "client.h"
27 extern AwesomeConf globalconf;
29 static client_node_t *
30 focus_get_node_by_client(Client *c)
32 client_node_t *node;
34 for(node = globalconf.focus; node; node = node->next)
35 if(node->client == c)
36 return node;
38 return NULL;
41 void
42 focus_add_client(Client *c)
44 client_node_t *node;
46 /* if we don't find this node, create a new one */
47 if(!(node = focus_get_node_by_client(c)))
49 node = p_new(client_node_t, 1);
50 node->client = c;
52 else /* if we've got a node, detach it */
53 client_node_list_detach(&globalconf.focus, node);
55 client_node_list_push(&globalconf.focus, node);
58 void
59 focus_delete_client(Client *c)
61 client_node_t *node = focus_get_node_by_client(c);
63 if(node)
65 client_node_list_detach(&globalconf.focus, node);
66 p_delete(&node);
70 static Client *
71 focus_get_latest_client_for_tags(Tag **t, int nindex)
73 client_node_t *node;
74 Tag **tags;
75 int i = 0;
77 for(node = globalconf.focus; node; node = node->next)
78 if(node->client && !node->client->skip)
79 for(tags = t; *tags; tags++)
80 if(is_client_tagged(node->client, *tags))
82 if(i == nindex)
83 return node->client;
84 else
85 i--;
88 return NULL;
91 Client *
92 focus_get_current_client(int screen)
94 Tag **curtags = get_current_tags(screen);
95 Client *sel = focus_get_latest_client_for_tags(curtags, 0);
96 p_delete(&curtags);
98 return sel;
101 /** Jump in focus history stack
102 * \param screen Screen ID
103 * \param arg Integer argument
104 * \ingroup ui_callback
106 void
107 uicb_focus_history(int screen, char *arg)
109 int i;
110 Tag **curtags;
111 Client *c;
113 if(arg)
115 i = atoi(arg);
117 if(i < 0)
119 curtags = get_current_tags(screen);
120 c = focus_get_latest_client_for_tags(curtags, i);
121 p_delete(&curtags);
122 if(c)
123 focus(c, True, screen);
128 void
129 uicb_focus_client_byname(int screen, char *arg)
131 Client *c;
132 Tag **curtags, **tag;
134 if(arg)
136 curtags = get_current_tags(screen);
137 if((c = get_client_byname(globalconf.clients, arg)))
138 for(tag = curtags; *tag; tag++)
139 if(is_client_tagged(c, *tag))
140 focus(c, True, screen);
141 p_delete(&curtags);
145 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80