Merge pull request #3819 from arnova/subtitles_for_stacks
[vuplus_xbmc] / xbmc / peripherals / devices / PeripheralHID.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "PeripheralHID.h"
22 #include "utils/log.h"
23 #include "guilib/LocalizeStrings.h"
24 #include "input/ButtonTranslator.h"
25
26 using namespace PERIPHERALS;
27 using namespace std;
28
29 CPeripheralHID::CPeripheralHID(const PeripheralScanResult& scanResult) :
30   CPeripheral(scanResult)
31 {
32   m_strDeviceName = scanResult.m_strDeviceName.empty() ? g_localizeStrings.Get(35001) : scanResult.m_strDeviceName;
33   m_features.push_back(FEATURE_HID);
34 }
35
36 CPeripheralHID::~CPeripheralHID(void)
37 {
38   if (!m_strKeymap.empty() && !GetSettingBool("do_not_use_custom_keymap"))
39   {
40     CLog::Log(LOGDEBUG, "%s - switching active keymapping to: default", __FUNCTION__);
41     CButtonTranslator::GetInstance().RemoveDevice(m_strKeymap);
42   }
43 }
44
45 bool CPeripheralHID::InitialiseFeature(const PeripheralFeature feature)
46 {
47   if (feature == FEATURE_HID && !m_bInitialised)
48   {
49     m_bInitialised = true;
50
51     if (HasSetting("keymap"))
52       m_strKeymap = GetSettingString("keymap");
53
54     if (m_strKeymap.empty())
55     {
56       m_strKeymap = StringUtils::Format("v%sp%s", VendorIdAsString(), ProductIdAsString());
57       SetSetting("keymap", m_strKeymap);
58     }
59
60     if (!IsSettingVisible("keymap"))
61       SetSettingVisible("do_not_use_custom_keymap", false);
62
63     if (!m_strKeymap.empty())
64     {
65       bool bKeymapEnabled(!GetSettingBool("do_not_use_custom_keymap"));
66       if (bKeymapEnabled)
67       {
68         CLog::Log(LOGDEBUG, "%s - adding keymapping for: %s", __FUNCTION__, m_strKeymap.c_str());
69         CButtonTranslator::GetInstance().AddDevice(m_strKeymap);
70       }
71       else if (!bKeymapEnabled)
72       {
73         CLog::Log(LOGDEBUG, "%s - removing keymapping for: %s", __FUNCTION__, m_strKeymap.c_str());
74         CButtonTranslator::GetInstance().RemoveDevice(m_strKeymap);
75       }
76     }
77
78     CLog::Log(LOGDEBUG, "%s - initialised HID device (%s:%s)", __FUNCTION__, m_strVendorId.c_str(), m_strProductId.c_str());
79   }
80
81   return CPeripheral::InitialiseFeature(feature);
82 }
83
84 void CPeripheralHID::OnSettingChanged(const CStdString &strChangedSetting)
85 {
86   if (m_bInitialised && ((strChangedSetting.Equals("keymap") && !GetSettingBool("do_not_use_custom_keymap")) || strChangedSetting.Equals("keymap_enabled")))
87   {
88     m_bInitialised = false;
89     InitialiseFeature(FEATURE_HID);
90   }
91 }
92