summaryrefslogtreecommitdiff
path: root/pjsip-apps/src/pjsua/wm/main_wm.c
blob: 8ab133840bc77b2faa04efd3c8f99f86773c6635 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* $Id$ */
/* 
 * Copyright (C) 2013 Teluu Inc. (http://www.teluu.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */
#include <windows.h>

#include <pjlib.h>
#include <windows.h>
#include <winuserm.h>
#include <aygshell.h>
#include "..\pjsua_app.h"

#define MAINWINDOWCLASS TEXT("PjsuaDlg")
#define MAINWINDOWTITLE TEXT("PJSUA")
#define LOGO_PATH TEXT("\\Program Files\\pjsua\\pjsua.bmp")

#define WM_APP_INIT	WM_USER + 1
#define WM_APP_DESTROY	WM_USER + 2
#define WM_APP_RESTART	WM_USER + 3

static HINSTANCE	 g_hInst;
static HWND		 g_hWndMenuBar;
static HWND		 g_hWndMain;
static HWND		 g_hWndLbl;
static HWND		 g_hWndImg;
static HBITMAP		 g_hBmp;

static int restart_argc = 0;
static char **restart_argv = NULL;

/* Helper funtions to init/restart/destroy the pjsua */
static void LibInit();
static void LibDestroy();
static void LibRestart();

/* pjsua app callbacks */
static void lib_on_started(pj_status_t status, const char* title);
static pj_bool_t lib_on_stopped(pj_bool_t restart, int argc, char** argv);
static void lib_on_config_init(pjsua_app_config *cfg);

LRESULT CALLBACK DialogProc(const HWND hWnd,
			    const UINT Msg, 
			    const WPARAM wParam,
			    const LPARAM lParam) 
{   
    LRESULT res = 0;

    switch (Msg) {
    case WM_CREATE:
	g_hWndMain = hWnd;
	break;

    case WM_COMMAND: /* Exit menu */
    case WM_CLOSE:
	PostQuitMessage(0);
	break;

    case WM_HOTKEY:
	/* Exit app when back is pressed. */
	if (VK_TBACK == HIWORD(lParam) && (0 != (MOD_KEYUP & LOWORD(lParam)))) {
	    PostQuitMessage(0);
	} else {
	    return DefWindowProc(hWnd, Msg, wParam, lParam);
	}
	break;

    case WM_CTLCOLORSTATIC:
	/* Set text and background color for static windows */
	SetTextColor((HDC)wParam, RGB(255, 255, 255));
	SetBkColor((HDC)wParam, RGB(0, 0, 0));
	return (LRESULT)GetStockObject(BLACK_BRUSH);

    case WM_APP_INIT:
	LibInit();
	break;

    case WM_APP_DESTROY:
	LibDestroy();
	PostQuitMessage(0);
	break;

    case WM_APP_RESTART:
	LibRestart();
	break;

    default:
	return DefWindowProc(hWnd, Msg, wParam, lParam);
    }

    return res;
}


/* === GUI === */

pj_status_t gui_init()
{
    WNDCLASS wc;
    HWND hWnd = NULL;	
    RECT r;
    DWORD dwStyle;
    enum { LABEL_HEIGHT = 30 };
    enum { MENU_ID_EXIT = 50000 };
    BITMAP bmp;
    HMENU hRootMenu;
    SHMENUBARINFO mbi;

    pj_status_t status  = PJ_SUCCESS;
    
    /* Check if app is running. If it's running then focus on the window */
    hWnd = FindWindow(MAINWINDOWCLASS, MAINWINDOWTITLE);

    if (NULL != hWnd) {
	SetForegroundWindow(hWnd);    
	return status;
    }

    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)DialogProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = g_hInst;
    wc.hIcon = 0;
    wc.hCursor = 0;
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName	= 0;
    wc.lpszClassName = MAINWINDOWCLASS;
    
    if (!RegisterClass(&wc) != 0) {
	DWORD err = GetLastError();
	return PJ_RETURN_OS_ERROR(err);
    }

    /* Create the app. window */
    g_hWndMain = CreateWindow(MAINWINDOWCLASS, MAINWINDOWTITLE,
			      WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 
			      CW_USEDEFAULT, CW_USEDEFAULT,
			      (HWND)NULL, NULL, g_hInst, (LPSTR)NULL);
    if (g_hWndMain == NULL) {
	DWORD err = GetLastError();
	return PJ_RETURN_OS_ERROR(err);
    }

    /* Create exit menu */
    hRootMenu = CreateMenu();
    AppendMenu(hRootMenu, MF_STRING, MENU_ID_EXIT, L"Exit");

    /* Initialize menubar */
    ZeroMemory(&mbi, sizeof(SHMENUBARINFO));
    mbi.cbSize      = sizeof(SHMENUBARINFO);
    mbi.hwndParent  = g_hWndMain;
    mbi.dwFlags	    = SHCMBF_HIDESIPBUTTON|SHCMBF_HMENU;
    mbi.nToolBarId  = (UINT)hRootMenu;
    mbi.hInstRes    = g_hInst;

    if (FALSE == SHCreateMenuBar(&mbi)) {
	DWORD err = GetLastError();
        return PJ_RETURN_OS_ERROR(err);
    }

    /* Store menu window handle */
    g_hWndMenuBar = mbi.hwndMB;

    /* Show the menu */
    DrawMenuBar(g_hWndMain);
    ShowWindow(g_hWndMenuBar, SW_SHOW);

    /* Override back button */
    SendMessage(g_hWndMenuBar, SHCMBM_OVERRIDEKEY, VK_TBACK,
	    MAKELPARAM(SHMBOF_NODEFAULT | SHMBOF_NOTIFY,
	    SHMBOF_NODEFAULT | SHMBOF_NOTIFY));

    /* Get main window size */
    GetClientRect(g_hWndMain, &r);
#if defined(WIN32_PLATFORM_PSPC) && WIN32_PLATFORM_PSPC != 0
    /* Adjust the height for PocketPC platform */
    r.bottom -= GetSystemMetrics(SM_CYMENU);
#endif

    /* Create logo */
    g_hBmp = SHLoadDIBitmap(LOGO_PATH); // for jpeg, uses SHLoadImageFile()
    if (g_hBmp == NULL) {
	DWORD err = GetLastError();
	return PJ_RETURN_OS_ERROR(err);
    }
    GetObject(g_hBmp, sizeof(bmp), &bmp);

    dwStyle = SS_CENTERIMAGE | SS_REALSIZEIMAGE | SS_BITMAP |
	      WS_CHILD | WS_VISIBLE;
    g_hWndImg = CreateWindow(TEXT("STATIC"), NULL, dwStyle,
			     (r.right-r.left-bmp.bmWidth)/2,
			     (r.bottom-r.top-bmp.bmHeight)/2,
			     bmp.bmWidth, bmp.bmHeight,
			     g_hWndMain, (HMENU)0, g_hInst, NULL);
    if (g_hWndImg == NULL) {
	DWORD err = GetLastError();
	return PJ_RETURN_OS_ERROR(err);
    }
    SendMessage(g_hWndImg, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)g_hBmp);

    /* Create label */
    dwStyle = WS_CHILD | WS_VISIBLE | ES_CENTER;
    g_hWndLbl = CreateWindow(TEXT("STATIC"), NULL, dwStyle,
		0, r.bottom-LABEL_HEIGHT, r.right-r.left, LABEL_HEIGHT,
                g_hWndMain, (HMENU)0, g_hInst, NULL);
    if (g_hWndLbl == NULL) {
	DWORD err = GetLastError();
	return PJ_RETURN_OS_ERROR(err);
    }
    SetWindowText(g_hWndLbl, _T("Please wait.."));

    return status;
}


pj_status_t gui_start()
{
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (msg.wParam);
}

void gui_destroy(void)
{
    if (g_hWndMain) {
	DestroyWindow(g_hWndMain);
	g_hWndMain = NULL;
    }
    if (g_hWndMenuBar) {
	DestroyWindow(g_hWndMenuBar);
	g_hWndMenuBar = NULL;
    }
    if (g_hWndLbl) {
	DestroyWindow(g_hWndLbl);
	g_hWndLbl = NULL;
    }
    if (g_hWndImg) {
	DestroyWindow(g_hWndImg);
	g_hWndImg = NULL;
    }
    if (g_hBmp) {
	DeleteObject(g_hBmp);
	g_hBmp = NULL;
    }
    UnregisterClass(MAINWINDOWCLASS, g_hInst);
}

/* === ENGINE === */

/* Called when pjsua is started */
void lib_on_started(pj_status_t status, const char* title)
{
    wchar_t wtitle[128];

    PJ_UNUSED_ARG(status);

    pj_ansi_to_unicode(title, strlen(title), wtitle, PJ_ARRAY_SIZE(wtitle));
    SetWindowText(g_hWndLbl, wtitle);
}

/* Called when pjsua is stopped */
pj_bool_t lib_on_stopped(pj_bool_t restart, int argc, char** argv)
{
    if (restart) {
	restart_argc = argc;
	restart_argv = argv;

	// Schedule Lib Restart
	PostMessage(g_hWndMain, WM_APP_RESTART, 0, 0);
    } else {
	/* Destroy & quit GUI, e.g: clean up window, resources  */
	PostMessage(g_hWndMain, WM_APP_DESTROY, 0, 0);
    }

    return PJ_FALSE;
}

/* Called before pjsua initializing config. */
void lib_on_config_init(pjsua_app_config *cfg)
{
    PJ_UNUSED_ARG(cfg);
}

void LibInit()
{
    char* argv[] = {
	"",
	"--use-cli",
	"--cli-telnet-port=0",
	"--no-cli-console"
    };
    app_cfg_t app_cfg;
    pj_status_t status;

    pj_bzero(&app_cfg, sizeof(app_cfg));
    app_cfg.argc = PJ_ARRAY_SIZE(argv);
    app_cfg.argv = argv;
    app_cfg.on_started = &lib_on_started;
    app_cfg.on_stopped = &lib_on_stopped;
    app_cfg.on_config_init = &lib_on_config_init;

    SetWindowText(g_hWndLbl, _T("Initializing.."));
    status = app_init(&app_cfg);
    if (status != PJ_SUCCESS)
	goto on_return;
    
    SetWindowText(g_hWndLbl, _T("Starting.."));
    status = app_run(PJ_FALSE);
    if (status != PJ_SUCCESS)
	goto on_return;

on_return:
    if (status != PJ_SUCCESS)
	SetWindowText(g_hWndLbl, _T("Initialization failed"));
}

void LibDestroy()
{
    app_destroy();
}

void LibRestart()
{
    app_cfg_t app_cfg;
    pj_status_t status;
    
    /* Destroy pjsua app first */

    app_destroy();

    /* Reinit pjsua app */
    
    pj_bzero(&app_cfg, sizeof(app_cfg));
    app_cfg.argc = restart_argc;
    app_cfg.argv = restart_argv;
    app_cfg.on_started = &lib_on_started;
    app_cfg.on_stopped = &lib_on_stopped;
    app_cfg.on_config_init = &lib_on_config_init;

    status = app_init(&app_cfg);
    if (status != PJ_SUCCESS) {
	SetWindowText(g_hWndLbl, _T("app_init() failed"));
	return;
    }
	
    /* Run pjsua app */

    status = app_run(PJ_FALSE);
    if (status != PJ_SUCCESS) {
	SetWindowText(g_hWndLbl, _T("app_run() failed"));
	return;
    }
}


/* === MAIN === */

int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPWSTR lpCmdLine,
    int nShowCmd
)
{
    int status;

    PJ_UNUSED_ARG(hPrevInstance);
    PJ_UNUSED_ARG(lpCmdLine);
    PJ_UNUSED_ARG(nShowCmd);

    // store the hInstance in global
    g_hInst = hInstance;

    status = gui_init();
    if (status != 0)
	goto on_return;

    // Start the engine
    PostMessage(g_hWndMain, WM_APP_INIT, 0, 0);

    status = gui_start();
	
on_return:
    LibDestroy();
    gui_destroy();

    return status;
}