VLCEventManager.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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->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. i++;
  109. continue;
  110. }
  111. /* It should be a good idea not to collapse event, with other kind of event in-between
  112. * Ignore for now only if target is the same */
  113. else if( message_newer->target == message->target )
  114. break;
  115. }
  116. if( newArg )
  117. {
  118. [message->u.object release];
  119. message->u.object = [newArg retain];
  120. [newArg retain];
  121. }
  122. }
  123. dataMessage = [[self messageQueue] lastObject];
  124. pthread_mutex_unlock( [self queueLock] );
  125. if( message->type == VLCNotification )
  126. [self performSelectorOnMainThread:@selector(callDelegateOfObjectAndSendNotificationWithArgs:) withObject:[dataMessage retain] /* released in the call */ waitUntilDone: NO];
  127. else
  128. [self performSelectorOnMainThread:@selector(callObjectMethodWithArgs:) withObject:[dataMessage retain] /* released in the call */ waitUntilDone: YES];
  129. pthread_mutex_lock( [self queueLock] );
  130. [[self messageQueue] removeLastObject];
  131. pthread_mutex_unlock( [self queueLock] );
  132. [pool release];
  133. }
  134. return nil;
  135. }
  136. @implementation VLCEventManager
  137. + (id)sharedManager
  138. {
  139. /* We do want a lock here to avoid leaks */
  140. if ( !defaultManager )
  141. {
  142. defaultManager = [[VLCEventManager alloc] init];
  143. }
  144. return defaultManager;
  145. }
  146. - (void)dummy
  147. {
  148. /* Put Cocoa in multithreaded mode by calling a dummy function */
  149. }
  150. - (id)init
  151. {
  152. if( self = [super init] )
  153. {
  154. if(![NSThread isMultiThreaded])
  155. {
  156. [NSThread detachNewThreadSelector:@selector(dummy) toTarget:self withObject:nil];
  157. NSAssert([NSThread isMultiThreaded], @"Can't put Cocoa in multithreaded mode");
  158. }
  159. pthread_mutex_init( &queueLock, NULL );
  160. pthread_cond_init( &signalData, NULL );
  161. pthread_create( &dispatcherThread, NULL, EventDispatcherMainLoop, self );
  162. messageQueue = [[NSMutableArray alloc] initWithCapacity:10];
  163. }
  164. return self;
  165. }
  166. - (void)dealloc
  167. {
  168. pthread_kill( dispatcherThread, SIGKILL );
  169. pthread_join( dispatcherThread, NULL );
  170. [messageQueue release];
  171. [super dealloc];
  172. }
  173. - (void)callOnMainThreadDelegateOfObject:(id)aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
  174. {
  175. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  176. struct message message =
  177. {
  178. [aTarget retain],
  179. aSelector,
  180. [aNotificationName retain],
  181. VLCNotification
  182. };
  183. if([NSThread isMainThread])
  184. {
  185. [self callDelegateOfObjectAndSendNotificationWithArgs:[[NSData dataWithBytes:&message length:sizeof(struct message)] retain] /* released in the call */];
  186. return;
  187. }
  188. // pthread_mutex_lock( [self queueLock] );
  189. // [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(struct message)] atIndex:0];
  190. // pthread_cond_signal( [self signalData] );
  191. // pthread_mutex_unlock( [self queueLock] );
  192. [pool release];
  193. }
  194. - (void)callOnMainThreadObject:(id)aTarget withMethod:(SEL)aSelector withArgumentAsObject: (id)arg
  195. {
  196. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  197. struct message message =
  198. {
  199. [aTarget retain],
  200. aSelector,
  201. [arg retain],
  202. [arg isKindOfClass:[NSArray class]] ? VLCObjectMethodWithArrayArg : VLCObjectMethodWithObjectArg
  203. };
  204. pthread_mutex_lock( [self queueLock] );
  205. [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(struct message)] atIndex:0];
  206. pthread_cond_signal( [self signalData] );
  207. pthread_mutex_unlock( [self queueLock] );
  208. [pool release];
  209. }
  210. @end
  211. @implementation VLCEventManager (Private)
  212. - (void)callDelegateOfObjectAndSendNotificationWithArgs:(NSData*)data
  213. {
  214. struct message * message = (struct message *)[data bytes];
  215. [self callDelegateOfObject:message->target withDelegateMethod:message->sel withNotificationName:message->u.name];
  216. [message->u.name release];
  217. [message->target release];
  218. [data release];
  219. }
  220. - (void)callObjectMethodWithArgs:(NSData*)data
  221. {
  222. struct message * message = (struct message *)[data bytes];
  223. void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[message->target methodForSelector: message->sel];
  224. method( message->target, message->sel, message->u.object);
  225. [message->u.object release];
  226. [message->target release];
  227. [data release];
  228. }
  229. - (NSMutableArray *)messageQueue
  230. {
  231. return messageQueue;
  232. }
  233. - (pthread_cond_t *)signalData
  234. {
  235. return &signalData;
  236. }
  237. - (pthread_mutex_t *)queueLock
  238. {
  239. return &queueLock;
  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. @end