VLCEventManager.m 11 KB

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