Sweden-Number/dlls/winex11.drv/settings.c

461 lines
16 KiB
C

/*
* 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
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
#include "x11drv.h"
#include "windef.h"
#include "winreg.h"
#include "wingdi.h"
#include "wine/debug.h"
#include "wine/unicode.h"
WINE_DEFAULT_DEBUG_CHANNEL(x11settings);
static struct x11drv_mode_info *dd_modes = NULL;
static unsigned int dd_mode_count = 0;
static unsigned int dd_max_modes = 0;
/* 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};
/* pointers to functions that actually do the hard stuff */
static int (*pGetCurrentMode)(void);
static LONG (*pSetCurrentMode)(int mode);
static const char *handler_name;
/*
* Set the handlers for resolution changing functions
* and initialize the master list of modes
*/
struct x11drv_mode_info *X11DRV_Settings_SetHandlers(const char *name,
int (*pNewGCM)(void),
LONG (*pNewSCM)(int),
unsigned int nmodes,
int reserve_depths)
{
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);
}
dd_modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dd_modes) * dd_max_modes);
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)
{
struct x11drv_mode_info *info = &dd_modes[dd_mode_count];
DWORD dwBpp = screen_bpp;
if (dd_mode_count >= dd_max_modes)
{
ERR("Maximum modes (%d) exceeded\n", dd_max_modes);
return;
}
if (bpp == 0) bpp = dwBpp;
info->width = width;
info->height = height;
info->refresh_rate = freq;
info->bpp = bpp;
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;
DWORD dwBpp = screen_bpp;
const DWORD *depths = screen_bpp == 32 ? depths_32 : depths_24;
for (j=0; j<3; j++)
{
if (depths[j] != dwBpp)
{
for (i=0; i < existing_modes; i++)
{
X11DRV_Settings_AddOneMode(dd_modes[i].width, dd_modes[i].height,
depths[j], dd_modes[i].refresh_rate);
}
}
}
}
/* 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;
}
static LONG X11DRV_nores_SetCurrentMode(int mode)
{
if (mode == 0) return DISP_CHANGE_SUCCESSFUL;
TRACE("Ignoring mode change request mode=%d\n", mode);
return DISP_CHANGE_FAILED;
}
/* default handler only gets the current X desktop resolution */
void X11DRV_Settings_Init(void)
{
RECT primary = get_host_primary_monitor_rect();
X11DRV_Settings_SetHandlers("NoRes",
X11DRV_nores_GetCurrentMode,
X11DRV_nores_SetCurrentMode,
1, 0);
X11DRV_Settings_AddOneMode( primary.right - primary.left, primary.bottom - primary.top, 0, 60);
}
static BOOL get_display_device_reg_key(const WCHAR *device_name, WCHAR *key, unsigned len)
{
static const WCHAR display[] = {'\\','\\','.','\\','D','I','S','P','L','A','Y'};
static const WCHAR video_value_fmt[] = {'\\','D','e','v','i','c','e','\\',
'V','i','d','e','o','%','d',0};
static const WCHAR video_key[] = {'H','A','R','D','W','A','R','E','\\',
'D','E','V','I','C','E','M','A','P','\\',
'V','I','D','E','O','\\',0};
WCHAR value_name[MAX_PATH], buffer[MAX_PATH], *end_ptr;
DWORD adapter_index, size;
/* Device name has to be \\.\DISPLAY%d */
if (strncmpiW(device_name, display, ARRAY_SIZE(display)))
return FALSE;
/* Parse \\.\DISPLAY* */
adapter_index = strtolW(device_name + ARRAY_SIZE(display), &end_ptr, 10) - 1;
if (*end_ptr)
return FALSE;
/* Open \Device\Video* in HKLM\HARDWARE\DEVICEMAP\VIDEO\ */
sprintfW(value_name, video_value_fmt, adapter_index);
size = sizeof(buffer);
if (RegGetValueW(HKEY_LOCAL_MACHINE, video_key, value_name, RRF_RT_REG_SZ, NULL, buffer, &size))
return FALSE;
if (len < lstrlenW(buffer + 18) + 1)
return FALSE;
/* Skip \Registry\Machine\ prefix */
lstrcpyW(key, buffer + 18);
TRACE("display device %s registry settings key %s.\n", wine_dbgstr_w(device_name), wine_dbgstr_w(key));
return TRUE;
}
static BOOL read_registry_settings(const WCHAR *device_name, DEVMODEW *dm)
{
WCHAR wine_x11_reg_key[MAX_PATH];
HANDLE mutex;
HKEY hkey;
DWORD type, size;
BOOL ret = TRUE;
dm->dmFields = 0;
mutex = get_display_device_init_mutex();
if (!get_display_device_reg_key(device_name, wine_x11_reg_key, ARRAY_SIZE(wine_x11_reg_key)))
{
release_display_device_init_mutex(mutex);
return FALSE;
}
if (RegOpenKeyExW(HKEY_CURRENT_CONFIG, wine_x11_reg_key, 0, KEY_READ, &hkey))
{
release_display_device_init_mutex(mutex);
return FALSE;
}
#define query_value(name, data) \
size = sizeof(DWORD); \
if (RegQueryValueExA(hkey, name, 0, &type, (LPBYTE)(data), &size) || \
type != REG_DWORD || size != sizeof(DWORD)) \
ret = FALSE
query_value("DefaultSettings.BitsPerPel", &dm->dmBitsPerPel);
dm->dmFields |= DM_BITSPERPEL;
query_value("DefaultSettings.XResolution", &dm->dmPelsWidth);
dm->dmFields |= DM_PELSWIDTH;
query_value("DefaultSettings.YResolution", &dm->dmPelsHeight);
dm->dmFields |= DM_PELSHEIGHT;
query_value("DefaultSettings.VRefresh", &dm->dmDisplayFrequency);
dm->dmFields |= DM_DISPLAYFREQUENCY;
query_value("DefaultSettings.Flags", &dm->u2.dmDisplayFlags);
dm->dmFields |= DM_DISPLAYFLAGS;
query_value("DefaultSettings.XPanning", &dm->u1.s2.dmPosition.x);
query_value("DefaultSettings.YPanning", &dm->u1.s2.dmPosition.y);
dm->dmFields |= DM_POSITION;
query_value("DefaultSettings.Orientation", &dm->u1.s2.dmDisplayOrientation);
dm->dmFields |= DM_DISPLAYORIENTATION;
query_value("DefaultSettings.FixedOutput", &dm->u1.s2.dmDisplayFixedOutput);
#undef query_value
RegCloseKey(hkey);
release_display_device_init_mutex(mutex);
return ret;
}
static BOOL write_registry_settings(const WCHAR *device_name, const DEVMODEW *dm)
{
WCHAR wine_x11_reg_key[MAX_PATH];
HANDLE mutex;
HKEY hkey;
BOOL ret = TRUE;
mutex = get_display_device_init_mutex();
if (!get_display_device_reg_key(device_name, wine_x11_reg_key, ARRAY_SIZE(wine_x11_reg_key)))
{
release_display_device_init_mutex(mutex);
return FALSE;
}
if (RegCreateKeyExW(HKEY_CURRENT_CONFIG, wine_x11_reg_key, 0, NULL,
REG_OPTION_VOLATILE, KEY_WRITE, NULL, &hkey, NULL))
{
release_display_device_init_mutex(mutex);
return FALSE;
}
#define set_value(name, data) \
if (RegSetValueExA(hkey, name, 0, REG_DWORD, (const BYTE*)(data), sizeof(DWORD))) \
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);
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);
#undef set_value
RegCloseKey(hkey);
release_display_device_init_mutex(mutex);
return ret;
}
BOOL get_primary_adapter(WCHAR *name)
{
DISPLAY_DEVICEW dd;
DWORD i;
dd.cb = sizeof(dd);
for (i = 0; EnumDisplayDevicesW(NULL, i, &dd, 0); ++i)
{
if (dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
{
lstrcpyW(name, dd.DeviceName);
return TRUE;
}
}
return FALSE;
}
/***********************************************************************
* EnumDisplaySettingsEx (X11DRV.@)
*
*/
BOOL CDECL X11DRV_EnumDisplaySettingsEx( LPCWSTR name, DWORD n, LPDEVMODEW devmode, DWORD flags)
{
static const WCHAR dev_name[CCHDEVICENAME] =
{ 'W','i','n','e',' ','X','1','1',' ','d','r','i','v','e','r',0 };
devmode->dmSize = FIELD_OFFSET(DEVMODEW, dmICMMethod);
devmode->dmSpecVersion = DM_SPECVERSION;
devmode->dmDriverVersion = DM_SPECVERSION;
memcpy(devmode->dmDeviceName, dev_name, sizeof(dev_name));
devmode->dmDriverExtra = 0;
devmode->u2.dmDisplayFlags = 0;
devmode->dmDisplayFrequency = 0;
devmode->u1.s2.dmPosition.x = 0;
devmode->u1.s2.dmPosition.y = 0;
devmode->u1.s2.dmDisplayOrientation = 0;
devmode->u1.s2.dmDisplayFixedOutput = 0;
if (n == ENUM_CURRENT_SETTINGS)
{
TRACE("mode %d (current) -- getting current mode (%s)\n", n, handler_name);
n = pGetCurrentMode();
}
if (n == ENUM_REGISTRY_SETTINGS)
{
TRACE("mode %d (registry) -- getting default mode (%s)\n", n, handler_name);
return read_registry_settings(name, devmode);
}
if (n < dd_mode_count)
{
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;
devmode->dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL |
DM_DISPLAYFLAGS;
if (devmode->dmDisplayFrequency)
{
devmode->dmFields |= DM_DISPLAYFREQUENCY;
TRACE("mode %d -- %dx%dx%dbpp @%d Hz (%s)\n", n,
devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel,
devmode->dmDisplayFrequency, handler_name);
}
else
{
TRACE("mode %d -- %dx%dx%dbpp (%s)\n", n,
devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel,
handler_name);
}
return TRUE;
}
TRACE("mode %d -- not present (%s)\n", n, handler_name);
SetLastError(ERROR_NO_MORE_FILES);
return FALSE;
}
BOOL is_detached_mode(const DEVMODEW *mode)
{
return mode->dmFields & DM_POSITION &&
mode->dmFields & DM_PELSWIDTH &&
mode->dmFields & DM_PELSHEIGHT &&
mode->dmPelsWidth == 0 &&
mode->dmPelsHeight == 0;
}
/***********************************************************************
* ChangeDisplaySettingsEx (X11DRV.@)
*
*/
LONG CDECL X11DRV_ChangeDisplaySettingsEx( LPCWSTR devname, LPDEVMODEW devmode,
HWND hwnd, DWORD flags, LPVOID lpvoid )
{
WCHAR primary_adapter[CCHDEVICENAME];
char bpp_buffer[16], freq_buffer[18];
DEVMODEW default_mode;
DWORD i;
if (!get_primary_adapter(primary_adapter))
return DISP_CHANGE_FAILED;
if (!devname && !devmode)
{
default_mode.dmSize = sizeof(default_mode);
if (!EnumDisplaySettingsExW(primary_adapter, ENUM_REGISTRY_SETTINGS, &default_mode, 0))
{
ERR("Default mode not found for %s!\n", wine_dbgstr_w(primary_adapter));
return DISP_CHANGE_BADMODE;
}
devname = primary_adapter;
devmode = &default_mode;
}
if (is_detached_mode(devmode))
{
FIXME("Detaching adapters is currently unsupported.\n");
return DISP_CHANGE_SUCCESSFUL;
}
for (i = 0; i < dd_mode_count; i++)
{
if (devmode->dmFields & DM_BITSPERPEL)
{
if (devmode->dmBitsPerPel != dd_modes[i].bpp)
continue;
}
if (devmode->dmFields & DM_PELSWIDTH)
{
if (devmode->dmPelsWidth != dd_modes[i].width)
continue;
}
if (devmode->dmFields & DM_PELSHEIGHT)
{
if (devmode->dmPelsHeight != dd_modes[i].height)
continue;
}
if ((devmode->dmFields & DM_DISPLAYFREQUENCY) && (dd_modes[i].refresh_rate != 0) &&
devmode->dmDisplayFrequency != 0 && devmode->dmDisplayFrequency != 1)
{
if (devmode->dmDisplayFrequency != dd_modes[i].refresh_rate)
continue;
}
/* we have a valid mode */
TRACE("Requested display settings match mode %d (%s)\n", i, handler_name);
if (flags & CDS_UPDATEREGISTRY && !write_registry_settings(devname, devmode))
{
ERR("Failed to write %s display settings to registry.\n", wine_dbgstr_w(devname));
return DISP_CHANGE_NOTUPDATED;
}
if (lstrcmpiW(primary_adapter, devname))
{
FIXME("Changing non-primary adapter %s settings is currently unsupported.\n",
wine_dbgstr_w(devname));
return DISP_CHANGE_SUCCESSFUL;
}
if (!(flags & (CDS_TEST | CDS_NORESET)))
return pSetCurrentMode(i);
return DISP_CHANGE_SUCCESSFUL;
}
/* 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);
return DISP_CHANGE_BADMODE;
}