1 /*****************************************************************************
2 * VLCEventManager.h: VLC.framework VLCEventManager implementation
3 * This is used to communicate in a sound way accross threads.
4 *****************************************************************************
5 * Copyright (C) 2007 Pierre d'Herbemont
6 * Copyright (C) 2007 the VideoLAN team
9 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #import "VLCEventManager.h"
29 static VLCEventManager * defaultManager = NULL;
34 VLCObjectMethodWithObjectArg
45 enum message_type_t type;
48 @interface VLCEventManager (Private)
49 - (void)callDelegateOfObjectAndSendNotificationWithArgs:(NSData*)data;
50 - (void)callObjectMethodWithArgs:(NSData*)data;
51 - (void)callDelegateOfObject:(id) aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName;
52 - (pthread_cond_t *)signalData;
53 - (pthread_mutex_t *)queueLock;
54 - (NSMutableArray *)messageQueue;
57 static void * EventDispatcherMainLoop(void * user_data)
59 VLCEventManager * self = user_data;
63 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
64 struct message * message, * message_newer = NULL;
68 /* Wait for some data */
69 pthread_mutex_lock( [self queueLock] );
71 /* Wait until we have something on the queue */
72 while([[self messageQueue] count] <= 0 )
74 pthread_cond_wait( [self signalData], [self queueLock] );
76 message = (struct message *)[(NSData *)[[self messageQueue] lastObject] bytes];
78 /* Don't send the same notification twice */
79 if( message->type == VLCNotification )
81 for( i = 0; i < [[self messageQueue] count]-1; i++ )
83 message_newer = (struct message *)[(NSData *)[[self messageQueue] objectAtIndex: i] bytes];
84 if( message_newer->type != VLCNotification )
87 if( message_newer->target == message->target && message_newer->target == message->target && [message_newer->u.name isEqualToString:message->u.name] )
89 [message_newer->target release];
90 [message->u.name release];
91 [[self messageQueue] removeObjectAtIndex: i];
97 dataMessage = [[self messageQueue] lastObject];
99 pthread_mutex_unlock( [self queueLock] );
101 if( message->type == VLCNotification )
102 [self performSelectorOnMainThread:@selector(callDelegateOfObjectAndSendNotificationWithArgs:) withObject:[dataMessage retain] /* released in the call */ waitUntilDone: NO];
104 [self performSelectorOnMainThread:@selector(callObjectMethodWithArgs:) withObject:[dataMessage retain] /* released in the call */ waitUntilDone: NO];
106 pthread_mutex_lock( [self queueLock] );
107 [[self messageQueue] removeLastObject];
108 pthread_mutex_unlock( [self queueLock] );
115 @implementation VLCEventManager
118 /* We do want a lock here to avoid leaks */
119 if ( !defaultManager )
121 defaultManager = [[VLCEventManager alloc] init];
124 return defaultManager;
129 if( self = [super init] )
131 pthread_mutex_init( &queueLock, NULL );
132 pthread_cond_init( &signalData, NULL );
133 pthread_create( &dispatcherThread, NULL, EventDispatcherMainLoop, self );
134 messageQueue = [[NSMutableArray alloc] initWithCapacity:10];
141 pthread_kill( dispatcherThread, SIGKILL );
142 pthread_join( dispatcherThread, NULL );
144 [messageQueue release];
148 - (void)callOnMainThreadDelegateOfObject:(id)aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
150 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
152 struct message message =
156 [aNotificationName retain],
160 if([NSThread isMainThread])
162 [self callDelegateOfObjectAndSendNotificationWithArgs:[[NSData dataWithBytes:&message length:sizeof(struct message)] retain] /* released in the call */];
166 pthread_mutex_lock( [self queueLock] );
167 [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(struct message)] atIndex:0];
168 pthread_cond_signal( [self signalData] );
169 pthread_mutex_unlock( [self queueLock] );
174 - (void)callOnMainThreadObject:(id)aTarget withMethod:(SEL)aSelector withArgumentAsObject: (id)arg
176 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
177 struct message message =
182 VLCObjectMethodWithObjectArg
185 if([NSThread isMainThread])
187 [self callObjectMethodWithArgs:[[NSData dataWithBytes:&message length:sizeof(struct message)] retain] /* released in the call */];
191 pthread_mutex_lock( [self queueLock] );
192 [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(struct message)] atIndex:0];
193 pthread_cond_signal( [self signalData] );
194 pthread_mutex_unlock( [self queueLock] );
200 @implementation VLCEventManager (Private)
201 - (void)callDelegateOfObjectAndSendNotificationWithArgs:(NSData*)data
203 struct message * message = (struct message *)[data bytes];
205 [self callDelegateOfObject:message->target withDelegateMethod:message->sel withNotificationName:message->u.name];
206 [message->u.name release];
207 [message->target release];
211 - (void)callObjectMethodWithArgs:(NSData*)data
213 struct message * message = (struct message *)[data bytes];
214 void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[message->target methodForSelector: message->sel];
216 method( message->target, message->sel, message->u.object);
217 [message->u.object release];
218 [message->target release];
222 - (NSMutableArray *)messageQueue
227 - (pthread_cond_t *)signalData
232 - (pthread_mutex_t *)queueLock
237 - (void)callDelegateOfObject:(id) aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
239 // [[NSNotificationCenter defaultCenter] postNotification: [NSNotification notificationWithName:aNotificationName object:aTarget]];
241 if (![aTarget delegate] || ![[aTarget delegate] respondsToSelector: aSelector])
244 void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[[aTarget delegate] methodForSelector: aSelector];
245 method( [aTarget delegate], aSelector, [NSNotification notificationWithName:aNotificationName object:aTarget]);