]> git.sesse.net Git - vlc/blob - modules/gui/macosx/SPInvocationGrabbing.m
macosx: redesigned info panel to HUD
[vlc] / modules / gui / macosx / SPInvocationGrabbing.m
1 /*
2  Copyright (c) 2011, Joachim Bengtsson
3  All rights reserved.
4  
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6  
7  * Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
8  
9  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10  */
11
12 #import "SPInvocationGrabbing.h"
13 #import <execinfo.h>
14
15 #pragma mark Invocation grabbing
16 @interface SPInvocationGrabber ()
17 @property (readwrite, retain, nonatomic) id object;
18 @property (readwrite, retain, nonatomic) NSInvocation *invocation;
19
20 @end
21
22 @implementation SPInvocationGrabber
23 - (id)initWithObject:(id)obj;
24 {
25         return [self initWithObject:obj stacktraceSaving:YES];
26 }
27
28 -(id)initWithObject:(id)obj stacktraceSaving:(BOOL)saveStack;
29 {
30         self.object = obj;
31
32         if(saveStack)
33                 [self saveBacktrace];
34
35         return self;
36 }
37 -(void)dealloc;
38 {
39         free(frameStrings);
40         self.object = nil;
41         self.invocation = nil;
42         [super dealloc];
43 }
44 @synthesize invocation = _invocation, object = _object;
45
46 @synthesize backgroundAfterForward, onMainAfterForward, waitUntilDone;
47 - (void)runInBackground;
48 {
49         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
50         @try {
51                 [self invoke];
52         }
53         @finally {
54                 [pool drain];
55         }
56 }
57
58
59 - (void)forwardInvocation:(NSInvocation *)anInvocation {
60         [anInvocation retainArguments];
61         anInvocation.target = _object;
62         self.invocation = anInvocation;
63         
64         if(backgroundAfterForward)
65                 [NSThread detachNewThreadSelector:@selector(runInBackground) toTarget:self withObject:nil];
66         else if(onMainAfterForward)
67         [self performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:waitUntilDone];
68 }
69 - (NSMethodSignature *)methodSignatureForSelector:(SEL)inSelector {
70         NSMethodSignature *signature = [super methodSignatureForSelector:inSelector];
71         if (signature == NULL)
72                 signature = [_object methodSignatureForSelector:inSelector];
73     
74         return signature;
75 }
76
77 - (void)invoke;
78 {
79
80         @try {
81                 [_invocation invoke];
82         }
83         @catch (NSException * e) {
84                 NSLog(@"SPInvocationGrabber's target raised %@:\n\t%@\nInvocation was originally scheduled at:", e.name, e);
85                 [self printBacktrace];
86                 printf("\n");
87                 [e raise];
88         }
89
90         self.invocation = nil;
91         self.object = nil;
92 }
93
94 -(void)saveBacktrace;
95 {
96   void *backtraceFrames[128];
97   frameCount = backtrace(&backtraceFrames[0], 128);
98   frameStrings = backtrace_symbols(&backtraceFrames[0], frameCount);
99 }
100 -(void)printBacktrace;
101 {
102         for(int x = 3; x < frameCount; x++) {
103                 if(frameStrings[x] == NULL) { break; }
104                 printf("%s\n", frameStrings[x]);
105         }
106 }
107 @end
108
109 @implementation NSObject (SPInvocationGrabbing)
110 -(id)grab;
111 {
112         return [[[SPInvocationGrabber alloc] initWithObject:self] autorelease];
113 }
114 -(id)invokeAfter:(NSTimeInterval)delta;
115 {
116         id grabber = [self grab];
117         [NSTimer scheduledTimerWithTimeInterval:delta target:grabber selector:@selector(invoke) userInfo:nil repeats:NO];
118         return grabber;
119 }
120 - (id)nextRunloop;
121 {
122         return [self invokeAfter:0];
123 }
124 -(id)inBackground;
125 {
126     SPInvocationGrabber *grabber = [self grab];
127         grabber.backgroundAfterForward = YES;
128         return grabber;
129 }
130 -(id)onMainAsync:(BOOL)async;
131 {
132     SPInvocationGrabber *grabber = [self grab];
133         grabber.onMainAfterForward = YES;
134     grabber.waitUntilDone = !async;
135         return grabber;
136 }
137
138 @end