1998-10-20 00:46:58 +04:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// "$Id$"
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// Idle routine support for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2010-11-29 00:06:39 +03:00
|
|
|
// Copyright 1998-2010 by Bill Spitzak and others.
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library 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
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
// USA.
|
|
|
|
//
|
2005-04-16 04:13:17 +04:00
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
// Allows you to manage an arbitrary set of idle() callbacks.
|
|
|
|
// Replaces the older set_idle() call (which is used to implement this)
|
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
|
|
|
|
struct idle_cb {
|
|
|
|
void (*cb)(void*);
|
|
|
|
void* data;
|
|
|
|
idle_cb *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
// the callbacks are stored linked in a ring. last points at the one
|
|
|
|
// just called, first at the next to call. last->next == first.
|
|
|
|
|
|
|
|
static idle_cb* first;
|
|
|
|
static idle_cb* last;
|
|
|
|
static idle_cb* freelist;
|
|
|
|
|
|
|
|
static void call_idle() {
|
|
|
|
idle_cb* p = first;
|
|
|
|
last = p; first = p->next;
|
|
|
|
p->cb(p->data); // this may call add_idle() or remove_idle()!
|
|
|
|
}
|
|
|
|
|
2008-09-14 02:33:03 +04:00
|
|
|
/**
|
2008-10-04 19:54:15 +04:00
|
|
|
Adds a callback function that is called every time by Fl::wait() and also
|
|
|
|
makes it act as though the timeout is zero (this makes Fl::wait() return
|
|
|
|
immediately, so if it is in a loop it is called repeatedly, and thus the
|
|
|
|
idle fucntion is called repeatedly). The idle function can be used to get
|
|
|
|
background processing done.
|
2008-09-14 02:33:03 +04:00
|
|
|
|
2008-10-04 19:54:15 +04:00
|
|
|
You can have multiple idle callbacks. To remove an idle callback use
|
|
|
|
Fl::remove_idle().
|
2008-09-14 02:33:03 +04:00
|
|
|
|
2008-10-04 19:54:15 +04:00
|
|
|
Fl::wait() and Fl::check() call idle callbacks, but Fl::ready() does not.
|
2008-09-14 02:33:03 +04:00
|
|
|
|
2008-10-04 19:54:15 +04:00
|
|
|
The idle callback can call any FLTK functions, including Fl::wait(),
|
|
|
|
Fl::check(), and Fl::ready().
|
|
|
|
|
|
|
|
FLTK will not recursively call the idle callback.
|
2008-09-14 02:33:03 +04:00
|
|
|
*/
|
2010-03-25 17:37:46 +03:00
|
|
|
void Fl::add_idle(Fl_Idle_Handler cb, void* data) {
|
1998-10-06 22:21:25 +04:00
|
|
|
idle_cb* p = freelist;
|
|
|
|
if (p) freelist = p->next;
|
|
|
|
else p = new idle_cb;
|
|
|
|
p->cb = cb;
|
|
|
|
p->data = data;
|
|
|
|
if (first) {
|
|
|
|
last->next = p;
|
2000-09-05 21:15:48 +04:00
|
|
|
last = p;
|
1998-10-06 22:21:25 +04:00
|
|
|
p->next = first;
|
|
|
|
} else {
|
|
|
|
first = last = p;
|
|
|
|
p->next = p;
|
|
|
|
set_idle(call_idle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-04 19:54:15 +04:00
|
|
|
/**
|
|
|
|
Returns true if the specified idle callback is currently installed.
|
|
|
|
*/
|
2010-03-25 17:37:46 +03:00
|
|
|
int Fl::has_idle(Fl_Idle_Handler cb, void* data) {
|
2001-04-13 21:22:43 +04:00
|
|
|
idle_cb* p = first;
|
|
|
|
if (!p) return 0;
|
|
|
|
for (;; p = p->next) {
|
2000-06-16 11:28:03 +04:00
|
|
|
if (p->cb == cb && p->data == data) return 1;
|
2001-04-13 21:22:43 +04:00
|
|
|
if (p==last) return 0;
|
|
|
|
}
|
2000-06-16 11:28:03 +04:00
|
|
|
}
|
|
|
|
|
2008-10-04 19:54:15 +04:00
|
|
|
/**
|
|
|
|
Removes the specified idle callback, if it is installed.
|
|
|
|
*/
|
2010-03-25 17:37:46 +03:00
|
|
|
void Fl::remove_idle(Fl_Idle_Handler cb, void* data) {
|
1998-10-06 22:21:25 +04:00
|
|
|
idle_cb* p = first;
|
|
|
|
if (!p) return;
|
|
|
|
idle_cb* l = last;
|
|
|
|
for (;; p = p->next) {
|
|
|
|
if (p->cb == cb && p->data == data) break;
|
|
|
|
if (p==last) return; // not found
|
|
|
|
l = p;
|
|
|
|
}
|
|
|
|
if (l == p) { // only one
|
|
|
|
first = last = 0;
|
|
|
|
set_idle(0);
|
|
|
|
} else {
|
|
|
|
last = l;
|
|
|
|
first = l->next = p->next;
|
|
|
|
}
|
|
|
|
p->next = freelist;
|
|
|
|
freelist = p;
|
|
|
|
}
|
1998-10-20 00:46:58 +04:00
|
|
|
|
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// End of "$Id$".
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|