blob: 0864380d95174df3f717cb09d3413891f3640df9 [file] [log] [blame]
bsalomon@google.comf75b84e2011-09-29 14:58:28 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
yangsu@google.com12d177d2011-08-01 17:07:12 +00009#import "SkEventNotifier.h"
10#include "SkEvent.h"
11#define SkEventClass @"SkEvenClass"
12@implementation SkEventNotifier
yangsu@google.com12d177d2011-08-01 17:07:12 +000013- (id)init {
14 self = [super init];
15 if (self) {
16 //Register as an observer for SkEventClass events and call
17 //receiveSkEvent: upon receiving the event
18 [[NSNotificationCenter defaultCenter] addObserver:self
19 selector:@selector(receiveSkEvent:)
20 name:SkEventClass
21 object:nil];
22 }
23 return self;
24}
25
26- (void)dealloc {
27 [[NSNotificationCenter defaultCenter] removeObserver:self];
28 [super dealloc];
29}
30
31-(BOOL) acceptsFirstResponder {
32 return YES;
33}
34
35//SkEvent Handers
36- (void)receiveSkEvent:(NSNotification *)notification {
37 if(SkEvent::ProcessEvent())
38 SkEvent::SignalNonEmptyQueue();
39}
40
41+ (void)postTimedSkEvent:(NSTimeInterval)timeInterval {
42 [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self
43 selector:@selector(timerFireMethod:)
44 userInfo:nil repeats:NO];
45}
46
47+ (void)timerFireMethod:(NSTimer*)theTimer {
48 SkEvent::ServiceQueueTimer();
49}
50
51@end
52////////////////////////////////////////////////////////////////////////////////
53void SkEvent::SignalNonEmptyQueue() {
yangsu@google.comf3493f02011-08-08 15:12:05 +000054 //post a SkEventClass event to the default notification queue
55 NSNotification* notification = [NSNotification notificationWithName:SkEventClass object:nil];
56 [[NSNotificationQueue defaultQueue] enqueueNotification:notification
57 postingStyle:NSPostWhenIdle
58 coalesceMask:NSNotificationNoCoalescing
59 forModes:nil];
yangsu@google.com12d177d2011-08-01 17:07:12 +000060}
61
62void SkEvent::SignalQueueTimer(SkMSec delay) {
63 if (delay) {
64 //Convert to seconds
65 NSTimeInterval ti = delay/(float)SK_MSec1;
66 [SkEventNotifier postTimedSkEvent:ti];
67 }
68}