]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/lib/vidxanm.cpp
no message
[wxWidgets.git] / utils / wxMMedia2 / lib / vidxanm.cpp
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 #ifdef __GNUG__
11 #pragma implementation "vidxanm.h"
12 #endif
13 #ifdef WX_PRECOMP
14 #include <wx_prec.h>
15 #else
16 #include <wx/wx.h>
17 #endif
18
19 // Pizza !
20 #include <wx/gtk/win_gtk.h>
21
22 #include <X11/Xlib.h>
23 #include <X11/Intrinsic.h>
24 #ifdef __WXGTK__
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkwindow.h>
27 #include <gdk/gdk.h>
28 #include <gdk/gdkprivate.h>
29 #endif
30
31 #include <wx/filefn.h>
32 #include <wx/wfstream.h>
33
34 #define WXMMEDIA_INTERNAL
35 #include "vidbase.h"
36 #include "vidxanm.h"
37
38 IMPLEMENT_DYNAMIC_CLASS(wxVideoXANIM, wxVideoBaseDriver)
39
40 class wxVideoXANIMProcess: public wxProcess {
41 public:
42 wxVideoXANIMProcess(wxVideoXANIM *xanim);
43
44 void OnTerminate(int pid, int status);
45
46 protected:
47 wxVideoXANIM *m_vid_xanim;
48 };
49
50
51 wxVideoXANIMProcess::wxVideoXANIMProcess(wxVideoXANIM *xanim)
52 {
53 m_vid_xanim = xanim;
54 }
55
56 void wxVideoXANIMProcess::OnTerminate(int WXUNUSED(pid), int WXUNUSED(status))
57 {
58 m_vid_xanim->m_xanim_started = FALSE;
59 }
60
61 wxVideoXANIM::wxVideoXANIM()
62 : wxVideoBaseDriver()
63 {
64 m_internal = new wxXANIMinternal;
65 m_xanim_detector = new wxVideoXANIMProcess(this);
66 m_xanim_started = FALSE;
67 m_paused = FALSE;
68 m_filename = "";
69 }
70
71 wxVideoXANIM::wxVideoXANIM(wxInputStream& str)
72 : wxVideoBaseDriver(str)
73 {
74 m_internal = new wxXANIMinternal;
75 m_xanim_detector = new wxVideoXANIMProcess(this);
76 m_xanim_started = FALSE;
77 m_paused = FALSE;
78
79 m_filename = wxGetTempFileName("vidxa");
80 wxFileOutputStream fout(m_filename);
81
82 fout << str;
83 }
84
85 wxVideoXANIM::~wxVideoXANIM()
86 {
87 if (m_xanim_started)
88 Stop();
89 delete m_internal;
90 delete m_xanim_detector;
91
92 wxRemoveFile(m_filename);
93 }
94
95 bool wxVideoXANIM::Play()
96 {
97 if (!m_paused && m_xanim_started)
98 return TRUE;
99 if (!m_video_output) {
100 wxVideoCreateFrame(this);
101 return TRUE;
102 }
103
104 // The movie starts with xanim
105 if (RestartXANIM()) {
106 m_paused = FALSE;
107 return TRUE;
108 }
109 return FALSE;
110 }
111
112 bool wxVideoXANIM::Pause()
113 {
114 if (!m_paused && SendCommand(" ")) {
115 m_paused = TRUE;
116 return TRUE;
117 }
118 return FALSE;
119 }
120
121 bool wxVideoXANIM::Resume()
122 {
123 if (m_paused && SendCommand(" ")) {
124 m_paused = FALSE;
125 return TRUE;
126 }
127 return FALSE;
128 }
129
130 bool wxVideoXANIM::Stop()
131 {
132 if (!m_xanim_started)
133 return FALSE;
134
135 SendCommand("q");
136
137 m_xanim_started = FALSE;
138 m_paused = FALSE;
139
140 return TRUE;
141 }
142
143 bool wxVideoXANIM::SetVolume(wxUint8 vol)
144 {
145 if (vol > 100)
146 vol = 100;
147
148 wxString str_vol("v%d", vol);
149 return SendCommand(str_vol.GetData());
150 }
151
152 bool wxVideoXANIM::Resize(wxUint16 WXUNUSED(w), wxUint16 WXUNUSED(h))
153 {
154 // Not implemented
155 // Actually, I think that we just need to resize the output window ...
156 return FALSE;
157 }
158
159 bool wxVideoXANIM::GetSize(wxSize& size) const
160 {
161 // Not implemented
162 return FALSE;
163 }
164
165 bool wxVideoXANIM::IsCapable(wxVideoType v_type)
166 {
167 if (v_type == wxVIDEO_MSAVI || v_type == wxVIDEO_MPEG ||
168 v_type == wxVIDEO_QT || v_type == wxVIDEO_GIF || v_type == wxVIDEO_JMOV ||
169 v_type == wxVIDEO_FLI || v_type == wxVIDEO_IFF || v_type == wxVIDEO_SGI)
170 return TRUE;
171 else
172 return FALSE;
173 }
174
175 bool wxVideoXANIM::IsPaused()
176 {
177 return m_paused;
178 }
179
180 bool wxVideoXANIM::IsStopped()
181 {
182 return !m_xanim_started;
183 }
184
185 bool wxVideoXANIM::AttachOutput(wxWindow& out)
186 {
187 if (!wxVideoBaseDriver::AttachOutput(out))
188 return FALSE;
189
190 return TRUE;
191 }
192
193 void wxVideoXANIM::DetachOutput()
194 {
195 SendCommand("q");
196 m_xanim_started = FALSE;
197 m_paused = FALSE;
198
199 wxVideoBaseDriver::DetachOutput();
200 }
201
202 bool wxVideoXANIM::SendCommand(const char *command, char **ret,
203 wxUint32 *size)
204 {
205 if (!m_xanim_started)
206 if (!RestartXANIM())
207 return FALSE;
208
209 // Send a command to XAnim through X11 Property
210 XChangeProperty(m_internal->xanim_dpy, m_internal->xanim_window,
211 m_internal->xanim_atom,
212 XA_STRING, 8, PropModeReplace, (unsigned char *)command,
213 strlen(command));
214 XFlush(m_internal->xanim_dpy);
215 if (ret) {
216 int prop_format;
217 Atom prop_type;
218 unsigned long extra;
219
220 XGetWindowProperty(m_internal->xanim_dpy, m_internal->xanim_window,
221 m_internal->xanim_ret, 0, 16, True, AnyPropertyType,
222 &prop_type, &prop_format, (unsigned long *)size,
223 &extra, (unsigned char **)ret);
224 }
225 return TRUE;
226 }
227
228 bool wxVideoXANIM::RestartXANIM()
229 {
230 wxString xanim_command;
231 int ret;
232 Atom prop_type;
233 int prop_format;
234 unsigned long nitems;
235 unsigned long extra;
236 char prop[4];
237 bool xanim_chg_size;
238
239 if (!m_video_output || m_xanim_started)
240 return FALSE;
241
242 // Check if we can change the size of the window dynamicly
243 xanim_chg_size = TRUE;
244 // Get current display
245 #ifdef __WXGTK__
246 m_internal->xanim_dpy = gdk_display;
247 // We absolutely need the window to be realized.
248 GtkPizza *pizza = GTK_PIZZA( m_video_output->m_wxwindow );
249 GdkWindow *window = pizza->bin_window;
250
251 m_internal->xanim_window =
252 ((GdkWindowPrivate *)window)->xwindow;
253 #endif
254 // Get the XANIM atom
255 m_internal->xanim_atom = XInternAtom(m_internal->xanim_dpy,
256 "XANIM_PROPERTY", False);
257
258 // Build the command
259 xanim_command.Printf(_T("xanim -Zr +Ze +Sr +f +W%d +f +q "
260 "+Av70 %s %s"), m_internal->xanim_window,
261 (xanim_chg_size) ? _T("") : _T(""),
262 WXSTRINGCAST m_filename);
263
264 // Execute it
265 if (!wxExecute(xanim_command, FALSE, m_xanim_detector))
266 return FALSE;
267
268 // Wait for XAnim to be ready
269 nitems = 0;
270 m_xanim_started = TRUE;
271 while (nitems == 0 && m_xanim_started) {
272 ret = XGetWindowProperty(m_internal->xanim_dpy, m_internal->xanim_window,
273 m_internal->xanim_atom,
274 0, 4, False, AnyPropertyType, &prop_type,
275 &prop_format, &nitems, &extra,
276 (unsigned char **)&prop);
277 wxYield();
278 }
279
280 m_paused = FALSE;
281
282 return TRUE;
283 }