mirror of
https://github.com/hyprwm/hyprpaper.git
synced 2024-12-22 20:29:48 +01:00
[RFC] Filetype detection without extension (#77)
* first draft of filetype detection * Nix: add file dependency * Fix coding style mistake * Manually format my code --------- Co-authored-by: Mihai Fufezan <fufexan@protonmail.com>
This commit is contained in:
parent
64d0ebd666
commit
a1d9ab7584
3 changed files with 21 additions and 2 deletions
|
@ -64,6 +64,7 @@ target_link_libraries(hyprpaper
|
||||||
OpenGL
|
OpenGL
|
||||||
GLESv2
|
GLESv2
|
||||||
pthread
|
pthread
|
||||||
|
magic
|
||||||
${CMAKE_THREAD_LIBS_INIT}
|
${CMAKE_THREAD_LIBS_INIT}
|
||||||
${CMAKE_SOURCE_DIR}/wlr-layer-shell-unstable-v1-protocol.o
|
${CMAKE_SOURCE_DIR}/wlr-layer-shell-unstable-v1-protocol.o
|
||||||
${CMAKE_SOURCE_DIR}/xdg-shell-protocol.o
|
${CMAKE_SOURCE_DIR}/xdg-shell-protocol.o
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
cmake,
|
cmake,
|
||||||
ninja,
|
ninja,
|
||||||
cairo,
|
cairo,
|
||||||
|
file,
|
||||||
fribidi,
|
fribidi,
|
||||||
libdatrie,
|
libdatrie,
|
||||||
libjpeg,
|
libjpeg,
|
||||||
|
@ -34,6 +35,7 @@ stdenv.mkDerivation {
|
||||||
|
|
||||||
buildInputs = [
|
buildInputs = [
|
||||||
cairo
|
cairo
|
||||||
|
file
|
||||||
fribidi
|
fribidi
|
||||||
libdatrie
|
libdatrie
|
||||||
libjpeg
|
libjpeg
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#include "WallpaperTarget.hpp"
|
#include "WallpaperTarget.hpp"
|
||||||
|
|
||||||
|
#include <magic.h>
|
||||||
|
|
||||||
CWallpaperTarget::~CWallpaperTarget() {
|
CWallpaperTarget::~CWallpaperTarget() {
|
||||||
cairo_surface_destroy(m_pCairoSurface);
|
cairo_surface_destroy(m_pCairoSurface);
|
||||||
}
|
}
|
||||||
|
@ -16,10 +18,24 @@ void CWallpaperTarget::create(const std::string& path) {
|
||||||
} else if (path.find(".jpg") == len - 4 || path.find(".JPG") == len - 4 || path.find(".jpeg") == len - 5 || path.find(".JPEG") == len - 5) {
|
} else if (path.find(".jpg") == len - 4 || path.find(".JPG") == len - 4 || path.find(".jpeg") == len - 5 || path.find(".JPEG") == len - 5) {
|
||||||
CAIROSURFACE = JPEG::createSurfaceFromJPEG(path);
|
CAIROSURFACE = JPEG::createSurfaceFromJPEG(path);
|
||||||
m_bHasAlpha = false;
|
m_bHasAlpha = false;
|
||||||
|
} else {
|
||||||
|
// magic is slow, so only use it when no recognized extension is found
|
||||||
|
auto handle = magic_open(MAGIC_NONE|MAGIC_COMPRESS);
|
||||||
|
magic_load(handle, nullptr);
|
||||||
|
|
||||||
|
const auto type_str = std::string(magic_file(handle, path.c_str()));
|
||||||
|
const auto first_word = type_str.substr(0, type_str.find(" "));
|
||||||
|
|
||||||
|
if (first_word == "PNG") {
|
||||||
|
CAIROSURFACE = cairo_image_surface_create_from_png(path.c_str());
|
||||||
|
} else if (first_word == "JPEG") {
|
||||||
|
CAIROSURFACE = JPEG::createSurfaceFromJPEG(path);
|
||||||
|
m_bHasAlpha = false;
|
||||||
} else {
|
} else {
|
||||||
Debug::log(CRIT, "unrecognized image %s", path.c_str());
|
Debug::log(CRIT, "unrecognized image %s", path.c_str());
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (cairo_surface_status(CAIROSURFACE) != CAIRO_STATUS_SUCCESS) {
|
if (cairo_surface_status(CAIROSURFACE) != CAIRO_STATUS_SUCCESS) {
|
||||||
Debug::log(CRIT, "Failed to read image %s because of:\n%s", path.c_str(), cairo_status_to_string(cairo_surface_status(CAIROSURFACE)));
|
Debug::log(CRIT, "Failed to read image %s because of:\n%s", path.c_str(), cairo_status_to_string(cairo_surface_status(CAIROSURFACE)));
|
||||||
|
|
Loading…
Reference in a new issue