Misc.cpp 2.12 KB
Newer Older
1
2
3
#include <string>
#include <Windows.h>
#include <tchar.h>
4
5
#include <Shlobj.h>
#include <Shlwapi.h>
6
7
8
9
10

#include <map>

namespace Misc
{
Gargaj's avatar
Gargaj committed
11
12
13
14
15
16
17
18
  void PlatformStartup()
  {
  }

  void PlatformShutdown()
  {
  }

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  std::map<std::string,std::string> keymaps;
  void InitKeymaps()
  {
    HKEY hk = NULL;
    RegOpenKeyExA( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts", NULL, KEY_READ, &hk );

    LONG result = NULL;
    int idx = 0;
    while ( result != ERROR_NO_MORE_ITEMS )
    {
      char szKeyName[255];
      ZeroMemory(szKeyName,255);
      DWORD nKeyName = 255;
      result = RegEnumKeyExA( hk, idx, szKeyName, &nKeyName, NULL, NULL, NULL, NULL);
      if (result == ERROR_SUCCESS)
      {
        HKEY hkSub = NULL;
        RegOpenKeyExA( hk, szKeyName, NULL, KEY_READ, &hkSub );

        BYTE szValue[255];
        ZeroMemory(szValue,255);
        DWORD nValue = 255;
        DWORD type = REG_SZ;
        LONG resultKey = RegQueryValueExA( hkSub, "Layout Text", NULL, &type, szValue, &nValue );

        CharLowerA( szKeyName );
Marco Foco's avatar
Marco Foco committed
45
        keymaps[szKeyName] = (const char*)szValue;
46
47
48
49
      }
      idx++;
    }
  }
Gargaj's avatar
Gargaj committed
50

51
52
53
54
55
56
57
  void GetKeymapName( char * sz )
  {
    char szCode[KL_NAMELENGTH];
    ::GetKeyboardLayoutNameA(szCode);
    CharLowerA( szCode );
    strncpy( sz, keymaps.count(szCode) ? keymaps[szCode].c_str() : "<unknown>" ,255);
  }
Gargaj's avatar
Gargaj committed
58

Marco Foco's avatar
Marco Foco committed
59
  bool ExecuteCommand( const char * cmd, const char * param )
60
61
62
63
  {
    HINSTANCE hI = ShellExecute( NULL, NULL, cmd, param, NULL, SW_SHOW );
    return (int)hI >= 32;
  }
Gargaj's avatar
Gargaj committed
64

Gargaj's avatar
Gargaj committed
65
66
67
68
  bool FileExists(const char * path)
  {
    return GetFileAttributesA(path) != INVALID_FILE_ATTRIBUTES;
  }
Gargaj's avatar
Gargaj committed
69

Gargaj's avatar
Gargaj committed
70
71
  const char * GetDefaultFontPath()
  {
72
73
74
75
76
    char windowsPath[ MAX_PATH ];
    if ( SHGetFolderPath( NULL, CSIDL_WINDOWS, NULL, 0, windowsPath ) != S_OK )
    {
      return NULL;
    }
Gargaj's avatar
Gargaj committed
77
78
    const char* fontPaths[] = 
    {
79
      "Fonts\\cour.ttf",
Gargaj's avatar
Gargaj committed
80
81
82
83
      NULL
    };
    for (int i = 0; fontPaths[i]; ++i)
    {
84
85
86
      static char fullPath[ MAX_PATH ] = { 0 };
      PathCombineA( fullPath, windowsPath, fontPaths[ i ] );
      if (FileExists( fullPath ))
Gargaj's avatar
Gargaj committed
87
      {
88
        return fullPath;
Gargaj's avatar
Gargaj committed
89
90
91
92
      }
    }
    return NULL;
  }
93
}