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