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