/
animate.mm
233 lines (198 loc) · 7.98 KB
/
animate.mm
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* GreenPois0n Medicine - animate.mm
* Copyright (C) 2011 Chronic-Dev Team
* Copyright (C) 2011 Nicolas Haunold
* Copyright (C) 2011 Justin Williams
* Copyright (C) 2011 Alex Mault
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
#include <Foundation/Foundation.h>
#include <CoreGraphics/CoreGraphics.h>
#include <sys/sysctl.h>
#include <sys/param.h>
//#include <sys/user.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <IOKit/IOKitLib.h>
#include <mach/mach_traps.h>
#include "animate_frames.h"
int screenWidth, screenHeight;
pid_t getProcessId(const char *csProcessName){
struct kinfo_proc *sProcesses = NULL, *sNewProcesses;
pid_t iCurrentPid;
int aiNames[4];
size_t iNamesLength;
int i, iRetCode, iNumProcs;
size_t iSize;
iSize = 0;
aiNames[0] = CTL_KERN;
aiNames[1] = KERN_PROC;
aiNames[2] = KERN_PROC_ALL;
aiNames[3] = 0;
iNamesLength = 3;
iRetCode = sysctl(aiNames, iNamesLength, NULL, &iSize, NULL, 0);
do {
iSize += iSize / 10;
sNewProcesses = (kinfo_proc *)realloc(sProcesses, iSize);
if (sNewProcesses == 0) {
if (sProcesses)
free(sProcesses);
}
sProcesses = sNewProcesses;
iRetCode = sysctl(aiNames, iNamesLength, sProcesses, &iSize, NULL, 0);
} while (iRetCode == -1 && errno == ENOMEM);
iNumProcs = iSize / sizeof(struct kinfo_proc);
for (i = 0; i < iNumProcs; i++) {
iCurrentPid = sProcesses[i].kp_proc.p_pid;
if( strncmp(csProcessName, sProcesses[i].kp_proc.p_comm, MAXCOMLEN) == 0 ) {
free(sProcesses);
return iCurrentPid;
}
}
free(sProcesses);
return (-1);
}
CGContextRef fb_open() {
io_connect_t conn = NULL;
int bytesPerRow;
void *surfaceBuffer;
void *frameBuffer;
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
void *mfb_lib = dlopen("/System/Library/PrivateFrameworks/IOMobileFramebuffer.framework/IOMobileFramebuffer", 2);
void *cs_lib = dlopen("/System/Library/PrivateFrameworks/CoreSurface.framework/CoreSurface", 2);
int (*mfb_open)(io_service_t, task_port_t, uint32_t, io_connect_t*);
int (*mfb_layr)(io_service_t, int, void *);
int (*cs_lock)(void *, unsigned int);
int (*cs_unlock)(void *);
int (*cs_height)(void *);
int (*cs_width)(void *);
int (*cs_bytes)(void *);
void *(*cs_addr)(void *);
*(void **)(&mfb_open) = dlsym(mfb_lib, "IOMobileFramebufferOpen");
*(void **)(&mfb_layr) = dlsym(mfb_lib, "IOMobileFramebufferGetLayerDefaultSurface");
*(void **)(&cs_lock) = dlsym(cs_lib, "CoreSurfaceBufferLock");
*(void **)(&cs_unlock) = dlsym(cs_lib, "CoreSurfaceBufferUnlock");
*(void **)(&cs_height) = dlsym(cs_lib, "CoreSurfaceBufferGetHeight");
*(void **)(&cs_width) = dlsym(cs_lib, "CoreSurfaceBufferGetWidth");
*(void **)(&cs_bytes) = dlsym(cs_lib, "CoreSurfaceBufferGetBytesPerRow");
*(void **)(&cs_addr) = dlsym(cs_lib, "CoreSurfaceBufferGetBaseAddress");
io_service_t fb_service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleCLCD"));
if (!fb_service) {
fb_service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleM2CLCD"));
if (!fb_service) {
printf("Couldn't find framebuffer.\n");
return NULL;
}
}
(*mfb_open)(fb_service, mach_task_self(), 0, &conn);
(*mfb_layr)(conn, 0, &surfaceBuffer);
screenHeight = (*cs_height)(surfaceBuffer);
screenWidth = (*cs_width)(surfaceBuffer);
bytesPerRow = (*cs_bytes)(surfaceBuffer);
(*cs_lock)(surfaceBuffer, 3);
frameBuffer = (*cs_addr)(surfaceBuffer);
(*cs_unlock)(surfaceBuffer);
// create bitmap context
colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate(frameBuffer, screenWidth, screenHeight, 8, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
if(context == NULL) {
printf("Couldn't create screen context!\n");
return NULL;
}
CGColorSpaceRelease(colorSpace);
return context;
}
NSInteger firstNumSort(id str1, id str2, void *context) {
int num1 = [str1 integerValue];
int num2 = [str2 integerValue];
if (num1 < num2)
return NSOrderedAscending;
else if (num1 > num2)
return NSOrderedDescending;
return NSOrderedSame;
}
int main(int argc, char **argv, char **envp) {
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSString *value = nil;
if ([[NSFileManager defaultManager] fileExistsAtPath:@"/Library/BootLogos/org.chronic-dev.animate.plist"]) {
//NSDictionary *plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:@"/Library/BootLogos/org.chronic-dev.animate.plist"] retain];
//not using a dict atm... seems to be saving problems.
NSError *error;
value = [NSString stringWithContentsOfFile:@"/Library/BootLogos/org.chronic-dev.animate.plist" encoding:NSUTF8StringEncoding error:&error];
}
if ([value isEqualToString:@"apple"] || value == nil || (![value isEqualToString:@"default"] && ![[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/Library/BootLogos/%@/0.png", value]])) {
return 0; //Exit and display nothing
} else if ([value isEqualToString:@"default"]) {
anim_sequence *sp = seq;
while (sp->data != NULL) {
CGDataProviderRef dpr = CGDataProviderCreateWithData(NULL, sp->data, sp->size, NULL);
CGImageRef img = CGImageCreateWithPNGDataProvider(dpr, NULL, true, kCGRenderingIntentDefault);
[arr addObject:(id)img];
CGDataProviderRelease(dpr);
sp++;
}
} else { //Preload other iamges
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSString stringWithFormat:@"/Library/BootLogos/%@/", value] error:nil];
NSArray *onlyPNGs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"]];
onlyPNGs = [onlyPNGs sortedArrayUsingFunction:firstNumSort context:NULL];
unsigned int j = 0;
for (j = 0; j < [onlyPNGs count]; j++) {
CGDataProviderRef dpr = CGDataProviderCreateWithFilename([[NSString stringWithFormat:@"/Library/BootLogos/%@/%@", value, [onlyPNGs objectAtIndex:j]] UTF8String]);
CGImageRef img = CGImageCreateWithPNGDataProvider(dpr, NULL, true, kCGRenderingIntentDefault);
[arr addObject:(id)img];
CGDataProviderRelease(dpr);
}
}
CGContextRef c = NULL;
int j = 0;
while (c == NULL && j < 50) {
c = fb_open();
j++;
}
if (c == NULL)
return -1;
if (argc == 1) {
CGContextSetInterpolationQuality(c, kCGInterpolationLow);
unsigned int i;
for (i = 0; i < [arr count]; i++) {
CGImageRef bootimg = (CGImageRef)[arr objectAtIndex:i];
CGContextDrawImage(c, CGRectMake(0, 0, screenWidth, screenHeight), bootimg);
}
if (vfork() == 0) {
char *args[] = { "-", "-", NULL };
execve(argv[0], args, envp);
}
CGImageRef bootimg = (CGImageRef)[arr objectAtIndex:([arr count] - 1)];
for (i = 0; i < 25; i++) {
CGContextDrawImage(c, CGRectMake(0, 0, screenWidth, screenHeight), bootimg);
}
} else {
//Loop last frame
unsigned int i = [arr count] - 1;
while (getProcessId("SpringBoard") < 0) {
CGImageRef bootimg = (CGImageRef)[arr objectAtIndex:i];
CGContextDrawImage(c, CGRectMake(0, 0, screenWidth, screenHeight), bootimg);
} //Springboard will kill us eventually...
}
[arr release];
[p drain];
return 0;
}
// vim:ft=objc