This repository was archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.m
166 lines (142 loc) · 5.1 KB
/
main.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <unistd.h>
#include <Foundation/Foundation.h>
NSString *const kLockscreen = @"Lockscreen";
NSString *const kHomescreen = @"Homescreen";
NSString *const kBothscreens = @"";
const char *help = "Usage: framecli -s /path/to/video\nAllowed video formats: [.mp4, .m4a, .mov].\nNote: This tool only sets the primary video path.\n\nOptions:\n\t-s\tSet Both.\n\t-l\tSet Lockscreen.\n\t-m\tSet Homescreen.\n\t-h\tHelp.\n";
char *acceptedFormats[] = {".mp4", ".m4a", ".mov"};
void notifyFrame() {
CFNotificationCenterRef center = CFNotificationCenterGetDarwinNotifyCenter();
NSString *name = @"com.zx02.framepreferences.videoChanged";
CFStringRef str = (__bridge CFStringRef) name;
CFNotificationCenterPostNotification(center, str, nil, nil, YES);
}
// Moves the file to a permanent URL of the same extension and provided key and return it. Returns nil if move failed.
// Copied from framepreferences.
NSURL *getPermanentVideoURL(NSURL *srcURL, NSString *key) {
NSURL *frameFolder = [NSURL fileURLWithPath: @"/var/mobile/Documents/com.ZX02.Frame/"];
// Get the extension of the original file.
NSString *ext = srcURL.pathExtension.lowercaseString;
NSURL *newURL = [frameFolder URLByAppendingPathComponent: [NSString stringWithFormat: @"wallpaper%@.%@", key, ext]];
// Remove the old file should it exist.
[NSFileManager.defaultManager removeItemAtPath: newURL.path error: nil];
// Attempt to copy the tmp item to a permanent url.
NSError *err;
if ([NSFileManager.defaultManager copyItemAtPath: srcURL.path toPath: newURL.path error: &err]) {
return newURL;
}
if (err != nil)
NSLog(@"[FrameCLI] Error on Copy %@", err);
return nil;
}
// Updates user defaults with the provided videoURL, the provided filename key and the keyPath for which the videoURL should be set.
void setVideoURL(NSURL *videoURLOri, NSString *key, NSUserDefaults *bundleDefaults) {
// Try to copy the file at videoURL to an internal URL, return if failed.
NSURL *videoURL = nil;
// Separated to allow this function to accept nil as videoURLOri.
if (videoURLOri != nil) {
videoURL = getPermanentVideoURL(videoURLOri, key);
if (videoURL == nil)
return;
}
NSString *completeKeyPath = [@"videoURL" stringByAppendingString: key];
NSURL *sharedVideoURL = [bundleDefaults URLForKey: @"videoURL"];
// Update videoURLs by cases.
if (sharedVideoURL != nil) {
// Previously set shared video.
if ([key isEqualToString: kBothscreens]) {
// Override if setting a new shared video.
[bundleDefaults setURL: videoURL forKey: @"videoURL"];
}
else {
// Else make the original shared video the key other than the specified key.
if ([key isEqualToString: kHomescreen])
[bundleDefaults setURL: sharedVideoURL forKey: @"videoURLLockscreen"];
else
[bundleDefaults setURL: sharedVideoURL forKey: @"videoURLHomescreen"];
// Set the URL for the actual keyPath.
[bundleDefaults setURL: videoURL forKey: completeKeyPath];
// Cleanup.
[bundleDefaults removeObjectForKey: @"videoURL"];
}
}
else {
// Previously no (shared) video was set.
if ([key isEqualToString: kBothscreens]) {
// Setting shared video.
[bundleDefaults setURL: videoURL forKey: @"videoURL"];
// Cleanup.
[bundleDefaults removeObjectForKey: @"videoURLLockscreen"];
[bundleDefaults removeObjectForKey: @"videoURLHomescreen"];
}
else {
// Setting individual video.
[bundleDefaults setURL: videoURL forKey: completeKeyPath];
}
}
// Since videoURLs aren't being monitored, notify Frame by IPC.
notifyFrame();
}
// Wrapper for the entire logic of setting videoURLs.
void handleSetURL(char *filePath, NSString *key, NSUserDefaults *bundleDefaults) {
// safety checks
if (strlen(filePath) < 4 ||
access(filePath, F_OK) == -1) {
printf("Invalid file path.\n%s\n", help);
return;
}
// check if file type is allowed
char *fileExt = filePath + strlen(filePath)-4;
bool fileTypeAllowed = false;
for (int i=0; i<3; i++) {
if (strcmp(fileExt, acceptedFormats[i]) == 0) {
fileTypeAllowed = true;
break;
}
}
if (!fileTypeAllowed) {
printf("Unsupported file type: %s\n%s\n", fileExt, help);
}
NSURL *fileURL = [NSURL fileURLWithPath: [NSString stringWithUTF8String: filePath]];
setVideoURL(fileURL, key, bundleDefaults);
printf("Successfully set video to %s\n", filePath);
}
int main(int argc, char *argv[], char *envp[]) {
// print help if not arg provided
if (argc < 2) {
printf("%s\n", help);
return 0;
}
// load userDefaults
NSUserDefaults *bundleDefaults = [[NSUserDefaults alloc] initWithSuiteName: @"com.Zerui.framepreferences"];
// otherwise parse args
int opt;
while(opt = getopt(argc, argv, ":s:l:m:h"), opt != -1) {
switch(opt) {
// set file
case 's': {
handleSetURL(optarg, kBothscreens, bundleDefaults);
break;
}
case 'l': {
handleSetURL(optarg, kLockscreen, bundleDefaults);
break;
}
case 'm': {
handleSetURL(optarg, kHomescreen, bundleDefaults);
break;
}
// help / missing optarg
case 'h':
case ':':
printf("%s\n", help);
break;
// unknown opt
case '?':
printf("Unknown option %c.\n%s\n", optopt, help);
break;
}
}
return 0;
}