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