]> git.sesse.net Git - vlc/blob - bindings/python/vlc_position.c
python bindings: allow to pass values in the vlc.Position constructor:
[vlc] / bindings / python / vlc_position.c
1 /*****************************************************************************
2  * vlc_position.c: vlc.Position binding
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Olivier Aubert <oaubert at bat710.univ-lyon1.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 #include "vlcglue.h"
24
25 /***********************************************************************
26  * Position
27  ***********************************************************************/
28
29 static PyObject *
30 PyPosition_new( PyTypeObject *type, PyObject *args, PyObject *kwds )
31 {
32     PyPosition *self;
33     static char *kwlist[] = { "value", "origin", "key", NULL};
34     
35     self = PyObject_New( PyPosition, &PyPosition_Type );
36
37     self->value=0;
38     self->origin=mediacontrol_AbsolutePosition;
39     self->key=mediacontrol_MediaTime;
40
41     /* We do not care about the return value, since it will leave the fields
42        with their default value. */
43     if(! PyArg_ParseTupleAndKeywords( args, kwds, "|lii", kwlist,
44                                       &(self->value),
45                                       &(self->origin),
46                                       &(self->key) ) )
47     {
48         return NULL;        
49     }
50        
51     Py_INCREF( self );
52     return ( PyObject * )self;    
53 }
54
55 mediacontrol_PositionKey
56 positionKey_py_to_c( PyObject * py_key )
57 {
58     mediacontrol_PositionKey key_position = mediacontrol_MediaTime;
59     int key;
60
61     if( !PyArg_Parse( py_key, "i", &key ) )
62     {
63         PyErr_SetString ( MediaControl_InternalException, "Invalid key value" );
64         return key_position;
65     }
66
67     switch ( key )
68     {
69     case 0: key = mediacontrol_ByteCount;   break;
70     case 1: key = mediacontrol_SampleCount; break;
71     case 2: key = mediacontrol_MediaTime;   break;
72     }
73     return key_position;
74 }
75
76 mediacontrol_PositionOrigin
77 positionOrigin_py_to_c( PyObject * py_origin )
78 {
79     mediacontrol_PositionOrigin  origin_position = mediacontrol_AbsolutePosition;
80     int origin;
81
82     if( !PyArg_Parse( py_origin,"i", &origin ) )
83     {
84         PyErr_SetString( MediaControl_InternalException,
85                          "Invalid origin value" );
86         return origin_position;
87     }
88
89     switch ( origin )
90     {
91     case 0: origin_position = mediacontrol_AbsolutePosition; break;
92     case 1: origin_position = mediacontrol_RelativePosition; break;
93     case 2: origin_position = mediacontrol_ModuloPosition;   break;
94     }
95
96     return origin_position;
97 }
98
99 /* Methods for transforming the Position Python object to Position structure*/
100 mediacontrol_Position*
101 position_py_to_c( PyObject * py_position )
102 {
103     mediacontrol_Position * a_position = NULL;
104     PyPosition *pos = ( PyPosition* )py_position;
105
106     a_position = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
107     if( !a_position )
108     {
109         PyErr_SetString( PyExc_MemoryError, "Out of memory" );
110         return NULL;
111     }
112
113     if( !py_position )
114     {
115         /* If we give a NULL value, it will be considered as
116            a 0 relative position in mediatime */
117         a_position->origin = mediacontrol_RelativePosition;
118         a_position->key    = mediacontrol_MediaTime;
119         a_position->value  = 0;
120     }
121     else if( PyObject_IsInstance( py_position, ( PyObject* )&PyPosition_Type ) )
122     {
123         a_position->origin = pos->origin;
124         a_position->key    = pos->key;
125         a_position->value  = ntohll(pos->value);
126     }
127     else
128     {
129         /* Feature: if we give an integer, it will be considered as
130            a relative position in mediatime */
131         a_position->origin = mediacontrol_RelativePosition;
132         a_position->key    = mediacontrol_MediaTime;
133         a_position->value  = PyLong_AsLongLong( py_position );
134     }
135     return a_position;
136 }
137
138 PyPosition*
139 position_c_to_py( mediacontrol_Position *position )
140 {
141     PyPosition* py_retval;
142
143     py_retval = PyObject_New( PyPosition, &PyPosition_Type );
144     py_retval->origin = position->origin;
145     py_retval->key    = position->key;
146     py_retval->value  = position->value;
147
148     return py_retval;
149 }
150
151 static PyMethodDef PyPosition_methods[] =
152 {
153     { NULL }  /* Sentinel */
154 };
155
156 static PyMemberDef PyPosition_members[] =
157 {
158     { "origin", T_INT, offsetof( PyPosition, origin ), 0, "Position origin" },
159     { "key",    T_INT, offsetof( PyPosition, key ),    0, "Position key" },
160     { "value",  T_ULONG, offsetof( PyPosition, value ), 0, "Position value" },
161     { NULL }  /* Sentinel */
162 };
163
164 static PyTypeObject PyPosition_Type =
165 {
166     PyObject_HEAD_INIT( NULL )
167     0,                         /*ob_size*/
168     "vlc.Position",            /*tp_name*/
169     sizeof( PyPosition_Type ),   /*tp_basicsize*/
170     0,                         /*tp_itemsize*/
171     0,                         /*tp_dealloc*/
172     0,                         /*tp_print*/
173     0,                         /*tp_getattr*/
174     0,                         /*tp_setattr*/
175     0,                         /*tp_compare*/
176     0,                         /*tp_repr*/
177     0,                         /*tp_as_number*/
178     0,                         /*tp_as_sequence*/
179     0,                         /*tp_as_mapping*/
180     0,                         /*tp_hash */
181     0,                         /*tp_call*/
182     0,                         /*tp_str*/
183     0,                         /*tp_getattro*/
184     0,                         /*tp_setattro*/
185     0,                         /*tp_as_buffer*/
186     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
187     "Represent a Position with value, origin and key",  /* tp_doc */
188     0,                        /* tp_traverse */
189     0,                        /* tp_clear */
190     0,                         /* tp_richcompare */
191     0,                         /* tp_weaklistoffset */
192     0,                         /* tp_iter */
193     0,                          /* tp_iternext */
194     PyPosition_methods,             /* tp_methods */
195     PyPosition_members,             /* tp_members */
196     0,                         /* tp_getset */
197     0,                         /* tp_base */
198     0,                         /* tp_dict */
199     0,                         /* tp_descr_get */
200     0,                         /* tp_descr_set */
201     0,                         /* tp_dictoffset */
202     0,                         /* tp_init */
203     0,                         /* tp_alloc */
204     PyPosition_new,            /* tp_new */
205 };