1f75b84e94b1ca1540295584421de0158d87a9b52bsalomon@google.com
2f75b84e94b1ca1540295584421de0158d87a9b52bsalomon@google.com/*
3f75b84e94b1ca1540295584421de0158d87a9b52bsalomon@google.com * Copyright 2011 Google Inc.
4f75b84e94b1ca1540295584421de0158d87a9b52bsalomon@google.com *
5f75b84e94b1ca1540295584421de0158d87a9b52bsalomon@google.com * Use of this source code is governed by a BSD-style license that can be
6f75b84e94b1ca1540295584421de0158d87a9b52bsalomon@google.com * found in the LICENSE file.
7f75b84e94b1ca1540295584421de0158d87a9b52bsalomon@google.com */
8f75b84e94b1ca1540295584421de0158d87a9b52bsalomon@google.com
912d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com#import "SkEventNotifier.h"
1012d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com#include "SkEvent.h"
1112d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com#define SkEventClass @"SkEvenClass"
1212d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com@implementation SkEventNotifier
1312d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com- (id)init {
1412d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com    self = [super init];
1512d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com    if (self) {
1612d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com        //Register as an observer for SkEventClass events and call
1712d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com        //receiveSkEvent: upon receiving the event
1812d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com        [[NSNotificationCenter defaultCenter] addObserver:self
1912d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com                                                 selector:@selector(receiveSkEvent:)
2012d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com                                                     name:SkEventClass
2112d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com                                                   object:nil];
2212d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com    }
2312d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com    return self;
2412d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com}
2512d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com
2612d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com- (void)dealloc {
2712d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com    [[NSNotificationCenter defaultCenter] removeObserver:self];
2812d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com    [super dealloc];
2912d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com}
3012d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com
3112d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com-(BOOL) acceptsFirstResponder {
3212d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com    return YES;
3312d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com}
3412d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com
3512d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com//SkEvent Handers
3612d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com- (void)receiveSkEvent:(NSNotification *)notification {
3712d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com    if(SkEvent::ProcessEvent())
3812d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com        SkEvent::SignalNonEmptyQueue();
3912d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com}
4012d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com
4112d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com+ (void)postTimedSkEvent:(NSTimeInterval)timeInterval {
4212d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com    [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self
4312d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com                                   selector:@selector(timerFireMethod:)
4412d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com                                   userInfo:nil repeats:NO];
4512d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com}
4612d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com
4712d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com+ (void)timerFireMethod:(NSTimer*)theTimer {
4812d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com	SkEvent::ServiceQueueTimer();
4912d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com}
5012d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com
5112d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com@end
5212d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com////////////////////////////////////////////////////////////////////////////////
5312d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.comvoid SkEvent::SignalNonEmptyQueue() {
54f3493f0e1f92a8a284adb93ecbf350401e1c7423yangsu@google.com    //post a SkEventClass event to the default notification queue
55f3493f0e1f92a8a284adb93ecbf350401e1c7423yangsu@google.com    NSNotification* notification = [NSNotification notificationWithName:SkEventClass object:nil];
56f3493f0e1f92a8a284adb93ecbf350401e1c7423yangsu@google.com    [[NSNotificationQueue defaultQueue] enqueueNotification:notification
57f3493f0e1f92a8a284adb93ecbf350401e1c7423yangsu@google.com                                               postingStyle:NSPostWhenIdle
58f3493f0e1f92a8a284adb93ecbf350401e1c7423yangsu@google.com                                               coalesceMask:NSNotificationNoCoalescing
59f3493f0e1f92a8a284adb93ecbf350401e1c7423yangsu@google.com                                                   forModes:nil];
6012d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com}
6112d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com
6212d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.comvoid SkEvent::SignalQueueTimer(SkMSec delay) {
6312d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com	if (delay) {
6412d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com        //Convert to seconds
6512d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com        NSTimeInterval ti = delay/(float)SK_MSec1;
6612d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com        [SkEventNotifier postTimedSkEvent:ti];
6712d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com	}
6812d177d1f3b61851c782930d5bd7cce80525deb3yangsu@google.com}
69