Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows using wallpaper files without extensions #55

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/helpers/System.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "System.hpp"
#include "../defines.hpp"
#include <array>

// Execute a shell command and get the output
std::string execAndGet(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
const std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
Debug::log(ERR, "execAndGet: failed in pipe");
return "";
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
5 changes: 5 additions & 0 deletions src/helpers/System.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "../includes.hpp"

std::string execAndGet(const char*);
24 changes: 12 additions & 12 deletions src/render/WallpaperTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ void CWallpaperTarget::create(const std::string& path) {
const auto BEGINLOAD = std::chrono::system_clock::now();

cairo_surface_t* CAIROSURFACE = nullptr;
const auto len = path.length()+36;
char cmd[len];
sprintf(cmd, "file -b --mime-type %s", path.c_str());
FILE *file = popen(cmd, "r");
char file_type[16];
fread(file_type, 1, 15, file);
file_type[15]='\0';
pclose(file);
Debug::log(LOG, "File: %s", file_type);
if (strncmp(file_type, "image/png", 9) == 0) { // file_type may have new line and EOF at the end
const std::string cmd = "file -b --mime-type " + path;
std::string file_type(execAndGet(cmd.c_str()));
if (file_type.contains("image/png")) { //Do not change to == as string has newline at the end
CAIROSURFACE = cairo_image_surface_create_from_png(path.c_str());
} else if (strncmp(file_type, "image/jpeg", 10) == 0) {
} else if (file_type.contains("image/jpeg")) {
CAIROSURFACE = JPEG::createSurfaceFromJPEG(path);
m_bHasAlpha = false;
} else {
}
else if (file_type == "")
{
Debug::log(CRIT, "file command didn't return anything, are you sure file is isntalled?");
vaxerski marked this conversation as resolved.
Show resolved Hide resolved
exit(1);
}
else {
Debug::log(CRIT, "unrecognized image %s", path.c_str());
Debug::log(LOG, "Returned file type is: %s", file_type.c_str());
exit(1);
}

Expand Down
3 changes: 1 addition & 2 deletions src/render/WallpaperTarget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

#include "../defines.hpp"
#include "../helpers/Jpeg.hpp"
#include <stdio.h>
#include <string.h>
#include "../helpers/System.hpp"

class CWallpaperTarget {
public:
Expand Down