void CLCDOutput::HandleButtonState(DWORD dwButtonState, DWORD dwButton)
{
    if ( (m_dwButtonState & dwButton) && !(dwButtonState & dwButton) )
    {
        LCDUITRACE(_T("Button 0x%x released\n"), dwButton);
        OnLCDButtonUp(dwButton);
    }
    if ( !(m_dwButtonState & dwButton) && (dwButtonState & dwButton) )
    {
        LCDUITRACE(_T("Button 0x%x pressed\n"), dwButton);
        OnLCDButtonDown(dwButton);
    }
}
//************************************************************************
// Update all Screens & draw
//************************************************************************
bool CLCDOutputManager::Update()
{
	ASSERT(m_bInitialized);

	// Update the active screen
	if(m_pActiveScreen != NULL)
	{
		m_pActiveScreen->Update();
		// Check if the active screen has expired
		if(m_pActiveScreen->HasExpired())
		{
			// Call event handlers
			DeactivateScreen();
			return true;
		}
	}

	if(!m_pLcdConnection)
		return true;
	// Update
	m_pLcdConnection->Update();
	
	// skip button checking and drawing if there is no connection to a device
	if(m_pLcdConnection->GetConnectionState() != CONNECTED)
		return true;
	

	// Handle buttons
	bool bState = false;
	int iId = 0;
	for(int i = 0; i < m_pLcdConnection->GetButtonCount(); i ++)
	{
		// get current state
		bState = m_pLcdConnection->GetButtonState(i);
		// handle input event
		if(bState != m_pbButtonStates[i])
		{
			iId = m_pLcdConnection->GetButtonId(i);
			if(bState) {
				OnLCDButtonDown(iId);
			} else {
				OnLCDButtonUp(iId);
			}
			m_pdwButtonRepeatStarts[i] = GetTickCount();
			m_pdwButtonRepeatTimers[i] = m_pdwButtonRepeatStarts[i] + m_dwButtonRepeatDelay;
		}
		// check if repeat event should be sent
		else if(bState && m_pdwButtonRepeatTimers[i] <= GetTickCount())
		{
			iId = m_pLcdConnection->GetButtonId(i);
		
			// reduce the delay by 5% per second
			DWORD dwNewDelay = m_dwButtonRepeatDelay - ((float)m_dwButtonRepeatDelay * 0.05 * (GetTickCount() - m_pdwButtonRepeatStarts[i]) / 250);
			// delay may not be less than 25% of the original value
			if(dwNewDelay < m_dwButtonRepeatDelay * 0.25)
				dwNewDelay = m_dwButtonRepeatDelay * 0.25;

			m_pdwButtonRepeatTimers[i] = GetTickCount() + dwNewDelay;

			OnLCDButtonRepeated(iId);
		}
		// save the current state
		m_pbButtonStates[i] = bState;
	}
	
	// Draw
	
	if(m_pActiveScreen != NULL && m_pGfx->IsInitialized())
	{
		m_pGfx->BeginDraw();
		m_pGfx->ClearScreen();
		m_pActiveScreen->Draw(m_pGfx);
		m_pGfx->EndDraw();

		m_pLcdConnection->SetAlert(m_pActiveScreen->GetAlert());
		m_pLcdConnection->Draw();
	}
	else
		m_pLcdConnection->HideApplet();

	m_dwLastUpdate = GetTickCount();
	return true;
}