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