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