]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-context.cc
* lily/ various: Introduce stream events of types Prepare,
[lilypond.git] / lily / global-context.cc
1 /*
2   global-context.cc -- implement Global_context
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "global-context.hh"
10
11 #include <cstdio>
12 using namespace std;
13
14 #include "context-def.hh"
15 #include "dispatcher.hh"
16 #include "international.hh"
17 #include "lilypond-key.hh"
18 #include "music-iterator.hh"
19 #include "music.hh"
20 #include "output-def.hh"
21 #include "score-context.hh"
22 #include "stream-event.hh"
23 #include "warn.hh"
24
25 Global_context::Global_context (Output_def *o, Object_key *key)
26   : Context (new Lilypond_context_key (key,
27                                        Moment (0),
28                                        "Global", "", 0))
29 {
30   output_def_ = o;
31   definition_ = find_context_def (o, ly_symbol2scm ("Global"));
32
33   now_mom_.set_infinite (-1);
34   prev_mom_.set_infinite (-1);
35
36   /* We only need the most basic stuff to bootstrap the context tree */
37   event_source ()->add_listener (GET_LISTENER (create_context_from_event),
38                                 ly_symbol2scm ("CreateContext"));
39   event_source ()->add_listener (GET_LISTENER (prepare),
40                                 ly_symbol2scm ("Prepare"));
41   events_below ()->register_as_listener (event_source_);
42
43   Context_def *globaldef = unsmob_context_def (definition_);
44   if (!globaldef)
45     programming_error ("no `Global' context found");
46   else
47     globaldef->apply_default_property_operations (this);
48   accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
49 }
50
51 Output_def *
52 Global_context::get_output_def () const
53 {
54   return output_def_;
55 }
56
57 void
58 Global_context::add_moment_to_process (Moment m)
59 {
60   if (m < now_mom_)
61     programming_error ("trying to freeze in time");
62
63   for (vsize i = 0; i < extra_mom_pq_.size (); i++)
64     if (extra_mom_pq_[i] == m)
65       return;
66   extra_mom_pq_.insert (m);
67 }
68
69 Moment
70 Global_context::sneaky_insert_extra_moment (Moment w)
71 {
72   while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
73     w = extra_mom_pq_.get ();
74   return w;
75 }
76
77 int
78 Global_context::get_moments_left () const
79 {
80   return extra_mom_pq_.size ();
81 }
82
83 IMPLEMENT_LISTENER (Global_context, prepare);
84 void
85 Global_context::prepare (SCM sev)
86 {
87   Stream_event *ev = unsmob_stream_event (sev);
88   Moment *mom = unsmob_moment (ev->get_property ("moment"));
89
90   assert (mom);
91
92   if (prev_mom_.main_part_.is_infinity () && prev_mom_ < 0)
93     prev_mom_ = *mom;
94   else
95     prev_mom_ = now_mom_;
96   now_mom_ = *mom;
97   
98   clear_key_disambiguations ();
99
100   // should do nothing now
101   if (get_score_context ())
102     get_score_context ()->prepare (now_mom_);
103 }
104
105 Moment
106 Global_context::now_mom () const
107 {
108   return now_mom_;
109 }
110
111 Score_context *
112 Global_context::get_score_context () const
113 {
114   return (scm_is_pair (context_list_))
115     ? dynamic_cast<Score_context *> (unsmob_context (scm_car (context_list_)))
116     : 0;
117 }
118
119 SCM
120 Global_context::get_output ()
121 {
122   return get_score_context ()->get_output ();
123 }
124
125 void
126 Global_context::one_time_step ()
127 {
128   get_score_context ()->one_time_step ();
129 }
130
131 void
132 Global_context::finish ()
133 {
134   if (get_score_context ())
135     get_score_context ()->finish ();
136 }
137
138 void
139 Global_context::run_iterator_on_me (Music_iterator *iter)
140 {
141   prev_mom_.set_infinite (-1);
142   now_mom_.set_infinite (-1);
143   Moment final_mom = iter->get_music ()->get_length ();
144
145   bool first = true;
146   while (iter->ok () || get_moments_left ())
147     {
148       Moment w;
149       w.set_infinite (1);
150       if (iter->ok ())
151         w = iter->pending_moment ();
152
153       w = sneaky_insert_extra_moment (w);
154       if (w.main_part_.is_infinity () || w > final_mom)
155         break;
156
157       if (first)
158         {
159           /*
160             Need this to get grace notes at start of a piece correct.
161           */
162           first = false;
163           set_property ("measurePosition", w.smobbed_copy ());
164         }
165
166       send_stream_event (this, "Prepare", 0,
167                          ly_symbol2scm ("moment"), w.smobbed_copy ());
168
169       if (iter->ok ())
170         iter->process (w);
171
172       if (!get_score_context ())
173         {
174           error ("ES TODO: no score context, this shouldn't happen");
175           SCM sym = ly_symbol2scm ("Score");
176           Context_def *t = unsmob_context_def (find_context_def (get_output_def (),
177                                                                  sym));
178           if (!t)
179             error (_f ("can't find `%s' context", "Score"));
180
181           Object_key const *key = get_context_key ("Score", "");
182           Context *c = t->instantiate (SCM_EOL, key);
183           add_context (c);
184
185           Score_context *sc = dynamic_cast<Score_context *> (c);
186           sc->prepare (w);
187         }
188
189       send_stream_event (this, "OneTimeStep", 0, 0);
190       apply_finalizations ();
191       check_removal ();
192     }
193 }
194
195 void
196 Global_context::apply_finalizations ()
197 {
198   SCM lst = get_property ("finalizations");
199   set_property ("finalizations", SCM_EOL);
200   for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
201
202     /* TODO: make safe.  */
203     scm_primitive_eval (scm_car (s));
204 }
205
206 /* Add a function to execute before stepping to the next time step.  */
207 void
208 Global_context::add_finalization (SCM x)
209 {
210   SCM lst = get_property ("finalizations");
211   lst = scm_cons (x, lst);
212   set_property ("finalizations", lst);
213 }
214
215 Moment
216 Global_context::previous_moment () const
217 {
218   return prev_mom_;
219 }
220
221 Context *
222 Global_context::get_default_interpreter ()
223 {
224   if (get_score_context ())
225     return get_score_context ()->get_default_interpreter ();
226   else
227     return Context::get_default_interpreter ();
228 }