1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-23 03:32:32 +00:00

Attempts to use xdg-user-dir PICTURES in preference to $HOME for pictures.

This commit is contained in:
Thomas Harte 2018-07-28 09:14:18 -04:00
parent 85ce21c79f
commit 34aa78b7ce

View File

@ -7,11 +7,13 @@
//
#include <algorithm>
#include <array>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <memory>
#include <sys/stat.h>
#include <unistd.h>
#include <SDL2/SDL.h>
@ -281,6 +283,22 @@ std::string final_path_component(const std::string &path) {
return path.substr(final_slash+1, path.size() - final_slash - 1);
}
/*!
Executes @c command and returns its STDOUT.
*/
std::string system_get(const char *command) {
std::unique_ptr<FILE, decltype((pclose))> pipe(popen(command, "r"), pclose);
if(!pipe) return "";
std::string result;
while(!feof(pipe.get())) {
std::array<char, 256> buffer;
if (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
result += buffer.data();
}
return result;
}
}
int main(int argc, char *argv[]) {
@ -617,12 +635,16 @@ int main(int argc, char *argv[]) {
memcpy(&pixels[(window_height - 1 - y)*proportional_width*4], swap_buffer.data(), swap_buffer.size());
}
// Pick the directory for images. Try `xdg-user-dir PICTURES` first; failing that
// fall back on the HOME directory.
std::string target_directory = system_get("xdg-user-dir PICTURES");
if(target_directory.empty()) target_directory = getenv("HOME");
// Find the first available name of the form ~/clk-screenshot-<number>.bmp.
size_t index = 0;
const std::string home = getenv("HOME");
std::string target;
while(true) {
target = home + "/clk-screenshot-" + std::to_string(index) + ".bmp";
target = target_directory + "/clk-screenshot-" + std::to_string(index) + ".bmp";
struct stat file_stats;
if(stat(target.c_str(), &file_stats))