add haupauge-1657 dual usb capture support, add deinterlace to recordmonitor, asset...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / shbtnprefs.C
1 #include "bcwindowbase.h"
2 #include "bcdisplayinfo.h"
3 #include "bcdialog.h"
4 #include "awindow.h"
5 #include "awindowgui.h"
6 #include "cstrdup.h"
7 #include "indexable.h"
8 #include "language.h"
9 #include "mainerror.h"
10 #include "mainsession.h"
11 #include "mwindow.h"
12 #include "shbtnprefs.h"
13 #include "preferences.h"
14 #include "preferencesthread.h"
15 #include "theme.h"
16
17 #include <sys/wait.h>
18
19 ShBtnRun::ShBtnRun(const char *nm, const char *cmds, int warn)
20  : Thread(0, 0, 1)
21 {
22         argv.set_array_delete();
23         strncpy(name, nm, sizeof(name)-1);
24         name[sizeof(name)-1] = 0;
25         strncpy(commands, cmds, sizeof(commands)-1);
26         commands[sizeof(commands)-1] = 0;
27         this->warn = warn;
28 }
29 ShBtnRun::~ShBtnRun()
30 {
31         argv.remove_all_objects();
32 }
33
34 void ShBtnRun::add_arg(const char *v)
35 {
36         argv.append(cstrdup(v));
37 }
38
39 void ShBtnRun::run()
40 {
41         pid_t pid = vfork();
42         if( pid < 0 ) {
43                 perror("fork");
44                 return;
45         }
46         char msg[BCTEXTLEN];
47         if( !pid ) {
48                 argv.append(0);
49                 execvp(argv[0], &argv[0]);
50                 return;
51         }
52         // warn <0:always, =0:never, >0:on err
53         if( !warn ) return;
54         int stat;  waitpid(pid, &stat, 0);
55         if( !stat ) {
56                 if( warn > 0 ) return;
57                 sprintf(msg, "%s: completed", name);
58         }
59         else
60                 sprintf(msg, "%s: error exit status %d", name, stat);
61         MainError::show_error(msg);
62 }
63
64 ShBtnPref::ShBtnPref(const char *nm, const char *cmds, int warn, int run_script)
65 {
66         strncpy(name, nm, sizeof(name));
67         strncpy(commands, cmds, sizeof(commands));
68         this->warn = warn;
69         this->run_script = run_script;
70 }
71
72 ShBtnPref::~ShBtnPref()
73 {
74 }
75
76 void ShBtnPref::execute(ArrayList<Indexable*> &args)
77 {
78 // thread async+autodelete, no explicit delete
79         ShBtnRun *job = new ShBtnRun(name, commands, warn);
80         job->add_arg("/bin/bash");
81         job->add_arg(commands);
82         int n = args.size();
83         for( int i=0; i<n; ++i ) {
84                 Indexable *idxbl = args[i];
85                 if( !idxbl->is_asset ) continue;
86                 job->add_arg(idxbl->path);
87         }
88         job->start();
89 }
90
91 void ShBtnPref::execute()
92 {
93         ShBtnRun *job = new ShBtnRun(name, commands, warn);
94         job->add_arg("/bin/bash");
95         job->add_arg("-c");
96         job->add_arg(commands);
97         job->start();
98 }
99
100 ShBtnEditDialog::ShBtnEditDialog(PreferencesWindow *pwindow)
101  : BC_DialogThread()
102 {
103         this->pwindow = pwindow;
104 }
105
106 ShBtnEditDialog::~ShBtnEditDialog()
107 {
108         close_window();
109 }
110
111 BC_Window* ShBtnEditDialog::new_gui()
112 {
113         BC_DisplayInfo display_info;
114         int x = display_info.get_abs_cursor_x();
115         int y = display_info.get_abs_cursor_y();
116
117         sb_window = new ShBtnEditWindow(this, x, y);
118         sb_window->create_objects();
119         return sb_window;
120 }
121
122 void ShBtnEditDialog::handle_close_event(int result)
123 {
124         sb_window = 0;
125 }
126
127
128 ShBtnEditWindow::ShBtnEditWindow(ShBtnEditDialog *shbtn_edit, int x, int y)
129  : BC_Window(_(PROGRAM_NAME ": Shell"), x, y,
130                 xS(300), yS(200), xS(300), yS(200), 0, 0, 1)
131 {
132         this->shbtn_edit = shbtn_edit;
133         sb_dialog = 0;
134 }
135
136 ShBtnEditWindow::~ShBtnEditWindow()
137 {
138         delete sb_dialog;
139 }
140
141 int ShBtnEditWindow::list_update()
142 {
143         shbtn_items.remove_all_objects();
144         Preferences *preferences = shbtn_edit->pwindow->thread->preferences;
145         for( int i=0; i<preferences->shbtn_prefs.size(); ++i ) {
146                 shbtn_items.append(new ShBtnPrefItem(preferences->shbtn_prefs[i]));
147         }
148         return op_list->update(&shbtn_items, 0, 0, 1);
149 }
150
151 ShBtnAddButton::ShBtnAddButton(ShBtnEditWindow *sb_window, int x, int y)
152  : BC_GenericButton(x, y, _("Add"))
153 {
154         this->sb_window = sb_window;
155 }
156
157 ShBtnAddButton::~ShBtnAddButton()
158 {
159 }
160
161 int ShBtnAddButton::handle_event()
162 {
163
164         Preferences *preferences = sb_window->shbtn_edit->pwindow->thread->preferences;
165         ShBtnPref *pref = new ShBtnPref(_("new"), "", 0, 0);
166         preferences->shbtn_prefs.append(pref);
167         sb_window->list_update();
168         return sb_window->start_edit(pref);
169 }
170
171 ShBtnDelButton::ShBtnDelButton(ShBtnEditWindow *sb_window, int x, int y)
172  : BC_GenericButton(x, y, _("Del"))
173 {
174         this->sb_window = sb_window;
175 }
176
177 ShBtnDelButton::~ShBtnDelButton()
178 {
179 }
180
181 int ShBtnDelButton::handle_event()
182 {
183         ShBtnPrefItem *sp = (ShBtnPrefItem *)sb_window->op_list->get_selection(0,0);
184         if( !sp ) return 0;
185         Preferences *preferences = sb_window->shbtn_edit->pwindow->thread->preferences;
186         preferences->shbtn_prefs.remove(sp->pref);
187         sb_window->list_update();
188         return 1;
189 }
190
191 ShBtnEditButton::ShBtnEditButton(ShBtnEditWindow *sb_window, int x, int y)
192  : BC_GenericButton(x, y, _("Edit"))
193 {
194         this->sb_window = sb_window;
195 }
196
197 ShBtnEditButton::~ShBtnEditButton()
198 {
199 }
200
201 int ShBtnEditButton::handle_event()
202 {
203         ShBtnPrefItem *sp = (ShBtnPrefItem *)sb_window->op_list->get_selection(0,0);
204         if( !sp ) return 0;
205         return sb_window->start_edit(sp->pref);
206 }
207
208 ShBtnTextDialog::ShBtnTextDialog(ShBtnEditWindow *sb_window)
209  : BC_DialogThread()
210 {
211         this->sb_window = sb_window;
212         this->pref = 0;
213 }
214
215 ShBtnTextDialog::~ShBtnTextDialog()
216 {
217         close_window();
218 }
219
220 ShBtnTextWindow::ShBtnTextWindow(ShBtnEditWindow *sb_window, int x, int y)
221  : BC_Window(_(PROGRAM_NAME ": Commands"), x, y,
222                 xS(640), yS(160), xS(640), yS(150), 0, 0, 1)
223 {
224         this->sb_window = sb_window;
225         warn = sb_window->sb_dialog->pref->warn;
226         run_script = sb_window->sb_dialog->pref->run_script;
227 }
228
229 ShBtnTextWindow::~ShBtnTextWindow()
230 {
231 }
232
233
234 ShBtnErrWarnItem::ShBtnErrWarnItem(ShBtnErrWarn *popup,
235                 const char *text, int warn)
236  : BC_MenuItem(text)
237 {
238         this->popup = popup;
239         this->warn = warn;
240 }
241
242 int ShBtnErrWarnItem::handle_event()
243 {
244         popup->set_text(get_text());
245         popup->st_window->warn = warn;
246         return 1;
247 }
248
249 ShBtnErrWarn::ShBtnErrWarn(ShBtnTextWindow *st_window, int x, int y)
250  : BC_PopupMenu(x, y, xS(120), st_window->warn < 0 ? _("Always"):
251         !st_window->warn ? _("Never") : _("On Error"))
252 {
253         this->st_window = st_window;
254 }
255 ShBtnErrWarn::~ShBtnErrWarn()
256 {
257 }
258 int ShBtnErrWarn::handle_event()
259 {
260         return 0;
261 }
262
263 void ShBtnErrWarn::create_objects()
264 {
265         add_item(new ShBtnErrWarnItem(this,_("Always"), -1));
266         add_item(new ShBtnErrWarnItem(this,_("Never"), 0));
267         add_item(new ShBtnErrWarnItem(this,_("On Error"), 1));
268 }
269
270
271 ShBtnRunScript::ShBtnRunScript(ShBtnTextWindow *st_window, int x, int y)
272  : BC_CheckBox(x, y, &st_window->run_script, _("run /path/script.sh + argvs"))
273 {
274         this->st_window = st_window;
275 }
276
277 ShBtnRunScript::~ShBtnRunScript()
278 {
279 }
280
281 void ShBtnTextWindow::create_objects()
282 {
283         lock_window("ShBtnTextWindow::create_objects");
284         int x = xS(10), y = yS(10);
285         int x1 = xS(160);
286         BC_Title *title = new BC_Title(x, y, _("Label:"));
287         add_subwindow(title);
288         title = new BC_Title(x1, y, _("Commands:"));
289         add_subwindow(title);
290         y += title->get_h() + yS(8);
291         ShBtnPref *pref = sb_window->sb_dialog->pref;
292         cmd_name = new BC_TextBox(x, y, xS(140), 1, pref->name);
293         add_subwindow(cmd_name);
294         cmd_text = new BC_ScrollTextBox(this, x1, y, get_w()-x1-xS(20), 4, pref->commands);
295         cmd_text->create_objects();
296         y += cmd_text->get_h() + yS(16);
297         add_subwindow(title = new BC_Title(x1,y, _("OnExit Notify:")));
298         x1 += title->get_w() + xS(10);
299         add_subwindow(st_err_warn = new ShBtnErrWarn(this, x1, y));
300         st_err_warn->create_objects();
301         x1 += st_err_warn->get_w() + xS(20);
302         add_subwindow(st_run_script = new ShBtnRunScript(this, x1, y));
303         y = get_h() - ShBtnTextOK::calculate_h() - yS(10);
304         add_subwindow(new ShBtnTextOK(this, x, y));
305         show_window();
306         unlock_window();
307 }
308
309 ShBtnTextOK::ShBtnTextOK(ShBtnTextWindow *st_window, int x, int y)
310  : BC_OKButton(x, y)
311 {
312         this->st_window = st_window;
313 }
314
315 ShBtnTextOK::~ShBtnTextOK()
316 {
317 }
318
319 int ShBtnTextOK::handle_event()
320 {
321         ShBtnPref *pref = st_window->sb_window->sb_dialog->pref;
322         strcpy(pref->name, st_window->cmd_name->get_text());
323         strcpy(pref->commands, st_window->cmd_text->get_text());
324         pref->warn = st_window->warn;
325         pref->run_script = st_window->run_script;
326         return BC_OKButton::handle_event();
327 }
328
329
330 BC_Window *ShBtnTextDialog::new_gui()
331 {
332         BC_DisplayInfo display_info;
333         int x = display_info.get_abs_cursor_x();
334         int y = display_info.get_abs_cursor_y();
335
336         st_window = new ShBtnTextWindow(sb_window, x, y);
337         st_window->create_objects();
338         return st_window;
339 }
340
341 void ShBtnTextDialog::handle_close_event(int result)
342 {
343         if( !result ) {
344                 sb_window->lock_window("ShBtnTextDialog::handle_close_event");
345                 sb_window->list_update();
346                 sb_window->unlock_window();
347         }
348         st_window = 0;
349 }
350
351 int ShBtnTextDialog::start_edit(ShBtnPref *pref)
352 {
353         this->pref = pref;
354         start();
355         return 1;
356 }
357
358 void ShBtnEditWindow::create_objects()
359 {
360         lock_window("ShBtnEditWindow::create_objects");
361         Preferences *preferences = shbtn_edit->pwindow->thread->preferences;
362         for( int i=0; i<preferences->shbtn_prefs.size(); ++i ) {
363                 shbtn_items.append(new ShBtnPrefItem(preferences->shbtn_prefs[i]));
364         }
365         int x = xS(10), y = yS(10);
366         add_subwindow(op_list = new ShBtnPrefList(this, x, y));
367         x = xS(190);
368         add_subwindow(add_button = new ShBtnAddButton(this, x, y));
369         y += add_button->get_h() + yS(8);
370         add_subwindow(del_button = new ShBtnDelButton(this, x, y));
371         y += del_button->get_h() + yS(8);
372         add_subwindow(edit_button = new ShBtnEditButton(this, x, y));
373         add_subwindow(new BC_OKButton(this));
374         show_window();
375         unlock_window();
376 }
377
378 int ShBtnEditWindow::start_edit(ShBtnPref *pref)
379 {
380         if( !sb_dialog )
381                 sb_dialog = new ShBtnTextDialog(this);
382         return sb_dialog->start_edit(pref);
383 }
384
385
386 ShBtnPrefItem::ShBtnPrefItem(ShBtnPref *pref)
387  : BC_ListBoxItem(pref->name)
388 {
389         this->pref = pref;
390 }
391
392 ShBtnPrefItem::~ShBtnPrefItem()
393 {
394 }
395
396 ShBtnPrefList::ShBtnPrefList(ShBtnEditWindow *sb_window, int x, int y)
397  : BC_ListBox(x, y, xS(140), yS(100), LISTBOX_TEXT, &sb_window->shbtn_items, 0, 0)
398 {
399         this->sb_window = sb_window;
400 }
401
402 ShBtnPrefList::~ShBtnPrefList()
403 {
404 }
405
406 int ShBtnPrefList::handle_event()
407 {
408         return 1;
409 }
410
411 MainShBtnItem::MainShBtnItem(MainShBtns *shbtns, ShBtnPref *pref)
412  : BC_MenuItem(pref->name)
413 {
414         this->shbtns = shbtns;
415         this->pref = pref;
416 }
417
418 int MainShBtnItem::handle_event()
419 {
420         MWindow *mwindow = shbtns->mwindow;
421         if( pref->run_script ) {
422                 AWindowGUI *agui = mwindow->awindow->gui;
423                 agui->lock_window("MainShBtnItem::handle_event");
424                 mwindow->awindow->gui->collect_assets();
425                 pref->execute(*mwindow->session->drag_assets);
426                 agui->unlock_window();
427         }
428         else
429                 pref->execute();
430         return 1;
431 }
432
433 MainShBtns::MainShBtns(MWindow *mwindow, int x, int y)
434  : BC_PopupMenu(x, y, 0, "", -1, mwindow->theme->shbtn_data)
435 {
436         this->mwindow = mwindow;
437         set_tooltip(_("shell cmds"));
438 }
439
440 int MainShBtns::load(Preferences *preferences)
441 {
442         while( total_items() ) del_item(0);
443         for( int i=0; i<preferences->shbtn_prefs.size(); ++i )
444                 add_item(new MainShBtnItem(this, preferences->shbtn_prefs[i]));
445         return 0;
446 }
447
448 int MainShBtns::handle_event()
449 {
450         return 1;
451 }
452