Debian Codegraph @

src/win32/d2cs_winmain.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2004 CreepLord ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #ifdef WIN32_GUI
20
21 #include "common/setup_before.h"
22
23 #include <cwchar>
24
25 #include <windows.h>
26 #include <windowsx.h>
27 #include <richedit.h>
28 #include <process.h>
29 #include "d2cs_resource.h"
30 #include "common/gui_printf.h"
31 #include "common/eventlog.h"
32 #include "d2cs/version.h"
33 #include "d2cs/handle_signal.h"
34 #include "d2cs/cmdline.h"
35 #include "service.h"
36 #include "winmain.h"
37 #include "console_output.h"
38
39 #include "common/setup_after.h"
40
41 #define WM_SHELLNOTIFY (WM_USER+1)
42
43 extern int app_main(int argc, char **argv); /* d2cs main function in d2cs/main.c */
44
45 namespace pvpgn
46 {
47
48 extern HWND ghwndConsole; /* hwnd for eventlog output */
49
50 namespace d2cs
51 {
52
53 static void KillTrayIcon(HWND hwnd);
54 static int OnShellNotify(HWND hwnd, int uID, int uMessage);
55 static void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
56 static BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct);
57 static void OnClose(HWND hwnd);
58 static void OnSize(HWND hwnd, UINT state, int cx, int cy);
59
60 LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
61 BOOL CALLBACK DlgProcAbout(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
62
63 static int gui_run = TRUE; /* default state: run gui */
64 static int d2cs_run = TRUE; /* default state: run d2cs */
65 static int d2cs_running = FALSE; /* currect state: not running */
66
67 int fprintf(FILE *stream, const char *format, ...)
68 {
69 int temp = 0;
70 va_list args;
71 va_start(args, format);
72
73 if (stream == stderr || stream == stdout)
74 {
75 char buf[1024] = {};
76 std::vsnprintf(buf, sizeof buf, format, args);
77 gui_lvprintf(eventlog_level_error, "{}", buf);
78 temp = 1;
79 }
80 else
81 temp = vfprintf(stream, format, args);
82
83 va_end(args);
84 return temp;
85 }
86
87 static void KillTrayIcon(HWND hwnd)
88 {
89 NOTIFYICONDATA dta;
90
91 dta.cbSize = sizeof(NOTIFYICONDATA);
92 dta.hWnd = hwnd;
93 dta.uID = ID_TRAY;
94 dta.uFlags = 0;
95 Shell_NotifyIcon(NIM_DELETE, &dta);
96 }
97
98 LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
99 {
100 switch (uMsg) {
101 HANDLE_MSG(hwnd, WM_CREATE, OnCreate);
102 HANDLE_MSG(hwnd, WM_SIZE, OnSize);
103 HANDLE_MSG(hwnd, WM_COMMAND, OnCommand);
104 HANDLE_MSG(hwnd, WM_CLOSE, OnClose);
105 case WM_SHELLNOTIFY:
106 return OnShellNotify(hwnd, wParam, lParam);
107 }
108 return DefWindowProc(hwnd, uMsg, wParam, lParam);
109 }
110
111 static BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
112 {
113 ghwndConsole = CreateWindowEx(WS_EX_CLIENTEDGE, RICHEDIT_CLASS, NULL,
114 WS_CHILD | WS_VISIBLE | ES_READONLY | ES_MULTILINE |
115 WS_VSCROLL | WS_HSCROLL | ES_NOHIDESEL,
116 0, 0,
117 0, 0, hwnd, 0,
118 0, NULL);
119
120 if (!ghwndConsole)
121 return FALSE;
122
123 return TRUE;
124 }
125
126 static void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
127 {
128 switch (id) {
129 case ID_RESTORE:
130 OnShellNotify(hwnd, ID_TRAY, WM_LBUTTONDBLCLK);
131 break;
132 case ID_START_D2CS:
133 d2cs::fprintf(stderr, "Sending Start Signal to d2cs\n");
134 d2cs_run = TRUE;
135 break;
136 case ID_SHUTDOWN_D2CS:
137 d2cs::fprintf(stderr, "Sending Shutdown Signal to d2cs\n");
138 d2cs_run = FALSE;
139 signal_quit_wrapper();
140 break;
141 case ID_RESTART_D2CS:
142 d2cs::fprintf(stderr, "Sending Restart Signal To d2cs\n");
143 d2cs_run = TRUE;
144 signal_quit_wrapper();
145 break;
146 case ID_EDITCONFIG_D2CS:
147 ShellExecuteW(NULL, L"open", L"notepad.exe", L"conf\\d2cs.conf", NULL, SW_SHOW);
148 break;
149 case ID_LOADCONFIG_D2CS:
150 d2cs::fprintf(stderr, "Sending Reload Config Signal To d2cs\n");
151 signal_reload_config_wrapper();
152 break;
153 case ID_LADDER_LOAD:
154 d2cs::fprintf(stderr, "Sending Reload Ladder Signal To d2cs\n");
155 signal_load_ladder_wrapper();
156 break;
157 case ID_RESTART_D2GS:
158 d2cs::fprintf(stderr, "Sending Restart d2gs Signal To d2cs\n");
159 signal_restart_d2gs_wrapper();
160 break;
161 case ID_EXIT:
162 OnClose(hwnd);
163 break;
164 case ID_CLEAR:
165 SendMessage(ghwndConsole, WM_SETTEXT, 0, 0);
166 break;
167 case ID_ABOUT:
168 DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_ABOUT_BOX), hwnd, (DLGPROC)DlgProcAbout);
169 break;
170 }
171 }
172
173 static int OnShellNotify(HWND hwnd, int uID, int uMessage)
174 {
175 if (uID == ID_TRAY) {
176 if (uMessage == WM_LBUTTONDBLCLK) {
177 if (!IsWindowVisible(hwnd))
178 ShowWindow(hwnd, SW_RESTORE);
179
180 SetForegroundWindow(hwnd);
181 }
182 }
183 return 0;
184 }
185
186 static void OnClose(HWND hwnd)
187 {
188 d2cs::fprintf(stderr, "Sending Exit Signal To d2cs\n");
189 gui_run = FALSE;
190 d2cs_run = FALSE;
191 signal_exit_wrapper();
192 }
193
194 static void OnSize(HWND hwnd, UINT state, int cx, int cy)
195 {
196 if (state == SIZE_MINIMIZED)
197 {
198 NOTIFYICONDATAW dta = {};
199 dta.cbSize = sizeof(NOTIFYICONDATAW);
200 dta.hWnd = hwnd;
201 dta.uID = ID_TRAY;
202 dta.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
203 dta.uCallbackMessage = WM_SHELLNOTIFY;
204 dta.hIcon = LoadIconW(GetWindowInstance(hwnd), MAKEINTRESOURCEW(ID_ICON1));
205 std::swprintf(dta.szTip, sizeof dta.szTip / sizeof *dta.szTip, L"D2CS Version " D2CS_VERSION_STRING);
206
207 Shell_NotifyIconW(NIM_ADD, &dta);
208 ShowWindow(hwnd, SW_HIDE);
209 return;
210 }
211
212 MoveWindow(ghwndConsole, 0, 0, cx, cy, TRUE);
213 }
214
215 BOOL CALLBACK DlgProcAbout(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
216 {
217 switch (uMsg) {
218 case WM_CLOSE:
219 EndDialog(hwnd, TRUE);
220 return TRUE;
221 case WM_INITDIALOG:
222 return TRUE;
223 case WM_COMMAND:
224 switch ((int)wParam) {
225 case IDOK:
226 EndDialog(hwnd, TRUE);
227 return TRUE;
228 }
229 }
230 return FALSE;
231 }
232
233 #define EXIT_ERROR -1
234 #define EXIT_OK 0
235 #define EXIT_SERVICE 1
236
237 static void d2cs(void * dummy)
238 {
239 switch (app_main(__argc, __argv))
240 {
241 case EXIT_SERVICE:
242 gui_run = FALSE; /* close gui */
243 case EXIT_ERROR:
244 d2cs_run = FALSE; /* don't restart */
245 case EXIT_OK:
246 ; /* do nothing */
247 }
248
249 d2cs::fprintf(stderr, "Server Stopped\n");
250 d2cs_running = FALSE;
251 }
252
253 }
254
255 }
256
257 using namespace pvpgn;
258 using namespace pvpgn::d2cs;
259
260 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
261 LPSTR lpszCmdLine, int nCmdShow)
262 {
263 HWND hwnd;
264 MSG msg;
265 Console console;
266
267 if (cmdline_load(__argc, __argv) != 1) {
268 return -1;
269 }
270
271 if (cmdline_get_console()){
272 console.RedirectIOToConsole();
273 return app_main(__argc, __argv);
274 }
275
276 if (LoadLibraryW(L"RichEd20.dll") == NULL)
277 {
278 return -1;
279 }
280
281 WNDCLASSEXW wc = {};
282 wc.cbSize = sizeof(WNDCLASSEXW);
283 wc.style = 0;
284 wc.lpfnWndProc = WndProc;
285 wc.cbClsExtra = 0;
286 wc.cbWndExtra = sizeof(LPVOID);
287 wc.hInstance = hInstance;
288 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(ID_ICON1));
289 wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
290 wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
291 wc.lpszMenuName = MAKEINTRESOURCEW(ID_MENU);
292 wc.lpszClassName = L"BnetWndClass";
293
294 if (!RegisterClassExW(&wc))
295 RegisterClassW((LPWNDCLASS)&wc.style);
296
297 hwnd = CreateWindowExW(0L, L"BnetWndClass", L"Diablo II Character Server",
298 WS_OVERLAPPEDWINDOW,
299 CW_USEDEFAULT, CW_USEDEFAULT,
300 CW_USEDEFAULT, CW_USEDEFAULT,
301 NULL,
302 LoadMenuW(hInstance, MAKEINTRESOURCEW(ID_MENU)),
303 hInstance, NULL);
304
305 if (hwnd) {
306 ShowWindow(hwnd, nCmdShow);
307 UpdateWindow(hwnd);
308 }
309
310 while (GetMessageW(&msg, NULL, 0, 0))
311 {
312 TranslateMessage(&msg);
313 DispatchMessageW(&msg);
314
315 if (!d2cs_running && d2cs_run && gui_run) {
316 d2cs_running = TRUE;
317 _beginthread(pvpgn::d2cs::d2cs, 0, NULL);
318 }
319
320 if (!gui_run && !d2cs_running) {
321 KillTrayIcon(hwnd);
322 exit(0);
323 }
324 }
325 return ((int)msg.wParam);
326 }
327
328 #endif