Fixing init result issue.
1 //------------------------------------------------------------------------------
3 #include "iMONDisplayAPI.h"
4 //------------------------------------------------------------------------------
5 #define CLASSNAME TEXT("IDW_IMON_COM_WNDCLASS")
6 //------------------------------------------------------------------------------
7 #define WM_IDW_IMON (WM_USER + 1)
8 #define WM_IDW_INIT (WM_USER + 2)
9 #define WM_IDW_UNINIT (WM_USER + 3)
10 #define WM_IDW_ISINITED (WM_USER + 4)
11 #define WM_IDW_ISPLUGINMODEENABLED (WM_USER + 5)
12 #define WM_IDW_SETVFDTEXT (WM_USER + 6)
13 #define WM_IDW_SETVFDEQ (WM_USER + 7)
14 #define WM_IDW_SETLCDTEXT (WM_USER + 8)
15 #define WM_IDW_SETLCDEQ (WM_USER + 9)
16 #define WM_IDW_SETLCDALLICONS (WM_USER + 10)
17 #define WM_IDW_SETLCDORANGEICON (WM_USER + 11)
18 #define WM_IDW_SETLCDMEDIATYPEICON (WM_USER + 12)
19 #define WM_IDW_SETLCDSPEAKERICON (WM_USER + 13)
20 #define WM_IDW_SETLCDVIDEOCODEC (WM_USER + 14)
21 #define WM_IDW_SETLCDAUDIOCODEC (WM_USER + 15)
22 #define WM_IDW_SETLCDASPECTRATIO (WM_USER + 16)
23 #define WM_IDW_SETLCDETCICON (WM_USER + 17)
24 #define WM_IDW_SETLCDPROGRESS (WM_USER + 18)
25 #define WM_IDW_STATUS (WM_USER + 19)
26 #define WM_IDW_INTERRUPT (WM_USER + 100)
27 //------------------------------------------------------------------------------
30 iMON Display initialization request.
32 class IdwImonInitRequest : public WindowEvent
35 IdwImonInitRequest(HWND aWnd, UINT aMsg): WindowEvent(aWnd,aMsg) {}
37 virtual LRESULT DoExecute();
38 void HandleImonNotifications(WPARAM wParam, LPARAM lParam);
40 DSPNotifyCode iNotification;
42 DSPNInitResult iInitResult;
44 static const UINT KMsgIdImonNotification = WM_IDW_IMON;
49 iMON Display status request.
50 Retrieve the latest iMON notification code.
52 class IdwImonStatusRequest : public WindowEvent
55 IdwImonStatusRequest(HWND aWnd, UINT aMsg): WindowEvent(aWnd,aMsg) {}
57 virtual LRESULT DoExecute();
60 DSPNotifyCode iNotification;
85 struct IdwSetLcdAllIcons
95 struct IdwSetLcdIcons2
101 struct IdwSetLcdProgress
107 //------------------------------------------------------------------------------
108 IdwThread::IdwThread(HINSTANCE hInstance)
109 : m_hInstance(hInstance)
113 //------------------------------------------------------------------------------
114 IdwThread::~IdwThread()
117 //------------------------------------------------------------------------------
118 void IdwThread::Interrupt()
120 if (!WaitForWindow())
122 PostMessage(m_hWnd, WM_IDW_INTERRUPT, 0, 0);
126 Issue iMON initialization request.
127 @param [in, out] Contains our request results on return.
129 DSPResult IdwThread::Init(IDW_INITRESULT* pInitResult)
131 if (!WaitForWindow())
134 IdwImonInitRequest request(m_hWnd, WM_IDW_INIT);
137 if (pInitResult != NULL)
139 pInitResult->iInitResult = request.iInitResult;
140 pInitResult->iDspType = request.iDspType;
141 pInitResult->iNotification = request.iNotification;
144 return request.iResult;
146 //------------------------------------------------------------------------------
147 DSPResult IdwThread::Uninit()
149 if (!WaitForWindow())
154 PostMessage(m_hWnd, WM_IDW_UNINIT, (WPARAM)&result, (LPARAM)&finished);
159 //------------------------------------------------------------------------------
160 DSPResult IdwThread::IsInitialized()
162 if (!WaitForWindow())
167 PostMessage(m_hWnd, WM_IDW_ISINITED, (WPARAM)&result, (LPARAM)&finished);
174 Issue iMON initialization request.
175 @param [in, out] Contains our request results on return.
177 DSPResult IdwThread::GetStatus(IDW_STATUS* aStatus)
179 if (!WaitForWindow())
184 IdwImonStatusRequest request(m_hWnd, WM_IDW_STATUS);
189 aStatus->iNotification = request.iNotification;
192 return request.iResult;
195 //------------------------------------------------------------------------------
196 DSPResult IdwThread::IsPluginModeEnabled()
198 if (!WaitForWindow())
203 PostMessage(m_hWnd, WM_IDW_ISPLUGINMODEENABLED, (WPARAM)&result,
209 //------------------------------------------------------------------------------
210 DSPResult IdwThread::SetVfdText(LPCWSTR lpszLine1, LPCWSTR lpszLine2)
212 if (!WaitForWindow())
216 text.pszLine1 = lpszLine1;
217 text.pszLine2 = lpszLine2;
219 PostMessage(m_hWnd, WM_IDW_SETVFDTEXT, (WPARAM)&text, (LPARAM)&finished);
224 //------------------------------------------------------------------------------
225 DSPResult IdwThread::SetVfdEqData(PDSPEQDATA pEqData)
227 if (!WaitForWindow())
231 eq.pEqData = pEqData;
233 PostMessage(m_hWnd, WM_IDW_SETVFDEQ, (WPARAM)&eq, (LPARAM)&finished);
238 //------------------------------------------------------------------------------
239 DSPResult IdwThread::SetLcdText(LPCWSTR lpszLine1)
241 if (!WaitForWindow())
245 text.pszLine1 = lpszLine1;
247 PostMessage(m_hWnd, WM_IDW_SETLCDTEXT, (WPARAM)&text, (LPARAM)&finished);
252 //------------------------------------------------------------------------------
253 DSPResult IdwThread::SetLcdEqData(PDSPEQDATA pEqDataL, PDSPEQDATA pEqDataR)
255 if (!WaitForWindow())
259 eq.pEqDataL = pEqDataL;
260 eq.pEqDataR = pEqDataR;
262 PostMessage(m_hWnd, WM_IDW_SETLCDEQ, (WPARAM)&eq, (LPARAM)&finished);
267 //------------------------------------------------------------------------------
268 DSPResult IdwThread::SetLcdAllIcons(BOOL bOn)
270 if (!WaitForWindow())
273 IdwSetLcdAllIcons allIcons;
276 PostMessage(m_hWnd, WM_IDW_SETLCDALLICONS,
277 (WPARAM)&allIcons, (LPARAM)&finished);
280 return allIcons.result;
282 //------------------------------------------------------------------------------
283 DSPResult IdwThread::SetLcdOrangeIcon(BYTE btIconData1, BYTE btIconData2)
285 if (!WaitForWindow())
288 IdwSetLcdIcons2 iconData;
289 iconData.btIconData1 = btIconData1;
290 iconData.btIconData2 = btIconData2;
292 PostMessage(m_hWnd, WM_IDW_SETLCDORANGEICON,
293 (WPARAM)&iconData, (LPARAM)&finished);
296 return iconData.result;
298 //------------------------------------------------------------------------------
299 DSPResult IdwThread::SetLcdMediaTypeIcon(BYTE btIconData)
301 if (!WaitForWindow())
304 IdwSetLcdIcons iconData;
305 iconData.btIconData = btIconData;
307 PostMessage(m_hWnd, WM_IDW_SETLCDMEDIATYPEICON,
308 (WPARAM)&iconData, (LPARAM)&finished);
311 return iconData.result;
313 //------------------------------------------------------------------------------
314 DSPResult IdwThread::SetLcdSpeakerIcon(BYTE btIconData1, BYTE btIconData2)
316 if (!WaitForWindow())
319 IdwSetLcdIcons2 iconData;
320 iconData.btIconData1 = btIconData1;
321 iconData.btIconData2 = btIconData2;
323 PostMessage(m_hWnd, WM_IDW_SETLCDSPEAKERICON,
324 (WPARAM)&iconData, (LPARAM)&finished);
327 return iconData.result;
329 //------------------------------------------------------------------------------
330 DSPResult IdwThread::SetLcdVideoCodecIcon(BYTE btIconData)
332 if (!WaitForWindow())
335 IdwSetLcdIcons iconData;
336 iconData.btIconData = btIconData;
338 PostMessage(m_hWnd, WM_IDW_SETLCDVIDEOCODEC,
339 (WPARAM)&iconData, (LPARAM)&finished);
342 return iconData.result;
344 //------------------------------------------------------------------------------
345 DSPResult IdwThread::SetLcdAudioCodecIcon(BYTE btIconData)
347 if (!WaitForWindow())
350 IdwSetLcdIcons iconData;
351 iconData.btIconData = btIconData;
353 PostMessage(m_hWnd, WM_IDW_SETLCDAUDIOCODEC,
354 (WPARAM)&iconData, (LPARAM)&finished);
357 return iconData.result;
359 //------------------------------------------------------------------------------
360 DSPResult IdwThread::SetLcdAspectRatioIcon(BYTE btIconData)
362 if (!WaitForWindow())
365 IdwSetLcdIcons iconData;
366 iconData.btIconData = btIconData;
368 PostMessage(m_hWnd, WM_IDW_SETLCDASPECTRATIO,
369 (WPARAM)&iconData, (LPARAM)&finished);
372 return iconData.result;
374 //------------------------------------------------------------------------------
375 DSPResult IdwThread::SetLcdEtcIcon(BYTE btIconData)
377 if (!WaitForWindow())
380 IdwSetLcdIcons iconData;
381 iconData.btIconData = btIconData;
383 PostMessage(m_hWnd, WM_IDW_SETLCDETCICON,
384 (WPARAM)&iconData, (LPARAM)&finished);
387 return iconData.result;
389 //------------------------------------------------------------------------------
390 DSPResult IdwThread::SetLcdProgress(int nCurPos, int nTotal)
392 if (!WaitForWindow())
395 IdwSetLcdProgress progressData;
396 progressData.nCurPos = nCurPos;
397 progressData.nTotal = nTotal;
399 PostMessage(m_hWnd, WM_IDW_SETLCDPROGRESS,
400 (WPARAM)&progressData, (LPARAM)&finished);
403 return progressData.result;
405 //------------------------------------------------------------------------------
406 void IdwThread::Run()
408 if (!RegisterClass())
410 m_eventWindowCreationDone.Signal();
413 if (!CreateMessageWindow())
415 m_eventWindowCreationDone.Signal();
420 m_eventWindowCreationDone.Signal();
424 while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0
425 && fGotMessage != -1)
427 TranslateMessage(&msg);
428 DispatchMessage(&msg);
431 //------------------------------------------------------------------------------
432 bool IdwThread::RegisterClass()
435 if (GetClassInfoEx(m_hInstance, CLASSNAME, &wc))
440 wc.cbSize = sizeof(wc);
441 wc.style = CS_HREDRAW | CS_VREDRAW;
442 wc.lpfnWndProc = IdwThread::WndProc;
445 wc.hInstance = m_hInstance;
448 wc.hbrBackground = NULL;
449 wc.lpszMenuName = NULL;
450 wc.lpszClassName = CLASSNAME;
452 if (::RegisterClassEx(&wc))
458 //------------------------------------------------------------------------------
459 bool IdwThread::CreateMessageWindow()
461 m_hWnd = CreateWindow(
463 TEXT("MP iMON MessageWindow"),
475 //------------------------------------------------------------------------------
476 void IdwThread::AllowImonMessages()
479 OSVERSIONINFO version;
480 version.dwOSVersionInfoSize = sizeof(version);
481 GetVersionEx(&version);
482 if (version.dwMajorVersion < 6)
487 // Determine and allow iMON message number
489 RegisterWindowMessage(
490 TEXT("iMonMessage-431F1DC6-F31A-4AC6-A1FA-A4BB9C44FF10"));
491 ChangeWindowMessageFilter(iMonMsg, MSGFLT_ADD);
493 //------------------------------------------------------------------------------
494 bool IdwThread::WaitForWindow()
496 m_eventWindowCreationDone.Await();
501 //------------------------------------------------------------------------------
502 LRESULT CALLBACK IdwThread::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
504 static IdwImonInitRequest* initRequest = NULL;
505 static DSPNotifyCode notification = DSPNM_PLUGIN_FAILED;
518 //Notifications from iMON Display API
519 case IdwImonInitRequest::KMsgIdImonNotification:
520 notification=(DSPNotifyCode)wParam; //Keep track of our current status
523 initRequest->HandleImonNotifications(wParam,lParam);
530 initRequest = (IdwImonInitRequest*)wParam;
531 LRESULT res=initRequest->Execute(uMsg);
534 //Request has been completed
541 pResult = (DSPResult*)wParam;
542 pFinished = (Event*)lParam;
543 *pResult = IMON_Display_Uninit();
547 case WM_IDW_ISINITED:
548 pResult = (DSPResult*)wParam;
549 pFinished = (Event*)lParam;
550 *pResult = IMON_Display_IsInited();
554 case WM_IDW_ISPLUGINMODEENABLED:
555 pResult = (DSPResult*)wParam;
556 pFinished = (Event*)lParam;
557 *pResult = IMON_Display_IsPluginModeEnabled();
563 IdwImonStatusRequest* request = (IdwImonStatusRequest*)wParam;
564 request->iNotification = notification; //Get our notification code
565 request->iResult = DSP_SUCCEEDED;
566 LRESULT res=request->Execute(uMsg);
570 case WM_IDW_SETVFDTEXT:
572 IdwSetVfdText* pSetVfdText = (IdwSetVfdText*)wParam;
575 pFinished = (Event*)lParam;
576 MapChars(szLine1, pSetVfdText->pszLine1, 16);
577 MapChars(szLine2, pSetVfdText->pszLine2, 16);
578 pSetVfdText->result = IMON_Display_SetVfdText(szLine1, szLine2);
583 case WM_IDW_SETVFDEQ:
585 IdwSetVfdEq* pSetVfdEq = (IdwSetVfdEq*)wParam;
586 pFinished = (Event*)lParam;
587 pSetVfdEq->result = IMON_Display_SetVfdEqData(pSetVfdEq->pEqData);
592 case WM_IDW_SETLCDTEXT:
594 IdwSetLcdText* pSetLcdText = (IdwSetLcdText*)wParam;
595 pFinished = (Event*)lParam;
596 pSetLcdText->result = IMON_Display_SetLcdText(pSetLcdText->pszLine1);
601 case WM_IDW_SETLCDEQ:
603 IdwSetLcdEq* pSetLcdEq = (IdwSetLcdEq*)wParam;
604 pFinished = (Event*)lParam;
606 IMON_Display_SetLcdEqData(pSetLcdEq->pEqDataL, pSetLcdEq->pEqDataR);
611 case WM_IDW_SETLCDALLICONS:
613 IdwSetLcdAllIcons* pSetLcdAllIcons = (IdwSetLcdAllIcons*)wParam;
614 pFinished = (Event*)lParam;
615 pSetLcdAllIcons->result =
616 IMON_Display_SetLcdAllIcons(pSetLcdAllIcons->bOn);
621 case WM_IDW_SETLCDORANGEICON:
623 IdwSetLcdIcons2* pSetLcdIcons2 = (IdwSetLcdIcons2*)wParam;
624 pFinished = (Event*)lParam;
625 pSetLcdIcons2->result = IMON_Display_SetLcdOrangeIcon(
626 pSetLcdIcons2->btIconData1, pSetLcdIcons2->btIconData2);
631 case WM_IDW_SETLCDMEDIATYPEICON:
633 IdwSetLcdIcons* pSetLcdIcons = (IdwSetLcdIcons*)wParam;
634 pFinished = (Event*)lParam;
635 pSetLcdIcons->result =
636 IMON_Display_SetLcdMediaTypeIcon(pSetLcdIcons->btIconData);
641 case WM_IDW_SETLCDSPEAKERICON:
643 IdwSetLcdIcons2* pSetLcdIcons2 = (IdwSetLcdIcons2*)wParam;
644 pFinished = (Event*)lParam;
645 pSetLcdIcons2->result = IMON_Display_SetLcdSpeakerIcon(
646 pSetLcdIcons2->btIconData1, pSetLcdIcons2->btIconData2);
651 case WM_IDW_SETLCDVIDEOCODEC:
653 IdwSetLcdIcons* pSetLcdIcons = (IdwSetLcdIcons*)wParam;
654 pFinished = (Event*)lParam;
655 pSetLcdIcons->result =
656 IMON_Display_SetLcdVideoCodecIcon(pSetLcdIcons->btIconData);
661 case WM_IDW_SETLCDAUDIOCODEC:
663 IdwSetLcdIcons* pSetLcdIcons = (IdwSetLcdIcons*)wParam;
664 pFinished = (Event*)lParam;
665 pSetLcdIcons->result =
666 IMON_Display_SetLcdAudioCodecIcon(pSetLcdIcons->btIconData);
671 case WM_IDW_SETLCDASPECTRATIO:
673 IdwSetLcdIcons* pSetLcdIcons = (IdwSetLcdIcons*)wParam;
674 pFinished = (Event*)lParam;
675 pSetLcdIcons->result =
676 IMON_Display_SetLcdAspectRatioIcon(pSetLcdIcons->btIconData);
681 case WM_IDW_SETLCDETCICON:
683 IdwSetLcdIcons* pSetLcdIcons = (IdwSetLcdIcons*)wParam;
684 pFinished = (Event*)lParam;
685 pSetLcdIcons->result =
686 IMON_Display_SetLcdEtcIcon(pSetLcdIcons->btIconData);
691 case WM_IDW_SETLCDPROGRESS:
693 IdwSetLcdProgress* pSetLcdProgress = (IdwSetLcdProgress*)wParam;
694 pFinished = (Event*)lParam;
695 pSetLcdProgress->result = IMON_Display_SetLcdProgress(
696 pSetLcdProgress->nCurPos, pSetLcdProgress->nTotal);
701 case WM_IDW_INTERRUPT:
705 return DefWindowProc(hwnd, uMsg, wParam, lParam);
707 //------------------------------------------------------------------------------
708 void IdwThread::MapChars(LPWSTR lpszTarget, LPCWSTR lpszSource,
711 int len = (int)wcslen(lpszSource);
712 if ((nMaxLength > 0) && (len > nMaxLength))
715 for (int i = 0; i < len; ++i)
717 wchar_t ch = lpszSource[i];
718 lpszTarget[i] = MapChar(ch);
721 //------------------------------------------------------------------------------
722 #define IN_RANGE(ch, s, e) ((ch >= s) && (ch <= e))
723 #define IS(ch, c) (ch == c)
724 wchar_t IdwThread::MapChar(wchar_t ch)
726 if (IN_RANGE(ch, 0x0020, 0x005B)
727 || IN_RANGE(ch, 0x005D, 0x007D)
730 || IN_RANGE(ch, 0x0406, 0x0407)
731 || IN_RANGE(ch, 0x0410, 0x044F)
734 || IN_RANGE(ch, 0x0456, 0x0457)
735 || IN_RANGE(ch, 0x0490, 0x0491))
808 Handle iMON notifications
810 void IdwImonInitRequest::HandleImonNotifications(WPARAM wParam, LPARAM lParam)
812 iNotification=(DSPNotifyCode)wParam;
813 iDspType=DSPN_DSP_NONE;
814 iResult= DSP_SUCCEEDED;
816 switch (iNotification)
818 case DSPNM_PLUGIN_SUCCEED:
819 case DSPNM_IMON_RESTARTED:
820 case DSPNM_HW_CONNECTED:
821 //lParam is display type
822 iDspType = (DSPType)lParam;
823 iInitResult = DSPN_SUCCEEDED;
825 case DSPNM_PLUGIN_FAILED:
826 case DSPNM_HW_DISCONNECTED:
827 case DSPNM_IMON_CLOSED:
829 iInitResult = (DSPNInitResult)lParam;
831 case DSPNM_LCD_TEXT_SCROLL_DONE:
832 //That should not happen, report an error
833 iResult = DSP_E_FAIL;
837 //Complete our request
843 Run our display initialization.
845 LRESULT IdwImonInitRequest::DoExecute()
847 iResult = IMON_Display_Init(iWnd, KMsgIdImonNotification);
848 if (iResult != DSP_SUCCEEDED)
850 iNotification = DSPNM_PLUGIN_FAILED;
851 iInitResult = DSPN_ERR_UNKNOWN;
852 iDspType = DSPN_DSP_NONE;
863 LRESULT IdwImonStatusRequest::DoExecute()