]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/mmedia/vidxanm.cpp
Avoid warnings from mgl headers if possible.
[wxWidgets.git] / contrib / src / mmedia / vidxanm.cpp
CommitLineData
e8482f24
GL
1// -------------------------------------------------------------------------
2// Name: vidxanm.cpp
3// Purpose: wxMMedia
4// Author: Guilhem Lavaux
5// Created: 1997
6// Updated: 1998
7// Copyright: (C) 1997, 1998, 1999 Guilhem Lavaux
8// License: wxWindows license
9// -------------------------------------------------------------------------
10
11#ifdef __GNUG__
12#pragma implementation "vidxanm.h"
13#endif
14
92a19c2e 15#include "wx/wxprec.h"
e8482f24
GL
16
17#ifndef WX_PRECOMP
18#include <wx/wx.h>
19#endif
20
c9ce02ac 21#ifdef __WXGTK__
e8482f24
GL
22// Pizza !
23#include <wx/gtk/win_gtk.h>
c9ce02ac 24#endif
e8482f24
GL
25
26#include <X11/Xlib.h>
27#include <X11/Intrinsic.h>
28#ifdef __WXGTK__
29#include <gtk/gtkwidget.h>
30#include <gtk/gtkwindow.h>
31#include <gdk/gdk.h>
32#include <gdk/gdkprivate.h>
33#endif
34
35#include <wx/filefn.h>
36#include <wx/wfstream.h>
37#include <wx/datstrm.h>
38#include <wx/tokenzr.h>
39
40#define WXMMEDIA_INTERNAL
41#include "wx/mmedia/vidbase.h"
42#include "wx/mmedia/vidxanm.h"
43
44IMPLEMENT_DYNAMIC_CLASS(wxVideoXANIM, wxVideoBaseDriver)
45
46// -------------------------------------------------------------------------
47// End process detector
48
49class wxVideoXANIMProcess: public wxProcess {
50public:
51 wxVideoXANIMProcess(wxVideoXANIM *xanim);
52
53 void OnTerminate(int pid, int status);
54
55protected:
56 wxVideoXANIM *m_vid_xanim;
57};
58
59class wxVideoXANIMOutput: public wxProcess {
60public:
61 wxVideoXANIMOutput();
62
63 void OnTerminate(int pid, int status);
64
65 bool IsTerminated() const;
66protected:
67 bool m_terminated;
68};
69
70// -------------------------------------------------------------------------
71// XAnim video driver (process handling implementation)
72
73wxVideoXANIMProcess::wxVideoXANIMProcess(wxVideoXANIM *xanim)
74{
75 m_vid_xanim = xanim;
76}
77
78void wxVideoXANIMProcess::OnTerminate(int WXUNUSED(pid), int WXUNUSED(status))
79{
dea7e44a 80 m_vid_xanim->m_xanim_started = false;
e8482f24
GL
81 m_vid_xanim->OnFinished();
82}
83
84wxVideoXANIMOutput::wxVideoXANIMOutput()
dea7e44a 85 : wxProcess(NULL, wxID_ANY)
e8482f24 86{
dea7e44a 87 m_terminated = false;
e8482f24
GL
88 Redirect();
89}
90
91bool wxVideoXANIMOutput::IsTerminated() const
92{
93 return m_terminated;
94}
95
96void wxVideoXANIMOutput::OnTerminate(int pid, int status)
97{
dea7e44a 98 m_terminated = true;
e8482f24
GL
99}
100
101// -------------------------------------------------------------------------
102// XAnim video driver (implementation)
103
104wxVideoXANIM::wxVideoXANIM()
105 : wxVideoBaseDriver()
106{
107 m_internal = new wxXANIMinternal;
108 m_xanim_detector = new wxVideoXANIMProcess(this);
dea7e44a
WS
109 m_xanim_started = false;
110 m_paused = false;
6f568182 111 m_filename = wxEmptyString;
dea7e44a 112 m_remove_file = false;
e8482f24
GL
113}
114
115wxVideoXANIM::wxVideoXANIM(wxInputStream& str)
116 : wxVideoBaseDriver(str)
117{
118 m_internal = new wxXANIMinternal;
119 m_xanim_detector = new wxVideoXANIMProcess(this);
dea7e44a
WS
120 m_xanim_started = false;
121 m_paused = false;
e8482f24
GL
122 m_size[0] = 0;
123 m_size[1] = 0;
124
6f568182 125 m_filename = wxGetTempFileName(_T("vidxa"));
dea7e44a 126 m_remove_file = true;
e8482f24
GL
127 wxFileOutputStream fout(m_filename);
128
129 fout << str;
130
131 CollectInfo();
132}
133
134wxVideoXANIM::wxVideoXANIM(const wxString& filename)
135{
136 m_internal = new wxXANIMinternal;
137 m_xanim_detector = new wxVideoXANIMProcess(this);
dea7e44a
WS
138 m_xanim_started = false;
139 m_paused = false;
e8482f24
GL
140
141 m_filename = filename;
dea7e44a 142 m_remove_file = false;
e8482f24
GL
143 m_size[0] = 0;
144 m_size[1] = 0;
145
146 CollectInfo();
147}
148
149wxVideoXANIM::~wxVideoXANIM()
150{
151 if (m_xanim_started)
152 Stop();
153 delete m_internal;
154 delete m_xanim_detector;
155
156 if (m_remove_file)
157 wxRemoveFile(m_filename);
158}
159
160// -------------------------------------------------------------------------
161// Movie controller
162
163bool wxVideoXANIM::Play()
164{
165 if (!m_paused && m_xanim_started)
dea7e44a 166 return true;
e8482f24
GL
167 if (!m_video_output) {
168 wxVideoCreateFrame(this);
dea7e44a 169 return true;
e8482f24
GL
170 }
171
172 // The movie starts with xanim
173 if (RestartXANIM()) {
dea7e44a
WS
174 m_paused = false;
175 return true;
e8482f24 176 }
dea7e44a 177 return false;
e8482f24
GL
178}
179
180bool wxVideoXANIM::Pause()
181{
182 if (!m_paused && SendCommand(" ")) {
dea7e44a
WS
183 m_paused = true;
184 return true;
e8482f24 185 }
dea7e44a 186 return false;
e8482f24
GL
187}
188
189bool wxVideoXANIM::Resume()
190{
191 if (m_paused && SendCommand(" ")) {
dea7e44a
WS
192 m_paused = false;
193 return true;
e8482f24 194 }
dea7e44a 195 return false;
e8482f24
GL
196}
197
198bool wxVideoXANIM::Stop()
199{
200 if (!m_xanim_started)
dea7e44a 201 return false;
e8482f24
GL
202
203 SendCommand("q");
204
205 // We are waiting for the termination of the subprocess.
206 while (m_xanim_started) {
207 wxYield();
208 }
209
dea7e44a 210 m_paused = false;
e8482f24 211
dea7e44a 212 return true;
e8482f24
GL
213}
214
215// -------------------------------------------------------------------------
216// Movie size controller
217
218bool wxVideoXANIM::SetSize(wxSize size)
219{
220 if (!m_video_output)
dea7e44a 221 return false;
e8482f24
GL
222
223 m_video_output->SetSize(size.GetWidth(), size.GetHeight());
dea7e44a 224 return false;
e8482f24
GL
225}
226
227bool wxVideoXANIM::GetSize(wxSize& size) const
228{
229 if (m_size[0] == 0)
dea7e44a 230 return false;
e8482f24 231 size.Set(m_size[0], m_size[1]);
dea7e44a 232 return true;
e8482f24
GL
233}
234
235// -------------------------------------------------------------------------
236// Capabilities of XAnim
237
238bool wxVideoXANIM::IsCapable(wxVideoType v_type) const
239{
240 if (v_type == wxVIDEO_MSAVI || v_type == wxVIDEO_MPEG ||
dea7e44a
WS
241 v_type == wxVIDEO_QT || v_type == wxVIDEO_GIF || v_type == wxVIDEO_JMOV ||
242 v_type == wxVIDEO_FLI || v_type == wxVIDEO_IFF || v_type == wxVIDEO_SGI)
243 return true;
e8482f24 244 else
dea7e44a 245 return false;
e8482f24
GL
246}
247
248// -------------------------------------------------------------------------
249// Movie state
250
251wxString wxVideoXANIM::GetMovieCodec() const
252{
253 if (m_size[0] == 0)
254 return wxT("");
255 return m_movieCodec;
256}
257
258wxString wxVideoXANIM::GetAudioCodec() const
259{
260 if (m_size[0] == 0)
261 return wxT("");
262 return m_audioCodec;
263}
264
265wxUint32 wxVideoXANIM::GetSampleRate() const
266{
267 if (m_size[0] == 0)
268 return 0;
269 return m_sampleRate;
270}
271
272wxUint8 wxVideoXANIM::GetChannels() const
273{
274 if (m_size[0] == 0)
275 return 0;
276 return m_channels;
277}
278
279wxUint8 wxVideoXANIM::GetBPS() const
280{
281 if (m_size[0] == 0)
282 return 0;
283 return m_bps;
284}
285
286double wxVideoXANIM::GetFrameRate() const
287{
288 if (m_size[0] == 0)
289 return 0.0;
290 return m_frameRate;
291}
292
293wxUint32 wxVideoXANIM::GetNbFrames() const
294{
295 if (m_size[0] == 0)
296 return 0;
297 return m_frames;
298}
299
300
301bool wxVideoXANIM::IsPaused() const
302{
303 return m_paused;
304}
305
306bool wxVideoXANIM::IsStopped() const
307{
308 return !m_xanim_started;
309}
310
311// -------------------------------------------------------------------------
312// Output management
313
314bool wxVideoXANIM::AttachOutput(wxWindow& out)
315{
316 if (!wxVideoBaseDriver::AttachOutput(out))
dea7e44a 317 return false;
e8482f24 318
dea7e44a 319 return true;
e8482f24
GL
320}
321
322void wxVideoXANIM::DetachOutput()
323{
324 SendCommand("q");
dea7e44a
WS
325 m_xanim_started = false;
326 m_paused = false;
e8482f24
GL
327
328 wxVideoBaseDriver::DetachOutput();
329}
330
331// -------------------------------------------------------------------------
332// Lowlevel XAnim controller
333
334bool wxVideoXANIM::SendCommand(const char *command, char **ret,
dea7e44a 335 wxUint32 *size)
e8482f24
GL
336{
337 if (!m_xanim_started)
338 if (!RestartXANIM())
dea7e44a 339 return false;
e8482f24
GL
340
341 // Send a command to XAnim through X11 Property
342 XChangeProperty(m_internal->xanim_dpy, m_internal->xanim_window,
dea7e44a
WS
343 m_internal->xanim_atom,
344 XA_STRING, 8, PropModeReplace, (unsigned char *)command,
345 strlen(command));
e8482f24
GL
346 XFlush(m_internal->xanim_dpy);
347 if (ret) {
348 int prop_format;
dea7e44a
WS
349 Atom prop_type;
350 unsigned long extra;
351
352 XGetWindowProperty(m_internal->xanim_dpy, m_internal->xanim_window,
353 m_internal->xanim_ret, 0, 16, True, AnyPropertyType,
354 &prop_type, &prop_format, (unsigned long *)size,
355 &extra, (unsigned char **)ret);
e8482f24 356 }
dea7e44a 357 return true;
e8482f24
GL
358}
359
360bool wxVideoXANIM::CollectInfo()
361{
362 wxVideoXANIMOutput *xanimProcess;
363 wxString xanim_command;
364 wxStringTokenizer tokenizer;
365
366 xanimProcess = new wxVideoXANIMOutput;
367 xanim_command = wxT("xanim +v +Zv -Ae ");
368 xanim_command += m_filename;
dea7e44a
WS
369 if (!wxExecute(xanim_command, false, xanimProcess))
370 return false;
e8482f24
GL
371
372 wxInputStream *infoStream = xanimProcess->GetInputStream();
373 wxString totalOutput;
374
15e8daec 375 while (infoStream->GetLastError() == wxSTREAM_NO_ERROR) {
e8482f24
GL
376 char line[100];
377
378 infoStream->Read(line, sizeof(line)-1);
379 if (infoStream->LastRead() == 0)
380 break;
381
382 line[infoStream->LastRead()] = 0;
383
6f568182 384 totalOutput += wxString::FromAscii(line);
e8482f24
GL
385 }
386
387 // This is good for everything ... :-)
388 int position = totalOutput.Find(wxT("Video Codec:"));
389
390 totalOutput.Remove(0, position+13);
391
392 position = totalOutput.Find(wxT("depth="));
393 m_movieCodec = totalOutput(0, position);
394
395 totalOutput.Remove(0, position);
6f568182 396 tokenizer.SetString(totalOutput, wxT("\n\r"));
e8482f24
GL
397
398 // the rest of the line
399 wxString token = tokenizer.GetNextToken();
400 unsigned long my_long;
401
402#define GETINT(i) \
403totalOutput.ToULong(&my_long); \
404i = my_long;
405
406 // 'Audio Codec:'
407 totalOutput = tokenizer.GetString();
408 totalOutput.Remove(0, totalOutput.Find(wxT(":"))+2);
409
410 position = totalOutput.Find(wxT("Rate"));
411 m_audioCodec = totalOutput(0, position-1);
412
413 // 'Rate='
414 totalOutput.Remove(0, totalOutput.Find(wxT("="))+1);
415 GETINT(m_sampleRate);
416 // 'Chans='
417 totalOutput.Remove(0, totalOutput.Find(wxT("="))+1);
418 GETINT(m_channels);
419 // 'Bps='
420 totalOutput.Remove(0, totalOutput.Find(wxT("="))+1);
421 GETINT(m_bps);
422 // 'Frame Stats:'
423 tokenizer.Reinit(totalOutput);
424 tokenizer.GetNextToken();
425 totalOutput = tokenizer.GetString();
426 totalOutput.Remove(0, totalOutput.Find(wxT(":"))+2);
427 // 'Size='
428 totalOutput.Remove(0, totalOutput.Find(wxT("="))+1);
429 GETINT(m_size[0]);
430 // 'x'
431 totalOutput.Remove(0,1);
432 GETINT(m_size[1]);
433 // 'Frames='
434 totalOutput.Remove(0, totalOutput.Find(wxT("="))+1);
435 GETINT(m_frames);
436 // 'avfps='
437 totalOutput.Remove(0, totalOutput.Find(wxT("="))+1);
438 totalOutput.ToDouble(&m_frameRate);
439
440 // We wait for the conclusion
441 while (!xanimProcess->IsTerminated())
442 wxYield();
443
444 delete xanimProcess;
445
dea7e44a 446 return true;
e8482f24
GL
447}
448
449bool wxVideoXANIM::RestartXANIM()
450{
451 wxString xanim_command;
452 int ret;
453 Atom prop_type;
454 int prop_format;
455 unsigned long nitems;
456 unsigned long extra;
457 char prop[4];
458 bool xanim_chg_size;
459
460 if (!m_video_output || m_xanim_started)
dea7e44a 461 return false;
e8482f24
GL
462
463 // Check if we can change the size of the window dynamicly
dea7e44a 464 xanim_chg_size = true;
e8482f24
GL
465 // Get current display
466#ifdef __WXGTK__
467 m_internal->xanim_dpy = gdk_display;
468 GtkPizza *pizza = GTK_PIZZA( m_video_output->m_wxwindow );
469 GdkWindow *window = pizza->bin_window;
470
15e8daec 471 m_internal->xanim_window = GDK_WINDOW_XWINDOW(window);
e8482f24
GL
472#endif
473 // Get the XANIM atom
474 m_internal->xanim_atom = XInternAtom(m_internal->xanim_dpy,
dea7e44a 475 "XANIM_PROPERTY", False);
e8482f24
GL
476
477 // Build the command
478 xanim_command.Printf(wxT("xanim -Zr +Ze +Sr +f +W%d +f +q "
dea7e44a
WS
479 "+Av70 %s %s"), m_internal->xanim_window,
480 (xanim_chg_size) ? _T("") : _T(""),
481 WXSTRINGCAST m_filename);
e8482f24
GL
482
483 // Execute it
dea7e44a
WS
484 if (!wxExecute(xanim_command, false, m_xanim_detector))
485 return false;
e8482f24
GL
486
487 // Wait for XAnim to be ready
488 nitems = 0;
dea7e44a 489 m_xanim_started = true;
e8482f24
GL
490 while (nitems == 0 && m_xanim_started) {
491 ret = XGetWindowProperty(m_internal->xanim_dpy, m_internal->xanim_window,
dea7e44a
WS
492 m_internal->xanim_atom,
493 0, 4, False, AnyPropertyType, &prop_type,
494 &prop_format, &nitems, &extra,
495 (unsigned char **)&prop);
e8482f24
GL
496 wxYield();
497 }
498
499 wxSize vibrato_size;
500
501 vibrato_size = m_video_output->GetSize();
502
503 vibrato_size.SetWidth(vibrato_size.GetWidth()+1);
504 m_video_output->SetSize(vibrato_size);
505 vibrato_size.SetWidth(vibrato_size.GetWidth()-1);
506 m_video_output->SetSize(vibrato_size);
c42b1de6
GL
507 // Very useful ! Actually it "should" sends a SETSIZE event to XAnim
508 // FIXME: This event is not sent !!
e8482f24 509
dea7e44a 510 m_paused = false;
e8482f24 511
dea7e44a 512 return true;
e8482f24 513}