1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
4 * Copyright 1998 - 2005 The Free Software Foundation
6 * Evince is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Evince is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
23 #include <glib/gstdio.h>
24 #include <glib/gi18n.h>
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkwindow.h>
34 #include "ps-interpreter.h"
37 #define MAX_BUFSIZE 1024
44 /* structure to describe section of file to send to ghostscript */
53 struct _PSInterpreter {
56 GtkWidget *target_window;
59 glong message_window; /* Used by ghostview to receive messages from app */
61 GPid pid; /* PID of interpreter, -1 if none */
62 GIOChannel *input; /* stdin of interpreter */
63 GIOChannel *output; /* stdout of interpreter */
64 GIOChannel *error; /* stderr of interpreter */
69 gboolean busy; /* Is gs busy drawing? */
70 gboolean structured_doc;
73 gchar *input_buffer_ptr;
75 guint buffer_bytes_left;
77 FILE *psfile; /* the currently loaded FILE */
78 gchar *psfilename; /* the currently loaded filename */
80 gboolean send_filename_to_gs; /* True if gs should read from file directly */
81 const struct document *doc;
84 struct _PSInterpreterClass {
85 GObjectClass parent_class;
87 void (* page_rendered) (PSInterpreter *gs,
96 static void ps_interpreter_start (PSInterpreter *gs);
97 static void ps_interpreter_stop (PSInterpreter *gs);
98 static void ps_interpreter_failed (PSInterpreter *gs,
100 static gboolean ps_interpreter_is_ready (PSInterpreter *gs);
102 static void push_pixbuf (PSInterpreter *gs);
104 G_DEFINE_TYPE (PSInterpreter, ps_interpreter, G_TYPE_OBJECT)
106 static guint gs_signals[LAST_SIGNAL];
109 ps_section_free (PSSection *section)
114 if (section->close && section->fp)
115 fclose (section->fp);
121 ps_interpreter_init (PSInterpreter *gs)
124 gs->ps_input = g_queue_new ();
128 ps_interpreter_dispose (GObject *object)
130 PSInterpreter *gs = PS_INTERPRETER (object);
139 if (gs->psfilename) {
140 g_free (gs->psfilename);
141 gs->psfilename = NULL;
145 g_object_unref (gs->bpixmap);
149 if (gs->input_buffer) {
150 g_free (gs->input_buffer);
151 gs->input_buffer = NULL;
154 if (gs->target_window) {
155 gtk_widget_destroy (gs->target_window);
156 gs->target_window = NULL;
161 g_queue_foreach (gs->ps_input, (GFunc)ps_section_free, NULL);
162 g_queue_free (gs->ps_input);
166 ps_interpreter_stop (gs);
168 G_OBJECT_CLASS (ps_interpreter_parent_class)->dispose (object);
172 ps_interpreter_class_init (PSInterpreterClass *klass)
174 GObjectClass *object_class;
176 object_class = G_OBJECT_CLASS (klass);
178 gs_signals[PAGE_RENDERED] =
179 g_signal_new ("page_rendered",
182 G_STRUCT_OFFSET (PSInterpreterClass, page_rendered),
184 g_cclosure_marshal_VOID__OBJECT,
189 klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE);
190 klass->next_atom = gdk_atom_intern ("NEXT", FALSE);
191 klass->page_atom = gdk_atom_intern ("PAGE", FALSE);
192 klass->string_atom = gdk_atom_intern ("STRING", FALSE);
194 object_class->dispose = ps_interpreter_dispose;
198 ps_interpreter_input (GIOChannel *io,
199 GIOCondition condition,
202 PSSection *section = NULL;
205 if (gs->buffer_bytes_left == 0) {
206 /* Get a new section if required */
207 if (gs->bytes_left == 0) {
208 ps_section_free (section);
210 g_queue_pop_tail (gs->ps_input);
213 if (section == NULL) {
214 section = g_queue_peek_tail (gs->ps_input);
217 /* Have to seek at the beginning of each section */
218 if (section && section->seek_needed) {
219 fseek (section->fp, section->begin, SEEK_SET);
220 section->seek_needed = FALSE;
221 gs->bytes_left = section->len;
224 if (gs->bytes_left > MAX_BUFSIZE) {
225 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof (char),
226 MAX_BUFSIZE, section->fp);
227 } else if (gs->bytes_left > 0) {
228 gs->buffer_bytes_left = fread (gs->input_buffer, sizeof (char),
229 gs->bytes_left, section->fp);
231 gs->buffer_bytes_left = 0;
234 if (gs->bytes_left > 0 && gs->buffer_bytes_left == 0) {
235 ps_interpreter_failed (gs, NULL); /* Error occurred */
238 gs->input_buffer_ptr = gs->input_buffer;
239 gs->bytes_left -= gs->buffer_bytes_left;
242 if (gs->buffer_bytes_left > 0) {
245 GError *error = NULL;
247 status = g_io_channel_write_chars (gs->input,
248 gs->input_buffer_ptr,
249 gs->buffer_bytes_left,
253 case G_IO_STATUS_NORMAL:
254 gs->buffer_bytes_left -= bytes_written;
255 gs->input_buffer_ptr += bytes_written;
258 case G_IO_STATUS_ERROR:
259 ps_interpreter_failed (gs, error->message);
260 g_error_free (error);
263 case G_IO_STATUS_AGAIN:
268 } while (!g_queue_is_empty (gs->ps_input) && gs->buffer_bytes_left == 0);
270 if (g_queue_is_empty (gs->ps_input) && gs->buffer_bytes_left == 0) {
273 flags = g_io_channel_get_flags (gs->input);
275 g_io_channel_set_flags (gs->input,
276 flags & ~G_IO_FLAG_NONBLOCK, NULL);
277 g_io_channel_flush (gs->input, NULL);
278 g_io_channel_set_flags (gs->input,
279 flags | G_IO_FLAG_NONBLOCK, NULL);
290 ps_interpreter_output (GIOChannel *io,
291 GIOCondition condition,
294 gchar buf[MAX_BUFSIZE + 1];
297 GError *error = NULL;
299 status = g_io_channel_read_chars (io, buf, MAX_BUFSIZE,
302 case G_IO_STATUS_NORMAL:
308 case G_IO_STATUS_EOF:
309 g_io_channel_unref (gs->output);
314 case G_IO_STATUS_ERROR:
315 ps_interpreter_failed (gs, error->message);
316 g_error_free (error);
325 ps_interpreter_failed (gs, NULL);
332 ps_interpreter_error (GIOChannel *io,
333 GIOCondition condition,
336 gchar buf[MAX_BUFSIZE + 1];
339 GError *error = NULL;
341 status = g_io_channel_read_chars (io, buf, MAX_BUFSIZE,
344 case G_IO_STATUS_NORMAL:
351 case G_IO_STATUS_EOF:
352 g_io_channel_unref (gs->error);
357 case G_IO_STATUS_ERROR:
358 ps_interpreter_failed (gs, error->message);
359 g_error_free (error);
368 ps_interpreter_failed (gs, NULL);
375 ps_interpreter_finished (GPid pid,
379 g_spawn_close_pid (gs->pid);
381 ps_interpreter_failed (gs, NULL);
385 #define NUM_GS_ARGS (NUM_ARGS - 20)
386 #define NUM_ALPHA_ARGS 10
389 setup_interpreter_env (gchar **envp)
393 for (i = 0; envp[i]; i++)
398 ps_interpreter_start (PSInterpreter *gs)
400 gchar *argv[NUM_ARGS], *dir, *gv_env, *gs_path;
401 gchar **gs_args, **alpha_args = NULL;
403 gint pin, pout, perr;
405 GError *error = NULL;
407 g_assert (gs->psfilename != NULL);
409 ps_interpreter_stop (gs);
411 dir = g_path_get_dirname (gs->psfilename);
413 /* set up the args... */
414 gs_path = g_find_program_in_path ("gs");
415 gs_args = g_strsplit (gs_path, " ", NUM_GS_ARGS);
417 for (i = 0; i < NUM_GS_ARGS && gs_args[i]; i++, argc++) {
418 argv[argc] = gs_args[i];
421 alpha_args = g_strsplit (ALPHA_PARAMS, " ", NUM_ALPHA_ARGS);
422 for (i = 0; i < NUM_ALPHA_ARGS && alpha_args[i]; i++, argc++) {
423 argv[argc] = alpha_args[i];
426 argv[argc++] = "-dNOPAUSE";
427 argv[argc++] = "-dQUIET";
428 argv[argc++] = "-dSAFER";
430 if (gs->send_filename_to_gs) {
431 argv[argc++] = gs->psfilename;
433 argv[argc++] = "quit";
440 gv_env = g_strdup_printf ("GHOSTVIEW=%ld %ld;DISPLAY=%s",
441 gdk_x11_drawable_get_xid (gs->pstarget),
442 gdk_x11_drawable_get_xid (gs->bpixmap),
443 gdk_display_get_name (gdk_drawable_get_display (gs->pstarget)));
444 envp = g_strsplit (gv_env, ";", 2);
447 if (g_spawn_async_with_pipes (dir, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
448 (GSpawnChildSetupFunc)setup_interpreter_env, envp,
449 &(gs->pid), &pin, &pout, &perr,
453 g_child_watch_add (gs->pid,
454 (GChildWatchFunc)ps_interpreter_finished,
457 gs->input = g_io_channel_unix_new (pin);
458 g_io_channel_set_encoding (gs->input, NULL, NULL);
459 flags = g_io_channel_get_flags (gs->input);
460 g_io_channel_set_flags (gs->input, flags | G_IO_FLAG_NONBLOCK, NULL);
463 gs->output = g_io_channel_unix_new (pout);
464 flags = g_io_channel_get_flags (gs->output);
465 g_io_channel_set_flags (gs->output, flags | G_IO_FLAG_NONBLOCK, NULL);
466 gs->output_id = g_io_add_watch (gs->output, G_IO_IN,
467 (GIOFunc)ps_interpreter_output,
470 gs->error = g_io_channel_unix_new (perr);
471 flags = g_io_channel_get_flags (gs->error);
472 g_io_channel_set_flags (gs->error, flags | G_IO_FLAG_NONBLOCK, NULL);
473 gs->error_id = g_io_add_watch (gs->error, G_IO_IN,
474 (GIOFunc)ps_interpreter_error,
477 g_warning (error->message);
478 g_error_free (error);
483 g_strfreev (gs_args);
484 g_strfreev (alpha_args);
488 ps_interpreter_stop (PSInterpreter *gs)
493 kill (gs->pid, SIGTERM);
494 while ((wait (&status) == -1) && (errno == EINTR));
495 g_spawn_close_pid (gs->pid);
500 g_io_channel_unref (gs->input);
503 if (gs->input_id > 0) {
504 g_source_remove (gs->input_id);
509 g_queue_foreach (gs->ps_input, (GFunc)ps_section_free, NULL);
510 g_queue_free (gs->ps_input);
511 gs->ps_input = g_queue_new ();
516 g_io_channel_unref (gs->output);
519 if (gs->output_id > 0) {
520 g_source_remove (gs->output_id);
526 g_io_channel_unref (gs->error);
529 if (gs->error_id > 0) {
530 g_source_remove (gs->error_id);
539 ps_interpreter_failed (PSInterpreter *gs, const char *msg)
541 g_warning (msg ? msg : _("Interpreter failed."));
544 ps_interpreter_stop (gs);
548 ps_interpreter_is_ready (PSInterpreter *gs)
550 return (gs->pid != -1 && !gs->busy &&
551 (g_queue_is_empty (gs->ps_input)));
555 setup_page (PSInterpreter *gs, int page, double scale, int rotation)
558 char scaled_dpi[G_ASCII_DTOSTR_BUF_SIZE];
559 int urx, ury, llx, lly;
560 PSInterpreterClass *gs_class = PS_INTERPRETER_GET_CLASS (gs);
562 psgetpagebox (gs->doc, page, &urx, &ury, &llx, &lly);
563 g_ascii_dtostr (scaled_dpi, G_ASCII_DTOSTR_BUF_SIZE, 72.0 * scale);
565 buf = g_strdup_printf ("%ld %d %d %d %d %d %s %s %d %d %d %d",
566 0L, rotation, llx, lly, urx, ury,
567 scaled_dpi, scaled_dpi,
570 gdk_property_change (gs->pstarget, gs_class->gs_atom, gs_class->string_atom,
571 8, GDK_PROP_MODE_REPLACE, (guchar *)buf, strlen (buf));
578 setup_pixmap (PSInterpreter *gs, int page, double scale, int rotation)
581 GdkColor white = { 0, 0xFFFF, 0xFFFF, 0xFFFF }; /* pixel, r, g, b */
582 GdkColormap *colormap;
583 double width, height;
584 int pixmap_width, pixmap_height;
585 int urx, ury, llx, lly;
587 psgetpagebox (gs->doc, page, &urx, &ury, &llx, &lly);
588 width = (urx - llx) + 0.5;
589 height = (ury - lly) + 0.5;
591 if (rotation == 90 || rotation == 270) {
592 pixmap_height = width * scale + 0.5;
593 pixmap_width = height * scale + 0.5;
595 pixmap_width = width * scale + 0.5;
596 pixmap_height = height * scale + 0.5;
602 gdk_drawable_get_size (gs->bpixmap, &w, &h);
604 if (pixmap_width != w || h != pixmap_height) {
605 g_object_unref (gs->bpixmap);
607 ps_interpreter_stop (gs);
612 fill = gdk_gc_new (gs->pstarget);
613 colormap = gdk_drawable_get_colormap (gs->pstarget);
614 gdk_colormap_alloc_color (colormap, &white, FALSE, TRUE);
615 gdk_gc_set_foreground (fill, &white);
616 gs->bpixmap = gdk_pixmap_new (gs->pstarget, pixmap_width,
618 gdk_draw_rectangle (gs->bpixmap, fill, TRUE,
619 0, 0, pixmap_width, pixmap_height);
624 push_pixbuf (PSInterpreter *gs)
630 if (gs->pstarget == NULL)
633 cmap = gdk_drawable_get_colormap (gs->pstarget);
634 gdk_drawable_get_size (gs->bpixmap, &width, &height);
635 pixbuf = gdk_pixbuf_get_from_drawable (NULL, gs->bpixmap, cmap,
638 g_signal_emit (gs, gs_signals[PAGE_RENDERED], 0, pixbuf);
639 g_object_unref (pixbuf);
643 ps_interpreter_widget_event (GtkWidget *widget,
647 PSInterpreterClass *gs_class = PS_INTERPRETER_GET_CLASS (gs);
649 if (event->type != GDK_CLIENT_EVENT)
652 gs->message_window = event->client.data.l[0];
654 if (event->client.message_type == gs_class->page_atom) {
664 send_ps (PSInterpreter *gs, glong begin, guint len, gboolean close)
668 g_assert (gs->psfile != NULL);
671 g_critical ("No pipe to gs: error in send_ps().");
675 ps_new = g_new0 (PSSection, 1);
676 ps_new->fp = gs->psfile;
677 ps_new->begin = begin;
679 ps_new->seek_needed = TRUE;
680 ps_new->close = close;
682 if (gs->input_buffer == NULL) {
683 gs->input_buffer = g_malloc (MAX_BUFSIZE);
686 if (g_queue_is_empty (gs->ps_input)) {
687 gs->input_buffer_ptr = gs->input_buffer;
688 gs->bytes_left = len;
689 gs->buffer_bytes_left = 0;
690 g_queue_push_head (gs->ps_input, ps_new);
691 gs->input_id = g_io_add_watch (gs->input, G_IO_OUT,
692 (GIOFunc)ps_interpreter_input,
695 g_queue_push_head (gs->ps_input, ps_new);
700 ps_interpreter_next_page (PSInterpreter *gs)
706 PSInterpreterClass *gs_class = PS_INTERPRETER_GET_CLASS (gs);
708 g_assert (gs->pid != 0);
709 g_assert (gs->busy != TRUE);
713 screen = gtk_window_get_screen (GTK_WINDOW (gs->target_window));
714 display = gdk_screen_get_display (screen);
715 dpy = gdk_x11_display_get_xdisplay (display);
717 event.xclient.type = ClientMessage;
718 event.xclient.display = dpy;
719 event.xclient.window = gs->message_window;
720 event.xclient.message_type =
721 gdk_x11_atom_to_xatom_for_display (display,
722 gs_class->next_atom);
723 event.xclient.format = 32;
725 gdk_error_trap_push ();
726 XSendEvent (dpy, gs->message_window, FALSE, 0, &event);
728 gdk_error_trap_pop ();
732 render_page (PSInterpreter *gs, gint page)
734 if (gs->structured_doc && gs->doc) {
735 if (ps_interpreter_is_ready (gs)) {
736 ps_interpreter_next_page (gs);
738 ps_interpreter_start (gs);
739 send_ps (gs, gs->doc->beginprolog, gs->doc->lenprolog, FALSE);
740 send_ps (gs, gs->doc->beginsetup, gs->doc->lensetup, FALSE);
743 send_ps (gs, gs->doc->pages[page].begin,
744 gs->doc->pages[page].len, FALSE);
746 /* Unstructured document
748 * In the case of non structured documents,
749 * GS read the PS from the actual file (via command
750 * line. Hence, ggv only send a signal next page.
751 * If ghostview is not running it is usually because
752 * the last page of the file was displayed. In that
753 * case, ggv restarts GS again and the first page is displayed.
756 if (!ps_interpreter_is_ready (gs)) {
757 ps_interpreter_start (gs);
759 ps_interpreter_next_page (gs);
765 ps_interpreter_new (const gchar *filename,
766 const struct document *doc)
770 g_return_val_if_fail (filename != NULL, NULL);
771 g_return_val_if_fail (doc != NULL, NULL);
773 gs = PS_INTERPRETER (g_object_new (PS_TYPE_INTERPRETER, NULL));
775 gs->psfilename = g_strdup (filename);
777 gs->structured_doc = FALSE;
778 gs->send_filename_to_gs = TRUE;
779 gs->psfile = fopen (gs->psfilename, "r");
781 if ((!gs->doc->epsf && gs->doc->numpages > 0) ||
782 (gs->doc->epsf && gs->doc->numpages > 1)) {
783 gs->structured_doc = TRUE;
784 gs->send_filename_to_gs = FALSE;
791 ps_interpreter_render_page (PSInterpreter *gs,
796 g_return_if_fail (PS_IS_INTERPRETER (gs));
798 if (gs->pstarget == NULL) {
799 gs->target_window = gtk_window_new (GTK_WINDOW_POPUP);
800 gtk_widget_realize (gs->target_window);
801 gs->pstarget = gs->target_window->window;
803 g_assert (gs->pstarget != NULL);
805 g_signal_connect (gs->target_window, "event",
806 G_CALLBACK (ps_interpreter_widget_event),
810 setup_pixmap (gs, page, scale, rotation);
811 setup_page (gs, page, scale, rotation);
813 render_page (gs, page);