0xced (owner)

Forks

Revisions

gist: 228140 Download_button fork
public
Description:
CLAlert
Public Clone URL: git://gist.github.com/228140.git
Embed All Files: show embed
CLAlert.h #
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
/*
* CLAlert is a drop-in replacement for NSAlert
*
* A CLAlert is exactly the same as a NSAlert, except for the alert style behavior
*
* - An alert with the informational style (NSInformationalAlertStyle) will always display a "Note icon" (kAlertNoteIcon)
* - An alert with the warning style (NSWarningAlertStyle) will always display a "Caution icon" (kAlertCautionIcon)
* - An alert with the critical style (NSCriticalAlertStyle) will always display a "Stop icon" (kAlertStopIcon)
*
* Tested on Mac OS X 10.5.8 and 10.6.1
*/
 
#import <AppKit/NSAlert.h>
 
@interface CLAlert : NSAlert {}
@end
 
/*
Licensed under the MIT License
 
Copyright (c) 2009 Cédric Luthi
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
 
CLAlert.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
#import "CLAlert.h"
 
#import <CoreServices/CoreServices.h>
#import <objc/runtime.h>
#import <mach/mach_init.h>
#import <mach/vm_map.h>
#import <libkern/OSCacheControl.h>
 
@implementation CLAlert
 
#if defined(__i386__) || defined(__x86_64__)
#define isIntel
#elif defined(__ppc__) || defined(__ppc64__)
#define isPowerPC
#else
#error Architecture not supported
#endif
 
#if defined(isIntel)
typedef char* instrPtr;
#elif defined(isPowerPC)
typedef uint32_t* instrPtr;
#endif
 
static OSType previousIconType = kAlertCautionIcon;
 
- (BOOL) patchBuildAlert:(NSAlertStyle)alertStyle
{
BOOL isPatched = NO;
 
Method buildAlertMethod = class_getInstanceMethod([super class], @selector(buildAlertStyle:title:formattedMsg:first:second:third:oldStyle:));
if (buildAlertMethod) {
const IMP buildAlertIMP = method_getImplementation(buildAlertMethod);
instrPtr buildAlertPtr = (instrPtr)buildAlertIMP;
// As of 10.5.8, i386 offset = 85, x86_64 offset = 107, ppc offset = 104 and ppc64 offset = 180
// So 360 should be enough to accomodate a few changes in the implementation
const instrPtr buildAlertStop = buildAlertPtr + 360;
 
BOOL isPatchSafe = NO;
 
#if defined(isIntel)
OSType *iconType = NULL;
while (buildAlertPtr < buildAlertStop) {
if (memcmp(buildAlertPtr, &previousIconType, sizeof(previousIconType)) == 0) {
iconType = (OSType*)buildAlertPtr;
break;
}
buildAlertPtr++;
}
isPatchSafe = (iconType != NULL);
#elif defined(isPowerPC)
instrPtr loadHi = NULL;
instrPtr loadLo = NULL;
const uint32_t expectedHiInstr = 0x3ca00000 | (previousIconType >> 16); // lis r5,0x....
const uint32_t expectedLoInstr = 0x60a50000 | (previousIconType & 0xFFFF); // ori r5,r5,0x....
 
while (buildAlertPtr < buildAlertStop) {
if (*buildAlertPtr == expectedHiInstr) {
loadHi = buildAlertPtr;
} else if (*buildAlertPtr == expectedLoInstr) {
loadLo = buildAlertPtr;
break;
}
buildAlertPtr++;
}
isPatchSafe = ((loadLo > loadHi) && (loadHi != NULL) && (loadLo != NULL));
#endif
 
if (isPatchSafe) {
const kern_return_t vm_err = vm_protect(mach_task_self(), (vm_address_t)buildAlertIMP, 360, false, VM_PROT_ALL);
if (vm_err == KERN_SUCCESS) {
OSType alertType = kUnknownType;
if (alertStyle == NSInformationalAlertStyle) {
alertType = kAlertNoteIcon;
} else if (alertStyle == NSWarningAlertStyle) {
alertType = kAlertCautionIcon;
} else if (alertStyle == NSCriticalAlertStyle) {
alertType = kAlertStopIcon;
}
 
if (alertType != kUnknownType) {
isPatched = YES;
#if defined(isIntel)
*iconType = alertType;
sys_icache_invalidate(iconType, sizeof(*iconType));
#elif defined(isPowerPC)
*loadHi = 0x3ca00000 | (alertType >> 16);
*loadLo = 0x60a50000 | (alertType & 0xFFFF);
sys_icache_invalidate(loadHi, sizeof(*loadHi));
sys_icache_invalidate(loadLo, sizeof(*loadLo));
#else
isPatched = NO;
#endif
previousIconType = alertType;
}
}
}
}
 
return isPatched;
}
 
- (void) layout
{
const NSAlertStyle superStyle = [self alertStyle];
 
if ([self patchBuildAlert:superStyle]) {
[self setAlertStyle:NSCriticalAlertStyle];
[super layout];
[self setAlertStyle:superStyle];
} else {
[super layout];
}
}
 
// MARK: Restoration of NSAlert default behavior with caution icon
 
- (NSInteger) runModal
{
const NSInteger response = [super runModal];
[self patchBuildAlert:NSWarningAlertStyle];
return response;
}
 
static id superModalDelegate = nil;
static SEL superDidEndSelector = NULL;
 
- (void) beginSheetModalForWindow:(NSWindow *)window modalDelegate:(id)modalDelegate didEndSelector:(SEL)alertDidEndSelector contextInfo:(void *)contextInfo
{
superModalDelegate = modalDelegate;
superDidEndSelector = alertDidEndSelector;
[super beginSheetModalForWindow:window modalDelegate:self didEndSelector:@selector(cla:clr:clc:) contextInfo:contextInfo];
}
 
- (void) cla:(NSAlert *)alert clr:(int)returnCode clc:(void *)contextInfo
{
[self patchBuildAlert:NSWarningAlertStyle];
objc_msgSend(superModalDelegate, superDidEndSelector, alert, returnCode, contextInfo);
}
 
@end