1 /*
2  *  Copyright (C) 2005-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #include "Spin.h"
10 
11 #include "ServiceBroker.h"
12 #include "addons/binary-addons/AddonDll.h"
13 #include "addons/kodi-dev-kit/include/kodi/gui/controls/Spin.h"
14 #include "guilib/GUIComponent.h"
15 #include "guilib/GUISpinControlEx.h"
16 #include "guilib/GUIWindowManager.h"
17 #include "utils/log.h"
18 
19 namespace ADDON
20 {
21 
Init(AddonGlobalInterface * addonInterface)22 void Interface_GUIControlSpin::Init(AddonGlobalInterface* addonInterface)
23 {
24   addonInterface->toKodi->kodi_gui->control_spin = new AddonToKodiFuncTable_kodi_gui_control_spin();
25 
26   addonInterface->toKodi->kodi_gui->control_spin->set_visible = set_visible;
27   addonInterface->toKodi->kodi_gui->control_spin->set_enabled = set_enabled;
28 
29   addonInterface->toKodi->kodi_gui->control_spin->set_text = set_text;
30   addonInterface->toKodi->kodi_gui->control_spin->reset = reset;
31   addonInterface->toKodi->kodi_gui->control_spin->set_type = set_type;
32 
33   addonInterface->toKodi->kodi_gui->control_spin->add_string_label = add_string_label;
34   addonInterface->toKodi->kodi_gui->control_spin->set_string_value = set_string_value;
35   addonInterface->toKodi->kodi_gui->control_spin->get_string_value = get_string_value;
36 
37   addonInterface->toKodi->kodi_gui->control_spin->add_int_label = add_int_label;
38   addonInterface->toKodi->kodi_gui->control_spin->set_int_range = set_int_range;
39   addonInterface->toKodi->kodi_gui->control_spin->set_int_value = set_int_value;
40   addonInterface->toKodi->kodi_gui->control_spin->get_int_value = get_int_value;
41 
42   addonInterface->toKodi->kodi_gui->control_spin->set_float_range = set_float_range;
43   addonInterface->toKodi->kodi_gui->control_spin->set_float_value = set_float_value;
44   addonInterface->toKodi->kodi_gui->control_spin->get_float_value = get_float_value;
45   addonInterface->toKodi->kodi_gui->control_spin->set_float_interval = set_float_interval;
46 }
47 
DeInit(AddonGlobalInterface * addonInterface)48 void Interface_GUIControlSpin::DeInit(AddonGlobalInterface* addonInterface)
49 {
50   delete addonInterface->toKodi->kodi_gui->control_spin;
51 }
52 
set_visible(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle,bool visible)53 void Interface_GUIControlSpin::set_visible(KODI_HANDLE kodiBase,
54                                            KODI_GUI_CONTROL_HANDLE handle,
55                                            bool visible)
56 {
57   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
58   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
59   if (!addon || !control)
60   {
61     CLog::Log(LOGERROR,
62               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}') "
63               "on addon '{}'",
64               __func__, kodiBase, handle, addon ? addon->ID() : "unknown");
65     return;
66   }
67 
68   control->SetVisible(visible);
69 }
70 
set_enabled(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle,bool enabled)71 void Interface_GUIControlSpin::set_enabled(KODI_HANDLE kodiBase,
72                                            KODI_GUI_CONTROL_HANDLE handle,
73                                            bool enabled)
74 {
75   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
76   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
77   if (!addon || !control)
78   {
79     CLog::Log(LOGERROR,
80               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}') "
81               "on addon '{}'",
82               __func__, kodiBase, handle, addon ? addon->ID() : "unknown");
83     return;
84   }
85 
86   control->SetEnabled(enabled);
87 }
88 
set_text(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle,const char * text)89 void Interface_GUIControlSpin::set_text(KODI_HANDLE kodiBase,
90                                         KODI_GUI_CONTROL_HANDLE handle,
91                                         const char* text)
92 {
93   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
94   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
95   if (!addon || !control || !text)
96   {
97     CLog::Log(LOGERROR,
98               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}', "
99               "text='{}') on addon '{}'",
100               __func__, kodiBase, handle, static_cast<const void*>(text),
101               addon ? addon->ID() : "unknown");
102     return;
103   }
104 
105   CGUIMessage msg(GUI_MSG_LABEL_SET, control->GetParentID(), control->GetID());
106   msg.SetLabel(text);
107   CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg, control->GetParentID());
108 }
109 
reset(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle)110 void Interface_GUIControlSpin::reset(KODI_HANDLE kodiBase, KODI_GUI_CONTROL_HANDLE handle)
111 {
112   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
113   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
114   if (!addon || !control)
115   {
116     CLog::Log(LOGERROR,
117               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}') "
118               "on addon '{}'",
119               __func__, kodiBase, handle, addon ? addon->ID() : "unknown");
120     return;
121   }
122 
123   CGUIMessage msg(GUI_MSG_LABEL_RESET, control->GetParentID(), control->GetID());
124   CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg, control->GetParentID());
125 }
126 
set_type(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle,int type)127 void Interface_GUIControlSpin::set_type(KODI_HANDLE kodiBase,
128                                         KODI_GUI_CONTROL_HANDLE handle,
129                                         int type)
130 {
131   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
132   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
133   if (!addon || !control)
134   {
135     CLog::Log(LOGERROR,
136               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}') "
137               "on addon '{}'",
138               __func__, kodiBase, handle, addon ? addon->ID() : "unknown");
139     return;
140   }
141 
142   control->SetType(type);
143 }
144 
add_string_label(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle,const char * label,const char * value)145 void Interface_GUIControlSpin::add_string_label(KODI_HANDLE kodiBase,
146                                                 KODI_GUI_CONTROL_HANDLE handle,
147                                                 const char* label,
148                                                 const char* value)
149 {
150   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
151   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
152   if (!addon || !control || !label || !value)
153   {
154     CLog::Log(LOGERROR,
155               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}', "
156               "label='{}', value='{}') on addon '{}'",
157               __func__, kodiBase, handle, static_cast<const void*>(label),
158               static_cast<const void*>(value), addon ? addon->ID() : "unknown");
159     return;
160   }
161 
162   control->AddLabel(std::string(label), std::string(value));
163 }
164 
set_string_value(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle,const char * value)165 void Interface_GUIControlSpin::set_string_value(KODI_HANDLE kodiBase,
166                                                 KODI_GUI_CONTROL_HANDLE handle,
167                                                 const char* value)
168 {
169   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
170   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
171   if (!addon || !control || !value)
172   {
173     CLog::Log(LOGERROR,
174               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}', "
175               "value='{}') on addon '{}'",
176               __func__, kodiBase, handle, static_cast<const void*>(value),
177               addon ? addon->ID() : "unknown");
178     return;
179   }
180 
181   control->SetStringValue(std::string(value));
182 }
183 
get_string_value(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle)184 char* Interface_GUIControlSpin::get_string_value(KODI_HANDLE kodiBase,
185                                                  KODI_GUI_CONTROL_HANDLE handle)
186 {
187   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
188   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
189   if (!addon || !control)
190   {
191     CLog::Log(LOGERROR,
192               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}') "
193               "on addon '{}'",
194               __func__, kodiBase, handle, addon ? addon->ID() : "unknown");
195     return nullptr;
196   }
197 
198   return strdup(control->GetStringValue().c_str());
199 }
200 
add_int_label(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle,const char * label,int value)201 void Interface_GUIControlSpin::add_int_label(KODI_HANDLE kodiBase,
202                                              KODI_GUI_CONTROL_HANDLE handle,
203                                              const char* label,
204                                              int value)
205 {
206   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
207   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
208   if (!addon || !control || !label)
209   {
210     CLog::Log(LOGERROR,
211               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}', "
212               "label='{}') on addon '{}'",
213               __func__, kodiBase, handle, static_cast<const void*>(label),
214               addon ? addon->ID() : "unknown");
215     return;
216   }
217 
218   control->AddLabel(std::string(label), value);
219 }
220 
set_int_range(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle,int start,int end)221 void Interface_GUIControlSpin::set_int_range(KODI_HANDLE kodiBase,
222                                              KODI_GUI_CONTROL_HANDLE handle,
223                                              int start,
224                                              int end)
225 {
226   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
227   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
228   if (!addon || !control)
229   {
230     CLog::Log(LOGERROR,
231               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}') "
232               "on addon '{}'",
233               __func__, kodiBase, handle, addon ? addon->ID() : "unknown");
234     return;
235   }
236 
237   control->SetRange(start, end);
238 }
239 
set_int_value(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle,int value)240 void Interface_GUIControlSpin::set_int_value(KODI_HANDLE kodiBase,
241                                              KODI_GUI_CONTROL_HANDLE handle,
242                                              int value)
243 {
244   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
245   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
246   if (!addon || !control)
247   {
248     CLog::Log(LOGERROR,
249               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}') "
250               "on addon '{}'",
251               __func__, kodiBase, handle, addon ? addon->ID() : "unknown");
252     return;
253   }
254 
255   control->SetValue(value);
256 }
257 
get_int_value(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle)258 int Interface_GUIControlSpin::get_int_value(KODI_HANDLE kodiBase, KODI_GUI_CONTROL_HANDLE handle)
259 {
260   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
261   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
262   if (!addon || !control)
263   {
264     CLog::Log(LOGERROR,
265               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}') "
266               "on addon '{}'",
267               __func__, kodiBase, handle, addon ? addon->ID() : "unknown");
268     return -1;
269   }
270 
271   return control->GetValue();
272 }
273 
set_float_range(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle,float start,float end)274 void Interface_GUIControlSpin::set_float_range(KODI_HANDLE kodiBase,
275                                                KODI_GUI_CONTROL_HANDLE handle,
276                                                float start,
277                                                float end)
278 {
279   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
280   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
281   if (!addon || !control)
282   {
283     CLog::Log(LOGERROR,
284               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}') "
285               "on addon '{}'",
286               __func__, kodiBase, handle, addon ? addon->ID() : "unknown");
287     return;
288   }
289 
290   control->SetFloatRange(start, end);
291 }
292 
set_float_value(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle,float value)293 void Interface_GUIControlSpin::set_float_value(KODI_HANDLE kodiBase,
294                                                KODI_GUI_CONTROL_HANDLE handle,
295                                                float value)
296 {
297   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
298   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
299   if (!addon || !control)
300   {
301     CLog::Log(LOGERROR,
302               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}') "
303               "on addon '{}'",
304               __func__, kodiBase, handle, addon ? addon->ID() : "unknown");
305     return;
306   }
307 
308   control->SetFloatValue(value);
309 }
310 
get_float_value(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle)311 float Interface_GUIControlSpin::get_float_value(KODI_HANDLE kodiBase,
312                                                 KODI_GUI_CONTROL_HANDLE handle)
313 {
314   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
315   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
316   if (!addon || !control)
317   {
318     CLog::Log(LOGERROR,
319               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}') "
320               "on addon '{}'",
321               __func__, kodiBase, handle, addon ? addon->ID() : "unknown");
322     return 0.0f;
323   }
324 
325   return control->GetFloatValue();
326 }
327 
set_float_interval(KODI_HANDLE kodiBase,KODI_GUI_CONTROL_HANDLE handle,float interval)328 void Interface_GUIControlSpin::set_float_interval(KODI_HANDLE kodiBase,
329                                                   KODI_GUI_CONTROL_HANDLE handle,
330                                                   float interval)
331 {
332   CAddonDll* addon = static_cast<CAddonDll*>(kodiBase);
333   CGUISpinControlEx* control = static_cast<CGUISpinControlEx*>(handle);
334   if (!addon || !control)
335   {
336     CLog::Log(LOGERROR,
337               "Interface_GUIControlSpin::{} - invalid handler data (kodiBase='{}', handle='{}') "
338               "on addon '{}'",
339               __func__, kodiBase, handle, addon ? addon->ID() : "unknown");
340     return;
341   }
342 
343   control->SetFloatInterval(interval);
344 }
345 
346 } /* namespace ADDON */
347