~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to gt/xt.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * xt.c
3
 
 * gnutime/gnuavi test program - Xt frontend
4
 
 *
5
 
 * Copyright (C) 1998 Rasca, Berlin
6
 
 * EMail: thron@gmx.de
7
 
 *
8
 
 * This program is free software; you can redistribute it and/or modify
9
 
 * it under the terms of the GNU General Public License as published by
10
 
 * the Free Software Foundation; either version 2 of the License, or
11
 
 * (at your option) any later version.
12
 
 *
13
 
 * This program is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
 * GNU General Public License for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU General Public License
19
 
 * along with this program; if not, write to the Free Software
20
 
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
 
 */
22
 
 
23
 
#include <stdio.h>
24
 
#include <sys/time.h>
25
 
#include <X11/Intrinsic.h>
26
 
#include <X11/Shell.h>
27
 
#include <X11/StringDefs.h>
28
 
#ifdef HasSharedMemory
29
 
#include <sys/ipc.h>
30
 
#include <sys/shm.h>
31
 
#include <X11/extensions/XShm.h>
32
 
#endif
33
 
#include "video.h"
34
 
#include "sound.h"
35
 
#include "xt.h"
36
 
 
37
 
static void x_fini(void);
38
 
 
39
 
static XtAppContext gAppCont;
40
 
static Widget gTop, gVid;
41
 
static String fallback[] = {
42
 
        "*background: blue",
43
 
        "*translations: #override \\n\\"
44
 
        " <Key>Q: quit() \\n\\"
45
 
        " <Key>P: play() \\n\\"
46
 
        " <Key>S: stop() \\n",
47
 
        NULL
48
 
};
49
 
 
50
 
typedef struct {
51
 
        int loop;               /* flag if looping is on */
52
 
        int depth;              /* display bits per pixel */
53
 
        video *vid;             /* */
54
 
        long time_ms;   /* */
55
 
        unsigned char *image;
56
 
        int use_snd;
57
 
        int use_shm;
58
 
        Widget widget;
59
 
        XImage *ximage;
60
 
        GC gc;
61
 
} app;
62
 
 
63
 
/*
64
 
 * called if "q" was pressed
65
 
 */
66
 
static void
67
 
action_quit (Widget w, XEvent *ev, String *parms, Cardinal *num_parms)
68
 
{
69
 
        x_fini();
70
 
}
71
 
 
72
 
/*
73
 
 */
74
 
static void
75
 
action_stop (Widget w, XEvent *ev, String *parms, Cardinal *num_parms)
76
 
{
77
 
        printf ("stop() not done ..\n");
78
 
}
79
 
 
80
 
/*
81
 
 */
82
 
static void
83
 
action_play (Widget w, XEvent *ev, String *parms, Cardinal *num_parms)
84
 
{
85
 
        printf ("play() not done..\n");
86
 
}
87
 
 
88
 
static XtActionsRec action_tab[] = {
89
 
        {"stop",        action_stop},
90
 
        {"play",        action_play},
91
 
        {"quit",        action_quit},
92
 
        };
93
 
 
94
 
/*
95
 
 * exit event handler
96
 
 */
97
 
static void
98
 
evh_exit (Widget w, XtPointer xtp, XEvent *ev, Boolean *boo)
99
 
{
100
 
        if ((ev->type == ClientMessage) &&                      
101
 
                (ev->xclient.format == 32) &&
102
 
                (ev->xclient.data.l[0] == *((Atom*)xtp)))
103
 
        x_fini();
104
 
}
105
 
 
106
 
void
107
 
x_play_sound (XtPointer xtp, XtIntervalId *id)
108
 
{
109
 
}
110
 
 
111
 
/*
112
 
 * returns time in mili secs
113
 
 */
114
 
long
115
 
time_ms (void)
116
 
{
117
 
        struct timeval curr_time;
118
 
        gettimeofday (&curr_time, NULL);
119
 
        return ((curr_time.tv_sec * 1000L + curr_time.tv_usec / 1000L)&0x7FFFFF);
120
 
}
121
 
 
122
 
/*
123
 
 * time callback procedure to display a frame
124
 
 */
125
 
void
126
 
x_display_frame (XtPointer xtp, XtIntervalId *id)
127
 
{
128
 
        app *ap = (app *)xtp;
129
 
        video *vid = ap->vid;
130
 
        XImage *ximage = ap->ximage;
131
 
        char *buf = ap->image;
132
 
        unsigned char *snd_buf;
133
 
        long time;
134
 
        int todo = 1, snd_size =0;
135
 
 
136
 
        time = ap->time_ms;
137
 
        ap->time_ms = time_ms();
138
 
        /* printf (" -t %ld %ld\n", ap->time_ms, time); */
139
 
 
140
 
        while (todo) {
141
 
                switch (vid_next_in(vid)) {
142
 
                        case VNI_VIDEO:
143
 
                                if (!vid_get_frame (vid, buf)) {
144
 
                                        if (ap->loop != 1)
145
 
                                                return;
146
 
                                        vid_reset (vid);
147
 
                                        XtAppAddTimeOut (gAppCont, vid->tpf, x_display_frame, ap);
148
 
                                        return;
149
 
                                }
150
 
                                /* printf (" -f\n"); */
151
 
                                todo = 0;
152
 
                                break;
153
 
                        case VNI_SOUND:
154
 
                                if (ap->use_snd) {
155
 
                                        if (vid_get_sound (vid, &snd_buf, &snd_size)) {
156
 
                                                snd_play_data (snd_buf, snd_size, 0);
157
 
                                                /* printf (" -s snd_size=%d\n", snd_size); */
158
 
                                        } else {
159
 
                                                vid_skip_obj(vid);
160
 
                                                todo = 0;
161
 
                                        }
162
 
                                } else
163
 
                                        vid_skip_obj (vid);
164
 
                                break;
165
 
                        default:
166
 
                                if (ap->loop) {
167
 
                                        vid_reset (vid);
168
 
                                        XtAppAddTimeOut (gAppCont, vid->tpf, x_display_frame, ap);
169
 
                                }
170
 
                                return;
171
 
                                break;
172
 
                }
173
 
        }
174
 
#ifdef HasSharedMemory
175
 
        if (ap->use_shm)
176
 
                XShmPutImage (XtDisplay(gTop), XtWindow(ap->widget), ap->gc, ximage,
177
 
                                0, 0, 0, 0, vid->width, vid->height, True);
178
 
        else
179
 
#endif
180
 
                XPutImage (XtDisplay(gTop), XtWindow(ap->widget), ap->gc, ximage,
181
 
                                0, 0, 0, 0, vid->width, vid->height);
182
 
 
183
 
        time = vid->tpf - (time_ms() - time) ;
184
 
        /* printf (" -t %ld\n", time); */
185
 
        if (time < 0) {
186
 
                if (vid->tpf + time < 0)
187
 
                        vid_skip_frame (vid);
188
 
                time = 0;
189
 
        }
190
 
#ifdef DEBUG
191
 
        printf ("x_display_frame() time=%ld tpf=%d\n", time, vid->tpf);
192
 
#endif
193
 
        XtAppAddTimeOut (gAppCont, time, x_display_frame, ap);
194
 
}
195
 
 
196
 
/*
197
 
 */
198
 
void
199
 
x_init (int *argc, char **argv)
200
 
{
201
 
        gTop = XtVaAppInitialize (&gAppCont, "XMplay", NULL, 0,
202
 
                                argc, argv, fallback, XtNinput, True, NULL);
203
 
        XtAppAddActions (gAppCont, 
204
 
                                action_tab, sizeof(action_tab)/sizeof(XtActionsRec));
205
 
}
206
 
 
207
 
/*
208
 
 */
209
 
void
210
 
x_main (int width, int height)
211
 
{
212
 
        static Atom wm_del;
213
 
 
214
 
        gVid = XtVaCreateManagedWidget ("xvid", coreWidgetClass, gTop,
215
 
                                XtNwidth, width, XtNheight, height, NULL);
216
 
        XtRealizeWidget (gTop);
217
 
        wm_del = XInternAtom (XtDisplay(gTop), "WM_DELETE_WINDOW", False);
218
 
        XSetWMProtocols (XtDisplay(gTop), XtWindow(gTop), &wm_del, 1);
219
 
        XtAddEventHandler (gTop, NoEventMask, True, evh_exit, &wm_del);
220
 
}
221
 
 
222
 
/*
223
 
 */
224
 
static app ap;
225
 
static XShmSegmentInfo shminfo;
226
 
 
227
 
/*
228
 
 */
229
 
void
230
 
x_loop (video *vid, int use_shm, int loop, int use_snd)
231
 
{
232
 
        int pad, codec;
233
 
        Screen *screen = XtScreen (gTop);
234
 
        Display *dpy = XtDisplay(gTop);
235
 
        Visual *vis = DefaultVisualOfScreen (screen);
236
 
#ifdef HasSharedMemory
237
 
 
238
 
        if (use_shm) {
239
 
                use_shm = XShmQueryExtension(dpy);
240
 
        }
241
 
#else
242
 
        use_shm = 0;
243
 
#endif
244
 
 
245
 
        ap.loop = loop;
246
 
        ap.vid = vid;
247
 
        ap.depth = DefaultDepthOfScreen (screen);
248
 
        ap.widget = gVid;
249
 
        ap.gc = XCreateGC (dpy, XtWindow(ap.widget), 0, NULL);
250
 
#ifdef HasSharedMemory
251
 
        if (use_shm) {
252
 
                ap.ximage =XShmCreateImage (dpy, vis, ap.depth, ZPixmap, NULL,
253
 
                                        &shminfo, vid->width, vid->height);
254
 
        } else
255
 
#endif
256
 
                        {
257
 
                ap.image = XtMalloc (vid->width * vid->height * 4 /* ap.bpp / 8*/);
258
 
                ap.ximage =XCreateImage (dpy, vis,
259
 
                                                ap.depth, ZPixmap, 0, ap.image, vid->width, vid->height,
260
 
                                                8, 0);
261
 
        }
262
 
 
263
 
        if (!ap.ximage) {
264
 
                fprintf (stderr, "Can't create XImage!\n");
265
 
                exit (2);
266
 
        }
267
 
        pad = ap.ximage->bytes_per_line -
268
 
                                ap.ximage->width * ap.ximage->bits_per_pixel / 8 ;
269
 
#ifdef DEBUG
270
 
        printf (" ximage bytes /p line = %d\n", ap.ximage->bytes_per_line);
271
 
        printf ("                  bpp = %d\n", ap.ximage->bits_per_pixel);
272
 
        printf ("                depth = %d\n", ap.ximage->depth);
273
 
        printf ("                  pad = %d\n", pad);
274
 
#endif
275
 
        switch (ap.ximage->depth) {
276
 
                case 15:
277
 
                        codec = VV_COD_RGB555;
278
 
                        break;
279
 
                case 16:
280
 
                        codec = VV_COD_RGB565;
281
 
                        break;
282
 
                case 24:
283
 
                        codec = VV_COD_RGB888;
284
 
                        break;
285
 
                default:
286
 
                        printf ("visual not supported\n");
287
 
                        exit (2);
288
 
        }
289
 
        vid_set_video_parms (vid, codec, pad);
290
 
 
291
 
#ifdef HasSharedMemory
292
 
        if (use_shm) {
293
 
                shminfo.shmid = shmget (IPC_PRIVATE,
294
 
                                                        vid->width * vid->height * 4 /* (ap.bpp/8)*/,
295
 
                                                        IPC_CREAT|0777);
296
 
                if (shminfo.shmid == -1) {
297
 
                        perror ("shmget()");
298
 
                        exit (2);
299
 
                }
300
 
                shminfo.shmaddr = ap.image = shmat (shminfo.shmid, 0, 0);
301
 
                shminfo.readOnly = False;
302
 
                ap.ximage->data = shminfo.shmaddr;
303
 
                if (XShmAttach (dpy, &shminfo) == 0) {
304
 
                        perror ("XShmAttach()");
305
 
                        exit (2);
306
 
                }
307
 
        }
308
 
#endif
309
 
        ap.use_shm = use_shm;
310
 
        ap.use_snd = use_snd;
311
 
        ap.time_ms = time_ms();
312
 
 
313
 
        XtAppAddTimeOut (gAppCont, 1, x_play_sound, &ap);
314
 
        XtAppAddTimeOut (gAppCont, 2, x_display_frame, &ap);
315
 
        XtAppMainLoop(gAppCont);
316
 
}
317
 
 
318
 
/*
319
 
 */
320
 
void
321
 
x_fini (void)
322
 
{
323
 
#ifdef HasSharedMemory
324
 
        if (ap.use_shm) {
325
 
                XShmDetach (XtDisplay(gTop), &shminfo);
326
 
                shmdt (shminfo.shmaddr);
327
 
                shmctl (shminfo.shmid, IPC_RMID, 0);
328
 
        } else
329
 
#endif
330
 
        {
331
 
                XtFree (ap.image);
332
 
        }
333
 
        exit (0);
334
 
}
335