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