]> 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 "inputman.h"
26 #include "input.h"
27
28 Input::Input()
29 {
30   learnmode = NOLEARNMODE;
31 }
32
33 Input::~Input()
34 {
35 }
36
37 void Input::EnterLearningMode(UCHAR command)
38 {
39     learnmode = command; //Armed
40 }
41
42 void Input::ResetToDefault()
43 {
44   translist.clear();
45   InitHWCListwithDefaults();
46 }
47
48 /*
49 UCHAR Input::TranslateHWCFixed(ULLONG code)
50 {
51   switch (code)
52   {
53     case DOWN:
54       return DOWN;
55     case UP:
56       return UP;
57     case LEFT:
58       return LEFT;
59     case RIGHT:
60       return RIGHT;
61     case MENU:
62       return MENU;
63     case BACK:
64       return BACK;
65     case OK:
66       return OK;
67     default:
68       return NA_UNKNOWN;
69   }
70 }
71 */
72
73 const char* Input::HardcodedTranslateStr(UCHAR command)
74 {
75   switch (command)
76   {
77     case DOWN:
78       return tr("Down");
79     case UP:
80       return tr("Up");
81     case LEFT:
82       return tr("Left");
83     case RIGHT:
84       return tr("Right");
85     case MENU:
86       return tr("Menu");
87     case BACK:
88       return tr("Back");
89     case OK:
90       return tr("Ok");
91     default:
92       return NULL;
93   }
94 }
95
96 UCHAR Input::TranslateHWCList(int code)
97 {
98   if (learnmode != NOLEARNMODE)
99   {
100     setHWCtoCommand(code, learnmode);
101     learnmode = NOLEARNMODE;
102     return NA_LEARN;
103   }
104   RemoteTranslationList::iterator it = translist.find(code);
105   if (it == translist.end())
106   {
107     return NA_UNKNOWN;
108   }
109   else
110   {
111     return it->second;
112   }
113 }
114
115 UCHAR Input::TranslateHWC(int code)
116 {
117   UCHAR ret = TranslateHWCFixed(code);
118   if (ret == NA_UNKNOWN)
119   {
120     ret = TranslateHWCList(code);
121   }
122   else
123   {
124     learnmode = NOLEARNMODE;
125   }
126
127   if (ret == NA_UNKNOWN)
128   {
129     return NA_UNKNOWN;
130   }
131   return ret;
132 }
133
134 void Input::setHWCtoCommand(ULLONG hcw, UCHAR command)
135 {
136   translist[hcw] = command;
137 }
138
139 void Input::unsetHWC(ULLONG hcw)
140 {
141   translist.erase(hcw);
142 }
143
144 void Input::LoadKeysConfig(VDR *vdr,const char *cfg)
145 {
146         ULONG number=0;
147         if (sscanf(cfg,"%ld",&number) != 1) return;
148         Log::getInstance()->log("Input", Log::INFO, "Config General/Remote keys num keys %d",number);
149         char keybuf[1024];
150         for (ULONG i = 0; i < number; i++) {
151                 sprintf(keybuf, "RemoteKey%lu", i);
152                 const char *keytrans = vdr->configLoad("General", keybuf);
153                 if (keytrans) {
154                         ULONG ul1, ul2;
155                         ULONG uc;
156                         if (sscanf(keytrans, "%lXI%lXK%lX", &ul1, &ul2, &uc) == 3) {
157                                 translist[((ULLONG) ul1) | ((ULLONG) ul2) << 32] = (UCHAR) uc;
158                         }
159                         delete[] keytrans;
160                 }
161
162         }
163 }
164
165 void Input::SaveKeysConfig()
166 {
167   int number=0;
168   char buffer[1024];
169   char keybuf[1024];
170   RemoteTranslationList::const_iterator it;
171   for (it = translist.begin(); it != translist.end(); it++)
172   {
173           sprintf(buffer,"%08lXI%08lXK%02X",
174                           (ULONG)it->first ,(ULONG) (it->first >> 32), it->second);
175           sprintf(keybuf,"RemoteKey%d",number);
176           VDR::getInstance()->configSave("General",keybuf,buffer);
177           number++;
178   }
179   sprintf(buffer,"%d",number);
180   VDR::getInstance()->configSave("General","RemoteKeyNum",buffer);
181 }
182
183 char* Input::HCWDesc(ULLONG hcw)
184 {
185   char *dest;
186   const char* temp = InputMan::CommandDesc((UCHAR)hcw);
187   if (temp != NULL)
188   {
189     dest=new char[strlen(temp)+1];
190     strcpy(dest,temp);
191   }
192   else
193   {
194     dest=new char[20];
195     sprintf(dest,"C:%lX",(ULONG)hcw);
196   }
197   return dest;
198 }
199
200 char* Input::CommandTranslateStr(UCHAR command)
201 {
202   char* desc;
203   int length = 5; //:+\t+0
204
205   const char* commanddesc = InputMan::CommandDesc(command);
206   if (commanddesc != NULL) length += strlen(commanddesc);
207
208   const char* preassigneddesc = HardcodedTranslateStr(command);
209   if (preassigneddesc != NULL) length += strlen(preassigneddesc);
210
211   char* keydesc[10];
212   int keys = 0; // max 10
213   RemoteTranslationList::const_iterator it;
214   for (it = translist.begin(); it != translist.end(); it++)
215   {
216     if (it->second == command)
217     {
218       keydesc[keys] = HCWDesc(it->first);
219       length += strlen(keydesc[keys])+2;
220       keys++;
221       if (keys == 10) break;
222     }
223   }
224
225   desc = new char[length];
226   char* current = desc;
227   if (commanddesc != NULL)
228   {
229     current += sprintf(current, "%s:\t ", commanddesc);
230   }
231   else
232   {
233     current += sprintf(current,":\t ");
234   }
235
236   if (preassigneddesc != NULL)
237   {
238     current += sprintf(current, "%s\t", preassigneddesc);
239   }
240   else
241   {
242     current+=sprintf(current,"\t");
243   }
244
245   for (int i = 0; i < keys; i++)
246   {
247     current += sprintf(current, "%s, ", keydesc[i]);
248     delete[] keydesc[i];
249   }
250
251   return desc;
252 }
253
254 // bool Input::addOptionPagesToWTB(WTabBar *wtb)
255 // {
256 //     WRemoteConfig* wrc = new WRemoteConfig();
257 //     wtb->addTab(tr("Remote Control"), wrc);
258 //     return true;
259 // }
260
261 bool Input::loadOptionsfromServer(VDR* vdr)
262 {
263    // Set remote keys
264   char * config;
265   config = vdr->configLoad("General", "RemoteKeyNum");
266
267   if (config)
268   {
269     Log::getInstance()->log("Input", Log::INFO, "Config General/Remote keys load");
270     LoadKeysConfig(vdr,config);
271     delete[] config;
272   }
273   else
274   {
275     Log::getInstance()->log("Input", Log::INFO, "Config General/Remote keys not found");
276     InitHWCListwithDefaults();
277   }
278   return true;
279 }
280
281 bool Input::saveOptionstoServer()
282 {
283     SaveKeysConfig();
284     return true;
285 }