/[rdesktop]/jpeg/rdesktop/trunk/printer.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /jpeg/rdesktop/trunk/printer.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 432 by matthewc, Tue Jul 1 09:31:25 2003 UTC revision 1365 by jsorg71, Thu Jan 4 05:39:39 2007 UTC
# Line 1  Line 1 
1    /* -*- c-basic-offset: 8 -*-
2       rdesktop: A Remote Desktop Protocol client.
3       Copyright (C) Matthew Chapman 1999-2007
4    
5       This program is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation; either version 2 of the License, or
8       (at your option) any later version.
9    
10       This program is distributed in the hope that it will be useful,
11       but WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13       GNU General Public License for more details.
14    
15       You should have received a copy of the GNU General Public License
16       along with this program; if not, write to the Free Software
17       Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18    */
19    
20  #include "rdesktop.h"  #include "rdesktop.h"
21    
22  FILE *printer_fp;  extern RDPDR_DEVICE g_rdpdr_device[];
23    
24  static NTSTATUS  static PRINTER *
25  printer_create(HANDLE *handle)  get_printer_data(RD_NTHANDLE handle)
26  {  {
27          printer_fp = popen("lpr", "w");          int index;
28          *handle = 0;  
29          return STATUS_SUCCESS;          for (index = 0; index < RDPDR_MAX_DEVICES; index++)
30            {
31                    if (handle == g_rdpdr_device[index].handle)
32                            return (PRINTER *) g_rdpdr_device[index].pdevice_data;
33            }
34            return NULL;
35  }  }
36    
37  static NTSTATUS  int
38  printer_close(HANDLE handle)  printer_enum_devices(uint32 * id, char *optarg)
39  {  {
40          pclose(printer_fp);          PRINTER *pprinter_data;
41          return STATUS_SUCCESS;  
42            char *pos = optarg;
43            char *pos2;
44            int count = 0;
45            int already = 0;
46    
47            /* we need to know how many printers we've already set up
48               supplied from other -r flags than this one. */
49            while (count < *id)
50            {
51                    if (g_rdpdr_device[count].device_type == DEVICE_TYPE_PRINTER)
52                            already++;
53                    count++;
54            }
55    
56            count = 0;
57    
58            if (*optarg == ':')
59                    optarg++;
60    
61            while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES)
62            {
63                    pprinter_data = (PRINTER *) xmalloc(sizeof(PRINTER));
64    
65                    strcpy(g_rdpdr_device[*id].name, "PRN");
66                    strcat(g_rdpdr_device[*id].name, l_to_a(already + count + 1, 10));
67    
68                    /* first printer is set as default printer */
69                    if ((already + count) == 0)
70                            pprinter_data->default_printer = True;
71                    else
72                            pprinter_data->default_printer = False;
73    
74                    pos2 = next_arg(optarg, '=');
75                    if (*optarg == (char) 0x00)
76                            pprinter_data->printer = "mydeskjet";   /* set default */
77                    else
78                    {
79                            pprinter_data->printer = xmalloc(strlen(optarg) + 1);
80                            strcpy(pprinter_data->printer, optarg);
81                    }
82    
83                    if (!pos2 || (*pos2 == (char) 0x00))
84                            pprinter_data->driver = "HP Color LaserJet 8500 PS";    /* no printer driver supplied set default */
85                    else
86                    {
87                            pprinter_data->driver = xmalloc(strlen(pos2) + 1);
88                            strcpy(pprinter_data->driver, pos2);
89                    }
90    
91                    printf("PRINTER %s to %s driver %s\n", g_rdpdr_device[*id].name,
92                           pprinter_data->printer, pprinter_data->driver);
93                    g_rdpdr_device[*id].device_type = DEVICE_TYPE_PRINTER;
94                    g_rdpdr_device[*id].pdevice_data = (void *) pprinter_data;
95                    count++;
96                    (*id)++;
97    
98                    optarg = pos;
99            }
100            return count;
101  }  }
102    
103  static NTSTATUS  static RD_NTSTATUS
104  printer_write(HANDLE handle, uint8 *data, uint32 length, uint32 *result)  printer_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition, uint32 flags,
105                   char *filename, RD_NTHANDLE * handle)
106  {  {
107          *result = fwrite(data, 1, length, printer_fp);          char cmd[256];
108          return STATUS_SUCCESS;          PRINTER *pprinter_data;
109    
110            pprinter_data = (PRINTER *) g_rdpdr_device[device_id].pdevice_data;
111    
112            /* default printer name use default printer queue as well in unix */
113            if (pprinter_data->printer == "mydeskjet")
114            {
115                    pprinter_data->printer_fp = popen("lpr", "w");
116            }
117            else
118            {
119                    sprintf(cmd, "lpr -P %s", pprinter_data->printer);
120                    pprinter_data->printer_fp = popen(cmd, "w");
121            }
122    
123            g_rdpdr_device[device_id].handle = fileno(pprinter_data->printer_fp);
124            *handle = g_rdpdr_device[device_id].handle;
125            return RD_STATUS_SUCCESS;
126  }  }
127    
128  DEVICE_FNS printer_fns =  static RD_NTSTATUS
129    printer_close(RD_NTHANDLE handle)
130  {  {
131            int i = get_device_index(handle);
132            if (i >= 0)
133            {
134                    PRINTER *pprinter_data = g_rdpdr_device[i].pdevice_data;
135                    if (pprinter_data)
136                            pclose(pprinter_data->printer_fp);
137                    g_rdpdr_device[i].handle = 0;
138            }
139            return RD_STATUS_SUCCESS;
140    }
141    
142    static RD_NTSTATUS
143    printer_write(RD_NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
144    {
145            PRINTER *pprinter_data;
146    
147            pprinter_data = get_printer_data(handle);
148            *result = length * fwrite(data, length, 1, pprinter_data->printer_fp);
149    
150            if (ferror(pprinter_data->printer_fp))
151            {
152                    *result = 0;
153                    return RD_STATUS_INVALID_HANDLE;
154            }
155            return RD_STATUS_SUCCESS;
156    }
157    
158    DEVICE_FNS printer_fns = {
159          printer_create,          printer_create,
160          printer_close,          printer_close,
161          NULL, /* read */          NULL,                   /* read */
162          printer_write,          printer_write,
163          NULL /* device_control */          NULL                    /* device_control */
164  };  };
   

Legend:
Removed from v.432  
changed lines
  Added in v.1365

  ViewVC Help
Powered by ViewVC 1.1.26