]> git.sesse.net Git - vlc/blob - extras/MacOSX/Framework/Sources/VLCEventManager.m
MacOSX/Framework/VLCEventManager: Code clean up/documentation. Standardized the...
[vlc] / extras / MacOSX / Framework / Sources / VLCEventManager.m
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
7  * $Id$
8  *
9  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 #import "VLCEventManager.h"
27 #import <pthread.h>
28
29 /**
30  * Defines the type of interthread message on the queue.
31  */
32 typedef enum
33 {
34     VLCNotification,                //< Standard NSNotification.
35     VLCObjectMethodWithObjectArg,   //< Method with an object argument.
36     VLCObjectMethodWithArrayArg     //< Method with an array argument.
37 } message_type_t;
38
39 /**
40  * Data structured used to enqueue messages onto the queue.
41  */
42 typedef struct {
43     id target;                      //< Target object that should receive the message (retained until method is called).
44     SEL sel;                        //< A selector that identifies the message to be sent to the target.
45     union u                         //< Object could either be a VLCNotification or other.
46     {
47         NSString * name;            //< Name to be used for NSNotification
48         id object;                  //< Object argument to pass to the target via the selector.
49     } u;
50     message_type_t type;            //< Type of queued message.
51 } message_t;
52
53 @interface VLCEventManager (Private)
54 - (void)callDelegateOfObjectAndSendNotificationWithArgs:(NSData*)data;
55 - (void)callObjectMethodWithArgs:(NSData*)data;
56 - (void)callDelegateOfObject:(id) aTarget withDelegateMethod:(SEL)aSelector withNotificationName:(NSString *)aNotificationName;
57 - (pthread_cond_t *)signalData;
58 - (pthread_mutex_t *)queueLock;
59 - (NSMutableArray *)messageQueue;
60 @end
61
62 /**
63  * Provides a function for the main entry point for the dispatch thread.  It dispatches any messages that is queued.
64  * \param user_data Pointer to the VLCEventManager instance that instiated this thread.
65  */
66 static void * EventDispatcherMainLoop(void * user_data)
67 {
68     VLCEventManager * self = user_data;
69     
70     for(;;)
71     {
72         NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
73         message_t * message, * message_newer = NULL;
74         NSData * dataMessage;
75         int i;
76
77         /* Sleep a bit not to flood the interface */
78         usleep(300);
79
80         /* Wait for some data */
81
82         pthread_mutex_lock( [self queueLock] );
83         /* Wait until we have something on the queue */
84         while( [[self messageQueue] count] <= 0 )
85         {
86             pthread_cond_wait( [self signalData], [self queueLock] );
87         }
88
89         //if( [[self messageQueue] count] % 100 == 0 || [[self messageQueue] count] < 100 )
90           //  NSLog(@"[EVENT_MANAGER] On the stack we have %d elements", [[self messageQueue] count]);
91
92         /* Get the first object off the queue. */
93         dataMessage = [[[self messageQueue] lastObject] retain];    // Released in 'call'
94         [[self messageQueue] removeLastObject];
95         message = (message_t *)[dataMessage bytes];
96         
97         /* Remove duplicate notifications. */
98         if( message->type == VLCNotification )
99         {
100             for( i = [[self messageQueue] count]-1; i >= 0; i-- )
101             {
102                 message_newer = (message_t *)[(NSData *)[[self messageQueue] objectAtIndex:i] bytes];
103                 if( message_newer->type == VLCNotification &&
104                     message_newer->target == message->target &&
105                    [message_newer->u.name isEqualToString:message->u.name] )
106                 {
107                     [message_newer->target release];
108                     [message_newer->u.name release];
109                     [[self messageQueue] removeObjectAtIndex:i];
110                 }
111             }
112         }
113         else if( message->type == VLCObjectMethodWithArrayArg )
114         {
115             NSMutableArray * newArg = nil;
116
117             /* Collapse messages that takes array arg by sending one bigger array */
118             for( i = [[self messageQueue] count]-1; i >= 0; i-- )
119             {
120                 message_newer = (message_t *)[(NSData *)[[self messageQueue] objectAtIndex: i] bytes];
121                 if( message_newer->type == VLCObjectMethodWithArrayArg &&
122                     message_newer->target == message->target && 
123                     message_newer->sel == message->sel )
124                 {
125                     if(!newArg)
126                     {
127                         newArg = [NSMutableArray arrayWithArray:message->u.object];
128                         [message->u.object release];
129                     }
130                     
131                     [newArg addObjectsFromArray:message_newer->u.object];
132                     [message_newer->target release];
133                     [message_newer->u.object release];
134                     [[self messageQueue] removeObjectAtIndex:i];
135                 }
136                 /* It should be a good idea not to collapse event, with other kind of event in-between
137                  * Ignore for now only if target is the same */
138                 else if( message_newer->target != message->target )
139                     break;
140             }
141             
142             if( newArg )
143                 message->u.object = [newArg retain];
144         }
145         
146         pthread_mutex_unlock( [self queueLock] );
147
148         if( message->type == VLCNotification )
149             [self performSelectorOnMainThread:@selector(callDelegateOfObjectAndSendNotificationWithArgs:) 
150                                    withObject:dataMessage
151                                 waitUntilDone: NO];
152         else
153             [self performSelectorOnMainThread:@selector(callObjectMethodWithArgs:) 
154                                    withObject:dataMessage
155                                 waitUntilDone: YES];
156
157         [pool release];
158     }
159     return nil;
160 }
161
162 @implementation VLCEventManager
163 + (id)sharedManager
164 {
165     static VLCEventManager * defaultManager = NULL;
166     
167     /* We do want a lock here to avoid leaks */
168     if ( !defaultManager )
169     {
170         defaultManager = [[VLCEventManager alloc] init];
171     }
172
173     return defaultManager;
174 }
175
176 - (void)dummy
177 {
178     /* Put Cocoa in multithreaded mode by calling a dummy function */
179 }
180
181 - (id)init
182 {
183     if( self = [super init] )
184     {
185         if(![NSThread isMultiThreaded])
186         {
187             [NSThread detachNewThreadSelector:@selector(dummy) toTarget:self withObject:nil];
188             NSAssert([NSThread isMultiThreaded], @"Can't put Cocoa in multithreaded mode");
189         }
190
191         pthread_mutex_init( &queueLock, NULL );
192         pthread_cond_init( &signalData, NULL );
193         pthread_create( &dispatcherThread, NULL, EventDispatcherMainLoop, self );
194         messageQueue = [[NSMutableArray alloc] initWithCapacity:10];
195     }
196     return self;
197 }
198
199 - (void)dealloc
200 {
201     pthread_kill( dispatcherThread, SIGKILL );
202     pthread_join( dispatcherThread, NULL );
203
204     [messageQueue release];
205     [super dealloc];
206 }
207
208 - (void)callOnMainThreadDelegateOfObject:(id)aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
209 {
210 //    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
211     
212     message_t message = 
213     { 
214         [aTarget retain], 
215         aSelector, 
216         [aNotificationName retain], 
217         VLCNotification 
218     };
219
220 //    if( [NSThread isMainThread] )
221 //    {
222         [self callDelegateOfObjectAndSendNotificationWithArgs:[[NSData dataWithBytes:&message length:sizeof(message_t)] retain] /* released in the call */];
223 //    } 
224 //    else 
225 //    {
226 //        pthread_mutex_lock( [self queueLock] );
227 //        [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(message_t)] atIndex:0];
228 //        pthread_cond_signal( [self signalData] );
229 //        pthread_mutex_unlock( [self queueLock] );
230 //    }
231     
232     [pool release];
233 }
234
235 - (void)callOnMainThreadObject:(id)aTarget withMethod:(SEL)aSelector withArgumentAsObject: (id)arg
236 {
237     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
238     message_t message = 
239     { 
240         [aTarget retain], 
241         aSelector, 
242         [arg retain], 
243         [arg isKindOfClass:[NSArray class]] ? VLCObjectMethodWithArrayArg : VLCObjectMethodWithObjectArg 
244     };
245
246     pthread_mutex_lock( [self queueLock] );
247     [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(message_t)] atIndex:0];
248     pthread_cond_signal( [self signalData] );
249     pthread_mutex_unlock( [self queueLock] );
250     
251     [pool release];
252 }
253 @end
254
255 @implementation VLCEventManager (Private)
256 - (void)callDelegateOfObjectAndSendNotificationWithArgs:(NSData*)data
257 {
258     message_t * message = (message_t *)[data bytes];
259
260     [self callDelegateOfObject:message->target withDelegateMethod:message->sel withNotificationName:message->u.name];
261     [message->u.name release];
262     [message->target release];
263     [data release];
264 }
265
266 - (void)callObjectMethodWithArgs:(NSData*)data
267 {
268     message_t * message = (message_t *)[data bytes];
269     void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[message->target methodForSelector: message->sel];
270
271     method( message->target, message->sel, message->u.object);
272     [message->u.object release];
273     [message->target release];
274     [data release];
275 }
276
277 - (void)callDelegateOfObject:(id) aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
278 {
279     //    [[NSNotificationCenter defaultCenter] postNotification: [NSNotification notificationWithName:aNotificationName object:aTarget]];
280     
281     if (![aTarget delegate] || ![[aTarget delegate] respondsToSelector:aSelector])
282         return;
283     
284     void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[[aTarget delegate] methodForSelector: aSelector];
285     method( [aTarget delegate], aSelector, [NSNotification notificationWithName:aNotificationName object:aTarget]);
286 }
287
288 - (NSMutableArray *)messageQueue
289 {
290     return messageQueue;
291 }
292
293 - (pthread_cond_t *)signalData
294 {
295     return &signalData;
296 }
297
298 - (pthread_mutex_t *)queueLock
299 {
300     return &queueLock;
301 }
302 @end