home *** CD-ROM | disk | FTP | other *** search
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- Copyright (c) 1998 Microsoft Corporation
-
- Module Name:
-
- appsink.cpp
-
- Abstract:
-
- Keyboard Sample application sinks. Implements two classes,
- CFormEventSink and CAppMessageSink.
-
- Environment:
-
- AutoPC
-
- -------------------------------------------------------------------*/
- // System specific includes
- #include <Windows.h>
- #include <asfc.h>
- #include <apcaudio.h>
- #include <ascmnctl.h>
- #include <keypad.h>
- #include <asstyles.h>
-
- // Application specific includes
- #include "resource.h"
- #include "appsink.h"
- #include "app.h"
-
- // Global Application object
- extern CKeyboardApp* g_pApp;
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Class:
- CKeyboardApp
-
- Description:
- Main application class.
- -------------------------------------------------------------------*/
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CKeybaordApp::CKeyboardApp
-
- Description:
- Constructor. Initializes all member variables.
-
- Parameters:
- HINSTANCE - Applications instance handle.
-
- Returns:
- Nothing.
- -------------------------------------------------------------------*/
- CKeyboardApp::CKeyboardApp(HINSTANCE hInst)
- {
- m_hInst = hInst;
- m_pFormEventSink = NULL;
- m_pAppMessageSink = NULL;
- m_pAppEventSink = NULL;
- m_bszAppName = NULL;
- m_pManage = NULL;
- m_pFmSys = NULL;
- m_hFC = NULL;
- m_xres = NULL;
- m_yres = NULL;
- m_pForm = NULL;
- m_pLabelCodes = NULL;
- m_pLabelSystem = NULL;
-
- m_idThread = GetCurrentThreadId(); // save the id of the main thread
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CKeybaordApp::CKeyboardApp
-
- Description:
- Destructor. Releases form and forms manager interfaces. Frees
- all strings.
-
- Parameters:
- Nothing.
-
- Returns:
- Nothing.
- -------------------------------------------------------------------*/
- CKeyboardApp::~CKeyboardApp()
- {
- if (m_pManage)
- {
- m_pManage->DeregisterStartedApplication(m_hFC, m_bszAppName);
- }
-
- // Release all the controls
- DeleteFormControls();
-
- if (m_pForm)
- {
- m_pForm->Close(); // Close form
- m_pForm->Release(); // Release the form
- }
-
- if (m_bszAppName) SysFreeString(m_bszAppName);
-
- if (m_pFmSys) m_pFmSys->Release();
-
- if (m_pManage){
- m_pManage->Release();
- }
-
- if (m_pFormEventSink){
- m_pFormEventSink->Release();
- }
-
- if (m_pAppMessageSink){
- m_pAppMessageSink->Release();
- }
-
- if (m_pAppEventSink){
- m_pAppEventSink->Release();
- }
-
- UnInitASFormsManager(); // uninitialize the forms manager
- CoUninitialize(); // uninitialize COM
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CKeybaordApp::Init
-
- Description:
- Main App initialization. Creates sinks, forms manager, and form.
-
- Parameters:
- Nothing.
-
- Returns:
- TRUE on success, FALSE on failure.
- -------------------------------------------------------------------*/
- BOOL
- CKeyboardApp::Init()
- {
- HRESULT hr;
-
- CoInitializeEx(NULL, COINIT_MULTITHREADED); // Initialize COM
-
- hr = InitASFormsManager(); // Initialize the forms manager
- if(FAILED(hr))
- return FALSE;
-
- // Load the App name from the resource script
- if(!LoadBSTR(STR_APP_SHELL_NAME, &m_bszAppName))
- return FALSE;
-
- hr = CreateSinks();
- if(FAILED(hr))
- return FALSE;
-
- hr = GetFormsManager();
- if(FAILED(hr))
- return FALSE;
-
- hr = CreateMainForm();
- if(FAILED(hr))
- return FALSE;
-
- return TRUE;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CKeybaordApp::CreateMainForm
-
- Description:
- Creates the main form and adds all controls to it.
-
- Parameters:
- Nothing.
-
- Returns:
- Returns HR failure code or NOERROR on success.
- -------------------------------------------------------------------*/
- HRESULT
- CKeyboardApp::CreateMainForm()
- {
- BSTR bstr;
- HRESULT hr;
-
- bstr = NULL;
-
- // Create the form
- hr = CoCreateInstance(CLSID_ASFORM, // COM object class identifier
- NULL, // NOT part of an aggregate
- CLSCTX_INPROC_SERVER, // Create object in our process
- IID_ASFORM, // Get the IASForm interface
- (PVOID *) &m_pForm); // Put pointer into this variable
- if (FAILED(hr))
- goto LReturn;
-
- // initialize the newly created form
- hr = m_pForm->Init(ID_MAINFORM, // Form ID
- NULL, // Parent Control's IDispatch *
- m_pFormEventSink); // Event sink to attach to form
- if(FAILED(hr))
- goto LReturn;
-
- hr = m_pManage->Start(m_pForm); // start the form
- if(FAILED(hr))
- goto LReturn;
-
- // Attach the app's ClassMsgSink sink and the EventSink to the forms manager.
- hr = m_pManage->put_ClassMsgSink(m_pAppMessageSink);
- if(FAILED(hr))
- goto LReturn;
-
- hr = m_pManage->put_EventSink(m_pAppEventSink);
- if(FAILED(hr))
- goto LReturn;
-
- m_pForm->put_Visible(TRUE);
-
- hr = m_pForm->put_Caption(m_bszAppName);
- if(FAILED(hr))
- goto LReturn;
-
- // create the forms controls
- hr = CreateFormControls();
- if(FAILED(hr))
- goto LReturn;
-
- // Register the application with the forms manager.
- hr = m_pManage->RegisterStartedApplication(m_hFC, m_bszAppName, 0, 0);
- if (FAILED(hr))
- goto LReturn;
-
- // Bring the app to the foreground (in case it wasn't started from shell)
- hr = m_pManage->MoveAppToForeground(GetCurrentProcessId(),0,0);
- if (FAILED(hr))
- goto LReturn;
-
- return NOERROR;
-
- LReturn:
- // there was an error. Cleanup
- if (m_pForm)
- {
- m_pForm->Close(); // close the form
- m_pForm->Release();
- m_pForm = NULL;
- }
-
- return hr;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CKeybaordApp::LoadBSTR
-
- Description:
- Loads a resource string into a system allocated buffer.
-
- Parameters:
- UINT - Resource ID
- BSTR * - New string put here
-
- Returns:
- TRUE on success, FALSE on failure.
- -------------------------------------------------------------------*/
- BOOL
- CKeyboardApp::LoadBSTR(UINT uID, BSTR* pBStr)
- {
- if(!pBStr)
- return FALSE;
-
- WCHAR wszTemp[128];
- int iStringLen = 128;
-
- if((iStringLen = LoadStringW(m_hInst, uID, wszTemp, iStringLen)) == 0)
- return(FALSE); // Something went wrong
-
- *pBStr = SysAllocString(wszTemp);
- if (!(*pBStr))
- return(FALSE); // Low memory probably
-
- return(TRUE);
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CKeybaordApp::CreateSinks
-
- Description:
- Creates a new CFormEventSink and CAppMessageSink object. It then
- gets an IASEventSink pointer from CFormEventSink and IASEventSink
- IASClassMsgSink pointers from CAppMessageSink.
-
- Parameters:
- Nothing.
-
- Returns:
- Returns HR failure code or NOERROR on success.
- -------------------------------------------------------------------*/
- HRESULT
- CKeyboardApp::CreateSinks()
- {
- HRESULT hr;
- CFormEventSink * pFormEventSink = NULL;
- CAppMessageSink * pMessageSink = NULL;
-
- pFormEventSink = new CFormEventSink(m_idThread); // create an event sink object
-
- if(!pFormEventSink)
- return E_OUTOFMEMORY;
-
- hr = pFormEventSink->QueryInterface(IID_ASEVENTSINK, // Requested interface
- (PVOID *) &m_pFormEventSink); // Place to put it
- if(FAILED(hr))
- {
- delete pFormEventSink;
- return E_FAIL;
- }
-
- pMessageSink = new CAppMessageSink(m_idThread); // create a message sink object
- if(!pMessageSink)
- return E_OUTOFMEMORY;
-
- hr = pMessageSink->QueryInterface(IID_ASCLASSMSGSINK, // Requested interface
- (PVOID *) &m_pAppMessageSink); // Place to put it
- if(FAILED(hr))
- {
- delete pMessageSink;
- return E_FAIL;
- }
-
- hr = pMessageSink->QueryInterface(IID_ASEVENTSINK, // Requested interface
- (PVOID *) &m_pAppEventSink); // Place to put it
- if(FAILED(hr))
- {
- delete pMessageSink;
- return E_FAIL;
- }
-
- return NOERROR;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CKeybaordApp::GetFormsManager
-
- Description:
- Creates the IfmManage COM interface.
-
- Parameters:
- Nothing.
-
- Returns:
- Returns HR failure code or NOERROR on success.
- -------------------------------------------------------------------*/
- HRESULT
- CKeyboardApp::GetFormsManager()
- {
- HRESULT hr;
-
- // Create the forms manager COM object
- hr = CoCreateInstance(CLSID_FMMANAGE, // Forms Manager Class ID
- NULL, // Not part of an aggregate
- CLSCTX_INPROC_SERVER, // In our process
- IID_FMMANAGE, // Request the IfmManage interface
- (PVOID *) &m_pManage); // Put it here
- if(FAILED(hr))
- return hr;
-
- // Forms Context Handle required to register application
- hr = m_pManage->GetFormsContextHandle(&m_hFC);
- if(FAILED(hr))
- return hr;
-
- hr = m_pManage->QueryInterface(IID_FMSYSTEM, (PVOID *) &m_pFmSys);
- if(FAILED(hr))
- return hr;
-
- // Get some simple system metrics, x resolution and y resolution
- hr = m_pFmSys->GetFormDisplayCaps(m_hFC, VERTRES, &m_yres);
- if(FAILED(hr))
- return hr;
-
- hr = m_pFmSys->GetFormDisplayCaps(m_hFC, HORZRES, &m_xres);
- if(FAILED(hr))
- return hr;
-
- return NOERROR;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CKeybaordApp::DeleteFormControls
-
- Description:
- Releases all the form controls.
-
- Parameters:
- Nothing.
-
- Returns:
- Nothing.
- -------------------------------------------------------------------*/
- void
- CKeyboardApp::DeleteFormControls()
- {
-
- if (m_pLabelCodes)
- {
- m_pLabelCodes->Release();
- m_pLabelCodes=NULL;
- }
-
- if (m_pLabelSystem)
- {
- m_pLabelSystem->Release();
- m_pLabelSystem=NULL;
- }
-
- return;
- }
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CKeybaordApp::CreateFormControls
-
- Description:
- Creates all the controls on the main form.
-
- Parameters:
- Nothing.
-
- Returns:
- Returns HR failure code or NOERROR on success.
- -------------------------------------------------------------------*/
- HRESULT
- CKeyboardApp::CreateFormControls()
- {
- HRESULT hr;
- BSTR bstr;
-
- // create the first label control
- hr=CoCreateInstance(CLSID_ASLABEL, // IASLabel class ID
- NULL, // Not part of an aggregate
- CLSCTX_INPROC_SERVER, // In our process
- IID_ASLABEL, // We request the IASLabel interface
- (PVOID *)&m_pLabelCodes);//Put the interface here.
- if(FAILED(hr))
- return hr;
-
- // create the second label control
- hr=CoCreateInstance(CLSID_ASLABEL, // IASLabel class ID
- NULL, // Not part of an aggregate
- CLSCTX_INPROC_SERVER, // In our process
- IID_ASLABEL, // We request the IASLabel interface
- (PVOID *)&m_pLabelSystem);// put the interface here
- if(FAILED(hr))
- return hr;
-
- // Set some of the properties of the first label; placement, alignment, and ForeColor
- m_pLabelCodes->SetBounds(10,20,240,40);
- m_pLabelCodes->put_Format(ASFC_ALIGN_LEFT | ASFC_ALIGN_WORDBREAK);
- m_pLabelCodes->put_ForeColor(RGB(255,0,0));
-
- // Get blank text string and set label text
- if(LoadBSTR(IDS_NULL, &bstr))
- {
- m_pLabelCodes->put_Caption(bstr);
- SysFreeString(bstr);
- }
-
- // Set some of the properties of the second label; placement, alignment, and ForeColor
- m_pLabelSystem->SetBounds(10,40,240,60);
- m_pLabelSystem->put_Format(ASFC_ALIGN_LEFT | ASFC_ALIGN_WORDBREAK);
- m_pLabelSystem->put_ForeColor(RGB(0,0,255));
-
- // Get blank text string and set label text
- if(LoadBSTR(IDS_NULL, &bstr))
- {
- m_pLabelSystem->put_Caption(bstr);
- SysFreeString(bstr);
- }
-
- // Add the two new controls to the form
- hr = m_pForm->Add(m_pLabelCodes, IDC_LABELCODES);
- if(FAILED(hr))
- return hr;
- hr = m_pForm->Add(m_pLabelSystem, IDC_LABELSYSTEM);
- if(FAILED(hr))
- return hr;
-
- return NOERROR;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CKeybaordApp::ProcessMessage
-
- Description:
- This function is called from the IASClassMsgSink attached to the
- forms manager.
-
- Parameters:
- long - Win32 Message
- long - Parameter
- long - Parameter
-
- Returns:
- Nothing.
- -------------------------------------------------------------------*/
- void
- CKeyboardApp::ProcessMessage(long uMsg, long wParam, long lParam)
- {
- WCHAR wtext[80]; // Buffer used later to set text labels
-
- switch (uMsg)
- {
- default:
- break;
-
- // Each message we want to display has a case block that displays
- // the message (or calls a function that displays the message)
- case WM_REMOTE_KEYDOWN: // Key pressed down
- case WM_REMOTE_KEYUP: // Key lifted
- UpdateKeyString(uMsg, wParam, lParam);
- break;
-
- case WM_HIBERNATE: // Application asked to hibernate
- UpdateSystemLabel(TEXT("WM_HIBERNATE"));
- break;
-
- case WM_APCSYSMSG_VOLUMECHANGED: // System volume changed
- UpdateSystemLabel(TEXT("Vol Changed"));
- break;
-
- case WM_APCSYSMSG_MUTE: // Mute turned on/off
- wsprintf(wtext, TEXT("Mute %s"), lParam ? TEXT("on") : TEXT("off"));
- UpdateSystemLabel(wtext);
- UpdateKeyLabel(TEXT(""));
- break;
-
- case WM_APCSYSMSG_POWERSTATECHANGE: // Power State changed
- lstrcpy(wtext, TEXT(""));
- switch (wParam)
- {
- default:
- break;
-
- case APC_PWRNOTIFYTYPE_USER:
- lstrcpy(wtext, TEXT("User Power "));
- break;
-
- case APC_PWRNOTIFYTYPE_ELECTRICAL:
- lstrcpy(wtext, TEXT("System Power "));
- break;
- }
- lstrcat(wtext, lParam ? TEXT("on") : TEXT("off"));
- UpdateSystemLabel(wtext);
- UpdateKeyLabel(TEXT(""));
- break;
-
- case WM_APCSYSMSG_MEDIASTATECHANGE: // CD insterted/ejected
- lstrcpy(wtext, TEXT(""));
- switch (HIWORD(wParam))
- {
- case APC_MEDIATYPE_CD:
- switch (HIWORD (lParam))
- {
- default:
- break;
-
- case AAM_SRC_CD:
- lstrcat(wtext, TEXT("Dash CD "));
- break;
-
- case AAM_SRC_CDCHANGER:
- lstrcat(wtext, TEXT("CD Chngr "));
- break;
-
- }
- break;
- }
- lstrcat (wtext , (LOWORD(wParam) & APC_FLG_MEDIAINSERT) ? TEXT("Insert ") : TEXT("Eject "));
- lstrcat (wtext , (LOWORD(wParam) & APC_FLG_MEDIAHASAUDIO) ? TEXT("Audio ") : TEXT(""));
- lstrcat (wtext , (LOWORD(wParam) & APC_FLG_MEDIAHASDATA) ? TEXT("Data ") : TEXT(""));
- UpdateSystemLabel(wtext);
- break;
-
- case WM_SETTINGCHANGE: // System setting changed by another app
- lstrcpy(wtext, TEXT("Setting Change "));
- switch (wParam)
- {
- default:
- lstrcat(wtext, TEXT("Unknown"));
- break;
-
- case SPI_SETAPCSHELL:
- lstrcat(wtext, TEXT("Shell"));
- break;
-
- case SPI_SETAPCSCREENSAVER:
- lstrcat(wtext, TEXT("Screen Saver"));
- break;
-
- case SPI_SETAPCAUDIO:
- lstrcat(wtext, TEXT("Audio"));
- break;
-
- case SPI_SETAPCLOCALE:
- lstrcat(wtext, TEXT("Locale"));
- break;
- }
- UpdateSystemLabel(wtext);
- break;
- }
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CKeybaordApp::ProcessMessage
-
- Description:
- This function is called from ProcessMessage whenever a Keyup/Down
- message is received.
-
- Parameters:
- long - Win32 Message
- long - Parameter
- long - Parameter
-
- Returns:
- Nothing.
- -------------------------------------------------------------------*/
- void
- CKeyboardApp::UpdateKeyString(long uMsg, long wParam, long lParam)
- {
- WCHAR wsztemp[80];
-
- // first interpret the key up or down
- switch (uMsg)
- {
- case WM_REMOTE_KEYDOWN:
- lstrcpy(wsztemp, TEXT("Keydown "));
- break;
- case WM_REMOTE_KEYUP:
- lstrcpy(wsztemp, TEXT("Keyup "));
- break;
-
- default:
- return;
- }
-
- if((LOWORD(wParam)<='9') && (LOWORD(wParam)>='0'))
- {
- // interpret the numerical codes from the number pad
- WCHAR wsznum[6];
- wsprintf(wsznum, TEXT("%c "), LOWORD(wParam));
- lstrcat(wsztemp, wsznum);
- }
- else
- {
- // interpret the other keys from the face plate
- BOOL bClearSystemLabel=FALSE;
-
- switch (LOWORD(wParam))
- {
- case VK_ESCAPE:
- lstrcat(wsztemp, TEXT("Escape "));
- break;
-
- case VK_LEFT:
- lstrcat(wsztemp, TEXT("Left" ));
- bClearSystemLabel=TRUE;
- break;
-
- case VK_UP:
- lstrcat(wsztemp, TEXT("Up "));
- bClearSystemLabel=TRUE;
- break;
-
- case VK_RIGHT:
- lstrcat(wsztemp, TEXT("Right "));
- bClearSystemLabel=TRUE;
- break;
-
- case VK_DOWN:
- lstrcat(wsztemp, TEXT("Down "));
- bClearSystemLabel=TRUE;
- break;
-
- case VK_STAR:
- lstrcat(wsztemp, TEXT("* "));
- break;
-
- case VK_POUND:
- lstrcat(wsztemp, TEXT("# "));
- break;
-
- case VK_VOLUMEDOWN:
- lstrcat(wsztemp, TEXT("VolDown "));
- break;
-
- case VK_VOLUMEUP:
- lstrcat(wsztemp, TEXT("VolUp "));
- break;
-
- case VK_RETURN:
- lstrcat(wsztemp, TEXT("Enter "));
- break;
-
- case VK_MENU:
- lstrcat(wsztemp, TEXT("Menu "));
- bClearSystemLabel=TRUE;
- break;
-
- case VK_HELP:
- lstrcat(wsztemp, TEXT("Help "));
- bClearSystemLabel=TRUE;
- break;
-
- default:
- break;
- }
-
- if (bClearSystemLabel)
- UpdateSystemLabel(TEXT("")); // we know we don't get notifications
- // for the system label so empty it for
- // neatness
- }
-
- UpdateKeyLabel(wsztemp);
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CKeybaordApp::UpdateKeyLabel
-
- Description:
- This function changes the text on the first label to whatever
- string is passed.
-
- Parameters:
- LPWSTR - String to display in label.
-
- Returns:
- Nothing.
- -------------------------------------------------------------------*/
- void
- CKeyboardApp::UpdateKeyLabel(LPWSTR lpwstrText)
- {
- if (!m_pLabelCodes || !lpwstrText) // Make sure we have an interface pointer and text
- return;
-
- BSTR bstr=SysAllocString(lpwstrText);
- if (bstr)
- {
- m_pLabelCodes->put_Caption(bstr);
- SysFreeString(bstr);
- } // endif
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CKeybaordApp::UpdateSystemLabel
-
- Description:
- This function changes the text on the second label to whatever
- string is passed.
-
- Parameters:
- LPWSTR - String to display in label.
-
- Returns:
- Nothing.
- -------------------------------------------------------------------*/
- void CKeyboardApp::UpdateSystemLabel(LPWSTR lpwstrText)
- {
- if (!m_pLabelSystem || !lpwstrText)
- return;
-
- BSTR bstr=SysAllocString(lpwstrText);
- if (bstr){
- m_pLabelSystem->put_Caption(bstr);
- SysFreeString(bstr);
- } // endif
- }
-