00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 #include <stdio.h>
00069 #include <stdlib.h>
00070 #include <string.h>
00071 #include "mb_types.h"
00072 #include "mb_redraw_man.h"
00073 #include "mb_timer.h"
00074 #include "mb_animate.h"
00075
00076
00077 #define STEP_INTERVAL 90000
00078 #define ASSERT(x)
00079
00080
00081
00082 struct _mb_word {
00083 mb_timeval_t start_time;
00084 mb_timeval_t playing_time;
00085 mb_timeval_t abs_start, abs_stop;
00086 STAILQ(mb_action_t) actions;
00087 };
00088
00089
00090
00091
00092
00093
00094 struct _mb_progm {
00095 redraw_man_t *rdman;
00096
00097 mb_timeval_t start_time;
00098 int first_playing;
00099 mb_tman_t *tman;
00100 subject_t *complete;
00101 mb_timer_t *cur_timer;
00102
00103 int n_words;
00104 int max_words;
00105 mb_word_t words[1];
00106 };
00107
00108
00109
00110
00111
00112
00113 mb_progm_t *mb_progm_new(int max_words, redraw_man_t *rdman) {
00114 mb_progm_t *progm;
00115 #ifndef UNITTEST
00116 ob_factory_t *factory;
00117 #endif
00118 int i;
00119
00120 progm = (mb_progm_t *)malloc(sizeof(mb_progm_t) +
00121 (sizeof(mb_word_t) * (max_words - 1)));
00122 if(progm == NULL)
00123 return NULL;
00124
00125 progm->rdman = rdman;
00126
00127 #ifndef UNITTEST
00128 factory = rdman_get_ob_factory(rdman);
00129 progm->complete = subject_new(factory, progm, OBJT_PROGM);
00130 #endif
00131
00132 progm->n_words = 0;
00133 progm->max_words = max_words;
00134 for(i = 0; i < max_words; i++)
00135 STAILQ_INIT(progm->words[i].actions);
00136 return progm;
00137 }
00138
00139 void mb_progm_free(mb_progm_t *progm) {
00140 int n_words;
00141 mb_word_t *word;
00142 mb_action_t *cur_act;
00143 #ifndef UNITTEST
00144 ob_factory_t *factory;
00145 #endif
00146 int i;
00147
00148 n_words = progm->n_words;
00149 for(i = 0; i < n_words; i++) {
00150 word = progm->words + i;
00151 for(cur_act = STAILQ_HEAD(word->actions);
00152 cur_act != NULL;
00153 cur_act = STAILQ_HEAD(word->actions)) {
00154 STAILQ_REMOVE(word->actions, mb_action_t, next, cur_act);
00155 cur_act->free(cur_act);
00156 }
00157 }
00158
00159 #ifndef UNITTEST
00160 factory = rdman_get_ob_factory(progm->rdman);
00161 subject_free(factory, progm->complete);
00162 #endif
00163
00164 free(progm);
00165 }
00166
00167
00168
00169
00170
00171
00172 mb_word_t *mb_progm_next_word(mb_progm_t *progm,
00173 const mb_timeval_t *start,
00174 const mb_timeval_t *playing) {
00175 mb_word_t *word;
00176 if(progm->n_words >= progm->max_words)
00177 return NULL;
00178 if(progm->n_words &&
00179 MB_TIMEVAL_LATER(&progm->words[progm->n_words - 1].start_time, start))
00180 return NULL;
00181 word = progm->words + progm->n_words++;
00182 MB_TIMEVAL_CP(&word->start_time, start);
00183 MB_TIMEVAL_CP(&word->playing_time, playing);
00184 return word;
00185 }
00186
00187 void mb_word_add_action(mb_word_t *word, mb_action_t *act) {
00188 STAILQ_INS_TAIL(word->actions, mb_action_t, next, act);
00189 }
00190
00191 static void mb_word_start(mb_word_t *word, const mb_timeval_t *tmo,
00192 const mb_timeval_t *now, redraw_man_t *rdman) {
00193 mb_action_t *act;
00194
00195 for(act = STAILQ_HEAD(word->actions);
00196 act != NULL;
00197 act = STAILQ_NEXT(mb_action_t, next, act)) {
00198 act->start(act, tmo, &word->playing_time, rdman);
00199 }
00200 }
00201
00202 static void mb_word_step(mb_word_t *word, const mb_timeval_t *tmo,
00203 const mb_timeval_t *now, redraw_man_t *rdman) {
00204 mb_action_t *act;
00205
00206 for(act = STAILQ_HEAD(word->actions);
00207 act != NULL;
00208 act = STAILQ_NEXT(mb_action_t, next, act)) {
00209 act->step(act, tmo, rdman);
00210 }
00211 }
00212
00213 static void mb_word_stop(mb_word_t *word, const mb_timeval_t *tmo,
00214 const mb_timeval_t *now, redraw_man_t *rdman) {
00215 mb_action_t *act;
00216
00217 for(act = STAILQ_HEAD(word->actions);
00218 act != NULL;
00219 act = STAILQ_NEXT(mb_action_t, next, act)) {
00220 act->stop(act, tmo, rdman);
00221 }
00222 }
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 static void mb_progm_step(const mb_timeval_t *tmo,
00235 const mb_timeval_t *now,
00236 void *arg) {
00237 mb_progm_t *progm = (mb_progm_t *)arg;
00238 #ifndef UNITTEST
00239
00240
00241 ob_factory_t *factory;
00242 mb_progm_complete_t comp_evt;
00243 #endif
00244 mb_timeval_t next_tmo;
00245 mb_word_t *word;
00246 mb_timer_t *timer;
00247 int i;
00248
00249 MB_TIMEVAL_SET(&next_tmo, 0, STEP_INTERVAL);
00250 MB_TIMEVAL_ADD(&next_tmo, now);
00251
00252
00253 i = progm->first_playing;
00254 for(word = progm->words + i;
00255 i < progm->n_words && MB_TIMEVAL_LATER(tmo, &word->abs_start);
00256 word = progm->words + ++i) {
00257 if(MB_TIMEVAL_LATER(tmo, &word->abs_stop))
00258 continue;
00259 if(MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop))
00260 mb_word_stop(word, tmo, now, progm->rdman);
00261 else
00262 mb_word_step(word, tmo, now, progm->rdman);
00263 }
00264
00265
00266
00267
00268 for(word = progm->words + i;
00269 i < progm->n_words && MB_TIMEVAL_LATER(&next_tmo, &word->abs_start);
00270 word = progm->words + ++i) {
00271 mb_word_start(word, tmo, now, progm->rdman);
00272 if(MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop))
00273 mb_word_stop(word, tmo, now, progm->rdman);
00274 }
00275
00276
00277
00278
00279 i = progm->first_playing;
00280 for(word = progm->words + i;
00281 i < progm->n_words && MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop);
00282 word = progm->words + ++i) {
00283 progm->first_playing++;
00284 }
00285
00286
00287 if(progm->first_playing < progm->n_words) {
00288 word = progm->words + progm->first_playing;
00289 if(MB_TIMEVAL_LATER(&word->abs_start, &next_tmo))
00290 MB_TIMEVAL_CP(&next_tmo, &word->abs_start);
00291 timer = mb_tman_timeout(progm->tman, &next_tmo,
00292 mb_progm_step, progm);
00293 progm->cur_timer = timer;
00294 } else {
00295
00296 #ifndef UNITTEST
00297 factory = rdman_get_ob_factory(progm->rdman);
00298 comp_evt.event.type = EVT_PROGM_COMPLETE;
00299 comp_evt.event.tgt = comp_evt.event.cur_tgt = progm->complete;
00300 comp_evt.progm = progm;
00301 subject_notify(factory, progm->complete, &comp_evt.event);
00302 #endif
00303 progm->cur_timer = NULL;
00304 }
00305 }
00306
00307 void mb_progm_start(mb_progm_t *progm, mb_tman_t *tman,
00308 mb_timeval_t *now) {
00309 mb_timer_t *timer;
00310 mb_word_t *word;
00311 int i;
00312
00313 if(progm->n_words == 0)
00314 return;
00315
00316 progm->tman = tman;
00317 MB_TIMEVAL_CP(&progm->start_time, now);
00318 progm->first_playing = 0;
00319
00320 for(i = 0; i < progm->n_words; i++) {
00321 word = progm->words + i;
00322 MB_TIMEVAL_CP(&word->abs_start, &word->start_time);
00323 MB_TIMEVAL_ADD(&word->abs_start, now);
00324 MB_TIMEVAL_CP(&word->abs_stop, &word->abs_start);
00325 MB_TIMEVAL_ADD(&word->abs_stop, &word->playing_time);
00326 }
00327
00328 if(MB_TIMEVAL_EQ(&progm->words[0].abs_start, now)) {
00329 mb_progm_step(now, now, progm);
00330 return;
00331 }
00332
00333 timer = mb_tman_timeout(tman, &progm->words[0].abs_start,
00334 mb_progm_step, progm);
00335 ASSERT(timer != NULL);
00336
00337
00338 progm->cur_timer = timer;
00339 }
00340
00341 void mb_progm_abort(mb_progm_t *progm) {
00342
00343 if(progm->cur_timer) {
00344 mb_tman_remove(progm->tman, progm->cur_timer);
00345 progm->cur_timer = NULL;
00346 }
00347 }
00348
00349
00350
00351 subject_t *mb_progm_get_complete(mb_progm_t *progm) {
00352 return progm->complete;
00353 }
00354
00355 #ifdef UNITTEST
00356
00357 #include <CUnit/Basic.h>
00358
00359 typedef struct _mb_dummy mb_dummy_t;
00360
00361 struct _mb_dummy {
00362 mb_action_t action;
00363 int id;
00364 int *logidx;
00365 int *logs;
00366 };
00367
00368
00369 static void mb_dummy_start(mb_action_t *act,
00370 const mb_timeval_t *now,
00371 const mb_timeval_t *playing_time,
00372 redraw_man_t *rdman) {
00373 mb_dummy_t *dummy = (mb_dummy_t *)act;
00374
00375 dummy->logs[(*dummy->logidx)++] = dummy->id;
00376 }
00377
00378 static void mb_dummy_step(mb_action_t *act,
00379 const mb_timeval_t *now,
00380 redraw_man_t *rdman) {
00381 mb_dummy_t *dummy = (mb_dummy_t *)act;
00382
00383 dummy->logs[(*dummy->logidx)++] = dummy->id;
00384 }
00385
00386 static void mb_dummy_stop(mb_action_t *act,
00387 const mb_timeval_t *now,
00388 redraw_man_t *rdman) {
00389 mb_dummy_t *dummy = (mb_dummy_t *)act;
00390
00391 dummy->logs[(*dummy->logidx)++] = dummy->id;
00392 }
00393
00394 static void mb_dummy_free(mb_action_t *act) {
00395 free(act);
00396 }
00397
00398 mb_action_t *mb_dummy_new(int id, int *logidx, int *logs, mb_word_t *word) {
00399 mb_dummy_t *dummy;
00400
00401 dummy = (mb_dummy_t *)malloc(sizeof(mb_dummy_t));
00402 if(dummy == NULL)
00403 return NULL;
00404
00405 dummy->id = id;
00406 dummy->logidx = logidx;
00407 dummy->logs = logs;
00408
00409 dummy->action.start = mb_dummy_start;
00410 dummy->action.step = mb_dummy_step;
00411 dummy->action.stop = mb_dummy_stop;
00412 dummy->action.free = mb_dummy_free;
00413
00414 mb_word_add_action(word, (mb_action_t *)dummy);
00415
00416 return (mb_action_t *)dummy;
00417 }
00418
00419 void test_animate_words(void) {
00420 mb_progm_t *progm;
00421 mb_word_t *word;
00422 mb_action_t *acts[4];
00423 mb_tman_t *tman;
00424 mb_timeval_t tv1, tv2, now, tmo_after;
00425 int logcnt = 0;
00426 int logs[256];
00427 int r;
00428
00429 tman = mb_tman_new();
00430 CU_ASSERT(tman != NULL);
00431
00432 progm = mb_progm_new(3, NULL);
00433 CU_ASSERT(progm != NULL);
00434
00435 MB_TIMEVAL_SET(&tv1, 1, 0);
00436 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL * 3);
00437 word = mb_progm_next_word(progm, &tv1, &tv2);
00438 CU_ASSERT(word != NULL);
00439 acts[0] = mb_dummy_new(0, &logcnt, logs, word);
00440 CU_ASSERT(acts[0] != NULL);
00441
00442 MB_TIMEVAL_SET(&tv1, 1, STEP_INTERVAL * 4 / 3);
00443 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL / 3);
00444 word = mb_progm_next_word(progm, &tv1, &tv2);
00445 CU_ASSERT(word != NULL);
00446 acts[1] = mb_dummy_new(1, &logcnt, logs, word);
00447 CU_ASSERT(acts[1] != NULL);
00448
00449 MB_TIMEVAL_SET(&tv1, 1, STEP_INTERVAL * 2);
00450 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL * 3);
00451 word = mb_progm_next_word(progm, &tv1, &tv2);
00452 CU_ASSERT(word != NULL);
00453 acts[2] = mb_dummy_new(2, &logcnt, logs, word);
00454 CU_ASSERT(acts[2] != NULL);
00455
00456 MB_TIMEVAL_SET(&now, 0, 0);
00457 mb_progm_start(progm, tman, &now);
00458
00459 r = mb_tman_next_timeout(tman, &now, &tmo_after);
00460 CU_ASSERT(r == 0);
00461 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 1 &&
00462 MB_TIMEVAL_USEC(&tmo_after) == 0);
00463
00464
00465 MB_TIMEVAL_ADD(&now, &tmo_after);
00466 mb_tman_handle_timeout(tman, &now);
00467 CU_ASSERT(logcnt == 1);
00468
00469 r = mb_tman_next_timeout(tman, &now, &tmo_after);
00470 CU_ASSERT(r == 0);
00471 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 &&
00472 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL);
00473
00474
00475 MB_TIMEVAL_ADD(&now, &tmo_after);
00476 mb_tman_handle_timeout(tman, &now);
00477 CU_ASSERT(logcnt == 4);
00478
00479 r = mb_tman_next_timeout(tman, &now, &tmo_after);
00480 CU_ASSERT(r == 0);
00481 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 &&
00482 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL);
00483
00484
00485 MB_TIMEVAL_ADD(&now, &tmo_after);
00486 mb_tman_handle_timeout(tman, &now);
00487 CU_ASSERT(logcnt == 6);
00488
00489 r = mb_tman_next_timeout(tman, &now, &tmo_after);
00490 CU_ASSERT(r == 0);
00491 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 &&
00492 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL);
00493
00494
00495 MB_TIMEVAL_ADD(&now, &tmo_after);
00496 mb_tman_handle_timeout(tman, &now);
00497 CU_ASSERT(logcnt == 8);
00498
00499 r = mb_tman_next_timeout(tman, &now, &tmo_after);
00500 CU_ASSERT(r == 0);
00501 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 &&
00502 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL);
00503
00504
00505 MB_TIMEVAL_ADD(&now, &tmo_after);
00506 mb_tman_handle_timeout(tman, &now);
00507 CU_ASSERT(logcnt == 9);
00508
00509 r = mb_tman_next_timeout(tman, &now, &tmo_after);
00510 CU_ASSERT(r == 0);
00511 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 &&
00512 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL);
00513
00514
00515 MB_TIMEVAL_ADD(&now, &tmo_after);
00516 mb_tman_handle_timeout(tman, &now);
00517 CU_ASSERT(logcnt == 10);
00518
00519 r = mb_tman_next_timeout(tman, &now, &tmo_after);
00520 CU_ASSERT(r == -1);
00521
00522 mb_progm_free(progm);
00523 mb_tman_free(tman);
00524 }
00525
00526 CU_pSuite get_animate_suite(void) {
00527 CU_pSuite suite;
00528
00529 suite = CU_add_suite("Suite_animate", NULL, NULL);
00530 if(suite == NULL)
00531 return NULL;
00532
00533 CU_ADD_TEST(suite, test_animate_words);
00534
00535 return suite;
00536 }
00537
00538 #endif