]> git.sesse.net Git - vlc/blob - bindings/python/vlc_module.c
python bindings: workaround for the #2257 / #2266 VLC bug (linux only ATM)
[vlc] / bindings / python / vlc_module.c
1 /*****************************************************************************
2  * vlc_module.c: vlc python binding module
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Olivier Aubert <oaubert at bat710.univ-lyon1.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "vlcglue.h"
25 #include <dlfcn.h>
26
27 /**************************************************************************
28  * VLC Module
29  **************************************************************************/
30
31 #ifndef vlcMODINIT_FUNC /* declarations for DLL import/export */
32 #define vlcMODINIT_FUNC void
33 #endif
34
35 static PyMethodDef vlc_methods[] = {
36     { NULL }  /* Sentinel */
37 };
38
39 /* Module globals */
40 PyObject* MediaControl_InternalException          = NULL;
41 PyObject* MediaControl_PositionKeyNotSupported    = NULL;
42 PyObject *MediaControl_PositionOriginNotSupported = NULL;
43 PyObject* MediaControl_InvalidPosition            = NULL;
44 PyObject *MediaControl_PlaylistException          = NULL;
45
46 vlcMODINIT_FUNC
47 initvlc( void )
48 {
49     PyObject* p_module;
50
51 #ifdef WIN32
52     /*
53       FIXME: Win32 support is trickier than that (does not support dlopen).
54       Get code from src/modules/os.c to work on different OSes.
55     */
56     #define LIBVLCCORE "libvlccore.dll"
57 #else
58     #define LIBVLCCORE "libvlccore.so"
59 #endif
60     /* Workaround for the 2257/2266 VLC bug. */
61     if ( !dlopen( LIBVLCCORE, RTLD_NOW | RTLD_GLOBAL ) )
62     {
63         fprintf( stderr, "Error when loading libvlccore:\n%s\nTrying to continue anyway.\n", dlerror() );
64     }
65
66     /* vlcMediaPlayer_Type.tp_new = PyType_GenericNew; */
67     vlcMediaPlayer_Type.tp_alloc = PyType_GenericAlloc;
68     /* vlcMedia_Type.tp_new = PyType_GenericNew; */
69     vlcMedia_Type.tp_alloc = PyType_GenericAlloc;
70
71     vlcInstance_Type.tp_alloc = PyType_GenericAlloc;
72     MediaControl_Type.tp_alloc = PyType_GenericAlloc;
73
74     p_module = Py_InitModule3( "vlc", vlc_methods,
75                                "VLC media player embedding module." );
76
77     if( !p_module )
78       return;
79
80     if( PyType_Ready( &PyPosition_Type ) < 0 )
81         return;
82     if( PyType_Ready( &MediaControl_Type ) < 0 )
83         return;
84     if( PyType_Ready( &vlcInstance_Type ) < 0 )
85         return;
86     if( PyType_Ready( &vlcMediaPlayer_Type ) < 0 )
87         return;
88     if( PyType_Ready( &vlcMedia_Type ) < 0 )
89         return;
90
91     /* Exceptions */
92     MediaControl_InternalException =
93             PyErr_NewException( "vlc.InternalException", NULL, NULL );
94     Py_INCREF( MediaControl_InternalException );
95     PyModule_AddObject( p_module, "InternalException",
96                         MediaControl_InternalException );
97
98     MediaControl_PositionKeyNotSupported =
99             PyErr_NewException( "vlc.PositionKeyNotSupported", NULL, NULL );
100     Py_INCREF( MediaControl_PositionKeyNotSupported );
101     PyModule_AddObject( p_module, "PositionKeyNotSupported",
102                         MediaControl_PositionKeyNotSupported );
103
104     MediaControl_PositionOriginNotSupported=
105             PyErr_NewException( "vlc.InvalidPosition", NULL, NULL );
106     Py_INCREF( MediaControl_PositionOriginNotSupported );
107     PyModule_AddObject( p_module, "PositionOriginNotSupported",
108                         MediaControl_PositionOriginNotSupported );
109
110     MediaControl_InvalidPosition =
111             PyErr_NewException( "vlc.InvalidPosition", NULL, NULL );
112     Py_INCREF( MediaControl_InvalidPosition );
113     PyModule_AddObject( p_module, "InvalidPosition",
114                         MediaControl_InvalidPosition );
115
116     MediaControl_PlaylistException =
117             PyErr_NewException( "vlc.PlaylistException", NULL, NULL );
118     Py_INCREF( MediaControl_PlaylistException );
119     PyModule_AddObject( p_module, "PlaylistException",
120                         MediaControl_PlaylistException );
121
122     /* Exceptions */
123     vlc_Exception =
124         PyErr_NewException( "vlc.InstanceException", NULL, NULL );
125     Py_INCREF( vlc_Exception );
126     PyModule_AddObject( p_module, "InstanceException",
127                         vlc_Exception );
128
129     /* Types */
130     Py_INCREF( &PyPosition_Type );
131     PyModule_AddObject( p_module, "Position",
132                         ( PyObject * )&PyPosition_Type );
133
134     Py_INCREF( &MediaControl_Type );
135     PyModule_AddObject( p_module, "MediaControl",
136                         ( PyObject * )&MediaControl_Type );
137
138     Py_INCREF( &vlcInstance_Type );
139     PyModule_AddObject( p_module, "Instance",
140                         ( PyObject * )&vlcInstance_Type );
141
142     Py_INCREF( &vlcMediaPlayer_Type );
143     PyModule_AddObject( p_module, "MediaPlayer",
144                         ( PyObject * )&vlcMediaPlayer_Type );
145
146     Py_INCREF( &vlcMedia_Type );
147     PyModule_AddObject( p_module, "Media",
148                         ( PyObject * )&vlcMedia_Type );
149
150     /* Constants */
151     PyModule_AddIntConstant( p_module, "AbsolutePosition",
152                              mediacontrol_AbsolutePosition );
153     PyModule_AddIntConstant( p_module, "RelativePosition",
154                              mediacontrol_RelativePosition );
155     PyModule_AddIntConstant( p_module, "ModuloPosition",
156                              mediacontrol_ModuloPosition );
157
158     PyModule_AddIntConstant( p_module, "ByteCount",
159                              mediacontrol_ByteCount );
160     PyModule_AddIntConstant( p_module, "SampleCount",
161                              mediacontrol_SampleCount );
162     PyModule_AddIntConstant( p_module, "MediaTime",
163                              mediacontrol_MediaTime );
164
165     PyModule_AddIntConstant( p_module, "PlayingStatus",
166                              mediacontrol_PlayingStatus );
167     PyModule_AddIntConstant( p_module, "PauseStatus",
168                              mediacontrol_PauseStatus );
169     PyModule_AddIntConstant( p_module, "InitStatus",
170                              mediacontrol_InitStatus );
171     PyModule_AddIntConstant( p_module, "EndStatus",
172                              mediacontrol_EndStatus );
173     PyModule_AddIntConstant( p_module, "UndefinedStatus",
174                              mediacontrol_UndefinedStatus );
175
176 }
177
178 /* Horrible hack... Please do not look.  Temporary workaround for the
179    forward declaration mess of python types (cf vlcglue.h). If we do a
180    separate compilation, we have to declare some types as extern. But
181    the recommended way to forward declared types in python is
182    static... I am sorting the mess but in the meantime, this will
183    produce a working python module.
184 */
185 #include "vlc_mediacontrol.c"
186 #include "vlc_position.c"
187 #include "vlc_instance.c"
188 #include "vlc_mediaplayer.c"
189 #include "vlc_media.c"