VKeybd: Pass on primary (Ctrl/Cmd) shortcuts
[ardour.git] / gtk2_ardour / utils_videotl.cc
1 /*
2  * Copyright (C) 2013-2017 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2013-2018 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2015 AndrĂ© Nusser <andre.nusser@googlemail.com>
5  * Copyright (C) 2016 Tim Mayberry <mojofunk@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 #include <cstdio>
22 #include <string>
23 #include <cerrno>
24 #include <gtkmm.h>
25
26 #include "pbd/error.h"
27 #include "pbd/string_convert.h"
28
29 #include "ardour/ardour.h"
30 #include "ardour/session_directory.h"
31
32 #include "ardour_http.h"
33 #include "utils.h"
34 #include "utils_videotl.h"
35 #include "video_image_frame.h"
36
37 #ifdef WAF_BUILD
38 #include "gtk2ardour-version.h"
39 #endif
40
41 #ifndef ARDOUR_CURL_TIMEOUT
42 #define ARDOUR_CURL_TIMEOUT (60)
43 #endif
44 #include "pbd/i18n.h"
45
46 using namespace Gtk;
47 using namespace std;
48 using namespace PBD;
49 using namespace ARDOUR;
50 using namespace VideoUtils;
51
52 unsigned int VideoUtils::harvid_version = 0x0;
53
54 bool
55 VideoUtils::confirm_video_outfn (Gtk::Window& parent, std::string outfn, std::string docroot)
56 {
57         /* replace docroot's '/' to G_DIR_SEPARATOR for the comparison */
58         size_t look_here = 0;
59         size_t found_here;
60         const char ds = G_DIR_SEPARATOR;
61         while((found_here = docroot.find('/', look_here)) != string::npos) {
62                 docroot.replace(found_here, 1, std::string(&ds, 1));
63                 look_here = found_here + 1;
64         }
65
66         if (!docroot.empty() && docroot.compare(0, docroot.length(), outfn, 0, docroot.length())) {
67                 ArdourDialog confirm (_("Destination is outside Video Server's docroot. "), true);
68                 Label m (_("The destination file path is outside of the Video Server's docroot. The file will not be readable by the Video Server. Do you still want to continue?"));
69                 confirm.get_vbox()->pack_start (m, true, true);
70                 confirm.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
71                 confirm.add_button (_("Continue"), Gtk::RESPONSE_ACCEPT);
72                 confirm.show_all ();
73                 if (confirm.run() == RESPONSE_CANCEL) { return false; }
74         }
75
76         if (Glib::file_test(outfn, Glib::FILE_TEST_EXISTS)) {
77                 bool overwrite = ARDOUR_UI_UTILS::overwrite_file_dialog (parent,
78                                                                          _("Confirm Overwrite"),
79                                                                          _("A file with the same name already exists. Do you want to overwrite it?"));
80
81                 if (!overwrite) {
82                         return false;
83                 }
84         }
85
86         std::string dir = Glib::path_get_dirname (outfn);
87         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
88                 error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
89                 return false;
90         }
91         return true;
92 }
93
94 std::string
95 VideoUtils::video_dest_dir (const std::string sessiondir, const std::string docroot)
96 {
97         std::string dir = docroot;
98         if (dir.empty() || !dir.compare(0, dir.length(), sessiondir, 0, dir.length())) {
99                 dir=sessiondir;
100         }
101         if ((dir.empty() || dir.at(dir.length()-1) != G_DIR_SEPARATOR)) { dir += G_DIR_SEPARATOR; }
102
103         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
104                 error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
105         }
106         return dir;
107 }
108
109 std::string
110 VideoUtils::video_get_docroot (ARDOUR::RCConfiguration* config)
111 {
112         if (config->get_video_advanced_setup()) {
113                 return config->get_video_server_docroot();
114         }
115 #ifndef PLATFORM_WINDOWS
116         return X_("/");
117 #else
118         if (harvid_version >= 0x000802) { // 0.8.2
119                 return X_("");
120         } else {
121                 return X_("C:\\");
122         }
123 #endif
124 }
125
126 std::string
127 VideoUtils::video_get_server_url (ARDOUR::RCConfiguration* config)
128 {
129         if (config->get_video_advanced_setup()) {
130                 return config->get_video_server_url();
131         }
132         return X_("http://127.0.0.1:1554");
133 }
134
135
136 std::string
137 VideoUtils::strip_file_extension (const std::string infile)
138 {
139         std::string rv;
140         char *ext, *bn = strdup(infile.c_str());
141         if ((ext=strrchr(bn, '.'))) {
142                 if (!strchr(ext, G_DIR_SEPARATOR)) {
143                         *ext = 0;
144                 }
145         }
146         rv = std::string(bn);
147         free(bn);
148         return rv;
149 }
150
151 std::string
152 VideoUtils::get_file_extension (const std::string infile)
153 {
154         std::string rv = "";
155         char *ext, *bn = strdup(infile.c_str());
156         if ((ext=strrchr(bn, '.'))) {
157                 if (!strchr(ext, G_DIR_SEPARATOR)) {
158                         rv=std::string(ext+1);
159                 }
160         }
161         free(bn);
162         return rv;
163 }
164
165 std::string
166 VideoUtils::video_dest_file (const std::string dir, const std::string infile)
167 {
168         return Glib::build_filename(dir, strip_file_extension(Glib::path_get_basename(infile)) + ".avi");
169 }
170
171 std::string
172 VideoUtils::video_map_path (std::string server_docroot, std::string filepath)
173 {
174         std::string rv = filepath;
175
176         /* strip docroot */
177         if (server_docroot.length() > 0) {
178                 if (rv.compare(0, server_docroot.length(), server_docroot) == 0 ) {
179                         rv = rv.substr(server_docroot.length());
180                 }
181         }
182
183         /* replace all G_DIR_SEPARATOR with '/' */
184         size_t look_here = 0;
185         size_t found_here;
186         while((found_here = rv.find(G_DIR_SEPARATOR, look_here)) != string::npos) {
187                 rv.replace(found_here, 1, "/");
188                 look_here = found_here + 1;
189         }
190
191         CURL *curl;
192         char *ue;
193         curl = curl_easy_init();
194         ue = curl_easy_escape(curl, rv.c_str(),rv.length());
195         if (ue) {
196                 rv = std::string(ue);
197                 curl_free(ue);
198         }
199         curl_easy_cleanup(curl);
200
201         return rv;
202 }
203
204 void
205 VideoUtils::ParseCSV (const std::string &csv, std::vector<std::vector<std::string> > &lines)
206 {
207         bool inQuote(false);
208         bool newLine(false);
209         std::string field;
210         lines.clear();
211         std::vector<std::string> line;
212
213         std::string::const_iterator aChar = csv.begin();
214         while (aChar != csv.end()) {
215                 switch (*aChar) {
216                 case '"':
217                  newLine = false;
218                  inQuote = !inQuote;
219                  break;
220
221                 case ',':
222                  newLine = false;
223                  if (inQuote == true) {
224                                 field += *aChar;
225                  } else {
226                                 line.push_back(field);
227                                 field.clear();
228                  }
229                  break;
230
231                 case '\n':
232                 case '\r':
233                  if (inQuote == true) {
234                                 field += *aChar;
235                  } else {
236                                 if (newLine == false) {
237                                          line.push_back(field);
238                                          lines.push_back(line);
239                                          field.clear();
240                                          line.clear();
241                                          newLine = true;
242                                 }
243                  }
244                  break;
245
246                 default:
247                          newLine = false;
248                          field.push_back(*aChar);
249                          break;
250                 }
251                 aChar++;
252         }
253
254          if (field.size())
255                 line.push_back(field);
256
257          if (line.size())
258                 lines.push_back(line);
259 }
260
261 bool
262 VideoUtils::video_query_info (
263                 std::string video_server_url,
264                 std::string filepath,
265                 double &video_file_fps,
266                 long long int &video_duration,
267                 double &video_start_offset,
268                 double &video_aspect_ratio
269                 )
270 {
271         char url[2048];
272
273         snprintf(url, sizeof(url), "%s%sinfo/?file=%s&format=csv"
274                         , video_server_url.c_str()
275                         , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
276                         , filepath.c_str());
277         std::string res = ArdourCurl::http_get (url, false);
278         if (res.empty ()) {
279                 return false;
280         }
281
282         std::vector<std::vector<std::string> > lines;
283         ParseCSV(res, lines);
284
285         if (lines.empty() || lines.at(0).empty() || lines.at(0).size() != 6) {
286                 return false;
287         }
288         if (atoi(lines.at(0).at(0)) != 1) return false; // version
289         video_start_offset = 0.0;
290         video_aspect_ratio = string_to<double>(lines.at(0).at(3));
291         video_file_fps = string_to<double>(lines.at(0).at(4));
292         video_duration = string_to<int64_t>(lines.at(0).at(5));
293
294         if (video_aspect_ratio < 0.01 || video_file_fps < 0.01) {
295                 /* catch errors early, aspect == 0 or fps == 0 will
296                  * wreak havoc down the road */
297                 return false;
298         }
299         return true;
300 }
301
302 void
303 VideoUtils::video_draw_cross (Glib::RefPtr<Gdk::Pixbuf> img)
304 {
305
306         int rowstride = img->get_rowstride();
307         int n_channels = img->get_n_channels();
308         guchar *pixels, *p;
309         pixels = img->get_pixels();
310
311         int x,y;
312         int clip_width = img->get_width();
313         int clip_height = img->get_height();
314
315         for (x=0;x<clip_width;x++) {
316                 y = clip_height * x / clip_width;
317                 p = pixels + y * rowstride + x * n_channels;
318                 p[0] = 192; p[1] = 192; p[2] = 192;
319                 if (n_channels>3) p[3] = 255;
320                 p = pixels + y * rowstride + (clip_width-x-1) * n_channels;
321                 p[0] = 192; p[1] = 192; p[2] = 192;
322                 if (n_channels>3) p[3] = 255;
323         }
324 }
325