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