]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-context.cc
b9afbd7671d51ee680030f14dcf40cb8bdd6586c
[lilypond.git] / lily / global-context.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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 "global-context.hh"
21
22 #include <cstdio>
23 using namespace std;
24
25 #include "context-def.hh"
26 #include "dispatcher.hh"
27 #include "international.hh"
28 #include "music-iterator.hh"
29 #include "music.hh"
30 #include "output-def.hh"
31 #include "warn.hh"
32
33 Global_context::Global_context (Output_def *o)
34   : Context ()
35 {
36   output_def_ = o;
37   definition_ = find_context_def (o, ly_symbol2scm ("Global"));
38
39   now_mom_.set_infinite (-1);
40   prev_mom_.set_infinite (-1);
41
42   /* We only need the most basic stuff to bootstrap the context tree */
43   event_source ()->add_listener (GET_LISTENER (Context, create_context_from_event),
44                                  ly_symbol2scm ("CreateContext"));
45   event_source ()->add_listener (GET_LISTENER (Global_context, prepare),
46                                  ly_symbol2scm ("Prepare"));
47   events_below ()->register_as_listener (event_source_);
48
49   Context_def *globaldef = Context_def::unsmob (definition_);
50   if (!globaldef)
51     programming_error ("no `Global' context found");
52   else
53     globaldef->apply_default_property_operations (this);
54
55   default_child_ = ly_symbol2scm ("Score");
56   accepts_list_ = scm_list_1 (default_child_);
57 }
58
59 Output_def *
60 Global_context::get_output_def () const
61 {
62   return output_def_;
63 }
64
65 void
66 Global_context::add_moment_to_process (Moment m)
67 {
68   if (m < now_mom_)
69     programming_error ("trying to freeze in time");
70
71   for (vsize i = 0; i < extra_mom_pq_.size (); i++)
72     if (extra_mom_pq_[i] == m)
73       return;
74   extra_mom_pq_.insert (m);
75 }
76
77 Moment
78 Global_context::sneaky_insert_extra_moment (Moment w)
79 {
80   while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
81     w = extra_mom_pq_.get ();
82   return w;
83 }
84
85 int
86 Global_context::get_moments_left () const
87 {
88   return extra_mom_pq_.size ();
89 }
90
91 void
92 Global_context::prepare (SCM sev)
93 {
94   Stream_event *ev = Stream_event::unsmob (sev);
95   Moment *mom = Moment::unsmob (ev->get_property ("moment"));
96
97   assert (mom);
98
99   if (prev_mom_.main_part_.is_infinity () && prev_mom_ < 0)
100     prev_mom_ = *mom;
101   else
102     prev_mom_ = now_mom_;
103   now_mom_ = *mom;
104 }
105
106 Moment
107 Global_context::now_mom () const
108 {
109   return now_mom_;
110 }
111
112 Context *
113 Global_context::get_score_context () const
114 {
115   return (scm_is_pair (context_list_))
116          ? Context::unsmob (scm_car (context_list_))
117          : 0;
118 }
119
120 SCM
121 Global_context::get_output ()
122 {
123   Context *c = get_score_context ();
124   if (c)
125     return c->get_property ("output");
126   else
127     return SCM_EOL;
128 }
129
130 void
131 Global_context::run_iterator_on_me (Music_iterator *iter)
132 {
133   prev_mom_.set_infinite (-1);
134   now_mom_.set_infinite (-1);
135   Moment final_mom = iter->get_music ()->get_length ();
136
137   bool first = true;
138   while (iter->ok () || get_moments_left ())
139     {
140       Moment w;
141       w.set_infinite (1);
142       if (iter->ok ())
143         w = iter->pending_moment ();
144
145       w = sneaky_insert_extra_moment (w);
146       if (w.main_part_.is_infinity () || w > final_mom)
147         break;
148
149       if (w == prev_mom_)
150         {
151           programming_error ("Moment is not increasing."
152                              "  Aborting interpretation.");
153           break;
154         }
155
156       if (first)
157         {
158           /*
159             Need this to get grace notes at start of a piece correct.
160           */
161           first = false;
162           set_property ("measurePosition", w.smobbed_copy ());
163         }
164
165       send_stream_event (this, "Prepare", 0,
166                          ly_symbol2scm ("moment"), w.smobbed_copy ());
167
168       if (iter->ok ())
169         iter->process (w);
170
171       send_stream_event (this, "OneTimeStep", 0, 0);
172       apply_finalizations ();
173       check_removal ();
174     }
175 }
176
177 void
178 Global_context::apply_finalizations ()
179 {
180   SCM lst = get_property ("finalizations");
181   set_property ("finalizations", SCM_EOL);
182   for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
183     scm_apply_0 (scm_caar (s), scm_cdar (s));
184 }
185
186 /* Add a function to execute before stepping to the next time step.  */
187 void
188 Global_context::add_finalization (SCM x)
189 {
190   SCM lst = get_property ("finalizations");
191   lst = scm_cons (x, lst);
192   set_property ("finalizations", lst);
193 }
194
195 Moment
196 Global_context::previous_moment () const
197 {
198   return prev_mom_;
199 }
200
201 Context *
202 Global_context::get_default_interpreter (const string &/* context_id */)
203 {
204   if (get_score_context ())
205     return get_score_context ()->get_default_interpreter ();
206   else
207     return Context::get_default_interpreter ();
208 }