0N/A/*
3909N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <awt.h>
0N/A#include <sun_awt_Win32GraphicsEnvironment.h>
1686N/A#include <sun_awt_Win32FontManager.h>
0N/A#include "awt_Canvas.h"
0N/A#include "awt_Win32GraphicsDevice.h"
0N/A#include "Devices.h"
0N/A#include "WindowsFlags.h"
1066N/A#include "DllUtil.h"
430N/A
430N/ABOOL DWMIsCompositionEnabled();
0N/A
0N/Avoid initScreens(JNIEnv *env) {
0N/A
0N/A if (!Devices::UpdateInstance(env)) {
0N/A JNU_ThrowInternalError(env, "Could not update the devices array.");
0N/A return;
0N/A }
0N/A}
0N/A
0N/A/**
0N/A * This function attempts to make a Win32 API call to
0N/A * BOOL SetProcessDPIAware(VOID);
0N/A * which is only present on Windows Vista, and which instructs the
0N/A * Vista Windows Display Manager that this application is High DPI Aware
0N/A * and does not need to be scaled by the WDM and lied about the
0N/A * actual system dpi.
0N/A */
0N/Astatic void
0N/ASetProcessDPIAwareProperty()
0N/A{
430N/A typedef BOOL (WINAPI SetProcessDPIAwareFunc)(void);
0N/A static BOOL bAlreadySet = FALSE;
0N/A
0N/A // setHighDPIAware is set in WindowsFlags.cpp
0N/A if (!setHighDPIAware || bAlreadySet) {
0N/A return;
0N/A }
0N/A
0N/A bAlreadySet = TRUE;
0N/A
4305N/A HMODULE hLibUser32Dll = JDK_LoadSystemLibrary("user32.dll");
0N/A
0N/A if (hLibUser32Dll != NULL) {
0N/A SetProcessDPIAwareFunc *lpSetProcessDPIAware =
0N/A (SetProcessDPIAwareFunc*)GetProcAddress(hLibUser32Dll,
0N/A "SetProcessDPIAware");
0N/A if (lpSetProcessDPIAware != NULL) {
0N/A lpSetProcessDPIAware();
0N/A }
0N/A ::FreeLibrary(hLibUser32Dll);
0N/A }
0N/A}
0N/A
430N/A#define DWM_COMP_UNDEFINED (~(TRUE|FALSE))
430N/Astatic int dwmIsCompositionEnabled = DWM_COMP_UNDEFINED;
430N/A
430N/A/**
430N/A * This function is called from toolkit event handling code when
430N/A * WM_DWMCOMPOSITIONCHANGED event is received
430N/A */
430N/Avoid DWMResetCompositionEnabled() {
430N/A dwmIsCompositionEnabled = DWM_COMP_UNDEFINED;
430N/A (void)DWMIsCompositionEnabled();
430N/A}
430N/A
430N/A/**
430N/A * Returns true if dwm composition is enabled, false if it is not applicable
430N/A * (if the OS is not Vista) or dwm composition is disabled.
430N/A */
430N/ABOOL DWMIsCompositionEnabled() {
430N/A // cheaper to check than whether it's vista or not
430N/A if (dwmIsCompositionEnabled != DWM_COMP_UNDEFINED) {
430N/A return (BOOL)dwmIsCompositionEnabled;
430N/A }
430N/A
430N/A if (!IS_WINVISTA) {
430N/A dwmIsCompositionEnabled = FALSE;
430N/A return FALSE;
430N/A }
430N/A
430N/A BOOL bRes = FALSE;
430N/A
1066N/A try {
1066N/A BOOL bEnabled;
1066N/A HRESULT res = DwmAPI::DwmIsCompositionEnabled(&bEnabled);
1066N/A if (SUCCEEDED(res)) {
1066N/A bRes = bEnabled;
1066N/A J2dTraceLn1(J2D_TRACE_VERBOSE, " composition enabled: %d",bRes);
430N/A } else {
1066N/A J2dTraceLn1(J2D_TRACE_ERROR,
1066N/A "IsDWMCompositionEnabled: error %x when detecting"\
1066N/A "if composition is enabled", res);
430N/A }
1066N/A } catch (const DllUtil::Exception &) {
430N/A J2dTraceLn(J2D_TRACE_ERROR,
1066N/A "IsDWMCompositionEnabled: no DwmIsCompositionEnabled() "\
1066N/A "in dwmapi.dll or dwmapi.dll cannot be loaded");
430N/A }
430N/A
430N/A dwmIsCompositionEnabled = bRes;
430N/A
430N/A JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
430N/A JNU_CallStaticMethodByName(env, NULL,
430N/A "sun/awt/Win32GraphicsEnvironment",
430N/A "dwmCompositionChanged", "(Z)V", (jboolean)bRes);
430N/A return bRes;
430N/A}
430N/A
0N/A/*
0N/A * Class: sun_awt_Win32GraphicsEnvironment
0N/A * Method: initDisplay
0N/A * Signature: ()V
0N/A */
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_awt_Win32GraphicsEnvironment_initDisplay(JNIEnv *env,
0N/A jclass thisClass)
0N/A{
0N/A // This method needs to be called prior to any display-related activity
0N/A SetProcessDPIAwareProperty();
0N/A
430N/A DWMIsCompositionEnabled();
430N/A
0N/A initScreens(env);
0N/A}
0N/A
0N/A/*
0N/A * Class: sun_awt_Win32GraphicsEnvironment
0N/A * Method: getNumScreens
0N/A * Signature: ()I
0N/A */
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_awt_Win32GraphicsEnvironment_getNumScreens(JNIEnv *env,
0N/A jobject thisobj)
0N/A{
0N/A Devices::InstanceAccess devices;
0N/A return devices->GetNumDevices();
0N/A}
0N/A
0N/A/*
0N/A * Class: sun_awt_Win32GraphicsEnvironment
0N/A * Method: getDefaultScreen
0N/A * Signature: ()I
0N/A */
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_awt_Win32GraphicsEnvironment_getDefaultScreen(JNIEnv *env,
0N/A jobject thisobj)
0N/A{
0N/A return AwtWin32GraphicsDevice::GetDefaultDeviceIndex();
0N/A}
0N/A
0N/A/*
1686N/A * Class: sun_awt_Win32FontManager
0N/A * Method: registerFontWithPlatform
0N/A * Signature: (Ljava/lang/String;)V
0N/A */
0N/AJNIEXPORT void JNICALL
1686N/AJava_sun_awt_Win32FontManager_registerFontWithPlatform(JNIEnv *env,
1686N/A jclass cl,
1686N/A jstring fontName)
0N/A{
862N/A LPTSTR file = (LPTSTR)JNU_GetStringPlatformChars(env, fontName, JNI_FALSE);
862N/A if (file) {
862N/A ::AddFontResourceEx(file, FR_PRIVATE, NULL);
862N/A JNU_ReleaseStringPlatformChars(env, fontName, file);
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
1686N/A * Class: sun_awt_Win32FontManagerEnvironment
0N/A * Method: deRegisterFontWithPlatform
0N/A * Signature: (Ljava/lang/String;)V
0N/A *
0N/A * This method intended for future use.
0N/A */
0N/AJNIEXPORT void JNICALL
1686N/AJava_sun_awt_Win32FontManager_deRegisterFontWithPlatform(JNIEnv *env,
1686N/A jclass cl,
1686N/A jstring fontName)
0N/A{
862N/A LPTSTR file = (LPTSTR)JNU_GetStringPlatformChars(env, fontName, JNI_FALSE);
862N/A if (file) {
862N/A ::RemoveFontResourceEx(file, FR_PRIVATE, NULL);
862N/A JNU_ReleaseStringPlatformChars(env, fontName, file);
0N/A }
0N/A}
0N/A
0N/A#define EUDCKEY_JA_JP L"EUDC\\932"
0N/A#define EUDCKEY_ZH_CN L"EUDC\\936"
0N/A#define EUDCKEY_ZH_TW L"EUDC\\950"
0N/A#define EUDCKEY_KO_KR L"EUDC\\949"
3354N/A#define EUDCKEY_EN_US L"EUDC\\1252"
0N/A#define LANGID_JA_JP 0x411
0N/A#define LANGID_ZH_CN 0x0804
0N/A#define LANGID_ZH_SG 0x1004
0N/A#define LANGID_ZH_TW 0x0404
0N/A#define LANGID_ZH_HK 0x0c04
0N/A#define LANGID_ZH_MO 0x1404
0N/A#define LANGID_KO_KR 0x0412
3354N/A#define LANGID_EN_US 0x0409
0N/A
0N/A
0N/AJNIEXPORT jstring JNICALL
1686N/AJava_sun_awt_Win32FontManager_getEUDCFontFile(JNIEnv *env, jclass cl) {
0N/A int rc;
0N/A HKEY key;
0N/A DWORD type;
0N/A WCHAR fontPathBuf[MAX_PATH + 1];
0N/A unsigned long fontPathLen = MAX_PATH + 1;
0N/A WCHAR tmpPath[MAX_PATH + 1];
0N/A LPWSTR fontPath = fontPathBuf;
0N/A LPWSTR eudcKey = NULL;
0N/A
0N/A LANGID langID = GetSystemDefaultLangID();
0N/A //lookup for encoding ID, EUDC only supported in
0N/A //codepage 932, 936, 949, 950 (and unicode)
3354N/A // On Windows 7, at least for me, it shows up in Cp1252 if
3354N/A // I create a custom font. Might as well support that as it makes
3354N/A // verification easier.
0N/A if (langID == LANGID_JA_JP) {
0N/A eudcKey = EUDCKEY_JA_JP;
0N/A } else if (langID == LANGID_ZH_CN || langID == LANGID_ZH_SG) {
0N/A eudcKey = EUDCKEY_ZH_CN;
0N/A } else if (langID == LANGID_ZH_HK || langID == LANGID_ZH_TW ||
0N/A langID == LANGID_ZH_MO) {
0N/A eudcKey = EUDCKEY_ZH_TW;
0N/A } else if (langID == LANGID_KO_KR) {
0N/A eudcKey = EUDCKEY_KO_KR;
3354N/A } else if (langID == LANGID_EN_US) {
3354N/A eudcKey = EUDCKEY_EN_US;
0N/A } else {
0N/A return NULL;
0N/A }
0N/A
0N/A rc = RegOpenKeyEx(HKEY_CURRENT_USER, eudcKey, 0, KEY_READ, &key);
0N/A if (rc != ERROR_SUCCESS) {
0N/A return NULL;
0N/A }
0N/A rc = RegQueryValueEx(key,
0N/A L"SystemDefaultEUDCFont",
0N/A 0,
0N/A &type,
0N/A (LPBYTE)fontPath,
0N/A &fontPathLen);
0N/A RegCloseKey(key);
0N/A if (rc != ERROR_SUCCESS || type != REG_SZ) {
0N/A return NULL;
0N/A }
0N/A fontPath[fontPathLen] = L'\0';
0N/A if (wcsstr(fontPath, L"%SystemRoot%")) {
0N/A //if the fontPath includes %SystemRoot%
0N/A LPWSTR systemRoot = _wgetenv(L"SystemRoot");
0N/A if (systemRoot != NULL
2859N/A && swprintf(tmpPath, MAX_PATH, L"%s%s", systemRoot, fontPath + 12) != -1) {
0N/A fontPath = tmpPath;
0N/A }
0N/A else {
0N/A return NULL;
0N/A }
0N/A } else if (wcscmp(fontPath, L"EUDC.TTE") == 0) {
0N/A //else to see if it only inludes "EUDC.TTE"
0N/A WCHAR systemRoot[MAX_PATH + 1];
0N/A if (GetWindowsDirectory(systemRoot, MAX_PATH + 1) != 0) {
2859N/A swprintf(tmpPath, MAX_PATH, L"%s\\FONTS\\EUDC.TTE", systemRoot);
0N/A fontPath = tmpPath;
0N/A }
0N/A else {
0N/A return NULL;
0N/A }
0N/A }
0N/A return JNU_NewStringPlatform(env, fontPath);
0N/A}
0N/A
0N/A/*
0N/A * Class: sun_awt_Win32GraphicsEnvironment
0N/A * Method: getXResolution
0N/A * Signature: ()I
0N/A */
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_awt_Win32GraphicsEnvironment_getXResolution(JNIEnv *env, jobject wge)
0N/A{
0N/A TRY;
0N/A
0N/A HWND hWnd = ::GetDesktopWindow();
0N/A HDC hDC = ::GetDC(hWnd);
0N/A jint result = ::GetDeviceCaps(hDC, LOGPIXELSX);
0N/A ::ReleaseDC(hWnd, hDC);
0N/A return result;
0N/A
0N/A CATCH_BAD_ALLOC_RET(0);
0N/A}
0N/A
0N/A/*
0N/A * Class: sun_awt_Win32GraphicsEnvironment
0N/A * Method: getYResolution
0N/A * Signature: ()I
0N/A */
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_awt_Win32GraphicsEnvironment_getYResolution(JNIEnv *env, jobject wge)
0N/A{
0N/A TRY;
0N/A
0N/A HWND hWnd = ::GetDesktopWindow();
0N/A HDC hDC = ::GetDC(hWnd);
0N/A jint result = ::GetDeviceCaps(hDC, LOGPIXELSY);
0N/A ::ReleaseDC(hWnd, hDC);
0N/A return result;
0N/A
0N/A CATCH_BAD_ALLOC_RET(0);
0N/A}
430N/A
430N/A/*
430N/A * Class: sun_awt_Win32GraphicsEnvironment
430N/A * Method: isVistaOS
430N/A * Signature: ()Z
430N/A */
430N/AJNIEXPORT jboolean JNICALL Java_sun_awt_Win32GraphicsEnvironment_isVistaOS
430N/A (JNIEnv *env, jclass wgeclass)
430N/A{
430N/A return IS_WINVISTA;
430N/A}