VLCEventManager.m 7.8 KB

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