dsound: Don't write more than three periods to IAudioClient.
[wine.git] / dlls / winex11.drv / desktop.c
blob034d7f3991c84b256b405827b27bcd4b8142577c
1 /*
2 * X11DRV desktop window handling
4 * Copyright 2001 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include <X11/cursorfont.h>
23 #include <X11/Xlib.h>
25 #include "x11drv.h"
27 /* avoid conflict with field names in included win32 headers */
28 #undef Status
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
33 /* data for resolution changing */
34 static struct x11drv_mode_info *dd_modes;
35 static unsigned int dd_mode_count;
37 static unsigned int max_width;
38 static unsigned int max_height;
40 static const unsigned int widths[] = {320, 320, 400, 512, 640, 800, 1024, 1152, 1280, 1280, 1400, 1600};
41 static const unsigned int heights[] = {200, 240, 300, 384, 480, 600, 768, 864, 960, 1024, 1050, 1200};
42 #define NUM_DESKTOP_MODES (sizeof(widths) / sizeof(widths[0]))
44 #define _NET_WM_STATE_REMOVE 0
45 #define _NET_WM_STATE_ADD 1
47 /* create the mode structures */
48 static void make_modes(void)
50 RECT primary_rect = get_primary_monitor_rect();
51 unsigned int i;
52 unsigned int screen_width = primary_rect.right - primary_rect.left;
53 unsigned int screen_height = primary_rect.bottom - primary_rect.top;
55 /* original specified desktop size */
56 X11DRV_Settings_AddOneMode(screen_width, screen_height, 0, 60);
57 for (i=0; i<NUM_DESKTOP_MODES; i++)
59 if ( (widths[i] <= max_width) && (heights[i] <= max_height) )
61 if ( ( (widths[i] != max_width) || (heights[i] != max_height) ) &&
62 ( (widths[i] != screen_width) || (heights[i] != screen_height) ) )
64 /* only add them if they are smaller than the root window and unique */
65 X11DRV_Settings_AddOneMode(widths[i], heights[i], 0, 60);
69 if ((max_width != screen_width) && (max_height != screen_height))
71 /* root window size (if different from desktop window) */
72 X11DRV_Settings_AddOneMode(max_width, max_height, 0, 60);
76 static int X11DRV_desktop_GetCurrentMode(void)
78 unsigned int i;
79 DWORD dwBpp = screen_bpp;
80 RECT primary_rect = get_primary_monitor_rect();
82 for (i=0; i<dd_mode_count; i++)
84 if ( (primary_rect.right - primary_rect.left == dd_modes[i].width) &&
85 (primary_rect.bottom - primary_rect.top == dd_modes[i].height) &&
86 (dwBpp == dd_modes[i].bpp))
87 return i;
89 ERR("In unknown mode, returning default\n");
90 return 0;
93 static LONG X11DRV_desktop_SetCurrentMode(int mode)
95 DWORD dwBpp = screen_bpp;
96 if (dwBpp != dd_modes[mode].bpp)
98 FIXME("Cannot change screen BPP from %d to %d\n", dwBpp, dd_modes[mode].bpp);
99 /* Ignore the depth mismatch
101 * Some (older) applications require a specific bit depth, this will allow them
102 * to run. X11drv performs a color depth conversion if needed.
105 TRACE("Resizing Wine desktop window to %dx%d\n", dd_modes[mode].width, dd_modes[mode].height);
106 X11DRV_resize_desktop(dd_modes[mode].width, dd_modes[mode].height);
107 return DISP_CHANGE_SUCCESSFUL;
110 /***********************************************************************
111 * X11DRV_init_desktop
113 * Setup the desktop when not using the root window.
115 void X11DRV_init_desktop( Window win, unsigned int width, unsigned int height )
117 RECT primary_rect = get_primary_monitor_rect();
119 root_window = win;
120 managed_mode = FALSE; /* no managed windows in desktop mode */
121 max_width = primary_rect.right - primary_rect.left;
122 max_height = primary_rect.bottom - primary_rect.top;
123 xinerama_init( width, height );
125 /* initialize the available resolutions */
126 dd_modes = X11DRV_Settings_SetHandlers("desktop",
127 X11DRV_desktop_GetCurrentMode,
128 X11DRV_desktop_SetCurrentMode,
129 NUM_DESKTOP_MODES+2, 1);
130 make_modes();
131 X11DRV_Settings_AddDepthModes();
132 dd_mode_count = X11DRV_Settings_GetModeCount();
136 /***********************************************************************
137 * X11DRV_create_desktop
139 * Create the X11 desktop window for the desktop mode.
141 BOOL CDECL X11DRV_create_desktop( UINT width, UINT height )
143 XSetWindowAttributes win_attr;
144 Window win;
145 Display *display = thread_init_display();
146 RECT rect;
148 TRACE( "%u x %u\n", width, height );
150 /* Create window */
151 win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | EnterWindowMask |
152 PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask;
153 win_attr.cursor = XCreateFontCursor( display, XC_top_left_arrow );
155 if (default_visual.visual != DefaultVisual( display, DefaultScreen(display) ))
156 win_attr.colormap = XCreateColormap( display, DefaultRootWindow(display),
157 default_visual.visual, AllocNone );
158 else
159 win_attr.colormap = None;
161 win = XCreateWindow( display, DefaultRootWindow(display),
162 0, 0, width, height, 0, default_visual.depth, InputOutput, default_visual.visual,
163 CWEventMask | CWCursor | CWColormap, &win_attr );
164 if (!win) return FALSE;
166 SetRect( &rect, 0, 0, width, height );
167 if (is_window_rect_fullscreen( &rect ))
169 TRACE("setting desktop to fullscreen\n");
170 XChangeProperty( display, win, x11drv_atom(_NET_WM_STATE), XA_ATOM, 32,
171 PropModeReplace, (unsigned char*)&x11drv_atom(_NET_WM_STATE_FULLSCREEN),
174 if (!create_desktop_win_data( win )) return FALSE;
175 XFlush( display );
176 X11DRV_init_desktop( win, width, height );
177 return TRUE;
181 struct desktop_resize_data
183 RECT old_virtual_rect;
184 RECT new_virtual_rect;
187 static BOOL CALLBACK update_windows_on_desktop_resize( HWND hwnd, LPARAM lparam )
189 struct x11drv_win_data *data;
190 struct desktop_resize_data *resize_data = (struct desktop_resize_data *)lparam;
191 int mask = 0;
193 if (!(data = get_win_data( hwnd ))) return TRUE;
195 /* update the full screen state */
196 update_net_wm_states( data );
198 if (resize_data->old_virtual_rect.left != resize_data->new_virtual_rect.left) mask |= CWX;
199 if (resize_data->old_virtual_rect.top != resize_data->new_virtual_rect.top) mask |= CWY;
200 if (mask && data->whole_window)
202 POINT pos = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
203 XWindowChanges changes;
204 changes.x = pos.x;
205 changes.y = pos.y;
206 XReconfigureWMWindow( data->display, data->whole_window, data->vis.screen, mask, &changes );
208 release_win_data( data );
209 if (hwnd == GetForegroundWindow()) clip_fullscreen_window( hwnd, TRUE );
210 return TRUE;
213 BOOL is_desktop_fullscreen(void)
215 RECT primary_rect = get_primary_monitor_rect();
216 return (primary_rect.right - primary_rect.left == max_width &&
217 primary_rect.bottom - primary_rect.top == max_height);
220 static void update_desktop_fullscreen( unsigned int width, unsigned int height)
222 Display *display = thread_display();
223 XEvent xev;
225 if (!display || root_window == DefaultRootWindow( display )) return;
227 xev.xclient.type = ClientMessage;
228 xev.xclient.window = root_window;
229 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
230 xev.xclient.serial = 0;
231 xev.xclient.display = display;
232 xev.xclient.send_event = True;
233 xev.xclient.format = 32;
234 if (width == max_width && height == max_height)
235 xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
236 else
237 xev.xclient.data.l[0] = _NET_WM_STATE_REMOVE;
238 xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_FULLSCREEN);
239 xev.xclient.data.l[2] = 0;
240 xev.xclient.data.l[3] = 1;
242 TRACE("action=%li\n", xev.xclient.data.l[0]);
244 XSendEvent( display, DefaultRootWindow(display), False,
245 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
247 xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_VERT);
248 xev.xclient.data.l[2] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
249 XSendEvent( display, DefaultRootWindow(display), False,
250 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
253 /***********************************************************************
254 * X11DRV_resize_desktop
256 void X11DRV_resize_desktop( unsigned int width, unsigned int height )
258 HWND hwnd = GetDesktopWindow();
259 struct desktop_resize_data resize_data;
261 resize_data.old_virtual_rect = get_virtual_screen_rect();
263 xinerama_init( width, height );
264 resize_data.new_virtual_rect = get_virtual_screen_rect();
266 if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
268 SendMessageW( hwnd, WM_X11DRV_RESIZE_DESKTOP, 0, MAKELPARAM( width, height ) );
270 else
272 TRACE( "desktop %p change to (%dx%d)\n", hwnd, width, height );
273 update_desktop_fullscreen( width, height );
274 SetWindowPos( hwnd, 0, resize_data.new_virtual_rect.left, resize_data.new_virtual_rect.top,
275 resize_data.new_virtual_rect.right - resize_data.new_virtual_rect.left,
276 resize_data.new_virtual_rect.bottom - resize_data.new_virtual_rect.top,
277 SWP_NOZORDER | SWP_NOACTIVATE | SWP_DEFERERASE );
278 ungrab_clipping_window();
279 SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_bpp,
280 MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
283 EnumWindows( update_windows_on_desktop_resize, (LPARAM)&resize_data );