1 /* ****************************************************************
3 * Copyright 2016 Intel Corporation All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************/
20 #include "winuiclient.h"
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
34 int g_CurSliderVal = 0;
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;
43 WinUIClient::WinUIClientApp* app = NULL;
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);
51 FILE* client_open(const char* /*path*/, const char *mode)
53 return fopen("./oic_svr_db_client.dat", mode);
56 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
57 LPSTR lpCmdLine, int nCmdShow )
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);
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);
73 InitCommonControls(); // loads common control's DLL
81 hwndVolumeExpectedLabel =
87 LabelPrintf(hwndVolumeExpectedLabel,
88 "Expected Volume: %i",
125 hwndButtonBeginObserve =
127 ID_BUTTONBEGINOBSERVE,
131 hwndButtonCancelObserve =
133 ID_BUTTONCANCELOBSERVE,
139 ShowWindow(hwnd, nCmdShow);
142 while (GetMessage(&msg, NULL, 0, 0))
144 TranslateMessage(&msg);
145 DispatchMessage(&msg);
148 return (int) msg.wParam;
153 WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
157 WinUIClient::Media myMedia;
161 switch (LOWORD(wParam)) {
163 g_CurSliderVal = SendMessage(hwndVolumeSlider, TBM_GETPOS, 0, 0);
164 LabelPrintf(hwndVolumeExpectedLabel,"Volume: %i", g_CurSliderVal);
166 myMedia = app->GetMedia();
167 myMedia.m_volume = g_CurSliderVal;
168 app->SetMedia(myMedia.m_state, myMedia.m_volume);
169 app->PostMediaRepresentation();
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);
180 OCPersistentStorage ps = {client_open, fread, fwrite, fclose, unlink };
181 app = new WinUIClient::WinUIClientApp(ps);
193 GetClientRect(hwnd, &rect);
195 SelectObject((HDC)wParam, g_BkgndBrush);
196 Rectangle((HDC)hDC, rect.left, rect.top, rect.right, rect.bottom);
197 ReleaseDC(hwnd, hDC);
209 if (HIWORD(wParam) == BN_CLICKED)
212 switch (LOWORD(wParam))
214 case ID_BUTTONPLAYPAUSE:
215 myMedia = app->GetMedia();
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
223 myMedia.m_state = true;
224 app->SetMedia(myMedia.m_state, myMedia.m_volume);
225 myMedia.m_state = false;
227 app->PutMediaRepresentation();
230 app->FindResources();
233 app->GetMediaRepresentation();
236 myMedia = app->GetMedia();
237 myMedia.m_volume = g_CurSliderVal;
238 app->SetMedia(myMedia.m_state, myMedia.m_volume);
239 app->PutMediaRepresentation();
242 myMedia = app->GetMedia();
243 myMedia.m_volume = g_CurSliderVal;
244 app->SetMedia(myMedia.m_state, myMedia.m_volume);
245 app->PostMediaRepresentation();
247 case ID_BUTTONBEGINOBSERVE:
248 app->BeginObserving();
250 case ID_BUTTONCANCELOBSERVE:
251 app->CancelObserving();
257 return DefWindowProc(hwnd, msg, wParam, lParam);
263 #define GWL_HINSTANCE -6
264 HWND WINAPI CreateButton(HWND parent,
273 HWND hwndButton = CreateWindow(
274 "BUTTON", // Predefined class; Unicode assumed
275 caption, // Button text
276 WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
281 parent, // Parent window
282 (HMENU)id, // No menu.
283 (HINSTANCE)GetWindowLongPtr(parent, GWL_HINSTANCE),
284 NULL); // Pointer not needed.
289 HWND WINAPI CreateLabel(
290 HWND parent, // handle of parent window
298 HWND hwndPowerLabel = CreateWindow("static", "ST_U",
299 WS_CHILD | WS_VISIBLE | WS_TABSTOP,
301 parent, (HMENU)(501),
302 (HINSTANCE)GetWindowLongPtr(parent, GWL_HINSTANCE),
304 SetWindowText(hwndPowerLabel, lpText);
305 return hwndPowerLabel;
308 // CreateTrackbar - creates and initializes a trackbar.
311 // g_hinstTrackBar - instance handle
313 HWND WINAPI CreateTrackbar(
314 HWND parent, // handle of dialog box (parent window)
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
324 HWND hwndVolumeSlider = CreateWindowEx(
325 0, // no extended styles
326 TRACKBAR_CLASS, // class name
327 "Trackbar Control", // title (caption)
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
339 SendMessage(hwndVolumeSlider, TBM_SETRANGE,
340 (WPARAM) TRUE, // redraw flag
341 (LPARAM) MAKELONG(iMin, iMax)); // min. & max. positions
343 SendMessage(hwndVolumeSlider, TBM_SETPAGESIZE,
344 0, (LPARAM) 4); // new page size
346 SendMessage(hwndVolumeSlider, TBM_SETSEL,
347 (WPARAM) FALSE, // redraw flag
348 (LPARAM) MAKELONG(iSelMin, iSelMax));
350 SendMessage(hwndVolumeSlider, TBM_SETPOS,
351 (WPARAM) TRUE, // redraw flag
354 SetFocus(hwndVolumeSlider);
356 return hwndVolumeSlider;
359 void LabelPrintf (HWND hwndEdit, TCHAR * szFormat, ...)
361 TCHAR szBuffer [1024] ;
364 va_start (pArgList, szFormat) ;
365 wvsprintf (szBuffer, szFormat, pArgList) ;
368 SetWindowText(hwndEdit, szBuffer);