]> git.sesse.net Git - vlc/blob - bindings/python/vlc_position.c
Removes trailing spaces. Removes tabs.
[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     if(! PyArg_ParseTupleAndKeywords( args, kwds, "|lii", kwlist,
42                                       &(self->value),
43                                       &(self->origin),
44                                       &(self->key) ) )
45     {
46         return NULL;
47     }
48
49     if( self->key != mediacontrol_MediaTime
50     && self->key != mediacontrol_ByteCount
51     && self->key != mediacontrol_SampleCount )
52     {
53         PyErr_SetString ( MediaControl_InternalException, "Invalid key value" );
54         return NULL;
55     }
56
57     if( self->origin != mediacontrol_AbsolutePosition
58     && self->origin != mediacontrol_RelativePosition
59     && self->origin != mediacontrol_ModuloPosition )
60     {
61         PyErr_SetString ( MediaControl_InternalException, "Invalid origin value" );
62         return NULL;
63     }
64
65     Py_INCREF( self );
66     return ( PyObject * )self;
67 }
68
69 mediacontrol_PositionKey
70 positionKey_py_to_c( PyObject * py_key )
71 {
72     mediacontrol_PositionKey key_position = mediacontrol_MediaTime;
73     int key;
74
75     if( !PyArg_Parse( py_key, "i", &key ) )
76     {
77         PyErr_SetString ( MediaControl_InternalException, "Invalid key value" );
78         return key_position;
79     }
80
81     switch ( key )
82     {
83     case 0: key = mediacontrol_ByteCount;   break;
84     case 1: key = mediacontrol_SampleCount; break;
85     case 2: key = mediacontrol_MediaTime;   break;
86     }
87     return key_position;
88 }
89
90 mediacontrol_PositionOrigin
91 positionOrigin_py_to_c( PyObject * py_origin )
92 {
93     mediacontrol_PositionOrigin  origin_position = mediacontrol_AbsolutePosition;
94     int origin;
95
96     if( !PyArg_Parse( py_origin,"i", &origin ) )
97     {
98         PyErr_SetString( MediaControl_InternalException,
99                          "Invalid origin value" );
100         return origin_position;
101     }
102
103     switch ( origin )
104     {
105     case 0: origin_position = mediacontrol_AbsolutePosition; break;
106     case 1: origin_position = mediacontrol_RelativePosition; break;
107     case 2: origin_position = mediacontrol_ModuloPosition;   break;
108     }
109
110     return origin_position;
111 }
112
113 /* Methods for transforming the Position Python object to Position structure*/
114 mediacontrol_Position*
115 position_py_to_c( PyObject * py_position )
116 {
117     mediacontrol_Position * a_position = NULL;
118     PyPosition *pos = ( PyPosition* )py_position;
119
120     a_position = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
121     if( !a_position )
122     {
123         PyErr_SetString( PyExc_MemoryError, "Out of memory" );
124         return NULL;
125     }
126
127     if( !py_position )
128     {
129         /* If we give a NULL value, it will be considered as
130            a 0 relative position in mediatime */
131         a_position->origin = mediacontrol_RelativePosition;
132         a_position->key    = mediacontrol_MediaTime;
133         a_position->value  = 0;
134     }
135     else if( PyObject_IsInstance( py_position, ( PyObject* )&PyPosition_Type ) )
136     {
137         a_position->origin = pos->origin;
138         a_position->key    = pos->key;
139         a_position->value  = ntohll(pos->value);
140     }
141     else
142     {
143         /* Feature: if we give an integer, it will be considered as
144            a relative position in mediatime */
145         a_position->origin = mediacontrol_RelativePosition;
146         a_position->key    = mediacontrol_MediaTime;
147         a_position->value  = PyLong_AsLongLong( py_position );
148     }
149     return a_position;
150 }
151
152 PyPosition*
153 position_c_to_py( mediacontrol_Position *position )
154 {
155     PyPosition* py_retval;
156
157     py_retval = PyObject_New( PyPosition, &PyPosition_Type );
158     py_retval->origin = position->origin;
159     py_retval->key    = position->key;
160     py_retval->value  = position->value;
161
162     return py_retval;
163 }
164
165 static PyMethodDef PyPosition_methods[] =
166 {
167     { NULL }  /* Sentinel */
168 };
169
170 static PyMemberDef PyPosition_members[] =
171 {
172     { "origin", T_INT, offsetof( PyPosition, origin ), 0, "Position origin" },
173     { "key",    T_INT, offsetof( PyPosition, key ),    0, "Position key" },
174     { "value",  T_ULONG, offsetof( PyPosition, value ), 0, "Position value" },
175     { NULL }  /* Sentinel */
176 };
177
178 static PyTypeObject PyPosition_Type =
179 {
180     PyObject_HEAD_INIT( NULL )
181     0,                         /*ob_size*/
182     "vlc.Position",            /*tp_name*/
183     sizeof( PyPosition_Type ),   /*tp_basicsize*/
184     0,                         /*tp_itemsize*/
185     0,                         /*tp_dealloc*/
186     0,                         /*tp_print*/
187     0,                         /*tp_getattr*/
188     0,                         /*tp_setattr*/
189     0,                         /*tp_compare*/
190     0,                         /*tp_repr*/
191     0,                         /*tp_as_number*/
192     0,                         /*tp_as_sequence*/
193     0,                         /*tp_as_mapping*/
194     0,                         /*tp_hash */
195     0,                         /*tp_call*/
196     0,                         /*tp_str*/
197     0,                         /*tp_getattro*/
198     0,                         /*tp_setattro*/
199     0,                         /*tp_as_buffer*/
200     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
201     "Represent a Position with value, origin and key",  /* tp_doc */
202     0,                        /* tp_traverse */
203     0,                        /* tp_clear */
204     0,                         /* tp_richcompare */
205     0,                         /* tp_weaklistoffset */
206     0,                         /* tp_iter */
207     0,                          /* tp_iternext */
208     PyPosition_methods,             /* tp_methods */
209     PyPosition_members,             /* tp_members */
210     0,                         /* tp_getset */
211     0,                         /* tp_base */
212     0,                         /* tp_dict */
213     0,                         /* tp_descr_get */
214     0,                         /* tp_descr_set */
215     0,                         /* tp_dictoffset */
216     0,                         /* tp_init */
217     0,                         /* tp_alloc */
218     PyPosition_new,            /* tp_new */
219 };