Update of the convert.py script which generates now fully automatically the LibVLC...
authorMatthias Braeger <matthias.braeger@cern.ch>
Thu, 29 Jul 2010 07:23:37 +0000 (09:23 +0200)
committerFilippo Carone <littlejohn@videolan.org>
Fri, 30 Jul 2010 23:04:22 +0000 (01:04 +0200)
extras/LibVlc.java_body [new file with mode: 0644]
extras/convert.py

diff --git a/extras/LibVlc.java_body b/extras/LibVlc.java_body
new file mode 100644 (file)
index 0000000..5158161
--- /dev/null
@@ -0,0 +1,367 @@
+/*****************************************************************************
+ * VLC Java Bindings JNA Glue
+ *****************************************************************************
+ * Copyright (C) 1998-$year the VideoLAN team
+ *
+ * Authors: Filippo Carone <filippo@carone.org>
+ *          Matthias Braeger <mbraeger@cern.ch>
+ *          VLC bindings generator
+ *
+ *
+ * $Id $
+ *
+ * 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 of the License, 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ *****************************************************************************/
+
+package org.videolan.jvlc.internal;
+
+import com.sun.jna.Callback;
+import com.sun.jna.Library;
+import com.sun.jna.Native;
+import com.sun.jna.NativeLong;
+import com.sun.jna.Platform;
+import com.sun.jna.Pointer;
+import com.sun.jna.PointerType;
+import com.sun.jna.Structure;
+import com.sun.jna.Union;
+
+/**
+ * This is the libvlc wrapper class which allows to access all native libvlc
+ * functions.
+ * <br><br>
+ * Please note that this class was auto-generated with the VLC bindings
+ * generator which can be found under <code>JVLC_HOME/extras/</code>. In case
+ * you miss some functionalities you should maybe re-run that script, but this
+ * is without any warranty!
+ */
+public interface LibVlc extends Library
+{
+
+    LibVlc INSTANCE = (LibVlc) Native.loadLibrary(Platform.isWindows() ? "libvlc" : "vlc", LibVlc.class);
+
+    LibVlc SYNC_INSTANCE = (LibVlc) Native.synchronizedLibrary(INSTANCE);
+
+    public static class libvlc_exception_t extends Structure
+    {
+
+        public int b_raised;
+    }
+
+    public static interface LibVlcCallback extends Callback
+    {
+
+        void callback(libvlc_event_t libvlc_event, Pointer userData);
+    }
+
+    public static class libvlc_log_message_t extends Structure
+    {
+
+        public int sizeof_msg; /* sizeof() of message structure, must be filled in by user */
+
+        public int i_severity; /* 0=INFO, 1=ERR, 2=WARN, 3=DBG */
+
+        public String psz_type; /* module type */
+
+        public String psz_name; /* module name */
+
+        public String psz_header; /* optional header */
+
+        public String psz_message; /* message */
+    }
+
+    public static class libvlc_event_t extends Structure
+    {
+
+        public int type;
+
+        public Pointer p_obj;
+
+        public event_type_specific event_type_specific;
+
+    }
+
+    public class media_meta_changed extends Structure
+    {
+
+        // Enum !
+        public Pointer meta_type;
+    }
+
+    public class media_subitem_added extends Structure
+    {
+
+        public LibVlcMedia new_child;
+    }
+
+    public class media_duration_changed extends Structure
+    {
+
+        public NativeLong new_duration;
+    }
+
+    public class media_preparsed_changed extends Structure
+    {
+
+        public int new_status;
+    }
+
+    public class media_freed extends Structure
+    {
+
+        public LibVlcMedia md;
+    }
+
+    public class media_state_changed extends Structure
+    {
+
+        // @todo: check this one
+        public int new_state;
+    }
+
+    /* media instance */
+
+    public class media_player_position_changed extends Structure
+    {
+
+        public float new_position;
+    }
+
+    public class media_player_time_changed extends Structure
+    {
+
+        // @todo: check this one
+        public long new_time;
+    }
+
+    public class media_player_title_changed extends Structure
+    {
+
+        public int new_title;
+    }
+
+    public class media_player_seekable_changed extends Structure
+    {
+
+        public NativeLong new_seekable;
+    }
+
+    public class media_player_pausable_changed extends Structure
+    {
+
+        public NativeLong new_pausable;
+    }
+
+    /* media list */
+    public class media_list_item_added extends Structure
+    {
+
+        public LibVlcMedia item;
+
+        public int index;
+    }
+
+    public class media_list_will_add_item extends Structure
+    {
+
+        public LibVlcMedia item;
+
+        public int index;
+    }
+
+    public class media_list_item_deleted extends Structure
+    {
+
+        public LibVlcMedia item;
+
+        public int index;
+    }
+
+    public class media_list_will_delete_item extends Structure
+    {
+
+        public LibVlcMedia item;
+
+        public int index;
+    }
+
+    /* media list view */
+    public class media_list_view_item_added extends Structure
+    {
+
+        public LibVlcMedia item;
+
+        public int index;
+    }
+
+    public class media_list_view_will_add_item extends Structure
+    {
+
+        public LibVlcMedia item;
+
+        public int index;
+    }
+
+    public class media_list_view_item_deleted extends Structure
+    {
+
+        public LibVlcMedia item;
+
+        public int index;
+    }
+
+    public class media_list_view_will_delete_item extends Structure
+    {
+
+        public LibVlcMedia item;
+
+        public int index;
+    }
+
+    public class media_list_player_next_item_set extends Structure
+    {
+
+        public LibVlcMedia item;
+    }
+
+    public class media_player_snapshot_taken extends Structure
+    {
+
+        public String psz_filename;
+    }
+
+    public class media_player_length_changed extends Structure
+    {
+
+        // @todo: check the type
+        public long new_length;
+    }
+
+    public class vlm_media_event extends Structure
+    {
+
+        public String psz_media_name;
+
+        public String psz_instance_name;
+    }
+
+    public class event_type_specific extends Union
+    {
+
+        public media_meta_changed media_meta_changed;
+
+        public media_subitem_added media_subitem_added;
+
+        public media_duration_changed media_duration_changed;
+
+        public media_preparsed_changed media_preparsed_changed;
+
+        public media_freed media_freed;
+
+        public media_state_changed media_state_changed;
+
+        public media_player_position_changed media_player_position_changed;
+
+        public media_player_time_changed media_player_time_changed;
+
+        public media_player_title_changed media_player_title_changed;
+
+        public media_player_seekable_changed media_player_seekable_changed;
+
+        public media_player_pausable_changed media_player_pausable_changed;
+
+        public media_list_item_added media_list_item_added;
+
+        public media_list_will_add_item media_list_will_add_item;
+
+        public media_list_item_deleted media_list_item_deleted;
+
+        public media_list_will_delete_item media_list_will_delete_item;
+
+        public media_list_view_item_added media_list_view_item_added;
+
+        public media_list_view_will_add_item media_list_view_will_add_item;
+
+        public media_list_view_item_deleted media_list_view_item_deleted;
+
+        public media_list_view_will_delete_item media_list_view_will_delete_item;
+
+        public media_list_player_next_item_set media_list_player_next_item_set;
+
+        public media_player_snapshot_taken media_player_snapshot_taken;
+
+        public media_player_length_changed media_player_length_changed;
+
+        public vlm_media_event vlm_media_event;
+    }
+
+    public class LibVlcLog extends PointerType
+    {
+    }
+
+    public class LibVlcMediaListView extends PointerType
+    {
+    }
+
+    public class LibVlcTrackDescription extends PointerType
+    {
+    }
+
+    public class LibVlcMediaListPlayer extends PointerType
+    {
+    }
+
+    public class LibVlcInstance extends PointerType
+    {
+    }
+
+    public class LibVlcEventManager extends PointerType
+    {
+    }
+
+    public class LibVlcMediaLibrary extends PointerType
+    {
+    }
+
+    public class LibVlcMediaList extends PointerType
+    {
+    }
+
+    public class LibVlcAudioOutput extends PointerType
+    {
+    }
+
+    public class LibVlcMediaPlayer extends PointerType
+    {
+    }
+
+    public class LibVlcMedia extends PointerType
+    {
+    }
+
+    public class LibVlcMediaDiscoverer extends PointerType
+    {
+    }
+
+    public class LibVlcLogIterator extends PointerType
+    {
+    }
+
+    ///////////////////////////////////////////////////////////////////////////
+    //                     Auto generated method stubs                       //
+    ///////////////////////////////////////////////////////////////////////////
+
+    $libVLC_stubs
+}
+
index 6b753cb..8a67d3d 100755 (executable)
@@ -1,6 +1,9 @@
 #!/usr/bin/python
 """
-vlc to jna almost-automatized interface converter.
+VLC to jna interface converter.
+
+Authors: Filippo Carone <filippo@carone.org>
+         Matthias Braeger <mbraeger@cern.ch>
 
 This program is free software. It comes without any warranty, to
 the extent permitted by applicable law. You can redistribute it
@@ -10,78 +13,182 @@ http://sam.zoy.org/wtfpl/COPYING for more details.
 
 """
 
-import sys
+import datetime
+import glob
+import os
+
 
-file = open('libvlc.h', 'r')
+libvlc_search = "/usr/include/vlc/libvlc*.h"
+libvlc_body_file = "LibVlc.java_body"
+out_file_name = "../core/src/main/java/org/videolan/jvlc/internal/LibVlc.java"
 
 parameter_parsing = False
 prototype_parsing = False
 
+# Mapping table to convert C types into Java types
 types_map = [
+       ["...", "String... args"],
+       ["(void)", "()"],
+       ["const char * const *", "String[] "],
        ["const ", ""],
+        ["const*", "String[]"],
        ["VLC_PUBLIC_API ", ""],
        ["char**", "String[] "],
        ["char **" , "String[] "],
        ["char*" , "String "],
        ["char *" , "String "],
-       ["libvlc_instance_t *", "LibVlcInstance "],
+       ["libvlc_audio_output_t *", "LibVlcAudioOutput"],
+       ["libvlc_callback_t", "LibVlcCallback"],
+       ["libvlc_event_manager_t *", "LibVlcEventManager "],
+       ["libvlc_event_type_t", "int"],
        ["libvlc_exception_t *", "libvlc_exception_t "],
+       ["libvlc_instance_t *", "LibVlcInstance "],
        ["libvlc_log_t *", "LibVlcLog "],
        ["libvlc_log_iterator_t *", "LibVlcLogIterator "],
        ["libvlc_log_message_t *", "libvlc_log_message_t "],
+       ["libvlc_media_discoverer_t *", "LibVlcMediaDiscoverer"],
+       ["libvlc_media_library_t *", "LibVlcMediaLibrary"],
+       ["VLC_FORWARD_DECLARE_OBJECT(libvlc_media_list_t *)", "LibVlcMediaList"],
+       ["libvlc_media_list_t *", "LibVlcMediaList"],
+       ["libvlc_media_list_player_t *", "LibVlcMediaListPlayer"],
+       ["libvlc_media_list_view_t *", "LibVlcMediaListView"],
+        ["libvlc_media_t *", "LibVlcMedia "],
+        ["libvlc_media_player_t *", "LibVlcMediaPlayer "],
+       ["libvlc_meta_t", "Meta"],
+       ["libvlc_state_t", "int"],
+       ["libvlc_time_t", "long"],
+       ["libvlc_track_description_t *", "LibVlcTrackDescription "],
+       ["uint32_t", "long"],
+       ["unsigned int", "int"],
        ["unsigned", "int"],
+        ["void *", "Pointer "]
+]
+
+# Mapping table to convert the existing description into JavaDoc
+javadoc_map = [
+       ["\\bug", "@bug"],
+       ["\param", "@param"],
+       ["\\return", "@return"],
+       ["\see", "@see"]
 ]
 
+
+#
+# Converts the header prototype into a Java method
+#
+# proto:      The function prototype taken from the C file
+# parameters: list of parameters taken out of the function description.
+#             This implies of course that the description is correct and complete
+#
 def convert_prototype(proto, parameters):
-       #print proto
-       #print parameters
-       tokens = proto.split(",")
+       # replace first all c++ types by Java types
+       for k,v in types_map:
+               proto = proto.replace(k,v)
+
+       res = proto[:proto.find('(')+1] # resulting java method stub
+       tokens = proto[proto.find('(')+1:].strip().split(",")
        last = tokens.pop().split(")")
-       res = ''
        for i in tokens:
-               param = parameters.pop(0)
-               if i.find(param)==-1:
-                       res += i + " " + param + ","
+               if parameters != None and len(parameters):
+                       param = parameters.pop(0) 
+                       if i.find(param)==-1 and len(i.strip().split(' '))==1:
+                               res += i + " " + param + ","
+                       else:
+                               res += i + ","
                else:
-                       res += i + " ,"
-       if len(parameters):
+                       res += i + ","
+                       
+       if parameters != None and len(parameters):
                param = parameters.pop(0)
-               if last[0].find(param)==-1:
+               if last[0].find(param)==-1 and len(last[0].strip().split(' '))==1:
                        res += last[0] + " " + param + ")" + last[1]
                else:
                        res += last[0] + ")" + last[1]
+       else:
+               res += last[0] + ")" + last[1]
 
-       for k,v in types_map:
-               res = res.replace(k,v)
-       print res
 
-for line in file.readlines():
-       if line.find("/**")!=-1:
-               parameters = []
-               parameter_parsing = True
+       # Print resulting Java method stub
+       out_file.write("    " + res + '\n\n')
+
+
+#
+# Reads in the given file and creates out of it the Java
+# calls + JavaDoc.
+#
+def convert_header_file(file):
+       javadoc = ''
+       parameters = []
+       parameter_parsing = True
+       prototype_line = ''
+       prototype_parsing = True
+       for line in file.readlines():
+               if line.find("/**")!=-1:
+                       javadoc = ''
+                       parameters = []
+                       parameter_parsing = True
+
+               if not line.startswith('#') and line.find("VLC_PUBLIC_API")!=-1 and line.find("VLC_PUBLIC_API struct")==-1:
+                       if javadoc.find("\defgroup") == -1:
+                               out_file.write(javadoc);
+                       prototype_line = ''
+                       prototype_parsing = True
+
+               if parameter_parsing:
+                       # Create JavaDoc output
+                       javadoc_line = line
+                       for k,v in javadoc_map:
+                               javadoc_line = javadoc_line.replace(k,v)
+                       javadoc += "    " + javadoc_line
 
-       if line.find("VLC_PUBLIC_API")!=-1:
-               if not parameters:
+                       # Get parameter names
+                       param_index = line.find("\param ")
+                       if not param_index==-1:
+                               parameter = line.split()[2]
+                               parameters.append(parameter)
+                       if line.find("*/")!=-1:
+                               parameter_parsing = False
+
+               if prototype_parsing:
+                       prototype_line += line.strip() + ' '
+                       if line.find(");")!=-1:
+                               prototype_line.strip()
+                               prototype_parsing = False
+                               convert_prototype(prototype_line, parameters)
+                               javadoc = ''
+                               parameters = None
                        continue
-               prototype_line = ''
-               prototype_parsing = True
-
-       if parameter_parsing:
-               param_index = line.find("\param ")
-               if not param_index==-1:
-                       parameter = line.split()[2]
-                       parameters.append(parameter)
-               if line.find("*/")!=-1:
-                       parameter_parsing = False
-
-       if prototype_parsing:
-               prototype_line += line.strip()
-               if line.find(");")!=-1:
-                       prototype_parsing = False
-                       convert_prototype(prototype_line, parameters)
-                       parameters = None
-               continue
-
-       #sys.stdout.write(line)
-
-# vi:ts=4
+
+#
+# Parses all libvlc header files and generates out
+# the public functions the Java method stubs.
+#
+def generate_libvlc():
+       fileList = glob.glob(libvlc_search)
+       for fname in fileList:
+               file = open(fname, 'r')
+               convert_header_file(file)
+               out_file.write('\n');   
+
+
+#
+# Main Routine
+#
+body_file = open(libvlc_body_file, 'r')
+if os.path.exists(out_file_name):
+       os.rename(out_file_name, out_file_name + "_bak")
+       print "Moved " + out_file_name + " to " + out_file_name + "_bak"
+out_file = open(out_file_name, 'w')
+
+for line in body_file.readlines():
+       if line.find("$year")!=-1:
+               curr_date = datetime.datetime.now()
+               curr_year = '%d' % curr_date.year
+               line = line.replace("$year", curr_year)
+       if line.find("$libVLC_stubs")!=-1:
+               generate_libvlc()
+       else:
+               out_file.write(line)
+out_file.close()
+print "Finished generation of " + out_file_name
+print "\nBye"