]> git.vomp.tv Git - vompclient.git/blob - input.cc
WIP [broken]
[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 Input::Input()
32 {
33   learnmode = NOLEARNMODE;
34 }
35
36 Input::~Input()
37 {
38 }
39
40 void Input::EnterLearningMode(UCHAR command)
41 {
42   learnmode = command; //Armed
43 }
44
45 void Input::ResetToDefault()
46 {
47   translist.clear();
48   InitHWCListwithDefaults();
49 }
50
51 UCHAR Input::TranslateHWCList(int code)
52 {
53   if (learnmode != NOLEARNMODE)
54   {
55     setHWCtoCommand(code, learnmode);
56     learnmode = NOLEARNMODE;
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 UCHAR Input::TranslateHWC(int code)
71 {
72   UCHAR ret = TranslateHWCFixed(code);
73   if (ret == NA_UNKNOWN)
74   {
75     ret = TranslateHWCList(code);
76   }
77   else
78   {
79     learnmode = NOLEARNMODE;
80   }
81
82   if (ret == NA_UNKNOWN)
83   {
84     return NA_UNKNOWN;
85   }
86   return ret;
87 }
88
89 void Input::setHWCtoCommand(ULLONG hcw, UCHAR command)
90 {
91   translist[hcw] = command;
92 }
93
94 void Input::unsetHWC(ULLONG hcw)
95 {
96   translist.erase(hcw);
97 }
98
99 void Input::LoadKeysConfig(VDR *vdr,const char *cfg)
100 {
101         ULONG number=0;
102         if (sscanf(cfg,"%ld",&number) != 1) return;
103         Log::getInstance()->log("Input", Log::INFO, "Config General/Remote keys num keys %d",number);
104         char keybuf[1024];
105         for (ULONG i = 0; i < number; i++) {
106                 sprintf(keybuf, "RemoteKey%lu", i);
107                 const char *keytrans = vdr->configLoad("General", keybuf);
108                 if (keytrans) {
109                         ULONG ul1, ul2;
110                         ULONG uc;
111                         if (sscanf(keytrans, "%lXI%lXK%lX", &ul1, &ul2, &uc) == 3) {
112                                 translist[((ULLONG) ul1) | ((ULLONG) ul2) << 32] = (UCHAR) uc;
113                         }
114                         delete[] keytrans;
115                 }
116
117         }
118 }
119
120 void Input::SaveKeysConfig()
121 {
122   int number=0;
123   char buffer[1024];
124   char keybuf[1024];
125   RemoteTranslationList::const_iterator it;
126   for (it = translist.begin(); it != translist.end(); it++)
127   {
128           sprintf(buffer,"%08lXI%08lXK%02X",
129                           (ULONG)it->first ,(ULONG) (it->first >> 32), it->second);
130           sprintf(keybuf,"RemoteKey%d",number);
131           VDR::getInstance()->configSave("General",keybuf,buffer);
132           number++;
133   }
134   sprintf(buffer,"%d",number);
135   VDR::getInstance()->configSave("General","RemoteKeyNum",buffer);
136 }
137
138
139 // bool Input::addOptionPagesToWTB(WTabBar *wtb)
140 // {
141 //     WRemoteConfig* wrc = new WRemoteConfig();
142 //     wtb->addTab(tr("Remote Control"), wrc);
143 //     return true;
144 // }
145
146 bool Input::loadOptionsfromServer(VDR* vdr)
147 {
148    // Set remote keys
149   char * config;
150   config = vdr->configLoad("General", "RemoteKeyNum");
151
152   if (config)
153   {
154     Log::getInstance()->log("Input", Log::INFO, "Config General/Remote keys load");
155     LoadKeysConfig(vdr,config);
156     delete[] config;
157   }
158   else
159   {
160     Log::getInstance()->log("Input", Log::INFO, "Config General/Remote keys not found");
161     InitHWCListwithDefaults();
162   }
163   return true;
164 }
165
166 bool Input::saveOptionstoServer()
167 {
168   SaveKeysConfig();
169   return true;
170 }
171
172 void Input::sendInputKey(int key)
173 {
174   Message* m = new Message();
175   m->message = Message::INPUT_EVENT;
176   m->to = Command::getInstance();
177   m->from = this;
178   m->parameter = TranslateHWC(key);
179   MessageQueue::getInstance()->postMessage(m);
180 }
181
182 std::string Input::getAllHardwareKeyNamesAssignedToVompKey(UCHAR vompKey)
183 {
184   std::string keyNames;
185   keyNames.reserve(50);
186   int keys = 0; // max 10
187   RemoteTranslationList::const_iterator it;
188   bool first = true;
189   for (it = translist.begin(); it != translist.end(); it++)
190   {
191     if (it->second == vompKey)
192     {
193       if (!first) keyNames += ", ";
194       first = false;
195
196       keyNames += getHardwareKeyName(it->first);
197       keys++;
198       if (keys == 10) break;
199     }
200   }
201   return keyNames;
202 }