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