]> git.donarmstrong.com Git - lilypond.git/blob - lily/dispatcher.cc
c6fedb23741f6b050dd4dffe2768e6bc3fc62727
[lilypond.git] / lily / dispatcher.cc
1 /*
2   dispatcher.cc -- implement Dispatcher
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005-2006 Erik Sandberg  <mandolaerik@gmail.com>
7 */
8
9 #include "dispatcher.hh"
10 #include "international.hh"
11 #include "ly-smobs.icc"
12 #include "stream-event.hh"
13 #include "warn.hh"
14
15 // ES todo: move to lily-guile.hh
16 SCM appendable_list ();
17 void appendable_list_append (SCM l, SCM elt);
18
19 IMPLEMENT_SMOBS (Dispatcher);
20 IMPLEMENT_TYPE_P (Dispatcher, "dispatcher");
21 IMPLEMENT_DEFAULT_EQUAL_P (Dispatcher);
22
23 Dispatcher::~Dispatcher ()
24 {
25 }
26
27 Dispatcher::Dispatcher ()
28 {
29   self_scm_ = SCM_EOL;
30   listeners_ = SCM_EOL;
31   dispatchers_ = SCM_EOL;
32   listen_classes_ = SCM_EOL;
33   smobify_self ();
34   listeners_ = scm_c_make_hash_table (0);
35   priority_count_ = 0;
36 }
37
38 SCM
39 Dispatcher::mark_smob (SCM sm)
40 {
41   Dispatcher *me = (Dispatcher *) SCM_CELL_WORD_1 (sm);
42   scm_gc_mark (me->dispatchers_);
43   scm_gc_mark (me->listen_classes_);
44   return me->listeners_;
45 }
46
47 int
48 Dispatcher::print_smob (SCM s, SCM p, scm_print_state*)
49 {
50   Dispatcher *me = (Dispatcher *) SCM_CELL_WORD_1 (s);
51   scm_puts ("#<Dispatcher ", p);
52   scm_write (scm_vector_to_list (me->listeners_), p);
53   scm_puts (">", p);
54   return 1;
55 }
56
57 /*
58 Event dispatching:
59 - Collect a list of listeners for each relevant class
60 - Send the event to each of these listeners, in increasing priority order.
61   This is done by keeping a priority queue of listener lists,
62   and iteratively send the event to the lowest-priority listener.
63 - An event is never sent twice to listeners with equal priority.
64 */
65 IMPLEMENT_LISTENER (Dispatcher, dispatch);
66 void
67 Dispatcher::dispatch (SCM sev)
68 {
69   Stream_event *ev = unsmob_stream_event (sev);
70   SCM class_symbol = ev->get_property ("class");
71   if (!scm_symbol_p (class_symbol))
72     {
73       warning (_f ("Unknown event class %s", ly_symbol2string (class_symbol).c_str ()));
74       return;
75     }
76
77   SCM class_list = scm_call_1 (ly_lily_module_constant ("ly:make-event-class"), class_symbol);
78   bool sent = false;
79   int num_classes = scm_ilength (class_list);
80
81   /*
82     For each event class there is a list of listeners, which is
83     ordered by priority. Our next task is to call these listeners, in
84     priority order.  A priority queue stores the next element in each
85     listener list, and the lowest priority element is repeatedly
86     extracted and called.
87
88     The priority queue is implemented as a bubble-sorted C
89     array. Using the stack instead of native Scheme datastructures
90     avoids overheads for memory allocation. The queue is usually small
91     (around 2 elements), so the quadratic sorting time is not a
92     problem. (if this changes, it's easy to rewrite this routine using
93     a heap)
94
95     The first step is to collect all listener lists and to initially
96     insert them in the priority queue.
97   */
98   struct { int prio; SCM list; } lists[num_classes+1];
99   int i = 0;
100   for (SCM cl = class_list; scm_is_pair(cl); cl = scm_cdr (cl))
101     {
102       SCM list = scm_hashq_ref (listeners_, scm_car (cl), SCM_EOL);
103       if (!scm_is_pair(list))
104         num_classes--;
105       else
106         {
107           // bubblesort.
108           int prio = scm_to_int (scm_caar (list));
109           int j;
110           for (j = i; j > 0 && lists[j-1].prio > prio; j--)
111             lists[j] = lists[j-1];
112           lists[j].prio = prio;
113           lists[j].list = list;
114           i++;
115         }
116     }
117   lists[num_classes].prio = INT_MAX;
118
119   // Never send an event to two listeners with equal priority.
120   int last_priority = -1;
121   /*
122     Each iteration extracts the lowest-priority element, which is a
123     list of listeners. The first listener is called, and the tail of
124     the list is pushed back into the priority queue.
125   */
126   while (num_classes)
127     {
128       // Send the event, if we haven't already sent it to this target.
129       if (lists[0].prio != last_priority)
130         {
131           // process the listener
132           assert (lists[0].prio > last_priority);
133           last_priority = lists[0].prio;
134
135           Listener *l = unsmob_listener (scm_cdar (lists[0].list));
136           l->listen (ev->self_scm ());
137           sent = true;
138         }
139       // go to the next listener; bubble-sort the class list.
140       SCM next = scm_cdr (lists[0].list);
141       if (!scm_is_pair(next))
142         num_classes--;
143       int prio = (scm_is_pair(next)) ? scm_to_int (scm_caar (next)) : INT_MAX;
144       for (i = 0; prio > lists[i+1].prio; i++)
145         lists[i] = lists[i+1];
146       lists[i].prio = prio;
147       lists[i].list = next;
148     }
149
150   if (!sent)
151     warning (_f ("Junking event: %s", ly_symbol2string (class_symbol).c_str ()));
152 }
153
154 void
155 Dispatcher::broadcast (Stream_event *ev)
156 {
157   dispatch (ev->self_scm ());
158 }
159
160 void
161 Dispatcher::add_listener (Listener l, SCM ev_class)
162 {
163   internal_add_listener (l, ev_class, ++priority_count_);
164 }
165
166 inline void
167 Dispatcher::internal_add_listener (Listener l, SCM ev_class, int priority)
168 {
169   SCM list = scm_hashq_ref (listeners_, ev_class, SCM_EOL);
170   if (list == SCM_EOL)
171     {
172       /* Register with all dispatchers. */
173       for (SCM disp = dispatchers_; scm_is_pair(disp); disp = scm_cdr (disp))
174         {
175           int priority = scm_to_int (scm_cdar (disp));
176           Dispatcher *d = unsmob_dispatcher (scm_caar (disp));
177           d->internal_add_listener (GET_LISTENER (dispatch), ev_class, priority);
178         }
179       listen_classes_ = scm_cons (ev_class, listen_classes_);
180     }
181   SCM entry = scm_cons (scm_int2num (priority), l.smobbed_copy ());
182   list = scm_merge_x (list, scm_list_1 (entry), ly_lily_module_constant ("car<"));
183   scm_hashq_set_x (listeners_, ev_class, list);
184 }
185
186 void
187 Dispatcher::remove_listener (Listener l, SCM ev_class)
188 {
189   SCM list = scm_hashq_ref (listeners_, ev_class, SCM_EOL);
190
191   if (list == SCM_EOL)
192     {
193       programming_error ("remove_listener called with incorrect class.");
194       return;
195     }
196
197   // We just remove the listener once.
198   bool first = true;
199
200   SCM dummy = scm_cons (SCM_EOL, list);
201   SCM e = dummy;
202   while (scm_is_pair(scm_cdr (e)))
203     if (*unsmob_listener (scm_cdadr (e)) == l && first)
204       {
205         scm_set_cdr_x (e, scm_cddr(e));
206         first = false;
207         break;
208       }
209     else
210       e = scm_cdr (e);
211   list = scm_cdr (dummy);
212
213   if (first)
214     warning ("Attempting to remove nonexisting listener.");
215   else if (list == SCM_EOL)
216     {
217       /* Unregister with all dispatchers. */
218       for (SCM disp = dispatchers_; disp != SCM_EOL; disp = scm_cdr (disp))
219         {
220           Dispatcher *d = unsmob_dispatcher (scm_caar (disp));
221           d->remove_listener (GET_LISTENER (dispatch), ev_class);
222         }
223       listen_classes_ = scm_delq_x (ev_class, listen_classes_);
224     }
225 }
226
227 /* Register as a listener to another dispatcher. */
228 void
229 Dispatcher::register_as_listener (Dispatcher *disp)
230 {
231   int priority = ++disp->priority_count_;
232
233   // Don't register twice to the same dispatcher.
234   if (scm_assq (disp->self_scm (), dispatchers_) != SCM_BOOL_F)
235     {
236       warning ("Already listening to dispatcher, ignoring request");
237       return;
238     }
239
240   dispatchers_ = scm_acons (disp->self_scm (), scm_int2num (priority), dispatchers_);
241
242   Listener list = GET_LISTENER (dispatch);
243   for (SCM cl = listen_classes_; cl != SCM_EOL; cl = scm_cdr (cl))
244     {
245       disp->internal_add_listener (list, scm_car (cl), priority);
246     }
247 }
248
249 /* Unregister as a listener to another dispatcher. */
250 void
251 Dispatcher::unregister_as_listener (Dispatcher *disp)
252 {
253   dispatchers_ = scm_assq_remove_x (dispatchers_, disp->self_scm ());
254
255   Listener list = GET_LISTENER (dispatch);
256   for (SCM cl = listen_classes_; cl != SCM_EOL; cl = scm_cdr (cl))
257     {
258       disp->remove_listener (list, scm_car (cl));
259     }
260 }