2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
20 #include "global-context.hh"
25 #include "context-def.hh"
26 #include "dispatcher.hh"
27 #include "international.hh"
28 #include "music-iterator.hh"
30 #include "output-def.hh"
33 Global_context::Global_context (Output_def
*o
)
37 definition_
= find_context_def (o
, ly_symbol2scm ("Global"));
39 now_mom_
.set_infinite (-1);
40 prev_mom_
.set_infinite (-1);
42 /* We only need the most basic stuff to bootstrap the context tree */
43 event_source ()->add_listener (GET_LISTENER (create_context_from_event
),
44 ly_symbol2scm ("CreateContext"));
45 event_source ()->add_listener (GET_LISTENER (prepare
),
46 ly_symbol2scm ("Prepare"));
47 events_below ()->register_as_listener (event_source_
);
49 Context_def
*globaldef
= unsmob_context_def (definition_
);
51 programming_error ("no `Global' context found");
53 globaldef
->apply_default_property_operations (this);
54 accepts_list_
= scm_list_1 (ly_symbol2scm ("Score"));
58 Global_context::get_output_def () const
64 Global_context::add_moment_to_process (Moment m
)
67 programming_error ("trying to freeze in time");
69 for (vsize i
= 0; i
< extra_mom_pq_
.size (); i
++)
70 if (extra_mom_pq_
[i
] == m
)
72 extra_mom_pq_
.insert (m
);
76 Global_context::sneaky_insert_extra_moment (Moment w
)
78 while (extra_mom_pq_
.size () && extra_mom_pq_
.front () <= w
)
79 w
= extra_mom_pq_
.get ();
84 Global_context::get_moments_left () const
86 return extra_mom_pq_
.size ();
89 IMPLEMENT_LISTENER (Global_context
, prepare
);
91 Global_context::prepare (SCM sev
)
93 Stream_event
*ev
= unsmob_stream_event (sev
);
94 Moment
*mom
= unsmob_moment (ev
->get_property ("moment"));
98 if (prev_mom_
.main_part_
.is_infinity () && prev_mom_
< 0)
101 prev_mom_
= now_mom_
;
106 Global_context::now_mom () const
112 Global_context::get_score_context () const
114 return (scm_is_pair (context_list_
))
115 ? unsmob_context (scm_car (context_list_
))
120 Global_context::get_output ()
122 Context
* c
= get_score_context ();
124 return c
->get_property ("output");
130 Global_context::run_iterator_on_me (Music_iterator
*iter
)
132 prev_mom_
.set_infinite (-1);
133 now_mom_
.set_infinite (-1);
134 Moment final_mom
= iter
->get_music ()->get_length ();
137 while (iter
->ok () || get_moments_left ())
142 w
= iter
->pending_moment ();
144 w
= sneaky_insert_extra_moment (w
);
145 if (w
.main_part_
.is_infinity () || w
> final_mom
)
150 programming_error ("Moment is not increasing. Aborting interpretation.");
158 Need this to get grace notes at start of a piece correct.
161 set_property ("measurePosition", w
.smobbed_copy ());
164 send_stream_event (this, "Prepare", 0,
165 ly_symbol2scm ("moment"), w
.smobbed_copy ());
170 send_stream_event (this, "OneTimeStep", 0, 0);
171 apply_finalizations ();
177 Global_context::apply_finalizations ()
179 SCM lst
= get_property ("finalizations");
180 set_property ("finalizations", SCM_EOL
);
181 for (SCM s
= lst
; scm_is_pair (s
); s
= scm_cdr (s
))
183 /* TODO: make safe. */
184 scm_primitive_eval (scm_car (s
));
187 /* Add a function to execute before stepping to the next time step. */
189 Global_context::add_finalization (SCM x
)
191 SCM lst
= get_property ("finalizations");
192 lst
= scm_cons (x
, lst
);
193 set_property ("finalizations", lst
);
197 Global_context::previous_moment () const
203 Global_context::get_default_interpreter (string
/* context_id */)
205 if (get_score_context ())
206 return get_score_context ()->get_default_interpreter ();
208 return Context::get_default_interpreter ();
212 unsmob_global_context (SCM x
)
214 return dynamic_cast<Global_context
*> (unsmob_context (x
));