fix indentation
[awesome.git] / screen.c
blob66559ef2a28d1292de070a44dcf1374a3d0e1409
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 "statusbar.h"
28 /** Get screens info
29 * \param disp Display ref
30 * \param screen Screen number
31 * \param statusbar statusbar
32 * \return ScreenInfo struct array with all screens info
34 ScreenInfo *
35 get_screen_info(Display *disp, int screen, Statusbar *statusbar)
37 int i, screen_number = 0;
38 ScreenInfo *si;
40 if(XineramaIsActive(disp))
41 si = XineramaQueryScreens(disp, &screen_number);
42 else
44 /* emulate Xinerama info but only fill the screen we want */
45 si = p_new(ScreenInfo, screen + 1);
46 si[screen].width = DisplayWidth(disp, screen);
47 si[screen].height = DisplayHeight(disp, screen);
48 si[screen].x_org = 0;
49 si[screen].y_org = 0;
50 screen_number = screen + 1;
53 if(statusbar)
54 for(i = 0; i < screen_number; i++)
55 switch(statusbar->position)
57 case BarTop:
58 si[i].y_org += statusbar->height;
59 case BarBot:
60 si[i].height -= statusbar->height;
61 break;
62 case BarLeft:
63 si[i].x_org += statusbar->height;
64 case BarRight:
65 si[i].width -= statusbar->height;
66 break;
69 return si;
72 /** Get display info
73 * \param disp Display ref
74 * \param screen Screen number
75 * \param statusbar the statusbar
76 * \return ScreenInfo struct pointer with all display info
78 ScreenInfo *
79 get_display_info(Display *disp, int screen, Statusbar *statusbar)
81 ScreenInfo *si;
83 si = p_new(ScreenInfo, 1);
85 si->x_org = 0;
86 si->y_org = statusbar && statusbar->position == BarTop ? statusbar->height : 0;
87 si->width = DisplayWidth(disp, screen);
88 si->height = DisplayHeight(disp, screen) -
89 (statusbar && (statusbar->position == BarTop || statusbar->position == BarBot) ? statusbar->height : 0);
91 return si;
94 /** Return the Xinerama screen number where the coordinates belongs to
95 * \param disp Display ref
96 * \param x x coordinate of the window
97 * \param y y coordinate of the window
98 * \return screen number or DefaultScreen of disp on no match
101 get_screen_bycoord(Display *disp, int x, int y)
103 ScreenInfo *si;
104 int i;
106 /* don't waste our time */
107 if(!XineramaIsActive(disp))
108 return DefaultScreen(disp);
110 si = get_screen_info(disp, 0, NULL);
112 for(i = 0; i < get_screen_count(disp); i++)
113 if((x < 0 || (x >= si[i].x_org && x < si[i].x_org + si[i].width))
114 && (y< 0 || (y >= si[i].y_org && y < si[i].y_org + si[i].height)))
116 p_delete(&si);
117 return i;
120 p_delete(&si);
121 return DefaultScreen(disp);
124 /** Return the actual screen count
125 * \param disp Display ref
126 * \return the number of screen available
129 get_screen_count(Display *disp)
131 int screen_number;
133 if(XineramaIsActive(disp))
134 XineramaQueryScreens(disp, &screen_number);
135 else
136 return ScreenCount(disp);
138 return screen_number;
141 /** This returns the real X screen number for a logical
142 * screen if Xinerama is active.
143 * \param disp Display ref
144 * \param screen the logical screen
145 * \return the X screen
148 get_phys_screen(Display *disp, int screen)
150 if(XineramaIsActive(disp))
151 return DefaultScreen(disp);
152 return screen;
155 /** Move a client to a virtual screen
156 * \param c the client
157 * \param acf_new the awesome_config for the new screen
158 * \param doresize set to True if we also move the client to the new x_org and
159 * y_org of the new screen
161 void
162 move_client_to_screen(Client *c, awesome_config *acf_new, Bool doresize)
164 int i, old_screen = c->screen;
166 c->screen = acf_new->screen;
168 p_realloc(&c->tags, acf_new->ntags);
169 for(i = 0; i < acf_new->ntags; i++)
170 c->tags[i] = acf_new->tags[i].selected;
172 if(doresize && old_screen != c->screen)
174 ScreenInfo *si, *si_old;
176 si = get_screen_info(c->display, c->screen, NULL);
177 si_old = get_screen_info(c->display, old_screen, NULL);
179 /* compute new coords in new screen */
180 c->rx = (c->rx - si_old[old_screen].x_org) + si[c->screen].x_org;
181 c->ry = (c->ry - si_old[old_screen].y_org) + si[c->screen].y_org;
182 /* check that new coords are still in the screen */
183 if(c->rw > si[c->screen].width)
184 c->rw = si[c->screen].width;
185 if(c->rh > si[c->screen].height)
186 c->rh = si[c->screen].height;
187 if(c->rx + c->rw >= si[c->screen].x_org + si[c->screen].width)
188 c->rx = si[c->screen].x_org + si[c->screen].width - c->rw - 2 * c->border;
189 if(c->ry + c->rh >= si[c->screen].y_org + si[c->screen].height)
190 c->ry = si[c->screen].y_org + si[c->screen].height - c->rh - 2 * c->border;
192 client_resize(c, c->rx, c->ry, c->rw, c->rh, acf_new, True, False);
194 p_delete(&si);
195 p_delete(&si_old);
198 focus(c, True, acf_new);
200 /* redraw statusbar on all screens */
201 drawstatusbar(&acf_new[old_screen - acf_new->screen]);
202 drawstatusbar(acf_new);
205 /** Move mouse pointer to x_org and y_xorg of specified screen
206 * \param disp display ref
207 * \param screen screen number
209 static void
210 move_mouse_pointer_to_screen(Display *disp, int screen)
212 if(XineramaIsActive(disp))
214 ScreenInfo *si = get_screen_info(disp, screen, NULL);
215 XWarpPointer(disp, None, DefaultRootWindow(disp), 0, 0, 0, 0, si[screen].x_org, si[screen].y_org);
216 p_delete(&si);
218 else
219 XWarpPointer(disp, None, RootWindow(disp, screen), 0, 0, 0, 0, 0, 0);
222 void
223 uicb_focusnextscreen(awesome_config * awesomeconf,
224 const char *arg __attribute__ ((unused)))
226 Client *c;
227 int next_screen = awesomeconf->screen + 1 >= get_screen_count(awesomeconf->display) ? 0 : awesomeconf->screen + 1;
229 for(c = *awesomeconf->clients; c && !isvisible(c, next_screen, awesomeconf[next_screen - awesomeconf->screen].tags, awesomeconf[next_screen - awesomeconf->screen].ntags); c = c->next);
230 if(c)
232 focus(c, True, &awesomeconf[next_screen - awesomeconf->screen]);
233 restack(&awesomeconf[next_screen - awesomeconf->screen]);
235 move_mouse_pointer_to_screen(awesomeconf->display, next_screen);
238 void
239 uicb_focusprevscreen(awesome_config * awesomeconf,
240 const char *arg __attribute__ ((unused)))
242 Client *c;
243 int prev_screen = awesomeconf->screen - 1 < 0 ? get_screen_count(awesomeconf->display) - 1 : awesomeconf->screen - 1;
245 for(c = *awesomeconf->clients; c && !isvisible(c, prev_screen, awesomeconf[prev_screen - awesomeconf->screen].tags, awesomeconf[prev_screen - awesomeconf->screen].ntags); c = c->next);
246 if(c)
248 focus(c, True, &awesomeconf[prev_screen - awesomeconf->screen]);
249 restack(&awesomeconf[prev_screen - awesomeconf->screen]);
251 move_mouse_pointer_to_screen(awesomeconf->display, prev_screen);
254 /** Move client to a virtual screen (if Xinerama is active)
255 * \param awesomeconf awesome config
256 * \param arg screen number
257 * \ingroup ui_callback
259 void
260 uicb_movetoscreen(awesome_config * awesomeconf,
261 const char *arg)
263 int new_screen, prev_screen;
264 Client *sel = get_current_tag(awesomeconf->tags, awesomeconf->ntags)->client_sel;
266 if(!sel || !XineramaIsActive(awesomeconf->display))
267 return;
269 if(arg)
270 new_screen = compute_new_value_from_arg(arg, sel->screen);
271 else
272 new_screen = sel->screen + 1;
274 if(new_screen >= get_screen_count(awesomeconf->display))
275 new_screen = 0;
276 else if(new_screen < 0)
277 new_screen = get_screen_count(awesomeconf->display) - 1;
279 prev_screen = sel->screen;
280 move_client_to_screen(sel, &awesomeconf[new_screen - awesomeconf->screen], True);
281 move_mouse_pointer_to_screen(awesomeconf->display, new_screen);
282 arrange(&awesomeconf[prev_screen - awesomeconf->screen]);
283 arrange(&awesomeconf[new_screen - awesomeconf->screen]);
285 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99