]> git.sesse.net Git - vlc/blob - bindings/java-gcj/vlc-glue.cc
Avoid allocating/freeing livlc_exception_t.
[vlc] / bindings / java-gcj / vlc-glue.cc
1 /* These are a must*/
2 #include <gcj/cni.h>
3 #include <vlc/vlc.h>
4
5 #include <stdio.h> // for printf
6 #include <stdlib.h> // for calloc
7
8 /* JVLC internal imports, generated by gcjh */
9 #include "JVLC.h"
10 #include "JVLCVariable.h"
11 #include "JVLCIntVariable.h"
12 #include "JVLCBoolVariable.h"
13 #include "JVLCFloatVariable.h"
14 #include "JVLCStringVariable.h"
15 #include "JVLCTimeVariable.h"
16 #include "JVLCVarVariable.h"
17 #include "JVLCVarValue.h"
18
19 /* Java classes used throughout here */
20 #include <java/lang/String.h>
21 #include <java/lang/Class.h>
22 #include <java/lang/System.h>
23 #include <java/io/PrintStream.h>
24
25 void setString(jstring orig, char* dest);
26
27 jint JVLC::create () {
28   return VLC_Create();
29 }
30
31 jint JVLC::init(JArray< ::java::lang::String *> *args) {
32
33   int argc = 0;
34   char* arguments[argc];
35
36   if (args != NULL) // It's a very bad day otherwise
37   {
38     argc = args->length;
39     arguments[argc];
40
41     /*
42      * convert the JArray<String*>*  in char**
43      * so in a way suitable for VLC_Init
44      */
45     jstring* argsElements = elements(args);
46
47     for (int i = 0; i < argc; i++) {
48       arguments[i] = (char*) malloc(JvGetStringUTFLength(argsElements[i]) + 1);
49       setString(argsElements[i], arguments[i]);
50     }
51   }
52
53   return VLC_Init(this->id, argc, arguments);
54 }
55
56
57 jint JVLC::addInterface(java::lang::String* moduleName, jboolean blocking, jboolean startPlay) {
58
59   char* psz_module = NULL;
60   if (moduleName != NULL) {
61     psz_module = (char *) malloc(JvGetStringUTFLength(moduleName));
62     setString(moduleName, psz_module);
63   }
64
65   int i_blocking = 0;
66   int i_startPlay = 0;
67   if (blocking) i_blocking = 1;
68   if (startPlay) i_startPlay = 1;
69
70   int addIntf_res = VLC_AddIntf(this->id, (char* const) psz_module, i_blocking, i_startPlay);
71   
72   if (psz_module != NULL)
73     free(psz_module);
74   
75   return addIntf_res;
76
77 }
78
79
80 jstring JVLC::getVersion() {
81   return JvNewStringUTF(VLC_Version());
82 }
83
84
85 jstring JVLC::getError(jint errorCode) {
86   return JvNewStringUTF(VLC_Error(errorCode));
87 }
88
89
90 jint JVLC::die() {
91   return VLC_Die(this->id);
92 }
93
94
95 jint JVLC::cleanUp() {
96   return VLC_CleanUp(this->id);
97 }
98
99
100 jint JVLC::setVariable(::JVLCVariable *jvlcVariable) {
101
102   /* these are the two parameters given the the
103    * VLC_VariableSet() function
104    */
105   vlc_value_t value;
106   char* psz_var = NULL;
107
108   if (jvlcVariable != NULL) {
109     jclass variableClass = jvlcVariable->getClass();
110
111     /* We use the class name for kinda of instanceof */
112     jstring className = variableClass->getName();
113
114     /**
115      * VLC_SetVariable takes a union as its second argument.
116      * The union members are mapped 1:1 to java types which
117      * extend JVLCVariable. So here we check the runtime type
118      * of the actual variable and act consequently. Here is the
119      * mapping:
120      *
121      * typedef union
122      *{
123      * int             i_int;      JVLCIntVariable
124      * vlc_bool_t      b_bool;     JVLCBoolVariable
125      * float           f_float;    JVLCFloatVariable
126      * char *          psz_string; JVLCStringVariable
127      * void *          p_address;  -- NOT IMPLEMENTED --
128      * vlc_object_t *  p_object;   -- NOT IMPLEMENTED --
129      * vlc_list_t *    p_list;     JVLCListVariable XXX:TODO
130      * signed long long i_time;    JVLCTimeVariable
131      * struct { char *psz_name; int i_object_id; } var; JVLCVarVariable <- this name sucks
132      * // Make sure the structure is at least 64bits
133      * struct { char a, b, c, d, e, f, g, h; } padding; <- Do we really need this?
134      *
135      * } vlc_value_t;
136      */
137
138     /* i_int */
139     if (className->equals(JvNewStringUTF("VLCIntVariable" ))) {
140       value.i_int = ((::JVLCIntVariable *)jvlcVariable)->getIntValue();
141     }
142     /* b_bool */
143     else if (className->equals(JvNewStringUTF("VLCBoolVariable"))) {
144       value.b_bool = ((::JVLCBoolVariable *)jvlcVariable)->getBoolValue();
145     } 
146     /* f_float */
147     else if (className->equals(JvNewStringUTF("VLCFloatVariable"))) {
148       value.f_float = ((::JVLCFloatVariable *)jvlcVariable)->getFloatValue();
149     }
150     /* psz_string */
151     else if (className->equals(JvNewStringUTF("VLCStringVariable"))) {
152       value.psz_string = (char* const) elements((((::JVLCStringVariable *)jvlcVariable)->getStringValue())->toCharArray());
153     }
154     /* i_time */
155     else if (className->equals(JvNewStringUTF("VLCTimeVariable"))) {
156       value.i_time = ((::JVLCTimeVariable *)jvlcVariable)->getTimeValue();
157     }
158
159     /* var */
160     else if (className->equals(JvNewStringUTF("VLCVarVariable"))) {
161       jstring varValueName = ((::JVLCVarVariable *)jvlcVariable)->getVarValue()->getName();
162       value.var.psz_name = (char *) malloc(JvGetStringUTFLength(varValueName));
163       setString(varValueName, value.var.psz_name);
164       value.var.i_object_id = (((::JVLCVarVariable *)jvlcVariable)->getVarValue())->getOID();
165     }
166     psz_var = (char *) malloc(JvGetStringUTFLength(jvlcVariable->getName()));
167     setString(jvlcVariable->getName(), psz_var);
168   }
169   
170   return VLC_VariableSet(this->id, (char* const) psz_var, value);
171
172 }
173
174 jint JVLC::addTarget(::java::lang::String *URI, JArray< ::java::lang::String *> *options, jint insertMode, jint position) {
175
176   char*  psz_target   = NULL;
177   char** ppsz_options = NULL;
178   int options_number = 0;
179   
180   if (URI != NULL) {
181     psz_target = (char *) malloc( JvGetStringUTFLength(URI));
182     setString(URI, psz_target);
183   }
184
185   if (options != NULL) {
186     options_number = options->length;
187     ppsz_options[options_number];
188     jstring* jstr_options = elements(options);
189
190     for (jint i = 0; i < options_number; i++) {
191       ppsz_options[i] = (char *) malloc(JvGetStringUTFLength(jstr_options[i]));
192       setString(jstr_options[i], ppsz_options[i]);
193     }
194   }
195   
196   return VLC_AddTarget(this->id, (char const *)psz_target, (const char **) ppsz_options, options_number, insertMode, position);
197 }
198
199 jint JVLC::play() {
200   return VLC_Play(this->id);
201 }
202
203 jint JVLC::pause() {
204   return VLC_Pause(this->id);
205 }
206
207 jint JVLC::stop() {
208   return VLC_Stop(this->id);
209 }
210
211 jboolean JVLC::isPlaying() {
212   return VLC_IsPlaying(this->id);
213 }
214
215 jfloat JVLC::getPosition() {
216   return VLC_PositionGet(this->id);
217 }
218
219 jfloat JVLC::setPosition(jfloat position) {
220   return VLC_PositionSet(this->id, position);
221 }
222
223 jint JVLC::getTime() {
224   return VLC_TimeGet(this->id);
225 }
226
227 jint JVLC::setTime(jint seconds, jboolean relative) {
228   return VLC_TimeSet(this->id, seconds, relative);
229 }
230
231 jint JVLC::getLength() {
232   return VLC_LengthGet(this->id);
233 }
234
235 jfloat JVLC::speedFaster() {
236   return VLC_SpeedFaster(this->id);
237 }
238
239 jfloat JVLC::speedSlower() {
240   return VLC_SpeedSlower(this->id);
241 }
242
243 jint JVLC::getPlaylistIndex() {
244   return VLC_PlaylistIndex(this->id);
245 }
246
247 jint JVLC::getPlaylistItems() {
248   return VLC_PlaylistNumberOfItems(this->id);
249 }
250
251 jint JVLC::playlistNext() {
252   return VLC_PlaylistNext(this->id);
253 }
254
255 jint JVLC::playlistPrev() {
256   return VLC_PlaylistPrev(this->id);
257 }
258
259 jint JVLC::playlistClear() {
260   return VLC_PlaylistClear(this->id);
261 }
262
263 jint JVLC::setVolume(jint volume) {
264   return VLC_VolumeSet(this->id, volume);
265 }
266
267 jint JVLC::getVolume() {
268   return VLC_VolumeGet(this->id);
269 }
270
271 jint JVLC::muteVolume() {
272   return VLC_VolumeMute(this->id);
273 }
274
275 jint JVLC::fullScreen() {
276   return VLC_FullScreen(this->id);
277 }
278
279
280 /* XXX: in progress */
281 ::JVLCVariable* JVLC::getVariable(::java::lang::String* varName) {
282
283   char* const psz_var = (char* const) elements( varName->toCharArray());
284   vlc_value_t value;
285   if (VLC_VariableGet(this->id, psz_var, &value) != VLC_SUCCESS) {
286     // throw exception
287     return NULL;
288   }
289   return NULL;
290 }
291
292 /*
293  * This is an helper function to convert jstrings to char*s
294  * setString _assumes_ the char* dest has been allocated
295  * XXX: should return >= 0 on success, < 0 on error
296  */
297 void setString(jstring orig, char* dest) {
298   jsize chars = JvGetStringUTFRegion(orig, 0, orig->length(), dest);
299   dest[chars] = '\0';
300 }