VLCEventManager.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. #import "VLCEventManager.h"
  26. #import <pthread.h>
  27. static VLCEventManager * defaultManager = NULL;
  28. enum message_type_t
  29. {
  30. VLCNotification,
  31. VLCObjectMethodWithObjectArg,
  32. VLCObjectMethodWithArrayArg
  33. };
  34. struct message {
  35. id target;
  36. SEL sel;
  37. union u
  38. {
  39. NSString * name;
  40. id object;
  41. } u;
  42. enum message_type_t type;
  43. };
  44. @interface VLCEventManager (Private)
  45. - (void)callDelegateOfObjectAndSendNotificationWithArgs:(NSData*)data;
  46. - (void)callObjectMethodWithArgs:(NSData*)data;
  47. - (void)callDelegateOfObject:(id) aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName;
  48. - (pthread_cond_t *)signalData;
  49. - (pthread_mutex_t *)queueLock;
  50. - (NSMutableArray *)messageQueue;
  51. @end
  52. static void * EventDispatcherMainLoop(void * user_data)
  53. {
  54. VLCEventManager * self = user_data;
  55. for(;;)
  56. {
  57. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  58. struct message * message, * message_newer = NULL;
  59. NSData * dataMessage;
  60. int i;
  61. /* Sleep a bit not to flood the interface */
  62. msleep(300);
  63. /* Wait for some data */
  64. pthread_mutex_lock( [self queueLock] );
  65. /* Wait until we have something on the queue */
  66. while( [[self messageQueue] count] <= 0 )
  67. {
  68. pthread_cond_wait( [self signalData], [self queueLock] );
  69. }
  70. //if( [[self messageQueue] count] % 100 == 0 || [[self messageQueue] count] < 100 )
  71. // NSLog(@"[EVENT_MANAGER] On the stack we have %d elements", [[self messageQueue] count]);
  72. message = (struct message *)[(NSData *)[[self messageQueue] lastObject] bytes];
  73. /* Don't send the same notification twice */
  74. if( message->type == VLCNotification )
  75. {
  76. for( i = 0; i < [[self messageQueue] count]-1; i++ )
  77. {
  78. message_newer = (struct message *)[(NSData *)[[self messageQueue] objectAtIndex:i] bytes];
  79. if( message_newer->type == VLCNotification &&
  80. message_newer->target == message->target &&
  81. [message_newer->u.name isEqualToString:message->u.name] )
  82. {
  83. [message_newer->target release];
  84. [message_newer->u.name release];
  85. [[self messageQueue] removeObjectAtIndex:i];
  86. i--;
  87. continue;
  88. }
  89. }
  90. }
  91. else if( message->type == VLCObjectMethodWithArrayArg )
  92. {
  93. NSMutableArray * newArg = nil;
  94. /* Collapse messages that takes array arg by sending one bigger array */
  95. for( i = [[self messageQueue] count]-2; i >= 0; i-- )
  96. {
  97. message_newer = (struct message *)[(NSData *)[[self messageQueue] objectAtIndex: i] bytes];
  98. if( message_newer->type == VLCObjectMethodWithArrayArg &&
  99. message_newer->target == message->target &&
  100. message_newer->sel == message->sel )
  101. {
  102. if(!newArg)
  103. newArg = [NSMutableArray arrayWithArray:message->u.object];
  104. [newArg addObjectsFromArray:message_newer->u.object];
  105. [message_newer->target release];
  106. [message_newer->u.object release];
  107. [[self messageQueue] removeObjectAtIndex: i];
  108. continue;
  109. }
  110. /* It should be a good idea not to collapse event, with other kind of event in-between
  111. * Ignore for now only if target is the same */
  112. else if( message_newer->target == message->target )
  113. break;
  114. }
  115. if( newArg )
  116. {
  117. [message->u.object release];
  118. message->u.object = [newArg retain];
  119. [newArg retain];
  120. }
  121. }
  122. dataMessage = [[self messageQueue] lastObject];
  123. pthread_mutex_unlock( [self queueLock] );
  124. if( message->type == VLCNotification )
  125. [self performSelectorOnMainThread:@selector(callDelegateOfObjectAndSendNotificationWithArgs:) withObject:[dataMessage retain] /* released in the call */ waitUntilDone: NO];
  126. else
  127. [self performSelectorOnMainThread:@selector(callObjectMethodWithArgs:) withObject:[dataMessage retain] /* released in the call */ waitUntilDone: YES];
  128. pthread_mutex_lock( [self queueLock] );
  129. [[self messageQueue] removeLastObject];
  130. pthread_mutex_unlock( [self queueLock] );
  131. [pool release];
  132. }
  133. return nil;
  134. }
  135. @implementation VLCEventManager
  136. + (id)sharedManager
  137. {
  138. /* We do want a lock here to avoid leaks */
  139. if ( !defaultManager )
  140. {
  141. defaultManager = [[VLCEventManager alloc] init];
  142. }
  143. return defaultManager;
  144. }
  145. - (void)dummy
  146. {
  147. /* Put Cocoa in multithreaded mode by calling a dummy function */
  148. }
  149. - (id)init
  150. {
  151. if( self = [super init] )
  152. {
  153. if(![NSThread isMultiThreaded])
  154. {
  155. [NSThread detachNewThreadSelector:@selector(dummy) toTarget:self withObject:nil];
  156. NSAssert([NSThread isMultiThreaded], @"Can't put Cocoa in multithreaded mode");
  157. }
  158. pthread_mutex_init( &queueLock, NULL );
  159. pthread_cond_init( &signalData, NULL );
  160. pthread_create( &dispatcherThread, NULL, EventDispatcherMainLoop, self );
  161. messageQueue = [[NSMutableArray alloc] initWithCapacity:10];
  162. }
  163. return self;
  164. }
  165. - (void)dealloc
  166. {
  167. pthread_kill( dispatcherThread, SIGKILL );
  168. pthread_join( dispatcherThread, NULL );
  169. [messageQueue release];
  170. [super dealloc];
  171. }
  172. - (void)callOnMainThreadDelegateOfObject:(id)aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
  173. {
  174. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  175. struct message message =
  176. {
  177. [aTarget retain],
  178. aSelector,
  179. [aNotificationName retain],
  180. VLCNotification
  181. };
  182. if([NSThread isMainThread])
  183. {
  184. [self callDelegateOfObjectAndSendNotificationWithArgs:[[NSData dataWithBytes:&message length:sizeof(struct message)] retain] /* released in the call */];
  185. return;
  186. }
  187. // pthread_mutex_lock( [self queueLock] );
  188. // [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(struct message)] atIndex:0];
  189. // pthread_cond_signal( [self signalData] );
  190. // pthread_mutex_unlock( [self queueLock] );
  191. [pool release];
  192. }
  193. - (void)callOnMainThreadObject:(id)aTarget withMethod:(SEL)aSelector withArgumentAsObject: (id)arg
  194. {
  195. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  196. struct message message =
  197. {
  198. [aTarget retain],
  199. aSelector,
  200. [arg retain],
  201. [arg isKindOfClass:[NSArray class]] ? VLCObjectMethodWithArrayArg : VLCObjectMethodWithObjectArg
  202. };
  203. pthread_mutex_lock( [self queueLock] );
  204. [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(struct message)] atIndex:0];
  205. pthread_cond_signal( [self signalData] );
  206. pthread_mutex_unlock( [self queueLock] );
  207. [pool release];
  208. }
  209. @end
  210. @implementation VLCEventManager (Private)
  211. - (void)callDelegateOfObjectAndSendNotificationWithArgs:(NSData*)data
  212. {
  213. struct message * message = (struct message *)[data bytes];
  214. [self callDelegateOfObject:message->target withDelegateMethod:message->sel withNotificationName:message->u.name];
  215. [message->u.name release];
  216. [message->target release];
  217. [data release];
  218. }
  219. - (void)callObjectMethodWithArgs:(NSData*)data
  220. {
  221. struct message * message = (struct message *)[data bytes];
  222. void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[message->target methodForSelector: message->sel];
  223. method( message->target, message->sel, message->u.object);
  224. [message->u.object release];
  225. [message->target release];
  226. [data release];
  227. }
  228. - (NSMutableArray *)messageQueue
  229. {
  230. return messageQueue;
  231. }
  232. - (pthread_cond_t *)signalData
  233. {
  234. return &signalData;
  235. }
  236. - (pthread_mutex_t *)queueLock
  237. {
  238. return &queueLock;
  239. }
  240. - (void)callDelegateOfObject:(id) aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
  241. {
  242. // [[NSNotificationCenter defaultCenter] postNotification: [NSNotification notificationWithName:aNotificationName object:aTarget]];
  243. if (![aTarget delegate] || ![[aTarget delegate] respondsToSelector: aSelector])
  244. return;
  245. void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[[aTarget delegate] methodForSelector: aSelector];
  246. method( [aTarget delegate], aSelector, [NSNotification notificationWithName:aNotificationName object:aTarget]);
  247. }
  248. @end