On 12/4/20 7:47 PM, ToddAndMargo via perl6-users wrote:
Hi All,

Fedora 33

I there a way to use what is on

https://docs.raku.org/type/Semaphore

to print out a list of my available printers?

Many thanks,
-T


Hi All,

With the help of Curt on Stack overflow and a few
helpful hints from JJ here and also on stack overflow,
I got a program that Curt gave me here to work:

<ListPrinters.pl6>
#!/usr/bin/env raku

#`{

https://www.cups.org/doc/cupspm.html

cupsGetDestMediaByName

Get media names, dimensions, and margins.

int cupsGetDestMediaByName(http_t *http, cups_dest_t *dest, cups_dinfo_t *dinfo, const char *media, unsigned flags, cups_size_t *size);
Parameters
http    Connection to destination
dest    Destination
dinfo   Destination information
media   Media name
flags   Media matching flags
size    Media size information
Return Value

1 on match, 0 on failure
Discussion

The "media" string is a PWG media name. "Flags" provides some matching guidance (multiple flags can be combined):

CUPS_MEDIA_FLAGS_DEFAULT = find the closest size supported by the printer, CUPS_MEDIA_FLAGS_BORDERLESS = find a borderless size, CUPS_MEDIA_FLAGS_DUPLEX = find a size compatible with 2-sided printing, CUPS_MEDIA_FLAGS_EXACT = find an exact match for the size, and CUPS_MEDIA_FLAGS_READY = if the printer supports media sensing, find the size amongst the "ready" media.

The matching result (if any) is returned in the "cups_size_t" structure.

Returns 1 when there is a match and 0 if there is not a match.


The C way:

     #include <iostream>
     #include <cups/cups.h>

     int main() {
        cups_dest_t* dests;
        int nCount = cupsGetDests2(CUPS_HTTP_DEFAULT, &dests);

        for (int i = 0; i < nCount; i++) {
           cups_dest_t dest = dests[i];
           std::cout << dest.name << std::endl;
        }
     }


https://stackoverflow.com/questions/65162829/raku-and-cupsgetdests


You've already got the symlink to the plain .so, but you can also just change is native('cups') to is native('cups', v2) to make it use the .so.2 library.

Fill in the whole Struct:

class CupsDest is repr('CStruct') {
    has Str $.name;
    has Str $.instance;
    has int32 $.is-default;
    has Pointer $.options;
}

and change this line:

my $dest = nativecast(CupsDest, Pointer.new($ptr + $i * nativesizeof(CupsDest)));



}


use NativeCall;

class CupsDest is repr('CStruct') {
    has Str $.name;
    has Str $.instance;
    has int32 $.is-default;
    has Pointer $.options;
}

sub cupsGetDests(Pointer is rw --> int32) is native('cups', v2) {}

my $ptr = Pointer.new;
my $nCount = cupsGetDests($ptr);

for ^$nCount -> $i {
my $dest = nativecast(CupsDest, Pointer.new($ptr + $i * nativesizeof(CupsDest)));
    print "<" ~ $dest.name ~ ">\n";
}

</ListPrinters.pl6>



$ ListPrinters.pl6
<B4350>
<Cups-PDF>
<Cups_PDF_rn6>
<Oki_B4350_on_dev_lp0_rn6>
<Virtual_PDF_Printer>


Thank you all for the tips!

-T

Reply via email to