mask mousewheel segv bug, mask opengl sw fallback read to ram, fix tiff config withou...
[goodguy/cinelerra.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 w, int h, double rate)
12 {
13         this->w = w;
14         this->h = h;
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->vw;
49         int sy0 = 0, sy1 = sy0 + vt->vh;
50         int dx0 = x, dx1 = dx0 + w;
51         int dy0 = y, dy1 = dy0 + h;
52         if( (x=vt->draw_x0-dx0) > 0 ) { sx0 += (x*vt->vw)/w;  dx0 = vt->draw_x0; }
53         if( (x=dx1-vt->draw_x1) > 0 ) { sx1 -= (x*vt->vw)/w;  dx1 = vt->draw_x1; }
54         if( (y=vt->draw_y0-dy0) > 0 ) { sy0 += (y*vt->vh)/h;  dy0 = vt->draw_y0; }
55         if( (y=dy1-vt->draw_y1) > 0 ) { sy1 -= (y*vt->vh)/h;  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
63 int VIconThread::cursor_inside(int x, int y)
64 {
65         if( !viewing ) return 0;
66         int vx = viewing->get_vx();
67         if( x < vx || x >= vx+vw ) return 0;
68         int vy = viewing->get_vy();
69         if( y < vy || y >= vy+vh ) return 0;
70         return 1;
71 }
72
73 void VIconThread::
74 set_drawing_area(int x0, int y0, int x1, int y1)
75 {
76         draw_x0 = x0;  draw_y0 = y0;
77         draw_x1 = x1;  draw_y1 = y1;
78 }
79
80 VIcon *VIconThread::low_vicon()
81 {
82         if( !t_heap.size() ) return 0;
83         VIcon *vip = t_heap[0];
84         remove_vicon(0);
85         return vip;
86 }
87
88 void VIconThread::remove_vicon(int i)
89 {
90         if( t_heap[i] == solo ) solo = 0;
91         int sz = t_heap.size();
92         for( int k; (k=2*(i+1)) < sz; i=k ) {
93                 if( t_heap[k]->age > t_heap[k-1]->age ) --k;
94                 t_heap[i] = t_heap[k];
95         }
96         VIcon *last = t_heap[--sz];
97         t_heap.remove_number(sz);
98         double age = last->age;
99         for( int k; i>0 && age<t_heap[k=(i-1)/2]->age; i=k )
100                 t_heap[i] = t_heap[k];
101         t_heap[i] = last;
102 }
103
104
105 VIconThread::
106 VIconThread(BC_WindowBase *wdw, int vw, int vh, int view_w, int view_h)
107  : Thread(1, 0, 0)
108 {
109         this->wdw = wdw;
110         this->vw = vw;         this->vh = vh;
111         this->view_w = view_w; this->view_h = view_h;
112         this->view_win = 0;    this->vicon = 0;
113         this->viewing = 0;     this->solo = 0;
114         this->draw_x0 = 0;     this->draw_x1 = wdw->get_w();
115         this->draw_y0 = 0;     this->draw_y1 = wdw->get_h();
116         draw_lock = new Condition(0, "VIconThread::draw_lock", 1);
117         timer = new Timer();
118         this->refresh_rate = VICON_RATE;
119         this->draw_flash = 0;
120         this->seq_no = 0;
121         this->now = 0;
122         done = 0;
123         interrupted = -1;
124         stop_age = 0;
125 }
126
127 VIconThread::
128 ~VIconThread()
129 {
130         stop_drawing();
131         done = 1;
132         draw_lock->unlock();
133         if( Thread::running() ) {
134                 Thread::cancel();
135         }
136         Thread::join();
137         t_heap.remove_all_objects();
138         delete timer;
139         delete draw_lock;
140 }
141
142 void VIconThread::
143 start_drawing()
144 {
145         wdw->lock_window("VIconThread::start_drawing");
146         if( view_win )
147                 wdw->set_active_subwindow(view_win);
148         if( interrupted < 0 )
149                 draw_lock->unlock();
150         timer->update();
151         timer->subtract(-stop_age);
152         interrupted = 0;
153         wdw->unlock_window();
154 }
155
156 void VIconThread::
157 stop_drawing()
158 {
159         wdw->lock_window("VIconThread::stop_drawing");
160         close_view_popup();
161         if( !interrupted )
162                 interrupted = 1;
163         stop_age = timer->get_difference();
164         wdw->unlock_window();
165 }
166
167 void VIconThread::
168 stop_viewing()
169 {
170         if( viewing ) {
171                 viewing->stop_audio();
172                 viewing = 0;
173         }
174 }
175
176 int VIconThread::keypress_event(int key)
177 {
178         if( key != ESC ) return 0;
179         close_view_popup();
180         return 1;
181 }
182
183 bool VIconThread::
184 visible(VIcon *vicon, int x, int y)
185 {
186         if( vicon->hidden ) return false;
187         if( y+vicon->h <= draw_y0 ) return false;
188         if( y >= draw_y1 ) return false;
189         if( x+vicon->w <= draw_x0 ) return false;
190         if( x >= draw_x1 ) return false;
191         return true;
192 }
193
194 int ViewPopup::keypress_event()
195 {
196         int key = get_keypress();
197         return vt->keypress_event(key);
198 }
199
200
201 ViewPopup::ViewPopup(VIconThread *vt, int x, int y, int w, int h)
202  : BC_Popup(vt->wdw, x, y, w, h, BLACK)
203 {
204         this->vt = vt;
205 }
206
207 ViewPopup::~ViewPopup()
208 {
209         vt->wdw->set_active_subwindow(0);
210 }
211
212 ViewPopup *VIconThread::new_view_window(ViewPopup *vpopup)
213 {
214         BC_WindowBase *parent = wdw->get_parent();
215         XineramaScreenInfo *info = parent->get_xinerama_info(-1);
216         int cx = info ? info->x_org + info->width/2 : parent->get_root_w(0)/2;
217         int cy = info ? info->y_org + info->height/2 : parent->get_root_h(0)/2;
218         int vx = viewing->get_vx(), rx = 0;
219         int vy = viewing->get_vy(), ry = 0;
220         wdw->get_root_coordinates(vx, vy, &rx, &ry);
221         rx += (rx >= cx ? -view_w+viewing->w/4 : viewing->w-viewing->w/4);
222         ry += (ry >= cy ? -view_h+viewing->h/4 : viewing->h-viewing->h/4);
223         if( vpopup )
224                 vpopup->reposition_window(rx, ry, view_w, view_h);
225         else
226                 vpopup = new ViewPopup(this, rx, ry, view_w, view_h);
227         wdw->set_active_subwindow(vpopup);
228         return vpopup;
229 }
230
231
232 void VIconThread::
233 reset_images()
234 {
235         for( int i=t_heap.size(); --i>=0; ) t_heap[i]->reset();
236         timer->update();
237         img_dirty = win_dirty = 0;
238 }
239
240 void VIconThread::add_vicon(VIcon *vip)
241 {
242         double age = vip->age;
243         int i = t_heap.size();  t_heap.append(vip);
244         for( int k; i>0 && age<t_heap[(k=(i-1)/2)]->age; i=k )
245                 t_heap[i] = t_heap[k];
246         t_heap[i] = vip;
247 }
248
249 int VIconThread::del_vicon(VIcon *vicon)
250 {
251         int i = t_heap.size();
252         while( --i >= 0 && t_heap[i] != vicon );
253         if( i < 0 ) return 0;
254         remove_vicon(i);
255         return 1;
256 }
257
258 void ViewPopup::draw_vframe(VFrame *frame)
259 {
260         if( !frame ) return;
261         BC_WindowBase::draw_vframe(frame, 0,0, get_w(),get_h());
262 }
263
264 void VIconThread::set_view_popup(VIcon *vicon)
265 {
266         if( viewing == vicon && !this->vicon ) return;
267         this->vicon = vicon;
268         if( interrupted ) update_view(vicon ? 1 : 0);
269
270 }
271
272 void VIconThread::close_view_popup()
273 {
274         set_view_popup(0);
275 }
276
277 int VIconThread::
278 update_view(int do_audio)
279 {
280         if( viewing ) viewing->stop_audio();
281         delete view_win;  view_win = 0;
282         if( (viewing=vicon) != 0 ) {
283                 view_win = new_view_window(0);
284                 view_win->draw_vframe(viewing->frame());
285                 view_win->flash(0);
286                 view_win->show_window();
287                 if( do_audio ) vicon->start_audio();
288         }
289         wdw->set_active_subwindow(view_win);
290         return 1;
291 }
292
293 int VIconThread::zoom_scale(int dir)
294 {
295         if( !viewing || !view_win ) return 0;
296         int view_h = this->view_h;
297         view_h += dir*view_h/10 + dir;
298         bclamp(view_h, 16,512);
299         this->view_h = view_h;
300         this->view_w = view_h * vw/vh;
301         new_view_window(view_win);
302         view_win->draw_vframe(viewing->frame());
303         view_win->flash(1);
304         return 1;
305 }
306
307
308 void VIconThread::
309 draw_images()
310 {
311         for( int i=0; i<t_heap.size(); ++i )
312                 draw(t_heap[i]);
313 }
314
315 void VIconThread::
316 flash()
317 {
318         if( !img_dirty && !win_dirty ) return;
319         if( img_dirty ) wdw->flash();
320         if( win_dirty && view_win ) view_win->flash();
321         win_dirty = img_dirty = 0;
322 }
323
324 int VIconThread::
325 draw(VIcon *vicon)
326 {
327         int x = vicon->get_vx(), y = vicon->get_vy();
328         int draw_img = visible(vicon, x, y);
329         int draw_win = view_win && viewing == vicon ? 1 : 0;
330         if( !draw_img && !draw_win ) return 0;
331         if( !vicon->frame() ) return 0;
332         if( draw_img ) {
333                 vicon->draw_vframe(this, wdw, x, y);
334                 img_dirty = 1;
335         }
336         if( draw_win ) {
337                 view_win->draw_vframe(vicon->frame());
338                 win_dirty = 1;
339         }
340         return 1;
341 }
342
343 void VIconThread::hide_vicons(int v)
344 {
345         for( int i=0; i<t_heap.size(); ++i ) {
346                 t_heap[i]->hidden = v;
347                 t_heap[i]->age = 0;
348         }
349 }
350
351 int VIconThread::show_vicon(VIcon *next)
352 {
353         now = timer->get_difference();
354         if( now >= draw_flash ) return 1;
355         draw(next);
356         if( !next->seq_no ) {
357                 next->cycle_start = now;
358                 if( next->playing_audio > 0 )
359                         next->start_audio();
360         }
361         int64_t ref_no = (now - next->cycle_start) / 1000. * refresh_rate;
362         int count = ref_no - next->seq_no;
363         if( count < 1 ) count = 1;
364         ref_no = next->seq_no + count;
365         next->age =  next->cycle_start + 1000. * ref_no / refresh_rate;
366         if( !next->set_seq_no(ref_no) )
367                 next->age = now + 1000.;
368         return 0;
369 }
370
371 void VIconThread::
372 run()
373 {
374         while(!done) {
375                 draw_lock->lock("VIconThread::run 0");
376                 if( done ) break;
377                 wdw->lock_window("BC_WindowBase::run 1");
378                 drawing_started();
379                 reset_images();
380                 draw_flash = 1000 / refresh_rate;
381                 seq_no = 0;  now = 0;
382                 while( !interrupted ) {
383                         if( viewing != vicon )
384                                 update_view(1);
385                         if( !solo ) {
386                                 VIcon *next = low_vicon();
387                                 while( next && next->age < draw_flash ) {
388                                         if( show_vicon(next) ) break;
389                                         add_vicon(next);
390                                         next = low_vicon();
391                                 }
392                                 if( !next ) break;
393                                 add_vicon(next);
394                                 if( draw_flash < now+1 )
395                                         draw_flash = now+1;
396                         }
397                         else
398                                 show_vicon(solo);
399                         wdw->unlock_window();
400                         while( !interrupted ) {
401                                 now = timer->get_difference();
402                                 int64_t ms = draw_flash - now;
403                                 if( ms <= 0 ) break;
404                                 if( ms > 100 ) ms = 100;
405                                 Timer::delay(ms);
406                         }
407                         wdw->lock_window("BC_WindowBase::run 2");
408                         now = timer->get_difference();
409                         int64_t late = now - draw_flash;
410                         if( late < 1000 ) flash();
411                         int64_t ref_no = now / 1000. * refresh_rate;
412                         int64_t count = ref_no - seq_no;
413                         if( count < 1 ) count = 1;
414                         seq_no += count;
415                         draw_flash = seq_no * 1000. / refresh_rate;
416                 }
417                 if( viewing != vicon )
418                         update_view(1);
419                 drawing_stopped();
420                 interrupted = -1;
421                 wdw->unlock_window();
422         }
423 }
424
425
426 void VIcon::init_audio(int audio_size)
427 {
428         this->audio_size = audio_size;
429         audio_data = new uint8_t[audio_size];
430         memset(audio_data, 0, audio_size);
431 }
432
433 void VIcon::dump(const char *dir)
434 {
435         mkdir(dir,0777);
436         for( int i=0; i<images.size(); ++i ) {
437                 char fn[1024];  sprintf(fn,"%s/img%05d.png",dir,i);
438                 printf("\r%s",fn);
439                 VFrame *img = *images[i];
440                 img->write_png(fn);
441         }
442         printf("\n");
443 }
444