summaryrefslogtreecommitdiff
path: root/Source/DirectFB/tools/dfbscreen.c
blob: b961d38bfd930e3c45fc242e0d77d8671c437f75 (plain)
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
/*
   (c) Copyright 2001-2009  The world wide DirectFB Open Source Community (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Hundt <andi@fischlustig.de>,
              Sven Neumann <neo@directfb.org>,
              Ville Syrjälä <syrjala@sci.fi> and
              Claudio Ciccani <klan@users.sf.net>.

   This file is subject to the terms and conditions of the MIT License:

   Permission is hereby granted, free of charge, to any person
   obtaining a copy of this software and associated documentation
   files (the "Software"), to deal in the Software without restriction,
   including without limitation the rights to use, copy, modify, merge,
   publish, distribute, sublicense, and/or sell copies of the Software,
   and to permit persons to whom the Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be
   included in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <direct/messages.h>
#include <direct/util.h>

#include <directfb.h>
#include <directfb_strings.h>

static const DirectFBScreenMixerTreeNames( tree_names );
static const DirectFBScreenEncoderScanModeNames( scan_mode_names );
static const DirectFBScreenEncoderTestPictureNames( test_picture_names );
static const DirectFBScreenEncoderTVStandardsNames( tv_standard_names );
static const DirectFBScreenOutputSignalsNames( signal_names );

/**************************************************************************************************/

static IDirectFB            *dfb;
static IDirectFBScreen      *screen;
static DFBScreenDescription  desc;

/**************************************************************************************************/

static DFBScreenID id      = DSCID_PRIMARY;
static int         mixer   = 0;
static int         encoder = 0;
static int         output  = 0;

/**************************************************************************************************/

static DFBScreenMixerConfig   mixer_config;
static DFBScreenEncoderConfig encoder_config;
static DFBScreenOutputConfig  output_config;

/**************************************************************************************************/

static bool parse_command_line( int argc, char *argv[] );

/**************************************************************************************************/

static void dump_mixer_config  ( const DFBScreenMixerConfig   *config );
static void dump_encoder_config( const DFBScreenEncoderConfig *config );

/**************************************************************************************************/

int
main( int argc, char *argv[] )
{
     DFBResult ret;

     /* Initialize DirectFB including command line parsing. */
     ret = DirectFBInit( &argc, &argv );
     if (ret) {
          D_DERROR( ret, "Tools/Screen: DirectFBInit() failed!\n" );
          goto error;
     }

     /* Parse the command line. */
     if (!parse_command_line( argc, argv ))
          goto error;

     /* Create the super interface. */
     ret = DirectFBCreate( &dfb );
     if (ret) {
          D_DERROR( ret, "Tools/Screen: DirectFBCreate() failed!\n" );
          goto error;
     }

     /* Get the primary display screen. */
     ret = dfb->GetScreen( dfb, id, &screen );
     if (ret) {
          if (ret == DFB_IDNOTFOUND)
               fprintf (stderr, "\nUnknown screen id, check 'dfbinfo' for valid values.\n\n");
          else
               D_DERROR( ret, "Tools/Screen: IDirectFB::GetScreen() failed!\n" );

          goto error;
     }

     /* Get a description of the screen. */
     ret = screen->GetDescription( screen, &desc );
     if (ret) {
          D_DERROR( ret, "Tools/Screen: IDirectFBScreen::GetDescription() failed!\n" );
          goto error;
     }

     /* Check arguments. */
     if (mixer_config.flags && (mixer < 0 || mixer >= desc.mixers)) {
          fprintf (stderr, "\nUnknown mixer (%d), check 'dfbinfo' for valid values.\n\n", mixer);
          goto error;
     }

     if (encoder_config.flags && (encoder < 0 || encoder >= desc.encoders)) {
          fprintf (stderr, "\nUnknown encoder (%d), check 'dfbinfo' for valid values.\n\n", encoder);
          goto error;
     }

     if (output_config.flags && (output < 0 || output >= desc.outputs)) {
          fprintf (stderr, "\nUnknown output (%d), check 'dfbinfo' for valid values.\n\n", output);
          goto error;
     }

     printf( "\n" );

     /* Do mixer. */
     if (mixer < desc.mixers && mixer >= 0) {
          DFBScreenMixerConfig config;

          printf( "\nMixer %d\n", mixer );

          if (mixer_config.flags) {
               ret = screen->SetMixerConfiguration( screen, mixer, &mixer_config );
               if (ret) {
                    D_DERROR( ret, "Tools/Screen: "
                              "IDirectFBScreen::SetMixerConfiguration(%d) failed!\n", mixer );
                    goto error;
               }
          }

          ret = screen->GetMixerConfiguration( screen, mixer, &config );
          if (ret) {
               D_DERROR( ret, "Tools/Screen: "
                         "IDirectFBScreen::GetMixerConfiguration(%d) failed!\n", mixer );
               goto error;
          }

          dump_mixer_config( &config );
     }

     /* Do encoder. */
     if (encoder < desc.encoders && encoder >= 0) {
          DFBScreenEncoderConfig config;

          printf( "\nEncoder %d\n", encoder );

          if (encoder_config.flags) {
               ret = screen->SetEncoderConfiguration( screen, encoder, &encoder_config );
               if (ret) {
                    D_DERROR( ret, "Tools/Screen: "
                              "IDirectFBScreen::SetEncoderConfiguration(%d) failed!\n", encoder );
                    goto error;
               }
          }

          ret = screen->GetEncoderConfiguration( screen, encoder, &config );
          if (ret) {
               D_DERROR( ret, "Tools/Screen: "
                         "IDirectFBScreen::GetEncoderConfiguration(%d) failed!\n", mixer );
               goto error;
          }

          dump_encoder_config( &config );
     }

error:
     /* Release the display screen. */
     if (screen)
          screen->Release( screen );

     /* Release the super interface. */
     if (dfb)
          dfb->Release( dfb );

     return ret;
}

/**************************************************************************************************/

typedef struct __AnyOption AnyOption;


typedef bool (*ParseFunc)( const AnyOption *option,
                           const char      *arg );

struct __AnyOption {
     const char   *short_name;
     const char   *long_name;

     const char   *arg_name;
     const char   *arg_desc;

     void         *value;

     unsigned int *flags;
     unsigned int  flag;

     ParseFunc     parse;
     const void   *data;
};

typedef struct {
     int           value;
     const char   *name;
} ValueName;

/**************************************************************************************************/

static bool parse_int  ( const AnyOption *option,
                         const char      *arg );

static bool parse_enum ( const AnyOption *option,
                         const char      *arg );

static bool parse_ids  ( const AnyOption *option,
                         const char      *arg );

static bool parse_color( const AnyOption *option,
                         const char      *arg );

/**************************************************************************************************/

static const AnyOption options[] = {
     { "-s",  "--screen",       "<id>",       "ID of screen to use",
       &id,      NULL, 0, parse_int, NULL },


     { "-m",  "--mixer",        "<index>",    "Index of mixer to use",
       &mixer,   NULL, 0, parse_int, NULL },

     { "-mt", "--tree",         "<mode>",     "Set (sub) tree mode",
       &mixer_config.tree, &mixer_config.flags,
       DSMCONF_TREE, parse_enum, tree_names },

     { "-mm", "--max-level",    "<level>",    "Set maximum level for SUB_LEVEL mode",
       &mixer_config.level, &mixer_config.flags,
       DSMCONF_LEVEL, parse_int, NULL },

     { "-ml", "--layers",       "<layers>",   "Select layers for SUB_LAYERS mode",
       &mixer_config.layers, &mixer_config.flags,
       DSMCONF_LAYERS, parse_ids, NULL },

     { "-mb", "--background",   "<rgb>",      "Set background color (hex)",
       &mixer_config.background, &mixer_config.flags,
       DSMCONF_BACKGROUND, parse_color, NULL },


     { "-e",  "--encoder",      "<index>",    "Index of encoder to use",
       &encoder, NULL, 0, parse_int, NULL },

     { "-et", "--tv-standard",  "<standard>", "Set TV standard",
       &encoder_config.tv_standard, &encoder_config.flags,
       DSECONF_TV_STANDARD, parse_enum, tv_standard_names },

     { "-ep", "--test-picture", "<mode>",     "Set test picture mode",
       &encoder_config.test_picture, &encoder_config.flags,
       DSECONF_TEST_PICTURE, parse_enum, test_picture_names },

     { "-em", "--sel-mixer",    "<index>",    "Select mixer for input",
       &encoder_config.mixer, &encoder_config.flags,
       DSECONF_MIXER, parse_int, NULL },

     { "-es", "--encode-sigs",  "<signals>",  "Select signal(s) to encode",
       &encoder_config.out_signals, &encoder_config.flags,
       DSECONF_OUT_SIGNALS, parse_enum, signal_names },

     { "-ec", "--scan-mode",    "<mode>",     "Set scan mode",
       &encoder_config.scanmode, &encoder_config.flags,
       DSECONF_SCANMODE, parse_enum, scan_mode_names },


     { "-o",  "--output",       "<index>",    "Index of output to use",
       &output,  NULL, 0, parse_int, NULL }
};

static void
print_usage (const char *prg_name)
{
     int i;

     fprintf (stderr, "\nDirectFB Screen Configuration (version %s)\n\n", DIRECTFB_VERSION);
     fprintf (stderr, "Usage: %s [options]\n\n", prg_name);
     fprintf (stderr, "Options:\n");
     fprintf (stderr, "   -h   --help                             Show this help message\n");
     fprintf (stderr, "   -v   --version                          Print version information\n");

     for (i=0; i<D_ARRAY_SIZE(options); i++) {
          const AnyOption *option = &options[i];

          fprintf( stderr, "   %-3s  %-16s   %-12s    %s\n",
                   option->short_name, option->long_name, option->arg_name, option->arg_desc );
     }

     fprintf (stderr, "\n");
}

static bool
parse_int( const AnyOption *option, const char *arg )
{
     int   ret;
     char *end;

     ret = strtoul( arg, &end, option->data ? (unsigned long) option->data : 10 );

     if (*end || ret < 0) {
          fprintf( stderr, "\nInvalid argument to '%s' or '%s' specified!\n\n",
                   option->short_name, option->long_name );

          return false;
     }

     *((int*)option->value) = ret;

     return true;
}

static bool
parse_enum( const AnyOption *option, const char *arg )
{
     int val = 0;

     if (! strcasecmp( arg, "help" )) {
          const ValueName *vn = option->data;

          fprintf( stderr, "\nPossible arguments to '%s' or '%s':\n",
                   option->short_name, option->long_name );

          do {
               fprintf( stderr, "    %s\n", vn->name );
          } while (vn++->value);

          fprintf (stderr, "\n");

          return false;
     }

     while (arg[0] == ',')
          arg++;

     while (arg[0]) {
          char            *p;
          int              len;
          int              vc = 0;
          bool             ok = false;
          const ValueName *vn = option->data;

          p = strchr( arg, ',' );
          if (p)
               len = p - arg;
          else
               len = strlen( arg );

          do {
               int vlen = strlen(vn->name);

               if (strncasecmp( vn->name, arg, len ))
                    continue;

               vc = vn->value;
               ok = true;

               if (vlen == len)
                    break;
          } while (vn++->value);

          if (ok)
               val |= vc;
          else {
               fprintf( stderr, "\nInvalid argument to '%s' or '%s' specified, "
                        "pass 'help' for a list!\n\n", option->short_name, option->long_name );
               return false;
          }

          arg += len;

          while (arg[0] == ',')
               arg++;
     }

     *((int*)option->value) = val;

     return true;
}

static bool
parse_ids( const AnyOption *option, const char *arg )
{
     u32    val  = 0;
     int    alen = strlen( arg );
     char  *abuf = alloca( alen + 1 );

     memcpy( abuf, arg, alen + 1 );

     if (! strcasecmp( arg, "help" )) {
          fprintf( stderr, "\nCheck 'dfbinfo' for valid values.\n\n" );
          return false;
     }

     while (abuf[0] == ',')
          abuf++;

     while (abuf[0]) {
          char *p;
          int   len;
          int   ret;

          p = strchr( abuf, ',' );
          if (p) {
               len = p - abuf;

               abuf[len++] = 0;
          }
          else
               len = strlen( abuf );

          ret = strtoul( abuf, &p, 10 );

          if (*p || ret < 0) {
               fprintf( stderr, "\nInvalid argument (%s) to '%s' or '%s' specified!\n\n",
                        p, option->short_name, option->long_name );

               return false;
          }

          val |= (1 << ret);

          abuf += len;

          while (abuf[0] == ',')
               abuf++;
     }

     *((u32*)option->value) = val;

     return true;
}

static bool
parse_color( const AnyOption *option, const char *arg )
{
     unsigned long  ret;
     char          *end;
     DFBColor      *color = option->value;

     ret = strtoul( arg, &end, 16 );

     if (*end) {
          fprintf( stderr, "\nInvalid argument (%s) to '%s' or '%s' specified!\n\n",
                   end, option->short_name, option->long_name );

          return false;
     }

     color->a = (ret >> 24) & 0xff;
     color->r = (ret >> 16) & 0xff;
     color->g = (ret >>  8) & 0xff;
     color->b = (ret      ) & 0xff;

     return true;
}

static bool
parse_option( const AnyOption *option, const char *arg )
{
     if (!option->parse( option, arg ))
          return false;

     if (option->flags)
          *option->flags |= option->flag;

     return true;
}

static bool
parse_command_line( int argc, char *argv[] )
{
     int i, n;

     for (n = 1; n < argc; n++) {
          bool        ok  = false;
          const char *arg = argv[n];

          if (strcmp (arg, "-h") == 0 || strcmp (arg, "--help") == 0) {
               print_usage (argv[0]);
               return false;
          }

          if (strcmp (arg, "-v") == 0 || strcmp (arg, "--version") == 0) {
               fprintf (stderr, "dfbscreen version %s\n", DIRECTFB_VERSION);
               return false;
          }

          for (i=0; i<D_ARRAY_SIZE(options); i++) {
               const AnyOption *opt = &options[i];

               if (!strcmp (arg, opt->short_name) || !strcmp (arg, opt->long_name)) {
                    if (++n == argc) {
                         print_usage (argv[0]);
                         return false;
                    }

                    if (!parse_option( opt, argv[n] ))
                         return false;

                    ok = true;

                    break;
               }
          }

          if (!ok) {
               print_usage (argv[0]);
               return false;
          }
     }

     return true;
}

/**************************************************************************************************/

static const char *
tree_name( DFBScreenMixerTree tree )
{
     int i;

     for (i=0; i<D_ARRAY_SIZE(tree_names); i++) {
          if (tree_names[i].tree == tree)
               return tree_names[i].name;
     }

     return "<invalid>";
}

static void
dump_mixer_config( const DFBScreenMixerConfig *config )
{
     int n;

     printf( "\n" );

     if (config->flags & DSMCONF_TREE)
          printf( "Tree:          %s\n", tree_name( config->tree ) );

     if (config->flags & DSMCONF_LEVEL)
          printf( "Level:         %d\n", config->level );

     if (config->flags & DSMCONF_LAYERS) {
          printf( "Layers:        " );

          for (n=0; n<DFB_DISPLAYLAYER_IDS_MAX; n++) {
               if (DFB_DISPLAYLAYER_IDS_HAVE( config->layers, n ))
                    printf( "(%02x) ", n );
          }

          printf( "\n" );
     }

     if (config->flags & DSMCONF_BACKGROUND)
          printf( "Background:    0x%02x, 0x%02x, 0x%02x (RGB)\n",
                  config->background.r, config->background.g, config->background.b );

     printf( "\n" );
}

/**************************************************************************************************/

static const char *
test_picture_name( DFBScreenEncoderTestPicture test_picture )
{
     int i;

     for (i=0; i<D_ARRAY_SIZE(test_picture_names); i++) {
          if (test_picture_names[i].test_picture == test_picture)
               return test_picture_names[i].name;
     }

     return "<invalid>";
}

static const char *
scan_mode_name( DFBScreenEncoderTestPicture scan_mode )
{
     int i;

     for (i=0; i<D_ARRAY_SIZE(scan_mode_names); i++) {
          if (scan_mode_names[i].scan_mode == scan_mode)
               return scan_mode_names[i].name;
     }

     return "<invalid>";
}

static void
dump_encoder_config( const DFBScreenEncoderConfig *config )
{
     int i;

     printf( "\n" );

     if (config->flags & DSECONF_TV_STANDARD) {
          printf( "TV Standard:   " );

          for (i=0; i<D_ARRAY_SIZE(tv_standard_names); i++) {
               if (config->tv_standard & tv_standard_names[i].standard)
                    printf( "%s ", tv_standard_names[i].name );
          }

          printf( "\n" );
     }

     if (config->flags & DSECONF_TEST_PICTURE)
          printf( "Test Picture:  %s\n", test_picture_name( config->test_picture ) );

     if (config->flags & DSECONF_MIXER)
          printf( "Mixer:         %d\n", config->mixer );

     if (config->flags & DSECONF_OUT_SIGNALS) {
          printf( "Signals:       " );

          for (i=0; i<D_ARRAY_SIZE(signal_names); i++) {
               if (config->out_signals & signal_names[i].signal)
                    printf( "%s ", signal_names[i].name );
          }

          printf( "\n" );
     }

     if (config->flags & DSECONF_SCANMODE)
          printf( "Scan Mode:     %s\n", scan_mode_name( config->scanmode ) );

     printf( "\n" );
}