/[LeafOK_CVS]/pvpgn-1.7.4/src/win32/d2dbs_winmain.c
ViewVC logotype

Annotation of /pvpgn-1.7.4/src/win32/d2dbs_winmain.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Tue Jun 6 03:41:38 2006 UTC (19 years, 9 months ago) by sysadm
CVS Tags: pvpgn_1-7-4-0_MIL
Branch point for: GNU, MAIN
Content type: text/x-csrc
Initial revision

1 sysadm 1.1 /*
2     * Copyright (C) 2004 CreepLord (creeplord@pvpgn.org)
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     #include <windows.h>
23     #include <windowsx.h>
24     #include <richedit.h>
25     #include "d2dbs_resource.h"
26     #include "common/eventlog.h"
27     #include "d2dbs/version.h"
28     #include "common/setup_after.h"
29    
30     #define WM_SHELLNOTIFY (WM_USER+1)
31    
32     extern int server_main(int argc, char *argv[]); /* d2dbs main function in d2dbs/main.c */
33    
34     static void d2dbs(void * dummy); /* thread function for d2dbs */
35    
36     static void guiAddText(const char *str, COLORREF clr);
37     static void KillTrayIcon(HWND hwnd);
38     static int OnShellNotify(HWND hwnd, int uID, int uMessage);
39     static void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
40     static BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct);
41     static void OnClose(HWND hwnd);
42     static void OnSize(HWND hwnd, UINT state, int cx, int cy);
43    
44     LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
45     BOOL CALLBACK DlgProcAbout(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
46    
47     HWND ghwndConsole; /* hwnd for eventlog output */
48    
49     static int gui_run = TRUE; /* default state: run gui */
50     static int d2dbs_run = TRUE; /* default state: run d2dbs */
51     static int d2dbs_running = FALSE; /* currect state: not running */
52    
53     static void KillTrayIcon(HWND hwnd)
54     {
55     NOTIFYICONDATA dta;
56    
57     dta.cbSize = sizeof(NOTIFYICONDATA);
58     dta.hWnd = hwnd;
59     dta.uID = ID_TRAY;
60     dta.uFlags = 0;
61     Shell_NotifyIcon(NIM_DELETE, &dta);
62     }
63    
64     int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
65     LPSTR lpszCmdLine, int nCmdShow)
66     {
67     WNDCLASSEX wc;
68     HWND hwnd;
69     MSG msg;
70    
71     /* if running as a service skip starting the GUI and go straight to starting d2dbs */
72     if (__argc==2 && strcmp(__argv[1],"--service")==0)
73     {
74     Win32_ServiceRun();
75     return 1;
76     }
77    
78     LoadLibrary("RichEd20.dll");
79    
80     wc.cbSize = sizeof(WNDCLASSEX);
81     wc.style = 0;
82     wc.lpfnWndProc = WndProc;
83     wc.cbClsExtra = 0;
84     wc.cbWndExtra = sizeof(LPVOID);
85     wc.hInstance = hInstance;
86     wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(ID_ICON1));
87     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
88     wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
89     wc.lpszMenuName = MAKEINTRESOURCE(ID_MENU);
90     wc.lpszClassName = "BnetWndClass";
91    
92     if(!RegisterClassEx( &wc ))
93     RegisterClass((LPWNDCLASS)&wc.style);
94    
95     hwnd = CreateWindow(TEXT("BnetWndClass"),"Diablo II DataBase Server",
96     WS_OVERLAPPEDWINDOW,
97     CW_USEDEFAULT,CW_USEDEFAULT,
98     CW_USEDEFAULT,CW_USEDEFAULT,
99     NULL,
100     LoadMenu(hInstance, MAKEINTRESOURCE(ID_MENU)),
101     hInstance,NULL);
102    
103     if(hwnd) {
104     ShowWindow(hwnd, nCmdShow);
105     UpdateWindow(hwnd);
106     }
107    
108     while(GetMessage(&msg, NULL, 0, 0)) {
109     TranslateMessage( &msg );
110     DispatchMessage( &msg );
111    
112     if(!d2dbs_running && d2dbs_run && gui_run) {
113     d2dbs_running = TRUE;
114     _beginthread(d2dbs, 0, NULL);
115     }
116    
117     if(!gui_run && !d2dbs_running) {
118     KillTrayIcon(hwnd);
119     exit(0);
120     }
121     }
122     return ((int) msg.wParam);
123     }
124    
125     LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
126     {
127     switch(uMsg) {
128     HANDLE_MSG(hwnd, WM_CREATE, OnCreate);
129     HANDLE_MSG(hwnd, WM_SIZE, OnSize);
130     HANDLE_MSG(hwnd, WM_COMMAND, OnCommand);
131     HANDLE_MSG(hwnd, WM_CLOSE, OnClose);
132     case WM_SHELLNOTIFY:
133     return OnShellNotify(hwnd, wParam, lParam);
134     }
135     return DefWindowProc(hwnd, uMsg, wParam, lParam);
136     }
137    
138     static BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
139     {
140     ghwndConsole = CreateWindowEx(WS_EX_CLIENTEDGE, RICHEDIT_CLASS, NULL,
141     WS_CHILD|WS_VISIBLE|ES_READONLY|ES_MULTILINE|
142     WS_VSCROLL|WS_HSCROLL|ES_NOHIDESEL,
143     0, 0,
144     0, 0, hwnd, 0,
145     0, NULL);
146    
147     if(!ghwndConsole)
148     return FALSE;
149    
150     return TRUE;
151     }
152    
153     static void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
154     {
155     switch (id) {
156     case ID_RESTORE:
157     OnShellNotify(hwnd, ID_TRAY, WM_LBUTTONDBLCLK);
158     break;
159     case ID_START_D2DBS:
160     eventlog(eventlog_level_warn,__FUNCTION__,"Sending Start Signal to d2dbs");
161     d2dbs_run = TRUE;
162     break;
163     case ID_SHUTDOWN_D2DBS:
164     eventlog(eventlog_level_warn,__FUNCTION__,"Sending Shutdown Signal to d2dbs");
165     d2dbs_run = FALSE;
166     d2dbs_signal_quit_wrapper();
167     break;
168     case ID_RESTART_D2DBS:
169     eventlog(eventlog_level_warn,__FUNCTION__,"Sending Restart Signal To d2dbs");
170     d2dbs_signal_quit_wrapper();
171     break;
172     case ID_EDITCONFIG_D2DBS:
173     ShellExecute(NULL, "open", "notepad.exe", "conf\\d2dbs.conf", NULL, SW_SHOW );
174     break;
175     case ID_LOADCONFIG_D2DBS:
176     eventlog(eventlog_level_warn,__FUNCTION__,"Sending Reload Config Signal To d2dbs");
177     d2dbs_signal_reload_config_wrapper();
178     break;
179     case ID_LADDER_SAVE:
180     eventlog(eventlog_level_warn,__FUNCTION__,"Sending Save Ladder Signal To d2dbs");
181     d2dbs_signal_save_ladder_wrapper();
182     break;
183     case ID_EXIT:
184     OnClose(hwnd);
185     break;
186     case ID_CLEAR:
187     SendMessage(ghwndConsole, WM_SETTEXT, 0, 0);
188     break;
189     case ID_ABOUT:
190     DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_ABOUT_BOX), hwnd, (DLGPROC)DlgProcAbout);
191     break;
192     }
193     }
194    
195     static int OnShellNotify(HWND hwnd, int uID, int uMessage)
196     {
197     if(uID == ID_TRAY) {
198     if(uMessage == WM_LBUTTONDBLCLK) {
199     if(!IsWindowVisible(hwnd))
200     ShowWindow(hwnd, SW_RESTORE);
201    
202     SetForegroundWindow(hwnd);
203     }
204     }
205     return 0;
206     }
207    
208     static void OnClose(HWND hwnd)
209     {
210     eventlog(eventlog_level_warn,__FUNCTION__,"Sending Exit Signal");
211     gui_run = FALSE;
212     d2dbs_run = FALSE;
213     d2dbs_signal_exit_wrapper();
214     }
215    
216     static void OnSize(HWND hwnd, UINT state, int cx, int cy)
217     {
218     NOTIFYICONDATA dta;
219    
220     if( state == SIZE_MINIMIZED ) {
221     dta.cbSize = sizeof(NOTIFYICONDATA);
222     dta.hWnd = hwnd;
223     dta.uID = ID_TRAY;
224     dta.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
225     dta.uCallbackMessage = WM_SHELLNOTIFY;
226     dta.hIcon = LoadIcon(GetWindowInstance(hwnd), MAKEINTRESOURCE(ID_ICON1));
227     strcpy(dta.szTip, "D2DBS Version");
228     strcat(dta.szTip, " ");
229     strcat(dta.szTip, D2DBS_VERSION_NUMBER);
230    
231     Shell_NotifyIcon(NIM_ADD, &dta);
232     ShowWindow(hwnd, SW_HIDE);
233     return;
234     }
235    
236     MoveWindow(ghwndConsole, 0, 0, cx, cy, TRUE);
237     }
238    
239     BOOL CALLBACK DlgProcAbout(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
240     {
241     switch (uMsg) {
242     case WM_CLOSE:
243     EndDialog(hwnd, TRUE);
244     return TRUE;
245     case WM_INITDIALOG:
246     return TRUE;
247     case WM_COMMAND:
248     switch ((int)wParam) {
249     case IDOK:
250     EndDialog(hwnd, TRUE);
251     return TRUE;
252     }
253     }
254     return FALSE;
255     }
256    
257     extern int gui_lprintf(t_eventlog_level l, const char *format, ...)
258     {
259     va_list arglist;
260     va_start(arglist, format);
261    
262     return gui_lvprintf(l, format, arglist);
263     }
264    
265     extern int gui_lvprintf(t_eventlog_level l, const char *format, va_list arglist)
266     {
267     char buff[4096];
268     int result;
269     COLORREF clr;
270    
271     result = vsprintf(buff, format, arglist);
272    
273     switch(l)
274     {
275     case eventlog_level_none:
276     clr = RGB(0, 0, 0);
277     break;
278     case eventlog_level_trace:
279     clr = RGB(255, 0, 255);
280     break;
281     case eventlog_level_debug:
282     clr = RGB(0, 0, 255);
283     break;
284     case eventlog_level_info:
285     clr = RGB(0, 0, 0);
286     break;
287     case eventlog_level_warn:
288     clr = RGB(255, 128, 64);
289     break;
290     case eventlog_level_error:
291     clr = RGB(255, 0, 0);
292     break;
293     case eventlog_level_fatal:
294     clr = RGB(255, 0, 0);
295     break;
296     default:
297     clr = RGB(0, 0, 0);
298     }
299     guiAddText(buff, clr);
300     return result;
301     }
302    
303     static void guiAddText(const char *str, COLORREF clr)
304     {
305     int start_lines, text_length, end_lines;
306     CHARRANGE cr;
307     CHARRANGE ds;
308     CHARFORMAT fmt;
309    
310     text_length = SendMessage(ghwndConsole, WM_GETTEXTLENGTH, 0, 0);
311    
312     if (text_length > 30000)
313     {
314     ds.cpMin = 0;
315     ds.cpMax = text_length - 30000;
316     SendMessage(ghwndConsole, EM_EXSETSEL, 0, (LPARAM)&ds);
317     SendMessage(ghwndConsole, EM_REPLACESEL, FALSE, 0);
318     }
319    
320     cr.cpMin = text_length;
321     cr.cpMax = text_length;
322     SendMessage(ghwndConsole, EM_EXSETSEL, 0, (LPARAM)&cr);
323    
324     fmt.cbSize = sizeof(CHARFORMAT);
325     fmt.dwMask = CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_BOLD|CFM_ITALIC|CFM_STRIKEOUT|CFM_UNDERLINE;
326     fmt.yHeight = 160;
327     fmt.dwEffects = 0;
328     fmt.crTextColor = clr;
329     strcpy(fmt.szFaceName,"Courier New");
330    
331     SendMessage(ghwndConsole, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
332     SendMessage(ghwndConsole, EM_REPLACESEL, FALSE, (LPARAM)str);
333     }
334    
335     #define EXIT_ERROR -1
336     #define EXIT_OK 0
337     #define EXIT_SERVICE 1
338    
339     static void d2dbs(void * dummy)
340     {
341     switch (server_main(__argc, __argv))
342     {
343     case EXIT_SERVICE:
344     gui_run = FALSE; /* close gui */
345     case EXIT_ERROR:
346     d2dbs_run = FALSE; /* don't restart */
347     case EXIT_OK:
348     ; /* do nothing */
349     }
350    
351     eventlog(eventlog_level_warn,__FUNCTION__,"Server Stopped");
352     d2dbs_running = FALSE;
353     }
354    
355     #endif

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1