fixed tag organisation, started video stuff
[swftools.git] / lib / python / taglist.c
1 #include <Python.h>
2 #undef HAVE_STAT
3 #include "../rfxswf.h"
4 #include "../log.h"
5 #include "./pyutils.h"
6 #include "primitives.h"
7 #include "action.h"
8 #include "tag.h"
9 #include "tagmap.h"
10 #include "taglist.h"
11
12 //----------------------------------------------------------------------------
13 typedef struct {
14     PyObject_HEAD
15     PyObject* taglist;
16 } TagListObject;
17 //----------------------------------------------------------------------------
18 static void taglist_showcontents(PyObject* self)
19 {
20     TagListObject*taglist = (TagListObject*)self;
21     int t, l = PyList_Size(taglist->taglist);
22     for(t=0;t<l;t++) {
23         PyObject*item = PyList_GetItem(taglist->taglist, t);
24         mylog(" %08x(%d) taglist_showcontents   item=%08x(%d)\n", (int)self, self->ob_refcnt, item, item->ob_refcnt);
25     }
26 }
27 //----------------------------------------------------------------------------
28 PyObject * taglist_new()
29 {
30     TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
31     mylog("+%08x(%d) taglist_new", (int)taglist, taglist->ob_refcnt);
32     taglist->taglist = PyList_New(0);
33     return (PyObject*)taglist;
34 }
35 //----------------------------------------------------------------------------
36 PyObject * taglist_new2(TAG*tag)
37 {
38     TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
39     mylog("+%08x(%d) taglist_new2 tag=%08x", (int)taglist, taglist->ob_refcnt, tag);
40     PyObject* tagmap = tagmap_new();
41
42     int nr=0, len=0;
43     TAG*t = tag;
44     TAG*last = t;
45     while(t) {len++;last=t;t=t->next;}
46
47     if(last && last->id==ST_END) {
48         swf_DeleteTag(last); last = 0;
49         len--;
50         if(len==0) tag = 0;
51     }
52
53     taglist->taglist = PyList_New(len);
54     
55     nr = 0;
56     t = tag;
57     while(t) {
58         PyObject*newtag = tag_new2(t, tagmap);
59         if(newtag==NULL) {
60             // pass through exception
61             Py_DECREF(tagmap);
62             return NULL;
63         }
64         PyList_SET_ITEM(taglist->taglist,nr,newtag);Py_INCREF(newtag);
65         if(swf_isDefiningTag(t)) {
66             int id = swf_GetDefineID(t);
67             tagmap_addMapping(tagmap, id, newtag);
68         }
69         nr++;
70         t=t->next;
71         Py_DECREF(newtag);
72     }
73     Py_DECREF(tagmap);
74     return (PyObject*)taglist;
75 }
76 //----------------------------------------------------------------------------
77 TAG* taglist_getTAGs(PyObject*self)
78 {
79     PyObject* tagmap = tagmap_new();
80     if(!PY_CHECK_TYPE(self,&TagListClass)) {
81         PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
82         return 0;
83     }
84     TagListObject*taglist = (TagListObject*)self;
85
86     /* TODO: the tags will be modified by this. We should set mutexes. */
87     
88     int l = PyList_Size(taglist->taglist);
89     int t;
90     TAG* tag = 0;
91     TAG* firstTag = 0;
92     mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
93     for(t=0;t<l;t++) {
94         PyObject*item = PyList_GetItem(taglist->taglist, t);
95         tag = tag_getTAG(item, tag, tagmap);
96         if(!tag) {
97             //pass through errors
98             Py_DECREF(tagmap);
99             return 0;
100         }
101         if(!firstTag)
102             firstTag = tag;
103     }
104     Py_DECREF(tagmap);
105     return firstTag;
106 }
107 //----------------------------------------------------------------------------
108 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
109 {
110 /*    SWF swf;
111     TagListObject*taglist = (TagListObject*)self;
112     if(!self || !PyArg_ParseTuple(args,"")) 
113         return NULL;
114     swf.firstTag = taglist->firstTag;
115     swf_FoldAll(&swf);
116     taglist->firstTag = swf.firstTag;
117     taglist->lastTag = 0; // FIXME
118     taglist->searchTag = 0;*/
119     return PY_NONE;
120 }
121 //----------------------------------------------------------------------------
122 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
123 {
124     SWF swf;
125 /*    TagListObject*taglist = (TagListObject*)self;
126     if(!self || !PyArg_ParseTuple(args,"")) 
127         return NULL;
128     swf.firstTag = taglist->firstTag;
129     swf_UnFoldAll(&swf);
130     taglist->firstTag = swf.firstTag;
131     taglist->lastTag = 0; // FIXME
132     taglist->searchTag = 0;*/
133     return PY_NONE;
134 }
135 //----------------------------------------------------------------------------
136 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
137 {
138     SWF swf;
139 /*    TagListObject*taglist = (TagListObject*)self;
140     if(!self || !PyArg_ParseTuple(args,"")) 
141         return NULL;
142     swf.firstTag = taglist->firstTag;
143     swf_UnFoldAll(&swf);
144     taglist->firstTag = swf.firstTag;
145     taglist->lastTag = 0; // FIXME
146     taglist->searchTag = 0;*/
147     return PY_NONE;
148 }
149 //----------------------------------------------------------------------------
150 static PyMethodDef taglist_functions[] =
151 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
152  {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
153  {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
154  {NULL, NULL, 0, NULL}
155 };
156
157 static PyObject* taglist_getattr(PyObject * self, char* a)
158 {
159     PyObject* ret = Py_FindMethod(taglist_functions, self, a);
160     mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
161     return ret;
162 }
163 //----------------------------------------------------------------------------
164 static int taglist_length(PyObject * self)
165 {
166     TagListObject*tags = (TagListObject*)self;
167     mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
168     return PyList_GET_SIZE(tags->taglist);
169 }
170 //----------------------------------------------------------------------------
171 static int taglist_contains(PyObject * self, PyObject * tag)
172 {
173     /* TODO: optimize! */
174     TagListObject*taglist = (TagListObject*)self;
175     PyObject*list = taglist->taglist;
176     int l = PyList_Size(list);
177     int t;
178     for(t=0;t<l;t++) {
179         PyObject*item = PyList_GetItem(list, t);
180         if(item == tag) {
181             return 1;
182         }
183     }
184     return 0;
185 }
186 //----------------------------------------------------------------------------
187 static PyObject * taglist_concat(PyObject * self, PyObject* list)
188 {
189     PyObject*tag = 0;
190     PY_ASSERT_TYPE(self, &TagListClass);
191     TagListObject*taglist = (TagListObject*)self;
192     mylog(" %08x(%d) taglist_concat %08x(%d)", (int)self, self->ob_refcnt, list, list->ob_refcnt);
193
194     if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
195         list = tag_getDependencies(tag);
196         int l = PyList_Size(list);
197         int t;
198         for(t=0;t<l;t++) {
199             PyObject*item = PyList_GetItem(list, t);
200             PyObject*_self = taglist_concat(self, item);
201             Py_DECREF(self);
202             self = _self;
203         }
204         if(!taglist_contains(self, tag)) {
205             mylog(" %08x(%d) taglist_concat: Adding Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
206             PyList_Append(taglist->taglist, tag);
207         } else {
208             mylog(" %08x(%d) taglist_concat: Already contains Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
209         }
210         Py_INCREF(self);
211         return self;
212         /* copy tag, so we don't have to do INCREF(tag) (and don't
213            get problems if the tag is appended to more than one
214            taglist) */
215         /* TODO: handle IDs */
216         /*      
217         TAG*t = tag_getTAG(tag);
218         TAG*nt = 0;
219         mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
220         // copy tag
221         nt = swf_InsertTag(0, t->id);
222         swf_SetBlock(nt,t->data,t->len);
223         PyObject*newtag = tag_new(taglist->swf, nt);
224         if(swf_isDefiningTag(t)) {
225             int id = swf_GetDefineID(t);
226             PyObject*id = PyLong_FromLong(id);
227             PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
228             Py_INCREF(id);
229             PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
230             Py_INCREF(id);
231         }
232         Py_INCREF(self);
233         return self;*/
234     }
235     PyErr_Clear();
236     if (PyList_Check(list)) {
237         int l = PyList_Size(list);
238         int t;
239         for(t=0;t<l;t++) {
240             PyObject*item = PyList_GetItem(list, t);
241             if(!PY_CHECK_TYPE(item, &TagClass)) {
242                 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
243                 return 0;
244             }
245             PyObject*_self = taglist_concat(self, item);
246             Py_DECREF(self);
247             self = _self;
248             if(!self)
249                 return 0;
250         }
251         Py_INCREF(self);
252         return self;
253     }
254     PyErr_Clear();
255     if (PY_CHECK_TYPE(list, &TagListClass)) {
256         TagListObject*taglist2 = (TagListObject*)list;
257         return taglist_concat(self, taglist2->taglist);
258
259         /*TAG* tags = taglist_getTAGs(self);
260         TAG* tags2 = taglist_getTAGs(list);
261         TAG* tags3;
262         tags3 = swf_Concatenate(tags,tags2);
263         PyObject* newtaglist = taglist_new(tags3);
264         swf_FreeTags(tags3);
265         Py_INCREF(newtaglist);*/
266     }
267     PyErr_Clear();
268
269     PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
270     return 0;
271 }
272 //----------------------------------------------------------------------------
273 static PyObject * taglist_item(PyObject * self, int index)
274 {
275     TagListObject*taglist = (TagListObject*)self;
276     PyObject*tag;
277     mylog(" %08x(%d) taglist_item(%d)", (int)self, self->ob_refcnt, index);
278     tag = PyList_GetItem(taglist->taglist, index);
279     Py_INCREF(tag);
280     return tag;
281 }
282 //----------------------------------------------------------------------------
283 static void taglist_dealloc(PyObject* self)
284 {
285     TagListObject*taglist = (TagListObject*)self;
286     mylog("-%08x(%d) taglist_dealloc list=%08x(%d)\n", (int)self, self->ob_refcnt, taglist->taglist, taglist->taglist->ob_refcnt);
287     Py_DECREF(taglist->taglist);
288     taglist->taglist = 0;
289     PyObject_Del(self);
290 }
291 //----------------------------------------------------------------------------
292 static PySequenceMethods taglist_as_sequence =
293 {
294     sq_length: taglist_length, // len(obj)
295     sq_concat: taglist_concat, // obj += [...], obj1+obj2
296     sq_repeat: 0,            // x*n, intargfunc
297     sq_item: taglist_item,  // obj[3]
298     sq_slice: 0,             // x[i:j] intintargfunc
299     sq_ass_item: 0,          // x[i] = y intobjargproc
300     sq_ass_slice: 0,         // x[i:j] = v intintobjargproc
301     sq_contains: taglist_contains,   //???
302 };
303 PyTypeObject TagListClass = 
304 {
305     PyObject_HEAD_INIT(NULL)
306     0,
307     tp_name: "TagList",
308     tp_basicsize: sizeof(TagListObject),
309     tp_itemsize: 0,
310     tp_dealloc: taglist_dealloc,
311     tp_print: 0,                 // print x
312     tp_getattr: taglist_getattr, // x.attr
313     tp_setattr: 0,               // x.attr = v
314     tp_compare: 0,               // x>y
315     tp_repr: 0,                  // `x`, print x
316     tp_as_number: 0,
317     tp_as_sequence: &taglist_as_sequence,
318 };