| 1 | /* |
| 2 | * This file is part of the libCEC(R) library. |
| 3 | * |
| 4 | * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. |
| 5 | * libCEC(R) is an original work, containing original code. |
| 6 | * |
| 7 | * libCEC(R) is a trademark of Pulse-Eight Limited. |
| 8 | * |
| 9 | * This program is dual-licensed; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | * |
| 23 | * |
| 24 | * Alternatively, you can license this library under a commercial license, |
| 25 | * please contact Pulse-Eight Licensing for more information. |
| 26 | * |
| 27 | * For more information contact: |
| 28 | * Pulse-Eight Licensing <license@pulse-eight.com> |
| 29 | * http://www.pulse-eight.com/ |
| 30 | * http://www.pulse-eight.net/ |
| 31 | */ |
| 32 | |
| 33 | #include "env.h" |
| 34 | #include "PHCommandHandler.h" |
| 35 | |
| 36 | #include "lib/devices/CECBusDevice.h" |
| 37 | #include "lib/CECProcessor.h" |
| 38 | #include "lib/LibCEC.h" |
| 39 | #include "lib/CECClient.h" |
| 40 | |
| 41 | using namespace CEC; |
| 42 | using namespace PLATFORM; |
| 43 | |
| 44 | #define LIB_CEC m_busDevice->GetProcessor()->GetLib() |
| 45 | #define ToString(p) LIB_CEC->ToString(p) |
| 46 | |
| 47 | #define TV_ON_CHECK_TIME_MS 5000 |
| 48 | |
| 49 | CImageViewOnCheck::~CImageViewOnCheck(void) |
| 50 | { |
| 51 | StopThread(-1); |
| 52 | m_event.Broadcast(); |
| 53 | StopThread(); |
| 54 | } |
| 55 | |
| 56 | void* CImageViewOnCheck::Process(void) |
| 57 | { |
| 58 | CCECBusDevice* tv = m_handler->m_processor->GetDevice(CECDEVICE_TV); |
| 59 | cec_power_status status(CEC_POWER_STATUS_UNKNOWN); |
| 60 | while (status != CEC_POWER_STATUS_ON) |
| 61 | { |
| 62 | m_event.Wait(TV_ON_CHECK_TIME_MS); |
| 63 | if (!IsRunning()) |
| 64 | return NULL; |
| 65 | |
| 66 | status = tv->GetPowerStatus(m_handler->m_busDevice->GetLogicalAddress()); |
| 67 | |
| 68 | if (status != CEC_POWER_STATUS_ON && |
| 69 | status != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON) |
| 70 | { |
| 71 | CLockObject lock(m_handler->m_mutex); |
| 72 | tv->OnImageViewOnSent(false); |
| 73 | m_handler->m_iActiveSourcePending = GetTimeMs(); |
| 74 | } |
| 75 | } |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | CPHCommandHandler::CPHCommandHandler(CCECBusDevice *busDevice, |
| 80 | int32_t iTransmitTimeout /* = CEC_DEFAULT_TRANSMIT_TIMEOUT */, |
| 81 | int32_t iTransmitWait /* = CEC_DEFAULT_TRANSMIT_WAIT */, |
| 82 | int8_t iTransmitRetries /* = CEC_DEFAULT_TRANSMIT_RETRIES */, |
| 83 | int64_t iActiveSourcePending /* = 0 */) : |
| 84 | CCECCommandHandler(busDevice, iTransmitTimeout, iTransmitWait, iTransmitRetries, iActiveSourcePending), |
| 85 | m_iLastKeyCode(CEC_USER_CONTROL_CODE_UNKNOWN) |
| 86 | { |
| 87 | m_imageViewOnCheck = new CImageViewOnCheck(this); |
| 88 | m_vendorId = CEC_VENDOR_PHILIPS; |
| 89 | } |
| 90 | |
| 91 | CPHCommandHandler::~CPHCommandHandler(void) |
| 92 | { |
| 93 | delete m_imageViewOnCheck; |
| 94 | } |
| 95 | |
| 96 | bool CPHCommandHandler::InitHandler(void) |
| 97 | { |
| 98 | CCECBusDevice *primary = m_processor->GetPrimaryDevice(); |
| 99 | if (primary && primary->GetLogicalAddress() != CECDEVICE_UNREGISTERED) |
| 100 | { |
| 101 | //XXX hack to use this handler for the primary device |
| 102 | if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV && |
| 103 | primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress()) |
| 104 | { |
| 105 | primary->SetVendorId(CEC_VENDOR_PHILIPS); |
| 106 | primary->ReplaceHandler(false); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return CCECCommandHandler::InitHandler(); |
| 111 | } |
| 112 | |
| 113 | bool CPHCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly /* = false */) |
| 114 | { |
| 115 | |
| 116 | CCECBusDevice* tv = m_processor->GetDevice(CECDEVICE_TV); |
| 117 | if (m_busDevice->IsActiveSource() && |
| 118 | m_busDevice->IsHandledByLibCEC() && |
| 119 | tv && tv->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON && |
| 120 | !bTransmitDelayedCommandsOnly) |
| 121 | { |
| 122 | // tv sometimes ignores image view on. check the power status of the tv in 5 seconds, and retry when it failed to power up |
| 123 | if (m_imageViewOnCheck && !m_imageViewOnCheck->IsRunning()) |
| 124 | m_imageViewOnCheck->CreateThread(false); |
| 125 | } |
| 126 | |
| 127 | return CCECCommandHandler::ActivateSource(bTransmitDelayedCommandsOnly); |
| 128 | } |
| 129 | |
| 130 | int CPHCommandHandler::HandleUserControlPressed(const cec_command& command) |
| 131 | { |
| 132 | // tv keeps sending these until a button is pressed |
| 133 | if (command.parameters[0] == CEC_USER_CONTROL_CODE_DISPLAY_INFORMATION && |
| 134 | m_iLastKeyCode == CEC_USER_CONTROL_CODE_DISPLAY_INFORMATION) |
| 135 | return COMMAND_HANDLED; |
| 136 | |
| 137 | m_iLastKeyCode = command.parameters[0]; |
| 138 | |
| 139 | return CCECCommandHandler::HandleUserControlPressed(command); |
| 140 | } |
| 141 | |
| 142 | int CPHCommandHandler::HandleUserControlRelease(const cec_command& command) |
| 143 | { |
| 144 | m_iLastKeyCode = CEC_USER_CONTROL_CODE_UNKNOWN; |
| 145 | |
| 146 | return CCECCommandHandler::HandleUserControlRelease(command); |
| 147 | } |
| 148 | |
| 149 | int CPHCommandHandler::HandleDeviceVendorId(const cec_command& command) |
| 150 | { |
| 151 | m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON); |
| 152 | return CCECCommandHandler::HandleDeviceVendorId(command); |
| 153 | } |
| 154 | |
| 155 | int CPHCommandHandler::HandleGiveDeviceVendorId(const cec_command& command) |
| 156 | { |
| 157 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): vendor id feature abort", ToString(command.destination), command.destination, ToString(command.initiator), command.initiator); |
| 158 | m_processor->TransmitAbort(command.destination, command.initiator, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); |
| 159 | return COMMAND_HANDLED; |
| 160 | } |