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