VLCEventManager.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 (PrivateAPI)
  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] waitUntilDone: NO];
  88. else
  89. [self performSelectorOnMainThread:@selector(callObjectMethodWithArgs:) withObject:[dataMessage retain] 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. pthread_mutex_lock( [self queueLock] );
  136. [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(struct message)] atIndex:0];
  137. pthread_cond_signal( [self signalData] );
  138. pthread_mutex_unlock( [self queueLock] );
  139. [pool release];
  140. }
  141. - (void)callOnMainThreadObject:(id)aTarget withMethod:(SEL)aSelector withArgumentAsObject: (id)arg
  142. {
  143. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  144. struct message message =
  145. {
  146. [aTarget retain],
  147. aSelector,
  148. [arg retain],
  149. VLCObjectMethodWithObjectArg
  150. };
  151. pthread_mutex_lock( [self queueLock] );
  152. [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(struct message)] atIndex:0];
  153. pthread_cond_signal( [self signalData] );
  154. pthread_mutex_unlock( [self queueLock] );
  155. [pool release];
  156. }
  157. @end
  158. @implementation VLCEventManager (PrivateAPI)
  159. - (void)callDelegateOfObjectAndSendNotificationWithArgs:(NSData*)data
  160. {
  161. struct message * message = (struct message *)[data bytes];
  162. [self callDelegateOfObject:message->target withDelegateMethod:message->sel withNotificationName:message->u.name];
  163. [message->u.name release];
  164. [message->target release];
  165. [data release];
  166. }
  167. - (void)callObjectMethodWithArgs:(NSData*)data
  168. {
  169. struct message * message = (struct message *)[data bytes];
  170. void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[message->target methodForSelector: message->sel];
  171. method( message->target, message->sel, message->u.object);
  172. [message->u.object release];
  173. [message->target release];
  174. [data release];
  175. }
  176. - (NSMutableArray *)messageQueue
  177. {
  178. return messageQueue;
  179. }
  180. - (pthread_cond_t *)signalData
  181. {
  182. return &signalData;
  183. }
  184. - (pthread_mutex_t *)queueLock
  185. {
  186. return &queueLock;
  187. }
  188. - (void)callDelegateOfObject:(id) aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
  189. {
  190. // [[NSNotificationCenter defaultCenter] postNotification: [NSNotification notificationWithName:aNotificationName object:aTarget]];
  191. if (![aTarget delegate] || ![[aTarget delegate] respondsToSelector: aSelector])
  192. return;
  193. void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[[aTarget delegate] methodForSelector: aSelector];
  194. method( [aTarget delegate], aSelector, [NSNotification notificationWithName:aNotificationName object:aTarget]);
  195. }
  196. @end