]> git.sesse.net Git - vlc/blob - bindings/python-ctypes/header.py
python-ctypes: fix pylint/pyflakes warnings
[vlc] / bindings / python-ctypes / header.py
1 #! /usr/bin/python
2
3 #
4 # Python ctypes bindings for VLC
5 # Copyright (C) 2009 the VideoLAN team
6 # $Id: $
7 #
8 # Authors: Olivier Aubert <olivier.aubert at liris.cnrs.fr>
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 #
24
25 """This module provides bindings for the
26 U{libvlc<http://wiki.videolan.org/ExternalAPI>} and
27 U{MediaControl<http://wiki.videolan.org/MediaControlAPI>} APIs.
28
29 You can find documentation at U{http://www.advene.org/download/python-ctypes/}.
30
31 Basically, the most important class is L{Instance}, which is used to
32 create a libvlc Instance. From this instance, you can then create
33 L{MediaPlayer} and L{MediaListPlayer} instances.
34 """
35
36 import logging
37 import ctypes
38 import sys
39
40 build_date="This will be replaced by the build date"
41
42 # Used for win32 and MacOS X
43 detected_plugin_path=None
44
45 if sys.platform == 'linux2':
46     dll=ctypes.CDLL('libvlc.so')
47 elif sys.platform == 'win32':
48     import ctypes.util
49     import os
50     detected_plugin_path=None
51     path=ctypes.util.find_library('libvlc.dll')
52     if path is None:
53         # Try to use registry settings
54         import _winreg
55         detected_plugin_path_found = None
56         subkey, name = 'Software\\VideoLAN\\VLC','InstallDir'
57         for hkey in _winreg.HKEY_LOCAL_MACHINE, _winreg.HKEY_CURRENT_USER:
58             try:
59                 reg = _winreg.OpenKey(hkey, subkey)
60                 detected_plugin_path_found, type_id = _winreg.QueryValueEx(reg, name)
61                 _winreg.CloseKey(reg)
62                 break
63             except _winreg.error:
64                 pass
65         if detected_plugin_path_found:
66             detected_plugin_path = detected_plugin_path_found
67         else:
68             # Try a standard location.
69             p='c:\\Program Files\\VideoLAN\\VLC\\libvlc.dll'
70             if os.path.exists(p):
71                 detected_plugin_path=os.path.dirname(p)
72         os.chdir(detected_plugin_path)
73         # If chdir failed, this will not work and raise an exception
74         path='libvlc.dll'
75     else:
76         detected_plugin_path=os.path.dirname(path)
77     dll=ctypes.CDLL(path)
78 elif sys.platform == 'darwin':
79     # FIXME: should find a means to configure path
80     d='/Applications/VLC.app'
81     import os
82     if os.path.exists(d):
83         dll=ctypes.CDLL(d+'/Contents/MacOS/lib/libvlc.2.dylib')
84         detected_plugin_path=d+'/Contents/MacOS/modules'
85     else:
86         # Hope some default path is set...
87         dll=ctypes.CDLL('libvlc.2.dylib')
88
89 #
90 # Generated enum types.
91 #
92
93 # GENERATED_ENUMS
94
95 #
96 # End of generated enum types.
97 #
98
99 class ListPOINTER(object):
100     '''Just like a POINTER but accept a list of ctype as an argument.
101     '''
102     def __init__(self, etype):
103         self.etype = etype
104
105     def from_param(self, param):
106         if isinstance(param, (list, tuple)):
107             return (self.etype * len(param))(*param)
108
109 class LibVLCException(Exception):
110     """Python exception raised by libvlc methods.
111     """
112     pass
113
114 # From libvlc_structures.h
115
116 # This is version-dependent, depending on the presence of libvlc_errmsg
117
118 if hasattr(dll, 'libvlc_errmsg'):
119     # New-style message passing
120     class VLCException(ctypes.Structure):
121         """libvlc exception.
122         """
123         _fields_= [
124                     ('raised', ctypes.c_int),
125                     ]
126
127         @property
128         def message(self):
129             return dll.libvlc_errmsg()
130
131         def init(self):
132             libvlc_exception_init(self)
133
134         def clear(self):
135             libvlc_exception_clear(self)
136 else:
137     # Old-style exceptions
138     class VLCException(ctypes.Structure):
139         """libvlc exception.
140         """
141         _fields_= [
142                     ('raised', ctypes.c_int),
143                     ('code', ctypes.c_int),
144                     ('message', ctypes.c_char_p),
145                     ]
146         def init(self):
147             libvlc_exception_init(self)
148
149         def clear(self):
150             libvlc_exception_clear(self)
151
152 class PlaylistItem(ctypes.Structure):
153     _fields_= [
154                 ('id', ctypes.c_int),
155                 ('uri', ctypes.c_char_p),
156                 ('name', ctypes.c_char_p),
157                 ]
158
159     def __str__(self):
160         return "PlaylistItem #%d %s (%uri)" % (self.id, self.name, self.uri)
161
162 class LogMessage(ctypes.Structure):
163     _fields_= [
164                 ('size', ctypes.c_uint),
165                 ('severity', ctypes.c_int),
166                 ('type', ctypes.c_char_p),
167                 ('name', ctypes.c_char_p),
168                 ('header', ctypes.c_char_p),
169                 ('message', ctypes.c_char_p),
170                 ]
171
172     def __init__(self):
173         super(LogMessage, self).__init__()
174         self.size=ctypes.sizeof(self)
175
176     def __str__(self):
177         return "vlc.LogMessage(%d:%s): %s" % (self.severity, self.type, self.message)
178
179 class MediaControlPosition(ctypes.Structure):
180     _fields_= [
181                 ('origin', PositionOrigin),
182                 ('key', PositionKey),
183                 ('value', ctypes.c_longlong),
184                 ]
185
186     def __init__(self, value=0, origin=None, key=None):
187         # We override the __init__ method so that instanciating the
188         # class with an int as parameter will create the appropriate
189         # default position (absolute position, media time, with the
190         # int as value).
191         super(MediaControlPosition, self).__init__()
192         self.value=value
193         if origin is None:
194             origin=PositionOrigin.AbsolutePosition
195         if key is None:
196             key=PositionKey.MediaTime
197         self.origin=origin
198         self.key=key
199
200     def __str__(self):
201         return "MediaControlPosition %ld (%s, %s)" % (
202             self.value,
203             str(self.origin),
204             str(self.key)
205             )
206
207     @staticmethod
208     def from_param(arg):
209         if isinstance(arg, (int, long)):
210             return MediaControlPosition(arg)
211         else:
212             return arg
213
214 class MediaControlException(ctypes.Structure):
215     _fields_= [
216                 ('code', ctypes.c_int),
217                 ('message', ctypes.c_char_p),
218                 ]
219     def init(self):
220         mediacontrol_exception_init(self)
221
222     def clear(self):
223         mediacontrol_exception_free(self)
224
225 class MediaControlStreamInformation(ctypes.Structure):
226     _fields_= [
227                 ('status', PlayerStatus),
228                 ('url', ctypes.c_char_p),
229                 ('position', ctypes.c_longlong),
230                 ('length', ctypes.c_longlong),
231                 ]
232
233     def __str__(self):
234         return "%s (%s) : %ld / %ld" % (self.url or "<No defined URL>",
235                                         str(self.status),
236                                         self.position,
237                                         self.length)
238
239 class RGBPicture(ctypes.Structure):
240     _fields_= [
241                 ('width', ctypes.c_int),
242                 ('height', ctypes.c_int),
243                 ('type', ctypes.c_uint32),
244                 ('date', ctypes.c_ulonglong),
245                 ('size', ctypes.c_int),
246                 ('data_pointer', ctypes.c_void_p),
247                 ]
248
249     @property
250     def data(self):
251         return ctypes.string_at(self.data_pointer, self.size)
252
253     def __str__(self):
254         return "RGBPicture (%d, %d) - %ld ms - %d bytes" % (self.width, self.height, self.date, self.size)
255
256     def free(self):
257         mediacontrol_RGBPicture__free(self)
258
259 def check_vlc_exception(result, func, args):
260     """Error checking method for functions using an exception in/out parameter.
261     """
262     ex=args[-1]
263     if not isinstance(ex, (VLCException, MediaControlException)):
264         logging.warn("python-vlc: error when processing function %s. Please report this as a bug to vlc-devel@videolan.org" % str(func))
265         return result
266     # Take into account both VLCException and MediacontrolException:
267     c=getattr(ex, 'raised', getattr(ex, 'code', 0))
268     if c:
269         raise LibVLCException(ex.message)
270     return result
271
272 ### End of header.py ###