Commit c57fc804 authored by gheet's avatar gheet
Browse files

Adding PAPI printbackend

Adding a new print backend module using libpapi detals see GNOME#382676.
(gheet)
parent 0fd185fa
......@@ -1879,6 +1879,28 @@ else
AM_CONDITIONAL(HAVE_CUPS, false)
fi
# Checks to see if we should compile with PAPI backend for GTK+
#
AC_ARG_ENABLE(papi,
[AC_HELP_STRING([--disable-papi]
[disable papi print backend])],,
[enable_papi=auto])
if test "x$enable_papi" = "xauto"
then
AC_MSG_CHECKING(libpapi)
AC_CHECK_LIB(papi, papiServiceCreate, have_papi=yes, have_papi=no)
if test $have_papi = yes; then
AC_DEFINE([HAVE_PAPI], [], [Define to 1 if libpapi available])
fi
AM_CONDITIONAL(HAVE_PAPI, test $have_papi = yes)
else
AM_CONDITIONAL(HAVE_PAPI, false)
fi
AM_CONDITIONAL(HAVE_PAPI_CUPS, test $have_papi = yes && test "x$CUPS_CONFIG" != "xno")
gtk_save_cppflags="$CPPFLAGS"
CPPFLAGS="$CPPFLAGS $GTK_DEP_CFLAGS"
......@@ -2107,6 +2129,7 @@ modules/printbackends/Makefile
modules/printbackends/cups/Makefile
modules/printbackends/lpr/Makefile
modules/printbackends/file/Makefile
modules/printbackends/papi/Makefile
modules/printbackends/test/Makefile
perf/Makefile
contrib/Makefile
......
......@@ -10,11 +10,19 @@ endif
SUBDIRS = theme-bits . tests
DIST_SUBDIRS=theme-bits tests
if HAVE_PAPI_CUPS
GTK_PRINT_BACKENDS=file,papi,cups
else
if HAVE_CUPS
GTK_PRINT_BACKENDS=file,cups
else
if HAVE_PAPI
GTK_PRINT_BACKENDS=file,papi
else
GTK_PRINT_BACKENDS=file,lpr
endif
endif
endif
INCLUDES = \
-DG_LOG_DOMAIN=\"Gtk\" \
......
......@@ -10,6 +10,10 @@ if TEST_PRINT_BACKEND
SUBDIRS += test
endif
if HAVE_PAPI
SUBDIRS += papi
endif
DIST_SUBDIRS = cups file lpr test
-include $(top_srcdir)/git.mk
if OS_WIN32
no_undefined = -no-undefined
endif
INCLUDES = \
-I$(top_srcdir) \
-I$(top_srcdir)/gtk \
-I$(top_builddir)/gtk \
-I$(top_srcdir)/gdk \
-I$(top_builddir)/gdk \
-DGTK_PRINT_BACKEND_ENABLE_UNSUPPORTED \
$(GTK_DEP_CFLAGS) \
$(GTK_DEBUG_FLAGS)
LDADDS = \
$(GTK_DEP_LIBS) \
$(top_builddir)/gtk/$(gtktargetlib)
backenddir = $(libdir)/gtk-2.0/$(GTK_BINARY_VERSION)/printbackends
backend_LTLIBRARIES = libprintbackend-papi.la
libprintbackend_papi_la_SOURCES = \
gtkprinterpapi.c \
gtkprintbackendpapi.c
noinst_HEADERS = \
gtkprinterpapi.h \
gtkprintbackendpapi.h
libprintbackend_papi_la_LDFLAGS = -avoid-version -module $(no_undefined)
libprintbackend_papi_la_LIBADD = $(LDADDS) -lpapi
/* GTK - The GIMP Toolkit
* gtkprintbackendpapi.c: Default implementation of GtkPrintBackend
* for printing to papi
* Copyright (C) 2003, Red Hat, Inc.
* Copyright (C) 2009, Sun Microsystems, Inc.
*
* 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 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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <papi.h>
#include <config.h>
#include <errno.h>
#include <cairo.h>
#include <cairo-ps.h>
#include <glib/gi18n-lib.h>
#include "gtk.h"
#include "gtkprintbackendpapi.h"
#include "gtkprinterpapi.h"
#include "gtkprinter-private.h"
typedef struct _GtkPrintBackendPapiClass GtkPrintBackendPapiClass;
#define GTK_PRINT_BACKEND_PAPI_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_BACKEND_PAPI, GtkPrintBackendPapiClass))
#define GTK_IS_PRINT_BACKEND_PAPI_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_BACKEND_PAPI))
#define GTK_PRINT_BACKEND_PAPI_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_BACKEND_PAPI, GtkPrintBackendPapiClass))
#define _PAPI_MAX_CHUNK_SIZE 8192
static GType print_backend_papi_type = 0;
struct _GtkPrintBackendPapiClass
{
GtkPrintBackendClass parent_class;
};
struct _GtkPrintBackendPapi
{
GtkPrintBackend parent_instance;
char *default_printer;
};
typedef struct {
GtkPrinter *printer;
} _PrinterStatus;
static GObjectClass *backend_parent_class;
static void gtk_print_backend_papi_class_init (GtkPrintBackendPapiClass *class);
static void gtk_print_backend_papi_init (GtkPrintBackendPapi *impl);
static void gtk_print_backend_papi_finalize (GObject *object);
static void gtk_print_backend_papi_dispose (GObject *object);
static void papi_request_printer_list (GtkPrintBackend *print_backend);
static gboolean papi_get_printer_list (GtkPrintBackendPapi *papi_backend);
static void papi_printer_request_details (GtkPrinter *printer);
static GtkPrintCapabilities papi_printer_get_capabilities (GtkPrinter *printer);
static void papi_printer_get_settings_from_options (GtkPrinter *printer,
GtkPrinterOptionSet *options,
GtkPrintSettings *settings);
static GtkPrinterOptionSet *papi_printer_get_options (GtkPrinter *printer,
GtkPrintSettings *settings,
GtkPageSetup *page_setup,
GtkPrintCapabilities capabilities);
static void papi_printer_prepare_for_print (GtkPrinter *printer,
GtkPrintJob *print_job,
GtkPrintSettings *settings,
GtkPageSetup *page_setup);
static cairo_surface_t * papi_printer_create_cairo_surface (GtkPrinter *printer,
GtkPrintSettings *settings,
gdouble width,
gdouble height,
GIOChannel *cache_io);
static void gtk_print_backend_papi_print_stream (GtkPrintBackend *print_backend,
GtkPrintJob *job,
GIOChannel *data_io,
GtkPrintJobCompleteFunc callback,
gpointer user_data,
GDestroyNotify dnotify);
static gboolean papi_display_printer_status (gpointer user_data);
static void papi_display_printer_status_done (gpointer user_data);
static void
gtk_print_backend_papi_register_type (GTypeModule *module)
{
static const GTypeInfo print_backend_papi_info =
{
sizeof (GtkPrintBackendPapiClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) gtk_print_backend_papi_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GtkPrintBackendPapi),
0, /* n_preallocs */
(GInstanceInitFunc) gtk_print_backend_papi_init,
};
print_backend_papi_type = g_type_module_register_type (module,
GTK_TYPE_PRINT_BACKEND,
"GtkPrintBackendPapi",
&print_backend_papi_info, 0);
}
G_MODULE_EXPORT void
pb_module_init (GTypeModule *module)
{
gtk_print_backend_papi_register_type (module);
gtk_printer_papi_register_type (module);
}
G_MODULE_EXPORT void
pb_module_exit (void)
{
}
G_MODULE_EXPORT GtkPrintBackend *
pb_module_create (void)
{
return gtk_print_backend_papi_new ();
}
/*
* GtkPrintBackendPapi
*/
GType
gtk_print_backend_papi_get_type (void)
{
return print_backend_papi_type;
}
/**
* gtk_print_backend_papi_new:
*
* Creates a new #GtkPrintBackendPapi object. #GtkPrintBackendPapi
* implements the #GtkPrintBackend interface with direct access to
* the filesystem using Unix/Linux API calls
*
* Return value: the new #GtkPrintBackendPapi object
**/
GtkPrintBackend *
gtk_print_backend_papi_new (void)
{
return g_object_new (GTK_TYPE_PRINT_BACKEND_PAPI, NULL);
}
static void
gtk_print_backend_papi_class_init (GtkPrintBackendPapiClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (class);
backend_parent_class = g_type_class_peek_parent (class);
gobject_class->finalize = gtk_print_backend_papi_finalize;
gobject_class->dispose = gtk_print_backend_papi_dispose;
backend_class->request_printer_list = papi_request_printer_list;
backend_class->printer_request_details = papi_printer_request_details;
backend_class->printer_get_capabilities = papi_printer_get_capabilities;
backend_class->printer_get_options = papi_printer_get_options;
backend_class->printer_get_settings_from_options = papi_printer_get_settings_from_options;
backend_class->printer_prepare_for_print = papi_printer_prepare_for_print;
backend_class->printer_create_cairo_surface = papi_printer_create_cairo_surface;
backend_class->print_stream = gtk_print_backend_papi_print_stream;
}
static cairo_status_t
_cairo_write (void *closure,
const unsigned char *data,
unsigned int length)
{
GIOChannel *io = (GIOChannel *)closure;
gsize written;
GError *error = NULL;
GTK_NOTE (PRINTING,
g_print ("PAPI Backend: Writting %i byte chunk to temp file\n", length));
while (length > 0)
{
g_io_channel_write_chars (io, (char *)data, length, &written, &error);
if (error != NULL)
{
GTK_NOTE (PRINTING,
g_print ("PAPI Backend: Error writting to temp file, %s\n", error->message));
g_error_free (error);
return CAIRO_STATUS_WRITE_ERROR;
}
GTK_NOTE (PRINTING,
g_print ("PAPI Backend: Wrote %i bytes to temp file\n", written));
data += written;
length -= written;
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_surface_t *
papi_printer_create_cairo_surface (GtkPrinter *printer,
GtkPrintSettings *settings,
gdouble width,
gdouble height,
GIOChannel *cache_io)
{
cairo_surface_t *surface;
surface = cairo_ps_surface_create_for_stream (_cairo_write, cache_io, width, height);
/* TODO: DPI from settings object? */
cairo_surface_set_fallback_resolution (surface, 300, 300);
return surface;
}
typedef struct {
GtkPrintBackend *backend;
GtkPrintJobCompleteFunc callback;
GtkPrintJob *job;
gpointer user_data;
GDestroyNotify dnotify;
papi_service_t service;
papi_stream_t stream;
} _PrintStreamData;
static void
papi_print_cb (GtkPrintBackendPapi *print_backend,
GError *error,
gpointer user_data)
{
_PrintStreamData *ps = (_PrintStreamData *) user_data;
if (ps->callback)
ps->callback (ps->job, ps->user_data, error);
if (ps->dnotify)
ps->dnotify (ps->user_data);
gtk_print_job_set_status (ps->job,
error ? GTK_PRINT_STATUS_FINISHED_ABORTED
: GTK_PRINT_STATUS_FINISHED);
if (ps->job)
g_object_unref (ps->job);
g_free (ps);
}
static gboolean
papi_write (GIOChannel *source,
GIOCondition con,
gpointer user_data)
{
gchar buf[_PAPI_MAX_CHUNK_SIZE];
gsize bytes_read;
GError *error;
GIOStatus status;
_PrintStreamData *ps = (_PrintStreamData *) user_data;
papi_job_t job = NULL;
error = NULL;
status = g_io_channel_read_chars (source,
buf,
_PAPI_MAX_CHUNK_SIZE,
&bytes_read,
&error);
/* Keep writing to PAPI input stream while there are data */
if (status != G_IO_STATUS_ERROR)
{
papiJobStreamWrite (ps->service, ps->stream, buf, bytes_read);
}
/* Finish reading input stream data. Closing the stream and handle to service */
if (bytes_read == 0) {
papiJobStreamClose (ps->service, ps->stream, &job);
ps->stream = NULL;
papiJobFree (job);
papiServiceDestroy (ps->service);
ps->service = NULL;
}
if (error != NULL || status == G_IO_STATUS_EOF)
{
papi_print_cb (GTK_PRINT_BACKEND_PAPI (ps->backend),
error, user_data);
if (error)
g_error_free (error);
if (error != NULL)
{
GTK_NOTE (PRINTING,
g_print ("PAPI Backend: %s\n", error->message));
g_error_free (error);
}
return FALSE;
}
GTK_NOTE (PRINTING,
g_print ("PAPI Backend: Writting %i byte chunk to papi pipe\n", bytes_read));
return TRUE;
}
static void
gtk_print_backend_papi_print_stream (GtkPrintBackend *print_backend,
GtkPrintJob *job,
GIOChannel *data_io,
GtkPrintJobCompleteFunc callback,
gpointer user_data,
GDestroyNotify dnotify)
{
GError *print_error = NULL;
GtkPrinterPapi *printer;
_PrintStreamData *ps;
GtkPrintSettings *settings;
gint argc;
gint in_fd;
gchar **argv = NULL;
const gchar *title;
char *prtnm = NULL;
GtkPrintDuplex val;
papi_status_t pstatus = NULL;
papi_attribute_t **attrs = NULL;
papi_job_ticket_t *ticket = NULL;
printer = GTK_PRINTER_PAPI (gtk_print_job_get_printer (job));
settings = gtk_print_job_get_settings (job);
/* FIXME - the title should be set as the job-name */
title = gtk_print_job_get_title (job);
ps = g_new0 (_PrintStreamData, 1);
ps->callback = callback;
ps->user_data = user_data;
ps->dnotify = dnotify;
ps->job = g_object_ref (job);
ps->service = NULL;
ps->stream = NULL;
/* This cannot be queried yet with the current API */
papiAttributeListAddString (&attrs, PAPI_ATTR_EXCL, "document-format", "application/postscript");
val = gtk_print_settings_get_duplex (settings) ;
if (val == GTK_PRINT_DUPLEX_HORIZONTAL)
papiAttributeListAddString (&attrs, PAPI_ATTR_EXCL, "Duplex", "DuplexNoTumble");
else if (val == GTK_PRINT_DUPLEX_VERTICAL)
papiAttributeListAddString (&attrs, PAPI_ATTR_EXCL, "Duplex", "DuplexTumble");
if (job->num_copies > 1)
{
papiAttributeListAddInteger (&attrs, PAPI_ATTR_EXCL, "copies", job->num_copies);
}
prtnm = strdup (gtk_printer_get_name (GTK_PRINTER(printer)));
if (papiServiceCreate (&(ps->service), prtnm, NULL, NULL, NULL,
PAPI_ENCRYPT_NEVER, NULL) != PAPI_OK)
return;
pstatus = papiJobStreamOpen (ps->service, prtnm, attrs, ticket, &(ps->stream));
if (pstatus != PAPI_OK)
{
papiServiceDestroy (ps->service);
ps->service = NULL;
return;
}
/* Everything set up fine, so get ready to wait for input data stream */
g_io_add_watch (data_io,
G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
(GIOFunc) papi_write,
ps);
}
static void
_papi_set_default_printer (GtkPrintBackendPapi *backend)
{
char *def_printer = NULL;
char *_default_attr[] = { "printer-name", NULL };
papi_service_t service = NULL;
papi_printer_t default_printer = NULL;
papi_attribute_t **attrs = NULL;
if (papiServiceCreate (&service, NULL, NULL, NULL, NULL, PAPI_ENCRYPT_NEVER,
NULL) != PAPI_OK)
return;
if (papiPrinterQuery (service, "_default", _default_attr, NULL,
&default_printer) == PAPI_OK)
{
if (default_printer != NULL)
{
attrs = papiPrinterGetAttributeList (default_printer);
if (attrs != NULL)
if (papiAttributeListGetString (attrs, NULL, "printer-name",
&def_printer) == PAPI_OK)
{
backend->default_printer = strdup (def_printer);
}
}
}
papiPrinterFree (default_printer);
papiServiceDestroy (service);
}
static void
gtk_print_backend_papi_init (GtkPrintBackendPapi *backend)
{
_papi_set_default_printer (backend);
}
static void
gtk_print_backend_papi_finalize (GObject *object)
{
GtkPrintBackendPapi *backend_papi;
GTK_NOTE (PRINTING,
g_print ("PAPI Backend: finalizing PAPI backend module\n"));
backend_papi = GTK_PRINT_BACKEND_PAPI (object);
g_free (backend_papi->default_printer);
backend_papi->default_printer = NULL;
backend_parent_class->finalize (object);
}
static void
gtk_print_backend_papi_dispose (GObject *object)
{
GtkPrintBackendPapi *backend_papi;
GTK_NOTE (PRINTING,
g_print ("PAPI Backend: %s\n", G_STRFUNC));
backend_papi = GTK_PRINT_BACKEND_PAPI (object);
backend_parent_class->dispose (object);
}
char **
get_all_list(papi_service_t svc)
{
papi_status_t status;
papi_printer_t printer = NULL;
char *attr[] = { "member-names", NULL };
char **names = NULL;
status = papiPrinterQuery(svc, "_all", attr, NULL, &printer);
if ((status == PAPI_OK) && (printer != NULL)) {
papi_attribute_t **attributes =
papiPrinterGetAttributeList(printer);
if (attributes != NULL) {
void *iter = NULL;
char *member = NULL;
for (status = papiAttributeListGetString(attributes,
&iter, "member-names", &member);
status == PAPI_OK;
status = papiAttributeListGetString(attributes,
&iter, NULL, &member))
list_append(&names, strdup(member));
}
papiPrinterFree(printer);
}
return (names);
}
static char **
get_printers_list(papi_service_t svc)
{
papi_status_t status;
papi_printer_t *printers = NULL;
char *keys[] = { "printer-name", "printer-uri-supported", NULL };