]> git.donarmstrong.com Git - lilypond.git/blob - lily/dispatcher-scheme.cc
Doc-es: various updates.
[lilypond.git] / lily / dispatcher-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2006--2015 Erik Sandberg  <mandolaerik@gmail.com>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "dispatcher.hh"
21
22 LY_DEFINE (ly_make_dispatcher, "ly:make-dispatcher",
23            0, 0, 0, (),
24            "Return a newly created dispatcher.")
25 {
26   return (new Dispatcher ())->unprotect ();
27 }
28
29 LY_DEFINE (ly_connect_dispatchers, "ly:connect-dispatchers",
30            2, 0, 0, (SCM to, SCM from),
31            "Make the dispatcher @var{to} listen to events from @var{from}.")
32 {
33   Dispatcher *t = unsmob<Dispatcher> (to);
34   Dispatcher *f = unsmob<Dispatcher> (from);
35
36   LY_ASSERT_SMOB (Dispatcher, to, 1);
37   LY_ASSERT_SMOB (Dispatcher, from, 2);
38
39   t->register_as_listener (f);
40
41   return SCM_UNSPECIFIED;
42 }
43
44 LY_DEFINE (ly_disconnect_dispatchers, "ly:disconnect-dispatchers",
45            2, 0, 0, (SCM to, SCM from),
46            "Stop the dispatcher @var{to} listening to events from @var{from}.")
47 {
48   Dispatcher *t = unsmob<Dispatcher> (to);
49   Dispatcher *f = unsmob<Dispatcher> (from);
50
51   LY_ASSERT_SMOB (Dispatcher, to, 1);
52   LY_ASSERT_SMOB (Dispatcher, from, 2);
53
54   t->unregister_as_listener (f);
55
56   return SCM_UNSPECIFIED;
57 }
58
59 LY_DEFINE (ly_add_listener, "ly:add-listener",
60            2, 0, 1, (SCM callback, SCM disp, SCM cl),
61            "Add the single-argument procedure @var{callback} as listener"
62            " to the dispatcher @var{disp}.  Whenever @var{disp} hears"
63            " an event of class @var{cl}, it calls @var{callback} with it.")
64 {
65   Dispatcher *d = unsmob<Dispatcher> (disp);
66
67   LY_ASSERT_TYPE (ly_is_procedure, callback, 1);
68   LY_ASSERT_SMOB (Dispatcher, disp, 2);
69
70   for (int arg = SCM_ARG3; scm_is_pair (cl); cl = scm_cdr (cl), arg++)
71     {
72       SCM sym = scm_car (cl);
73       SCM_ASSERT_TYPE (scm_is_symbol (sym), sym, arg, __FUNCTION__, "symbol");
74       d->add_listener (callback, sym);
75     }
76
77   return SCM_UNSPECIFIED;
78 }
79
80 LY_DEFINE (ly_listened_event_types, "ly:listened-event-types",
81            1, 0, 0, (SCM disp),
82            "Return a list of all event types that @var{disp} listens"
83            " to.")
84 {
85   LY_ASSERT_SMOB (Dispatcher, disp, 1);
86
87   SCM result = unsmob<Dispatcher> (disp)->listened_types ();
88
89   scm_remember_upto_here_1 (disp);
90
91   return result;
92 }
93
94 LY_DEFINE (ly_listened_event_class_p, "ly:listened-event-class?",
95            2, 0, 0, (SCM disp, SCM cl),
96            "Does @var{disp} listen to any event type in the list"
97            " @var{cl}?")
98 {
99   LY_ASSERT_SMOB (Dispatcher, disp, 1);
100   LY_ASSERT_TYPE (scm_is_pair, cl, 2);
101
102   bool result = unsmob<Dispatcher> (disp)->is_listened_class (cl);
103
104   scm_remember_upto_here_1 (disp);
105
106   return scm_from_bool (result);
107 }
108
109 LY_DEFINE (ly_broadcast, "ly:broadcast",
110            2, 0, 0, (SCM disp, SCM ev),
111            "Send the stream event @var{ev} to the dispatcher @var{disp}.")
112 {
113   Dispatcher *d = unsmob<Dispatcher> (disp);
114   Stream_event *e = unsmob<Stream_event> (ev);
115
116   LY_ASSERT_SMOB (Dispatcher, disp, 1);
117
118   LY_ASSERT_SMOB (Stream_event, ev, 2);
119   d->broadcast (e);
120   return SCM_UNSPECIFIED;
121 }