| 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 "d2dbs_resource.h" |
| 30 | #include "common/gui_printf.h" |
| 31 | #include "common/eventlog.h" |
| 32 | #include "d2dbs/version.h" |
| 33 | #include "d2dbs/handle_signal.h" |
| 34 | #include "d2dbs/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); /* d2dbs main function in d2dbs/main.c */ |
| 44 | |
| 45 | namespace pvpgn |
| 46 | { |
| 47 | |
| 48 | extern HWND ghwndConsole; /* hwnd for eventlog output */ |
| 49 | |
| 50 | namespace d2dbs |
| 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 d2dbs_run = TRUE; /* default state: run d2dbs */ |
| 65 | static int d2dbs_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_D2DBS: |
| 133 | d2dbs::fprintf(stderr, "Sending Start Signal to d2dbs\n"); |
| 134 | d2dbs_run = TRUE; |
| 135 | break; |
| 136 | case ID_SHUTDOWN_D2DBS: |
| 137 | d2dbs::fprintf(stderr, "Sending Shutdown Signal to d2dbs\n"); |
| 138 | d2dbs_run = FALSE; |
| 139 | d2dbs_signal_quit_wrapper(); |
| 140 | break; |
| 141 | case ID_RESTART_D2DBS: |
| 142 | d2dbs::fprintf(stderr, "Sending Restart Signal To d2dbs\n"); |
| 143 | d2dbs_run = TRUE; |
| 144 | d2dbs_signal_quit_wrapper(); |
| 145 | break; |
| 146 | case ID_EDITCONFIG_D2DBS: |
| 147 | ShellExecuteW(nullptr, L"open", L"notepad.exe", L"conf\\d2dbs.conf", NULL, SW_SHOW); |
| 148 | break; |
| 149 | case ID_LOADCONFIG_D2DBS: |
| 150 | d2dbs::fprintf(stderr, "Sending Reload Config Signal To d2dbs\n"); |
| 151 | d2dbs_signal_reload_config_wrapper(); |
| 152 | break; |
| 153 | case ID_LADDER_SAVE: |
| 154 | d2dbs::fprintf(stderr, "Sending Save Ladder Signal To d2dbs\n"); |
| 155 | d2dbs_signal_save_ladder_wrapper(); |
| 156 | break; |
| 157 | case ID_EXIT: |
| 158 | OnClose(hwnd); |
| 159 | break; |
| 160 | case ID_CLEAR: |
| 161 | SendMessage(ghwndConsole, WM_SETTEXT, 0, 0); |
| 162 | break; |
| 163 | case ID_ABOUT: |
| 164 | DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_ABOUT_BOX), hwnd, (DLGPROC)DlgProcAbout); |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | static int OnShellNotify(HWND hwnd, int uID, int uMessage) |
| 170 | { |
| 171 | if (uID == ID_TRAY) { |
| 172 | if (uMessage == WM_LBUTTONDBLCLK) { |
| 173 | if (!IsWindowVisible(hwnd)) |
| 174 | ShowWindow(hwnd, SW_RESTORE); |
| 175 | |
| 176 | SetForegroundWindow(hwnd); |
| 177 | } |
| 178 | } |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static void OnClose(HWND hwnd) |
| 183 | { |
| 184 | d2dbs::fprintf(stderr, "Sending Exit Signal To d2dbs\n"); |
| 185 | gui_run = FALSE; |
| 186 | d2dbs_run = FALSE; |
| 187 | d2dbs_signal_exit_wrapper(); |
| 188 | } |
| 189 | |
| 190 | static void OnSize(HWND hwnd, UINT state, int cx, int cy) |
| 191 | { |
| 192 | if (state == SIZE_MINIMIZED) |
| 193 | { |
| 194 | NOTIFYICONDATAW dta = {}; |
| 195 | dta.cbSize = sizeof(NOTIFYICONDATAW); |
| 196 | dta.hWnd = hwnd; |
| 197 | dta.uID = ID_TRAY; |
| 198 | dta.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; |
| 199 | dta.uCallbackMessage = WM_SHELLNOTIFY; |
| 200 | dta.hIcon = LoadIconW(GetWindowInstance(hwnd), MAKEINTRESOURCEW(ID_ICON1)); |
| 201 | std::swprintf(dta.szTip, sizeof dta.szTip / sizeof *dta.szTip, L"D2DBS Version " D2DBS_VERSION_NUMBER); |
| 202 | |
| 203 | Shell_NotifyIconW(NIM_ADD, &dta); |
| 204 | ShowWindow(hwnd, SW_HIDE); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | MoveWindow(ghwndConsole, 0, 0, cx, cy, TRUE); |
| 209 | } |
| 210 | |
| 211 | BOOL CALLBACK DlgProcAbout(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) |
| 212 | { |
| 213 | switch (uMsg) { |
| 214 | case WM_CLOSE: |
| 215 | EndDialog(hwnd, TRUE); |
| 216 | return TRUE; |
| 217 | case WM_INITDIALOG: |
| 218 | return TRUE; |
| 219 | case WM_COMMAND: |
| 220 | switch ((int)wParam) { |
| 221 | case IDOK: |
| 222 | EndDialog(hwnd, TRUE); |
| 223 | return TRUE; |
| 224 | } |
| 225 | } |
| 226 | return FALSE; |
| 227 | } |
| 228 | |
| 229 | #define EXIT_ERROR -1 |
| 230 | #define EXIT_OK 0 |
| 231 | #define EXIT_SERVICE 1 |
| 232 | |
| 233 | static void d2dbs(void * dummy) |
| 234 | { |
| 235 | switch (app_main(__argc, __argv)) |
| 236 | { |
| 237 | case EXIT_SERVICE: |
| 238 | gui_run = FALSE; /* close gui */ |
| 239 | case EXIT_ERROR: |
| 240 | d2dbs_run = FALSE; /* don't restart */ |
| 241 | case EXIT_OK: |
| 242 | ; /* do nothing */ |
| 243 | } |
| 244 | |
| 245 | d2dbs::fprintf(stderr, "Server Stopped\n"); |
| 246 | d2dbs_running = FALSE; |
| 247 | } |
| 248 | |
| 249 | } |
| 250 | |
| 251 | } |
| 252 | |
| 253 | using namespace pvpgn; |
| 254 | using namespace pvpgn::d2dbs; |
| 255 | |
| 256 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, |
| 257 | LPSTR lpszCmdLine, int nCmdShow) |
| 258 | { |
| 259 | HWND hwnd; |
| 260 | MSG msg; |
| 261 | |
| 262 | Console console; |
| 263 | |
| 264 | if (cmdline_load(__argc, __argv) != 1) { |
| 265 | return -1; |
| 266 | } |
| 267 | |
| 268 | if (cmdline_get_console()){ |
| 269 | console.RedirectIOToConsole(); |
| 270 | return app_main(__argc, __argv); |
| 271 | } |
| 272 | |
| 273 | if (LoadLibraryW(L"RichEd20.dll") == NULL) |
| 274 | { |
| 275 | return -1; |
| 276 | } |
| 277 | |
| 278 | WNDCLASSEXW wc = {}; |
| 279 | wc.cbSize = sizeof(WNDCLASSEXW); |
| 280 | wc.style = 0; |
| 281 | wc.lpfnWndProc = WndProc; |
| 282 | wc.cbClsExtra = 0; |
| 283 | wc.cbWndExtra = sizeof(LPVOID); |
| 284 | wc.hInstance = hInstance; |
| 285 | wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(ID_ICON1)); |
| 286 | wc.hCursor = LoadCursorW(NULL, IDC_ARROW); |
| 287 | wc.hbrBackground = (HBRUSH)COLOR_WINDOW; |
| 288 | wc.lpszMenuName = MAKEINTRESOURCEW(ID_MENU); |
| 289 | wc.lpszClassName = L"BnetWndClass"; |
| 290 | |
| 291 | if (!RegisterClassExW(&wc)) |
| 292 | RegisterClassW((LPWNDCLASS)&wc.style); |
| 293 | |
| 294 | hwnd = CreateWindowExW(0L, L"BnetWndClass", L"Diablo II DataBase Server", |
| 295 | WS_OVERLAPPEDWINDOW, |
| 296 | CW_USEDEFAULT, CW_USEDEFAULT, |
| 297 | CW_USEDEFAULT, CW_USEDEFAULT, |
| 298 | NULL, |
| 299 | LoadMenuW(hInstance, MAKEINTRESOURCEW(ID_MENU)), |
| 300 | hInstance, NULL); |
| 301 | |
| 302 | if (hwnd) { |
| 303 | ShowWindow(hwnd, nCmdShow); |
| 304 | UpdateWindow(hwnd); |
| 305 | } |
| 306 | |
| 307 | while (GetMessageW(&msg, NULL, 0, 0)) |
| 308 | { |
| 309 | TranslateMessage(&msg); |
| 310 | DispatchMessageW(&msg); |
| 311 | |
| 312 | if (!d2dbs_running && d2dbs_run && gui_run) |
| 313 | { |
| 314 | d2dbs_running = TRUE; |
| 315 | _beginthread(pvpgn::d2dbs::d2dbs, 0, NULL); |
| 316 | } |
| 317 | |
| 318 | if (!gui_run && !d2dbs_running) { |
| 319 | KillTrayIcon(hwnd); |
| 320 | exit(0); |
| 321 | } |
| 322 | } |
| 323 | return ((int)msg.wParam); |
| 324 | } |
| 325 | |
| 326 | #endif |