adding option 'x' and 'y' and general cleanup
[awesome.git] / screen.c
blobdef89a1c00abb97b4743d8af559139a195cd26c7
1 /*
2 * screen.c - screen 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 <X11/extensions/Xinerama.h>
24 #include "util.h"
25 #include "screen.h"
26 #include "tag.h"
27 #include "focus.h"
28 #include "statusbar.h"
29 #include "client.h"
31 extern AwesomeConf globalconf;
33 /** Get screens info
34 * \param screen Screen number
35 * \param statusbar statusbar
36 * \param padding Padding
37 * \return Area
39 Area
40 get_screen_area(int screen, Statusbar *statusbar, Padding *padding)
42 int screen_number = 0;
43 XineramaScreenInfo *si;
44 Area area;
45 Statusbar *sb;
47 if(XineramaIsActive(globalconf.display))
49 si = XineramaQueryScreens(globalconf.display, &screen_number);
50 if (screen_number < screen)
51 eprint("Info request for unknown screen.");
52 area.x = si[screen].x_org;
53 area.y = si[screen].y_org;
54 area.width = si[screen].width;
55 area.height = si[screen].height;
56 XFree(si);
58 else
60 /* emulate Xinerama info but only fill the screen we want */
61 area.x = 0;
62 area.y = 0;
63 area.width = DisplayWidth(globalconf.display, screen);
64 area.height = DisplayHeight(globalconf.display, screen);
67 /* make padding corrections */
68 if(padding)
70 area.x += padding->left;
71 area.y += padding->top;
72 area.width -= padding->left + padding->right;
73 area.height -= padding->top + padding->bottom;
76 for(sb = statusbar; sb; sb = sb->next)
77 switch(sb->position)
79 case Top:
80 area.y += sb->height;
81 case Bottom:
82 area.height -= sb->height;
83 break;
84 case Left:
85 area.x += sb->height;
86 case Right:
87 area.width -= sb->height;
88 break;
89 case Off:
90 break;
93 return area;
96 /** Get display info
97 * \param screen Screen number
98 * \param statusbar the statusbar
99 * \param padding Padding
100 * \return Area
102 Area
103 get_display_area(int screen, Statusbar *statusbar, Padding *padding)
105 Area area;
106 Statusbar *sb;
108 area.x = 0;
109 area.y = 0;
110 area.width = DisplayWidth(globalconf.display, screen);
111 area.height = DisplayHeight(globalconf.display, screen);
113 for(sb = statusbar; sb; sb = sb->next)
115 area.y += sb->position == Top ? sb->height : 0;
116 area.height -= (sb->position == Top || sb->position == Bottom) ? sb->height : 0;
119 /* make padding corrections */
120 if(padding)
122 area.x += padding->left;
123 area.y += padding->top;
124 area.width -= padding->left + padding->right;
125 area.height -= padding->top + padding->bottom;
127 return area;
130 /** Return the Xinerama screen number where the coordinates belongs to
131 * \param x x coordinate of the window
132 * \param y y coordinate of the window
133 * \return screen number or DefaultScreen of disp on no match
136 get_screen_bycoord(int x, int y)
138 int i;
139 Area area;
141 /* don't waste our time */
142 if(!XineramaIsActive(globalconf.display))
143 return DefaultScreen(globalconf.display);
146 for(i = 0; i < get_screen_count(); i++)
148 area = get_screen_area(i, NULL, NULL);
149 if((x < 0 || (x >= area.x && x < area.x + area.width))
150 && (y < 0 || (y >= area.y && y < area.y + area.height)))
151 return i;
153 return DefaultScreen(globalconf.display);
156 /** Return the actual screen count
157 * \return the number of screen available
160 get_screen_count(void)
162 int screen_number;
164 if(XineramaIsActive(globalconf.display))
165 XineramaQueryScreens(globalconf.display, &screen_number);
166 else
167 return ScreenCount(globalconf.display);
169 return screen_number;
172 /** This returns the real X screen number for a logical
173 * screen if Xinerama is active.
174 * \param screen the logical screen
175 * \return the X screen
178 get_phys_screen(int screen)
180 if(XineramaIsActive(globalconf.display))
181 return DefaultScreen(globalconf.display);
182 return screen;
185 /** Move a client to a virtual screen
186 * \param c the client
187 * \param new_screen The destinatiuon screen
188 * \param doresize set to True if we also move the client to the new x and
189 * y of the new screen
191 void
192 move_client_to_screen(Client *c, int new_screen, Bool doresize)
194 Tag *tag;
195 int old_screen = c->screen;
196 Area from, to;
198 for(tag = globalconf.screens[old_screen].tags; tag; tag = tag->next)
199 untag_client(c, tag);
201 c->screen = new_screen;
203 /* tag client with new screen tags */
204 tag_client_with_current_selected(c);
206 if(doresize && old_screen != c->screen)
208 to = get_screen_area(c->screen, NULL, NULL);
209 from = get_screen_area(old_screen, NULL, NULL);
211 /* compute new coords in new screen */
212 c->rx = (c->rx - from.x) + to.x;
213 c->ry = (c->ry - from.y) + to.y;
214 /* check that new coords are still in the screen */
215 if(c->rw > to.width)
216 c->rw = to.width;
217 if(c->rh > to.height)
218 c->rh = to.height;
219 if(c->rx + c->rw >= to.x + to.width)
220 c->rx = to.x + to.width - c->rw - 2 * c->border;
221 if(c->ry + c->rh >= to.y + to.height)
222 c->ry = to.y + to.height - c->rh - 2 * c->border;
224 client_resize(c, c->rx, c->ry, c->rw, c->rh, True, False);
227 focus(c, True, c->screen);
229 /* redraw statusbar on all screens */
230 statusbar_draw_all(old_screen);
231 statusbar_draw_all(new_screen);
234 /** Move mouse pointer to x_org and y_xorg of specified screen
235 * \param screen screen number
237 static void
238 move_mouse_pointer_to_screen(int screen)
240 if(XineramaIsActive(globalconf.display))
242 Area area = get_screen_area(screen, NULL, NULL);
243 XWarpPointer(globalconf.display,
244 None,
245 DefaultRootWindow(globalconf.display),
246 0, 0, 0, 0, area.x, area.y);
248 else
249 XWarpPointer(globalconf.display,
250 None,
251 RootWindow(globalconf.display, screen),
252 0, 0, 0, 0, 0, 0);
256 /** Switch focus to a specified screen
257 * \param screen Screen ID
258 * \param arg screen number
259 * \ingroup ui_callback
261 void
262 uicb_screen_focus(int screen, char *arg)
264 int new_screen, numscreens = get_screen_count();
266 if(arg)
267 new_screen = compute_new_value_from_arg(arg, screen);
268 else
269 new_screen = screen + 1;
271 if (new_screen < 0)
272 new_screen = numscreens - 1;
273 if (new_screen > (numscreens - 1))
274 new_screen = 0;
276 focus(focus_get_current_client(new_screen), True, new_screen);
278 move_mouse_pointer_to_screen(new_screen);
281 /** Move client to a virtual screen (if Xinerama is active)
282 * \param screen Screen ID
283 * \param arg screen number
284 * \ingroup ui_callback
286 void
287 uicb_client_movetoscreen(int screen __attribute__ ((unused)), char *arg)
289 int new_screen, prev_screen;
290 Client *sel = globalconf.focus->client;
292 if(!sel || !XineramaIsActive(globalconf.display))
293 return;
295 if(arg)
296 new_screen = compute_new_value_from_arg(arg, sel->screen);
297 else
298 new_screen = sel->screen + 1;
300 if(new_screen >= get_screen_count())
301 new_screen = 0;
302 else if(new_screen < 0)
303 new_screen = get_screen_count() - 1;
305 prev_screen = sel->screen;
306 move_client_to_screen(sel, new_screen, True);
307 move_mouse_pointer_to_screen(new_screen);
308 arrange(prev_screen);
309 arrange(new_screen);
311 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80