DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame^] | 1 | // |
| 2 | // "$Id: Fl_add_idle.cxx 7903 2010-11-28 21:06:39Z matt $" |
| 3 | // |
| 4 | // Idle routine support for the Fast Light Tool Kit (FLTK). |
| 5 | // |
| 6 | // Copyright 1998-2010 by Bill Spitzak and others. |
| 7 | // |
| 8 | // This library is free software; you can redistribute it and/or |
| 9 | // modify it under the terms of the GNU Library General Public |
| 10 | // License as published by the Free Software Foundation; either |
| 11 | // version 2 of the License, or (at your option) any later version. |
| 12 | // |
| 13 | // This library is distributed in the hope that it will be useful, |
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | // Library General Public License for more details. |
| 17 | // |
| 18 | // You should have received a copy of the GNU Library General Public |
| 19 | // License along with this library; if not, write to the Free Software |
| 20 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 21 | // USA. |
| 22 | // |
| 23 | // Please report all bugs and problems on the following page: |
| 24 | // |
| 25 | // http://www.fltk.org/str.php |
| 26 | // |
| 27 | |
| 28 | // Allows you to manage an arbitrary set of idle() callbacks. |
| 29 | // Replaces the older set_idle() call (which is used to implement this) |
| 30 | |
| 31 | #include <FL/Fl.H> |
| 32 | |
| 33 | struct idle_cb { |
| 34 | void (*cb)(void*); |
| 35 | void* data; |
| 36 | idle_cb *next; |
| 37 | }; |
| 38 | |
| 39 | // the callbacks are stored linked in a ring. last points at the one |
| 40 | // just called, first at the next to call. last->next == first. |
| 41 | |
| 42 | static idle_cb* first; |
| 43 | static idle_cb* last; |
| 44 | static idle_cb* freelist; |
| 45 | |
| 46 | static void call_idle() { |
| 47 | idle_cb* p = first; |
| 48 | last = p; first = p->next; |
| 49 | p->cb(p->data); // this may call add_idle() or remove_idle()! |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | Adds a callback function that is called every time by Fl::wait() and also |
| 54 | makes it act as though the timeout is zero (this makes Fl::wait() return |
| 55 | immediately, so if it is in a loop it is called repeatedly, and thus the |
| 56 | idle fucntion is called repeatedly). The idle function can be used to get |
| 57 | background processing done. |
| 58 | |
| 59 | You can have multiple idle callbacks. To remove an idle callback use |
| 60 | Fl::remove_idle(). |
| 61 | |
| 62 | Fl::wait() and Fl::check() call idle callbacks, but Fl::ready() does not. |
| 63 | |
| 64 | The idle callback can call any FLTK functions, including Fl::wait(), |
| 65 | Fl::check(), and Fl::ready(). |
| 66 | |
| 67 | FLTK will not recursively call the idle callback. |
| 68 | */ |
| 69 | void Fl::add_idle(Fl_Idle_Handler cb, void* data) { |
| 70 | idle_cb* p = freelist; |
| 71 | if (p) freelist = p->next; |
| 72 | else p = new idle_cb; |
| 73 | p->cb = cb; |
| 74 | p->data = data; |
| 75 | if (first) { |
| 76 | last->next = p; |
| 77 | last = p; |
| 78 | p->next = first; |
| 79 | } else { |
| 80 | first = last = p; |
| 81 | p->next = p; |
| 82 | set_idle(call_idle); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | Returns true if the specified idle callback is currently installed. |
| 88 | */ |
| 89 | int Fl::has_idle(Fl_Idle_Handler cb, void* data) { |
| 90 | idle_cb* p = first; |
| 91 | if (!p) return 0; |
| 92 | for (;; p = p->next) { |
| 93 | if (p->cb == cb && p->data == data) return 1; |
| 94 | if (p==last) return 0; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | Removes the specified idle callback, if it is installed. |
| 100 | */ |
| 101 | void Fl::remove_idle(Fl_Idle_Handler cb, void* data) { |
| 102 | idle_cb* p = first; |
| 103 | if (!p) return; |
| 104 | idle_cb* l = last; |
| 105 | for (;; p = p->next) { |
| 106 | if (p->cb == cb && p->data == data) break; |
| 107 | if (p==last) return; // not found |
| 108 | l = p; |
| 109 | } |
| 110 | if (l == p) { // only one |
| 111 | first = last = 0; |
| 112 | set_idle(0); |
| 113 | } else { |
| 114 | last = l; |
| 115 | first = l->next = p->next; |
| 116 | } |
| 117 | p->next = freelist; |
| 118 | freelist = p; |
| 119 | } |
| 120 | |
| 121 | // |
| 122 | // End of "$Id: Fl_add_idle.cxx 7903 2010-11-28 21:06:39Z matt $". |
| 123 | // |