]> git.vomp.tv Git - vompclient.git/blob - src/input.cc
Fix hardware key type to u8 in input system
[vompclient.git] / src / input.cc
1 /*
2     Copyright 2004-2020 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "wremoteconfig.h"
21 #include "i18n.h"
22 #include "log.h"
23 #include "vdr.h"
24 #include "wtabbar.h"
25 #include "message.h"
26 #include "messagequeue.h"
27 #include "inputman.h"
28 #include "input.h"
29
30 static const char* TAG = "Input";
31
32 void Input::ResetToDefault()
33 {
34   translist.clear();
35   InitHWCListwithDefaults();
36 }
37
38 u1 Input::TranslateHWC(u8 code)
39 {
40   u1 ret = TranslateHWCFixed(code);
41
42   if (ret != NA_UNKNOWN) // Found in fixed list
43   {
44     InputMan::getInstance()->cancelLearnMode(); // Just in case
45     return ret;
46   }
47
48   return TranslateHWCList(code);
49 }
50
51 u1 Input::TranslateHWCList(u8 code)
52 {
53   if (learnMode != NOLEARNMODE)
54   {
55     setHWCtoCommand(code, static_cast<u1>(learnMode));
56     InputMan::getInstance()->cancelLearnMode();
57     return NA_LEARN;
58   }
59   RemoteTranslationList::iterator it = translist.find(code);
60   if (it == translist.end())
61   {
62     return NA_UNKNOWN;
63   }
64   else
65   {
66     return it->second;
67   }
68 }
69
70 void Input::setHWCtoCommand(u8 hcw, u1 command)
71 {
72   translist[hcw] = command;
73 }
74
75 void Input::unsetHWC(u8 hcw) // FIXME never used
76 {
77   translist.erase(hcw);
78 }
79
80 void Input::LoadKeysConfig(VDR *vdr,const char *cfg)
81 {
82         u4 number=0;
83         if (sscanf(cfg,"%d",&number) != 1) return;
84         LogNT::getInstance()->info(TAG, "Config Input/Remote keys num keys {}", number);
85         char keybuf[1024];
86         for (u4 i = 0; i < number; i++) {
87                 sprintf(keybuf, "RemoteKey%u", i);
88                 const char *keytrans = vdr->configLoad(modName(), keybuf);
89                 if (keytrans) {
90                         u4 ul1, ul2;
91                         u4 uc;
92                         if (sscanf(keytrans, "%XI%XK%X", &ul1, &ul2, &uc) == 3) {
93                                 translist[((u8) ul1) | ((u8) ul2) << 32] = (u1) uc;
94                         }
95                         delete[] keytrans;
96                 }
97
98         }
99 }
100
101 // FIXME - These two still use the bit shifting stuff. Leave it for now, might still
102 // need it for Windows
103
104 void Input::SaveKeysConfig()
105 {
106   int number=0;
107   char buffer[1024];
108   char keybuf[1024];
109   RemoteTranslationList::const_iterator it;
110   for (it = translist.begin(); it != translist.end(); it++)
111   {
112           sprintf(buffer,"%08XI%08XK%02X", (u4)it->first, (u4)(it->first >> 32), it->second);
113           sprintf(keybuf,"RemoteKey%d",number);
114           VDR::getInstance()->configSave(modName(), keybuf, buffer);
115           number++;
116   }
117   sprintf(buffer,"%d",number);
118   VDR::getInstance()->configSave(modName(), "RemoteKeyNum", buffer);
119 }
120
121 bool Input::loadOptionsFromServer(VDR* vdr)
122 {
123    // Set remote keys
124   char * config;
125   config = vdr->configLoad(modName(), "RemoteKeyNum");
126
127   if (config)
128   {
129     LogNT::getInstance()->info(TAG, "Config Input/Remote keys load");
130     LoadKeysConfig(vdr,config);
131     delete[] config;
132   }
133   else
134   {
135     LogNT::getInstance()->info(TAG, "Config Input/Remote keys not found");
136     InitHWCListwithDefaults();
137   }
138   return true;
139 }
140
141 bool Input::saveOptionstoServer()
142 {
143   SaveKeysConfig();
144   return true;
145 }
146
147 void Input::sendInputKey(int key)
148 {
149   Message* m = new Message();
150   m->message = Message::INPUT_EVENT;
151   m->p_to = Message::CONTROL;
152   m->from = NULL;
153   m->parameter = key;
154   MessageQueue::getInstance()->postMessage(m);
155 }
156
157 std::string Input::getAllHardwareKeyNamesAssignedToVompKey(u1 vompKey)
158 {
159   std::string keyNames;
160   keyNames.reserve(50);
161   int keys = 0; // max 10
162   RemoteTranslationList::const_iterator it;
163   bool first = true;
164   for (it = translist.begin(); it != translist.end(); it++)
165   {
166     if (it->second == vompKey)
167     {
168       if (!first) keyNames += ", ";
169       first = false;
170
171       keyNames += getHardwareKeyName(it->first);
172       keys++;
173       if (keys == 10) break;
174     }
175   }
176   return keyNames;
177 }