]> git.sesse.net Git - vlc/blob - bindings/python-ctypes/header.py
python-ctypes: generate classes for enum typedefs
[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 MediaControlException(ctypes.Structure):
105     _fields_= [
106                 ('code', ctypes.c_int),
107                 ('message', ctypes.c_char_p),
108                 ]
109     def init(self):
110         mediacontrol_exception_init(self)
111
112     def clear(self):
113         mediacontrol_exception_free(self)
114
115 class MediaControlStreamInformation(ctypes.Structure):
116     _fields_= [
117                 ('code', ctypes.c_int),
118                 ('message', ctypes.c_char_p),
119                 ]
120
121 class RGBPicture(ctypes.Structure):
122     _fields_= [
123                 ('width', ctypes.c_int),
124                 ('height', ctypes.c_int),
125                 ('type', ctypes.c_uint32),
126                 ('date', ctypes.c_longlong),
127                 ('size', ctypes.c_int),
128                 ('data', ctypes.c_char_p),
129                 ]
130
131     def free(self):
132         mediacontrol_RGBPicture__free(self)
133
134 # Decorator for callback methods
135 callbackmethod=ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p)
136
137 def check_vlc_exception(result, func, args):
138     """Error checking method for functions using an exception in/out parameter.
139     """
140     ex=args[-1]
141     # Take into account both VLCException and MediacontrolException
142     c=getattr(ex, 'raised', getattr(ex, 'code', 0))
143     if c:
144         raise Exception(args[-1].message)
145     return result