Пример #1
0
java_props_t *
GetJavaProperties(JNIEnv* env)
{
    static java_props_t sprops = {0};

    OSVERSIONINFOEX ver;

    if (sprops.user_dir) {
        return &sprops;
    }

    /* AWT properties */
    sprops.awt_toolkit = "sun.awt.windows.WToolkit";

    /* tmp dir */
    {
        WCHAR tmpdir[MAX_PATH + 1];
        /* we might want to check that this succeed */
        GetTempPathW(MAX_PATH + 1, tmpdir);
        sprops.tmp_dir = _wcsdup(tmpdir);
    }

    /* Printing properties */
    sprops.printerJob = "sun.awt.windows.WPrinterJob";

    /* Java2D properties */
    sprops.graphics_env = "sun.awt.Win32GraphicsEnvironment";

    {   /* This is used only for debugging of font problems. */
        WCHAR *path = _wgetenv(L"JAVA2D_FONTPATH");
        sprops.font_dir = (path != NULL) ? _wcsdup(path) : NULL;
    }

    /* OS properties */
    {
        char buf[100];
        SYSTEM_INFO si;
        PGNSI pGNSI;

        ver.dwOSVersionInfoSize = sizeof(ver);
        GetVersionEx((OSVERSIONINFO *) &ver);

        ZeroMemory(&si, sizeof(SYSTEM_INFO));
        // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
        pGNSI = (PGNSI) GetProcAddress(
                    GetModuleHandle(TEXT("kernel32.dll")),
                    "GetNativeSystemInfo");
        if(NULL != pGNSI)
            pGNSI(&si);
        else
            GetSystemInfo(&si);

        /*
         * From msdn page on OSVERSIONINFOEX, current as of this
         * writing, decoding of dwMajorVersion and dwMinorVersion.
         *
         *  Operating system            dwMajorVersion  dwMinorVersion
         * ==================           ==============  ==============
         *
         * Windows 95                   4               0
         * Windows 98                   4               10
         * Windows ME                   4               90
         * Windows 3.51                 3               51
         * Windows NT 4.0               4               0
         * Windows 2000                 5               0
         * Windows XP 32 bit            5               1
         * Windows Server 2003 family   5               2
         * Windows XP 64 bit            5               2
         *       where ((&ver.wServicePackMinor) + 2) = 1
         *       and  si.wProcessorArchitecture = 9
         * Windows Vista family         6               0  (VER_NT_WORKSTATION)
         * Windows Server 2008          6               0  (!VER_NT_WORKSTATION)
         * Windows 7                    6               1  (VER_NT_WORKSTATION)
         * Windows Server 2008 R2       6               1  (!VER_NT_WORKSTATION)
         *
         * This mapping will presumably be augmented as new Windows
         * versions are released.
         */
        switch (ver.dwPlatformId) {
        case VER_PLATFORM_WIN32s:
            sprops.os_name = "Windows 3.1";
            break;
        case VER_PLATFORM_WIN32_WINDOWS:
            if (ver.dwMajorVersion == 4) {
                switch (ver.dwMinorVersion) {
                case  0:
                    sprops.os_name = "Windows 95";
                    break;
                case 10:
                    sprops.os_name = "Windows 98";
                    break;
                case 90:
                    sprops.os_name = "Windows Me";
                    break;
                default:
                    sprops.os_name = "Windows 9X (unknown)";
                    break;
                }
            } else {
                sprops.os_name = "Windows 9X (unknown)";
            }
            break;
        case VER_PLATFORM_WIN32_NT:
            if (ver.dwMajorVersion <= 4) {
                sprops.os_name = "Windows NT";
            } else if (ver.dwMajorVersion == 5) {
                switch (ver.dwMinorVersion) {
                case  0:
                    sprops.os_name = "Windows 2000";
                    break;
                case  1:
                    sprops.os_name = "Windows XP";
                    break;
                case  2:
                    /*
                     * From MSDN OSVERSIONINFOEX and SYSTEM_INFO documentation:
                     *
                     * "Because the version numbers for Windows Server 2003
                     * and Windows XP 6u4 bit are identical, you must also test
                     * whether the wProductType member is VER_NT_WORKSTATION.
                     * and si.wProcessorArchitecture is
                     * PROCESSOR_ARCHITECTURE_AMD64 (which is 9)
                     * If it is, the operating system is Windows XP 64 bit;
                     * otherwise, it is Windows Server 2003."
                     */
                    if(ver.wProductType == VER_NT_WORKSTATION &&
                            si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
                        sprops.os_name = "Windows XP"; /* 64 bit */
                    } else {
                        sprops.os_name = "Windows 2003";
                    }
                    break;
                default:
                    sprops.os_name = "Windows NT (unknown)";
                    break;
                }
            } else if (ver.dwMajorVersion == 6) {
                /*
                 * See table in MSDN OSVERSIONINFOEX documentation.
                 */
                if (ver.wProductType == VER_NT_WORKSTATION) {
                    switch (ver.dwMinorVersion) {
                    case  0:
                        sprops.os_name = "Windows Vista";
                        break;
                    case  1:
                        sprops.os_name = "Windows 7";
                        break;
                    default:
                        sprops.os_name = "Windows NT (unknown)";
                    }
                } else {
                    switch (ver.dwMinorVersion) {
                    case  0:
                        sprops.os_name = "Windows Server 2008";
                        break;
                    case  1:
                        sprops.os_name = "Windows Server 2008 R2";
                        break;
                    default:
                        sprops.os_name = "Windows NT (unknown)";
                    }
                }
            } else {
                sprops.os_name = "Windows NT (unknown)";
            }
            break;
        default:
            sprops.os_name = "Windows (unknown)";
            break;
        }
        sprintf(buf, "%d.%d", ver.dwMajorVersion, ver.dwMinorVersion);
        sprops.os_version = _strdup(buf);
#if _M_IA64
        sprops.os_arch = "ia64";
#elif _M_AMD64
        sprops.os_arch = "amd64";
#elif _X86_
        sprops.os_arch = "x86";
#else
        sprops.os_arch = "unknown";
#endif

        sprops.patch_level = _strdup(ver.szCSDVersion);

        sprops.desktop = "windows";
    }

    /* Endianness of platform */
    {
        unsigned int endianTest = 0xff000000;
        if (((char*)(&endianTest))[0] != 0) {
            sprops.cpu_endian = "big";
        } else {
            sprops.cpu_endian = "little";
        }
    }

    /* CPU ISA list */
    sprops.cpu_isalist = cpu_isalist();

    /*
     * User name
     * We try to avoid calling GetUserName as it turns out to
     * be surprisingly expensive on NT.  It pulls in an extra
     * 100 K of footprint.
     */
    {
        WCHAR *uname = _wgetenv(L"USERNAME");
        if (uname != NULL && wcslen(uname) > 0) {
            sprops.user_name = _wcsdup(uname);
        } else {
            DWORD buflen = 0;
            if (GetUserNameW(NULL, &buflen) == 0 &&
                    GetLastError() == ERROR_INSUFFICIENT_BUFFER)
            {
                uname = (WCHAR*)malloc(buflen * sizeof(WCHAR));
                if (uname != NULL && GetUserNameW(uname, &buflen) == 0) {
                    free(uname);
                    uname = NULL;
                }
            } else {
                uname = NULL;
            }
            sprops.user_name = (uname != NULL) ? uname : L"unknown";
        }
    }

    /*
     * Home directory/
     *
     * We first look under a standard registry key.  If that fails we
     * fall back on using a SHELL32.DLL API.  If that fails we use a
     * default value.
     *
     * Note: To save space we want to avoid loading SHELL32.DLL
     * unless really necessary.  However if we do load it, we leave it
     * in memory, as it may be needed again later.
     *
     * The normal result is that for a given user name XXX:
     *     On multi-user NT, user.home gets set to c:\winnt\profiles\XXX.
     *     On multi-user Win95, user.home gets set to c:\windows\profiles\XXX.
     *     On single-user Win95, user.home gets set to c:\windows.
     */
    {
        WCHAR *homep = getHomeFromRegistry();
        if (homep == NULL) {
            homep = getHomeFromShell32();
            if (homep == NULL)
                homep = L"C:\\";
        }
        sprops.user_home = _wcsdup(homep);
    }

    /*
     *  user.language
     *  user.script, user.country, user.variant (if user's environment specifies them)
     *  file.encoding
     *  file.encoding.pkg
     */
    {
        /*
         * query the system for the current system default locale
         * (which is a Windows LCID value),
         */
        LCID userDefaultLCID = GetUserDefaultLCID();
        LCID systemDefaultLCID = GetSystemDefaultLCID();
        LCID userDefaultUILang = GetUserDefaultUILanguage();

        {
            char * display_encoding;

            // Windows UI Language selection list only cares "language"
            // information of the UI Language. For example, the list
            // just lists "English" but it actually means "en_US", and
            // the user cannot select "en_GB" (if exists) in the list.
            // So, this hack is to use the user LCID region information
            // for the UI Language, if the "language" portion of those
            // two locales are the same.
            if (PRIMARYLANGID(LANGIDFROMLCID(userDefaultLCID)) ==
                    PRIMARYLANGID(LANGIDFROMLCID(userDefaultUILang))) {
                userDefaultUILang = userDefaultLCID;
            }

            SetupI18nProps(userDefaultUILang,
                           &sprops.language,
                           &sprops.script,
                           &sprops.country,
                           &sprops.variant,
                           &display_encoding);
            SetupI18nProps(userDefaultLCID,
                           &sprops.format_language,
                           &sprops.format_script,
                           &sprops.format_country,
                           &sprops.format_variant,
                           &sprops.encoding);
            SetupI18nProps(userDefaultUILang,
                           &sprops.display_language,
                           &sprops.display_script,
                           &sprops.display_country,
                           &sprops.display_variant,
                           &display_encoding);

            sprops.sun_jnu_encoding = getEncodingInternal(systemDefaultLCID);
            if (LANGIDFROMLCID(userDefaultLCID) == 0x0c04 && ver.dwMajorVersion == 6) {
                // MS claims "Vista has built-in support for HKSCS-2004.
                // All of the HKSCS-2004 characters have Unicode 4.1.
                // PUA code point assignments". But what it really means
                // is that the HKSCS-2004 is ONLY supported in Unicode.
                // Test indicates the MS950 in its zh_HK locale is a
                // "regular" MS950 which does not handle HKSCS-2004 at
                // all. Set encoding to MS950_HKSCS.
                sprops.encoding = "MS950_HKSCS";
                sprops.sun_jnu_encoding = "MS950_HKSCS";
            }
        }
    }

    sprops.unicode_encoding = "UnicodeLittle";
    /* User TIMEZONE */
    {
        /*
         * We defer setting up timezone until it's actually necessary.
         * Refer to TimeZone.getDefault(). However, the system
         * property is necessary to be able to be set by the command
         * line interface -D. Here temporarily set a null string to
         * timezone.
         */
        sprops.timezone = "";
    }

    /* Current directory */
    {
        WCHAR buf[MAX_PATH];
        if (GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR), buf) != 0)
            sprops.user_dir = _wcsdup(buf);
    }

    sprops.file_separator = "\\";
    sprops.path_separator = ";";
    sprops.line_separator = "\r\n";

    return &sprops;
}
java_props_t *
GetJavaProperties(JNIEnv* env)
{
    static java_props_t sprops = {0};
    char *v; /* tmp var */

    if (sprops.user_dir) {
        return &sprops;
    }

    /* AWT properties */
    sprops.awt_toolkit = "sun.awt.windows.WToolkit";

    /* tmp dir */
    {
        char tmpdir[MAX_PATH + 1];
        /* we might want to check that this succeed */
        GetTempPath(MAX_PATH + 1, tmpdir);
        sprops.tmp_dir = strdup(tmpdir);
    }

    /* Printing properties */
    sprops.printerJob = "sun.awt.windows.WPrinterJob";

    /* Preferences properties */
    sprops.util_prefs_PreferencesFactory =  
                                           "java.util.prefs.WindowsPreferencesFactory";

    /* Java2D properties */
    sprops.graphics_env = "sun.awt.Win32GraphicsEnvironment";

    {
        char buf[max(512, MAX_PATH+1)];
        char *path = getenv("JAVA_FONTS");
        if (path != 0) {
            if (sprintf(buf, "%s", path) != -1) {
                v = buf; /* sysNativePath(buf); Is this necessary? */
            } else {
                v = "";
            }
        } else {
            v = "";
        }
        sprops.font_dir = strdup(v);
    }

    /* OS properties */
    {
        char buf[100];
        OSVERSIONINFO ver;    
	ver.dwOSVersionInfoSize = sizeof(ver);
	GetVersionEx(&ver);
	
	/*
	 * From msdn page on OSVERSIONINFOEX, current as of this
	 * writing decoding of dwMajorVersion and dwMinorVersion.
	 *
	 *  Operating system		dwMajorVersion	dwMinorVersion
	 * ==================		==============	==============
	 *
	 * Windows 95			4		0
	 * Windows 98			4		10
	 * Windows ME			4		90
	 * Windows 3.51			3		51
	 * Windows NT 4.0		4		0
	 * Windows 2000			5		0
	 * Windows XP			5		1
	 * Windows Server 2003 family	5		2
	 *
	 * This mapping will presumably be augmented as new Windows
	 * versions are released.
	 */
	switch (ver.dwPlatformId) {
	case VER_PLATFORM_WIN32s:
	    sprops.os_name = "Windows 3.1";
	    break;
	case VER_PLATFORM_WIN32_WINDOWS:
           if (ver.dwMajorVersion == 4) {
                switch (ver.dwMinorVersion) {
                case 0:
                    sprops.os_name = "Windows 95";
                    break;
                case 10:
                    sprops.os_name = "Windows 98";
                    break;
                case 90:
                    sprops.os_name = "Windows Me";
                    break;
                default:
                    sprops.os_name = "Windows 9X (unknown)";
                }
            } else {
                sprops.os_name = "Windows 9X (unknown)";
            }
            break;
	case VER_PLATFORM_WIN32_NT:
            if (ver.dwMajorVersion <= 4) {
	        sprops.os_name = "Windows NT";
            } else if (ver.dwMajorVersion == 5) {
	        switch (ver.dwMinorVersion) {
		case 0:
		    sprops.os_name = "Windows 2000";
		    break;
	        case 1:
		    sprops.os_name = "Windows XP";
		    break;
	        case 2:
		    sprops.os_name = "Windows 2003";
		    break;
	        default:
		    sprops.os_name = "Windows NT (unknown)";
		    break;
	      }
            } else {
                sprops.os_name = "Windows NT (unknown)";
            }
	    break;
        default:
            sprops.os_name = "Windows (unknown)";
            break;
	}
	sprintf(buf, "%d.%d", ver.dwMajorVersion, ver.dwMinorVersion);
	sprops.os_version = strdup(buf);
#if _M_IA64
        sprops.os_arch = "ia64";
#elif _X86_
        sprops.os_arch = "x86";
#else
        sprops.os_arch = "unknown";
#endif

	sprops.patch_level = strdup(ver.szCSDVersion);
    }

    /* Endianness of platform */
    {
        unsigned int endianTest = 0xff000000; 
        if (((char*)(&endianTest))[0] != 0) {
            sprops.cpu_endian = "big";
        } else {
            sprops.cpu_endian = "little";
        }
    }

    /* ISA list */
    {
        int procType = 0;
        SYSTEM_INFO info;
        HMODULE lib;
        ProcPresentType proc_present;

        GetSystemInfo(&info);
        
        if (strcmp(sprops.os_name, "Windows NT") == 0) {
            boolean mmx = 0;
            char list[100];
            list[0] = 0;

            lib = LoadLibrary("KERNEL32");
            proc_present = (ProcPresentType)GetProcAddress(lib,
                                               "IsProcessorFeaturePresent");
            if (proc_present) {
                mmx = (*proc_present)(PF_MMX_INSTRUCTIONS_AVAILABLE);
                procType = info.wProcessorLevel;
                switch (procType) {
                    case 6:
                        if (mmx) {
                            strcat(list, "pentium_pro+mmx pentium_pro ");
                        } else {
                            strcat(list, "pentium_pro ");
                        }
                        /* FALL THROUGH */
                    case 5:
                        if (mmx) {
                            strcat(list, "pentium+mmx pentium ");
                        } else {
                            strcat(list, "pentium ");
                        }
                        /* FALL THROUGH */
                    case 4:
                        strcat(list, "i486 ");
                        /* FALL THROUGH */
                    case 3:
                        strcat(list, "i386");
                }
                sprops.cpu_isalist = strdup(list);
            }
	    FreeLibrary(lib);
        } else {
            procType = info.dwProcessorType;
            switch(procType) {
                case PROCESSOR_INTEL_386:
                    sprops.cpu_isalist = "i386";
                    break;
                case PROCESSOR_INTEL_486:
                    sprops.cpu_isalist = "i486 i386";
                    break;
                case PROCESSOR_INTEL_PENTIUM:
                    sprops.cpu_isalist = "pentium i486 i386";
                    break;
#ifdef _M_IA64
                case PROCESSOR_INTEL_IA64:
                    sprops.cpu_isalist = "ia64";
                    break;
#endif
            }
        }
    }

    /*
     * User name
     * We try to avoid calling GetUserName as it turns out to
     * be surprisingly expensive on NT.  It pulls in an extra
     * 100 K of footprint.
     */
    {
        char *uname = getenv("USERNAME");
        if (uname != NULL && strlen(uname) > 0) {
	    sprops.user_name = strdup(uname);
        } else {
            char buf[100];
            int buflen = sizeof(buf);
            sprops.user_name =
                GetUserName(buf, &buflen) ? strdup(buf) : "unknown";
	}
    }

    /*
     * Home directory/
     *
     * We first look under a standard registry key.  If that fails we
     * fall back on using a SHELL32.DLL API.  If that fails we use a
     * default value. 
     *
     * Note: To save space we want to avoid loading SHELL32.DLL
     * unless really necessary.  However if we do load it, we leave it
     * in memory, as it may be needed again later.
     *
     * The normal result is that for a given user name XXX:
     *     On multi-user NT, user.home gets set to c:\winnt\profiles\XXX.
     *     On multi-user Win95, user.home gets set to c:\windows\profiles\XXX.
     *     On single-user Win95, user.home gets set to c:\windows.
     */
    {
	char *homep = getHomeFromRegistry();
	if (homep == NULL) {
	    homep = getHomeFromShell32();
	    if (homep == NULL) {
                homep = "C:\\";
	    }
        }
        sprops.user_home = homep;
    }

    /*
     *  user.language
     *  user.country, user.variant (if user's environment specifies them)
     *  file.encoding
     *  file.encoding.pkg
     */
    {
        /*
         * query the system for the current system default locale
         * (which is a Windows LCID value), 
         */
        LANGID langID = LANGIDFROMLCID(GetThreadLocale()); 
        {
            int index = getLocaleEntryIndex(langID);

            /*
             * if we didn't find the LCID that the system returned to us,
	     * we don't have a Java locale ID that corresponds to it. 
	     * Fall back on en_US.
             */
            if (index == -1) {
                sprops.language = "en";
                sprops.country = "US";
                sprops.encoding = "Cp1252";
            } else {

                /* otherwise, look up the corresponding Java locale ID from
                 * the list of Java locale IDs and set up the system properties
                 * accordingly.
                 */
            
                char* lang;
                char* ctry;
                char* variant;

                lang = strdup(langIDMap[index].javaID);
                ctry = lang;

                while (*ctry != '_' && *ctry != 0)
                    ++ctry;

                if (*ctry == '_') {
                    *ctry++ = 0;
                }
                
                variant = ctry;
                while (*variant != '_' && *variant != 0)
                    ++variant;

                if (*variant == '_') {
                    *variant++ = 0;
                }
                
                sprops.language = lang;
                sprops.country = ctry;
                sprops.variant = variant;
                sprops.encoding = getEncodingInternal(index);
            }
        }
    }

    sprops.unicode_encoding = "UnicodeLittle";
    /* User TIMEZONE */
    {
	/*
	 * We defer setting up timezone until it's actually necessary.
	 * Refer to TimeZone.getDefault(). However, the system
	 * property is necessary to be able to be set by the command
	 * line interface -D. Here temporarily set a null string to
	 * timezone.
	 */
	sprops.timezone = "";
    }

    /* Current directory */
    {
        char buf[MAX_PATH];
        GetCurrentDirectory(sizeof(buf), buf);
        sprops.user_dir = strdup(buf);
    }

    sprops.file_separator = "\\";
    sprops.path_separator = ";";
    sprops.line_separator = "\r\n";

    return &sprops;
}