~ubuntu-branches/ubuntu/feisty/zapping/feisty

« back to all changes in this revision

Viewing changes to src/subtitle.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-03-08 23:19:08 UTC
  • mfrom: (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050308231908-oip7rfv6lcmo8c0e
Tags: 0.9.2-2ubuntu1
Rebuilt for Python transition (2.3 -> 2.4)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Zapping (TV viewer for the Gnome Desktop)
 
3
 *
 
4
 * Copyright (C) 2001 I�aki Garc�a Etxebarria
 
5
 * Copyright (C) 2003 Michael H. Schimek
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
 */
 
21
 
 
22
/* $Id: subtitle.c,v 1.3 2004/11/03 06:42:17 mschimek Exp $ */
 
23
 
 
24
#include "subtitle.h"
 
25
 
 
26
#ifdef HAVE_LIBZVBI
 
27
 
 
28
#include <gnome.h>
 
29
 
 
30
#include "zmisc.h"
 
31
#include "zconf.h"
 
32
#include "zvbi.h"
 
33
#include "osd.h"
 
34
#include "remote.h"
 
35
 
 
36
#ifndef ZVBI_CAPTION_DEBUG
 
37
#define ZVBI_CAPTION_DEBUG 0
 
38
#endif
 
39
 
 
40
static const char *
 
41
caption_key = "/zapping/internal/callbacks/closed_caption";
 
42
 
 
43
/* used by osd.c; use py_closed_caption to change this var. */
 
44
vbi_pgno                zvbi_caption_pgno       = 0;
 
45
 
 
46
static void
 
47
on_subtitle_menu_activate       (GtkWidget *            menu_item,
 
48
                                 gpointer               user_data)
 
49
{
 
50
  vbi_decoder *vbi;
 
51
  vbi_page_type classf;
 
52
  vbi_pgno pgno;
 
53
 
 
54
  if (!(vbi = zvbi_get_object ()))
 
55
    return;
 
56
 
 
57
  pgno = GPOINTER_TO_INT (user_data);
 
58
 
 
59
  classf = vbi_classify_page (vbi, pgno, NULL, NULL);
 
60
 
 
61
  if (VBI_SUBTITLE_PAGE == classf
 
62
      || (VBI_NORMAL_PAGE == classf
 
63
          && (pgno >= 5 && pgno <= 8)))
 
64
    {
 
65
      zvbi_caption_pgno = pgno;
 
66
 
 
67
      python_command_printf (menu_item, "zapping.closed_caption(1)");
 
68
    }
 
69
}
 
70
 
 
71
static GnomeUIInfo
 
72
subtitles_uiinfo [] = {
 
73
  {
 
74
    GNOME_APP_UI_ITEM, N_("_Disable"), NULL,
 
75
    G_CALLBACK (on_python_command1), "zapping.closed_caption(0)", NULL,
 
76
    GNOME_APP_PIXMAP_NONE, NULL,
 
77
    0, (GdkModifierType) 0, NULL
 
78
  },
 
79
  GNOMEUIINFO_END
 
80
};
 
81
 
 
82
GtkWidget *
 
83
subtitle_menu_new               (void)
 
84
{
 
85
  vbi_decoder *vbi;
 
86
  GtkMenuShell *menu;
 
87
  vbi_pgno pgno;
 
88
  unsigned int count;
 
89
 
 
90
  if (!(vbi = zvbi_get_object ()))
 
91
    return NULL;
 
92
 
 
93
  menu = GTK_MENU_SHELL (gtk_menu_new ());
 
94
 
 
95
  gnome_app_fill_menu (menu, subtitles_uiinfo,
 
96
                       /* accel */ NULL,
 
97
                       /* mnemo */ TRUE,
 
98
                       /* position */ 0);
 
99
 
 
100
  count = 0;
 
101
 
 
102
  for (pgno = 1; pgno <= 0x899;
 
103
       pgno = (pgno == 8) ? 0x100 : vbi_add_bcd (pgno, 0x001))
 
104
    {
 
105
      vbi_page_type classf;
 
106
      gchar *language;
 
107
      gchar *buffer;
 
108
      GtkWidget *menu_item;
 
109
 
 
110
      classf = vbi_classify_page (vbi, pgno, NULL, &language);
 
111
 
 
112
      if (classf != VBI_SUBTITLE_PAGE
 
113
          && (pgno > 8 || classf != VBI_NORMAL_PAGE))
 
114
          continue;
 
115
 
 
116
      if (language)
 
117
        language = g_convert (language, strlen (language),
 
118
                              "UTF-8", "ISO-8859-1",
 
119
                              NULL, NULL, NULL);
 
120
 
 
121
      if (pgno <= 8)
 
122
        {
 
123
          if (language)
 
124
            {
 
125
              if (classf == VBI_SUBTITLE_PAGE)
 
126
                buffer = g_strdup_printf (("Caption %x - %s"),
 
127
                                          pgno, language);
 
128
              else
 
129
                buffer = g_strdup_printf (("Text %x - %s"),
 
130
                                          pgno - 4, language);
 
131
            }
 
132
          else
 
133
            if (classf == VBI_SUBTITLE_PAGE)
 
134
              buffer = g_strdup_printf (("Caption %x"), pgno);
 
135
            else
 
136
              buffer = g_strdup_printf (("Text %x"), pgno - 4);
 
137
        }
 
138
      else
 
139
        {
 
140
          if (language)
 
141
            buffer = g_strdup (language);
 
142
          else
 
143
            buffer = g_strdup_printf (_("Page %x"), pgno);
 
144
        }
 
145
 
 
146
      menu_item = gtk_menu_item_new_with_label (buffer);
 
147
      g_free (buffer);
 
148
 
 
149
      gtk_widget_show (menu_item);
 
150
 
 
151
      if (pgno >= 0x100 && language)
 
152
        {
 
153
          gchar buffer [32];
 
154
 
 
155
          g_snprintf (buffer, sizeof (buffer), "%x", pgno);
 
156
          z_tooltip_set (menu_item, buffer);
 
157
        }
 
158
 
 
159
      g_signal_connect (G_OBJECT (menu_item), "activate",
 
160
                        G_CALLBACK (on_subtitle_menu_activate),
 
161
                        GINT_TO_POINTER (pgno));
 
162
 
 
163
      if (0 == count)
 
164
        gtk_menu_shell_append (menu, gtk_separator_menu_item_new ());
 
165
 
 
166
      gtk_menu_shell_append (menu, menu_item);
 
167
 
 
168
      g_free (language);
 
169
 
 
170
      ++count;
 
171
    }
 
172
 
 
173
  return menu ? GTK_WIDGET (menu) : NULL;
 
174
}
 
175
 
 
176
static vbi_pgno
 
177
find_subtitle_page              (void)
 
178
{
 
179
  vbi_decoder *vbi;
 
180
  vbi_pgno pgno;
 
181
 
 
182
  if (!(vbi = zvbi_get_object ()))
 
183
    return 0;
 
184
 
 
185
  for (pgno = 1; pgno <= 0x899;
 
186
       pgno = (pgno == 4) ? 0x100 : vbi_add_bcd (pgno, 0x001))
 
187
    {
 
188
      vbi_page_type classf;
 
189
 
 
190
      classf = vbi_classify_page (vbi, pgno, NULL, NULL);
 
191
 
 
192
      if (VBI_SUBTITLE_PAGE == classf)
 
193
        return pgno;
 
194
    }
 
195
 
 
196
  return 0;
 
197
}
 
198
 
 
199
static PyObject *
 
200
py_closed_caption               (PyObject *             self _unused_,
 
201
                                 PyObject *             args)
 
202
{
 
203
  static int block = 0;
 
204
  int active;
 
205
 
 
206
  if (block)
 
207
    py_return_true;
 
208
  block = 1; /* XXX should be fixed elsewhere, toolbar button? */
 
209
 
 
210
  active = -1; /* toggle */
 
211
 
 
212
  if (!ParseTuple (args, "|i", &active))
 
213
    g_error ("zapping.closed_caption(|i)");
 
214
 
 
215
  if (-1 == active)
 
216
    active = !zconf_get_boolean (NULL, caption_key);
 
217
 
 
218
  if (ZVBI_CAPTION_DEBUG)
 
219
    fprintf (stderr, "CC active: %d\n", active);
 
220
 
 
221
  zconf_set_boolean (active, caption_key);
 
222
 
 
223
  if (active)
 
224
    {
 
225
      TeletextView *view;
 
226
      vbi_pgno pgno;
 
227
  
 
228
      /* In Teletext mode, overlay currently displayed page. */
 
229
      if (/* have */ _teletext_view_from_widget
 
230
          && (view = _teletext_view_from_widget (GTK_WIDGET (zapping)))
 
231
          && 0 != (pgno = view->cur_pgno (view)))
 
232
        {
 
233
          zvbi_caption_pgno = pgno;
 
234
 
 
235
          if (ZVBI_CAPTION_DEBUG)
 
236
            fprintf (stderr, "CC Teletext pgno %x\n", zvbi_caption_pgno);
 
237
 
 
238
          zmisc_restore_previous_mode (zapping->info);
 
239
        }
 
240
      /* In video mode, use previous page or find subtitles. */
 
241
      else if (zvbi_caption_pgno <= 0)
 
242
        {
 
243
          zvbi_caption_pgno = find_subtitle_page ();
 
244
 
 
245
          if (ZVBI_CAPTION_DEBUG)
 
246
            fprintf (stderr, "CC lookup pgno %x\n", zvbi_caption_pgno);
 
247
 
 
248
          if (zvbi_caption_pgno <= 0)
 
249
            {
 
250
              /* Bad luck. */
 
251
              zconf_set_boolean (FALSE, caption_key);
 
252
            }
 
253
        }
 
254
      else
 
255
        {
 
256
          if (ZVBI_CAPTION_DEBUG)
 
257
            fprintf (stderr, "CC previous pgno %x\n", zvbi_caption_pgno);
 
258
        }
 
259
    }
 
260
  else
 
261
    {
 
262
      osd_clear ();
 
263
    }
 
264
 
 
265
  block = 0; 
 
266
 
 
267
  py_return_true;
 
268
}
 
269
 
 
270
void
 
271
shutdown_subtitle               (void)
 
272
{
 
273
  /* Nothing to do. */
 
274
}
 
275
 
 
276
void
 
277
startup_subtitle                (void)
 
278
{
 
279
  zconf_create_boolean (FALSE, "Display subtitles", caption_key);
 
280
 
 
281
  cmd_register ("closed_caption", py_closed_caption, METH_VARARGS,
 
282
                ("Closed Caption on/off"), "zapping.closed_caption()");
 
283
}
 
284
 
 
285
#else /* !HAVE_LIBZVBI */
 
286
 
 
287
GtkWidget *
 
288
subtitle_menu_new               (void)
 
289
{
 
290
  return NULL;
 
291
}
 
292
 
 
293
void
 
294
shutdown_subtitle               (void)
 
295
{
 
296
}
 
297
 
 
298
void
 
299
startup_subtitle                (void)
 
300
{
 
301
}
 
302
 
 
303
#endif /* !HAVE_LIBZVBI */