3 * Copyright (C) 2005-2013 Team XBMC
6 * This Program 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, or (at your option)
11 * This Program 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.
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
30 #ifdef _WIN32 // windows
31 #include "dlfcn-win32.h"
32 #define ADDON_DLL "\\library.xbmc.addon\\libXBMC_addon" ADDON_HELPER_EXT
33 #define ADDON_HELPER_EXT ".dll"
35 #if defined(__APPLE__) // osx
36 #if defined(__POWERPC__)
37 #define ADDON_HELPER_ARCH "powerpc-osx"
38 #elif defined(__arm__)
39 #define ADDON_HELPER_ARCH "arm-osx"
40 #elif defined(__x86_64__)
41 #define ADDON_HELPER_ARCH "x86-osx"
43 #define ADDON_HELPER_ARCH "x86-osx"
46 #if defined(__x86_64__)
47 #define ADDON_HELPER_ARCH "x86_64-linux"
48 #elif defined(_POWERPC)
49 #define ADDON_HELPER_ARCH "powerpc-linux"
50 #elif defined(_POWERPC64)
51 #define ADDON_HELPER_ARCH "powerpc64-linux"
52 #elif defined(__ARMEL__)
53 #define ADDON_HELPER_ARCH "arm"
54 #elif defined(_MIPSEL)
55 #define ADDON_HELPER_ARCH "mipsel-linux"
57 #define ADDON_HELPER_ARCH "i486-linux"
60 #include <dlfcn.h> // linux+osx
61 #define ADDON_HELPER_EXT ".so"
62 #define ADDON_DLL_NAME "libXBMC_addon-" ADDON_HELPER_ARCH ADDON_HELPER_EXT
63 #define ADDON_DLL "/library.xbmc.addon/" ADDON_DLL_NAME
84 typedef enum addon_log
92 typedef enum queue_msg
99 class CHelper_libXBMC_addon
102 CHelper_libXBMC_addon()
104 m_libXBMC_addon = NULL;
108 ~CHelper_libXBMC_addon()
112 XBMC_unregister_me(m_Handle, m_Callbacks);
113 dlclose(m_libXBMC_addon);
117 bool RegisterMe(void *Handle)
121 std::string libBasePath;
122 libBasePath = ((cb_array*)m_Handle)->libPath;
123 libBasePath += ADDON_DLL;
127 if(stat(libBasePath.c_str(),&st) != 0)
129 std::string tempbin = getenv("XBMC_ANDROID_LIBS");
130 libBasePath = tempbin + "/" + ADDON_DLL_NAME;
134 m_libXBMC_addon = dlopen(libBasePath.c_str(), RTLD_LAZY);
135 if (m_libXBMC_addon == NULL)
137 fprintf(stderr, "Unable to load %s\n", dlerror());
141 XBMC_register_me = (void* (*)(void *HANDLE))
142 dlsym(m_libXBMC_addon, "XBMC_register_me");
143 if (XBMC_register_me == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
145 XBMC_unregister_me = (void (*)(void* HANDLE, void* CB))
146 dlsym(m_libXBMC_addon, "XBMC_unregister_me");
147 if (XBMC_unregister_me == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
149 XBMC_log = (void (*)(void* HANDLE, void* CB, const addon_log_t loglevel, const char *msg))
150 dlsym(m_libXBMC_addon, "XBMC_log");
151 if (XBMC_log == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
153 XBMC_get_setting = (bool (*)(void* HANDLE, void* CB, const char* settingName, void *settingValue))
154 dlsym(m_libXBMC_addon, "XBMC_get_setting");
155 if (XBMC_get_setting == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
157 XBMC_queue_notification = (void (*)(void* HANDLE, void* CB, const queue_msg_t loglevel, const char *msg))
158 dlsym(m_libXBMC_addon, "XBMC_queue_notification");
159 if (XBMC_queue_notification == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
161 XBMC_unknown_to_utf8 = (char* (*)(void* HANDLE, void* CB, const char* str))
162 dlsym(m_libXBMC_addon, "XBMC_unknown_to_utf8");
163 if (XBMC_unknown_to_utf8 == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
165 XBMC_get_localized_string = (char* (*)(void* HANDLE, void* CB, int dwCode))
166 dlsym(m_libXBMC_addon, "XBMC_get_localized_string");
167 if (XBMC_get_localized_string == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
169 XBMC_free_string = (void (*)(void* HANDLE, void* CB, char* str))
170 dlsym(m_libXBMC_addon, "XBMC_free_string");
171 if (XBMC_free_string == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
173 XBMC_get_dvd_menu_language = (char* (*)(void* HANDLE, void* CB))
174 dlsym(m_libXBMC_addon, "XBMC_get_dvd_menu_language");
175 if (XBMC_get_dvd_menu_language == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
177 XBMC_open_file = (void* (*)(void* HANDLE, void* CB, const char* strFileName, unsigned int flags))
178 dlsym(m_libXBMC_addon, "XBMC_open_file");
179 if (XBMC_open_file == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
181 XBMC_open_file_for_write = (void* (*)(void* HANDLE, void* CB, const char* strFileName, bool bOverWrite))
182 dlsym(m_libXBMC_addon, "XBMC_open_file_for_write");
183 if (XBMC_open_file_for_write == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
185 XBMC_read_file = (unsigned int (*)(void* HANDLE, void* CB, void* file, void* lpBuf, int64_t uiBufSize))
186 dlsym(m_libXBMC_addon, "XBMC_read_file");
187 if (XBMC_read_file == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
189 XBMC_read_file_string = (bool (*)(void* HANDLE, void* CB, void* file, char *szLine, int iLineLength))
190 dlsym(m_libXBMC_addon, "XBMC_read_file_string");
191 if (XBMC_read_file_string == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
193 XBMC_write_file = (int (*)(void* HANDLE, void* CB, void* file, const void* lpBuf, int64_t uiBufSize))
194 dlsym(m_libXBMC_addon, "XBMC_write_file");
195 if (XBMC_write_file == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
197 XBMC_flush_file = (void (*)(void* HANDLE, void* CB, void* file))
198 dlsym(m_libXBMC_addon, "XBMC_flush_file");
199 if (XBMC_flush_file == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
201 XBMC_seek_file = (int64_t (*)(void* HANDLE, void* CB, void* file, int64_t iFilePosition, int iWhence))
202 dlsym(m_libXBMC_addon, "XBMC_seek_file");
203 if (XBMC_seek_file == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
205 XBMC_truncate_file = (int (*)(void* HANDLE, void* CB, void* file, int64_t iSize))
206 dlsym(m_libXBMC_addon, "XBMC_truncate_file");
207 if (XBMC_truncate_file == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
209 XBMC_get_file_position = (int64_t (*)(void* HANDLE, void* CB, void* file))
210 dlsym(m_libXBMC_addon, "XBMC_get_file_position");
211 if (XBMC_get_file_position == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
213 XBMC_get_file_length = (int64_t (*)(void* HANDLE, void* CB, void* file))
214 dlsym(m_libXBMC_addon, "XBMC_get_file_length");
215 if (XBMC_get_file_length == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
217 XBMC_close_file = (void (*)(void* HANDLE, void* CB, void* file))
218 dlsym(m_libXBMC_addon, "XBMC_close_file");
219 if (XBMC_close_file == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
221 XBMC_get_file_chunk_size = (int (*)(void* HANDLE, void* CB, void* file))
222 dlsym(m_libXBMC_addon, "XBMC_get_file_chunk_size");
223 if (XBMC_get_file_chunk_size == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
225 XBMC_file_exists = (bool (*)(void* HANDLE, void* CB, const char *strFileName, bool bUseCache))
226 dlsym(m_libXBMC_addon, "XBMC_file_exists");
227 if (XBMC_file_exists == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
229 XBMC_stat_file = (int (*)(void* HANDLE, void* CB, const char *strFileName, struct __stat64* buffer))
230 dlsym(m_libXBMC_addon, "XBMC_stat_file");
231 if (XBMC_stat_file == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
233 XBMC_delete_file = (bool (*)(void* HANDLE, void* CB, const char *strFileName))
234 dlsym(m_libXBMC_addon, "XBMC_delete_file");
235 if (XBMC_delete_file == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
237 XBMC_can_open_directory = (bool (*)(void* HANDLE, void* CB, const char* strURL))
238 dlsym(m_libXBMC_addon, "XBMC_can_open_directory");
239 if (XBMC_can_open_directory == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
241 XBMC_create_directory = (bool (*)(void* HANDLE, void* CB, const char* strPath))
242 dlsym(m_libXBMC_addon, "XBMC_create_directory");
243 if (XBMC_create_directory == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
245 XBMC_directory_exists = (bool (*)(void* HANDLE, void* CB, const char* strPath))
246 dlsym(m_libXBMC_addon, "XBMC_directory_exists");
247 if (XBMC_directory_exists == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
249 XBMC_remove_directory = (bool (*)(void* HANDLE, void* CB, const char* strPath))
250 dlsym(m_libXBMC_addon, "XBMC_remove_directory");
251 if (XBMC_remove_directory == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }
253 m_Callbacks = XBMC_register_me(m_Handle);
254 return m_Callbacks != NULL;
258 * @brief Add a message to XBMC's log.
259 * @param loglevel The log level of the message.
260 * @param format The format of the message to pass to XBMC.
262 void Log(const addon_log_t loglevel, const char *format, ... )
266 va_start (args, format);
267 vsprintf (buffer, format, args);
269 return XBMC_log(m_Handle, m_Callbacks, loglevel, buffer);
273 * @brief Get a settings value for this add-on.
274 * @param settingName The name of the setting to get.
275 * @param settingValue The value.
276 * @return True if the settings was fetched successfully, false otherwise.
278 bool GetSetting(const char* settingName, void *settingValue)
280 return XBMC_get_setting(m_Handle, m_Callbacks, settingName, settingValue);
284 * @brief Queue a notification in the GUI.
285 * @param type The message type.
286 * @param format The format of the message to pass to display in XBMC.
288 void QueueNotification(const queue_msg_t type, const char *format, ... )
292 va_start (args, format);
293 vsprintf (buffer, format, args);
295 return XBMC_queue_notification(m_Handle, m_Callbacks, type, buffer);
299 * @brief Translate a string with an unknown encoding to UTF8.
300 * @param str The string to translate.
301 * @return The string translated to UTF8. Must be freed by calling FreeString() when done.
303 char* UnknownToUTF8(const char* str)
305 return XBMC_unknown_to_utf8(m_Handle, m_Callbacks, str);
309 * @brief Get a localised message.
310 * @param dwCode The code of the message to get.
311 * @return The message. Must be freed by calling FreeString() when done.
313 char* GetLocalizedString(int dwCode)
315 return XBMC_get_localized_string(m_Handle, m_Callbacks, dwCode);
320 * @brief Get the DVD menu language.
321 * @return The language. Must be freed by calling FreeString() when done.
323 char* GetDVDMenuLanguage()
325 return XBMC_get_dvd_menu_language(m_Handle, m_Callbacks);
329 * @brief Free the memory used by str
330 * @param str The string to free
332 void FreeString(char* str)
334 return XBMC_free_string(m_Handle, m_Callbacks, str);
338 * @brief Open the file with filename via XBMC's CFile. Needs to be closed by calling CloseFile() when done.
339 * @param strFileName The filename to open.
340 * @param flags The flags to pass. Documented in XBMC's File.h
341 * @return A handle for the file, or NULL if it couldn't be opened.
343 void* OpenFile(const char* strFileName, unsigned int flags)
345 return XBMC_open_file(m_Handle, m_Callbacks, strFileName, flags);
349 * @brief Open the file with filename via XBMC's CFile in write mode. Needs to be closed by calling CloseFile() when done.
350 * @param strFileName The filename to open.
351 * @param bOverWrite True to overwrite, false otherwise.
352 * @return A handle for the file, or NULL if it couldn't be opened.
354 void* OpenFileForWrite(const char* strFileName, bool bOverWrite)
356 return XBMC_open_file_for_write(m_Handle, m_Callbacks, strFileName, bOverWrite);
360 * @brief Read from an open file.
361 * @param file The file handle to read from.
362 * @param lpBuf The buffer to store the data in.
363 * @param uiBufSize The size of the buffer.
364 * @return Number of bytes read.
366 unsigned int ReadFile(void* file, void* lpBuf, int64_t uiBufSize)
368 return XBMC_read_file(m_Handle, m_Callbacks, file, lpBuf, uiBufSize);
372 * @brief Read a string from an open file.
373 * @param file The file handle to read from.
374 * @param szLine The buffer to store the data in.
375 * @param iLineLength The size of the buffer.
376 * @return True when a line was read, false otherwise.
378 bool ReadFileString(void* file, char *szLine, int iLineLength)
380 return XBMC_read_file_string(m_Handle, m_Callbacks, file, szLine, iLineLength);
384 * @brief Write to a file opened in write mode.
385 * @param file The file handle to write to.
386 * @param lpBuf The data to write.
387 * @param uiBufSize Size of the data to write.
388 * @return The number of bytes read.
390 int WriteFile(void* file, const void* lpBuf, int64_t uiBufSize)
392 return XBMC_write_file(m_Handle, m_Callbacks, file, lpBuf, uiBufSize);
396 * @brief Flush buffered data.
397 * @param file The file handle to flush the data for.
399 void FlushFile(void* file)
401 return XBMC_flush_file(m_Handle, m_Callbacks, file);
405 * @brief Seek in an open file.
406 * @param file The file handle to see in.
407 * @param iFilePosition The new position.
408 * @param iWhence Seek argument. See stdio.h for possible values.
409 * @return The new position.
411 int64_t SeekFile(void* file, int64_t iFilePosition, int iWhence)
413 return XBMC_seek_file(m_Handle, m_Callbacks, file, iFilePosition, iWhence);
417 * @brief Truncate a file to the requested size.
418 * @param file The file handle to truncate.
419 * @param iSize The new max size.
422 int TruncateFile(void* file, int64_t iSize)
424 return XBMC_truncate_file(m_Handle, m_Callbacks, file, iSize);
428 * @brief The current position in an open file.
429 * @param file The file handle to get the position for.
430 * @return The requested position.
432 int64_t GetFilePosition(void* file)
434 return XBMC_get_file_position(m_Handle, m_Callbacks, file);
438 * @brief Get the file size of an open file.
439 * @param file The file to get the size for.
440 * @return The requested size.
442 int64_t GetFileLength(void* file)
444 return XBMC_get_file_length(m_Handle, m_Callbacks, file);
448 * @brief Close an open file.
449 * @param file The file handle to close.
451 void CloseFile(void* file)
453 return XBMC_close_file(m_Handle, m_Callbacks, file);
457 * @brief Get the chunk size for an open file.
458 * @param file the file handle to get the size for.
459 * @return The requested size.
461 int GetFileChunkSize(void* file)
463 return XBMC_get_file_chunk_size(m_Handle, m_Callbacks, file);
467 * @brief Check if a file exists.
468 * @param strFileName The filename to check.
469 * @param bUseCache Check in file cache.
470 * @return true if the file exists false otherwise.
472 bool FileExists(const char *strFileName, bool bUseCache)
474 return XBMC_file_exists(m_Handle, m_Callbacks, strFileName, bUseCache);
478 * @brief Reads file status.
479 * @param strFileName The filename to read the status from.
480 * @param buffer The file status is written into this buffer.
481 * @return The file status was successfully read.
483 int StatFile(const char *strFileName, struct __stat64* buffer)
485 return XBMC_stat_file(m_Handle, m_Callbacks, strFileName, buffer);
489 * @brief Deletes a file.
490 * @param strFileName The filename to delete.
491 * @return The file was successfully deleted.
493 bool DeleteFile(const char *strFileName)
495 return XBMC_delete_file(m_Handle, m_Callbacks, strFileName);
499 * @brief Checks whether a directory can be opened.
500 * @param strUrl The URL of the directory to check.
501 * @return True when it can be opened, false otherwise.
503 bool CanOpenDirectory(const char* strUrl)
505 return XBMC_can_open_directory(m_Handle, m_Callbacks, strUrl);
509 * @brief Creates a directory.
510 * @param strPath Path to the directory.
511 * @return True when it was created, false otherwise.
513 bool CreateDirectory(const char *strPath)
515 return XBMC_create_directory(m_Handle, m_Callbacks, strPath);
519 * @brief Checks if a directory exists.
520 * @param strPath Path to the directory.
521 * @return True when it exists, false otherwise.
523 bool DirectoryExists(const char *strPath)
525 return XBMC_directory_exists(m_Handle, m_Callbacks, strPath);
529 * @brief Removes a directory.
530 * @param strPath Path to the directory.
531 * @return True when it was removed, false otherwise.
533 bool RemoveDirectory(const char *strPath)
535 return XBMC_remove_directory(m_Handle, m_Callbacks, strPath);
539 void* (*XBMC_register_me)(void *HANDLE);
540 void (*XBMC_unregister_me)(void *HANDLE, void* CB);
541 void (*XBMC_log)(void *HANDLE, void* CB, const addon_log_t loglevel, const char *msg);
542 bool (*XBMC_get_setting)(void *HANDLE, void* CB, const char* settingName, void *settingValue);
543 void (*XBMC_queue_notification)(void *HANDLE, void* CB, const queue_msg_t type, const char *msg);
544 char* (*XBMC_unknown_to_utf8)(void *HANDLE, void* CB, const char* str);
545 char* (*XBMC_get_localized_string)(void *HANDLE, void* CB, int dwCode);
546 char* (*XBMC_get_dvd_menu_language)(void *HANDLE, void* CB);
547 void (*XBMC_free_string)(void *HANDLE, void* CB, char* str);
548 void* (*XBMC_open_file)(void *HANDLE, void* CB, const char* strFileName, unsigned int flags);
549 void* (*XBMC_open_file_for_write)(void *HANDLE, void* CB, const char* strFileName, bool bOverWrite);
550 unsigned int (*XBMC_read_file)(void *HANDLE, void* CB, void* file, void* lpBuf, int64_t uiBufSize);
551 bool (*XBMC_read_file_string)(void *HANDLE, void* CB, void* file, char *szLine, int iLineLength);
552 int (*XBMC_write_file)(void *HANDLE, void* CB, void* file, const void* lpBuf, int64_t uiBufSize);
553 void (*XBMC_flush_file)(void *HANDLE, void* CB, void* file);
554 int64_t (*XBMC_seek_file)(void *HANDLE, void* CB, void* file, int64_t iFilePosition, int iWhence);
555 int (*XBMC_truncate_file)(void *HANDLE, void* CB, void* file, int64_t iSize);
556 int64_t (*XBMC_get_file_position)(void *HANDLE, void* CB, void* file);
557 int64_t (*XBMC_get_file_length)(void *HANDLE, void* CB, void* file);
558 void (*XBMC_close_file)(void *HANDLE, void* CB, void* file);
559 int (*XBMC_get_file_chunk_size)(void *HANDLE, void* CB, void* file);
560 bool (*XBMC_file_exists)(void *HANDLE, void* CB, const char *strFileName, bool bUseCache);
561 int (*XBMC_stat_file)(void *HANDLE, void* CB, const char *strFileName, struct __stat64* buffer);
562 bool (*XBMC_delete_file)(void *HANDLE, void* CB, const char *strFileName);
563 bool (*XBMC_can_open_directory)(void *HANDLE, void* CB, const char* strURL);
564 bool (*XBMC_create_directory)(void *HANDLE, void* CB, const char* strPath);
565 bool (*XBMC_directory_exists)(void *HANDLE, void* CB, const char* strPath);
566 bool (*XBMC_remove_directory)(void *HANDLE, void* CB, const char* strPath);
569 void *m_libXBMC_addon;