1 /*****************************************************************************
2 * VLCTime.h: VLC.framework VLCTime implementation
3 *****************************************************************************
4 * Copyright (C) 2007 Pierre d'Herbemont
5 * Copyright (C) 2007 the VideoLAN team
8 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #import "VLCEventManager.h"
28 static VLCEventManager * defaultManager = NULL;
33 VLCObjectMethodWithObjectArg
44 enum message_type_t type;
47 @interface VLCEventManager (Private)
48 - (void)callDelegateOfObjectAndSendNotificationWithArgs:(NSData*)data;
49 - (void)callObjectMethodWithArgs:(NSData*)data;
50 - (void)callDelegateOfObject:(id) aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName;
51 - (pthread_cond_t *)signalData;
52 - (pthread_mutex_t *)queueLock;
53 - (NSMutableArray *)messageQueue;
56 static void * EventDispatcherMainLoop(void * user_data)
58 VLCEventManager * self = user_data;
62 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
63 struct message * message, * message_newer = NULL;
67 /* Wait for some data */
68 pthread_mutex_lock( [self queueLock] );
70 /* Wait until we have something on the queue */
71 while([[self messageQueue] count] <= 0 )
73 pthread_cond_wait( [self signalData], [self queueLock] );
75 message = (struct message *)[(NSData *)[[self messageQueue] lastObject] bytes];
77 /* Don't send the same notification twice */
78 if( message->type == VLCNotification )
80 for( i = 0; i < [[self messageQueue] count]-1; i++ )
82 message_newer = (struct message *)[(NSData *)[[self messageQueue] objectAtIndex: i] bytes];
83 if( message_newer->type != VLCNotification )
86 if( message_newer->target == message->target && message_newer->target == message->target && [message_newer->u.name isEqualToString:message->u.name] )
88 [message_newer->target release];
89 [message->u.name release];
90 [[self messageQueue] removeObjectAtIndex: i];
96 dataMessage = [[self messageQueue] lastObject];
98 pthread_mutex_unlock( [self queueLock] );
100 if( message->type == VLCNotification )
101 [self performSelectorOnMainThread:@selector(callDelegateOfObjectAndSendNotificationWithArgs:) withObject:[dataMessage retain] waitUntilDone: NO];
103 [self performSelectorOnMainThread:@selector(callObjectMethodWithArgs:) withObject:[dataMessage retain] waitUntilDone: NO];
105 pthread_mutex_lock( [self queueLock] );
106 [[self messageQueue] removeLastObject];
107 pthread_mutex_unlock( [self queueLock] );
114 @implementation VLCEventManager
117 /* We do want a lock here to avoid leaks */
118 if ( !defaultManager )
120 defaultManager = [[VLCEventManager alloc] init];
123 return defaultManager;
128 if( self = [super init] )
130 pthread_mutex_init( &queueLock, NULL );
131 pthread_cond_init( &signalData, NULL );
132 pthread_create( &dispatcherThread, NULL, EventDispatcherMainLoop, self );
133 messageQueue = [[NSMutableArray alloc] initWithCapacity:10];
140 pthread_kill( dispatcherThread, SIGKILL );
141 pthread_join( dispatcherThread, NULL );
143 [messageQueue release];
147 - (void)callOnMainThreadDelegateOfObject:(id)aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
149 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
151 struct message message =
155 [aNotificationName retain],
159 pthread_mutex_lock( [self queueLock] );
160 [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(struct message)] atIndex:0];
161 pthread_cond_signal( [self signalData] );
162 pthread_mutex_unlock( [self queueLock] );
167 - (void)callOnMainThreadObject:(id)aTarget withMethod:(SEL)aSelector withArgumentAsObject: (id)arg
169 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
171 struct message message =
176 VLCObjectMethodWithObjectArg
179 pthread_mutex_lock( [self queueLock] );
180 [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(struct message)] atIndex:0];
181 pthread_cond_signal( [self signalData] );
182 pthread_mutex_unlock( [self queueLock] );
188 @implementation VLCEventManager (Private)
189 - (void)callDelegateOfObjectAndSendNotificationWithArgs:(NSData*)data
191 struct message * message = (struct message *)[data bytes];
193 [self callDelegateOfObject:message->target withDelegateMethod:message->sel withNotificationName:message->u.name];
194 [message->u.name release];
195 [message->target release];
199 - (void)callObjectMethodWithArgs:(NSData*)data
201 struct message * message = (struct message *)[data bytes];
202 void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[message->target methodForSelector: message->sel];
204 method( message->target, message->sel, message->u.object);
205 [message->u.object release];
206 [message->target release];
210 - (NSMutableArray *)messageQueue
215 - (pthread_cond_t *)signalData
220 - (pthread_mutex_t *)queueLock
225 - (void)callDelegateOfObject:(id) aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
227 // [[NSNotificationCenter defaultCenter] postNotification: [NSNotification notificationWithName:aNotificationName object:aTarget]];
229 if (![aTarget delegate] || ![[aTarget delegate] respondsToSelector: aSelector])
232 void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[[aTarget delegate] methodForSelector: aSelector];
233 method( [aTarget delegate], aSelector, [NSNotification notificationWithName:aNotificationName object:aTarget]);