aboutsummaryrefslogtreecommitdiff
path: root/addons/library.xbmc.addon/libXBMC_addon.h
blob: ac6e7fc757d1a9d3419934e401ca1970ba595c17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#pragma once
/*
 *      Copyright (C) 2005-2010 Team XBMC
 *      http://www.xbmc.org
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with XBMC; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#include <string>
#include <vector>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#ifdef _WIN32                   // windows
#include "dlfcn-win32.h"
#define ADDON_DLL               "\\library.xbmc.addon\\libXBMC_addon" ADDON_HELPER_EXT
#define ADDON_HELPER_EXT        ".dll"
#else
#if defined(__APPLE__)          // osx
#if defined(__POWERPC__)
#define ADDON_HELPER_ARCH       "powerpc-osx"
#elif defined(__arm__)
#define ADDON_HELPER_ARCH       "arm-osx"
#elif defined(__x86_64__)
#define ADDON_HELPER_ARCH       "x86-osx"
#else
#define ADDON_HELPER_ARCH       "x86-osx"
#endif
#else                           // linux
#if defined(__x86_64__)
#define ADDON_HELPER_ARCH       "x86_64-linux"
#elif defined(_POWERPC)
#define ADDON_HELPER_ARCH       "powerpc-linux"
#elif defined(_POWERPC64)
#define ADDON_HELPER_ARCH       "powerpc64-linux"
#elif defined(__ARMEL__)
#define ADDON_HELPER_ARCH       "arm"
#elif defined(_MIPSEL)
#define ADDON_HELPER_ARCH       "mipsel-linux"
#else
#define ADDON_HELPER_ARCH       "i486-linux"
#endif
#endif
#include <dlfcn.h>              // linux+osx
#define ADDON_HELPER_EXT        ".so"
#define ADDON_DLL "/library.xbmc.addon/libXBMC_addon-" ADDON_HELPER_ARCH ADDON_HELPER_EXT
#endif

#ifdef LOG_DEBUG
#undef LOG_DEBUG
#endif
#ifdef LOG_INFO
#undef LOG_INFO
#endif
#ifdef LOG_NOTICE
#undef LOG_NOTICE
#endif
#ifdef LOG_ERROR
#undef LOG_ERROR
#endif

namespace ADDON
{
  typedef enum addon_log
  {
    LOG_DEBUG,
    LOG_INFO,
    LOG_NOTICE,
    LOG_ERROR
  } addon_log_t;

  typedef enum queue_msg
  {
    QUEUE_INFO,
    QUEUE_WARNING,
    QUEUE_ERROR
  } queue_msg_t;

  class CHelper_libXBMC_addon
  {
  public:
    CHelper_libXBMC_addon()
    {
      m_libXBMC_addon = NULL;
      m_Handle        = NULL;
    }

    ~CHelper_libXBMC_addon()
    {
      if (m_libXBMC_addon)
      {
        XBMC_unregister_me();
        dlclose(m_libXBMC_addon);
      }
    }

    bool RegisterMe(void *Handle)
    {
      m_Handle = Handle;

      std::string libBasePath;
      libBasePath  = ((cb_array*)m_Handle)->libPath;
      libBasePath += ADDON_DLL;

      m_libXBMC_addon = dlopen(libBasePath.c_str(), RTLD_LAZY);
      if (m_libXBMC_addon == NULL)
      {
        fprintf(stderr, "Unable to load %s\n", dlerror());
        return false;
      }

      XBMC_register_me   = (int (*)(void *HANDLE))
        dlsym(m_libXBMC_addon, "XBMC_register_me");
      if (XBMC_register_me == NULL)   { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      XBMC_unregister_me = (void (*)())
        dlsym(m_libXBMC_addon, "XBMC_unregister_me");
      if (XBMC_unregister_me == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      Log                = (void (*)(const addon_log_t loglevel, const char *format, ... ))
        dlsym(m_libXBMC_addon, "XBMC_log");
      if (Log == NULL)                { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      GetSetting         = (bool (*)(const char* settingName, void *settingValue))
        dlsym(m_libXBMC_addon, "XBMC_get_setting");
      if (GetSetting == NULL)         { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      QueueNotification  = (void (*)(const queue_msg_t loglevel, const char *format, ... ))
        dlsym(m_libXBMC_addon, "XBMC_queue_notification");
      if (QueueNotification == NULL)  { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      UnknownToUTF8      = (void (*)(std::string &str))
        dlsym(m_libXBMC_addon, "XBMC_unknown_to_utf8");
      if (UnknownToUTF8 == NULL)      { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      GetLocalizedString = (const char* (*)(int dwCode))
        dlsym(m_libXBMC_addon, "XBMC_get_localized_string");
      if (GetLocalizedString == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      GetDVDMenuLanguage = (const char* (*)())
        dlsym(m_libXBMC_addon, "XBMC_get_dvd_menu_language");
      if (GetDVDMenuLanguage == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      OpenFile = (void* (*)(const char* strFileName, unsigned int flags))
        dlsym(m_libXBMC_addon, "XBMC_open_file");
      if (OpenFile == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      OpenFileForWrite = (void* (*)(const char* strFileName, bool bOverWrite))
        dlsym(m_libXBMC_addon, "XBMC_open_file_for_write");
      if (OpenFileForWrite == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      ReadFile = (unsigned int (*)(void* file, void* lpBuf, int64_t uiBufSize))
        dlsym(m_libXBMC_addon, "XBMC_read_file");
      if (ReadFile == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      ReadFileString = (bool (*)(void* file, char *szLine, int iLineLength))
        dlsym(m_libXBMC_addon, "XBMC_read_file_string");
      if (ReadFileString == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      WriteFile = (int (*)(void* file, const void* lpBuf, int64_t uiBufSize))
        dlsym(m_libXBMC_addon, "XBMC_write_file");
      if (WriteFile == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      FlushFile = (void (*)(void* file))
        dlsym(m_libXBMC_addon, "XBMC_flush_file");
      if (FlushFile == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      SeekFile = (int64_t (*)(void* file, int64_t iFilePosition, int iWhence))
        dlsym(m_libXBMC_addon, "XBMC_seek_file");
      if (SeekFile == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      TruncateFile = (int (*)(void* file, int64_t iSize))
        dlsym(m_libXBMC_addon, "XBMC_truncate_file");
      if (TruncateFile == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      GetFilePosition = (int64_t (*)(void* file))
        dlsym(m_libXBMC_addon, "XBMC_get_file_position");
      if (GetFilePosition == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      GetFileLength = (int64_t (*)(void* file))
        dlsym(m_libXBMC_addon, "XBMC_get_file_length");
      if (GetFileLength == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      CloseFile = (void (*)(void* file))
        dlsym(m_libXBMC_addon, "XBMC_close_file");
      if (CloseFile == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      GetFileChunkSize = (int (*)(void* file))
        dlsym(m_libXBMC_addon, "XBMC_get_file_chunk_size");
      if (GetFileChunkSize == NULL) { fprintf(stderr, "Unable to assign function %s\n", dlerror()); return false; }

      return XBMC_register_me(m_Handle) > 0;
    }

    void (*Log)(const addon_log_t loglevel, const char *format, ... );
    bool (*GetSetting)(const char* settingName, void *settingValue);
    void (*QueueNotification)(const queue_msg_t type, const char *format, ... );
    void (*UnknownToUTF8)(std::string &str);
    const char* (*GetLocalizedString)(int dwCode);
    const char* (*GetDVDMenuLanguage)();

    void* (*OpenFile)(const char* strFileName, unsigned int flags);
    void* (*OpenFileForWrite)(const char* strFileName, bool bOverWrite);
    unsigned int (*ReadFile)(void* file, void* lpBuf, int64_t uiBufSize);
    bool (*ReadFileString)(void* file, char *szLine, int iLineLength);
    int (*WriteFile)(void* file, const void* lpBuf, int64_t uiBufSize);
    void (*FlushFile)(void* file);
    int64_t (*SeekFile)(void* file, int64_t iFilePosition, int iWhence);
    int (*TruncateFile)(void* file, int64_t iSize);
    int64_t (*GetFilePosition)(void* file);
    int64_t (*GetFileLength)(void* file);
    void (*CloseFile)(void* file);
    int (*GetFileChunkSize)(void* file);

  protected:
    int (*XBMC_register_me)(void *HANDLE);
    void (*XBMC_unregister_me)();

  private:
    void *m_libXBMC_addon;
    void *m_Handle;
    struct cb_array
    {
      const char* libPath;
    };
  };
};