Giter Site home page Giter Site logo

cups-sys's Introduction

cups-sys Build Status

Rust FFI bindings to CUPS.

Background

CUPS is the standards-based, open source printing system developed by Apple Inc. for macOS and other UNIX-like operating systems. CUPS uses the Internet Printing Protocol (IPP) to support printing to local and network printers.

This library (cups-sys) provides a low-level interface to the CUPS library installed on your system. The binding is generated at build time via the bindgen project.

I just want to print from Rust

use std::mem;
use std::ptr;
use cups-sys::*;

unsafe {
  let mut dests: *mut cups_dest_t = mem::zeroed();
  let num_dests = cupsGetDests(&mut dests as *mut _);
  // Get the default printer.
  let destination: cups_dest_t = cupsGetDest(ptr::null(), ptr::null(), num_dests, dests);
  // Print a real page.
  let job_id: i32 = cupsPrintFile(
      (*destination).name,
      // File to print.
      CString::new("/path/to/file")
          .unwrap()
          .as_ptr(),
      // Name of the print job.
      CString::new("Test print job")
          .unwrap()
          .as_ptr(),
      (*destination).num_options,
      (*destination).options
  );
  println!("{}", job_id);
  cupsFreeDests(num_dests, dests);
}

For a pure-Rust IPP implementation, check out ipp.rs.

Documentation

The auto-generated FFI reference docs can be found at https://legneato.github.io/cups-sys/cups_sys/.

The original CUPS API documentation (with examples) can be found at https://www.cups.org/doc/api-cups.html.

Example usage

unsafe {
    let mut dests: *mut cups_dest_t = mem::zeroed();
    let num_dests = cupsGetDests(&mut dests as *mut _);
    let destinations = std::slice::from_raw_parts(dests, num_dests as usize);

    for destination in destinations {
        let c_printer_name = CStr::from_ptr((*destination).name);
        let printer_name = c_printer_name.to_string_lossy();

        let c_make_and_model = cupsGetOption(
            CString::new("printer-make-and-model").unwrap().as_ptr(),
            destination.num_options,
            destination.options
        );
        let make_and_model = CStr::from_ptr(c_make_and_model).to_string_lossy();
        println!("{} ({})", printer_name, make_and_model);
    }

    cupsFreeDests(num_dests, dests);
}

License

cups-sys is licensed under either of the following, at your option:

cups-sys's People

Contributors

charles-schleich avatar legneato avatar redrield avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cups-sys's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.