File: context.c

package info (click to toggle)
lebiniou 3.18-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,396 kB
  • sloc: ansic: 20,369; sh: 4,169; makefile: 834; awk: 194
file content (677 lines) | stat: -rw-r--r-- 14,205 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
 *  Copyright 1994-2012 Olivier Girondel
 *
 *  This file is part of lebiniou.
 *
 *  lebiniou is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  lebiniou is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with lebiniou. If not, see <http://www.gnu.org/licenses/>.
 */

#include "globals.h"
#include "context.h"
#include "brandom.h"
#include "pictures.h"
#include "colormaps.h"
#include "sequences.h"


#define DELAY_MIN 15
#define DELAY_MAX 30


#ifdef WITH_WEBCAM
#include "webcam.h"

extern int webcams;
extern char *video_base;


static webcam_t *cams[MAX_CAMS];
static pthread_t thr[MAX_CAMS];

#define MAX_TRIES 5


static void
Context_open_webcam(Context_t *ctx)
{
  int i;
  uint8_t try = 0;

  parse_options();

  for (i = 0; i < MAX_CAMS; i++)
    pthread_mutex_init(&ctx->cam_mtx[i], NULL);

  for (i = 0; (i < webcams) && (try < MAX_TRIES); i++) {
  retry:
    cams[i] = xmalloc(sizeof(webcam_t));

    cams[i]->io = IO_METHOD_MMAP;
    cams[i]->fd = -1;
    cams[i]->ctx = ctx;
    cams[i]->cam_no = i;

    if (-1 != open_device(cams[i], try)) {
      if (-1 != init_device(cams[i])) {
	enumerate_cids(cams[i]);
	list_inputs(cams[i]);
	start_capturing(cams[i]);

	pthread_create(&thr[i], NULL, loop, (void *)cams[i]);
      } else {
	fprintf(stderr, "[i] Webcam: failed to initialize device #%d\n", i);
	close_device(cams[i]);
	xfree(cams[i]);
	try++;
	goto retry;
      }
    } else
      xfree(cams[i]);
  }
}
#endif


Context_t *
Context_new()
{
  int i;

  Context_t *ctx = xcalloc(1, sizeof(Context_t));

  ctx->running = 1;

  if (libbiniou_verbose)
    printf("[+] Creating buffers... ");
  for (i = 0; i < NSCREENS; i++) {
    if (libbiniou_verbose)
      printf("%d ", i);
    ctx->buffers[i] = Buffer8_new();
  }
#ifdef WITH_WEBCAM
  {
    int k;
    for (k = 0; k < webcams; k++) {
      for (i = 0; i < CAM_SAVE; i++)
	ctx->cam_save[k][i] = Buffer8_new();
      ctx->cam_ref[k] = Buffer8_new();
      ctx->rgba_cam_buffers[k] = BufferRGBA_new();
    }
  }
#endif
  ctx->rgba_buffers[ACTIVE_BUFFER] = BufferRGBA_new();
  if (libbiniou_verbose)
    printf("\n");

#if WITH_GL
  glGenTextures(NSCREENS, ctx->textures); // TODO: delete on exit
  glGenTextures(MAX_CAMS, ctx->cam_textures); // TODO: delete on exit
#endif

  if (pictures != NULL) {
    if (libbiniou_verbose)
      printf("[+] Creating pictures fader (%i pictures)\n", pictures->size);
    ctx->pf = PictFader_new(pictures->size);

    if (libbiniou_verbose)
      printf("[+] Creating pictures timer\n");
    ctx->a_picts = Alarm_new(DELAY_MIN, DELAY_MAX);
  }

  if (colormaps != NULL) {
    if (libbiniou_verbose)
      printf("[+] Creating colormaps fader (%i colormaps)\n", colormaps->size);
    ctx->cf = CmapFader_new(colormaps->size);

    if (libbiniou_verbose)
      printf("[+] Creating colormaps timer\n");
    ctx->a_cmaps = Alarm_new(DELAY_MIN, DELAY_MAX);
  }

  ctx->a_random = Alarm_new(DELAY_MIN, DELAY_MAX);
  ctx->random_mode = BR_NONE;

  if (libbiniou_verbose)
    printf("[+] Initializing sequence manager\n");
  ctx->sm = SequenceManager_new();

  Context_load_banks(ctx);

  if (libbiniou_verbose)
    printf("[+] Initializing 3D engine\n");
  Params3d_init(&ctx->params3d);
	
  ctx->events = NULL;
  ctx->frames = ctx->nb_events = 0;
  for (i = 0; i < NFPS; i++)
    ctx->fps[i] = 0;

  ctx->timer = b_timer_new();
  ctx->fps_timer = b_timer_new();

  ctx->osd_mode = OSD_NONE;
  ctx->display_colormap = 0;

  ctx->outputs = NULL;

#ifdef FEAT_TARGET
  ctx->target = 1; /* maybe someday we want a switch to disable,
		      or worse, a global variable :) */
  ctx->target_pic = Picture8_new();
  /* FIXME temp */
  {
    extern char *data_dir;
    gchar *tmp;
    int res;

    tmp = g_strdup_printf("%s/%s", data_dir, "/images/zebulon/z-biniou-tv-1.png");
    printf("[+] Loading '%s'\n", tmp);
    res = Picture8_load_PNG(ctx->target_pic, tmp);
    assert(!res);
    if (res == -1)
      xerror("Picture8_load_PNG(%s)\n", tmp);
    g_free(tmp);
  }
#endif

#ifdef WITH_WEBCAM
  printf("[i] Initialiazing %d webcams base: %s\n", webcams, video_base);

  ctx->cam = 0;
  Context_open_webcam(ctx);

  for (i = 0; i < webcams; i++)
    Buffer8_copy(ctx->target_pic->buff, ctx->cam_save[i][0]);
#endif

  return ctx;
}


#ifdef WITH_WEBCAM
static void
Context_close_webcam(const u_char cam_no)
{
  if (NULL != cams[cam_no]) {
    pthread_join(thr[cam_no], NULL);
    stop_capturing(cams[cam_no]);
    uninit_device(cams[cam_no]);
    close_device(cams[cam_no]);
  }
}
#endif


void
Context_delete(Context_t *ctx)
{
  int i;
  GSList *outputs = ctx->outputs;

#ifdef WITH_WEBCAM
  if (webcams) {
    int i;

    for (i = 0; i < webcams; i++)
      Context_close_webcam(i);
  }
  xfree(video_base);
#endif

  if (libbiniou_verbose)
    printf("[i] %lu frames, %lu events\n", ctx->frames, ctx->nb_events);

  if (ctx->input_plugin != NULL)
    Plugin_delete(ctx->input_plugin);

  for ( ; outputs != NULL; outputs = g_slist_next(outputs)) {
    Plugin_t *output = (Plugin_t *)outputs->data;

    Plugin_delete(output);
  }
  g_slist_free(outputs);
  
  if (libbiniou_verbose)
    printf("[+] Freeing buffers... ");
  for (i = 0; i < NSCREENS; i++) {
    if (libbiniou_verbose)
      printf("%d ", i);
    Buffer8_delete(ctx->buffers[i]);
  }
#ifdef WITH_WEBCAM
  {
    int k;
    for (k = 0; k < webcams; k++) {
      for (i = 0; i < CAM_SAVE; i++)
	Buffer8_delete(ctx->cam_save[k][i]);
      Buffer8_delete(ctx->cam_ref[k]);
      BufferRGBA_delete(ctx->rgba_cam_buffers[k]);
    }
  }
#endif
  BufferRGBA_delete(ctx->rgba_buffers[ACTIVE_BUFFER]);
  if (libbiniou_verbose)
    printf("\n");

  if (ctx->pf != NULL) {
    if (libbiniou_verbose)
      printf("[+] Freeing pictures fader\n");
    PictFader_delete(ctx->pf);

    if (libbiniou_verbose)
      printf("[+] Freeing pictures timer\n");
    Alarm_delete(ctx->a_picts);
  }

  if (ctx->cf != NULL) {
    if (libbiniou_verbose)
      printf("[+] Freeing colormaps fader\n");
    CmapFader_delete(ctx->cf);

    if (libbiniou_verbose)
      printf("[+] Freeing colormaps timer\n");
    Alarm_delete(ctx->a_cmaps);
  }

  Alarm_delete(ctx->a_random);
  SequenceManager_delete(ctx->sm);
  b_timer_delete(ctx->timer);
  b_timer_delete(ctx->fps_timer);

#ifdef FEAT_TARGET
  if (NULL != ctx->target_pic)
    Picture8_delete(ctx->target_pic);
#endif

  xfree(ctx);
}


void
Context_set_colormap(Context_t *ctx)
{
  /* find the cmap, if any. default cmap otherwise */
  if (ctx->cf != NULL) {
    ctx->cf->fader->target
      = (ctx->sm->next->cmap_id) ?
      Colormaps_index(ctx->sm->next->cmap_id) : 0;
    CmapFader_set(ctx->cf);
  }
}


void
Context_set_picture(Context_t *ctx)
{
  /* find the picture, if any. default picture otherwise */
  if (ctx->pf != NULL) {
    ctx->pf->fader->target
      = (ctx->sm->next->picture_id) ?
      Pictures_index(ctx->sm->next->picture_id) : 0;
    PictFader_set(ctx->pf);
  }
}

void
Context_set(Context_t *ctx)
{
  GList *tmp;

#ifdef XDEBUG
  printf("*** Context_set: cur sequence= %li\n", ctx->sm->cur->id);
  printf("*** Context_set: next sequence= %li\n", ctx->sm->next->id);
#endif

  tmp = g_list_first(ctx->sm->cur->layers);
	
  /* call on_switch_off() on old plugins */
  while (tmp != NULL) {
    Layer_t *layer = tmp->data;
    Plugin_t *p = layer->plugin;

    assert(p != NULL);
    if (p->on_switch_off != NULL)
      if (!(*p->options & BEQ_DISABLED))
	p->on_switch_off(ctx);
    tmp = g_list_next(tmp);
  }		

  Context_set_colormap(ctx);
  Context_set_picture(ctx);

  /* call on_switch_on() on new plugins */
  tmp = g_list_first(ctx->sm->next->layers);
  while (tmp != NULL) {
    Layer_t *layer = (Layer_t *)tmp->data;
    Plugin_t *p = layer->plugin;
	  
    assert(p != NULL);
    if (p->on_switch_on != NULL)
      if (!(*p->options & BEQ_DISABLED))
	p->on_switch_on(ctx);
    tmp = g_list_next(tmp);
  }	
	
  Sequence_copy(ctx->sm->next, ctx->sm->cur);

  Context_update_auto(ctx);
	
  Sequence_display(ctx->sm->cur);
  okdone("Context_set");
}


void
Context_update_auto(Context_t *ctx)
{
  /* set auto stuff */
  if (ctx->pf != NULL) {
    ctx->pf->on = ctx->sm->cur->auto_pictures;
    if (ctx->pf->on && (pictures != NULL) && (pictures->size > 1)) {
      /* select random picture and reinitialize timer */
      PictFader_random(ctx->pf);
      Alarm_init(ctx->a_picts);
    }
  }
  
  if (ctx->cf != NULL) {
    ctx->cf->on = ctx->sm->cur->auto_colormaps;
    if (ctx->cf->on && (colormaps->size > 1)) {
      /* select random colormap and reinitialize timer */
      CmapFader_random(ctx->cf);
      Alarm_init(ctx->a_cmaps);
    }
  }
}


int
Context_add_rand(Sequence_t *seq,
		 const enum PluginOptions options, const int not_lens)
{
  Plugin_t *p;

  do {
    p = Plugins_get_random(options);
    if (p == NULL)
      return -1;
  } while (Sequence_find(seq, p) != NULL);

  if (*p->options & BEQ_FLUSH)
    Sequence_clear(seq, 0);

  Sequence_insert(seq, p);

  if ((*p->options & BE_LENS) && !not_lens && (seq->lens == NULL))
    seq->lens = p;
	
  return 0;
}


void
Context_randomize(Context_t *ctx)
{
  Sequence_t *new = ctx->sm->next;
  int rand;

  /* Use random picture */
  if (pictures != NULL) {
    if (pictures->size > 1) {
      rand = b_rand_int_range(0, pictures->size-1);
      new->auto_pictures = b_rand_boolean();
    } else
      rand = new->auto_pictures = 0;
    new->picture_id = pictures->pics[rand]->id;
  }
  
  /* Use random colormap */
  assert(colormaps != NULL);
  if (colormaps->size > 1) {
    rand = b_rand_int_range(0, colormaps->size-1);
    new->auto_colormaps = b_rand_boolean();
  } else
    rand =new->auto_colormaps = 0;
  new->cmap_id = colormaps->cmaps[rand]->id;

  /* Set 3D rotations */
  if (b_rand_boolean())
    Params3d_randomize(&ctx->params3d);
  else
    ctx->params3d.do_auto_rotate = 0;
}


void
Context_insert_plugin(Context_t *ctx, Plugin_t *p)
{
  /* switch the plugin on */
  if (p->on_switch_on != NULL) {
    if (libbiniou_verbose)
      printf("[i] on_switch_on(%s)\n", p->name);
    p->on_switch_on(ctx);
  }

  Sequence_insert(ctx->sm->cur, p);
}


void
Context_remove_plugin(Context_t *ctx, Plugin_t *p)
{
  /* switch the plugin off */
  if (p->on_switch_off != NULL) {
    if (libbiniou_verbose)
      printf("[i] on_switch_off(%s)\n", p->name);
    p->on_switch_off(ctx);
  }

  Sequence_remove(ctx->sm->cur, p);
}


void
Context_set_max_fps(Context_t *ctx, const u_short max_fps)
{
  ctx->sync_fps = 1;
  assert(max_fps);
  ctx->max_fps = max_fps;
  ctx->i_max_fps = 1.0 / ctx->max_fps;
}


void
Context_set_random_mode(Context_t *ctx, const enum RandomMode r)
{
  ctx->random_mode = r;
}


void
Context_set_osd_mode(Context_t *ctx, const enum OSDMode m)
{
  ctx->osd_mode = m;
}


float
Context_fps(const Context_t *ctx)
{
  float mfps = 0.0;
  int i;

  for (i = 0; i < NFPS; i++)
    mfps += ctx->fps[i];
  return (mfps / (float)NFPS);
}


void
Context_previous_sequence(Context_t *ctx)
{
  Sequence_t *s;

  if (ctx->sm->curseq == NULL)
    return;

  if (ctx->sm->curseq->prev != NULL)
    ctx->sm->curseq = ctx->sm->curseq->prev;
  else
    ctx->sm->curseq = g_list_last(sequences->seqs);

  s = (Sequence_t *)ctx->sm->curseq->data;
  Sequence_copy(s, ctx->sm->next);

  Context_set(ctx);
}


void
Context_next_sequence(Context_t *ctx)
{
  Sequence_t *s;

  if (ctx->sm->curseq == NULL)
    return;

  if (ctx->sm->curseq->next != NULL)
    ctx->sm->curseq = ctx->sm->curseq->next;
  else
    ctx->sm->curseq = sequences->seqs;

  s = (Sequence_t *)ctx->sm->curseq->data;
  Sequence_copy(s, ctx->sm->next);

  Context_set(ctx);
}


void
Context_latest_sequence(Context_t *ctx)
{
  Sequence_t *s;

  if (ctx->sm->curseq == NULL)
    return;

  ctx->sm->curseq = sequences->seqs;

  s = (Sequence_t *)ctx->sm->curseq->data;
  Sequence_copy(s, ctx->sm->next);

  Context_set(ctx);
}


void
Context_random_sequence(Context_t *ctx)
{
  u_short rand;
  GList *tmp;

  rand = Shuffler_get(sequences->shuffler);

  tmp = g_list_nth(sequences->seqs, rand);
  assert(tmp != NULL);
  
  if (libbiniou_verbose)
    printf("[s] Random sequence: %d\n", rand);
  
  ctx->sm->curseq = tmp;
  Sequence_copy(tmp->data, ctx->sm->next);

  Context_set(ctx);
}


void
Context_set_sequence(Context_t *ctx, const uint32_t id)
{
  Sequence_t *seq = Sequences_find(id);

  if (NULL == seq)
    if (ctx->sm->transient->id == id)
      seq = ctx->sm->transient;

  assert(NULL != seq);
  ctx->sm->curseq = seq->layers;
  Sequence_copy(seq, ctx->sm->next);

  Context_set(ctx);
}


void
Context_use_sequence_bank(Context_t *ctx, const u_char bank)
{
  u_long id;

  id = ctx->banks[SEQUENCES][ctx->bankset[SEQUENCES]][bank];
  if (id) {
    Sequence_t *seq;

    printf("[i] Using sequence in bank #%d\n", (bank+1));

    if (libbiniou_verbose)
      printf("[s] Set sequence: %li\n", id);

    seq = Sequences_find(id);
    if (NULL == seq)
      if (ctx->sm->transient->id == id)
	seq = ctx->sm->transient;
    assert(NULL != seq);
    ctx->sm->curseq = seq->layers;
    Sequence_copy(seq, ctx->sm->next);

    ctx->bank[SEQUENCES] = bank;

    Context_set(ctx);
  } else
    printf("[i] Bank %d/%d is empty\n", ctx->bankset[SEQUENCES]+1, bank+1);
}


void
Context_clear_bank(Context_t *ctx, const u_char bank)
{
  ctx->banks[ctx->bank_mode][ctx->bankset[ctx->bank_mode]][bank] = 0;
}


Buffer8_t *
active_buffer(const Context_t *ctx)
{
  return ctx->buffers[ACTIVE_BUFFER];
}


Buffer8_t *
passive_buffer(const Context_t *ctx)
{
  return ctx->buffers[PASSIVE_BUFFER];
}


#ifdef WITH_WEBCAM
void
Context_push_webcam(Context_t *ctx, Buffer8_t *buff, const int cam)
{
  int i;

  Buffer8_delete(ctx->cam_save[cam][CAM_SAVE-1]);
  for (i = CAM_SAVE-1; i >= 1; i--)
    ctx->cam_save[cam][i] = ctx->cam_save[cam][i-1];
  ctx->cam_save[cam][0] = buff;
}
#endif