/*  printer.c
 *
 *  Created on: Jan 4, 2011
 *  Author: Jan Axelson
 *
 * Demonstrates printing a file to the default printer.
 * Requires a file to print: "test.pdf" (can be any PDF).
 * Uses the CUPS API.
 * Compile with the -lcups option.
 * This file is at www.Lvr.com/beagleboard.htm
 */

#include <cups/cups.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	// The file to print.

	char *file_to_print = "test.pdf";

	cups_dest_t *dest;
	cups_dest_t *dests;
	int jobid;
	int num_dests;
	const char *value;

	// Get a list of available printer destinations.

	num_dests = cupsGetDests(&dests);
	printf("num_dests = %d\n", num_dests);

	// Get the default printer.

	dest = cupsGetDest(NULL, NULL, num_dests, dests);

	// You can also specify a printer to use.
	// The printer names are in /etc/cups/printers.conf
	//dest = cupsGetDest("Deskjet-460", NULL, num_dests, dests);

	if (dest)
	{
		// The destination exists, but is the printer attached and available?

		printf("Default printer: %s\n", dest->name);

		value = cupsGetOption("printer-state", dest->num_options, dest->options);

		if (strtol(value, NULL, 10) < IPP_PRINTER_STOPPED)
		{
			// The printer is attached and available. Print the file.

			jobid = cupsPrintFile(
					dest->name,
					file_to_print,
					"Test Job",
					dest->num_options,
					dest->options);
			printf("status = %s\n", cupsLastErrorString());
		}
		else
		{
			printf("Printer is stopped. Check printer.\n");
		}
	}
	else
	{
		  printf ("No default printer.\n");
	}

	// Finished using the printer.

	cupsFreeDests(num_dests, dests);
	return 0;
}


