Debian Codegraph @

Xpjproject/pjsip-apps/src/pjsua/wm/main_wm.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2013 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (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 #include <winuserm.h>
19 #include <aygshell.h>
20 #include "..\pjsua_app.h"
21 #include "..\pjsua_app_config.h"
22
23 #define MAINWINDOWCLASS TEXT("PjsuaDlg")
24 #define MAINWINDOWTITLE TEXT("PJSUA")
25 #define LOGO_PATH TEXT("\\Program Files\\pjsua\\pjsua.bmp")
26
27 #define WM_APP_INIT WM_USER + 1
28 #define WM_APP_DESTROY WM_USER + 2
29 #define WM_APP_RESTART WM_USER + 3
30
31 static HINSTANCE g_hInst;
32 static HWND g_hWndMenuBar;
33 static HWND g_hWndMain;
34 static HWND g_hWndLbl;
35 static HWND g_hWndImg;
36 static HBITMAP g_hBmp;
37
38 static int start_argc;
39 static char **start_argv;
40
41 /* Helper funtions to init/destroy the pjsua */
42 static void PjsuaInit();
43 static void PjsuaDestroy();
44
45 /* pjsua app callbacks */
46 static void PjsuaOnStarted(pj_status_t status, const char* title);
47 static void PjsuaOnStopped(pj_bool_t restart, int argc, char** argv);
48 static void PjsuaOnConfig(pjsua_app_config *cfg);
49
50 LRESULT CALLBACK DialogProc(const HWND hWnd,
51 const UINT Msg,
52 const WPARAM wParam,
53 const LPARAM lParam)
54 {
55 LRESULT res = 0;
56
57 switch (Msg) {
58 case WM_CREATE:
59 g_hWndMain = hWnd;
60 break;
61
62 case WM_COMMAND: /* Exit menu */
63 case WM_CLOSE:
64 PostQuitMessage(0);
65 break;
66
67 case WM_HOTKEY:
68 /* Exit app when back is pressed. */
69 if (VK_TBACK == HIWORD(lParam) && (0 != (MOD_KEYUP & LOWORD(lParam)))) {
70 PostQuitMessage(0);
71 } else {
72 return DefWindowProc(hWnd, Msg, wParam, lParam);
73 }
74 break;
75
76 case WM_CTLCOLORSTATIC:
77 /* Set text and background color for static windows */
78 SetTextColor((HDC)wParam, RGB(255, 255, 255));
79 SetBkColor((HDC)wParam, RGB(0, 0, 0));
80 return (LRESULT)GetStockObject(BLACK_BRUSH);
81
82 case WM_APP_INIT:
83 case WM_APP_RESTART:
84 PjsuaInit();
85 break;
86
87 case WM_APP_DESTROY:
88 PostQuitMessage(0);
89 break;
90
91 default:
92 return DefWindowProc(hWnd, Msg, wParam, lParam);
93 }
94
95 return res;
96 }
97
98
99 /* === GUI === */
100
101 pj_status_t GuiInit()
102 {
103 WNDCLASS wc;
104 HWND hWnd = NULL;
105 RECT r;
106 DWORD dwStyle;
107 enum { LABEL_HEIGHT = 30 };
108 enum { MENU_ID_EXIT = 50000 };
109 BITMAP bmp;
110 HMENU hRootMenu;
111 SHMENUBARINFO mbi;
112
113 pj_status_t status = PJ_SUCCESS;
114
115 /* Check if app is running. If it's running then focus on the window */
116 hWnd = FindWindow(MAINWINDOWCLASS, MAINWINDOWTITLE);
117
118 if (NULL != hWnd) {
119 SetForegroundWindow(hWnd);
120 return status;
121 }
122
123 wc.style = CS_HREDRAW | CS_VREDRAW;
124 wc.lpfnWndProc = (WNDPROC)DialogProc;
125 wc.cbClsExtra = 0;
126 wc.cbWndExtra = 0;
127 wc.hInstance = g_hInst;
128 wc.hIcon = 0;
129 wc.hCursor = 0;
130 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
131 wc.lpszMenuName = 0;
132 wc.lpszClassName = MAINWINDOWCLASS;
133
134 if (!RegisterClass(&wc) != 0) {
135 DWORD err = GetLastError();
136 return PJ_RETURN_OS_ERROR(err);
137 }
138
139 /* Create the app. window */
140 g_hWndMain = CreateWindow(MAINWINDOWCLASS, MAINWINDOWTITLE,
141 WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
142 CW_USEDEFAULT, CW_USEDEFAULT,
143 (HWND)NULL, NULL, g_hInst, (LPSTR)NULL);
144 if (g_hWndMain == NULL) {
145 DWORD err = GetLastError();
146 return PJ_RETURN_OS_ERROR(err);
147 }
148
149 /* Create exit menu */
150 hRootMenu = CreateMenu();
151 AppendMenu(hRootMenu, MF_STRING, MENU_ID_EXIT, L"Exit");
152
153 /* Initialize menubar */
154 ZeroMemory(&mbi, sizeof(SHMENUBARINFO));
155 mbi.cbSize = sizeof(SHMENUBARINFO);
156 mbi.hwndParent = g_hWndMain;
157 mbi.dwFlags = SHCMBF_HIDESIPBUTTON|SHCMBF_HMENU;
158 mbi.nToolBarId = (UINT)hRootMenu;
159 mbi.hInstRes = g_hInst;
160
161 if (FALSE == SHCreateMenuBar(&mbi)) {
162 DWORD err = GetLastError();
163 return PJ_RETURN_OS_ERROR(err);
164 }
165
166 /* Store menu window handle */
167 g_hWndMenuBar = mbi.hwndMB;
168
169 /* Show the menu */
170 DrawMenuBar(g_hWndMain);
171 ShowWindow(g_hWndMenuBar, SW_SHOW);
172
173 /* Override back button */
174 SendMessage(g_hWndMenuBar, SHCMBM_OVERRIDEKEY, VK_TBACK,
175 MAKELPARAM(SHMBOF_NODEFAULT | SHMBOF_NOTIFY,
176 SHMBOF_NODEFAULT | SHMBOF_NOTIFY));
177
178 /* Get main window size */
179 GetClientRect(g_hWndMain, &r);
180 #if defined(WIN32_PLATFORM_PSPC) && WIN32_PLATFORM_PSPC != 0
181 /* Adjust the height for PocketPC platform */
182 r.bottom -= GetSystemMetrics(SM_CYMENU);
183 #endif
184
185 /* Create logo */
186 g_hBmp = SHLoadDIBitmap(LOGO_PATH); /* for jpeg, uses SHLoadImageFile() */
187 if (g_hBmp == NULL) {
188 DWORD err = GetLastError();
189 return PJ_RETURN_OS_ERROR(err);
190 }
191 GetObject(g_hBmp, sizeof(bmp), &bmp);
192
193 dwStyle = SS_CENTERIMAGE | SS_REALSIZEIMAGE | SS_BITMAP |
194 WS_CHILD | WS_VISIBLE;
195 g_hWndImg = CreateWindow(TEXT("STATIC"), NULL, dwStyle,
196 (r.right-r.left-bmp.bmWidth)/2,
197 (r.bottom-r.top-bmp.bmHeight)/2,
198 bmp.bmWidth, bmp.bmHeight,
199 g_hWndMain, (HMENU)0, g_hInst, NULL);
200 if (g_hWndImg == NULL) {
201 DWORD err = GetLastError();
202 return PJ_RETURN_OS_ERROR(err);
203 }
204 SendMessage(g_hWndImg, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)g_hBmp);
205
206 /* Create label */
207 dwStyle = WS_CHILD | WS_VISIBLE | ES_CENTER;
208 g_hWndLbl = CreateWindow(TEXT("STATIC"), NULL, dwStyle,
209 0, r.bottom-LABEL_HEIGHT, r.right-r.left, LABEL_HEIGHT,
210 g_hWndMain, (HMENU)0, g_hInst, NULL);
211 if (g_hWndLbl == NULL) {
212 DWORD err = GetLastError();
213 return PJ_RETURN_OS_ERROR(err);
214 }
215 SetWindowText(g_hWndLbl, _T("Please wait.."));
216
217 return status;
218 }
219
220
221 pj_status_t GuiStart()
222 {
223 MSG msg;
224 while (GetMessage(&msg, NULL, 0, 0)) {
225 TranslateMessage(&msg);
226 DispatchMessage(&msg);
227 }
228
229 return (msg.wParam);
230 }
231
232 void GuiDestroy(void)
233 {
234 if (g_hWndMain) {
235 DestroyWindow(g_hWndMain);
236 g_hWndMain = NULL;
237 }
238 if (g_hWndMenuBar) {
239 DestroyWindow(g_hWndMenuBar);
240 g_hWndMenuBar = NULL;
241 }
242 if (g_hWndLbl) {
243 DestroyWindow(g_hWndLbl);
244 g_hWndLbl = NULL;
245 }
246 if (g_hWndImg) {
247 DestroyWindow(g_hWndImg);
248 g_hWndImg = NULL;
249 }
250 if (g_hBmp) {
251 DeleteObject(g_hBmp);
252 g_hBmp = NULL;
253 }
254 UnregisterClass(MAINWINDOWCLASS, g_hInst);
255 }
256
257 /* === ENGINE === */
258
259 /* Called when pjsua is started */
260 void PjsuaOnStarted(pj_status_t status, const char* title)
261 {
262 wchar_t wtitle[128];
263 char err_msg[128];
264
265 if (status != PJ_SUCCESS || title == NULL) {
266 char err_str[PJ_ERR_MSG_SIZE];
267 pj_strerror(status, err_str, sizeof(err_str));
268 pj_ansi_snprintf(err_msg, sizeof(err_msg), "%s: %s",
269 (title?title:"App start error"), err_str);
270 title = err_msg;
271 }
272
273 pj_ansi_to_unicode(title, strlen(title), wtitle, PJ_ARRAY_SIZE(wtitle));
274 SetWindowText(g_hWndLbl, wtitle);
275 }
276
277 /* Called when pjsua is stopped */
278 void PjsuaOnStopped(pj_bool_t restart, int argc, char** argv)
279 {
280 if (restart) {
281 start_argc = argc;
282 start_argv = argv;
283
284 // Schedule Lib Restart
285 PostMessage(g_hWndMain, WM_APP_RESTART, 0, 0);
286 } else {
287 /* Destroy & quit GUI, e.g: clean up window, resources */
288 PostMessage(g_hWndMain, WM_APP_DESTROY, 0, 0);
289 }
290 }
291
292 /* Called before pjsua initializing config. */
293 void PjsuaOnConfig(pjsua_app_config *cfg)
294 {
295 PJ_UNUSED_ARG(cfg);
296 }
297
298 void PjsuaInit()
299 {
300 pjsua_app_cfg_t app_cfg;
301 pj_status_t status;
302
303 /* Destroy pjsua app first */
304 pjsua_app_destroy();
305
306 /* Init pjsua app */
307 pj_bzero(&app_cfg, sizeof(app_cfg));
308 app_cfg.argc = start_argc;
309 app_cfg.argv = start_argv;
310 app_cfg.on_started = &PjsuaOnStarted;
311 app_cfg.on_stopped = &PjsuaOnStopped;
312 app_cfg.on_config_init = &PjsuaOnConfig;
313
314 SetWindowText(g_hWndLbl, _T("Initializing.."));
315 status = pjsua_app_init(&app_cfg);
316 if (status != PJ_SUCCESS)
317 goto on_return;
318
319 SetWindowText(g_hWndLbl, _T("Starting.."));
320 status = pjsua_app_run(PJ_FALSE);
321 if (status != PJ_SUCCESS)
322 goto on_return;
323
324 on_return:
325 if (status != PJ_SUCCESS)
326 SetWindowText(g_hWndLbl, _T("Initialization failed"));
327 }
328
329 void PjsuaDestroy()
330 {
331 pjsua_app_destroy();
332 }
333
334 /* === MAIN === */
335
336 int WINAPI WinMain(
337 HINSTANCE hInstance,
338 HINSTANCE hPrevInstance,
339 LPWSTR lpCmdLine,
340 int nShowCmd
341 )
342 {
343 int status;
344
345 PJ_UNUSED_ARG(hPrevInstance);
346 PJ_UNUSED_ARG(lpCmdLine);
347 PJ_UNUSED_ARG(nShowCmd);
348
349 // store the hInstance in global
350 g_hInst = hInstance;
351
352 // Start GUI
353 status = GuiInit();
354 if (status != 0)
355 goto on_return;
356
357 // Setup args and start pjsua
358 start_argc = pjsua_app_def_argc;
359 start_argv = (char**)pjsua_app_def_argv;
360 PostMessage(g_hWndMain, WM_APP_INIT, 0, 0);
361
362 status = GuiStart();
363
364 on_return:
365 PjsuaDestroy();
366 GuiDestroy();
367
368 return status;
369 }