2
* Pure Data Packet module. Xvideo image packet output
3
* Copyright (c) by Tom Schouten <pdp@zzz.kotnet.org>
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.
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.
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.
25
// some x window glue code
26
#include "pdp_xwindow.h"
27
#include "pdp_xvideo.h"
30
#define PDP_XV_AUTOCREATE_RETRY 10
33
typedef struct pdp_xv_struct
37
t_pdp_xdisplay *x_xdpy;
38
t_pdp_xwindow *x_xwin;
56
static void pdp_xv_cursor(t_pdp_xv *x, t_floatarg f)
58
if (x->x_xwin) pdp_xwindow_cursor(x->x_xwin, f);
61
/* delete all submodules */
62
static void _pdp_xv_cleanup(t_pdp_xv *x)
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);
73
/* wait for thread to finish */
74
static void _pdp_xv_waitforthread(t_pdp_xv *x)
76
t_pdp_procqueue *q = pdp_queue_get_queue();
77
pdp_procqueue_finish(q, x->x_queue_id); // wait for thread to finish
81
// this destroys the window and all the x connections
82
static void pdp_xv_destroy(t_pdp_xv* x)
84
if (x->x_initialized){
86
_pdp_xv_waitforthread(x); // wait for thread
87
_pdp_xv_cleanup(x); // delete all objects
89
pdp_packet_mark_unused(x->x_packet0); // delete packet
97
/* this creates a window (opens a dpy connection, creates xvideo and xwinow objects) */
98
static void pdp_xv_create(t_pdp_xv* x)
101
if(x->x_initialized) return;
105
if (!(x->x_xdpy = pdp_xdisplay_new(x->x_display->s_name))) goto exit;
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;
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;
116
x->x_initialized = 1;
121
post("pdp_xv: cant open display %s\n",x->x_display->s_name);
126
static int pdp_xv_try_autocreate(t_pdp_xv *x)
129
if (x->x_autocreate){
130
post("pdp_xv: autocreate window");
132
if (!(x->x_initialized)){
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");
146
static void pdp_xv_bang(t_pdp_xv *x);
148
static void pdp_xv_bang_thread(t_pdp_xv *x)
150
pdp_xvideo_display_packet(x->x_xvid, x->x_xwin, x->x_packet0);
154
static void pdp_xv_bang_callback(t_pdp_xv *x)
156
/* receive events + send to outputs */
157
t_pdp_list *eventlist = pdp_xwindow_get_eventlist(x->x_xwin);
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);
166
pdp_tree_free(eventlist);
168
/* release the packet if there is one */
169
pdp_packet_mark_unused(x->x_packet0);
174
/* manually poll for events */
175
static void pdp_xv_poll(t_pdp_xv *x)
177
if (x->x_initialized)
178
pdp_xv_bang_callback(x);
181
static void pdp_xv_bang(t_pdp_xv *x)
183
t_pdp_procqueue *q = pdp_queue_get_queue();
185
/* check if window is initialized */
186
if (!(x->x_initialized)){
187
if (!pdp_xv_try_autocreate(x)) return;
190
/* check if we can proceed */
191
if (-1 != x->x_queue_id) return;
192
if (-1 == x->x_packet0) return;
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);
202
static void pdp_xv_input_0(t_pdp_xv *x, t_symbol *s, t_floatarg f)
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);
212
static void pdp_xv_autocreate(t_pdp_xv *x, t_floatarg f)
214
if (f != 0.0f) x->x_autocreate = PDP_XV_AUTOCREATE_RETRY;
215
else x->x_autocreate = 0;
218
static void pdp_xv_display(t_pdp_xv *x, t_symbol *s)
220
_pdp_xv_waitforthread(x);
223
/* only create if already active */
224
if (x->x_initialized){
230
static void pdp_xv_movecursor(t_pdp_xv *x, float cx, float cy)
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);
239
static void pdp_xv_fullscreen(t_pdp_xv *x)
241
if (x->x_initialized)
242
pdp_xwindow_fullscreen(x->x_xwin);
245
static void pdp_xv_resize(t_pdp_xv* x, t_floatarg width, t_floatarg height)
247
if (x->x_initialized)
248
pdp_xwindow_resize(x->x_xwin, width, height);
251
static void pdp_xv_move(t_pdp_xv* x, t_floatarg width, t_floatarg height)
253
if (x->x_initialized)
254
pdp_xwindow_move(x->x_xwin, width, height);
257
static void pdp_xv_moveresize(t_pdp_xv* x, t_floatarg xoff, t_floatarg yoff, t_floatarg width, t_floatarg height)
259
if (x->x_initialized)
260
pdp_xwindow_moveresize(x->x_xwin, xoff, yoff, width, height);
263
static void pdp_xv_tile(t_pdp_xv* x, t_floatarg xtiles, t_floatarg ytiles, t_floatarg i, t_floatarg j)
265
if (x->x_initialized)
266
pdp_xwindow_tile(x->x_xwin, xtiles, ytiles, i, j);
269
static void pdp_xv_vga(t_pdp_xv *x)
271
pdp_xv_resize(x, 640, 480);
274
static void pdp_xv_free(t_pdp_xv *x)
279
t_class *pdp_xv_class;
283
void *pdp_xv_new(void)
285
t_pdp_xv *x = (t_pdp_xv *)pd_new(pdp_xv_class);
286
x->x_outlet = outlet_new(&x->x_obj, &s_anything);
292
x->x_display = gensym(":0");
294
pdp_xv_autocreate(x,1);
309
void pdp_xv_setup(void)
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);
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);
334
/* some shortcuts for the lazy */
335
class_addmethod(pdp_xv_class, (t_method)pdp_xv_vga, gensym("vga"), A_NULL);