]> git.vomp.tv Git - vompclient.git/blob - input.cc
Message queue fix for VVideoRec
[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 "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 UCHAR Input::TranslateHWC(HWC_TYPE code)
39 {
40   UCHAR 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 UCHAR Input::TranslateHWCList(HWC_TYPE code)
52 {
53   if (learnMode != NOLEARNMODE)
54   {
55     setHWCtoCommand(code, static_cast<UCHAR>(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(HWC_TYPE hcw, UCHAR command)
71 {
72   translist[hcw] = command;
73 }
74
75 void Input::unsetHWC(HWC_TYPE hcw) // FIXME never used
76 {
77   translist.erase(hcw);
78 }
79
80 void Input::LoadKeysConfig(VDR *vdr,const char *cfg)
81 {
82         ULONG number=0;
83         if (sscanf(cfg,"%ld",&number) != 1) return;
84         LogNT::getInstance()->info(TAG, "Config Input/Remote keys num keys {}", number);
85         char keybuf[1024];
86         for (ULONG i = 0; i < number; i++) {
87                 sprintf(keybuf, "RemoteKey%lu", i);
88                 const char *keytrans = vdr->configLoad(modName(), keybuf);
89                 if (keytrans) {
90                         ULONG ul1, ul2;
91                         ULONG uc;
92                         if (sscanf(keytrans, "%lXI%lXK%lX", &ul1, &ul2, &uc) == 3) {
93                                 translist[((ULLONG) ul1) | ((ULLONG) ul2) << 32] = (UCHAR) 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,"%08lXI%08lXK%02X",
113                           (ULONG)it->first ,(ULONG) (it->first >> 32), it->second);
114           sprintf(keybuf,"RemoteKey%d",number);
115           VDR::getInstance()->configSave(modName(), keybuf, buffer);
116           number++;
117   }
118   sprintf(buffer,"%d",number);
119   VDR::getInstance()->configSave(modName(), "RemoteKeyNum", buffer);
120 }
121
122 bool Input::loadOptionsFromServer(VDR* vdr)
123 {
124    // Set remote keys
125   char * config;
126   config = vdr->configLoad(modName(), "RemoteKeyNum");
127
128   if (config)
129   {
130     LogNT::getInstance()->info(TAG, "Config Input/Remote keys load");
131     LoadKeysConfig(vdr,config);
132     delete[] config;
133   }
134   else
135   {
136     LogNT::getInstance()->info(TAG, "Config Input/Remote keys not found");
137     InitHWCListwithDefaults();
138   }
139   return true;
140 }
141
142 bool Input::saveOptionstoServer()
143 {
144   SaveKeysConfig();
145   return true;
146 }
147
148 void Input::sendInputKey(int key)
149 {
150   Message* m = new Message();
151   m->message = Message::INPUT_EVENT;
152   m->p_to = Message::CONTROL;
153   m->from = NULL;
154   m->parameter = key;
155   MessageQueue::getInstance()->postMessage(m);
156 }
157
158 std::string Input::getAllHardwareKeyNamesAssignedToVompKey(UCHAR vompKey)
159 {
160   std::string keyNames;
161   keyNames.reserve(50);
162   int keys = 0; // max 10
163   RemoteTranslationList::const_iterator it;
164   bool first = true;
165   for (it = translist.begin(); it != translist.end(); it++)
166   {
167     if (it->second == vompKey)
168     {
169       if (!first) keyNames += ", ";
170       first = false;
171
172       keyNames += getHardwareKeyName(it->first);
173       keys++;
174       if (keys == 10) break;
175     }
176   }
177   return keyNames;
178 }