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 #include <stdlib.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include "ifc_print_job.h"
25 #include "wprint_debug.h"
26 
27 #define TAG "plugin_pdf"
28 #define BUFF_SIZE 8192
29 
30 extern int g_API_version;
31 
32 typedef struct {
33     const ifc_wprint_t *wprint_ifc;
34     const ifc_print_job_t *print_ifc;
35 } plugin_data_t;
36 
37 static const char *_mime_types[] = {
38         MIME_TYPE_PDF,
39         NULL};
40 
41 static const char *_print_formats[] = {
42         PRINT_FORMAT_PDF,
43         NULL};
44 
_get_mime_types(void)45 static const char **_get_mime_types(void) {
46     return _mime_types;
47 }
48 
_get_print_formats(void)49 static const char **_get_print_formats(void) {
50     return _print_formats;
51 }
52 
_start_job(wJob_t job_handle,const ifc_wprint_t * wprint_ifc_p,const ifc_print_job_t * print_job_ifc_p,wprint_job_params_t * job_params)53 static int _start_job(wJob_t job_handle, const ifc_wprint_t *wprint_ifc_p,
54         const ifc_print_job_t *print_job_ifc_p,
55         wprint_job_params_t *job_params) {
56     plugin_data_t *priv;
57     if (job_params == NULL) return ERROR;
58 
59     job_params->plugin_data = NULL;
60     if ((wprint_ifc_p == NULL) || (print_job_ifc_p == NULL)) return ERROR;
61 
62     priv = (plugin_data_t *) malloc(sizeof(plugin_data_t));
63     priv->wprint_ifc = (ifc_wprint_t *) wprint_ifc_p;
64     priv->print_ifc = (ifc_print_job_t *) print_job_ifc_p;
65 
66     job_params->plugin_data = (void *) priv;
67     return OK;
68 }
69 
_print_page(wprint_job_params_t * job_params,const char * mime_type,const char * pathname)70 static int _print_page(wprint_job_params_t *job_params, const char *mime_type,
71         const char *pathname) {
72     plugin_data_t *priv;
73     int fd;
74     int result = OK;
75     int rbytes, wbytes, nbytes = 0;
76     char *buff;
77 
78     if (job_params == NULL) return ERROR;
79 
80     priv = (plugin_data_t *) job_params->plugin_data;
81 
82     if (priv == NULL) return ERROR;
83 
84     //  open the PDF file and dump it to the socket
85     if (pathname && strlen(pathname)) {
86         buff = malloc(BUFF_SIZE);
87         if (buff == NULL) {
88             return ERROR;
89         }
90 
91         fd = open(pathname, O_RDONLY);
92         if (fd != ERROR) {
93             rbytes = read(fd, buff, BUFF_SIZE);
94 
95             while ((rbytes > 0) && !job_params->cancelled) {
96                 wbytes = priv->print_ifc->send_data(priv->print_ifc, buff, rbytes);
97                 if (wbytes == rbytes) {
98                     nbytes += wbytes;
99                     rbytes = read(fd, buff, BUFF_SIZE);
100                 } else {
101                     LOGE("ERROR: write() failed, %s", strerror(errno));
102                     result = ERROR;
103                     break;
104                 }
105             }
106             LOGI("dumped %d bytes of %s to printer", nbytes, pathname);
107             close(fd);
108         }
109 
110         free(buff);
111     }
112     if ((job_params->page_range != NULL) && (strcmp(job_params->page_range, "") != 0)) {
113         remove(pathname);
114     }
115     return result;
116 }
117 
_end_job(wprint_job_params_t * job_params)118 static int _end_job(wprint_job_params_t *job_params) {
119     if (job_params != NULL) {
120         if (job_params->plugin_data != NULL) {
121             free(job_params->plugin_data);
122         }
123     }
124     return OK;
125 }
126 
libwprintplugin_pdf_reg(void)127 wprint_plugin_t *libwprintplugin_pdf_reg(void) {
128     static const wprint_plugin_t _pdf_plugin = {.version = WPRINT_PLUGIN_VERSION(0),
129             .priority = PRIORITY_PASSTHRU, .get_mime_types = _get_mime_types,
130             .get_print_formats = _get_print_formats, .start_job = _start_job,
131             .print_page = _print_page, .print_blank_page = NULL, .end_job = _end_job,};
132     return ((wprint_plugin_t *) &_pdf_plugin);
133 }