[ros-diffs] [hpoussin] 22206: Don't crash when no working display adapter is present

hpoussin at svn.reactos.org hpoussin at svn.reactos.org
Sun Jun 4 16:09:29 CEST 2006


Author: hpoussin
Date: Sun Jun  4 18:09:29 2006
New Revision: 22206

URL: http://svn.reactos.ru/svn/reactos?rev=22206&view=rev
Log:
Don't crash when no working display adapter is present

Modified:
    trunk/reactos/dll/cpl/desk/settings.c

Modified: trunk/reactos/dll/cpl/desk/settings.c
URL: http://svn.reactos.ru/svn/reactos/trunk/reactos/dll/cpl/desk/settings.c?rev=22206&r1=22205&r2=22206&view=diff
==============================================================================
--- trunk/reactos/dll/cpl/desk/settings.c (original)
+++ trunk/reactos/dll/cpl/desk/settings.c Sun Jun  4 18:09:29 2006
@@ -1,12 +1,11 @@
-/* $Id$
- *
+/*
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS Display Control Panel
  * FILE:            lib/cpl/desk/settings.c
  * PURPOSE:         Settings property page
- * 
+ *
  * PROGRAMMERS:     Trevor McCort (lycan359 at gmail.com)
- *                  Hervé Poussineau (poussine at freesurf.fr)
+ *                  Hervé Poussineau (hpoussin at reactos.org)
  */
 
 #include "desk.h"
@@ -54,14 +53,13 @@
 	TCHAR Buffer[64];
 	TCHAR Pixel[64];
 	DWORD index;
-	
+
 	LoadString(hApplet, IDS_PIXEL, Pixel, sizeof(Pixel) / sizeof(TCHAR));
 	_stprintf(Buffer, Pixel, CurrentDisplayDevice->CurrentSettings->dmPelsWidth, CurrentDisplayDevice->CurrentSettings->dmPelsHeight, Pixel);
 	SendDlgItemMessage(hwndDlg, IDC_SETTINGS_RESOLUTION_TEXT, WM_SETTEXT, 0, (LPARAM)Buffer);
-		
 
 	for (index = 0; index < CurrentDisplayDevice->ResolutionsCount; index++)
-	{		
+	{
 
 		if (CurrentDisplayDevice->Resolutions[index].dmPelsWidth == CurrentDisplayDevice->CurrentSettings->dmPelsWidth &&
 		    CurrentDisplayDevice->Resolutions[index].dmPelsHeight == CurrentDisplayDevice->CurrentSettings->dmPelsHeight)
@@ -70,7 +68,7 @@
 			break;
 		}
 	}
-	if (LoadString(hApplet, (2900 + CurrentDisplayDevice->CurrentSettings->dmBitsPerPel), Buffer, sizeof(Buffer) / sizeof(TCHAR))) 
+	if (LoadString(hApplet, (2900 + CurrentDisplayDevice->CurrentSettings->dmBitsPerPel), Buffer, sizeof(Buffer) / sizeof(TCHAR)))
 		SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_SELECTSTRING, -1, (LPARAM)Buffer);
 }
 
@@ -85,7 +83,7 @@
 	HDC hDC;
 	PSETTINGS_ENTRY Current;
 	DWORD bpp, xres, yres, checkbpp;
-	
+
 	/* Get current settings */
 	*CurrentSettings = NULL;
 	hDC = CreateIC(NULL, DeviceName, NULL, NULL);
@@ -94,23 +92,22 @@
 	xres = GetDeviceCaps(hDC, HORZRES);
 	yres = GetDeviceCaps(hDC, VERTRES);
 	DeleteDC(hDC);
-	
+
 	/* List all settings */
 	devmode.dmSize = (WORD)sizeof(DEVMODE);
 	devmode.dmDriverExtra = 0;
 	while (EnumDisplaySettingsEx(DeviceName, iMode, &devmode, dwFlags))
 	{
-	
 		if (devmode.dmBitsPerPel==8 || devmode.dmBitsPerPel==16 || devmode.dmBitsPerPel==24 || devmode.dmBitsPerPel==32) checkbpp=1;
 		else checkbpp=0;
 
 		if (devmode.dmPelsWidth < 640 ||
 			devmode.dmPelsHeight < 480 || checkbpp == 0)
 		{
- 			iMode++;
- 			continue;
-		}
-		
+			iMode++;
+			continue;
+		}
+
 		Current = HeapAlloc(GetProcessHeap(), 0, sizeof(SETTINGS_ENTRY));
 		if (Current != NULL)
 		{
@@ -123,7 +120,7 @@
 			while (Next != NULL && (
 			       Next->dmPelsHeight < Current->dmPelsHeight ||
 			       (Next->dmPelsHeight == Current->dmPelsHeight && Next->dmBitsPerPel < Current->dmBitsPerPel) ||
-			       (Next->dmPelsHeight == Current->dmPelsHeight && 
+			       (Next->dmPelsHeight == Current->dmPelsHeight &&
 			        Next->dmBitsPerPel == Current->dmBitsPerPel &&
 			        Next->dmPelsWidth < Current->dmPelsWidth)))
 			{
@@ -146,12 +143,12 @@
 		}
 		iMode++;
 	}
-	
+
 	*pSettingsCount = NbSettings;
 	return Settings;
 }
 
-static VOID
+static BOOL
 AddDisplayDevice(IN LPTSTR Description, IN LPTSTR DeviceName)
 {
 	PDISPLAY_DEVICE_ENTRY newEntry = NULL;
@@ -162,17 +159,18 @@
 	PSETTINGS_ENTRY Current;
 	DWORD ResolutionsCount = 1;
 	DWORD i;
-	
+
 	newEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(DISPLAY_DEVICE_ENTRY));
+	memset(newEntry, 0, sizeof(DISPLAY_DEVICE_ENTRY));
 	if (!newEntry) goto ByeBye;
-	
+
 	newEntry->Settings = GetPossibleSettings(DeviceName, &newEntry->SettingsCount, &newEntry->CurrentSettings);
 	if (!newEntry->Settings) goto ByeBye;
-	
+
 	newEntry->InitialSettings.dmPelsWidth = newEntry->CurrentSettings->dmPelsWidth;
 	newEntry->InitialSettings.dmPelsHeight = newEntry->CurrentSettings->dmPelsHeight;
 	newEntry->InitialSettings.dmBitsPerPel = newEntry->CurrentSettings->dmBitsPerPel;
-	
+
 	/* Count different resolutions */
 	for (Current = newEntry->Settings; Current != NULL; Current = Current->Flink)
 		if (Current->Flink != NULL &&
@@ -190,23 +188,23 @@
 			newEntry->Resolutions[i].dmPelsHeight = Current->dmPelsHeight;
 			i++;
 		}
-	
+
 	descriptionSize = (_tcslen(Description) + 1) * sizeof(TCHAR);
 	description = HeapAlloc(GetProcessHeap(), 0, descriptionSize);
 	if (!description) goto ByeBye;
-	
+
 	nameSize = (_tcslen(DeviceName) + 1) * sizeof(TCHAR);
 	name = HeapAlloc(GetProcessHeap(), 0, nameSize);
 	if (!name) goto ByeBye;
-	
+
 	memcpy(description, Description, descriptionSize);
 	memcpy(name, DeviceName, nameSize);
 	newEntry->DeviceDescription = description;
 	newEntry->DeviceName = name;
 	newEntry->Flink = DisplayDeviceList;
 	DisplayDeviceList = newEntry;
-	return;
-	
+	return TRUE;
+
 ByeBye:
 	if (newEntry != NULL)
 	{
@@ -228,6 +226,7 @@
 		HeapFree(GetProcessHeap(), 0, description);
 	if (name != NULL)
 		HeapFree(GetProcessHeap(), 0, name);
+	return FALSE;
 }
 
 static VOID
@@ -235,9 +234,9 @@
 {
 	PSETTINGS_ENTRY Current;
 	DWORD index;
-	
+
 	CurrentDisplayDevice = pDeviceEntry; /* Update global variable */
-	
+
 	/* Fill color depths combo box */
 	SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_RESETCONTENT, 0, 0);
 	for (Current = pDeviceEntry->Settings; Current != NULL; Current = Current->Flink)
@@ -253,11 +252,11 @@
 			}
 		}
 	}
-	
+
 	/* Fill resolutions slider */
 	SendDlgItemMessage(hwndDlg, IDC_SETTINGS_RESOLUTION, TBM_CLEARTICS, TRUE, 0);
 	SendDlgItemMessage(hwndDlg, IDC_SETTINGS_RESOLUTION, TBM_SETRANGE, TRUE, MAKELONG(0, pDeviceEntry->ResolutionsCount - 1));
-	
+
 	UpdateDisplay(hwndDlg);
 }
 
@@ -267,16 +266,16 @@
 	DWORD Result = 0;
 	DWORD iDevNum = 0;
 	DISPLAY_DEVICE displayDevice;
-    BITMAP bitmap;
-	
+	BITMAP bitmap;
+
 	/* Get video cards list */
 	displayDevice.cb = (DWORD)sizeof(DISPLAY_DEVICE);
 	while (EnumDisplayDevices(NULL, iDevNum, &displayDevice, 0))
 	{
 		if ((displayDevice.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) != 0)
 		{
-			AddDisplayDevice(displayDevice.DeviceString, displayDevice.DeviceName);
-			Result++;
+			if (AddDisplayDevice(displayDevice.DeviceString, displayDevice.DeviceName))
+				Result++;
 		}
 		iDevNum++;
 	}
@@ -300,14 +299,14 @@
 		/* FIXME: choose selected adapter being the primary one */
 	}
 
-    hBitmap = LoadImage(hApplet, MAKEINTRESOURCE(IDC_MONITOR), IMAGE_BITMAP, 0, 0, LR_LOADTRANSPARENT);
-    if (hBitmap != NULL)
-    {
-        GetObject(hBitmap, sizeof(BITMAP), &bitmap);
-
-        cxSource = bitmap.bmWidth;
-        cySource = bitmap.bmHeight;
-    }
+	hBitmap = LoadImage(hApplet, MAKEINTRESOURCE(IDC_MONITOR), IMAGE_BITMAP, 0, 0, LR_LOADTRANSPARENT);
+	if (hBitmap != NULL)
+	{
+		GetObject(hBitmap, sizeof(BITMAP), &bitmap);
+
+		cxSource = bitmap.bmWidth;
+		cySource = bitmap.bmHeight;
+	}
 }
 
 static VOID
@@ -321,11 +320,11 @@
 	DWORD dmNewBitsPerPel;
 	DWORD index;
 	TCHAR Buffer[64];
-	
+
 	SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, WM_GETTEXT, (WPARAM)(sizeof(Buffer) / sizeof(TCHAR)), (LPARAM)Buffer);
 	index = SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)Buffer);
 	dmNewBitsPerPel = SendDlgItemMessage(hwndDlg, IDC_SETTINGS_BPP, CB_GETITEMDATA, index, 0);
-	
+
 	/* find if new parameters are valid */
 	Current = CurrentDisplayDevice->CurrentSettings;
 	if (dmNewBitsPerPel == Current->dmBitsPerPel)
@@ -333,9 +332,9 @@
 		/* no change */
 		return;
 	}
-	
+
 	PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
-	
+
 	if (dmNewBitsPerPel < Current->dmBitsPerPel)
 	{
 		Current = Current->Blink;
@@ -368,7 +367,7 @@
 			Current = Current->Flink;
 		}
 	}
-	
+
 	/* search smaller resolution compatible with current color depth */
 	Current = CurrentDisplayDevice->CurrentSettings->Blink;
 	while (Current != NULL)
@@ -381,7 +380,7 @@
 		}
 		Current = Current->Blink;
 	}
-	
+
 	/* search bigger resolution compatible with current color depth */
 	Current = CurrentDisplayDevice->CurrentSettings->Flink;
 	while (Current != NULL)
@@ -394,7 +393,7 @@
 		}
 		Current = Current->Flink;
 	}
-	
+
 	/* we shouldn't go there */
 }
 
@@ -408,7 +407,7 @@
 	PSETTINGS_ENTRY Current;
 	DWORD dmNewPelsHeight = CurrentDisplayDevice->Resolutions[NewPosition].dmPelsHeight;
 	DWORD dmNewPelsWidth = CurrentDisplayDevice->Resolutions[NewPosition].dmPelsWidth;
-	
+
 	/* find if new parameters are valid */
 	Current = CurrentDisplayDevice->CurrentSettings;
 	if (dmNewPelsHeight == Current->dmPelsHeight && dmNewPelsWidth == Current->dmPelsWidth)
@@ -416,9 +415,9 @@
 		/* no change */
 		return;
 	}
-	
+
 	PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
-	
+
 	if (dmNewPelsHeight < Current->dmPelsHeight)
 	{
 		Current = Current->Blink;
@@ -451,7 +450,7 @@
 			Current = Current->Flink;
 		}
 	}
-	
+
 	/* search bigger color depth compatible with current resolution */
 	Current = CurrentDisplayDevice->CurrentSettings->Flink;
 	while (Current != NULL)
@@ -464,7 +463,7 @@
 		}
 		Current = Current->Flink;
 	}
-	
+
 	/* search smaller color depth compatible with current resolution */
 	Current = CurrentDisplayDevice->CurrentSettings->Blink;
 	while (Current != NULL)
@@ -477,7 +476,7 @@
 		}
 		Current = Current->Blink;
 	}
-	
+
 	/* we shouldn't go there */
 }
 
@@ -500,7 +499,7 @@
 		{
 			DWORD controlId = LOWORD(wParam);
 			DWORD command   = HIWORD(wParam);
-		
+
 			if (controlId == IDC_SETTINGS_ADVANCED && command == BN_CLICKED)
 				OnAdvancedButton();
 			else if (controlId == IDC_SETTINGS_BPP && command == CBN_SELCHANGE)
@@ -572,22 +571,23 @@
 			break;
 		}
 
-        case WM_PAINT:
-            {
-                PAINTSTRUCT ps;
-                HDC hdc, hdcMem;
-       
-                hdc = BeginPaint(hwndDlg, &ps);
- 
-                hdcMem = CreateCompatibleDC(hdc);
-                SelectObject(hdcMem, hBitmap);
-
-                TransparentBlt(hdc, 98, 0, cxSource, cySource, hdcMem, 0, 0, cxSource, cySource, 0xFF80FF);
-
-                DeleteDC(hdcMem);
-                EndPaint(hwndDlg, &ps);
-
-            } break;
+		case WM_PAINT:
+		{
+			PAINTSTRUCT ps;
+			HDC hdc, hdcMem;
+
+			hdc = BeginPaint(hwndDlg, &ps);
+
+			hdcMem = CreateCompatibleDC(hdc);
+			SelectObject(hdcMem, hBitmap);
+
+			TransparentBlt(hdc, 98, 0, cxSource, cySource, hdcMem, 0, 0, cxSource, cySource, 0xFF80FF);
+
+			DeleteDC(hdcMem);
+			EndPaint(hwndDlg, &ps);
+
+			break;
+		}
 
 		case WM_DESTROY:
 		{
@@ -606,7 +606,7 @@
 				Current = Next;
 			}
 
-            DeleteObject(hBitmap);
+			DeleteObject(hBitmap);
 		}
 	}
 	return FALSE;




More information about the Ros-diffs mailing list