1 /*****************************************************************************
2 * JVLC.java: CNI interface for vlc Java Bindings
3 *****************************************************************************
4 * Copyright (C) 1998-2005 the VideoLAN team
6 * Authors: Filippo Carone <filippo@carone.org>
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.
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.
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 *****************************************************************************/
27 #include <stdio.h> // for printf
28 #include <stdlib.h> // for calloc
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"
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>
47 void setString(jstring orig, char* dest);
49 jint org::videolan::jvlc::JVLC::create () {
53 jint org::videolan::jvlc::JVLC::init(JArray< ::java::lang::String *> *args) {
56 char* arguments[argc];
58 if (args != NULL) // It's a very bad day otherwise
64 * convert the JArray<String*>* in char**
65 * in a suitable way for VLC_Init
67 jstring* argsElements = elements(args);
69 for (int i = 0; i < argc; i++) {
70 arguments[i] = (char*) malloc(JvGetStringUTFLength(argsElements[i]) + 1);
71 setString(argsElements[i], arguments[i]);
75 return VLC_Init(0, argc, arguments);
79 jint org::videolan::jvlc::JVLC::addInterface(java::lang::String* moduleName, jboolean blocking, jboolean startPlay) {
81 char* psz_module = NULL;
82 if (moduleName != NULL) {
83 psz_module = (char *) malloc(JvGetStringUTFLength(moduleName));
84 setString(moduleName, psz_module);
89 if (blocking) i_blocking = 1;
90 if (startPlay) i_startPlay = 1;
92 int addIntf_res = VLC_AddIntf(this->id, (char* const) psz_module, i_blocking, i_startPlay);
94 if (psz_module != NULL)
102 jstring org::videolan::jvlc::JVLC::getVersion() {
103 return JvNewStringUTF(VLC_Version());
107 jstring org::videolan::jvlc::JVLC::getError(jint errorCode) {
108 return JvNewStringUTF(VLC_Error(errorCode));
112 jint org::videolan::jvlc::JVLC::die() {
113 return VLC_Die(this->id);
117 jint org::videolan::jvlc::JVLC::cleanUp() {
118 return VLC_CleanUp(this->id);
122 jint org::videolan::jvlc::JVLC::setVariable(org::videolan::jvlc::JVLCVariable *jvlcVariable) {
124 /* these are the two parameters given the the
125 * VLC_VariableSet() function
128 char* psz_var = NULL;
130 if (jvlcVariable != NULL) {
131 jclass variableClass = jvlcVariable->getClass();
133 /* We use the class name for kinda of instanceof */
134 jstring className = variableClass->getName();
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
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?
161 if (className->equals(JvNewStringUTF("VLCIntVariable" ))) {
162 value.i_int = ((org::videolan::jvlc::JVLCIntVariable *)jvlcVariable)->getIntValue();
165 else if (className->equals(JvNewStringUTF("VLCBoolVariable"))) {
166 value.b_bool = ((org::videolan::jvlc::JVLCBoolVariable *)jvlcVariable)->getBoolValue();
169 else if (className->equals(JvNewStringUTF("VLCFloatVariable"))) {
170 value.f_float = ((org::videolan::jvlc::JVLCFloatVariable *)jvlcVariable)->getFloatValue();
173 else if (className->equals(JvNewStringUTF("VLCStringVariable"))) {
174 value.psz_string = (char* const) elements((((org::videolan::jvlc::JVLCStringVariable *)jvlcVariable)->getStringValue())->toCharArray());
177 else if (className->equals(JvNewStringUTF("VLCTimeVariable"))) {
178 value.i_time = ((org::videolan::jvlc::JVLCTimeVariable *)jvlcVariable)->getTimeValue();
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();
188 psz_var = (char *) malloc(JvGetStringUTFLength(jvlcVariable->getName()));
189 setString(jvlcVariable->getName(), psz_var);
192 return VLC_VariableSet(this->id, (char* const) psz_var, value);
196 jint org::videolan::jvlc::JVLC::addTarget(::java::lang::String *URI, JArray< ::java::lang::String *> *options, jint insertMode, jint position) {
198 char* psz_target = NULL;
199 char** ppsz_options = NULL;
200 int options_number = 0;
203 psz_target = (char *) malloc( JvGetStringUTFLength(URI));
204 setString(URI, psz_target);
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);
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]);
219 int res = VLC_AddTarget(this->id, (char const *)psz_target, (const char **) ppsz_options, options_number, insertMode, position);
221 if (ppsz_options != NULL) {
222 for (int i = 0; i < options_number; i++) {
223 free(ppsz_options[i]);
227 if (psz_target != NULL) {
235 jint org::videolan::jvlc::JVLC::play() {
236 return VLC_Play(this->id);
239 jint org::videolan::jvlc::JVLC::pause() {
240 return VLC_Pause(this->id);
243 jint org::videolan::jvlc::JVLC::stop() {
244 return VLC_Stop(this->id);
247 jboolean org::videolan::jvlc::JVLC::isPlaying() {
248 return VLC_IsPlaying(this->id);
251 jfloat org::videolan::jvlc::JVLC::getPosition() {
252 return VLC_PositionGet(this->id);
255 jfloat org::videolan::jvlc::JVLC::setPosition(jfloat position) {
256 return VLC_PositionSet(this->id, position);
259 jint org::videolan::jvlc::JVLC::getTime() {
260 return VLC_TimeGet(this->id);
263 jint org::videolan::jvlc::JVLC::setTime(jint seconds, jboolean relative) {
264 return VLC_TimeSet(this->id, seconds, relative);
267 jint org::videolan::jvlc::JVLC::getLength() {
268 return VLC_LengthGet(this->id);
271 jfloat org::videolan::jvlc::JVLC::speedFaster() {
272 return VLC_SpeedFaster(this->id);
275 jfloat org::videolan::jvlc::JVLC::speedSlower() {
276 return VLC_SpeedSlower(this->id);
279 jint org::videolan::jvlc::JVLC::getPlaylistIndex() {
280 return VLC_PlaylistIndex(this->id);
283 jint org::videolan::jvlc::JVLC::getPlaylistItems() {
284 return VLC_PlaylistNumberOfItems(this->id);
287 jint org::videolan::jvlc::JVLC::playlistNext() {
288 return VLC_PlaylistNext(this->id);
291 jint org::videolan::jvlc::JVLC::playlistPrev() {
292 return VLC_PlaylistPrev(this->id);
295 jint org::videolan::jvlc::JVLC::playlistClear() {
296 return VLC_PlaylistClear(this->id);
299 jint org::videolan::jvlc::JVLC::setVolume(jint volume) {
300 return VLC_VolumeSet(this->id, volume);
303 jint org::videolan::jvlc::JVLC::getVolume() {
304 return VLC_VolumeGet(this->id);
307 jint org::videolan::jvlc::JVLC::muteVolume() {
308 return VLC_VolumeMute(this->id);
311 jint org::videolan::jvlc::JVLC::fullScreen() {
312 return VLC_FullScreen(this->id);
316 /* XXX: in progress */
317 org::videolan::jvlc::JVLCVariable* org::videolan::jvlc::JVLC::getVariable(::java::lang::String* varName) {
319 char* const psz_var = (char* const) elements( varName->toCharArray());
321 if (VLC_VariableGet(this->id, psz_var, &value) != VLC_SUCCESS) {
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
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.