1/*
2 * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
3 * Copyright (C) 2009 Torch Mobile, Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "PlatformScreen.h"
29
30#include "HostWindow.h"
31#include "IntRect.h"
32#include "FloatRect.h"
33#include "Frame.h"
34#include "FrameView.h"
35#include "Page.h"
36#include <windows.h>
37
38namespace WebCore {
39
40// Returns info for the default monitor if widget is NULL
41static MONITORINFOEX monitorInfoForWidget(Widget* widget)
42{
43    HWND window = widget ? widget->root()->hostWindow()->platformPageClient() : 0;
44    HMONITOR monitor = MonitorFromWindow(window, MONITOR_DEFAULTTOPRIMARY);
45
46    MONITORINFOEX monitorInfo;
47    monitorInfo.cbSize = sizeof(MONITORINFOEX);
48    GetMonitorInfo(monitor, &monitorInfo);
49    return monitorInfo;
50}
51
52static DEVMODE deviceInfoForWidget(Widget* widget)
53{
54    DEVMODE deviceInfo;
55    deviceInfo.dmSize = sizeof(DEVMODE);
56    deviceInfo.dmDriverExtra = 0;
57#if OS(WINCE)
58    if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &deviceInfo))
59        deviceInfo.dmBitsPerPel = 16;
60#else
61    MONITORINFOEX monitorInfo = monitorInfoForWidget(widget);
62    EnumDisplaySettings(monitorInfo.szDevice, ENUM_CURRENT_SETTINGS, &deviceInfo);
63#endif
64
65    return deviceInfo;
66}
67
68int screenDepth(Widget* widget)
69{
70    DEVMODE deviceInfo = deviceInfoForWidget(widget);
71    if (deviceInfo.dmBitsPerPel == 32) {
72        // Some video drivers return 32, but this function is supposed to ignore the alpha
73        // component. See <http://webkit.org/b/42972>.
74        return 24;
75    }
76    return deviceInfo.dmBitsPerPel;
77}
78
79int screenDepthPerComponent(Widget* widget)
80{
81    // FIXME: Assumes RGB -- not sure if this is right.
82    return screenDepth(widget) / 3;
83}
84
85bool screenIsMonochrome(Widget* widget)
86{
87#if OS(WINCE)
88    // EnumDisplaySettings doesn't set dmColor in DEVMODE.
89    return false;
90#else
91    DEVMODE deviceInfo = deviceInfoForWidget(widget);
92    return deviceInfo.dmColor == DMCOLOR_MONOCHROME;
93#endif
94}
95
96FloatRect screenRect(Widget* widget)
97{
98    MONITORINFOEX monitorInfo = monitorInfoForWidget(widget);
99    return monitorInfo.rcMonitor;
100}
101
102FloatRect screenAvailableRect(Widget* widget)
103{
104    MONITORINFOEX monitorInfo = monitorInfoForWidget(widget);
105    return monitorInfo.rcWork;
106}
107
108} // namespace WebCore
109