Third set of 50 GPL attribution for CV-Contributors added +
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / remotecontrol.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2016-2020 William Morrow
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20
21 #include "bccolors.h"
22 #include "mainsession.h"
23 #include "mwindow.h"
24 #include "mwindowgui.h"
25 #include "record.h"
26 #include "remotecontrol.h"
27
28
29 RemoteWindow::RemoteWindow(RemoteControl *remote_control)
30  : BC_Window(_(PROGRAM_NAME ": RemoteWindow"),
31                 0, 0, xS(16), yS(16), -1, -1, 1, 0, 1)
32 {
33         this->remote_control = remote_control;
34 // *** CONTEXT_HELP ***
35         context_help_set_keyword("Remote Control for DVB");
36 }
37
38 RemoteWindow::~RemoteWindow()
39 {
40 }
41
42
43 RemoteControl::RemoteControl(MWindowGUI *mwindow_gui)
44  : Thread(1, 0, 0)
45 {
46         this->mwindow_gui = mwindow_gui;
47         this->handler = 0;
48         active_lock = new Mutex("RemoteControl::active_lock");
49
50         remote_window = new RemoteWindow(this);
51         Thread::start();
52         remote_window->init_wait();
53         gui = new RemoteGUI(remote_window, this);
54 }
55
56 void RemoteControl::run()
57 {
58         remote_window->run_window();
59 }
60
61 RemoteControl::~RemoteControl()
62 {
63         if( Thread::running() ) {
64                 remote_window->close(1);
65         }
66         Thread::join();
67         delete gui;
68         delete remote_window;
69         delete active_lock;
70 }
71
72
73
74 int RemoteControl::activate(RemoteHandler *handler)
75 {
76         int result = 0;
77         deactivate();
78         if( !handler )
79                 handler = !mwindow_gui->record->running() ?
80                         (RemoteHandler *)mwindow_gui->cwindow_remote_handler :
81                         (RemoteHandler *)mwindow_gui->record_remote_handler ;
82         active_lock->lock("RemoteControl::activate");
83         if( handler ) {
84                 gui->lock_window("RemoteControl::activate");
85                 gui->set_active(handler);
86                 gui->set_color(handler->color);
87                 gui->fill_color(handler->color);
88                 gui->unlock_window();
89                 result = 1;
90         }
91         active_lock->unlock();
92         return result;
93 }
94
95 int RemoteControl::deactivate()
96 {
97         int result = 0;
98         if( is_active() ) {
99                 active_lock->lock("RemoteControl::deactivate");
100                 if( is_active() ) {
101                         gui->set_inactive();
102                         result = 1;
103                 }
104                 active_lock->unlock();
105         }
106         return result;
107 }
108
109 int RemoteControl::remote_key(int key)
110 {
111         if( !is_active() ) return 0;
112         return handler->remote_key(key);
113 }
114
115 void RemoteControl::set_color(int color)
116 {
117         gui->lock_window("RemoteControl::fill_color");
118         gui->set_color(color);
119         gui->unlock_window();
120 }
121
122 void RemoteControl::fill_color(int color)
123 {
124         gui->lock_window("RemoteControl::fill_color");
125         gui->fill_color(color);
126         gui->unlock_window();
127 }
128
129 RemoteGUI::RemoteGUI(BC_WindowBase *wdw, RemoteControl *remote_control)
130  : BC_Popup(wdw, remote_control->mwindow_gui->mwindow->session->mwindow_x,0,
131                 xS(16),yS(16), -1, 1)
132 {
133         this->remote_control = remote_control;
134 }
135
136 RemoteGUI::~RemoteGUI()
137 {
138 }
139
140 void RemoteGUI::set_active(RemoteHandler *handler)
141 {
142         remote_control->handler = handler;
143         show_window();
144         grab_keyboard();
145 }
146
147 void RemoteGUI::set_inactive()
148 {
149         remote_control->handler = 0;
150         ungrab_keyboard();
151         hide_window();
152 }
153
154 void RemoteGUI::
155 fill_color(int color)
156 {
157         set_color(color);
158         draw_box(0, 0, get_w(), get_h());
159         flash();
160 }
161
162 void RemoteGUI::tile_windows(int config)
163 {
164         MWindow *mwindow = remote_control->mwindow_gui->mwindow;
165         if( config == mwindow->session->window_config ) return;
166         lock_window("RemoteGUI::tile_windows");
167         ungrab_keyboard();  hide_window();
168         mwindow->gui->lock_window("RemoteGUI::tile_windows 1");
169         int need_reload = mwindow->tile_windows(config);
170         mwindow->gui->unlock_window();
171         if( !need_reload ) {
172                 show_window();  raise_window();  grab_keyboard();
173                 reposition_window(mwindow->session->mwindow_x,0);
174                 unlock_window();
175         }
176         else {
177                 unlock_window();
178                 mwindow->restart_status = 1;
179                 mwindow->quit();
180         }
181 }
182
183 int RemoteGUI::button_press_event()
184 {
185         remote_control->deactivate();
186         return 1;
187 }
188
189 int RemoteGUI::keypress_event()
190 {
191         int key = get_keypress();
192         int result = remote_control->remote_key(key);
193         if( result < 0 ) {
194                 remote_control->deactivate();
195                 result = 0;
196         }
197         return result;
198 }
199
200 RemoteHandler::RemoteHandler(RemoteGUI *gui, int color)
201 {
202         this->gui = gui;
203         this->color = color;
204 }
205
206 RemoteHandler::~RemoteHandler()
207 {
208 }
209
210 int RemoteHandler::remote_key(int key)
211 {
212         gui->set_inactive();
213         return -1;
214 }
215
216 int RemoteHandler::process_key(int key)
217 {
218         return -1;
219 }
220