1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
#include "stdafx.h"
#include "AppState.h"
#include "Utils/miscUtils.h"
#include <commctrl.h>
#pragma comment(lib, "Comctl32.lib")
#include "CircleIndicator.h"
namespace
{
static const HKEY regKeyRoot = HKEY_CURRENT_USER;
const LPCTSTR regKey = LR"(SOFTWARE\const.me\WhisperDesktop)";
const LPCTSTR regValPath = L"modelPath";
const LPCTSTR regValImpl = L"modelImpl";
const LPCTSTR regValLang = L"language";
const LPCTSTR regValLastScreen = L"screen";
const LPCTSTR regValGpuFlags = L"gpuFlags";
static HRESULT readString( CRegKey& k, LPCTSTR name, CString& rdi )
{
ULONG nChars = 0;
LSTATUS lss = k.QueryStringValue( name, nullptr, &nChars );
if( lss != ERROR_SUCCESS )
return HRESULT_FROM_WIN32( lss );
if( nChars == 0 )
{
rdi = L"";
return S_FALSE;
}
lss = k.QueryStringValue( name, rdi.GetBufferSetLength( nChars ), &nChars );
rdi.ReleaseBuffer();
if( lss != ERROR_SUCCESS )
return HRESULT_FROM_WIN32( lss );
return S_OK;
}
using Whisper::eModelImplementation;
}
HRESULT AppState::startup()
{
HRESULT hr = CoInitializeEx( nullptr, COINIT_MULTITHREADED );
if( FAILED( hr ) )
{
reportFatalError( "CoInitializeEx failed", hr );
return hr;
}
coInit = true;
LSTATUS lss = registryKey.Create( regKeyRoot, regKey );
if( lss != ERROR_SUCCESS )
{
hr = HRESULT_FROM_WIN32( lss );
reportFatalError( "Unable to open the registry key", hr );
return hr;
}
INITCOMMONCONTROLSEX init;
init.dwSize = sizeof( init );
init.dwICC = ICC_LINK_CLASS | ICC_PROGRESS_CLASS | ICC_STANDARD_CLASSES | ICC_TAB_CLASSES;
const BOOL icc = InitCommonControlsEx( &init );
if( !icc )
{
reportFatalError( "InitCommonControlsEx failed", HRESULT_FROM_WIN32( GetLastError() ) );
return E_FAIL;
}
hr = initMediaFoundation( &mediaFoundation );
if( FAILED( hr ) )
{
reportFatalError( "Unable to initialize Media Foundation runtime", hr );
return hr;
}
hr = console.initialize();
if( FAILED( hr ) )
{
reportFatalError( "Unable to initialize logging", hr );
return hr;
}
hr = CircleIndicator::registerClass();
if( FAILED( hr ) )
{
reportFatalError( "Unable to register custom controls", hr );
return hr;
}
appIcon.LoadIcon( IDI_WHISPERDESKTOP );
return S_OK;
}
AppState::~AppState()
{
if( coInit )
{
CoUninitialize();
coInit = false;
}
}
HRESULT AppState::findModelSource()
{
CHECK( readString( registryKey, regValPath, source.path ) );
{
CAtlFile file;
CHECK( file.Create( source.path, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING ) );
ULONGLONG len;
CHECK( file.GetSize( len ) );
source.sizeInBytes = len;
}
CString impl;
CHECK( readString( registryKey, regValImpl, impl ) );
CHECK( implParse( impl, source.impl ) );
source.found = true;
return S_OK;
}
HRESULT AppState::saveModelSource()
{
LSTATUS lss = registryKey.SetStringValue( regValPath, source.path );
if( lss != ERROR_SUCCESS )
return HRESULT_FROM_WIN32( lss );
LPCTSTR impl = implString( source.impl );
if( nullptr == impl )
return E_INVALIDARG;
lss = registryKey.SetStringValue( regValImpl, impl );
if( lss != ERROR_SUCCESS )
return HRESULT_FROM_WIN32( lss );
return S_OK;
}
uint32_t AppState::languageRead()
{
DWORD dw;
LSTATUS lss = registryKey.QueryDWORDValue( regValLang, dw );
if( lss == ERROR_SUCCESS )
return dw;
return UINT_MAX;
}
void AppState::languageWrite( uint32_t key )
{
registryKey.SetDWORDValue( regValLang, key );
}
CString AppState::stringLoad( LPCTSTR name )
{
CString res;
readString( registryKey, name, res );
return res;
}
void AppState::stringStore( LPCTSTR name, LPCTSTR value )
{
registryKey.SetStringValue( name, value );
}
uint32_t AppState::dwordLoad( LPCTSTR name, uint32_t fallback )
{
DWORD dw;
LSTATUS lss = registryKey.QueryDWORDValue( name, dw );
if( lss == ERROR_SUCCESS )
return dw;
return fallback;
}
void AppState::dwordStore( LPCTSTR name, uint32_t value )
{
registryKey.SetDWORDValue( name, value );
}
void AppState::lastScreenSave( HRESULT code )
{
dwordStore( regValLastScreen, (uint32_t)code );
}
HRESULT AppState::lastScreenLoad()
{
return (HRESULT)dwordLoad( regValLastScreen, SCREEN_TRANSCRIBE );
}
void AppState::setupIcon( CWindow* wnd )
{
HICON ic = appIcon;
if( nullptr != ic )
{
wnd->SendMessage( WM_SETICON, ICON_SMALL, (LPARAM)ic );
wnd->SendMessage( WM_SETICON, ICON_BIG, (LPARAM)ic );
}
}
uint32_t AppState::gpuFlagsLoad()
{
return dwordLoad( regValGpuFlags, 0 );
}
void AppState::gpuFlagsStore( uint32_t flags )
{
if( 0 == flags )
registryKey.DeleteValue( regValGpuFlags );
else
dwordStore( regValGpuFlags, flags );
}
bool AppState::boolLoad( LPCTSTR name )
{
return dwordLoad( name, 0 ) != 0;
}
void AppState::boolStore( LPCTSTR name, bool val )
{
if( val )
dwordStore( name, 1 );
else
registryKey.DeleteValue( name );
}
|