From 83ce3e455446dbab64fbf54bc7e892204bf234f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 3 Jan 2024 10:12:37 +0800 Subject: [PATCH] Wallpaper (macOS): support Sonoma --- CMakeLists.txt | 2 +- src/detection/wallpaper/wallpaper_apple.c | 30 ---------- src/detection/wallpaper/wallpaper_apple.m | 72 +++++++++++++++++++++++ 3 files changed, 73 insertions(+), 31 deletions(-) delete mode 100644 src/detection/wallpaper/wallpaper_apple.c create mode 100644 src/detection/wallpaper/wallpaper_apple.m diff --git a/CMakeLists.txt b/CMakeLists.txt index de7abbfa..83d9105e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -601,7 +601,7 @@ elseif(APPLE) src/detection/theme/theme_nosupport.c src/detection/uptime/uptime_bsd.c src/detection/users/users_linux.c - src/detection/wallpaper/wallpaper_apple.c + src/detection/wallpaper/wallpaper_apple.m src/detection/wifi/wifi_apple.m src/detection/wm/wm_apple.c src/detection/de/de_nosupport.c diff --git a/src/detection/wallpaper/wallpaper_apple.c b/src/detection/wallpaper/wallpaper_apple.c deleted file mode 100644 index 87691abb..00000000 --- a/src/detection/wallpaper/wallpaper_apple.c +++ /dev/null @@ -1,30 +0,0 @@ -#include "wallpaper.h" -#include "common/settings.h" -#include "util/apple/osascript.h" - -const char* ffDetectWallpaper(FFstrbuf* result) -{ - // https://stackoverflow.com/questions/301215/getting-desktop-background-on-mac - - #ifdef FF_HAVE_SQLITE3 - - FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateCopy(&instance.state.platform.homeDir); - ffStrbufAppendS(&path, "Library/Application Support/Dock/desktoppicture.db"); - if (ffSettingsGetSQLite3String(path.chars, - "SELECT value\n" - "FROM preferences\n" - "JOIN data ON preferences.data_id=data.ROWID\n" - "JOIN pictures ON preferences.picture_id=pictures.ROWID\n" - "JOIN displays ON pictures.display_id=displays.ROWID\n" - "JOIN spaces ON pictures.space_id=spaces.ROWID\n" - "WHERE display_id=1 AND space_id=1 AND key=1", result) - ) - return NULL; - - #endif - - if (!ffOsascript("tell application \"Finder\" to get POSIX path of (get desktop picture as alias)", result)) - return "ffOsascript(get desktop picture) failed"; - - return NULL; -} diff --git a/src/detection/wallpaper/wallpaper_apple.m b/src/detection/wallpaper/wallpaper_apple.m new file mode 100644 index 00000000..eb330dda --- /dev/null +++ b/src/detection/wallpaper/wallpaper_apple.m @@ -0,0 +1,72 @@ +#include "wallpaper.h" +#include "common/settings.h" +#include "util/apple/osascript.h" + +#import + +const char* ffDetectWallpaper(FFstrbuf* result) +{ + { + // For Sonoma + // https://github.com/JohnCoates/Aerial/issues/1332 + NSError* error; + NSString* fileName = [NSString stringWithFormat:@"file://%s/Library/Application Support/com.apple.wallpaper/Store/Index.plist", instance.state.platform.homeDir.chars]; + NSDictionary* dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:fileName] + error:&error]; + if (!error) + { + NSArray* choices = [dict valueForKeyPath:@"SystemDefault.Desktop.Content.Choices"]; + if (choices.count > 0) + { + NSDictionary* choice = [choices objectAtIndex:0]; + NSArray* files = [choice valueForKey:@"Files"]; + if (files.count > 0) + { + NSString* file = [[files objectAtIndex: 0] valueForKey: @"relative"]; + ffStrbufAppendS(result, [NSURL URLWithString:file].path.UTF8String); + } + else + { + NSString* provider = [choice valueForKey:@"Provider"]; + NSString* builtinPrefix = @"com.apple.wallpaper.choice."; + if ([provider hasPrefix:builtinPrefix]) + provider = [provider substringFromIndex:builtinPrefix.length]; + if ([provider isEqualToString:@"sonoma"]) + ffStrbufSetStatic(result, "macOS Sonoma"); + else if ([provider isEqualToString:@"aerials"]) // Most builtin aerial wallpapers are private + ffStrbufSetStatic(result, "Built-in aerial photography"); + else + ffStrbufAppendF(result, "Built-in %s wallpaper", provider.UTF8String); + } + } + if (result->length > 0) + return NULL; + } + } + + #ifdef FF_HAVE_SQLITE3 + + { + // For Ventura + // https://stackoverflow.com/questions/301215/getting-desktop-background-on-mac + FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateCopy(&instance.state.platform.homeDir); + ffStrbufAppendS(&path, "Library/Application Support/Dock/desktoppicture.db"); + if (ffSettingsGetSQLite3String(path.chars, + "SELECT value\n" + "FROM preferences\n" + "JOIN data ON preferences.data_id=data.ROWID\n" + "JOIN pictures ON preferences.picture_id=pictures.ROWID\n" + "JOIN displays ON pictures.display_id=displays.ROWID\n" + "JOIN spaces ON pictures.space_id=spaces.ROWID\n" + "WHERE display_id=1 AND space_id=1 AND key=1", result) + ) + return NULL; + } + + #endif + + if (ffOsascript("tell application \"Finder\" to get POSIX path of (get desktop picture as alias)", result)) + return NULL; + + return "All detection methods failed"; +}