summaryrefslogtreecommitdiff
path: root/player/main.c
blob: 94bf414541cb73e3a5fca01ad0e271ef04ebd5a5 (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
/* Swfdec
 * Copyright (C) 2007 Benjamin Otte <otte@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <swfdec-directfb/swfdec-directfb.h>
#include <directfb.h>
#include <directfb/directfb_strings.h>
#include <stdio.h>

#define ERROR_CHECK(x) G_STMT_START{ \
  DFBResult err = (x); \
  if (err != DFB_OK) { \
    DirectFBErrorFatal ("fatal error", err); \
  } \
}G_STMT_END

static DFBSurfacePixelFormat pixel_format = DSPF_UNKNOWN;

static gboolean
parse_pixel_format (const gchar *option_name, const gchar *value, gpointer data,
    GError **error)
{
  static const DirectFBPixelFormatNames (names)
  guint i;

  for (i = 0; i < G_N_ELEMENTS (names); i++) {
    if (g_ascii_strcasecmp (names[i].name, value) == 0) {
      pixel_format = names[i].format;
      return TRUE;
    }
  }
  g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
      "%s is not a valid pixel format", value);
  return FALSE;
}

int 
main (int argc, char *argv[])
{
  GError *error = NULL;
  GOptionContext *ctx;
  DFBSurfaceDescription dsc;
  SwfdecPlayer *player;
  SwfdecDfbRenderer *renderer;
  guint w, h;
  IDirectFB *dfb;
  IDirectFBSurface *surface;
  /* config variables */
  char *size = NULL;
  int cache_size = 50 * 1024; /* 50 MB - Swfdec default value */
  int garbage_size = 8 * 1024; /* 8 MB - Swfdec default value */

  GOptionEntry options[] = {
    { "cache-size", 'c', 0, G_OPTION_ARG_INT, &cache_size, "Maxmimum amount of memory to be used as cache (default: 50MB)", "KB" },
    { "format", 'f', 0, G_OPTION_ARG_CALLBACK, parse_pixel_format, "pixel format to use for the outpt window", "FORMAT" },
    { "garbage-size", 'g', 0, G_OPTION_ARG_INT, &garbage_size, "Amount of kB before garbage collection runs (default: 8MB)", "KB" },
    { "size", 's', 0, G_OPTION_ARG_STRING, &size, "WIDTHxHEIGHT to specify a size or \'fullscreen\' to run fullscreen", "STRING" },
    { NULL }
  };

  ERROR_CHECK (DirectFBInit (&argc, &argv));
  swfdec_init ();

  ctx = g_option_context_new ("");
  g_option_context_add_main_entries (ctx, options, "options");
  g_option_context_parse (ctx, &argc, &argv, &error);
  g_option_context_free (ctx);

  if (error) {
    g_printerr ("Error parsing command line arguments: %s\n", error->message);
    g_error_free (error);
    return 1;
  }

  if (argc < 2) {
    g_printerr ("Usage: %s [OPTIONS] filename\n", argv[0]);
    return 1;
  }
  
  ERROR_CHECK (DirectFBCreate (&dfb));
  player = swfdec_dfb_player_new_from_file (dfb, argv[1]);
  g_object_set (player, "cache-size", (gulong) cache_size * 1024, 
      "memory-until-gc", (gulong) garbage_size * 1024, NULL);

  dsc.flags = DSDESC_CAPS;
  dsc.caps = DSCAPS_DOUBLE | DSCAPS_PRIMARY;
  if (pixel_format != DSPF_UNKNOWN) {
    dsc.flags |= DSDESC_PIXELFORMAT;
    dsc.pixelformat = pixel_format;
  }
  if (size) {
    if (g_ascii_strcasecmp (size, "fullscreen") == 0) {
      ERROR_CHECK (dfb->SetCooperativeLevel (dfb, DFSCL_FULLSCREEN));
    } else if (sscanf (size, "%ux%u", &w, &h) == 2) {
      dsc.flags |= DSDESC_WIDTH | DSDESC_HEIGHT;
      dsc.width = w;
      dsc.height = h;
    } else {
      g_printerr ("invalid argument for --size specified\n");
      g_object_unref (player);
      return 1;
    }
  } else {
    ERROR_CHECK (dfb->SetCooperativeLevel (dfb, DFSCL_FULLSCREEN));
  }

  ERROR_CHECK (dfb->CreateSurface (dfb, &dsc, &surface));
  //ERROR_CHECK (surface->GetSize (surface, &dsc.width, &dsc.height));
  //swfdec_player_set_size (player, dsc.width, dsc.height);
  renderer = swfdec_dfb_renderer_new (dfb, surface, player);

  swfdec_dfb_player_set_playing (SWFDEC_DFB_PLAYER (player), TRUE);
  swfdec_dfb_main ();

  g_object_unref (renderer);
  g_object_unref (player);
  surface->Release (surface);
  dfb->Release (dfb);
  return 0;
}