simplify proto
[awesome.git] / screen.c
blob3fd903ac5fa1f2134763cee5dc6ee6f8748510e8
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 "util.h"
23 #include "screen.h"
24 #include "tag.h"
25 #include "layout.h"
26 #include "focus.h"
27 #include "statusbar.h"
29 extern AwesomeConf globalconf;
31 /** Get screens info
32 * \param screen Screen number
33 * \param statusbar statusbar
34 * \return Area
35 * \param padding Padding
37 Area
38 get_screen_area(int screen, Statusbar *statusbar, Padding *padding)
40 int screen_number = 0;
41 XineramaScreenInfo *si;
42 Area area;
44 if(XineramaIsActive(globalconf.display))
46 si = XineramaQueryScreens(globalconf.display, &screen_number);
47 if (screen_number < screen)
48 eprint("Info request for unknown screen.");
49 area.x = si[screen].x_org;
50 area.y = si[screen].y_org;
51 area.width = si[screen].width;
52 area.height = si[screen].height;
53 XFree(si);
55 else
57 /* emulate Xinerama info but only fill the screen we want */
58 area.x = 0;
59 area.y = 0;
60 area.width = DisplayWidth(globalconf.display, screen);
61 area.height = DisplayHeight(globalconf.display, screen);
64 /* make padding corrections */
65 if(padding)
67 area.x += padding->left;
68 area.y += padding->top;
69 area.width -= padding->left + padding->right;
70 area.height -= padding->top + padding->bottom;
73 if (statusbar)
74 switch(statusbar->position)
76 case BarTop:
77 area.y += statusbar->height;
78 case BarBot:
79 area.height -= statusbar->height;
80 break;
81 case BarLeft:
82 area.x += statusbar->height;
83 case BarRight:
84 area.width -= statusbar->height;
85 break;
88 return area;
91 /** Get display info
92 * \param screen Screen number
93 * \param statusbar the statusbar
94 * \param padding Padding
95 * \return Area
97 Area
98 get_display_area(int screen, Statusbar *statusbar, Padding *padding)
100 Area area;
102 area.x = 0;
103 area.y = statusbar && statusbar->position == BarTop ? statusbar->height : 0;
104 area.width = DisplayWidth(globalconf.display, screen);
105 area.height = DisplayHeight(globalconf.display, screen) -
106 (statusbar &&
107 (statusbar->position == BarTop || statusbar->position == BarBot) ? statusbar->height : 0);
109 /* make padding corrections */
110 if(padding)
112 area.x += padding->left;
113 area.y += padding->top;
114 area.width -= padding->left + padding->right;
115 area.height -= padding->top + padding->bottom;
117 return area;
120 /** Return the Xinerama screen number where the coordinates belongs to
121 * \param x x coordinate of the window
122 * \param y y coordinate of the window
123 * \return screen number or DefaultScreen of disp on no match
126 get_screen_bycoord(int x, int y)
128 int i;
129 Area area;
131 /* don't waste our time */
132 if(!XineramaIsActive(globalconf.display))
133 return DefaultScreen(globalconf.display);
136 for(i = 0; i < get_screen_count(); i++)
138 area = get_screen_area(i, NULL, NULL);
139 if((x < 0 || (x >= area.x && x < area.x + area.width))
140 && (y < 0 || (y >= area.y && y < area.y + area.height)))
141 return i;
143 return DefaultScreen(globalconf.display);
146 /** Return the actual screen count
147 * \return the number of screen available
150 get_screen_count(void)
152 int screen_number;
154 if(XineramaIsActive(globalconf.display))
155 XineramaQueryScreens(globalconf.display, &screen_number);
156 else
157 return ScreenCount(globalconf.display);
159 return screen_number;
162 /** This returns the real X screen number for a logical
163 * screen if Xinerama is active.
164 * \param screen the logical screen
165 * \return the X screen
168 get_phys_screen(int screen)
170 if(XineramaIsActive(globalconf.display))
171 return DefaultScreen(globalconf.display);
172 return screen;
175 /** Move a client to a virtual screen
176 * \param c the client
177 * \param new_screen The destinatiuon screen
178 * \param doresize set to True if we also move the client to the new x_org and
179 * y_org of the new screen
181 void
182 move_client_to_screen(Client *c, int new_screen, Bool doresize)
184 Tag *tag;
185 int old_screen = c->screen;
186 Area from, to;
188 for(tag = globalconf.screens[old_screen].tags; tag; tag = tag->next)
189 untag_client(c, tag, old_screen);
191 /* tag client with new screen tags */
192 tag_client_with_current_selected(c);
194 c->screen = new_screen;
196 if(doresize && old_screen != c->screen)
198 to = get_screen_area(c->screen, NULL, NULL);
199 from = get_screen_area(old_screen, NULL, NULL);
201 /* compute new coords in new screen */
202 c->rx = (c->rx - from.x) + to.x;
203 c->ry = (c->ry - from.y) + to.y;
204 /* check that new coords are still in the screen */
205 if(c->rw > to.width)
206 c->rw = to.width;
207 if(c->rh > to.height)
208 c->rh = to.height;
209 if(c->rx + c->rw >= to.x + to.width)
210 c->rx = to.x + to.width - c->rw - 2 * c->border;
211 if(c->ry + c->rh >= to.y + to.height)
212 c->ry = to.y + to.height - c->rh - 2 * c->border;
214 client_resize(c, c->rx, c->ry, c->rw, c->rh, True, False);
217 focus(c, True, c->screen);
219 /* redraw statusbar on all screens */
220 statusbar_draw(old_screen);
221 statusbar_draw(new_screen);
224 /** Move mouse pointer to x_org and y_xorg of specified screen
225 * \param screen screen number
227 static void
228 move_mouse_pointer_to_screen(int screen)
230 if(XineramaIsActive(globalconf.display))
232 Area area = get_screen_area(screen, NULL, NULL);
233 XWarpPointer(globalconf.display,
234 None,
235 DefaultRootWindow(globalconf.display),
236 0, 0, 0, 0, area.x, area.y);
238 else
239 XWarpPointer(globalconf.display,
240 None,
241 RootWindow(globalconf.display, screen),
242 0, 0, 0, 0, 0, 0);
246 /** Switch focus to a specified screen
247 * \param screen Screen ID
248 * \param arg screen number
249 * \ingroup ui_callback
251 void
252 uicb_screen_focus(int screen, char *arg)
254 int new_screen, numscreens = get_screen_count();
255 Tag **curtags = get_current_tags(screen);
257 if(arg)
258 new_screen = compute_new_value_from_arg(arg, screen);
259 else
260 new_screen = screen + 1;
262 if (new_screen < 0)
263 new_screen = numscreens - 1;
264 if (new_screen > (numscreens - 1))
265 new_screen = 0;
267 focus(focus_get_latest_client_for_tag(new_screen, curtags[0]),
268 True, new_screen);
270 p_delete(&curtags);
272 move_mouse_pointer_to_screen(new_screen);
275 /** Move client to a virtual screen (if Xinerama is active)
276 * \param screen Screen ID
277 * \param arg screen number
278 * \ingroup ui_callback
280 void
281 uicb_client_movetoscreen(int screen __attribute__ ((unused)), char *arg)
283 int new_screen, prev_screen;
284 Client *sel = globalconf.focus->client;
286 if(!sel || !XineramaIsActive(globalconf.display))
287 return;
289 if(arg)
290 new_screen = compute_new_value_from_arg(arg, sel->screen);
291 else
292 new_screen = sel->screen + 1;
294 if(new_screen >= get_screen_count())
295 new_screen = 0;
296 else if(new_screen < 0)
297 new_screen = get_screen_count() - 1;
299 prev_screen = sel->screen;
300 move_client_to_screen(sel, new_screen, True);
301 move_mouse_pointer_to_screen(new_screen);
302 arrange(prev_screen);
303 arrange(new_screen);
305 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80