VLCEventManager.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. };
  33. struct message {
  34. id target;
  35. SEL sel;
  36. union u
  37. {
  38. NSString * name;
  39. id object;
  40. } u;
  41. enum message_type_t type;
  42. };
  43. @interface VLCEventManager (Private)
  44. - (void)callDelegateOfObjectAndSendNotificationWithArgs:(NSData*)data;
  45. - (void)callObjectMethodWithArgs:(NSData*)data;
  46. - (void)callDelegateOfObject:(id) aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName;
  47. - (pthread_cond_t *)signalData;
  48. - (pthread_mutex_t *)queueLock;
  49. - (NSMutableArray *)messageQueue;
  50. @end
  51. static void * EventDispatcherMainLoop(void * user_data)
  52. {
  53. VLCEventManager * self = user_data;
  54. for(;;)
  55. {
  56. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  57. struct message * message, * message_newer = NULL;
  58. NSData *dataMessage;
  59. int i;
  60. /* Wait for some data */
  61. pthread_mutex_lock( [self queueLock] );
  62. /* Wait until we have something on the queue */
  63. while([[self messageQueue] count] <= 0 )
  64. {
  65. pthread_cond_wait( [self signalData], [self queueLock] );
  66. }
  67. message = (struct message *)[(NSData *)[[self messageQueue] lastObject] bytes];
  68. /* Don't send the same notification twice */
  69. if( message->type == VLCNotification )
  70. {
  71. for( i = 0; i < [[self messageQueue] count]-1; i++ )
  72. {
  73. message_newer = (struct message *)[(NSData *)[[self messageQueue] objectAtIndex: i] bytes];
  74. if( message_newer->type != VLCNotification )
  75. continue;
  76. if( message_newer->target == message->target && message_newer->target == message->target && [message_newer->u.name isEqualToString:message->u.name] )
  77. {
  78. [message_newer->target release];
  79. [message->u.name release];
  80. [[self messageQueue] removeObjectAtIndex: i];
  81. i--;
  82. }
  83. }
  84. }
  85. dataMessage = [[self messageQueue] lastObject];
  86. pthread_mutex_unlock( [self queueLock] );
  87. if( message->type == VLCNotification )
  88. [self performSelectorOnMainThread:@selector(callDelegateOfObjectAndSendNotificationWithArgs:) withObject:[dataMessage retain] /* released in the call */ waitUntilDone: NO];
  89. else
  90. [self performSelectorOnMainThread:@selector(callObjectMethodWithArgs:) withObject:[dataMessage retain] /* released in the call */ waitUntilDone: NO];
  91. pthread_mutex_lock( [self queueLock] );
  92. [[self messageQueue] removeLastObject];
  93. pthread_mutex_unlock( [self queueLock] );
  94. [pool release];
  95. }
  96. return nil;
  97. }
  98. @implementation VLCEventManager
  99. + (id)sharedManager
  100. {
  101. /* We do want a lock here to avoid leaks */
  102. if ( !defaultManager )
  103. {
  104. defaultManager = [[VLCEventManager alloc] init];
  105. }
  106. return defaultManager;
  107. }
  108. - (id)init
  109. {
  110. if( self = [super init] )
  111. {
  112. pthread_mutex_init( &queueLock, NULL );
  113. pthread_cond_init( &signalData, NULL );
  114. pthread_create( &dispatcherThread, NULL, EventDispatcherMainLoop, self );
  115. messageQueue = [[NSMutableArray alloc] initWithCapacity:10];
  116. }
  117. return self;
  118. }
  119. - (void)dealloc
  120. {
  121. pthread_kill( dispatcherThread, SIGKILL );
  122. pthread_join( dispatcherThread, NULL );
  123. [messageQueue release];
  124. [super dealloc];
  125. }
  126. - (void)callOnMainThreadDelegateOfObject:(id)aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
  127. {
  128. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  129. struct message message =
  130. {
  131. [aTarget retain],
  132. aSelector,
  133. [aNotificationName retain],
  134. VLCNotification
  135. };
  136. if([NSThread isMainThread])
  137. {
  138. [self callDelegateOfObjectAndSendNotificationWithArgs:[[NSData dataWithBytes:&message length:sizeof(struct message)] retain] /* released in the call */];
  139. return;
  140. }
  141. pthread_mutex_lock( [self queueLock] );
  142. [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(struct message)] atIndex:0];
  143. pthread_cond_signal( [self signalData] );
  144. pthread_mutex_unlock( [self queueLock] );
  145. [pool release];
  146. }
  147. - (void)callOnMainThreadObject:(id)aTarget withMethod:(SEL)aSelector withArgumentAsObject: (id)arg
  148. {
  149. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  150. struct message message =
  151. {
  152. [aTarget retain],
  153. aSelector,
  154. [arg retain],
  155. VLCObjectMethodWithObjectArg
  156. };
  157. if([NSThread isMainThread])
  158. {
  159. [self callObjectMethodWithArgs:[[NSData dataWithBytes:&message length:sizeof(struct message)] retain] /* released in the call */];
  160. return;
  161. }
  162. pthread_mutex_lock( [self queueLock] );
  163. [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(struct message)] atIndex:0];
  164. pthread_cond_signal( [self signalData] );
  165. pthread_mutex_unlock( [self queueLock] );
  166. [pool release];
  167. }
  168. @end
  169. @implementation VLCEventManager (Private)
  170. - (void)callDelegateOfObjectAndSendNotificationWithArgs:(NSData*)data
  171. {
  172. struct message * message = (struct message *)[data bytes];
  173. [self callDelegateOfObject:message->target withDelegateMethod:message->sel withNotificationName:message->u.name];
  174. [message->u.name release];
  175. [message->target release];
  176. [data release];
  177. }
  178. - (void)callObjectMethodWithArgs:(NSData*)data
  179. {
  180. struct message * message = (struct message *)[data bytes];
  181. void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[message->target methodForSelector: message->sel];
  182. method( message->target, message->sel, message->u.object);
  183. [message->u.object release];
  184. [message->target release];
  185. [data release];
  186. }
  187. - (NSMutableArray *)messageQueue
  188. {
  189. return messageQueue;
  190. }
  191. - (pthread_cond_t *)signalData
  192. {
  193. return &signalData;
  194. }
  195. - (pthread_mutex_t *)queueLock
  196. {
  197. return &queueLock;
  198. }
  199. - (void)callDelegateOfObject:(id) aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
  200. {
  201. // [[NSNotificationCenter defaultCenter] postNotification: [NSNotification notificationWithName:aNotificationName object:aTarget]];
  202. if (![aTarget delegate] || ![[aTarget delegate] respondsToSelector: aSelector])
  203. return;
  204. void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[[aTarget delegate] methodForSelector: aSelector];
  205. method( [aTarget delegate], aSelector, [NSNotification notificationWithName:aNotificationName object:aTarget]);
  206. }
  207. @end