1
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
9#import "SkEventNotifier.h"
10#include "SkEvent.h"
11#define SkEventClass @"SkEvenClass"
12@implementation SkEventNotifier
13- (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() {
54    //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];
60}
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}
69