]> git.sesse.net Git - vlc/blob - bindings/python-ctypes/header.py
python-ctypes: improve documentation generation
[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 ctypes
37 import sys
38
39 build_date="This will be replaced by the build date"
40
41 if sys.platform == 'linux2':
42     dll=ctypes.CDLL('libvlc.so')
43 elif sys.platform == 'win32':
44     dll=ctypes.CDLL('libvlc.dll')
45 elif sys.platform == 'darwin':
46     # FIXME: should find a means to configure path
47     dll=ctypes.CDLL('/Applications/VLC.app/Contents/MacOS/lib/libvlc.2.dylib')
48
49 class ListPOINTER(object):
50     '''Just like a POINTER but accept a list of ctype as an argument.
51     '''
52     def __init__(self, etype):
53         self.etype = etype
54
55     def from_param(self, param):
56         if isinstance(param, (list,tuple)):
57             return (self.etype * len(param))(*param)
58
59 # From libvlc_structures.h
60 class VLCException(ctypes.Structure):
61     _fields_= [
62                 ('raised', ctypes.c_int),
63                 ('code', ctypes.c_int),
64                 ('message', ctypes.c_char_p),
65                 ]
66     def init(self):
67         libvlc_exception_init(self)
68
69     def clear(self):
70         libvlc_exception_clear(self)
71
72 class PlaylistItem(ctypes.Structure):
73     _fields_= [
74                 ('id', ctypes.c_int),
75                 ('uri', ctypes.c_char_p),
76                 ('name', ctypes.c_char_p),
77                 ]
78
79 class LogMessage(ctypes.Structure):
80     _fields_= [
81                 ('size', ctypes.c_uint),
82                 ('severity', ctypes.c_int),
83                 ('type', ctypes.c_char_p),
84                 ('name', ctypes.c_char_p),
85                 ('header', ctypes.c_char_p),
86                 ('message', ctypes.c_char_p),
87                 ]
88
89 class MediaControlPosition(ctypes.Structure):
90     _fields_= [
91                 ('origin', ctypes.c_ushort),
92                 ('key', ctypes.c_ushort),
93                 ('value', ctypes.c_longlong),
94                 ]
95
96     @staticmethod
97     def from_param(arg):
98         if isinstance(arg, (int, long)):
99             p=MediaControlPosition()
100             p.value=arg
101             p.key=2
102             return p
103         else:
104             return arg
105
106 class MediaControlPositionOrigin(ctypes.c_uint):
107     enum=(
108         'AbsolutePosition',
109         'RelativePosition',
110         'ModuloPosition',
111         )
112     def __repr__(self):
113         return self.enum[self.value]
114
115 class MediaControlException(ctypes.Structure):
116     _fields_= [
117                 ('code', ctypes.c_int),
118                 ('message', ctypes.c_char_p),
119                 ]
120     def init(self):
121         mediacontrol_exception_init(self)
122
123     def clear(self):
124         mediacontrol_exception_free(self)
125
126 class MediaControlStreamInformation(ctypes.Structure):
127     _fields_= [
128                 ('code', ctypes.c_int),
129                 ('message', ctypes.c_char_p),
130                 ]
131
132 class RGBPicture(ctypes.Structure):
133     _fields_= [
134                 ('width', ctypes.c_int),
135                 ('height', ctypes.c_int),
136                 ('type', ctypes.c_uint32),
137                 ('date', ctypes.c_longlong),
138                 ('size', ctypes.c_int),
139                 ('data', ctypes.c_char_p),
140                 ]
141
142     def free(self):
143         mediacontrol_RGBPicture__free(self)
144
145 # Decorator for callback methods
146 callbackmethod=ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p)
147
148 def check_vlc_exception(result, func, args):
149     """Error checking method for functions using an exception in/out parameter.
150     """
151     ex=args[-1]
152     # Take into account both VLCException and MediacontrolException
153     c=getattr(ex, 'raised', getattr(ex, 'code', 0))
154     if c:
155         raise Exception(args[-1].message)
156     return result