[CONPRO-1473] Fix for build error [-Werror=implicit-function-declaration]
[platform/upstream/iotivity.git] / resource / examples / winuiclientgui.cpp
1 /* ****************************************************************
2  *
3  * Copyright 2016 Intel Corporation All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  ******************************************************************/
18
19 #include <string>
20 #include "winuiclient.h"
21
22 #include <Windows.h>
23 #include <Commctrl.h>
24
25 #define ID_TRACKBAR             0x50505
26 #define ID_BUTTONPLAYPAUSE      0xFFF0
27 #define ID_BUTTONFIND           0xFFF1
28 #define ID_BUTTONGET            0xFFF2
29 #define ID_BUTTONPUT            0xFFF3
30 #define ID_BUTTONPOST           0xFFF4
31 #define ID_BUTTONBEGINOBSERVE   0xFFF5
32 #define ID_BUTTONCANCELOBSERVE  0xFFF6
33
34 int g_CurSliderVal = 0;
35
36 HWND hwnd, icon_button, hwndVolumeSlider, hwndVolumeExpectedLabel;
37 HWND hwndButtonPlayPause, hwndButtonFind, hwndButtonGet, hwndButtonPut, hwndButtonPost, hwndButtonBeginObserve, hwndButtonCancelObserve;
38 HINSTANCE hInstance, g_hinstTrackBar, g_hinstVolumeSlider, g_hinstVolumeLabel;
39 HANDLE Timer, Thread;
40 HICON hIcon1;
41 HBRUSH g_BkgndBrush;
42
43 WinUIClient::WinUIClientApp* app = NULL;
44
45 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
46 void LabelPrintf (HWND hwndEdit, TCHAR * szFormat, ...);
47 HWND WINAPI CreateTrackbar(HWND parent, int x, int y, int w, int h,UINT iMin, UINT iMax, UINT iSelMin, UINT iSelMax);
48 HWND WINAPI CreateLabel(HWND parent, LPCTSTR lpText, int x, int y, int w, int h);
49 HWND WINAPI CreateButton(HWND parent, UINT_PTR id, LPCTSTR caption, int x, int y, int w, int h);
50
51 FILE* client_open(const char* /*path*/, const char *mode)
52 {
53     return fopen("./oic_svr_db_client.dat", mode);
54 }
55
56 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
57             LPSTR lpCmdLine, int nCmdShow )
58 {
59     MSG msg;
60     WNDCLASS wc = {sizeof(WNDCLASS)};
61     wc.lpszClassName = TEXT( "Static Control" );
62     wc.hInstance     = hInstance ;
63     wc.hbrBackground = GetSysColorBrush(COLOR_MENU);
64     wc.lpfnWndProc   = WndProc;
65     wc.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
66     wc.hCursor = LoadCursor (NULL, IDC_ARROW);
67
68     RegisterClass(&wc);
69     hwnd = CreateWindow( wc.lpszClassName, TEXT("IoTivity Media Client - Windows UI"),
70                    WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME,
71                    100, 100, 275, 170, 0, 0, hInstance, 0);
72
73     InitCommonControls(); // loads common control's DLL
74
75     hwndVolumeSlider =
76              CreateTrackbar(hwnd,
77                             10,10,
78                             170,40,
79                             0,100,
80                             0,0);
81     hwndVolumeExpectedLabel =
82                 CreateLabel(hwnd,
83                             "Expected Volume",
84                             10,50,
85                             240,30);
86
87     LabelPrintf(hwndVolumeExpectedLabel,
88                 "Expected Volume: %i",
89                 0);
90
91     hwndButtonPlayPause =
92                 CreateButton(hwnd,
93                              ID_BUTTONPLAYPAUSE,
94                              "PlayPause",
95                              180,10,
96                              80,35);
97
98     hwndButtonFind =
99                 CreateButton(hwnd,
100                              ID_BUTTONFIND,
101                              "Find",
102                              10,75,
103                              64,25);
104
105     hwndButtonGet =
106                 CreateButton(hwnd,
107                              ID_BUTTONGET,
108                              "Get",
109                              10,105,
110                              64,25);
111
112     hwndButtonPut =
113                 CreateButton(hwnd,
114                              ID_BUTTONPUT,
115                              "Put",
116                              80,75,
117                              64,25);
118
119     hwndButtonPost =
120                 CreateButton(hwnd,
121                              ID_BUTTONPOST,
122                              "Post",
123                              80,105,
124                              64,25);
125     hwndButtonBeginObserve =
126                 CreateButton(hwnd,
127                              ID_BUTTONBEGINOBSERVE,
128                              "Bgn Obs",
129                              150,75,
130                              64,25);
131     hwndButtonCancelObserve =
132                 CreateButton(hwnd,
133                              ID_BUTTONCANCELOBSERVE,
134                              "Cncl Obs",
135                              150,105,
136                              64,25);
137
138
139     ShowWindow(hwnd, nCmdShow);
140     UpdateWindow(hwnd);
141
142     while (GetMessage(&msg, NULL, 0, 0))
143     {
144         TranslateMessage(&msg);
145         DispatchMessage(&msg);
146     }
147
148     return (int) msg.wParam;
149 }
150
151 LRESULT
152 CALLBACK
153 WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
154 {
155     HDC hDC;
156     RECT rect;
157     WinUIClient::Media myMedia;
158
159     switch(msg){
160       case WM_HSCROLL:
161         switch (LOWORD(wParam)) {
162             case TB_ENDTRACK:
163                 g_CurSliderVal = SendMessage(hwndVolumeSlider, TBM_GETPOS, 0, 0);
164                 LabelPrintf(hwndVolumeExpectedLabel,"Volume: %i", g_CurSliderVal);
165
166                 myMedia = app->GetMedia();
167                 myMedia.m_volume = g_CurSliderVal;
168                 app->SetMedia(myMedia.m_state, myMedia.m_volume);
169                 app->PostMediaRepresentation();
170                 break;
171         }
172         break;
173       case WM_CREATE:
174           {
175               hIcon1 = LoadIcon (NULL, IDI_WARNING);
176               SendMessage(icon_button,BM_SETIMAGE,IMAGE_ICON,(LPARAM)hIcon1);
177               GetClientRect(hwnd, &rect);
178               g_BkgndBrush = GetSysColorBrush(COLOR_MENU);
179
180               OCPersistentStorage ps = {client_open, fread, fwrite, fclose, unlink };
181               app = new WinUIClient::WinUIClientApp(ps);
182               app->Initialize();
183               app->Run();
184           }
185           break;
186       case WM_DESTROY:
187
188           delete app;
189
190           PostQuitMessage(0);
191           break;
192       case WM_ERASEBKGND:
193           GetClientRect(hwnd, &rect);
194           hDC = GetDC(hwnd);
195           SelectObject((HDC)wParam, g_BkgndBrush);
196           Rectangle((HDC)hDC, rect.left, rect.top, rect.right, rect.bottom);
197           ReleaseDC(hwnd, hDC);
198           break;
199       case WM_SIZE:
200           break;
201       case WM_RBUTTONDOWN:
202       case WM_LBUTTONDOWN:
203           break;
204       case WM_MOUSELEAVE:
205       case WM_RBUTTONUP:
206       case WM_LBUTTONUP:
207           break;
208       case WM_COMMAND:
209         if (HIWORD(wParam) == BN_CLICKED)
210         {
211
212             switch (LOWORD(wParam))
213             {
214                 case ID_BUTTONPLAYPAUSE:
215                     myMedia = app->GetMedia();
216
217                     // HACK: We are using m_state in a stateless way.
218                     // True means "toggle".  The server does not have state
219                     // knowledge about whether or not media is playing on the
220                     // system.  It's dumb and hits the "PlayPause" key on the
221                     // keyboard.
222                     //
223                     myMedia.m_state = true;
224                     app->SetMedia(myMedia.m_state, myMedia.m_volume);
225                     myMedia.m_state = false;
226
227                     app->PutMediaRepresentation();
228                     break;
229                 case ID_BUTTONFIND:
230                     app->FindResources();
231                     break;
232                 case ID_BUTTONGET:
233                     app->GetMediaRepresentation();
234                     break;
235                 case ID_BUTTONPUT:
236                     myMedia = app->GetMedia();
237                     myMedia.m_volume = g_CurSliderVal;
238                     app->SetMedia(myMedia.m_state, myMedia.m_volume);
239                     app->PutMediaRepresentation();
240                     break;
241                 case ID_BUTTONPOST:
242                     myMedia = app->GetMedia();
243                     myMedia.m_volume = g_CurSliderVal;
244                     app->SetMedia(myMedia.m_state, myMedia.m_volume);
245                     app->PostMediaRepresentation();
246                     break;
247                 case ID_BUTTONBEGINOBSERVE:
248                     app->BeginObserving();
249                     break;
250                 case ID_BUTTONCANCELOBSERVE:
251                     app->CancelObserving();
252                     break;
253             }
254         }
255         break;
256       default:
257           return DefWindowProc(hwnd, msg, wParam, lParam);
258     }
259
260     return 0;
261 }
262
263 #define GWL_HINSTANCE -6
264 HWND WINAPI CreateButton(HWND parent,
265                             UINT_PTR id,
266                             LPCTSTR caption    ,
267                             int x,
268                             int y,
269                             int w,
270                             int h
271                             )
272 {
273     HWND hwndButton = CreateWindow(
274         "BUTTON",  // Predefined class; Unicode assumed
275         caption,      // Button text
276         WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles
277         x,         // x position
278         y,         // y position
279         w,        // Button width
280         h,        // Button height
281         parent,     // Parent window
282         (HMENU)id,       // No menu.
283         (HINSTANCE)GetWindowLongPtr(parent, GWL_HINSTANCE),
284         NULL);      // Pointer not needed.
285
286     return hwndButton;
287 }
288
289 HWND WINAPI CreateLabel(
290     HWND parent,  // handle of parent window
291     LPCTSTR lpText,
292     int x,
293     int y,
294     int w,
295     int h
296 )
297 {
298    HWND hwndPowerLabel  = CreateWindow("static", "ST_U",
299                                       WS_CHILD | WS_VISIBLE | WS_TABSTOP,
300                                       x, y, w, h,
301                                       parent, (HMENU)(501),
302                                       (HINSTANCE)GetWindowLongPtr(parent, GWL_HINSTANCE),
303                                       NULL);
304    SetWindowText(hwndPowerLabel, lpText);
305    return hwndPowerLabel;
306 }
307
308 // CreateTrackbar - creates and initializes a trackbar.
309 //
310 // Global variable
311 //     g_hinstTrackBar - instance handle
312 //
313 HWND WINAPI CreateTrackbar(
314     HWND parent,  // handle of dialog box (parent window)
315     int x,
316     int y,
317     int w,
318     int h,
319     UINT iMin,     // minimum value in trackbar range
320     UINT iMax,     // maximum value in trackbar range
321     UINT iSelMin,  // minimum value in trackbar selection
322     UINT iSelMax)  // maximum value in trackbar selection
323 {
324     HWND hwndVolumeSlider = CreateWindowEx(
325         0,                              // no extended styles
326         TRACKBAR_CLASS,                 // class name
327         "Trackbar Control",             // title (caption)
328         WS_CHILD |
329         WS_VISIBLE |
330         TBS_AUTOTICKS |
331         TBS_ENABLESELRANGE,             // style
332         x,y,w,h,                        // position ,size
333         parent,                        // parent window
334         (HMENU)ID_TRACKBAR,             // control identifier
335         (HINSTANCE)GetWindowLongPtr(parent, GWL_HINSTANCE),// instance
336         NULL                            // no WM_CREATE parameter
337         );
338
339     SendMessage(hwndVolumeSlider, TBM_SETRANGE,
340         (WPARAM) TRUE,                  // redraw flag
341         (LPARAM) MAKELONG(iMin, iMax)); // min. & max. positions
342
343     SendMessage(hwndVolumeSlider, TBM_SETPAGESIZE,
344         0, (LPARAM) 4);                 // new page size
345
346     SendMessage(hwndVolumeSlider, TBM_SETSEL,
347         (WPARAM) FALSE,                 // redraw flag
348         (LPARAM) MAKELONG(iSelMin, iSelMax));
349
350     SendMessage(hwndVolumeSlider, TBM_SETPOS,
351         (WPARAM) TRUE,                  // redraw flag
352         (LPARAM) iSelMin);
353
354     SetFocus(hwndVolumeSlider);
355
356     return hwndVolumeSlider;
357 }
358
359 void LabelPrintf (HWND hwndEdit, TCHAR * szFormat, ...)
360 {
361      TCHAR   szBuffer [1024] ;
362      va_list pArgList ;
363
364      va_start (pArgList, szFormat) ;
365      wvsprintf (szBuffer, szFormat, pArgList) ;
366      va_end (pArgList) ;
367
368      SetWindowText(hwndEdit, szBuffer);
369 }
370