~kamalmostafa/ubuntu/lucid/pdp/fix-504941-ftbfs

« back to all changes in this revision

Viewing changes to modules/image_io/pdp_xv.c

  • Committer: Bazaar Package Importer
  • Author(s): Guenter Geiger (Debian/GNU)
  • Date: 2005-03-15 22:21:05 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050315222105-1q287rsihmd9j1tb
Tags: 1:0.12.4-2
* fixed the hardcoded depends
* added 3dp library

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *   Pure Data Packet module. Xvideo image packet output
3
 
 *   Copyright (c) by Tom Schouten <pdp@zzz.kotnet.org>
4
 
 *
5
 
 *   This program is free software; you can redistribute it and/or modify
6
 
 *   it under the terms of the GNU General Public License as published by
7
 
 *   the Free Software Foundation; either version 2 of the License, or
8
 
 *   (at your option) any later version.
9
 
 *
10
 
 *   This program is distributed in the hope that it will be useful,
11
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *   GNU General Public License for more details.
14
 
 *
15
 
 *   You should have received a copy of the GNU General Public License
16
 
 *   along with this program; if not, write to the Free Software
17
 
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 
 *
19
 
 */
20
 
 
21
 
// pdp stuff
22
 
#include "pdp.h"
23
 
#include "pdp_base.h"
24
 
 
25
 
// some x window glue code
26
 
#include "pdp_xwindow.h"
27
 
#include "pdp_xvideo.h"
28
 
 
29
 
 
30
 
#define PDP_XV_AUTOCREATE_RETRY 10
31
 
 
32
 
 
33
 
typedef struct pdp_xv_struct
34
 
{
35
 
    t_object x_obj;
36
 
 
37
 
    t_pdp_xdisplay *x_xdpy;
38
 
    t_pdp_xwindow *x_xwin;
39
 
    t_pdp_xvideo *x_xvid;
40
 
 
41
 
    t_outlet *x_outlet;
42
 
 
43
 
    int x_packet0;
44
 
    int x_queue_id;
45
 
    t_symbol *x_display;
46
 
 
47
 
    //Display *x_dpy;
48
 
 
49
 
    int  x_initialized;
50
 
    int  x_autocreate;
51
 
 
52
 
 
53
 
} t_pdp_xv;
54
 
 
55
 
 
56
 
static void pdp_xv_cursor(t_pdp_xv *x, t_floatarg f)
57
 
{
58
 
    if (x->x_xwin) pdp_xwindow_cursor(x->x_xwin, f);
59
 
}
60
 
 
61
 
/* delete all submodules */
62
 
static void _pdp_xv_cleanup(t_pdp_xv *x)
63
 
{
64
 
    if (x->x_xwin) pdp_xwindow_free(x->x_xwin);
65
 
    if (x->x_xvid) pdp_xvideo_free(x->x_xvid);
66
 
    if (x->x_xdpy) pdp_xdisplay_free(x->x_xdpy);
67
 
    x->x_xwin = 0;
68
 
    x->x_xvid = 0;
69
 
    x->x_xdpy = 0;
70
 
    x->x_initialized = 0;
71
 
}
72
 
 
73
 
/* wait for thread to finish */
74
 
static void _pdp_xv_waitforthread(t_pdp_xv *x)
75
 
{
76
 
    t_pdp_procqueue *q = pdp_queue_get_queue();
77
 
    pdp_procqueue_finish(q, x->x_queue_id);   // wait for thread to finish
78
 
    x->x_queue_id = -1;
79
 
}
80
 
 
81
 
// this destroys the window and all the x connections
82
 
static void pdp_xv_destroy(t_pdp_xv* x)
83
 
{
84
 
    if (x->x_initialized){
85
 
        
86
 
        _pdp_xv_waitforthread(x);  // wait for thread
87
 
        _pdp_xv_cleanup(x);        // delete all objects
88
 
 
89
 
        pdp_packet_mark_unused(x->x_packet0); // delete packet
90
 
        x->x_packet0 = -1;
91
 
        x->x_initialized = 0;
92
 
 
93
 
    }
94
 
}
95
 
 
96
 
 
97
 
/* this creates a window (opens a dpy connection, creates xvideo and xwinow objects) */
98
 
static void pdp_xv_create(t_pdp_xv* x)
99
 
{
100
 
    int i;
101
 
    if(x->x_initialized) return;
102
 
 
103
 
    
104
 
    /* open a display */
105
 
    if (!(x->x_xdpy = pdp_xdisplay_new(x->x_display->s_name))) goto exit;
106
 
 
107
 
    /* open an xv port on the display */
108
 
    x->x_xvid = pdp_xvideo_new();
109
 
    if (!pdp_xvideo_open_on_display(x->x_xvid, x->x_xdpy)) goto exit;
110
 
 
111
 
    /* create a window on the display */
112
 
    x->x_xwin = pdp_xwindow_new();
113
 
    if (!pdp_xwindow_create_on_display(x->x_xwin, x->x_xdpy)) goto exit;
114
 
 
115
 
    /* done */
116
 
    x->x_initialized = 1;
117
 
    return;
118
 
 
119
 
    /* cleanup exits */
120
 
 exit:
121
 
    post("pdp_xv: cant open display %s\n",x->x_display->s_name);
122
 
    _pdp_xv_cleanup(x);
123
 
 
124
 
}
125
 
 
126
 
static int pdp_xv_try_autocreate(t_pdp_xv *x)
127
 
{
128
 
 
129
 
    if (x->x_autocreate){
130
 
        post("pdp_xv: autocreate window");
131
 
        pdp_xv_create(x);
132
 
        if (!(x->x_initialized)){
133
 
            x->x_autocreate--;
134
 
            if (!x->x_autocreate){
135
 
                post ("pdp_xv: autocreate failed %d times: disabled", PDP_XV_AUTOCREATE_RETRY);
136
 
                post ("pdp_xv: send [autocreate 1] message to re-enable");
137
 
                return 0;
138
 
            }
139
 
        }
140
 
        else return 1;
141
 
 
142
 
    }
143
 
    return 0;
144
 
}
145
 
 
146
 
static void pdp_xv_bang(t_pdp_xv *x);
147
 
 
148
 
static void pdp_xv_bang_thread(t_pdp_xv *x)
149
 
{
150
 
    pdp_xvideo_display_packet(x->x_xvid, x->x_xwin, x->x_packet0);
151
 
}
152
 
 
153
 
 
154
 
static void pdp_xv_bang_callback(t_pdp_xv *x)
155
 
{
156
 
    /* receive events + send to outputs */
157
 
    t_pdp_list *eventlist = pdp_xwindow_get_eventlist(x->x_xwin);
158
 
    t_pdp_atom *a;
159
 
 
160
 
    for (a=eventlist->first; a; a=a->next){
161
 
        //pdp_list_print(a->w.w_list);
162
 
        outlet_pdp_list(x->x_outlet, a->w.w_list);
163
 
    }
164
 
 
165
 
    /* free list */
166
 
    pdp_tree_free(eventlist);
167
 
 
168
 
    /* release the packet if there is one */
169
 
    pdp_packet_mark_unused(x->x_packet0);
170
 
    x->x_packet0 = -1;
171
 
 
172
 
}
173
 
 
174
 
/* manually poll for events */
175
 
static void pdp_xv_poll(t_pdp_xv *x)
176
 
{
177
 
    if (x->x_initialized)
178
 
        pdp_xv_bang_callback(x);
179
 
}
180
 
 
181
 
static void pdp_xv_bang(t_pdp_xv *x)
182
 
{
183
 
    t_pdp_procqueue *q = pdp_queue_get_queue();
184
 
 
185
 
    /* check if window is initialized */
186
 
    if (!(x->x_initialized)){
187
 
        if (!pdp_xv_try_autocreate(x)) return;
188
 
    }
189
 
 
190
 
    /* check if we can proceed */
191
 
    if (-1 != x->x_queue_id) return;
192
 
    if (-1 == x->x_packet0) return;
193
 
 
194
 
    /* if previous queued method returned
195
 
       schedule a new one, else ignore */
196
 
    if (-1 == x->x_queue_id) {
197
 
        pdp_procqueue_add(q, x, pdp_xv_bang_thread, pdp_xv_bang_callback, &x->x_queue_id);
198
 
    }
199
 
    
200
 
}
201
 
 
202
 
static void pdp_xv_input_0(t_pdp_xv *x, t_symbol *s, t_floatarg f)
203
 
{
204
 
 
205
 
    if (s == gensym("register_ro")) pdp_packet_convert_ro_or_drop(&x->x_packet0, (int)f, pdp_gensym("bitmap/yv12/*"));
206
 
    if (s == gensym("process")) pdp_xv_bang(x);
207
 
 
208
 
}
209
 
 
210
 
 
211
 
 
212
 
static void pdp_xv_autocreate(t_pdp_xv *x, t_floatarg f)
213
 
{
214
 
  if (f != 0.0f) x->x_autocreate = PDP_XV_AUTOCREATE_RETRY;
215
 
  else x->x_autocreate = 0;
216
 
}
217
 
 
218
 
static void pdp_xv_display(t_pdp_xv *x, t_symbol *s)
219
 
{
220
 
    _pdp_xv_waitforthread(x);
221
 
    x->x_display = s;
222
 
 
223
 
    /* only create if already active */
224
 
    if (x->x_initialized){
225
 
        pdp_xv_destroy(x);
226
 
        pdp_xv_create(x);
227
 
    }
228
 
}
229
 
 
230
 
static void pdp_xv_movecursor(t_pdp_xv *x, float cx, float cy)
231
 
{
232
 
    if (x->x_initialized){
233
 
        cx *= x->x_xwin->winwidth;
234
 
        cy *= x->x_xwin->winheight;
235
 
        pdp_xwindow_warppointer(x->x_xwin, cx, cy);
236
 
    }
237
 
}
238
 
 
239
 
static void pdp_xv_fullscreen(t_pdp_xv *x)
240
 
{
241
 
    if (x->x_initialized)
242
 
        pdp_xwindow_fullscreen(x->x_xwin);
243
 
}
244
 
 
245
 
static void pdp_xv_resize(t_pdp_xv* x, t_floatarg width, t_floatarg height)
246
 
{
247
 
    if (x->x_initialized)
248
 
        pdp_xwindow_resize(x->x_xwin, width, height);
249
 
}
250
 
 
251
 
static void pdp_xv_move(t_pdp_xv* x, t_floatarg width, t_floatarg height)
252
 
{
253
 
    if (x->x_initialized)
254
 
        pdp_xwindow_move(x->x_xwin, width, height);
255
 
}
256
 
 
257
 
static void pdp_xv_moveresize(t_pdp_xv* x, t_floatarg xoff, t_floatarg yoff, t_floatarg width, t_floatarg height)
258
 
{
259
 
    if (x->x_initialized)
260
 
        pdp_xwindow_moveresize(x->x_xwin, xoff, yoff, width, height);
261
 
}
262
 
 
263
 
static void pdp_xv_tile(t_pdp_xv* x, t_floatarg xtiles, t_floatarg ytiles, t_floatarg i, t_floatarg j)
264
 
{
265
 
    if (x->x_initialized)
266
 
        pdp_xwindow_tile(x->x_xwin, xtiles, ytiles, i, j);
267
 
}
268
 
 
269
 
static void pdp_xv_vga(t_pdp_xv *x)
270
 
{
271
 
    pdp_xv_resize(x, 640, 480);
272
 
}
273
 
 
274
 
static void pdp_xv_free(t_pdp_xv *x)
275
 
{
276
 
    pdp_xv_destroy(x);
277
 
}
278
 
 
279
 
t_class *pdp_xv_class;
280
 
 
281
 
 
282
 
 
283
 
void *pdp_xv_new(void)
284
 
{
285
 
    t_pdp_xv *x = (t_pdp_xv *)pd_new(pdp_xv_class);
286
 
    x->x_outlet = outlet_new(&x->x_obj, &s_anything);
287
 
    x->x_xwin = 0;
288
 
    x->x_xvid = 0;
289
 
    x->x_xdpy = 0;
290
 
    x->x_packet0 = -1;
291
 
    x->x_queue_id = -1;
292
 
    x->x_display = gensym(":0");
293
 
    x->x_xdpy = 0;
294
 
    pdp_xv_autocreate(x,1);
295
 
 
296
 
    return (void *)x;
297
 
}
298
 
 
299
 
 
300
 
 
301
 
 
302
 
 
303
 
#ifdef __cplusplus
304
 
extern "C"
305
 
{
306
 
#endif
307
 
 
308
 
 
309
 
void pdp_xv_setup(void)
310
 
{
311
 
    pdp_xv_class = class_new(gensym("pdp_xv"), (t_newmethod)pdp_xv_new,
312
 
                             (t_method)pdp_xv_free, sizeof(t_pdp_xv), 0, A_NULL);
313
 
 
314
 
 
315
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_bang, gensym("bang"), A_NULL);
316
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_create, gensym("open"), A_NULL);
317
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_create, gensym("create"), A_NULL);
318
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_autocreate, gensym("autocreate"), A_FLOAT, A_NULL);
319
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_destroy, gensym("destroy"), A_NULL);
320
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_destroy, gensym("close"), A_NULL);
321
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_resize, gensym("dim"), A_FLOAT, A_FLOAT, A_NULL);
322
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_move, gensym("move"), A_FLOAT, A_FLOAT, A_NULL);
323
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_move, gensym("pos"), A_FLOAT, A_FLOAT, A_NULL);
324
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_resize, gensym("size"), A_FLOAT, A_FLOAT, A_NULL);
325
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_display, gensym("display"), A_SYMBOL, A_NULL);
326
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_input_0, gensym("pdp"),  A_SYMBOL, A_DEFFLOAT, A_NULL);
327
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_cursor, gensym("cursor"), A_FLOAT, A_NULL);
328
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_movecursor, gensym("movecursor"), A_FLOAT, A_FLOAT, A_NULL);
329
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_fullscreen, gensym("fullscreen"), A_NULL);
330
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_poll, gensym("poll"), A_NULL);
331
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_moveresize, gensym("posdim"), A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_NULL);
332
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_tile, gensym("tile"), A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_NULL);
333
 
 
334
 
    /* some shortcuts for the lazy */
335
 
    class_addmethod(pdp_xv_class, (t_method)pdp_xv_vga, gensym("vga"), A_NULL);
336
 
 
337
 
}
338
 
 
339
 
#ifdef __cplusplus
340
 
}
341
 
#endif
342
 
 
343