ReactOS  0.4.12-dev-43-g63b00d8
winmain.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Applications Manager
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * FILE: base/applications/rapps/winmain.cpp
5  * PURPOSE: Main program
6  * COPYRIGHT: Copyright 2009 Dmitry Chapyshev (dmitry@reactos.org)
7  * Copyright 2015 Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
8  * Copyright 2017 Alexander Shaposhnikov (sanchaez@reactos.org)
9  */
10 #include "rapps.h"
11 
12 #include "unattended.h"
13 
14 #include <atlcom.h>
15 
20 
22 
23 class CRAppsModule : public CComModule
24 {
25 public:
26 };
27 
28 BEGIN_OBJECT_MAP(ObjectMap)
30 
33 
35 {
36  if (bInitialize)
37  {
38  gModule.Init(ObjectMap, hInstance, NULL);
39  }
40  else
41  {
42  gModule.Term();
43  }
44 }
45 
47 {
48  ATL::CStringW szDownloadDir;
49  ZeroMemory(pSettingsInfo, sizeof(SETTINGS_INFO));
50 
51  pSettingsInfo->bSaveWndPos = TRUE;
52  pSettingsInfo->bUpdateAtStart = FALSE;
53  pSettingsInfo->bLogEnabled = TRUE;
54 
56  {
57  szDownloadDir.ReleaseBuffer();
58  if (!szDownloadDir.GetEnvironmentVariableW(L"SystemDrive"))
59  {
60  szDownloadDir = L"C:";
61  }
62  }
63  else
64  {
65  szDownloadDir.ReleaseBuffer();
66  }
67 
68  szDownloadDir += L"\\RAPPS Downloads";
70  _countof(pSettingsInfo->szDownloadDir),
71  szDownloadDir.GetString(),
72  szDownloadDir.GetLength() + 1);
73 
74  pSettingsInfo->bDelInstaller = FALSE;
75  pSettingsInfo->Maximized = FALSE;
76  pSettingsInfo->Left = CW_USEDEFAULT;
77  pSettingsInfo->Top = CW_USEDEFAULT;
78  pSettingsInfo->Width = 680;
79  pSettingsInfo->Height = 450;
80 }
81 
83 {
84  ATL::CRegKey RegKey;
85  DWORD dwSize;
86  BOOL bResult = FALSE;
87  if (RegKey.Open(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", KEY_READ) == ERROR_SUCCESS)
88  {
89  dwSize = sizeof(SettingsInfo);
90  bResult = (RegKey.QueryBinaryValue(L"Settings", (PVOID) &SettingsInfo, &dwSize) == ERROR_SUCCESS);
91 
92  RegKey.Close();
93  }
94 
95  return bResult;
96 }
97 
99 {
100  WINDOWPLACEMENT wp;
101  ATL::CRegKey RegKey;
102 
103  if (SettingsInfo.bSaveWndPos)
104  {
105  wp.length = sizeof(wp);
106  GetWindowPlacement(hwnd, &wp);
107 
108  SettingsInfo.Left = wp.rcNormalPosition.left;
109  SettingsInfo.Top = wp.rcNormalPosition.top;
110  SettingsInfo.Width = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
111  SettingsInfo.Height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
112  SettingsInfo.Maximized = (wp.showCmd == SW_MAXIMIZE
113  || (wp.showCmd == SW_SHOWMINIMIZED
114  && (wp.flags & WPF_RESTORETOMAXIMIZED)));
115  }
116 
117  if (RegKey.Create(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", NULL,
119  {
120  RegKey.SetBinaryValue(L"Settings", (const PVOID) &SettingsInfo, sizeof(SettingsInfo));
121  RegKey.Close();
122  }
123 }
124 
125 INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, INT nShowCmd)
126 {
127  LPCWSTR szWindowClass = L"ROSAPPMGR";
128  HANDLE hMutex;
129  HACCEL KeyBrd;
130  MSG Msg;
131  BOOL bIsFirstLaunch;
132 
133  InitializeAtlModule(hInstance, TRUE);
134 
136  {
138  }
139 
140  hInst = hInstance;
141 
142  hMutex = CreateMutexW(NULL, FALSE, szWindowClass);
143  if ((!hMutex) || (GetLastError() == ERROR_ALREADY_EXISTS))
144  {
145  /* If already started, it is found its window */
146  HWND hWindow = FindWindowW(szWindowClass, NULL);
147 
148  /* Activate window */
149  ShowWindow(hWindow, SW_SHOWNORMAL);
150  SetForegroundWindow(hWindow);
151  return 1;
152  }
153  bIsFirstLaunch = !LoadSettings();
154  if (bIsFirstLaunch)
155  {
156  FillDefaultSettings(&SettingsInfo);
157  }
158 
159  InitLogs();
161 
162  // skip window creation if there were some keys
164  {
165  if (SettingsInfo.bUpdateAtStart || bIsFirstLaunch)
167 
168  hMainWnd = CreateMainWindow();
169 
170  if (hMainWnd)
171  {
172  /* Maximize it if we must */
173  ShowWindow(hMainWnd, ((SettingsInfo.bSaveWndPos && SettingsInfo.Maximized) ? SW_MAXIMIZE : nShowCmd));
174  UpdateWindow(hMainWnd);
175 
176  /* Load the menu hotkeys */
178 
179  /* Message Loop */
180  while (GetMessageW(&Msg, NULL, 0, 0))
181  {
182  if (!TranslateAcceleratorW(hMainWnd, KeyBrd, &Msg))
183  {
184  TranslateMessage(&Msg);
185  DispatchMessageW(&Msg);
186  }
187  }
188  }
189  }
190 
191  if (hMutex)
192  CloseHandle(hMutex);
193 
194  InitializeAtlModule(hInstance, FALSE);
195 
196  return 0;
197 }
int GetLength() const
Definition: atlsimpstr.h:356
LONG QueryBinaryValue(LPCTSTR pszValueName, void *pValue, ULONG *pnBytes)
Definition: atlbase.h:1146
BOOL bUpdateAtStart
Definition: winmain.h:8
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
LANGID WINAPI GetUserDefaultUILanguage(void)
Definition: lang.c:561
BOOL WINAPI TranslateMessage(_In_ const MSG *)
#define TRUE
Definition: types.h:120
static VOID InitializeAtlModule(HINSTANCE hInstance, BOOL bInitialize)
Definition: winmain.cpp:34
#define CloseHandle
Definition: compat.h:398
#define LAYOUT_RTL
Definition: wingdi.h:1352
#define ERROR_SUCCESS
Definition: deptool.c:10
#define KEY_READ
Definition: nt_native.h:1023
#define _countof(array)
Definition: fontsub.cpp:30
#define HKEY_CURRENT_USER
Definition: winreg.h:11
TW_UINT32 TW_UINT16 TW_UINT16 MSG
Definition: twain.h:1827
#define SUBLANG_DEFAULT
Definition: nls.h:168
INT Height
Definition: winmain.h:17
CAtlWinModule gWinModule
Definition: winmain.cpp:32
BOOL WINAPI UpdateWindow(_In_ HWND)
INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, INT nShowCmd)
Definition: winmain.cpp:125
LONG top
Definition: windef.h:297
LONG SetBinaryValue(LPCTSTR pszValueName, const void *pValue, ULONG nBytes)
Definition: atlbase.h:1261
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define ZeroMemory
Definition: winbase.h:1635
HINSTANCE hInst
Definition: winmain.cpp:17
#define WPF_RESTORETOMAXIMIZED
Definition: winuser.h:2486
LONG left
Definition: windef.h:296
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
#define SW_SHOWMINIMIZED
Definition: winuser.h:765
LONG right
Definition: windef.h:298
BOOL Maximized
Definition: winmain.h:13
LONG Close()
Definition: atlbase.h:1061
int32_t INT
Definition: typedefs.h:56
#define LANG_HEBREW
Definition: nls.h:67
void Term()
Definition: atlbase.h:829
struct @1571 Msg[]
WCHAR szDownloadDir[MAX_PATH]
Definition: winmain.h:10
HRESULT Init(_ATL_OBJMAP_ENTRY *p, HINSTANCE, const GUID *plibid)
Definition: atlbase.h:806
const TCHAR szWindowClass[]
Definition: magnifier.c:29
HINSTANCE hInstance
Definition: charmap.c:20
VOID InitLogs()
Definition: misc.cpp:201
void ReleaseBuffer(_In_ int nNewLength=-1)
Definition: atlsimpstr.h:372
LRESULT WINAPI DispatchMessageW(_In_ const MSG *)
VOID FillDefaultSettings(PSETTINGS_INFO pSettingsInfo)
Definition: winmain.cpp:46
VOID SaveSettings(HWND hwnd)
Definition: winmain.cpp:98
LPWSTR WINAPI GetCommandLineW(VOID)
Definition: proc.c:2043
#define CW_USEDEFAULT
Definition: winuser.h:225
HACCEL WINAPI LoadAcceleratorsW(_In_opt_ HINSTANCE, _In_ LPCWSTR)
smooth NULL
Definition: ftsmooth.c:416
HANDLE hMutex
Definition: mutex.c:11
BOOL WINAPI GetWindowPlacement(_In_ HWND, _Inout_ WINDOWPLACEMENT *)
BOOL WINAPI SetProcessDefaultLayout(DWORD dwDefaultLayout)
Definition: window.c:1686
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1057
#define KEY_WRITE
Definition: nt_native.h:1031
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL UseCmdParameters(LPWSTR lpCmdLine)
Definition: unattended.cpp:16
HWND WINAPI FindWindowW(_In_opt_ LPCWSTR, _In_opt_ LPCWSTR)
#define MAX_PATH
Definition: compat.h:26
PCXSTR GetString() const
Definition: atlsimpstr.h:361
LONG Open(HKEY hKeyParent, LPCTSTR lpszKeyName, REGSAM samDesired=KEY_READ|KEY_WRITE)
Definition: atlbase.h:1078
unsigned long DWORD
Definition: ntddk_ex.h:95
#define HOTKEYS
Definition: resource.h:208
int WINAPI TranslateAcceleratorW(_In_ HWND, _In_ HACCEL, _In_ LPMSG)
static const WCHAR L[]
Definition: oid.c:1087
LONG Create(HKEY hKeyParent, LPCTSTR lpszKeyName, LPTSTR lpszClass=REG_NONE, DWORD dwOptions=REG_OPTION_NON_VOLATILE, REGSAM samDesired=KEY_READ|KEY_WRITE, LPSECURITY_ATTRIBUTES lpSecAttr=NULL, LPDWORD lpdwDisposition=NULL)
Definition: atlbase.h:1099
#define WINAPI
Definition: msvc.h:20
static BOOL ForceUpdateAppsDB()
Definition: available.cpp:317
INT SelectedEnumType
Definition: winmain.cpp:18
#define SW_SHOWNORMAL
Definition: winuser.h:764
BOOL bLogEnabled
Definition: winmain.h:9
static BOOL LoadSettings()
Definition: winmain.cpp:82
HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexW(IN LPSECURITY_ATTRIBUTES lpMutexAttributes OPTIONAL, IN BOOL bInitialOwner, IN LPCWSTR lpName OPTIONAL)
Definition: synch.c:522
static void __cdecl CopyChars(_Out_writes_to_(nDestLen, nChars) XCHAR *pchDest, _In_ size_t nDestLen, _In_reads_opt_(nChars) const XCHAR *pchSrc, _In_ int nChars)
Definition: atlsimpstr.h:423
BOOL WINAPI GetMessageW(_Out_ LPMSG, _In_opt_ HWND, _In_ UINT, _In_ UINT)
HWND hMainWnd
Definition: winmain.cpp:16
BOOL bDelInstaller
Definition: winmain.h:11
RECT rcNormalPosition
Definition: winuser.h:3237
#define CSIDL_PERSONAL
Definition: shlobj.h:1993
HRESULT WINAPI SHGetFolderPathW(HWND hwndOwner, int nFolder, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath)
Definition: shellpath.c:2085
BOOL bSaveWndPos
Definition: winmain.h:7
#define MAKELANGID(p, s)
Definition: nls.h:15
BOOL WINAPI SetForegroundWindow(_In_ HWND)
LONG bottom
Definition: windef.h:299
#define BEGIN_OBJECT_MAP(x)
Definition: atlcom.h:649
ATL::CStringW szSearchPattern
Definition: winmain.cpp:21
VOID WINAPI InitCommonControls(void)
Definition: commctrl.c:863
#define MAKEINTRESOURCEW(i)
Definition: winuser.h:582
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define SW_MAXIMIZE
Definition: winuser.h:766
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
HWND CreateMainWindow(LPCTSTR lpCaption, int nCmdShow)
Definition: mainwnd.c:920
CRAppsModule gModule
Definition: winmain.cpp:31
#define END_OBJECT_MAP()
Definition: atlcom.h:651
SETTINGS_INFO SettingsInfo
Definition: winmain.cpp:19