9c41ccf1ea915d20548a0f755f6676ea92249abe
[goodguy/history.git] / cinelerra-5.1 / guicast / vicon.C
1 #include "vicon.h"
2
3 #include "bctimer.h"
4 #include "bcwindow.h"
5 #include "bccolors.h"
6 #include "keys.h"
7 #include "mutex.h"
8 #include "condition.h"
9
10 VIcon::
11 VIcon(int vw, int vh, double rate)
12 {
13         this->vw = vw;
14         this->vh = vh;
15         this->frame_rate = rate;
16
17         cycle_start = 0;
18         age = 0;
19         seq_no = 0;
20         in_use = 1;
21         hidden = 0;
22         audio_data = 0;
23         audio_size = 0;
24         playing_audio = 0;
25 }
26
27 VIcon::
28 ~VIcon()
29 {
30         clear_images();
31         delete [] audio_data;
32 }
33
34 void VIcon::
35 add_image(VFrame *frm, int ww, int hh, int vcmdl)
36 {
37         VIFrame *vifrm = new VIFrame(ww, hh, vcmdl);
38         VFrame *img = *vifrm;
39         img->transfer_from(frm);
40         images.append(vifrm);
41 }
42
43 void VIcon::
44 draw_vframe(VIconThread *vt, BC_WindowBase *wdw, int x, int y)
45 {
46         VFrame *vfrm = frame();
47         if( !vfrm ) return;
48         int sx0 = 0, sx1 = sx0 + vt->view_w;
49         int sy0 = 0, sy1 = sy0 + vt->view_h;
50         int dx0 = x, dx1 = dx0 + vw;
51         int dy0 = y, dy1 = dy0 + vh;
52         if( (x=vt->draw_x0-dx0) > 0 ) { sx0 += (x*vt->view_w)/vw;  dx0 = vt->draw_x0; }
53         if( (x=dx1-vt->draw_x1) > 0 ) { sx1 -= (x*vt->view_w)/vw;  dx1 = vt->draw_x1; }
54         if( (y=vt->draw_y0-dy0) > 0 ) { sy0 += (y*vt->view_h)/vh;  dy0 = vt->draw_y0; }
55         if( (y=dy1-vt->draw_y1) > 0 ) { sy1 -= (y*vt->view_h)/vh;  dy1 = vt->draw_y1; }
56         int sw = sx1 - sx0, sh = sy1 - sy0;
57         int dw = dx1 - dx0, dh = dy1 - dy0;
58         if( dw > 0 && dh > 0 && sw > 0 && sh > 0 )
59                 wdw->draw_vframe(vfrm, dx0,dy0, dw,dh, sx0,sy0, sw,sh);
60 }
61
62 void VIconThread::
63 set_drawing_area(int x0, int y0, int x1, int y1)
64 {
65         draw_x0 = x0;  draw_y0 = y0;
66         draw_x1 = x1;  draw_y1 = y1;
67 }
68
69 VIcon *VIconThread::low_vicon()
70 {
71         if( !t_heap.size() ) return 0;
72         VIcon *vip = t_heap[0];
73         remove_vicon(0);
74         return vip;
75 }
76
77 void VIconThread::remove_vicon(int i)
78 {
79         int sz = t_heap.size();
80         for( int k; (k=2*(i+1)) < sz; i=k ) {
81                 if( t_heap[k]->age > t_heap[k-1]->age ) --k;
82                 t_heap[i] = t_heap[k];
83         }
84         VIcon *last = t_heap[--sz];
85         t_heap.remove_number(sz);
86         double age = last->age;
87         for( int k; i>0 && age<t_heap[k=(i-1)/2]->age; i=k )
88                 t_heap[i] = t_heap[k];
89         t_heap[i] = last;
90 }
91
92
93 VIconThread::
94 VIconThread(BC_WindowBase *wdw, int vw, int vh)
95  : Thread(1, 0, 0)
96 {
97         this->wdw = wdw;
98         this->view_win = 0;  this->vicon = 0;
99         this->view_w = vw;   this->view_h = vh;
100         this->viewing = 0;
101         this->draw_x0 = 0;   this->draw_x1 = wdw->get_w();
102         this->draw_y0 = 0;   this->draw_y1 = wdw->get_h();
103         draw_lock = new Condition(0, "VIconThread::draw_lock", 1);
104         timer = new Timer();
105         this->refresh_rate = VICON_RATE;
106         done = 0;
107         interrupted = -1;
108 }
109
110 VIconThread::
111 ~VIconThread()
112 {
113         stop_drawing();
114         done = 1;
115         draw_lock->unlock();
116         if( Thread::running() ) {
117                 Thread::cancel();
118         }
119         Thread::join();
120         t_heap.remove_all_objects();
121         delete timer;
122         delete draw_lock;
123 }
124
125 void VIconThread::
126 start_drawing()
127 {
128         wdw->lock_window("VIconThread::start_drawing");
129         if( view_win )
130                 wdw->set_active_subwindow(view_win);
131         if( interrupted < 0 )
132                 draw_lock->unlock();
133         interrupted = 0;
134         wdw->unlock_window();
135 }
136
137 void VIconThread::
138 stop_drawing()
139 {
140         wdw->lock_window("VIconThread::stop_drawing");
141         set_view_popup(0);
142         if( !interrupted )
143                 interrupted = 1;
144         wdw->unlock_window();
145 }
146
147 int VIconThread::keypress_event(int key)
148 {
149         if( key != ESC ) return 0;
150         set_view_popup(0);
151         return 1;
152 }
153
154 int ViewPopup::button_press_event()
155 {
156         vt->set_view_popup(0);
157         return 1;
158 }
159
160 bool VIconThread::
161 visible(VIcon *vicon, int x, int y)
162 {
163         if( vicon->hidden ) return false;
164         if( y+vicon->vh <= draw_y0 ) return false;
165         if( y >= draw_y1 ) return false;
166         if( x+vicon->vw <= draw_x0 ) return false;
167         if( x >= draw_x1 ) return false;
168         return true;
169 }
170
171 int ViewPopup::keypress_event()
172 {
173         int key = get_keypress();
174         return vt->keypress_event(key);
175 }
176 ViewPopup::ViewPopup(VIconThread *vt, VFrame *frame, int x, int y, int w, int h)
177  : BC_Popup(vt->wdw, x, y, w, h, BLACK)
178 {
179         this->vt = vt;
180 }
181
182 ViewPopup::~ViewPopup()
183 {
184         vt->wdw->set_active_subwindow(0);
185 }
186
187 void ViewPopup::draw_vframe(VFrame *frame)
188 {
189         if( !frame ) return;
190         BC_WindowBase::draw_vframe(frame, 0,0, get_w(),get_h());
191 }
192
193 ViewPopup *VIconThread::new_view_window(VFrame *frame)
194 {
195         BC_WindowBase *parent = wdw->get_parent();
196         XineramaScreenInfo *info = parent->get_xinerama_info(-1);
197         int cx = info ? info->x_org + info->width/2 : parent->get_root_w(0)/2;
198         int cy = info ? info->y_org + info->height/2 : parent->get_root_h(0)/2;
199         int vx = viewing->get_vx(), rx = 0;
200         int vy = viewing->get_vy(), ry = 0;
201         wdw->get_root_coordinates(vx, vy, &rx, &ry);
202         rx += (rx >= cx ? -view_w : viewing->vw);
203         ry += (ry >= cy ? -view_h : viewing->vh);
204         ViewPopup *vwin = new ViewPopup(this, frame, rx, ry, view_w, view_h);
205         wdw->set_active_subwindow(vwin);
206         return vwin;
207 }
208
209 void VIconThread::
210 reset_images()
211 {
212         for( int i=t_heap.size(); --i>=0; ) t_heap[i]->reset();
213         timer->update();
214         img_dirty = win_dirty = 0;
215 }
216
217 void VIconThread::add_vicon(VIcon *vip)
218 {
219         double age = vip->age;
220         int i = t_heap.size();  t_heap.append(vip);
221         for( int k; i>0 && age<t_heap[(k=(i-1)/2)]->age; i=k )
222                 t_heap[i] = t_heap[k];
223         t_heap[i] = vip;
224 }
225
226 int VIconThread::del_vicon(VIcon *&vicon)
227 {
228         int i = t_heap.size();
229         while( --i >= 0 && t_heap[i] != vicon );
230         if( i < 0 ) return 0;
231         remove_vicon(i);
232         delete vicon;  vicon = 0;
233         return 1;
234 }
235
236 void VIconThread::set_view_popup(VIcon *vicon)
237 {
238         if( !vicon && this->vicon )
239                 this->vicon->stop_audio();
240         this->vicon = vicon;
241 }
242
243 int VIconThread::
244 update_view()
245 {
246         delete view_win;  view_win = 0;
247         if( (viewing=vicon) != 0 ) {
248                 VFrame *frame = viewing->frame();
249                 view_win = new_view_window(frame);
250                 view_win->show_window();
251                 vicon->start_audio();
252         }
253         wdw->set_active_subwindow(view_win);
254         return 1;
255 }
256
257
258 void VIconThread::
259 draw_images()
260 {
261         for( int i=0; i<t_heap.size(); ++i )
262                 draw(t_heap[i]);
263 }
264
265 void VIconThread::
266 flash()
267 {
268         if( !img_dirty && !win_dirty ) return;
269         if( img_dirty ) wdw->flash();
270         if( win_dirty && view_win ) view_win->flash();
271         win_dirty = img_dirty = 0;
272 }
273
274 int VIconThread::
275 draw(VIcon *vicon)
276 {
277         int x = vicon->get_vx(), y = vicon->get_vy();
278         int draw_img = visible(vicon, x, y);
279         int draw_win = view_win && viewing == vicon ? 1 : 0;
280         if( !draw_img && !draw_win ) return 0;
281         if( !vicon->frame() ) return 0;
282         if( draw_img ) {
283                 vicon->draw_vframe(this, wdw, x, y);
284                 img_dirty = 1;
285         }
286         if( draw_win ) {
287                 view_win->draw_vframe(vicon->frame());
288                 win_dirty = 1;
289         }
290         return 1;
291 }
292
293 void VIconThread::hide_vicons(int v)
294 {
295         for( int i=0; i<t_heap.size(); ++i ) {
296                 t_heap[i]->hidden = v;
297                 t_heap[i]->age = 0;
298         }
299 }
300
301 void VIconThread::
302 run()
303 {
304         while(!done) {
305                 draw_lock->lock("VIconThread::run 0");
306                 if( done ) break;
307                 wdw->lock_window("BC_WindowBase::run 1");
308                 drawing_started();
309                 reset_images();
310                 int64_t seq_no = 0, now = 0;
311                 int64_t draw_flash = 1000 / refresh_rate;
312                 while( !interrupted ) {
313                         if( viewing != vicon )
314                                 update_view();
315                         VIcon *next = low_vicon();
316                         while( next && next->age < draw_flash ) {
317                                 now = timer->get_difference();
318                                 if( now >= draw_flash ) break;
319                                 draw(next);
320                                 if( !next->seq_no ) {
321                                         next->cycle_start = now;
322                                         if( next->playing_audio )
323                                                 next->start_audio();
324                                 }
325                                 int64_t ref_no = (now - next->cycle_start) / 1000. * refresh_rate;
326                                 int count = ref_no - next->seq_no;
327                                 if( count < 1 ) count = 1;
328                                 ref_no = next->seq_no + count;
329                                 next->age =  next->cycle_start + 1000. * ref_no / refresh_rate;
330                                 if( !next->set_seq_no(ref_no) )
331                                         next->age = now + 1000.;
332                                 add_vicon(next);
333                                 next = low_vicon();
334                         }
335                         if( !next ) break;
336                         add_vicon(next);
337                         if( draw_flash < now+1 )
338                                 draw_flash = now+1;
339                         wdw->unlock_window();
340                         while( !interrupted ) {
341                                 now = timer->get_difference();
342                                 int64_t ms = draw_flash - now;
343                                 if( ms <= 0 ) break;
344                                 if( ms > 100 ) ms = 100;
345                                 Timer::delay(ms);
346                         }
347                         wdw->lock_window("BC_WindowBase::run 2");
348                         now = timer->get_difference();
349                         int64_t late = now - draw_flash;
350                         if( late < 1000 ) flash();
351                         int64_t ref_no = now / 1000. * refresh_rate;
352                         int64_t count = ref_no - seq_no;
353                         if( count < 1 ) count = 1;
354                         seq_no += count;
355                         draw_flash = seq_no * 1000. / refresh_rate;
356                 }
357                 if( viewing != vicon )
358                         update_view();
359                 drawing_stopped();
360                 interrupted = -1;
361                 wdw->unlock_window();
362         }
363 }
364
365
366 void VIcon::init_audio(int audio_size)
367 {
368         this->audio_size = audio_size;
369         audio_data = new uint8_t[audio_size];
370         memset(audio_data, 0, audio_size);
371 }
372
373 void VIcon::dump(const char *dir)
374 {
375         mkdir(dir,0777);
376         for( int i=0; i<images.size(); ++i ) {
377                 char fn[1024];  sprintf(fn,"%s/img%05d.png",dir,i);
378                 printf("\r%s",fn);
379                 VFrame *img = *images[i];
380                 img->write_png(fn);
381         }
382         printf("\n");
383 }
384