summaryrefslogtreecommitdiff
path: root/utils/viewer-main.c
blob: 293ce1b46b7b809eaa773039908586c005cc7e09 (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
/* viewer-main.c: Main routine for viewers
 *
 * Copyright (C) 1999,2004,2005 Red Hat, Inc.
 * Copyright (C) 2001 Sun Microsystems
 * Copyright (C) 2006 Behdad Esfahbod
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include "config.h"
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>

#include <glib.h>
#include <glib/gstdio.h>

#ifdef G_OS_UNIX
#include <sys/wait.h>
#endif

#include "viewer.h"
#include "viewer-render.h"

int
main (int    argc,
      char **argv)
{
  const PangoViewer *view;
  gpointer instance;
  PangoContext *context;
  int run;
  int width, height;
  gpointer surface;

  g_set_prgname ("pango-view");
  setlocale (LC_ALL, "");
  parse_options (argc, argv);

  view = opt_viewer;

  g_assert (view->id);

  instance = view->create (view);
  context = view->get_context (instance);
  width = height = 1;
  surface = view->create_surface (instance, width, height);
  view->render (instance, surface, context, &width, &height, NULL);
  view->destroy_surface (instance, surface);
  surface = view->create_surface (instance, width, height);
  for (run = 0; run < MAX(1,opt_runs); run++)
    view->render (instance, surface, context, &width, &height, NULL);

  if (opt_output)
    {
      if (!view->write)
	fail ("%s viewer backend does not support writing", view->name);
      else
	{
	  FILE *stream;
	  GPid pid = 0;

	  if (view->write_suffix && g_str_has_suffix (opt_output, view->write_suffix))
	    {
	      stream = g_fopen (opt_output, "wb");
	      if (!stream)
		fail ("Cannot open output file %s: %s\n",
		      opt_output, g_strerror (errno));
	    }
	  else
	    {
	      int fd;
	      const gchar *convert_argv[5] = {"gm", "convert", "-", "%s"};
	      GError *error;

	      convert_argv[3] = opt_output;

	      if (!g_spawn_async_with_pipes (NULL, (gchar **)(void*)convert_argv, NULL,
					     G_SPAWN_DO_NOT_REAP_CHILD |
					     G_SPAWN_SEARCH_PATH |
					     G_SPAWN_STDOUT_TO_DEV_NULL |
					     G_SPAWN_STDERR_TO_DEV_NULL,
					     NULL, NULL, &pid, &fd, NULL, NULL, &error))
		fail ("When running GraphicsMagick 'gm convert' command: %s\n", error->message);
	      stream = fdopen (fd, "wb");
	    }
	  view->write (instance, surface, stream, width, height);
	  fclose (stream);
#ifdef G_OS_UNIX
	  if (pid)
	    waitpid (pid, NULL, 0);
#endif
	}
    }

  if (opt_display)
    {
      char *title;
      title = get_options_string ();

      if (view->display)
	{
	  gpointer window = NULL;
	  gpointer state = NULL;

	  if (view->create_window)
	    {
	      window = view->create_window (instance, title, width, height);
	      if (!window)
	        goto no_display;
	    }

	  opt_display = FALSE;
	  while (1)
	    {
	      state = view->display (instance, surface, window, width, height, state);
	      if (state == GINT_TO_POINTER (-1))
		break;

	      view->render (instance, surface, context, &width, &height, state);
	    }

	  if (view->destroy_window)
	    view->destroy_window (instance, window);
	}
no_display:

      /* If failed to display natively, call ImageMagick */
      if (opt_display)
	{
	  int fd;
	  FILE *stream;
	  const gchar *display_argv[6] = {"gm", "display", "-title", "%s", "-"};
	  GError *error = NULL;
	  GPid pid;

	  if (!view->write)
	    fail ("%s viewer backend does not support displaying or writing", view->name);
	  display_argv[3] = title;

	  if (!g_spawn_async_with_pipes (NULL, (gchar **)(void*)display_argv, NULL,
					 G_SPAWN_DO_NOT_REAP_CHILD |
					 G_SPAWN_SEARCH_PATH |
					 G_SPAWN_STDOUT_TO_DEV_NULL |
					 G_SPAWN_STDERR_TO_DEV_NULL,
					 NULL, NULL, &pid, &fd, NULL, NULL, &error))
	    fail ("When running GraphicsMagick 'gm display' command: %s\n", error->message);
	  stream = fdopen (fd, "wb");
	  view->write (instance, surface, stream, width, height);
	  fclose (stream);
#ifdef G_OS_UNIX
	  waitpid (pid, NULL, 0);
#endif
	  g_spawn_close_pid (pid);
	}

      g_free (title);
    }

  view->destroy_surface (instance, surface);
  g_object_unref (context);
  view->destroy (instance);
  finalize ();
  return 0;
}