1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  * Copyright (C) 2016 Mopria Alliance, Inc.
4  * Copyright (C) 2013 Hewlett-Packard Development Company, L.P.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef __LIB_WPRINT_H__
20 #define __LIB_WPRINT_H__
21 
22 #include "wtypes.h"
23 #include "wprint_df_types.h"
24 #include "mime_types.h"
25 #include "printer_capabilities_types.h"
26 #include "wprint_status_types.h"
27 #include "ifc_wprint.h"
28 #include <dlfcn.h>
29 #include <ctype.h>
30 #include <stdlib.h>
31 
32 #define WPRINT_BAD_JOB_HANDLE ((wJob_t) ERROR)
33 
34 #define _INTERFACE_MAJOR_VERSION  1
35 #define _INTERFACE_MINOR_VERSION  0
36 
37 #define _PLUGIN_MAJOR_VERSION 1
38 
39 #define WPRINT_INTERFACE_VERSION ((uint32)((_INTERFACE_MAJOR_VERSION << 16) | \
40     (_INTERFACE_MINOR_VERSION & 0xffff)))
41 #define WPRINT_PLUGIN_VERSION(X) ((uint32)((_PLUGIN_MAJOR_VERSION << 16) | (X & 0xffff)))
42 
43 #define major_version(X) ((X >> 16) & 0xffff)
44 #define minor_version(X) ((X >> 0) & 0xffff)
45 
46 #define STRIPE_HEIGHT           (16)
47 #define BUFFERED_ROWS           (STRIPE_HEIGHT * 8)
48 
49 #define MAX_MIME_LENGTH         (64)
50 #define MAX_PRINTER_ADDR_LENGTH (64)
51 #define MAX_FILENAME_LENGTH     (32)
52 #define MAX_PATHNAME_LENGTH     (255)
53 #define MAX_ID_STRING_LENGTH    (64)
54 #define MAX_NAME_LENGTH         (255)
55 
56 #define HTTP_TIMEOUT_MILLIS 30000
57 
58 #ifdef __cplusplus
59 extern "C"
60 {
61 #endif
62 
63 typedef enum {
64     DUPLEX_DRY_TIME_NORMAL, // 18 seconds
65     DUPLEX_DRY_TIME_LOWER, // 10 seconds
66     DUPLEX_DRY_TIME_MINIMUM     // 5 seconds
67 } duplex_dry_time_t;
68 
69 typedef enum {
70     PORT_INVALID = -1,
71     PORT_FILE = 0,
72     PORT_IPP = 631,
73 } port_t;
74 
75 typedef enum {
76     PCLNONE,
77     PCLm,
78     PCLJPEG,
79     PCLPWG,
80 
81     PCL_NUM_TYPES
82 } pcl_t;
83 
84 typedef enum {
85     AUTO_ROTATE,
86     CENTER_VERTICAL,
87     CENTER_HORIZONTAL,
88     ROTATE_BACK_PAGE,
89     BACK_PAGE_PREROTATED,
90     AUTO_SCALE,
91     AUTO_FIT,
92     PORTRAIT_MODE,
93     LANDSCAPE_MODE,
94     CENTER_ON_ORIENTATION,
95     DOCUMENT_SCALING,
96 } render_flags_t;
97 
98 #define RENDER_FLAG_AUTO_ROTATE           (1 << AUTO_ROTATE)
99 #define RENDER_FLAG_ROTATE_BACK_PAGE      (1 << ROTATE_BACK_PAGE)
100 #define RENDER_FLAG_BACK_PAGE_PREROTATED  (1 << BACK_PAGE_PREROTATED)
101 #define RENDER_FLAG_CENTER_VERTICAL       (1 << CENTER_VERTICAL)
102 #define RENDER_FLAG_CENTER_HORIZONTAL     (1 << CENTER_HORIZONTAL)
103 #define RENDER_FLAG_AUTO_SCALE            (1 << AUTO_SCALE)
104 #define RENDER_FLAG_AUTO_FIT              (1 << AUTO_FIT)
105 #define RENDER_FLAG_PORTRAIT_MODE         (1 << PORTRAIT_MODE)
106 #define RENDER_FLAG_LANDSCAPE_MODE        (1 << LANDSCAPE_MODE)
107 #define RENDER_FLAG_CENTER_ON_ORIENTATION (1 << CENTER_ON_ORIENTATION)
108 #define RENDER_FLAG_DOCUMENT_SCALING      (1 << DOCUMENT_SCALING)
109 
110 #define AUTO_SCALE_RENDER_FLAGS          (RENDER_FLAG_AUTO_SCALE | \
111                                           RENDER_FLAG_AUTO_ROTATE | \
112                                           RENDER_FLAG_CENTER_VERTICAL | \
113                                           RENDER_FLAG_CENTER_HORIZONTAL)
114 
115 #define AUTO_FIT_RENDER_FLAGS            (RENDER_FLAG_AUTO_FIT | \
116                                           RENDER_FLAG_AUTO_ROTATE | \
117                                           RENDER_FLAG_CENTER_ON_ORIENTATION)
118 
119 #define ORIENTATION_RENDER_FLAGS         (RENDER_FLAG_AUTO_ROTATE | \
120                                           RENDER_FLAG_PORTRAIT_MODE | \
121                                           RENDER_FLAG_LANDSCAPE_MODE | \
122                                           RENDER_FLAG_CENTER_ON_ORIENTATION)
123 
124 typedef void (*wprint_status_cb_t)(wJob_t job_id, void *parm);
125 
126 /*
127  * Parameters describing a job request
128  */
129 typedef struct {
130     media_size_t media_size;
131     media_type_t media_type;
132     duplex_t duplex;
133     duplex_dry_time_t dry_time;
134     color_space_t color_space;
135     media_tray_t media_tray;
136     unsigned int num_copies;
137     unsigned int job_pages_per_set;
138     bool borderless;
139     unsigned int render_flags;
140     float job_top_margin;
141     float job_left_margin;
142     float job_right_margin;
143     float job_bottom_margin;
144     bool preserve_scaling;
145 
146     bool face_down_tray;
147 
148     // these values are pixels
149     unsigned int print_top_margin;
150     unsigned int print_left_margin;
151     unsigned int print_right_margin;
152     unsigned int print_bottom_margin;
153 
154     // these values are in pixels
155     unsigned int pixel_units;
156     unsigned int width;
157     unsigned int height;
158     unsigned int printable_area_width;
159     unsigned int printable_area_height;
160     unsigned int strip_height;
161 
162     bool cancelled;
163     bool last_page;
164     int page_num;
165     int copy_num;
166     int copy_page_num;
167     int page_corrupted;
168     bool page_printing;
169     bool page_backside;
170 
171     bool media_size_name;
172 
173     // these values are in inches
174     float page_width;
175     float page_height;
176     float page_top_margin;
177     float page_left_margin;
178     float page_right_margin;
179     float page_bottom_margin;
180 
181     const char *print_format;
182     char *page_range;
183     pcl_t pcl_type;
184     void *plugin_data;
185     bool ipp_1_0_supported;
186     bool ipp_2_0_supported;
187     bool epcl_ipp_supported;
188     bool accepts_pclm;
189     bool accepts_pdf;
190     bool copies_supported;
191     int print_quality;
192     const char *useragent;
193     char docCategory[10];
194     const char *media_default;
195     char print_scaling[MAX_PRINT_SCALING_LENGTH];
196 
197     // Expected certificate if any
198     uint8 *certificate;
199     int certificate_len;
200 
201     // IPP max job-name is 2**31 - 1, we set a shorter limit
202     char job_name[MAX_ID_STRING_LENGTH + 1];
203     char job_originating_user_name[MAX_NAME_LENGTH + 1];
204     int pdf_render_resolution;
205     bool accepts_app_name;
206     bool accepts_app_version;
207     bool accepts_os_name;
208     bool accepts_os_version;
209 
210     float source_width;
211     float source_height;
212 } wprint_job_params_t;
213 
214 typedef struct wprint_connect_info_st wprint_connect_info_t;
215 
216 /*
217  * Parameters defining how to reach a remote printing service
218  */
219 struct wprint_connect_info_st {
220     const char *printer_addr;
221     const char *uri_path;
222     const char *uri_scheme;
223     int port_num;
224     /* Timeout per retry in milliseconds */
225     long timeout;
226     /* Return non-0 if the received certificate is not acceptable. */
227     int (*validate_certificate)(struct wprint_connect_info_st *connect_info,
228                                 uint8 *data, int data_len);
229     /* User-supplied data. */
230     void *user;
231     const char *requesting_user_name;
232 };
233 
234 /*
235  * Current state of a queued job
236  */
237 typedef enum {
238     JOB_QUEUED = 1,
239     JOB_RUNNING,
240     JOB_BLOCKED,
241     JOB_DONE
242 } wprint_job_state_t;
243 
244 typedef enum
245 {
246     WPRINT_CB_PARAM_JOB_STATE
247 } wprint_cb_param_id_t;
248 
249 typedef struct
250 {
251    int                 page_num;
252    int                 copy_num;
253    int                 corrupted_file;
254    int                 current_page;
255    int                 total_pages;
256    int                 page_total_update;
257 } wprint_page_info_t;
258 
259 typedef struct {
260     wprint_cb_param_id_t id;
261     union {
262         wprint_job_state_t state;
263         wprint_page_info_t page_info;
264     } param;
265     unsigned int blocked_reasons;
266     int job_done_result;
267     // Certificate received from printer, if any
268     uint8 *certificate;
269     int certificate_len;
270 } wprint_job_callback_params_t;
271 
272 typedef enum {
273     PRIORITY_PASSTHRU = 1,
274     PRIORITY_LOCAL,
275 } wprint_priority_t;
276 
277 /* Forward declaration (actual definition in ifc_print_job.h) */
278 struct ifc_print_job_st;
279 
280 /*
281  * Defines an interface for delivering print jobs
282  */
283 typedef struct {
284     uint32 version;
285     wprint_priority_t priority;
286 
287     char const **(*get_mime_types)(void);
288 
289     char const **(*get_print_formats)(void);
290 
291     status_t (*start_job)(wJob_t job_handle, const ifc_wprint_t *wprint_ifc,
292             const struct ifc_print_job_st *job_ifc, wprint_job_params_t *job_params);
293 
294     status_t (*print_page)(wprint_job_params_t *job_params, const char *mime_type,
295             const char *pathname);
296 
297     status_t (*print_blank_page)(wJob_t job_handle, wprint_job_params_t *job_params,
298             const char *mime_type, const char *pathname);
299 
300     status_t (*end_job)(wprint_job_params_t *job_params);
301 } wprint_plugin_t;
302 
303 /*
304  * Initialize the wprint system. Identify and gather capabilities of available plug-ins.
305  * Returns the number of plugins found or ERROR.
306  */
307 int wprintInit(void);
308 
309 /*
310  * Call to test if wprint is running or has been shut down.
311  */
312 bool wprintIsRunning();
313 
314 /*
315  * Gets the capabilities of the specified printer.
316  */
317 status_t wprintGetCapabilities(const wprint_connect_info_t *connect_info,
318         printer_capabilities_t *printer_cap);
319 
320 /*
321  * Returns a preferred print format supported by the printer
322  */
323 char *_get_print_format(const char *mime_type, const wprint_job_params_t *job_params,
324                         const printer_capabilities_t *cap);
325 
326 /*
327  * Fills in the job params structure with default values.
328  */
329 status_t wprintGetDefaultJobParams(wprint_job_params_t *job_params);
330 
331 /*
332  * Fills in the job params structure with values in accordance with printer capabilities.
333  */
334 status_t wprintGetFinalJobParams(wprint_job_params_t *job_param,
335         const printer_capabilities_t *printer_cap);
336 
337 /*
338  * Called once per job at the start of the job. Returns a print job handle that is used in
339  * other functions of this library. Returns WPRINT_BAD_JOB_HANDLE for errors.
340  */
341 wJob_t wprintStartJob(const char *printer_addr, port_t port_num,
342         const wprint_job_params_t *job_params, const printer_capabilities_t *printer_cap,
343         const char *mime_type, const char *pathname, wprint_status_cb_t cb_fn,
344         const char *debugDir, const char *scheme);
345 
346 /*
347  * Sent once per job at the end of the job. A current print job must end for the next one
348  * to start.
349  */
350 status_t wprintEndJob(wJob_t job_handle);
351 
352 /*
353  * Sent once per page of a multi-page job to deliver a page image in a previously
354  * specified MIME type. The page_number must increment from 1. last_page flag is true if it
355  * is the last page of the job.
356  *
357  * top/left/right/bottom margin are the incremental per page margins in pixels
358  * at the current print resolution that are added on top of the physical page
359  * page margins, passing in 0 results in the default page margins being used.
360  */
361 status_t wprintPage(wJob_t job_handle, int page_number, const char *filename, bool last_page,
362         bool pdf_page, unsigned int top_margin, unsigned int left_margin,
363         unsigned int right_margin, unsigned int bottom_margin);
364 
365 /*
366  * Cancels a spooled or running job. Returns OK or ERROR
367  */
368 status_t wprintCancelJob(wJob_t job_handle);
369 
370 /*
371  * Exits the print subsystem
372  */
373 status_t wprintExit(void);
374 
375 /*
376  * Supplies info about the sending application and OS name
377  */
378 void wprintSetSourceInfo(const char *appName, const char *appVersion, const char *osName);
379 
380 /*
381  * Returns true, if a blank page to be printed in duplex print for PCLm
382  */
383 bool wprintBlankPageForPclm(const wprint_job_params_t *job_params,
384         const printer_capabilities_t *printer_cap);
385 
386 /*
387  * Returns true, if a blank page to be printed in duplex print for PWG
388  */
389 bool wprintBlankPageForPwg(const wprint_job_params_t *job_params,
390         const printer_capabilities_t *printer_cap);
391 
392 /* Global variables to hold API, application, and OS details */
393 extern int g_API_version;
394 extern char g_osName[MAX_ID_STRING_LENGTH + 1];
395 extern char g_appName[MAX_ID_STRING_LENGTH + 1];
396 extern char g_appVersion[MAX_ID_STRING_LENGTH + 1];
397 
398 #ifdef __cplusplus
399 }
400 #endif
401 
402 #endif // __LIB_WPRINT_H__