-/* [NSObject yieldToSelector:(withObject:)] {{{*/
-@interface NSObject (Cydia)
-- (id) yieldToSelector:(SEL)selector withObject:(id)object;
-- (id) yieldToSelector:(SEL)selector;
-@end
-
-@implementation NSObject (Cydia)
-
-- (void) doNothing {
-}
-
-- (void) _yieldToContext:(NSMutableArray *)context { _pooled
- SEL selector(reinterpret_cast<SEL>([[context objectAtIndex:0] pointerValue]));
- id object([[context objectAtIndex:1] nonretainedObjectValue]);
- volatile bool &stopped(*reinterpret_cast<bool *>([[context objectAtIndex:2] pointerValue]));
-
- /* XXX: deal with exceptions */
- id value([self performSelector:selector withObject:object]);
-
- NSMethodSignature *signature([self methodSignatureForSelector:selector]);
- [context removeAllObjects];
- if ([signature methodReturnLength] != 0 && value != nil)
- [context addObject:value];
-
- stopped = true;
-
- [self
- performSelectorOnMainThread:@selector(doNothing)
- withObject:nil
- waitUntilDone:NO
- ];
-}
-
-- (id) yieldToSelector:(SEL)selector withObject:(id)object {
- volatile bool stopped(false);
-
- NSMutableArray *context([NSMutableArray arrayWithObjects:
- [NSValue valueWithPointer:selector],
- [NSValue valueWithNonretainedObject:object],
- [NSValue valueWithPointer:const_cast<bool *>(&stopped)],
- nil]);
-
- NSThread *thread([[[NSThread alloc]
- initWithTarget:self
- selector:@selector(_yieldToContext:)
- object:context
- ] autorelease]);
-
- [thread start];
-
- NSRunLoop *loop([NSRunLoop currentRunLoop]);
- NSDate *future([NSDate distantFuture]);
- NSString *mode([loop currentMode] ?: NSDefaultRunLoopMode);
-
- while (!stopped && [loop runMode:mode beforeDate:future]);
-
- return [context count] == 0 ? nil : [context objectAtIndex:0];
-}
-
-- (id) yieldToSelector:(SEL)selector {
- return [self yieldToSelector:selector withObject:nil];
-}
-
-@end
-/* }}} */
-
-/* Cydia Alert View {{{ */
-@interface CYAlertView : UIAlertView {
- unsigned button_;
-}
-
-- (int) yieldToPopupAlertAnimated:(BOOL)animated;
-
-@end
-
-@implementation CYAlertView
-
-- (id) initWithTitle:(NSString *)title buttons:(NSArray *)buttons defaultButtonIndex:(int)index {
- if ((self = [super init]) != nil) {
- [self setTitle:title];
- [self setDelegate:self];
- for (NSString *button in buttons) [self addButtonWithTitle:button];
- [self setCancelButtonIndex:index];
- } return self;
-}
-
-- (void) _updateFrameForDisplay {
- [super _updateFrameForDisplay];
- if ([self cancelButtonIndex] == -1) {
- NSArray *buttons = [self buttons];
- if ([buttons count]) {
- UIImage *background = [[buttons objectAtIndex:0] backgroundForState:0];
- for (UIThreePartButton *button in buttons)
- [button setBackground:background forState:0];
- }
- }
-}
-
-- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
- button_ = buttonIndex + 1;
-}
-
-- (void) dismiss {
- [self dismissWithClickedButtonIndex:-1 animated:YES];
-}
-
-- (int) yieldToPopupAlertAnimated:(BOOL)animated {
- [self setRunsModal:YES];
- button_ = 0;
- [self show];
- return button_;
-}
-
-@end
-/* }}} */
-