Complete Example Code

Here is the complete code for the two examples used in the guide. I normally write the C code in comments before the Pyrex code so I can check I have all the arguments correct, hence the large comments before each declaration.

CUPS

The Entire pyxcups.pyx Code

Here, for your editing pleasure is the entire wrapper for the CUPS cupsGetDests function call.

cdef extern from "cups/cups.h":
    # typedef struct                 /**** Printer Options ****/
    # {
    #   char          *name;         /* Name of option */
    #   char          *value;        /* Value of option */
    # } cups_option_t;
    ctypedef struct cups_option_t:
        char *name
        char *value
    # typedef struct               /**** Destination ****/
    # {
    #   char          *name,       /* Printer or class name */
    #                 *instance;   /* Local instance name or */
    #                              /*   NULL */
    #   int           is_default;  /* Is this printer the */
    #                              /*   default? */
    #   int           num_options; /* Number of options */
    #   cups_option_t *options;    /* Options */
    # } cups_dest_t;
    ctypedef struct cups_dest_t:
        char          *name
        char          *instance
        int           is_default
        int           num_options
        cups_option_t *options
    # extern int              cupsGetDests(cups_dest_t **dests);
    int cupsGetDests(cups_dest_t **dests)
    # extern int              cupsPrintFile(const char *printer,
    #                                       const char *filename,
    #                                       const char *title,
    #                                       int num_options,
    #                                       cups_option_t *options);
    ctypedef cups_option_t
    int cupsPrintFile(char *printer, char *filename,
                      char *title, int num_options,
                      cups_option_t *options)
def get_dests():
    cdef cups_dest_t *dests
    cdef cups_dest_t currDest
    numDests = cupsGetDests(&dests)
    retval = []
    for i in range(numDests):
        currDest = dests[i]
        retval.append(currDest.name)
    return retval
def print_file(printer, filename, title):
    jobID = cupsPrintFile(printer, filename, title,
                          0, NULL)
    return jobID

Example Python Code for pyxcups.pyx

import pyxcups
for printer in pyxcups.get_dests():
    print printer
      

XOSD

The Entire pyxxosd.pyx Code

cdef extern from "xosd.h":
    # typedef enum
    #    {
    #    XOSD_top,
    #    XOSD_bottom,
    #    XOSD_middle
    #    } xosd_pos;
    ctypedef enum xosd_pos:
        XOSD_top
        XOSD_bottom
        XOSD_middle
    # typedef enum
    #    {
    #    XOSD_percentage,
    #    XOSD_string,
    #    XOSD_printf,
    #    XOSD_slider
    #    } xosd_command;
    ctypedef enum xosd_command:
       XOSD_percentage
       XOSD_string
       XOSD_printf
       XOSD_slider
    # typedef struct xosd xosd;
    ctypedef struct xosd
    # xosd *xosd_create (int number_lines);
    xosd *xosd_create(int number_lines)
    # int xosd_destroy (xosd *osd);
    int xosd_destroy (xosd *osd)
    # int xosd_display (xosd *osd, int line, 
    #                   xosd_command command, ...);
    int xosd_display(xosd *osd, int line,
                     xosd_command command, 
                     ...)
cdef class XOSD:
    cdef xosd *xosdWin
    def __new__(self, font, colour):
        self.xosdWin = xosd_create(1)
        xosd_set_font(self.xosdWin, font)
        xosd_set_colour(self.xosdWin, colour)
    def __dealloc__(self):
        xosd_destroy(self.xosdWin)
    def display_text(self, text):
        cdef char *cText
        cText = text
        xosd_display(self.xosdWin, 1, XOSD_string, cText)

Example Python Code for pyxosd.pyx

import pyxosd, time
x = pyxosd.XOSD('fixed', 'lawngreen')
x.display_text('You have been R00ted')
time.sleep(3)
x.display_text('by an 31373 HaX0r.')
time.sleep(5)