]> git.sesse.net Git - nageru/blobdiff - image_input.cpp
Make search_for_file() understand URLs.
[nageru] / image_input.cpp
index 9f474c94b14ae2b8bf315e0d0a4b2308f153b8f3..a3a110c6ca293ae8beb12525f574c2c9cf6ad4c1 100644 (file)
@@ -37,10 +37,24 @@ struct SwsContext;
 
 using namespace std;
 
-namespace {
-
 string search_for_file(const string &filename)
 {
+       if (!filename.empty() && filename[0] == '/') {
+               // Absolute path.
+               return filename;
+       }
+
+       // See if we match ^[a-z]:/, which is probably a URL of some sort
+       // (FFmpeg understands various forms of these).
+       for (size_t i = 0; i < filename.size() - 1; ++i) {
+               if (filename[i] == ':' && filename[i + 1] == '/') {
+                       return filename;
+               }
+               if (!isalpha(filename[i])) {
+                       break;
+               }
+       }
+
        // Look for the file in all theme_dirs until we find one;
        // that will be the permanent resolution of this file, whether
        // it is actually valid or not.
@@ -61,24 +75,33 @@ string search_for_file(const string &filename)
        for (const string &error : errors) {
                fprintf(stderr, "%s\n", error.c_str());
        }
-       fprintf(stderr, "Couldn't find %s in any directory in --theme-dirs, exiting.\n",
-               filename.c_str());
-       exit(1);
+       return "";
 }
 
-}  // namespace
+string search_for_file_or_die(const string &filename)
+{
+       string pathname = search_for_file(filename);
+       if (pathname.empty()) {
+               fprintf(stderr, "Couldn't find %s in any directory in --theme-dirs, exiting.\n",
+                       filename.c_str());
+               exit(1);
+       }
+       return pathname;
+}
 
 ImageInput::ImageInput(const string &filename)
        : movit::FlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA,
-                          GL_UNSIGNED_BYTE, 1280, 720),  // FIXME
+                          GL_UNSIGNED_BYTE, 1280, 720),  // Resolution will be overwritten.
          filename(filename),
-         pathname(search_for_file(filename)),
+         pathname(search_for_file_or_die(filename)),
          current_image(load_image(filename, pathname))
 {
        if (current_image == nullptr) {  // Could happen even though search_for_file() returned.
                fprintf(stderr, "Couldn't load image, exiting.\n");
                exit(1);
        }
+       set_width(current_image->width);
+       set_height(current_image->height);
        set_pixel_data(current_image->pixels.get());
 }
 
@@ -206,7 +229,6 @@ shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pat
                return nullptr;
        }
 
-       // TODO: Scale down if needed!
        uint8_t *pic_data[4] = {nullptr};
        unique_ptr<uint8_t *, decltype(av_freep)*> pic_data_cleanup(
                &pic_data[0], av_freep);
@@ -230,7 +252,7 @@ shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pat
        unique_ptr<uint8_t[]> image_data(new uint8_t[len]);
        av_image_copy_to_buffer(image_data.get(), len, pic_data, linesizes, AV_PIX_FMT_RGBA, frame->width, frame->height, 1);
 
-       shared_ptr<Image> image(new Image{move(image_data), last_modified});
+       shared_ptr<Image> image(new Image{unsigned(frame->width), unsigned(frame->height), move(image_data), last_modified});
        return image;
 }
 
@@ -245,7 +267,10 @@ void ImageInput::update_thread_func(const std::string &filename, const std::stri
        timespec last_modified = first_modified;
        struct stat buf;
        for ( ;; ) {
-               sleep(1);
+               {
+                       unique_lock<mutex> lock(threads_should_quit_mu);
+                       threads_should_quit_modified.wait_for(lock, chrono::seconds(1), []() { return threads_should_quit; });
+               }
 
                if (threads_should_quit) {
                        return;
@@ -274,9 +299,11 @@ void ImageInput::update_thread_func(const std::string &filename, const std::stri
 
 void ImageInput::shutdown_updaters()
 {
-       // TODO: Kick these out of the sleep before one second?
-       threads_should_quit = true;
-
+       {
+               unique_lock<mutex> lock(threads_should_quit_mu);
+               threads_should_quit = true;
+               threads_should_quit_modified.notify_all();
+       }
        for (auto &it : update_threads) {
                it.second.join();
        }
@@ -285,4 +312,6 @@ void ImageInput::shutdown_updaters()
 mutex ImageInput::all_images_lock;
 map<string, shared_ptr<const ImageInput::Image>> ImageInput::all_images;
 map<string, thread> ImageInput::update_threads;
-volatile bool ImageInput::threads_should_quit = false;
+mutex ImageInput::threads_should_quit_mu;
+bool ImageInput::threads_should_quit = false;
+condition_variable ImageInput::threads_should_quit_modified;