desktop.c 10.2 KB
Newer Older
1 2 3 4
/*
 * X11DRV desktop window handling
 *
 * Copyright 2001 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
19 20 21 22
 */

#include "config.h"
#include <X11/cursorfont.h>
23
#include <X11/Xlib.h>
24 25

#include "x11drv.h"
26 27 28

/* avoid conflict with field names in included win32 headers */
#undef Status
29
#include "wine/debug.h"
30

31
WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
32

33
/* data for resolution changing */
34
static struct x11drv_mode_info *dd_modes;
35
static unsigned int dd_mode_count;
36 37 38 39

static unsigned int max_width;
static unsigned int max_height;

40 41 42
static const unsigned int widths[]  = {320, 400, 512, 640, 800, 1024, 1152, 1280, 1400, 1600};
static const unsigned int heights[] = {200, 300, 384, 480, 600,  768,  864, 1024, 1050, 1200};
#define NUM_DESKTOP_MODES (sizeof(widths) / sizeof(widths[0]))
43

44 45 46
#define _NET_WM_STATE_REMOVE 0
#define _NET_WM_STATE_ADD 1

47 48 49
/* create the mode structures */
static void make_modes(void)
{
50
    RECT primary_rect = get_primary_monitor_rect();
51
    unsigned int i;
52 53 54
    unsigned int screen_width = primary_rect.right - primary_rect.left;
    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++)
58
    {
59 60 61 62 63 64
        if ( (widths[i] <= max_width) && (heights[i] <= max_height) )
        {
            if ( ( (widths[i] != max_width) || (heights[i] != max_height) ) &&
                 ( (widths[i] != screen_width) || (heights[i] != screen_height) ) )
            {
                /* only add them if they are smaller than the root window and unique */
65
                X11DRV_Settings_AddOneMode(widths[i], heights[i], 0, 60);
66 67
            }
        }
68
    }
69
    if ((max_width != screen_width) && (max_height != screen_height))
70
    {
71
        /* root window size (if different from desktop window) */
72
        X11DRV_Settings_AddOneMode(max_width, max_height, 0, 60);
73 74 75
    }
}

76
static int X11DRV_desktop_GetCurrentMode(void)
77
{
78
    unsigned int i;
79
    DWORD dwBpp = screen_bpp;
80 81
    RECT primary_rect = get_primary_monitor_rect();

82 83
    for (i=0; i<dd_mode_count; i++)
    {
84 85
        if ( (primary_rect.right - primary_rect.left == dd_modes[i].width) &&
             (primary_rect.bottom - primary_rect.top == dd_modes[i].height) &&
86
             (dwBpp == dd_modes[i].bpp))
87 88 89 90 91 92
            return i;
    }
    ERR("In unknown mode, returning default\n");
    return 0;
}

93
static LONG X11DRV_desktop_SetCurrentMode(int mode)
94
{
95
    DWORD dwBpp = screen_bpp;
96
    if (dwBpp != dd_modes[mode].bpp)
97
    {
98
        FIXME("Cannot change screen BPP from %d to %d\n", dwBpp, dd_modes[mode].bpp);
Austin English's avatar
Austin English committed
99
        /* Ignore the depth mismatch
100 101 102 103
         *
         * Some (older) applications require a specific bit depth, this will allow them
         * to run. X11drv performs a color depth conversion if needed.
         */
104
    }
105 106
    TRACE("Resizing Wine desktop window to %dx%d\n", dd_modes[mode].width, dd_modes[mode].height);
    X11DRV_resize_desktop(dd_modes[mode].width, dd_modes[mode].height);
107
    return DISP_CHANGE_SUCCESSFUL;
108
}
109

110 111 112 113 114 115 116
/***********************************************************************
 *		X11DRV_init_desktop
 *
 * Setup the desktop when not using the root window.
 */
void X11DRV_init_desktop( Window win, unsigned int width, unsigned int height )
{
117 118
    RECT primary_rect = get_primary_monitor_rect();

119
    root_window = win;
120
    managed_mode = FALSE;  /* no managed windows in desktop mode */
121 122
    max_width = primary_rect.right - primary_rect.left;
    max_height = primary_rect.bottom - primary_rect.top;
123
    xinerama_init( width, height );
124 125 126 127 128 129 130 131 132 133 134 135

    /* initialize the available resolutions */
    dd_modes = X11DRV_Settings_SetHandlers("desktop", 
                                           X11DRV_desktop_GetCurrentMode, 
                                           X11DRV_desktop_SetCurrentMode, 
                                           NUM_DESKTOP_MODES+2, 1);
    make_modes();
    X11DRV_Settings_AddDepthModes();
    dd_mode_count = X11DRV_Settings_GetModeCount();
}


136 137 138 139 140
/***********************************************************************
 *		X11DRV_create_desktop
 *
 * Create the X11 desktop window for the desktop mode.
 */
141
BOOL CDECL X11DRV_create_desktop( UINT width, UINT height )
142 143 144
{
    XSetWindowAttributes win_attr;
    Window win;
145
    Display *display = thread_init_display();
146
    RECT rect;
147

148 149
    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 154
    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 159 160 161
    else
        win_attr.colormap = None;

    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 165
    if (!win) return FALSE;

166 167
    SetRect( &rect, 0, 0, width, height );
    if (is_window_rect_fullscreen( &rect ))
168 169 170 171 172 173
    {
        TRACE("setting desktop to fullscreen\n");
        XChangeProperty( display, win, x11drv_atom(_NET_WM_STATE), XA_ATOM, 32,
            PropModeReplace, (unsigned char*)&x11drv_atom(_NET_WM_STATE_FULLSCREEN),
            1);
    }
174
    if (!create_desktop_win_data( win )) return FALSE;
175
    XFlush( display );
176 177
    X11DRV_init_desktop( win, width, height );
    return TRUE;
178
}
179 180 181 182 183 184


struct desktop_resize_data
{
    RECT  old_screen_rect;
    RECT  old_virtual_rect;
185
    RECT  new_virtual_rect;
186 187 188 189 190 191 192 193
};

static BOOL CALLBACK update_windows_on_desktop_resize( HWND hwnd, LPARAM lparam )
{
    struct x11drv_win_data *data;
    struct desktop_resize_data *resize_data = (struct desktop_resize_data *)lparam;
    int mask = 0;

194
    if (!(data = get_win_data( hwnd ))) return TRUE;
195 196

    /* update the full screen state */
197
    update_net_wm_states( data );
198

199 200
    if (resize_data->old_virtual_rect.left != resize_data->new_virtual_rect.left) mask |= CWX;
    if (resize_data->old_virtual_rect.top != resize_data->new_virtual_rect.top) mask |= CWY;
201 202
    if (mask && data->whole_window)
    {
203
        POINT pos = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
204
        XWindowChanges changes;
205 206
        changes.x = pos.x;
        changes.y = pos.y;
207
        XReconfigureWMWindow( data->display, data->whole_window, data->vis.screen, mask, &changes );
208
    }
209
    release_win_data( data );
210
    if (hwnd == GetForegroundWindow()) clip_fullscreen_window( hwnd, TRUE );
211 212 213
    return TRUE;
}

214 215
BOOL is_desktop_fullscreen(void)
{
216 217 218
    RECT primary_rect = get_primary_monitor_rect();
    return (primary_rect.right - primary_rect.left == max_width &&
            primary_rect.bottom - primary_rect.top == max_height);
219 220
}

221 222 223 224 225
static void update_desktop_fullscreen( unsigned int width, unsigned int height)
{
    Display *display = thread_display();
    XEvent xev;

226
    if (!display || root_window == DefaultRootWindow( display )) return;
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

    xev.xclient.type = ClientMessage;
    xev.xclient.window = root_window;
    xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
    xev.xclient.serial = 0;
    xev.xclient.display = display;
    xev.xclient.send_event = True;
    xev.xclient.format = 32;
    if (width == max_width && height == max_height)
        xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
    else
        xev.xclient.data.l[0] = _NET_WM_STATE_REMOVE;
    xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_FULLSCREEN);
    xev.xclient.data.l[2] = 0;
    xev.xclient.data.l[3] = 1;

    TRACE("action=%li\n", xev.xclient.data.l[0]);

245 246 247 248 249
    XSendEvent( display, DefaultRootWindow(display), False,
                SubstructureRedirectMask | SubstructureNotifyMask, &xev );

    xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_VERT);
    xev.xclient.data.l[2] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
250 251 252
    XSendEvent( display, DefaultRootWindow(display), False,
                SubstructureRedirectMask | SubstructureNotifyMask, &xev );
}
253 254 255 256 257 258 259 260 261

/***********************************************************************
 *		X11DRV_resize_desktop
 */
void X11DRV_resize_desktop( unsigned int width, unsigned int height )
{
    HWND hwnd = GetDesktopWindow();
    struct desktop_resize_data resize_data;

262
    resize_data.old_screen_rect = get_primary_monitor_rect();
263
    resize_data.old_virtual_rect = get_virtual_screen_rect();
264 265

    xinerama_init( width, height );
266
    resize_data.new_virtual_rect = get_virtual_screen_rect();
267 268 269 270 271 272 273 274

    if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
    {
        SendMessageW( hwnd, WM_X11DRV_RESIZE_DESKTOP, 0, MAKELPARAM( width, height ) );
    }
    else
    {
        TRACE( "desktop %p change to (%dx%d)\n", hwnd, width, height );
275
        update_desktop_fullscreen( width, height );
276 277 278
        SetWindowPos( hwnd, 0, resize_data.new_virtual_rect.left, resize_data.new_virtual_rect.top,
                      resize_data.new_virtual_rect.right - resize_data.new_virtual_rect.left,
                      resize_data.new_virtual_rect.bottom - resize_data.new_virtual_rect.top,
279
                      SWP_NOZORDER | SWP_NOACTIVATE | SWP_DEFERERASE );
280
        ungrab_clipping_window();
281 282 283 284 285 286
        SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_bpp,
                             MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
    }

    EnumWindows( update_windows_on_desktop_resize, (LPARAM)&resize_data );
}