%module VLC

/* Just to be able to compile, these 2 functions fail for some reason. TODO Need to investigate why. */
%ignore libvlc_vprinterr;
%ignore libvlc_module_description_list_get;

/*
   There's no good portable way to produce char** from any non-C language.
   These typemaps are to be able to call libvlc_new(argc, argv)
   Here in both cases reference to a list of strings is converted to argc+argv.
Python: inst = VLC.libvlc_new(["vlc", "foo", "bar"])
Perl: my $inst = VLC::libvlc_new(["vlc", "foo", "bar"])
*/

#ifdef SWIGPYTHON
%typemap(in) (int argc, char const*const*argv) {
    int i;
    if (!PyList_Check($input)) {
        PyErr_SetString(PyExc_ValueError, "Expecting a list");
        return NULL;
    }
    $1 = PyList_Size($input);
    $2 = (char **) malloc(($1+1)*sizeof(char *));
    for (i = 0; i < $1; i++) {
        PyObject *s = PyList_GetItem($input,i);
        if (!PyString_Check(s)) {
            free($2);
            PyErr_SetString(PyExc_ValueError, "List items must be strings");
            return NULL;
        }
/* py3 needs another string conversion, PyUnicode_AsUTF8String() + PyBytes_AsString()
TODO: add #ifdef to check python version */
        $2[i] = PyString_AsString(s);
    }
    $2[i] = 0;
}
%typemap(freearg) (int argc, char const*const*argv) {
    if ($2) free($2);
}
#endif

#ifdef SWIGPERL
%typemap(in) (int argc, char const*const*argv) {
    AV *tempav;
    I32 len;
    int i;
    SV  **tv;
    if (!SvROK($input))
        croak("Argument $argnum is not a reference.");
    if (SvTYPE(SvRV($input)) != SVt_PVAV)
        croak("Argument $argnum doesn't refer to an array.");
    tempav = (AV*)SvRV($input);
    len = av_len(tempav);
    $2 = (char **) malloc((len+2)*sizeof(char *));
    for (i = 0; i <= len; i++) {
        tv = av_fetch(tempav, i, 0);    
        $2[i] = (char *) SvPV(*tv,PL_na);
    }
    $2[i] = NULL;
    $1 = len + 1;
}
%typemap(freearg) (int argc, char const*const*argv) {
    if ($2) free($2);
}
#endif

%{
#include <vlc/vlc.h>
%}
%include <vlc/libvlc_structures.h>
%include <vlc/libvlc.h>
/*
%include <vlc/libvlc_media.h>
%include <vlc/libvlc_media_player.h>
%include <vlc/libvlc_media_list.h>
%include <vlc/libvlc_media_list_player.h>
%include <vlc/libvlc_media_library.h>
%include <vlc/libvlc_media_discoverer.h>
%include <vlc/libvlc_events.h>
%include <vlc/libvlc_vlm.h>
*/

