-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
407 lines (348 loc) · 13.5 KB
/
main.c
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/********************************************************************************
* This file is part of FSTimeSync. *
* *
* FSTimeSync is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* FSTimeSync is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with FSTimeSync. If not, see <http://www.gnu.org/licenses/>. *
********************************************************************************/
/* Program timer, mainly controls responsiveness */
#define SLEEP_DURATION 100
#include "globalinc.h"
#include "debug.h"
#include "main.h"
#include "gui.h"
#include "sync.h"
#include "registry.h"
#include <windows.h>
#include <commctrl.h>
#include "FSUIPC_User.h"
/* Globals */
SyncOptions_t Settings;
SyncOptions_t Defaults = {0,0,1,1,1,10,0,0,1,1,0,MAKEWORD(0x53,(HOTKEYF_CONTROL | HOTKEYF_SHIFT)),MAKEWORD(0x4D,(HOTKEYF_CONTROL | HOTKEYF_SHIFT))}; /* Default options */
SyncStats_t Stats;
CRITICAL_SECTION ProgramDataCS;
static RuntimeVals_t RuntimeVals;
static CRITICAL_SECTION ProgramControlCS;
/* Version info */
Version_t Ver = {__TIME__,__DATE__,"v1.0"};
/* Locals */
static DWORD ReUpdateGUI; /* Perform full GUI update at the end of the loop iteration */
static DWORD CancelSync; /* Skip any sync attempt */
static DWORD SimStatusPrev;
static FSState_t SimStatePrev;
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) {
/* Mutex to prevent multiple instances of this application */
if (CreateMutex(NULL,FALSE,"_FSTimeSyncMutex_") && GetLastError() == ERROR_ALREADY_EXISTS) {
MessageBox(NULL,"An instance of FS Time Sync is already running.","FS Time Sync Error",MB_OK | MB_ICONERROR);
return 100;
}
/* Initialize the critical sections and perform indepenedant modules startup */
InitializeCriticalSection(&ProgramDataCS);
InitializeCriticalSection(&ProgramControlCS);
DebugStartup();
RegistryStartup();
SyncStartup();
GUIStartup();
/* Load the settings from the registry */
RegistryReadSettings(&Settings);
/* Set the operating mode (manual or auto) based on the setting and next sync */
SetRTVal(FST_AUTOMODE,Settings.AutoOnStart);
Stats.SyncNext = time(NULL)+Settings.AutoSyncInterval;
Stats.SyncInterval = Settings.AutoSyncInterval;
/* Start the GUI Thread, based on the Start Minimized setting */
if(Settings.StartMinimized == 1)
GUIStartThread(SW_MINIMIZE);
else
GUIStartThread(nCmdShow);
/* Main program loop */
while(!GetRTVal(FST_QUIT)) {
time_t CurrentTime;
struct tm* systm;
int nResult;
time_t SimUTCDest;
/* Reset CancelSync */
if(CancelSync)
CancelSync = 0;
EnterCriticalSection(&ProgramDataCS);
Stats.SimStatus = SyncGetConStatus(); /* Get updated state of the connection */
CurrentTime = time(NULL); /* Saves multiple expensive calls to time() */
/* System UTC time */
systm = gmtime(&CurrentTime);
if((Stats.SysUTCTime = mktime(systm)) == (time_t)-1) {
debuglog(DEBUG_ERROR,"mktime failed making system UTC time!\n");
Stats.SysUTCTime = CurrentTime; /* If we can't get UTC, then lets have local */
}
if(Settings.SystemUTCCorrectionState)
Stats.SysUTCTime += Settings.SystemUTCCorrection*60;
/* Only proceed if sim is connected */
if(Stats.SimStatus) {
/* Get the simulator's UTC time */
SyncGetFSTimestamp(&Stats.SimUTCTime);
/* Get some information what the simulator is doing */
SyncGetState(&Stats.SimState,Settings.FSMenuDetection);
/* Check to see if FSMenuDetection is not set to 0 (skip) */
if(Settings.FSMenuDetection) {
/* Cancel any possible sync if simulator is in a menu or dialog*/
if(!Stats.SimState.SimInFlight)
CancelSync = 1;
/* Sim in flight state might have changed, need to update the GUI */
if(Stats.SimState.SimInFlight != SimStatePrev.SimInFlight)
ReUpdateGUI = 1;
}
/* Check for the setting to sync if paused */
if(Settings.NoSyncPaused) {
/* Cancel any possible sync if simulator is paused */
if(Stats.SimState.SimPaused)
CancelSync = 1;
/* Sim pause state might have changed, need to update the GUI */
if(Stats.SimState.SimPaused != SimStatePrev.SimPaused)
ReUpdateGUI = 1;
}
/* Check the no sync when sim rate other than 1x setting */
if(Settings.NoSyncSimRate) {
/* Cancel any possible sync if simulator is at any other rate than 1x */
if(Stats.SimState.SimRate != 256)
CancelSync = 1;
/* Sim rate might have changed, need to update the GUI */
if(Stats.SimState.SimRate != SimStatePrev.SimRate)
ReUpdateGUI = 1;
}
if(GetRTVal(FST_AUTOMODE)) {
/* Check for no sync, e.g. paused or sim rate */
if(CancelSync) {
Stats.SyncNext = CurrentTime+Settings.AutoSyncInterval;
Stats.SyncInterval = Settings.AutoSyncInterval;
}
/* Check if switched to faster synch interval and there's too long to wait */
if((Stats.SyncNext-CurrentTime) > Settings.AutoSyncInterval) {
Stats.SyncNext = CurrentTime+Settings.AutoSyncInterval;
Stats.SyncInterval = Settings.AutoSyncInterval; /* Progress bar is drawn based on current synch, not next one */
}
/* Check if switched to slower interval */
if(Stats.SyncInterval < Settings.AutoSyncInterval) {
Stats.SyncNext = CurrentTime+Settings.AutoSyncInterval;
Stats.SyncInterval = Settings.AutoSyncInterval; /* Progress bar is drawn based on current synch, not next one */
}
/* Check if it's time to sync and if time, sync*/
if(Stats.SyncNext <= CurrentTime) {
/* Use system UTC time */
SimUTCDest = Stats.SysUTCTime;
if(nResult = SyncGo(SimUTCDest,Settings.FSSyncMethod)) {
Stats.SyncNext = time(&CurrentTime)+Settings.AutoSyncInterval; /* Sync takes time, update the time on the way */
Stats.SyncLast = CurrentTime;
Stats.SyncInterval = Settings.AutoSyncInterval;
Stats.SyncLastModified = 1;
} else {
Stats.SyncNext = CurrentTime+3;
Stats.SyncInterval = 3;
}
}
} else {
/* Manual mode */
Stats.SyncInterval = 0;
if(GetRTVal(FST_SYNCNOW)) {
SetRTVal(FST_SYNCNOW, 0); /* Clear the event, regardless of success or failure */
if(!CancelSync) {
/* Use system UTC time */
SimUTCDest = Stats.SysUTCTime;
if(SyncGo(SimUTCDest,Settings.FSSyncMethod)) {
Stats.SyncLast = time(&CurrentTime);
Stats.SyncLastModified = 1;
}
}
} /* Sync now */
} /* Mode */
} else { /* Sim not connected */
/* Connect to the sim */
Stats.SimStatus = SyncConnect(SIM_ANY);
if(Stats.SimStatus != SimStatusPrev)
ReUpdateGUI = 1; /* Force updating of the GUI */
/* Set up the affinity fix and\or the priority fix depending on settings */
if(Stats.SimStatus)
AffinityPriorityFix(Settings.EnableAffinityFix,Settings.EnablePriorityFix);
} /* SimStatus */
/* Copy the variables over the previous ones
so in next loop iteration we can know if something changed */
memcpy(&SimStatePrev,&Stats.SimState,sizeof(FSState_t));
SimStatusPrev = Stats.SimStatus;
/* Call GUIUpdate() (indirectly) if set to 1 */
/* This updates everything in the main window and calls update elements and update tray */
if(ReUpdateGUI) {
ReUpdateGUI = 0;
/* Signal GUI thread to call GUIUpdate */
PostMessage(hDummyWindow,FSTSGUI_WM_MAINMSG,FSTSGUI_GUIFULLUPDATE,0);
} else {
/* Updating the elements (such as progress bar) should happen at every loop iteration
If GUIUpdate() was called, it will also update the elements so no need to call it again.
But if it wasn't, we do it ourselves by telling GUI thread to update the elements */
/* Signal GUI thread to call GUIElementsUpdate */
PostMessage(hDummyWindow,FSTSGUI_WM_MAINMSG,FSTSGUI_GUIELEMUPDATE,0);
}
LeaveCriticalSection(&ProgramDataCS);
Sleep(SLEEP_DURATION);
} /* Loop end */
/* Stop the GUI thread, or at least wait for it to close */
GUIStopThread();
/* Disconnect from the simulator if it's still connected */
if(SyncGetConStatus())
SyncDisconnect();
/* Write settings to the registry */
RegistryWriteSettings(&Settings);
/* Delete the critical sections and perform complete shutdown */
DeleteCriticalSection(&ProgramDataCS);
DeleteCriticalSection(&ProgramControlCS);
GUIShutdown();
SyncShutdown();
RegistryShutdown();
DebugShutdown();
return 0;
}
/* Thread safe */
DWORD GetRTVal(int RTVal) {
DWORD dwRet;
EnterCriticalSection(&ProgramControlCS);
switch(RTVal) {
case FST_QUIT:
dwRet = RuntimeVals.bQuit;
break;
case FST_SYNCNOW:
dwRet = RuntimeVals.bSyncNow;
break;
case FST_AUTOMODE:
dwRet = RuntimeVals.bAutoMode;
break;
default:
dwRet = 0;
}
LeaveCriticalSection(&ProgramControlCS);
return dwRet;
}
/* Thread safe */
void SetRTVal(int RTVal, int NewValue) {
EnterCriticalSection(&ProgramControlCS);
switch(RTVal) {
case FST_QUIT:
RuntimeVals.bQuit = NewValue;
break;
case FST_SYNCNOW:
RuntimeVals.bSyncNow = NewValue;
break;
case FST_AUTOMODE:
RuntimeVals.bAutoMode = NewValue;
break;
}
LeaveCriticalSection(&ProgramControlCS);
}
/* Thread safe - doesn't care about the lock */
int HotkeysRegister(HWND hwnd, WORD ManSync, WORD OperModeSwitch) {
UINT ManSyncModifiers = 0;
UINT OperModeSwitchModifiers = 0;
UINT ManSyncVk;
UINT OperModeSwitchVk;
/* Perform conversion of the format we got from HKM_GETHOTKEY
To the format needed by RegisterHotkey() */
if (HOTKEYF_ALT & HIBYTE(ManSync))
ManSyncModifiers |= MOD_ALT;
if (HOTKEYF_CONTROL & HIBYTE(ManSync))
ManSyncModifiers |= MOD_CONTROL;
if (HOTKEYF_SHIFT & HIBYTE(ManSync))
ManSyncModifiers |= MOD_SHIFT;
ManSyncVk = LOBYTE(ManSync);
if (HOTKEYF_ALT & HIBYTE(OperModeSwitch))
OperModeSwitchModifiers |= MOD_ALT;
if (HOTKEYF_CONTROL & HIBYTE(OperModeSwitch))
OperModeSwitchModifiers |= MOD_CONTROL;
if (HOTKEYF_SHIFT & HIBYTE(OperModeSwitch))
OperModeSwitchModifiers |= MOD_SHIFT;
OperModeSwitchVk = LOBYTE(OperModeSwitch);
if(!(RegisterHotKey(hwnd, 443, ManSyncModifiers, ManSyncVk)))
debuglog(DEBUG_ERROR,"Failed registering manual sync hotkey\n");
if(!(RegisterHotKey(hwnd, 444, OperModeSwitchModifiers, OperModeSwitchVk)))
debuglog(DEBUG_ERROR,"Failed registering mode switch hotkey\n");
return 1;
}
/* Thread safe - doesn't care about the lock */
int HotkeysUnregister(HWND hwnd) {
if(!(UnregisterHotKey(hwnd, 443)))
debuglog(DEBUG_ERROR,"Failed unregistering manual sync hotkey\n");
if(!(UnregisterHotKey(hwnd, 444)))
debuglog(DEBUG_ERROR,"Failed unregistering mode switch hotkey\n");
return 1;
}
/* Thread safe - doesn't care about the lock */
static int AffinityPriorityFix(DWORD EnableAffinityFix, DWORD EnablePriorityFix) {
HWND hFShwnd;
DWORD nPriority;
DWORD nProcAffinity;
DWORD nSysAffinity;
DWORD nProcId;
HANDLE hFS;
if(EnableAffinityFix || EnablePriorityFix) {
/* Start by finding the window */
switch(FSUIPC_FS_Version) {
case SIM_FSX:
hFShwnd = FindWindow(NULL,"Microsoft Flight Simulator X");
break;
case SIM_FS2K4:
hFShwnd = FindWindow(NULL,"Microsoft Flight Simulator 2004 - A Century of Flight");
break;
case SIM_FS2K2:
hFShwnd = FindWindow(NULL,"Microsoft Flight Simulator 2002");
break;
case SIM_FS2K:
hFShwnd = FindWindow(NULL,"Microsoft Flight Simulator 2000");
break;
case SIM_FS98:
hFShwnd = FindWindow(NULL,"Microsoft Flight Simulator 98");
break;
default:
/* Couldn't find the window name, giving up on affinity or priority fix */
debuglog(DEBUG_WARNING,"Unknown FS, don't know what window to search\n");
return 0;
}
if(hFShwnd == NULL) {
debuglog(DEBUG_WARNING,"Failed finding the window for the connected FS\n");
return 0;
}
GetWindowThreadProcessId(hFShwnd,&nProcId);
if(!nProcId) {
debuglog(DEBUG_WARNING,"Failed getting FS process ID\n");
return 0;
}
hFS = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_SET_INFORMATION,FALSE,nProcId);
if(hFS == NULL) {
debuglog(DEBUG_WARNING,"Failed opening FS process!\n");
return 0;
}
/* Get FS priority class and copy it to our process */
if(EnablePriorityFix) {
if(!(nPriority = GetPriorityClass(hFS))) {
debuglog(DEBUG_WARNING,"Failed getting FS priority class!\n");
} else {
if(!SetPriorityClass(GetCurrentProcess(),nPriority))
debuglog(DEBUG_WARNING,"Failed setting priority class!\n");
}
}
/* Get FS affinity mask and copy it to our process */
if(EnableAffinityFix) {
if(!GetProcessAffinityMask(hFS,&nProcAffinity,&nSysAffinity)) {
debuglog(DEBUG_WARNING,"Failed getting FS affinity mask!\n");
} else {
if(!SetProcessAffinityMask(GetCurrentProcess(),nProcAffinity))
debuglog(DEBUG_WARNING,"Failed setting affinity mask!\n");
}
}
/* Finished with the FS process, let's close it */
CloseHandle(hFS);
}
return 1;
}