summaryrefslogtreecommitdiff
path: root/xplsprinters.c
blob: 5b63fa8b5fcf6eaa93d63f96244771f349bd9169 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
 * $Xorg: xlsprinters.c,v 1.1 2002/02/09 22:54:18 gisburn Exp $
 * 
 * xlsprinters - print information about Xprint printers and their attributes
 *
 * 
Copyright 2002-2004 Roland Mainz <roland.mainz@nrubsig.org>

All Rights Reserved.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
 *
 * Author:  Roland Mainz <roland.mainz@nrubsig.org>
 */

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XprintUtil/xprintutil.h>

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

/* Turn a NULL pointer string into an empty string */
#define NULLSTR(x) (((x)!=NULL)?(x):(""))

#define BOOL2STR(x) ((x)?("true"):("false"))

static const char *ProgramName;

static 
void usage(void)
{
    fprintf (stderr, "usage:  %s [options]\n", ProgramName);
    fprintf (stderr, "-printer printername\tprinter to use\n");
    fprintf (stderr, "-l\tlist detailed printer info\n");
    fprintf (stderr, "-dump\tdump all available printer attrbutes\n");
    fprintf (stderr, "\n");
    exit(EXIT_FAILURE);
}

static
void dumpAttributes( Display *pdpy, XPContext pcontext )
{
    char *s;
    printf("--> Job\n%s\n",         s=XpuGetJobAttributes(pdpy, pcontext));     XFree(s);
    printf("--> Doc\n%s\n",         s=XpuGetDocAttributes(pdpy, pcontext));     XFree(s);
    printf("--> Page\n%s\n",        s=XpuGetPageAttributes(pdpy, pcontext));    XFree(s);
    printf("--> Printer\n%s\n",     s=XpuGetPrinterAttributes(pdpy, pcontext)); XFree(s);
    printf("--> Server\n%s\n",      s=XpuGetServerAttributes(pdpy, pcontext));  XFree(s);
    printf("image resolution %d\n", (int)XpGetImageResolution(pdpy, pcontext));
}

static
void print_medium_sizes( Display *pdpy, XPContext pcontext )
{
    XpuMediumSourceSizeList list;
    int                     list_count;
    char                   *value;
    int                     i;

    value = XpGetOneAttribute(pdpy, pcontext, XPDocAttr, "default-medium");
    if( value )
    {
      printf("\tdefault-medium=%s\n", NULLSTR(value));
      XFree(value);
    }
    value = XpGetOneAttribute(pdpy, pcontext, XPDocAttr, "default-input-tray");
    if( value )
    {
      printf("\tdefault-input-tray=%s\n", NULLSTR(value));
      XFree(value); 
    }

    list = XpuGetMediumSourceSizeList(pdpy, pcontext, &list_count);
    if( !list )
    {
      fprintf(stderr, "XpuGetMediumSourceSizeList returned NULL\n");
      return;
    }

    for( i = 0 ; i < list_count ; i++ )
    {
      XpuMediumSourceSizeRec *curr = &list[i];
      if( curr->tray_name )
      {
        printf("\tmedium-source-sizes-supported=%s/%s %s %g %g %g %g\n", 
               curr->tray_name, curr->medium_name, BOOL2STR(curr->mbool),
               curr->ma1, curr->ma2, curr->ma3, curr->ma4);
      }
      else
      {
        printf("\tmedium-source-sizes-supported=%s %s %g %g %g %g\n", 
               curr->medium_name, BOOL2STR(curr->mbool), 
               curr->ma1, curr->ma2, curr->ma3, curr->ma4);
      }
    }
  
    XpuFreeMediumSourceSizeList(list);
}


static
void print_resolutions( Display *pdpy, XPContext pcontext )
{
    XpuResolutionList list;
    int               list_count;
    int               i;
    char              *defresname; /* name of default resolution */

    list = XpuGetResolutionList(pdpy, pcontext, &list_count);
    if( !list )
    {
      fprintf(stderr, "XpuGetResolutionList returned NULL\n");
      return;
    }

    defresname = XpGetOneAttribute(pdpy, pcontext, XPDocAttr, "default-printer-resolution");
    if( defresname )
    {
      XpuResolutionRec *res = XpuFindResolutionByName(list, list_count, defresname);
      if( res )
      {
        printf("\tdefault-printer-resolution=%s (%ldx%ld)\n", res->name, res->x_dpi, res->y_dpi);
      }
      else
      {
        fprintf(stderr, "XpuFindResolutionByName() returned no match for default resolution '%s'\n",
                defresname);
      }
      XFree(defresname);
    }

    for( i = 0 ; i < list_count ; i++ )
    {
      XpuResolutionRec *curr = &list[i];
      printf("\tresolution=%s (%ldx%ld)\n", curr->name, curr->x_dpi, curr->y_dpi);
    }
  
    XpuFreeResolutionList(list);
}

static
void print_orientations( Display *pdpy, XPContext pcontext )
{
    char               *default_orientation;
    XpuOrientationList  list;
    int                 list_count;
    int                 i;

    default_orientation = XpGetOneAttribute(pdpy, pcontext, XPDocAttr, "content-orientation"); 
    if( default_orientation )
    {
      printf("\tdefault_orientation=%s\n", default_orientation);
      XFree(default_orientation);
    }

    list = XpuGetOrientationList(pdpy, pcontext, &list_count);
    if( !list || list_count == 0 )
    {
      fprintf(stderr, "XpuGetOrientationList returned NULL\n");
      return;
    }

    for( i = 0 ; i < list_count ; i++ )
    {
      XpuOrientationRec *curr = &list[i];
      printf("\torientation=%s\n", curr->orientation);
    }
  
    XpuFreeOrientationList(list);
}

static
void print_plexes( Display *pdpy, XPContext pcontext )
{
    char        *default_plex;
    XpuPlexList  list;
    int          list_count;
    int          i;

    default_plex = XpGetOneAttribute(pdpy, pcontext, XPDocAttr, "plex");
    if( default_plex )
    {
      printf("\tdefault_plex=%s\n", default_plex);
      XFree(default_plex);
    }

    list = XpuGetPlexList(pdpy, pcontext, &list_count);
    if( !list || list_count == 0 )
    {
      fprintf(stderr, "XpGetOneAttribute returned NULL\n");
      return;
    }

    for( i = 0 ; i < list_count ; i++ )
    {
      XpuPlexRec *curr = &list[i];
      printf("\tplex=%s\n", curr->plex);
    }
  
    XpuFreePlexList(list);
}

static
void print_detailed_printer_info(XPPrinterRec *xp_rec, int detailLevel)
{   
    Display    *pdpy;     /* X connection */
    XPContext   pcontext; /* Xprint context */

    if( detailLevel < 2 )
      return;

    if( XpuGetPrinter(xp_rec->name, &pdpy, &pcontext) != 1 )
    {
      fprintf(stderr, "Cannot open printer '%s'\n", xp_rec->name);
      return;
    }
    
    printf("printer: %s\n", xp_rec->name);
    printf("\tdescription=%s\n", NULLSTR(xp_rec->desc));
    printf("\tmodel-identifier=%s\n", NULLSTR(XpGetOneAttribute(pdpy, pcontext, XPPrinterAttr, "xp-model-identifier")));
  
    print_medium_sizes(pdpy, pcontext);
    print_resolutions(pdpy, pcontext);
    print_orientations(pdpy, pcontext);
    print_plexes(pdpy, pcontext);
    
    if (detailLevel > 100)
      dumpAttributes(pdpy, pcontext);
     
    XpuClosePrinterDisplay(pdpy, pcontext);
}

static
void print_printer_info(XPPrinterRec *xp_rec, int detailLevel)
{   
    printf("printer: %s\n", xp_rec->name);
    
    if( detailLevel < 1 )
      return;
      
    printf("\tdescription=%s\n", NULLSTR(xp_rec->desc));
}

int main (int argc, char *argv[])
{
    char *printername = NULL;  /* printer to query */
    int   details     = 0;
    Bool  use_threadsafe_api = False; /* Use threadsafe API (for debugging) */
    int i;                     /* temp variable:  iterator */
    XPPrinterList plist;       /* list of printers */
    int           plist_count; /* number of entries in |plist|-array */
    

    ProgramName = argv[0];

    for( i = 1 ; i < argc ; i++ )
    {
      char *arg = argv[i];
      int   len = strlen(arg);
    
      if( !strncmp("-printer", arg, len) )
      {
        if (++i >= argc)
          usage ();
        printername = argv[i];
      } 
      else if( !strncmp("-d", arg, len) )
      {
        details = 1;
      }
      else if( !strncmp("-l", arg, len) )
      {
        details = 2;
      }
      else if( !strncmp("-dump", arg, len) )
      {
        details = 255;
      }
      else if( !strncmp("-debug_use_threadsafe_api", arg, len) )
      {
        use_threadsafe_api = True;
      }
      else
      {
        usage();
      }  
    }

    if( use_threadsafe_api )
    {
      if( !XInitThreads() )
      {
        fprintf(stderr, "%s: XInitThreads() failure.\n", ProgramName);
        exit(EXIT_FAILURE);
      }
    }
    
    plist = XpuGetPrinterList(printername, &plist_count);

    if (!plist) {
      fprintf(stderr, "%s:  no printers found for printer spec \"%s\".\n",
         ProgramName, NULLSTR(printername));
      exit(EXIT_FAILURE);
    }

    for (i = 0; i < plist_count; i++)
    {
      if( details < 2)
        print_printer_info(&plist[i], details);
      else
        print_detailed_printer_info(&plist[i], details);
    }
    
    XpuFreePrinterList(plist);

    return(EXIT_SUCCESS);
}