]> git.sesse.net Git - vlc/blob - bindings/python-ctypes/header.py
python-ctypes: implement support for callbacks
[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     def __str__(self):
90         print "vlc.LogMessage(%d:%s): %s" % (self.severity, self.type, self.message)
91
92 class MediaControlPosition(ctypes.Structure):
93     _fields_= [
94                 ('origin', ctypes.c_ushort),
95                 ('key', ctypes.c_ushort),
96                 ('value', ctypes.c_longlong),
97                 ]
98
99     @staticmethod
100     def from_param(arg):
101         if isinstance(arg, (int, long)):
102             p=MediaControlPosition()
103             p.value=arg
104             p.key=2
105             return p
106         else:
107             return arg
108
109 class MediaControlPositionOrigin(ctypes.c_uint):
110     enum=(
111         'AbsolutePosition',
112         'RelativePosition',
113         'ModuloPosition',
114         )
115     def __repr__(self):
116         return self.enum[self.value]
117
118 class MediaControlException(ctypes.Structure):
119     _fields_= [
120                 ('code', ctypes.c_int),
121                 ('message', ctypes.c_char_p),
122                 ]
123     def init(self):
124         mediacontrol_exception_init(self)
125
126     def clear(self):
127         mediacontrol_exception_free(self)
128
129 class MediaControlStreamInformation(ctypes.Structure):
130     _fields_= [
131                 ('code', ctypes.c_int),
132                 ('message', ctypes.c_char_p),
133                 ]
134
135 class RGBPicture(ctypes.Structure):
136     _fields_= [
137                 ('width', ctypes.c_int),
138                 ('height', ctypes.c_int),
139                 ('type', ctypes.c_uint32),
140                 ('date', ctypes.c_longlong),
141                 ('size', ctypes.c_int),
142                 ('data', ctypes.c_char_p),
143                 ]
144
145     def free(self):
146         mediacontrol_RGBPicture__free(self)
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