settings.c 14.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Wine X11drv display settings functions
 *
 * Copyright 2003 Alexander James Pasadyn
 *
 * 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 23
 */

#include "config.h"
#include <string.h>
#include <stdio.h>
24
#include <assert.h>
25 26 27 28

#define NONAMELESSUNION
#define NONAMELESSSTRUCT

29 30 31
#include "x11drv.h"

#include "windef.h"
32
#include "winreg.h"
33 34 35 36 37
#include "wingdi.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(x11settings);

38
static struct x11drv_mode_info *dd_modes = NULL;
39 40
static unsigned int dd_mode_count = 0;
static unsigned int dd_max_modes = 0;
41 42 43 44 45
/* All Windows drivers seen so far either support 32 bit depths, or 24 bit depths, but never both. So if we have
 * a 32 bit framebuffer, report 32 bit bpps, otherwise 24 bit ones.
 */
static const unsigned int depths_24[]  = {8, 16, 24};
static const unsigned int depths_32[]  = {8, 16, 32};
46 47 48

/* pointers to functions that actually do the hard stuff */
static int (*pGetCurrentMode)(void);
49
static LONG (*pSetCurrentMode)(int mode);
50 51
static const char *handler_name;

52
/*
53 54 55
 * Set the handlers for resolution changing functions
 * and initialize the master list of modes
 */
56 57 58 59 60
struct x11drv_mode_info *X11DRV_Settings_SetHandlers(const char *name,
                                                     int (*pNewGCM)(void),
                                                     LONG (*pNewSCM)(int),
                                                     unsigned int nmodes,
                                                     int reserve_depths)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
{
    handler_name = name;
    pGetCurrentMode = pNewGCM;
    pSetCurrentMode = pNewSCM;
    TRACE("Resolution settings now handled by: %s\n", name);
    if (reserve_depths)
        /* leave room for other depths */
        dd_max_modes = (3+1)*(nmodes);
    else 
        dd_max_modes = nmodes;

    if (dd_modes) 
    {
        TRACE("Destroying old display modes array\n");
        HeapFree(GetProcessHeap(), 0, dd_modes);
    }
77
    dd_modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dd_modes) * dd_max_modes);
78 79 80 81 82 83 84 85
    dd_mode_count = 0;
    TRACE("Initialized new display modes array\n");
    return dd_modes;
}

/* Add one mode to the master list */
void X11DRV_Settings_AddOneMode(unsigned int width, unsigned int height, unsigned int bpp, unsigned int freq)
{
86
    struct x11drv_mode_info *info = &dd_modes[dd_mode_count];
87
    DWORD dwBpp = screen_bpp;
88 89 90 91 92 93
    if (dd_mode_count >= dd_max_modes)
    {
        ERR("Maximum modes (%d) exceeded\n", dd_max_modes);
        return;
    }
    if (bpp == 0) bpp = dwBpp;
94 95 96 97
    info->width         = width;
    info->height        = height;
    info->refresh_rate  = freq;
    info->bpp           = bpp;
98 99 100 101 102 103 104 105 106 107
    TRACE("initialized mode %d: %dx%dx%d @%d Hz (%s)\n", 
          dd_mode_count, width, height, bpp, freq, handler_name);
    dd_mode_count++;
}

/* copy all the current modes using the other color depths */
void X11DRV_Settings_AddDepthModes(void)
{
    int i, j;
    int existing_modes = dd_mode_count;
108
    DWORD dwBpp = screen_bpp;
109
    const DWORD *depths = screen_bpp == 32 ? depths_32 : depths_24;
110

111 112 113 114 115 116
    for (j=0; j<3; j++)
    {
        if (depths[j] != dwBpp)
        {
            for (i=0; i < existing_modes; i++)
            {
117 118
                X11DRV_Settings_AddOneMode(dd_modes[i].width, dd_modes[i].height,
                                           depths[j], dd_modes[i].refresh_rate);
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
            }
        }
    }
}

/* return the number of modes that are initialized */
unsigned int X11DRV_Settings_GetModeCount(void)
{
    return dd_mode_count;
}

/***********************************************************************
 * Default handlers if resolution switching is not enabled
 *
 */
static int X11DRV_nores_GetCurrentMode(void)
{
    return 0;
}
138

139
static LONG X11DRV_nores_SetCurrentMode(int mode)
140
{
141 142
    if (mode == 0) return DISP_CHANGE_SUCCESSFUL;
    TRACE("Ignoring mode change request mode=%d\n", mode);
143
    return DISP_CHANGE_FAILED;
144
}
145

146 147 148
/* default handler only gets the current X desktop resolution */
void X11DRV_Settings_Init(void)
{
149
    RECT primary = get_primary_monitor_rect();
150 151 152 153
    X11DRV_Settings_SetHandlers("NoRes", 
                                X11DRV_nores_GetCurrentMode, 
                                X11DRV_nores_SetCurrentMode, 
                                1, 0);
154
    X11DRV_Settings_AddOneMode( primary.right - primary.left, primary.bottom - primary.top, 0, 60);
155 156
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
static BOOL get_display_device_reg_key(char *key, unsigned len)
{
    static const char display_device_guid_prop[] = "__wine_display_device_guid";
    static const char video_path[] = "System\\CurrentControlSet\\Control\\Video\\{";
    static const char display0[] = "}\\0000";
    ATOM guid_atom;

    assert(len >= sizeof(video_path) + sizeof(display0) + 40);

    guid_atom = HandleToULong(GetPropA(GetDesktopWindow(), display_device_guid_prop));
    if (!guid_atom) return FALSE;

    memcpy(key, video_path, sizeof(video_path));

    if (!GlobalGetAtomNameA(guid_atom, key + strlen(key), 40))
        return FALSE;

    strcat(key, display0);

    TRACE("display device key %s\n", wine_dbgstr_a(key));
    return TRUE;
}
179 180 181

static BOOL read_registry_settings(DEVMODEW *dm)
{
182
    char wine_x11_reg_key[128];
183 184 185 186
    HKEY hkey;
    DWORD type, size;
    BOOL ret = TRUE;

187 188
    dm->dmFields = 0;

189 190 191 192
    if (!get_display_device_reg_key(wine_x11_reg_key, sizeof(wine_x11_reg_key)))
        return FALSE;

    if (RegOpenKeyExA(HKEY_CURRENT_CONFIG, wine_x11_reg_key, 0, KEY_READ, &hkey))
193 194 195
        return FALSE;

#define query_value(name, data) \
196
    size = sizeof(DWORD); \
197 198 199 200 201
    if (RegQueryValueExA(hkey, name, 0, &type, (LPBYTE)(data), &size) || \
        type != REG_DWORD || size != sizeof(DWORD)) \
        ret = FALSE

    query_value("DefaultSettings.BitsPerPel", &dm->dmBitsPerPel);
202
    dm->dmFields |= DM_BITSPERPEL;
203
    query_value("DefaultSettings.XResolution", &dm->dmPelsWidth);
204
    dm->dmFields |= DM_PELSWIDTH;
205
    query_value("DefaultSettings.YResolution", &dm->dmPelsHeight);
206
    dm->dmFields |= DM_PELSHEIGHT;
207
    query_value("DefaultSettings.VRefresh", &dm->dmDisplayFrequency);
208
    dm->dmFields |= DM_DISPLAYFREQUENCY;
209
    query_value("DefaultSettings.Flags", &dm->u2.dmDisplayFlags);
210
    dm->dmFields |= DM_DISPLAYFLAGS;
211 212 213 214
    query_value("DefaultSettings.XPanning", &dm->u1.s2.dmPosition.x);
    query_value("DefaultSettings.YPanning", &dm->u1.s2.dmPosition.y);
    query_value("DefaultSettings.Orientation", &dm->u1.s2.dmDisplayOrientation);
    query_value("DefaultSettings.FixedOutput", &dm->u1.s2.dmDisplayFixedOutput);
215 216 217 218 219 220 221 222 223

#undef query_value

    RegCloseKey(hkey);
    return ret;
}

static BOOL write_registry_settings(const DEVMODEW *dm)
{
224
    char wine_x11_reg_key[128];
225 226 227
    HKEY hkey;
    BOOL ret = TRUE;

228 229 230 231 232
    if (!get_display_device_reg_key(wine_x11_reg_key, sizeof(wine_x11_reg_key)))
        return FALSE;

    if (RegCreateKeyExA(HKEY_CURRENT_CONFIG, wine_x11_reg_key, 0, NULL,
                        REG_OPTION_VOLATILE, KEY_WRITE, NULL, &hkey, NULL))
233 234 235
        return FALSE;

#define set_value(name, data) \
236
    if (RegSetValueExA(hkey, name, 0, REG_DWORD, (const BYTE*)(data), sizeof(DWORD))) \
237 238 239 240 241 242
        ret = FALSE

    set_value("DefaultSettings.BitsPerPel", &dm->dmBitsPerPel);
    set_value("DefaultSettings.XResolution", &dm->dmPelsWidth);
    set_value("DefaultSettings.YResolution", &dm->dmPelsHeight);
    set_value("DefaultSettings.VRefresh", &dm->dmDisplayFrequency);
243 244 245 246 247
    set_value("DefaultSettings.Flags", &dm->u2.dmDisplayFlags);
    set_value("DefaultSettings.XPanning", &dm->u1.s2.dmPosition.x);
    set_value("DefaultSettings.YPanning", &dm->u1.s2.dmPosition.y);
    set_value("DefaultSettings.Orientation", &dm->u1.s2.dmDisplayOrientation);
    set_value("DefaultSettings.FixedOutput", &dm->u1.s2.dmDisplayFixedOutput);
248 249 250 251 252 253 254

#undef set_value

    RegCloseKey(hkey);
    return ret;
}

255
/***********************************************************************
256
 *		EnumDisplaySettingsEx  (X11DRV.@)
257 258
 *
 */
259
BOOL CDECL X11DRV_EnumDisplaySettingsEx( LPCWSTR name, DWORD n, LPDEVMODEW devmode, DWORD flags)
260
{
261 262 263
    static const WCHAR dev_name[CCHDEVICENAME] =
        { 'W','i','n','e',' ','X','1','1',' ','d','r','i','v','e','r',0 };

264 265 266
    devmode->dmSize = FIELD_OFFSET(DEVMODEW, dmICMMethod);
    devmode->dmSpecVersion = DM_SPECVERSION;
    devmode->dmDriverVersion = DM_SPECVERSION;
267 268
    memcpy(devmode->dmDeviceName, dev_name, sizeof(dev_name));
    devmode->dmDriverExtra = 0;
269
    devmode->u2.dmDisplayFlags = 0;
270
    devmode->dmDisplayFrequency = 0;
271 272 273 274
    devmode->u1.s2.dmPosition.x = 0;
    devmode->u1.s2.dmPosition.y = 0;
    devmode->u1.s2.dmDisplayOrientation = 0;
    devmode->u1.s2.dmDisplayFixedOutput = 0;
275

276
    if (n == ENUM_CURRENT_SETTINGS)
277
    {
278
        TRACE("mode %d (current) -- getting current mode (%s)\n", n, handler_name);
279 280
        n = pGetCurrentMode();
    }
281
    if (n == ENUM_REGISTRY_SETTINGS)
282
    {
283
        TRACE("mode %d (registry) -- getting default mode (%s)\n", n, handler_name);
284
        return read_registry_settings(devmode);
285 286 287
    }
    if (n < dd_mode_count)
    {
288 289 290 291
        devmode->dmPelsWidth = dd_modes[n].width;
        devmode->dmPelsHeight = dd_modes[n].height;
        devmode->dmBitsPerPel = dd_modes[n].bpp;
        devmode->dmDisplayFrequency = dd_modes[n].refresh_rate;
292 293
        devmode->dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL |
                            DM_DISPLAYFLAGS;
294 295 296
        if (devmode->dmDisplayFrequency)
        {
            devmode->dmFields |= DM_DISPLAYFREQUENCY;
297
            TRACE("mode %d -- %dx%dx%dbpp @%d Hz (%s)\n", n,
298 299 300 301 302
                  devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel,
                  devmode->dmDisplayFrequency, handler_name);
        }
        else
        {
303
            TRACE("mode %d -- %dx%dx%dbpp (%s)\n", n,
304 305 306 307 308
                  devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel, 
                  handler_name);
        }
        return TRUE;
    }
309
    TRACE("mode %d -- not present (%s)\n", n, handler_name);
310
    SetLastError(ERROR_NO_MORE_FILES);
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    return FALSE;
}

#define _X_FIELD(prefix, bits) if ((fields) & prefix##_##bits) {p+=sprintf(p, "%s%s", first ? "" : ",", #bits); first=FALSE;}
static const char * _CDS_flags(DWORD fields)
{
    BOOL first = TRUE;
    char buf[128];
    char *p = buf;
    _X_FIELD(CDS,UPDATEREGISTRY);_X_FIELD(CDS,TEST);_X_FIELD(CDS,FULLSCREEN);
    _X_FIELD(CDS,GLOBAL);_X_FIELD(CDS,SET_PRIMARY);_X_FIELD(CDS,RESET);
    _X_FIELD(CDS,SETRECT);_X_FIELD(CDS,NORESET);
    *p = 0;
    return wine_dbg_sprintf("%s", buf);
}
static const char * _DM_fields(DWORD fields)
{
    BOOL first = TRUE;
    char buf[128];
    char *p = buf;
    _X_FIELD(DM,BITSPERPEL);_X_FIELD(DM,PELSWIDTH);_X_FIELD(DM,PELSHEIGHT);
    _X_FIELD(DM,DISPLAYFLAGS);_X_FIELD(DM,DISPLAYFREQUENCY);_X_FIELD(DM,POSITION);
    *p = 0;
    return wine_dbg_sprintf("%s", buf);
}
#undef _X_FIELD

/***********************************************************************
339
 *		ChangeDisplaySettingsEx  (X11DRV.@)
340 341
 *
 */
342 343
LONG CDECL X11DRV_ChangeDisplaySettingsEx( LPCWSTR devname, LPDEVMODEW devmode,
                                           HWND hwnd, DWORD flags, LPVOID lpvoid )
344
{
345
    DWORD i, dwBpp = 0;
346
    DEVMODEW dm;
347
    BOOL def_mode = TRUE;
348
    char bpp_buffer[16], freq_buffer[18];
349

350
    TRACE("(%s,%p,%p,0x%08x,%p)\n",debugstr_w(devname),devmode,hwnd,flags,lpvoid);
351 352 353
    TRACE("flags=%s\n",_CDS_flags(flags));
    if (devmode)
    {
354 355 356 357
        /* this is the minimal dmSize that XP accepts */
        if (devmode->dmSize < FIELD_OFFSET(DEVMODEW, dmFields))
            return DISP_CHANGE_FAILED;

358
        TRACE("DM_fields=%s\n",_DM_fields(devmode->dmFields));
359
        TRACE("width=%d height=%d bpp=%d freq=%d (%s)\n",
360 361
              devmode->dmPelsWidth,devmode->dmPelsHeight,
              devmode->dmBitsPerPel,devmode->dmDisplayFrequency, handler_name);
362

363
        dwBpp = devmode->dmBitsPerPel;
364 365 366 367
        if (devmode->dmFields & DM_BITSPERPEL) def_mode &= !dwBpp;
        if (devmode->dmFields & DM_PELSWIDTH)  def_mode &= !devmode->dmPelsWidth;
        if (devmode->dmFields & DM_PELSHEIGHT) def_mode &= !devmode->dmPelsHeight;
        if (devmode->dmFields & DM_DISPLAYFREQUENCY) def_mode &= !devmode->dmDisplayFrequency;
368
    }
369

370
    if (def_mode || !dwBpp)
371
    {
372
        if (!X11DRV_EnumDisplaySettingsEx(devname, ENUM_REGISTRY_SETTINGS, &dm, 0))
373 374 375 376
        {
            ERR("Default mode not found!\n");
            return DISP_CHANGE_BADMODE;
        }
377 378 379 380 381 382
        if (def_mode)
        {
            TRACE("Return to original display mode (%s)\n", handler_name);
            devmode = &dm;
        }
        dwBpp = dm.dmBitsPerPel;
383
    }
384 385

    if ((devmode->dmFields & (DM_PELSWIDTH | DM_PELSHEIGHT)) != (DM_PELSWIDTH | DM_PELSHEIGHT))
386 387
    {
        WARN("devmode doesn't specify the resolution: %04x\n", devmode->dmFields);
388
        return DISP_CHANGE_BADMODE;
389
    }
390 391 392 393 394

    for (i = 0; i < dd_mode_count; i++)
    {
        if (devmode->dmFields & DM_BITSPERPEL)
        {
395
            if (dwBpp != dd_modes[i].bpp)
396 397 398 399
                continue;
        }
        if (devmode->dmFields & DM_PELSWIDTH)
        {
400
            if (devmode->dmPelsWidth != dd_modes[i].width)
401 402 403 404
                continue;
        }
        if (devmode->dmFields & DM_PELSHEIGHT)
        {
405
            if (devmode->dmPelsHeight != dd_modes[i].height)
406 407
                continue;
        }
408
        if ((devmode->dmFields & DM_DISPLAYFREQUENCY) && (dd_modes[i].refresh_rate != 0) &&
409
            devmode->dmDisplayFrequency != 0)
410
        {
411
            if (devmode->dmDisplayFrequency != dd_modes[i].refresh_rate)
412 413 414
                continue;
        }
        /* we have a valid mode */
415
        TRACE("Requested display settings match mode %d (%s)\n", i, handler_name);
416 417 418 419 420

        if (flags & CDS_UPDATEREGISTRY)
            write_registry_settings(devmode);

        if (!(flags & (CDS_TEST | CDS_NORESET)))
421
            return pSetCurrentMode(i);
422

423 424 425
        return DISP_CHANGE_SUCCESSFUL;
    }

426 427 428 429 430 431 432 433 434
    /* no valid modes found, only print the fields we were trying to matching against */
    bpp_buffer[0] = freq_buffer[0] = 0;
    if (devmode->dmFields & DM_BITSPERPEL)
        sprintf(bpp_buffer, "bpp=%u ",  devmode->dmBitsPerPel);
    if ((devmode->dmFields & DM_DISPLAYFREQUENCY) && (devmode->dmDisplayFrequency != 0))
        sprintf(freq_buffer, "freq=%u ", devmode->dmDisplayFrequency);
    ERR("No matching mode found: width=%d height=%d %s%s(%s)\n",
        devmode->dmPelsWidth, devmode->dmPelsHeight, bpp_buffer, freq_buffer, handler_name);

435 436
    return DISP_CHANGE_BADMODE;
}