VLCEventManager.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. if ([[self messageQueue] count] <= 0 ) do {
  63. pthread_cond_wait( [self signalData], [self queueLock] );
  64. } while([[self messageQueue] count] <= 0 );
  65. message = (struct message *)[(NSData *)[[self messageQueue] lastObject] bytes];
  66. /* Don't send the same notification twice */
  67. if( message->type == VLCNotification )
  68. {
  69. for( i = 0; i < [[self messageQueue] count]-1; i++ )
  70. {
  71. message_newer = (struct message *)[(NSData *)[[self messageQueue] objectAtIndex: i] bytes];
  72. if( message_newer->type != VLCNotification )
  73. continue;
  74. if( message_newer->target == message->target && message_newer->target == message->target && [message_newer->u.name isEqualToString:message->u.name] )
  75. {
  76. [message_newer->target release];
  77. [message->u.name release];
  78. [[self messageQueue] removeObjectAtIndex: i];
  79. i--;
  80. }
  81. }
  82. }
  83. dataMessage = [[self messageQueue] lastObject];
  84. pthread_mutex_unlock( [self queueLock] );
  85. if( message->type == VLCNotification )
  86. [self performSelectorOnMainThread:@selector(callDelegateOfObjectAndSendNotificationWithArgs:) withObject:[dataMessage retain] waitUntilDone: NO];
  87. else
  88. [self performSelectorOnMainThread:@selector(callObjectMethodWithArgs:) withObject:[dataMessage retain] waitUntilDone: NO];
  89. pthread_mutex_lock( [self queueLock] );
  90. [[self messageQueue] removeLastObject];
  91. pthread_mutex_unlock( [self queueLock] );
  92. [pool release];
  93. }
  94. }
  95. @implementation VLCEventManager
  96. + (id)sharedManager
  97. {
  98. /* We do want a lock here to avoid leaks */
  99. if ( !defaultManager )
  100. {
  101. defaultManager = [[VLCEventManager alloc] init];
  102. }
  103. return defaultManager;
  104. }
  105. - (id)init
  106. {
  107. if( self = [super init] )
  108. {
  109. pthread_mutex_init( &queueLock, NULL );
  110. pthread_cond_init( &signalData, NULL );
  111. pthread_create( &dispatcherThread, NULL, EventDispatcherMainLoop, self );
  112. messageQueue = [[NSMutableArray alloc] initWithCapacity:10];
  113. }
  114. return self;
  115. }
  116. - (void)dealloc
  117. {
  118. pthread_kill( dispatcherThread, SIGKILL );
  119. pthread_join( dispatcherThread, NULL );
  120. [super dealloc];
  121. }
  122. - (void)callOnMainThreadDelegateOfObject:(id)aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
  123. {
  124. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  125. struct message message = { [aTarget retain], aSelector, [aNotificationName retain], VLCNotification };
  126. pthread_mutex_lock( [self queueLock] );
  127. [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(struct message)] atIndex:0];
  128. pthread_cond_signal( [self signalData] );
  129. pthread_mutex_unlock( [self queueLock] );
  130. [pool release];
  131. }
  132. - (void)callOnMainThreadObject:(id)aTarget withMethod:(SEL)aSelector withArgumentAsObject: (id)arg
  133. {
  134. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  135. struct message message = { [aTarget retain], aSelector, [arg retain], VLCObjectMethodWithObjectArg };
  136. pthread_mutex_lock( [self queueLock] );
  137. [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(struct message)] atIndex:0];
  138. pthread_cond_signal( [self signalData] );
  139. pthread_mutex_unlock( [self queueLock] );
  140. [pool release];
  141. }
  142. @end
  143. @implementation VLCEventManager (Private)
  144. - (void)callDelegateOfObjectAndSendNotificationWithArgs:(NSData*)data
  145. {
  146. struct message * message = (struct message *)[data bytes];
  147. [self callDelegateOfObject:message->target withDelegateMethod:message->sel withNotificationName:message->u.name];
  148. [message->u.name release];
  149. [message->target release];
  150. [data release];
  151. }
  152. - (void)callObjectMethodWithArgs:(NSData*)data
  153. {
  154. struct message * message = (struct message *)[data bytes];
  155. void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[message->target methodForSelector: message->sel];
  156. method( message->target, message->sel, message->u.object);
  157. [message->u.object release];
  158. [message->target release];
  159. [data release];
  160. }
  161. - (NSMutableArray *)messageQueue
  162. {
  163. return messageQueue;
  164. }
  165. - (pthread_cond_t *)signalData
  166. {
  167. return &signalData;
  168. }
  169. - (pthread_mutex_t *)queueLock
  170. {
  171. return &queueLock;
  172. }
  173. - (void)callDelegateOfObject:(id) aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
  174. {
  175. NSMethodSignature * aSignature;
  176. NSInvocation * anInvocation;
  177. [[NSNotificationCenter defaultCenter] postNotification: [NSNotification notificationWithName:aNotificationName object:aTarget]];
  178. if (![aTarget delegate] || ![[aTarget delegate] respondsToSelector: aSelector])
  179. return;
  180. /* NSInvocation could maybe be prefered here */
  181. void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[[aTarget delegate] methodForSelector: aSelector];
  182. method( [aTarget delegate], aSelector, [NSNotification notificationWithName:aNotificationName object:aTarget]);
  183. }
  184. @end