sdltimer.cpp

Go to the documentation of this file.
00001 /*
00002   libwt - Vassilis Virvilis Toolkit - a widget library
00003   Copyright (C) 2006 Vassilis Virvilis <vasvir2@fastmail.fm>
00004  
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009   
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014   
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the
00017   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018   Boston, MA  02111-1307, SA.
00019 */
00020 
00021 #include <SDL/SDL_events.h>
00022 
00023 #include "sdltimer.h"
00024 #include "sdlevent.h"
00025 #include <wt/event.h>
00026 #include <wt/application.h>
00027 
00028 namespace Wt {
00029 
00030 /*! Timer callback. Executed by timer (other) thread
00031  It is safe since we only pushing to SDL event queue.
00032  At that stage SDLTimer object is not dead. However
00033  it is possible (legal) for the SDLTimer to die after
00034  this  callback invocation */
00035 static Uint32 timer_callback(Uint32 interval, void *param) {
00036     SDL_Event event;
00037 
00038     event.type = SDLEvent::Time;
00039     event.user.code = 0;
00040     event.user.data1 = new
00041                        SDLTimer::Event(* static_cast<SDLTimer::Event *>(param));
00042     event.user.data2 = 0;
00043 
00044     trace("sdltimer", "timer_callback called\n");
00045 
00046     SDLEvent::push(&event);
00047 
00048     return interval;
00049 }
00050 
00051 SDLTimer::SDLTimer(Object *receiver, int id, int interval) :
00052         sdl_timer_event(Event(receiver, id)),
00053         timerid_(interval ? SDL_AddTimer(interval,
00054                                          timer_callback, &sdl_timer_event) :
00055                  0),
00056 interval_(interval) {
00057     scheduleNext();
00058 }
00059 
00060 SDLTimer::~SDLTimer() {
00061     if (timerid_)
00062         SDL_RemoveTimer(timerid_);
00063 }
00064 
00065 void SDLTimer::scheduleNext() {
00066     if (!interval_)
00067         timer_callback(interval_, &sdl_timer_event);
00068 }
00069 
00070 void SDLTimer::init() {
00071     SDLEvent& sdlevent = *SDLEvent::instance();
00072     sdlevent[SDLEvent::Time] = &handleTimerEvent;
00073 }
00074 
00075 void SDLTimer::quit() {}
00076 
00077 void SDLTimer::handleTimerEvent(const SDL_Event *event) {
00078     Event *sdl_timer_event = static_cast<Event *>(event->user.data1);
00079     Application::postEvent(sdl_timer_event->receiver(),
00080                            new TimerEvent(sdl_timer_event->id()));
00081     delete sdl_timer_event;
00082 }
00083 
00084 } // namespace

Generated Fri Jul 28 19:23:01 2006.
Copyright © 1998-2003 by the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.