]> git.vomp.tv Git - vompclient.git/blob - input.cc
Windows fixes
[vompclient.git] / 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 "command.h" // FIXME - get rid after predefined message targets
28 #include "inputman.h"
29 #include "input.h"
30
31 void Input::ResetToDefault()
32 {
33   translist.clear();
34   InitHWCListwithDefaults();
35 }
36
37 UCHAR Input::TranslateHWC(HWC_TYPE code)
38 {
39   UCHAR ret = TranslateHWCFixed(code);
40
41   if (ret != NA_UNKNOWN) // Found in fixed list
42   {
43     InputMan::getInstance()->cancelLearnMode(); // Just in case
44     return ret;
45   }
46
47   return TranslateHWCList(code);
48 }
49
50 UCHAR Input::TranslateHWCList(HWC_TYPE code)
51 {
52   if (learnMode != NOLEARNMODE)
53   {
54     setHWCtoCommand(code, learnMode);
55     InputMan::getInstance()->cancelLearnMode();
56     return NA_LEARN;
57   }
58   RemoteTranslationList::iterator it = translist.find(code);
59   if (it == translist.end())
60   {
61     return NA_UNKNOWN;
62   }
63   else
64   {
65     return it->second;
66   }
67 }
68
69 void Input::setHWCtoCommand(HWC_TYPE hcw, UCHAR command)
70 {
71   translist[hcw] = command;
72 }
73
74 void Input::unsetHWC(HWC_TYPE hcw) // FIXME never used
75 {
76   translist.erase(hcw);
77 }
78
79 void Input::LoadKeysConfig(VDR *vdr,const char *cfg)
80 {
81         ULONG number=0;
82         if (sscanf(cfg,"%ld",&number) != 1) return;
83         Log::getInstance()->log("Input", Log::INFO, "Config Input/Remote keys num keys %d",number);
84         char keybuf[1024];
85         for (ULONG i = 0; i < number; i++) {
86                 sprintf(keybuf, "RemoteKey%lu", i);
87                 const char *keytrans = vdr->configLoad(modName(), keybuf);
88                 if (keytrans) {
89                         ULONG ul1, ul2;
90                         ULONG uc;
91                         if (sscanf(keytrans, "%lXI%lXK%lX", &ul1, &ul2, &uc) == 3) {
92                                 translist[((ULLONG) ul1) | ((ULLONG) ul2) << 32] = (UCHAR) uc;
93                         }
94                         delete[] keytrans;
95                 }
96
97         }
98 }
99
100 // FIXME - These two still use the bit shifting stuff. Leave it for now, might still
101 // need it for Windows
102
103 void Input::SaveKeysConfig()
104 {
105   int number=0;
106   char buffer[1024];
107   char keybuf[1024];
108   RemoteTranslationList::const_iterator it;
109   for (it = translist.begin(); it != translist.end(); it++)
110   {
111           sprintf(buffer,"%08lXI%08lXK%02X",
112                           (ULONG)it->first ,(ULONG) (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     Log::getInstance()->log("Input", Log::INFO, "Config Input/Remote keys load");
130     LoadKeysConfig(vdr,config);
131     delete[] config;
132   }
133   else
134   {
135     Log::getInstance()->log("Input", Log::INFO, "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->to = Command::getInstance();
152   m->from = this;
153   m->parameter = key;
154   MessageQueue::getInstance()->postMessage(m);
155 }
156
157 std::string Input::getAllHardwareKeyNamesAssignedToVompKey(UCHAR 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 }