2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "Visualization.h"
11 #include "filesystem/SpecialProtocol.h"
12 #include "guilib/GUIWindowManager.h"
13 #include "utils/log.h"
15 using namespace ADDON
;
16 using namespace KODI::ADDONS
;
21 void get_properties(const KODI_HANDLE hdl
, struct KODI_ADDON_VISUALIZATION_PROPS
* props
)
24 static_cast<CVisualization
*>(hdl
)->GetProperties(props
);
27 void transfer_preset(const KODI_HANDLE hdl
, const char* preset
)
30 static_cast<CVisualization
*>(hdl
)->TransferPreset(preset
);
33 void clear_presets(const KODI_HANDLE hdl
)
36 static_cast<CVisualization
*>(hdl
)->ClearPresets();
41 CVisualization::CVisualization(const AddonInfoPtr
& addonInfo
, float x
, float y
, float w
, float h
)
42 : IAddonInstanceHandler(ADDON_INSTANCE_VISUALIZATION
, addonInfo
),
43 m_x(static_cast<int>(x
)),
44 m_y(static_cast<int>(y
)),
45 m_width(static_cast<int>(w
)),
46 m_height(static_cast<int>(h
))
48 // Setup new Visualization instance
49 m_ifc
.visualization
= new AddonInstance_Visualization
;
50 m_ifc
.visualization
->toAddon
= new KodiToAddonFuncTable_Visualization();
51 m_ifc
.visualization
->toKodi
= new AddonToKodiFuncTable_Visualization();
52 m_ifc
.visualization
->toKodi
->get_properties
= get_properties
;
53 m_ifc
.visualization
->toKodi
->transfer_preset
= transfer_preset
;
54 m_ifc
.visualization
->toKodi
->clear_presets
= clear_presets
;
56 /* Open the class "kodi::addon::CInstanceVisualization" on add-on side */
57 if (CreateInstance() != ADDON_STATUS_OK
)
59 CLog::Log(LOGFATAL
, "Visualization: failed to create instance for '{}' and not usable!", ID());
63 /* presets becomes send with "transfer_preset" during call of function below */
64 if (m_ifc
.visualization
->toAddon
->get_presets
)
65 m_ifc
.visualization
->toAddon
->get_presets(m_ifc
.hdl
);
68 CVisualization::~CVisualization()
70 /* Destroy the class "kodi::addon::CInstanceVisualization" on add-on side */
73 delete m_ifc
.visualization
->toAddon
;
74 delete m_ifc
.visualization
->toKodi
;
75 delete m_ifc
.visualization
;
78 bool CVisualization::Start(int channels
,
81 const std::string
& songName
)
83 if (m_ifc
.visualization
->toAddon
->start
)
84 return m_ifc
.visualization
->toAddon
->start(m_ifc
.hdl
, channels
, samplesPerSec
, bitsPerSample
,
89 void CVisualization::Stop()
91 if (m_ifc
.visualization
->toAddon
->stop
)
92 m_ifc
.visualization
->toAddon
->stop(m_ifc
.hdl
);
95 void CVisualization::AudioData(const float* audioData
, int audioDataLength
)
97 if (m_ifc
.visualization
->toAddon
->audio_data
)
98 m_ifc
.visualization
->toAddon
->audio_data(m_ifc
.hdl
, audioData
, audioDataLength
);
101 bool CVisualization::IsDirty()
103 if (m_ifc
.visualization
->toAddon
->is_dirty
)
104 return m_ifc
.visualization
->toAddon
->is_dirty(m_ifc
.hdl
);
108 void CVisualization::Render()
110 if (m_ifc
.visualization
->toAddon
->render
)
111 m_ifc
.visualization
->toAddon
->render(m_ifc
.hdl
);
114 int CVisualization::GetSyncDelay()
116 if (m_ifc
.visualization
->toAddon
->get_sync_delay
)
117 m_ifc
.visualization
->toAddon
->get_sync_delay(m_ifc
.hdl
);
121 bool CVisualization::NextPreset()
123 if (m_ifc
.visualization
->toAddon
->next_preset
)
124 return m_ifc
.visualization
->toAddon
->next_preset(m_ifc
.hdl
);
128 bool CVisualization::PrevPreset()
130 if (m_ifc
.visualization
->toAddon
->prev_preset
)
131 return m_ifc
.visualization
->toAddon
->prev_preset(m_ifc
.hdl
);
135 bool CVisualization::LoadPreset(int select
)
137 if (m_ifc
.visualization
->toAddon
->load_preset
)
138 return m_ifc
.visualization
->toAddon
->load_preset(m_ifc
.hdl
, select
);
142 bool CVisualization::RandomPreset()
144 if (m_ifc
.visualization
->toAddon
->random_preset
)
145 return m_ifc
.visualization
->toAddon
->random_preset(m_ifc
.hdl
);
149 bool CVisualization::LockPreset()
151 if (m_ifc
.visualization
->toAddon
->lock_preset
)
152 return m_ifc
.visualization
->toAddon
->lock_preset(m_ifc
.hdl
);
156 bool CVisualization::RatePreset(bool plus_minus
)
158 if (m_ifc
.visualization
->toAddon
->rate_preset
)
159 return m_ifc
.visualization
->toAddon
->rate_preset(m_ifc
.hdl
, plus_minus
);
163 bool CVisualization::UpdateAlbumart(const char* albumart
)
165 if (m_ifc
.visualization
->toAddon
->update_albumart
)
166 return m_ifc
.visualization
->toAddon
->update_albumart(m_ifc
.hdl
, albumart
);
170 bool CVisualization::UpdateTrack(const KODI_ADDON_VISUALIZATION_TRACK
* track
)
172 if (m_ifc
.visualization
->toAddon
->update_track
)
173 return m_ifc
.visualization
->toAddon
->update_track(m_ifc
.hdl
, track
);
177 bool CVisualization::HasPresets()
179 return !m_presets
.empty();
182 bool CVisualization::GetPresetList(std::vector
<std::string
>& vecpresets
)
184 vecpresets
= m_presets
;
185 return !m_presets
.empty();
188 int CVisualization::GetActivePreset()
190 if (m_ifc
.visualization
->toAddon
->get_active_preset
)
191 return m_ifc
.visualization
->toAddon
->get_active_preset(m_ifc
.hdl
);
195 std::string
CVisualization::GetActivePresetName()
197 if (!m_presets
.empty())
198 return m_presets
[GetActivePreset()];
202 bool CVisualization::IsLocked()
204 if (m_ifc
.visualization
->toAddon
->is_locked
)
205 return m_ifc
.visualization
->toAddon
->is_locked(m_ifc
.hdl
);
209 void CVisualization::TransferPreset(const std::string
& preset
)
211 m_presets
.emplace_back(preset
);
214 void CVisualization::ClearPresets()
219 void CVisualization::GetProperties(struct KODI_ADDON_VISUALIZATION_PROPS
* props
)
224 const auto winSystem
= CServiceBroker::GetWinSystem();
230 props
->width
= m_width
;
231 props
->height
= m_height
;
232 props
->device
= winSystem
->GetHWContext();
233 props
->pixelRatio
= winSystem
->GetGfxContext().GetResInfo().fPixelRatio
;