Don't crash when no working display adapter is present
authorHervé Poussineau <hpoussin@reactos.org>
Sun, 4 Jun 2006 14:09:29 +0000 (14:09 +0000)
committerHervé Poussineau <hpoussin@reactos.org>
Sun, 4 Jun 2006 14:09:29 +0000 (14:09 +0000)
svn path=/trunk/; revision=22206

reactos/dll/cpl/desk/settings.c

index 3ece291..f7a38d8 100644 (file)
@@ -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@gmail.com)
- *                  Hervé Poussineau (poussine@freesurf.fr)
+ *                  Hervé Poussineau (hpoussin@reactos.org)
  */
 
 #include "desk.h"
@@ -54,14 +53,13 @@ UpdateDisplay(IN HWND hwndDlg)
        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 @@ UpdateDisplay(IN HWND hwndDlg)
                        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 @@ GetPossibleSettings(IN LPTSTR DeviceName, OUT DWORD* pSettingsCount, OUT PSETTIN
        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 @@ GetPossibleSettings(IN LPTSTR DeviceName, OUT DWORD* pSettingsCount, OUT PSETTIN
        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 @@ GetPossibleSettings(IN LPTSTR DeviceName, OUT DWORD* pSettingsCount, OUT PSETTIN
                        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 @@ GetPossibleSettings(IN LPTSTR DeviceName, OUT DWORD* pSettingsCount, OUT PSETTIN
                }
                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 @@ AddDisplayDevice(IN LPTSTR Description, IN LPTSTR DeviceName)
        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 @@ AddDisplayDevice(IN LPTSTR Description, IN LPTSTR DeviceName)
                        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 @@ ByeBye:
                HeapFree(GetProcessHeap(), 0, description);
        if (name != NULL)
                HeapFree(GetProcessHeap(), 0, name);
+       return FALSE;
 }
 
 static VOID
@@ -235,9 +234,9 @@ OnDisplayDeviceChanged(IN HWND hwndDlg, IN PDISPLAY_DEVICE_ENTRY pDeviceEntry)
 {
        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 @@ OnDisplayDeviceChanged(IN HWND hwndDlg, IN PDISPLAY_DEVICE_ENTRY pDeviceEntry)
                        }
                }
        }
-       
+
        /* 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 @@ OnInitDialog(IN HWND hwndDlg)
        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 @@ OnInitDialog(IN HWND hwndDlg)
                /* 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);
+       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;
-    }
+               cxSource = bitmap.bmWidth;
+               cySource = bitmap.bmHeight;
+       }
 }
 
 static VOID
@@ -321,11 +320,11 @@ OnBPPChanged(IN HWND hwndDlg)
        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 @@ OnBPPChanged(IN HWND hwndDlg)
                /* no change */
                return;
        }
-       
+
        PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
-       
+
        if (dmNewBitsPerPel < Current->dmBitsPerPel)
        {
                Current = Current->Blink;
@@ -368,7 +367,7 @@ OnBPPChanged(IN HWND hwndDlg)
                        Current = Current->Flink;
                }
        }
-       
+
        /* search smaller resolution compatible with current color depth */
        Current = CurrentDisplayDevice->CurrentSettings->Blink;
        while (Current != NULL)
@@ -381,7 +380,7 @@ OnBPPChanged(IN HWND hwndDlg)
                }
                Current = Current->Blink;
        }
-       
+
        /* search bigger resolution compatible with current color depth */
        Current = CurrentDisplayDevice->CurrentSettings->Flink;
        while (Current != NULL)
@@ -394,7 +393,7 @@ OnBPPChanged(IN HWND hwndDlg)
                }
                Current = Current->Flink;
        }
-       
+
        /* we shouldn't go there */
 }
 
@@ -408,7 +407,7 @@ OnResolutionChanged(IN HWND hwndDlg, IN DWORD NewPosition)
        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 @@ OnResolutionChanged(IN HWND hwndDlg, IN DWORD NewPosition)
                /* no change */
                return;
        }
-       
+
        PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
-       
+
        if (dmNewPelsHeight < Current->dmPelsHeight)
        {
                Current = Current->Blink;
@@ -451,7 +450,7 @@ OnResolutionChanged(IN HWND hwndDlg, IN DWORD NewPosition)
                        Current = Current->Flink;
                }
        }
-       
+
        /* search bigger color depth compatible with current resolution */
        Current = CurrentDisplayDevice->CurrentSettings->Flink;
        while (Current != NULL)
@@ -464,7 +463,7 @@ OnResolutionChanged(IN HWND hwndDlg, IN DWORD NewPosition)
                }
                Current = Current->Flink;
        }
-       
+
        /* search smaller color depth compatible with current resolution */
        Current = CurrentDisplayDevice->CurrentSettings->Blink;
        while (Current != NULL)
@@ -477,7 +476,7 @@ OnResolutionChanged(IN HWND hwndDlg, IN DWORD NewPosition)
                }
                Current = Current->Blink;
        }
-       
+
        /* we shouldn't go there */
 }
 
@@ -500,7 +499,7 @@ SettingsPageProc(IN HWND hwndDlg, IN UINT uMsg, IN WPARAM wParam, IN LPARAM lPar
                {
                        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 @@ SettingsPageProc(IN HWND hwndDlg, IN UINT uMsg, IN WPARAM wParam, IN LPARAM lPar
                        break;
                }
 
-        case WM_PAINT:
-            {
-                PAINTSTRUCT ps;
-                HDC hdc, hdcMem;
-       
-                hdc = BeginPaint(hwndDlg, &ps);
-                hdcMem = CreateCompatibleDC(hdc);
-                SelectObject(hdcMem, hBitmap);
+               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);
+                       TransparentBlt(hdc, 98, 0, cxSource, cySource, hdcMem, 0, 0, cxSource, cySource, 0xFF80FF);
 
-                DeleteDC(hdcMem);
-                EndPaint(hwndDlg, &ps);
+                       DeleteDC(hdcMem);
+                       EndPaint(hwndDlg, &ps);
 
-            } break;
+                       break;
+               }
 
                case WM_DESTROY:
                {
@@ -606,7 +606,7 @@ SettingsPageProc(IN HWND hwndDlg, IN UINT uMsg, IN WPARAM wParam, IN LPARAM lPar
                                Current = Next;
                        }
 
-            DeleteObject(hBitmap);
+                       DeleteObject(hBitmap);
                }
        }
        return FALSE;