2008-08-02 18:31:32 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Chris Young <chris@unsatisfactorysoftware.co.uk>
|
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "desktop/browser.h"
|
2008-08-03 20:10:10 +04:00
|
|
|
#include "amiga/object.h"
|
|
|
|
#include "amiga/schedule.h"
|
|
|
|
#include <proto/exec.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedule a callback.
|
|
|
|
*
|
|
|
|
* \param t interval before the callback should be made / cs
|
|
|
|
* \param callback callback function
|
|
|
|
* \param p user parameter, passed to callback function
|
|
|
|
*
|
|
|
|
* The callback function will be called as soon as possible after t cs have
|
|
|
|
* passed.
|
|
|
|
*/
|
2008-08-02 18:31:32 +04:00
|
|
|
|
|
|
|
void schedule(int t, void (*callback)(void *p), void *p)
|
|
|
|
{
|
2008-08-03 20:10:10 +04:00
|
|
|
struct nsObject *obj;
|
|
|
|
struct nscallback *nscb;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
obj = AddObject(schedule_list,AMINS_CALLBACK);
|
2008-09-09 10:25:22 +04:00
|
|
|
if(!obj) return;
|
|
|
|
|
2008-08-03 20:10:10 +04:00
|
|
|
obj->objstruct_size = sizeof(struct nscallback);
|
2008-11-02 03:21:49 +03:00
|
|
|
obj->objstruct = AllocVec(obj->objstruct_size,MEMF_PRIVATE | MEMF_CLEAR);
|
2008-09-09 10:25:22 +04:00
|
|
|
if(!obj->objstruct)
|
|
|
|
{
|
|
|
|
DelObject(obj);
|
|
|
|
return;
|
|
|
|
}
|
2008-08-03 20:10:10 +04:00
|
|
|
|
|
|
|
nscb = (struct nscallback *)obj->objstruct;
|
|
|
|
|
2008-12-20 13:54:59 +03:00
|
|
|
nscb->tv.Seconds = 0;
|
|
|
|
nscb->tv.Microseconds = t*10000;
|
2008-09-08 22:14:14 +04:00
|
|
|
|
2008-12-20 13:54:59 +03:00
|
|
|
while(nscb->tv.Microseconds >= 1000000)
|
2008-09-08 22:14:14 +04:00
|
|
|
{
|
2008-12-20 13:54:59 +03:00
|
|
|
nscb->tv.Seconds++;
|
|
|
|
nscb->tv.Microseconds -= 1000000;
|
2008-09-08 22:14:14 +04:00
|
|
|
}
|
|
|
|
|
2008-08-03 20:10:10 +04:00
|
|
|
GetSysTime(&tv);
|
2008-09-08 22:14:14 +04:00
|
|
|
AddTime(&nscb->tv,&tv); // now contains time when event occurs
|
2009-10-16 23:49:56 +04:00
|
|
|
#ifdef AMI_SCHEDULER_USES_TIMER
|
2009-10-16 01:35:29 +04:00
|
|
|
if(nscb->treq = AllocVec(sizeof(struct TimeRequest),MEMF_PRIVATE | MEMF_CLEAR))
|
2008-09-09 10:25:22 +04:00
|
|
|
{
|
|
|
|
*nscb->treq = *tioreq;
|
2008-12-20 13:54:59 +03:00
|
|
|
nscb->treq->Request.io_Command=TR_ADDREQUEST;
|
2009-10-16 01:35:29 +04:00
|
|
|
nscb->treq->Time.Seconds=nscb->tv.Seconds; // secs
|
2008-12-20 13:54:59 +03:00
|
|
|
nscb->treq->Time.Microseconds=nscb->tv.Microseconds; // micro
|
2008-09-09 10:25:22 +04:00
|
|
|
SendIO((struct IORequest *)nscb->treq);
|
|
|
|
}
|
2009-10-16 23:49:56 +04:00
|
|
|
#endif
|
2008-08-03 20:10:10 +04:00
|
|
|
nscb->callback = callback;
|
|
|
|
nscb->p = p;
|
2008-08-02 18:31:32 +04:00
|
|
|
}
|
|
|
|
|
2008-08-03 20:10:10 +04:00
|
|
|
/**
|
|
|
|
* Unschedule a callback.
|
|
|
|
*
|
|
|
|
* \param callback callback function
|
|
|
|
* \param p user parameter, passed to callback function
|
|
|
|
*
|
|
|
|
* All scheduled callbacks matching both callback and p are removed.
|
|
|
|
*/
|
|
|
|
|
2008-08-02 18:31:32 +04:00
|
|
|
void schedule_remove(void (*callback)(void *p), void *p)
|
|
|
|
{
|
2008-08-03 20:10:10 +04:00
|
|
|
struct nsObject *node;
|
|
|
|
struct nsObject *nnode;
|
|
|
|
struct nscallback *nscb;
|
|
|
|
|
|
|
|
if(IsMinListEmpty(schedule_list)) return;
|
|
|
|
|
2008-10-06 22:20:16 +04:00
|
|
|
node = (struct nsObject *)GetHead((struct List *)schedule_list);
|
2008-08-03 20:10:10 +04:00
|
|
|
|
2008-11-03 22:21:40 +03:00
|
|
|
do
|
2008-08-03 20:10:10 +04:00
|
|
|
{
|
2008-11-03 22:21:40 +03:00
|
|
|
nnode=(struct nsObject *)GetSucc((struct Node *)node);
|
|
|
|
|
2008-08-03 20:10:10 +04:00
|
|
|
nscb = node->objstruct;
|
2008-09-08 22:14:14 +04:00
|
|
|
if(!nscb) continue;
|
2008-08-03 20:10:10 +04:00
|
|
|
|
|
|
|
if((nscb->callback == callback) && (nscb->p == p))
|
|
|
|
{
|
2008-09-08 22:14:14 +04:00
|
|
|
ami_remove_timer_event(nscb);
|
2008-08-03 20:10:10 +04:00
|
|
|
DelObject(node);
|
|
|
|
}
|
2008-11-03 22:21:40 +03:00
|
|
|
|
|
|
|
}while (node=nnode);
|
2008-08-02 18:31:32 +04:00
|
|
|
}
|
|
|
|
|
2008-08-03 20:10:10 +04:00
|
|
|
/**
|
|
|
|
* Process events up to current time.
|
|
|
|
*/
|
|
|
|
|
2010-04-29 22:22:04 +04:00
|
|
|
BOOL schedule_run(void)
|
2008-08-02 18:31:32 +04:00
|
|
|
{
|
2008-08-03 20:10:10 +04:00
|
|
|
struct nsObject *node;
|
|
|
|
struct nsObject *nnode;
|
|
|
|
struct nscallback *nscb;
|
|
|
|
void (*callback)(void *p);
|
2008-08-17 20:24:22 +04:00
|
|
|
void *p;
|
2008-08-03 20:10:10 +04:00
|
|
|
struct timeval tv;
|
|
|
|
|
2009-02-12 01:33:55 +03:00
|
|
|
if(IsMinListEmpty(schedule_list)) return false;
|
2008-08-03 20:10:10 +04:00
|
|
|
|
|
|
|
GetSysTime(&tv);
|
|
|
|
|
2008-10-06 22:20:16 +04:00
|
|
|
node = (struct nsObject *)GetHead((struct List *)schedule_list);
|
2008-08-03 20:10:10 +04:00
|
|
|
|
2008-10-06 22:20:16 +04:00
|
|
|
do
|
2008-08-03 20:10:10 +04:00
|
|
|
{
|
2008-10-06 22:20:16 +04:00
|
|
|
nnode=(struct nsObject *)GetSucc((struct Node *)node);
|
|
|
|
|
2008-08-03 20:10:10 +04:00
|
|
|
if((node->Type == AMINS_CALLBACK) && (node->objstruct))
|
|
|
|
{
|
|
|
|
nscb = node->objstruct;
|
|
|
|
|
|
|
|
if(CmpTime(&tv,&nscb->tv) <= 0)
|
|
|
|
{
|
|
|
|
callback = nscb->callback;
|
2008-08-17 20:24:22 +04:00
|
|
|
p = nscb->p;
|
2008-09-08 22:14:14 +04:00
|
|
|
ami_remove_timer_event(nscb);
|
2008-08-03 20:10:10 +04:00
|
|
|
DelObject(node);
|
2008-08-17 20:24:22 +04:00
|
|
|
callback(p);
|
2008-08-03 20:10:10 +04:00
|
|
|
}
|
|
|
|
}
|
2008-10-06 22:20:16 +04:00
|
|
|
} while(node=nnode);
|
2009-02-12 01:33:55 +03:00
|
|
|
|
|
|
|
return true;
|
2008-08-02 18:31:32 +04:00
|
|
|
}
|
2008-09-08 22:14:14 +04:00
|
|
|
|
|
|
|
void ami_remove_timer_event(struct nscallback *nscb)
|
|
|
|
{
|
2010-04-29 02:19:10 +04:00
|
|
|
#ifdef AMI_SCHEDULER_USES_TIMER
|
2008-09-08 22:14:14 +04:00
|
|
|
if(!nscb) return;
|
|
|
|
|
|
|
|
if(nscb->treq)
|
|
|
|
{
|
2008-09-09 10:25:22 +04:00
|
|
|
if(CheckIO((struct IORequest *)nscb->treq)==NULL)
|
2008-09-08 22:14:14 +04:00
|
|
|
AbortIO((struct IORequest *)nscb->treq);
|
|
|
|
|
|
|
|
WaitIO((struct IORequest *)nscb->treq);
|
|
|
|
FreeVec(nscb->treq);
|
|
|
|
}
|
2010-04-29 02:19:10 +04:00
|
|
|
#endif
|
2008-09-08 22:14:14 +04:00
|
|
|
}
|