Fixed CloseHandle() on global handles.
[wine.git] / misc / spooler.c
blobc17671b41f2e6368e251e28bd2db3eab27c810d1
1 /*
2 * Print spooler and PQ functions
4 * Copyright 1996 John Harvey
5 * 1998 Huw Davies
7 */
9 #include <ctype.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include "callback.h"
16 #include "dc.h"
17 #include "debug.h"
18 #include "gdi.h"
19 #include "options.h"
20 #include "windows.h"
21 #include "winerror.h"
22 #include "xmalloc.h"
24 /**********************************************************************
25 * QueryAbort (GDI.155)
27 * Calls the app's AbortProc function if avail.
29 * RETURNS
30 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
31 * FALSE if AbortProc wants to abort printing.
33 BOOL16 WINAPI QueryAbort(HDC16 hdc, INT16 reserved)
35 DC *dc = DC_GetDCPtr( hdc );
37 if ((!dc) || (!dc->w.lpfnPrint))
38 return TRUE;
39 return Callbacks->CallDrvAbortProc(dc->w.lpfnPrint, hdc, 0);
42 /**********************************************************************
43 * SetAbortProc16 (GDI.381)
46 INT16 WINAPI SetAbortProc16(HDC16 hdc, SEGPTR abrtprc)
48 return Escape16(hdc, SETABORTPROC, 0, abrtprc, (SEGPTR)0);
51 /**********************************************************************
52 * SetAbortProc32 (GDI32.301)
55 INT32 WINAPI SetAbortProc32(HDC32 hdc, FARPROC32 abrtprc)
57 FIXME(print, "stub\n");
58 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
59 return SP_ERROR;
63 /****************** misc. printer related functions */
66 * The following function should implement a queing system
68 #ifndef HPQ
69 #define HPQ WORD
70 #endif
71 struct hpq
73 struct hpq *next;
74 int tag;
75 int key;
78 static struct hpq *hpqueue;
80 /**********************************************************************
81 * CreatePQ (GDI.230)
84 HPQ WINAPI CreatePQ(int size)
86 #if 0
87 HGLOBAL16 hpq = 0;
88 WORD tmp_size;
89 LPWORD pPQ;
91 tmp_size = size << 2;
92 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
93 return 0xffff;
94 pPQ = GlobalLock16(hpq);
95 *pPQ++ = 0;
96 *pPQ++ = tmp_size;
97 *pPQ++ = 0;
98 *pPQ++ = 0;
99 GlobalUnlock16(hpq);
101 return (HPQ)hpq;
102 #else
103 FIXME(print, "(%d): stub\n",size);
104 return 1;
105 #endif
108 /**********************************************************************
109 * DeletePQ (GDI.235)
112 int WINAPI DeletePQ(HPQ hPQ)
114 return GlobalFree16((HGLOBAL16)hPQ);
117 /**********************************************************************
118 * ExtractPQ (GDI.232)
121 int WINAPI ExtractPQ(HPQ hPQ)
123 struct hpq *queue, *prev, *current, *currentPrev;
124 int key = 0, tag = -1;
125 currentPrev = prev = NULL;
126 queue = current = hpqueue;
127 if (current)
128 key = current->key;
130 while (current)
132 currentPrev = current;
133 current = current->next;
134 if (current)
136 if (current->key < key)
138 queue = current;
139 prev = currentPrev;
143 if (queue)
145 tag = queue->tag;
147 if (prev)
148 prev->next = queue->next;
149 else
150 hpqueue = queue->next;
151 free(queue);
154 TRACE(print, "%x got tag %d key %d\n", hPQ, tag, key);
156 return tag;
159 /**********************************************************************
160 * InsertPQ (GDI.233)
163 int WINAPI InsertPQ(HPQ hPQ, int tag, int key)
165 struct hpq *queueItem = xmalloc(sizeof(struct hpq));
166 queueItem->next = hpqueue;
167 hpqueue = queueItem;
168 queueItem->key = key;
169 queueItem->tag = tag;
171 FIXME(print, "(%x %d %d): stub???\n", hPQ, tag, key);
172 return TRUE;
175 /**********************************************************************
176 * MinPQ (GDI.231)
179 int WINAPI MinPQ(HPQ hPQ)
181 FIXME(print, "(%x): stub\n", hPQ);
182 return 0;
185 /**********************************************************************
186 * SizePQ (GDI.234)
189 int WINAPI SizePQ(HPQ hPQ, int sizechange)
191 FIXME(print, "(%x %d): stub\n", hPQ, sizechange);
192 return -1;
198 * The following functions implement part of the spooling process to
199 * print manager. I would like to see wine have a version of print managers
200 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
201 * now.
203 typedef struct PRINTJOB
205 char *pszOutput;
206 char *pszTitle;
207 HDC16 hDC;
208 HANDLE16 hHandle;
209 int nIndex;
210 int fd;
211 } PRINTJOB, *PPRINTJOB;
213 #define MAX_PRINT_JOBS 1
214 #define SP_OK 1
216 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
219 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
221 return gPrintJobsTable[0];
224 /* TTD Need to do some DOS->UNIX file conversion here */
225 static int CreateSpoolFile(LPSTR pszOutput)
227 int fd=-1;
228 char psCmd[1024];
229 char *psCmdP = psCmd;
231 /* TTD convert the 'output device' into a spool file name */
233 if (pszOutput == NULL || *pszOutput == '\0')
234 return -1;
236 PROFILE_GetWineIniString( "spooler", pszOutput, "", psCmd, sizeof(psCmd) );
237 TRACE(print, "Got printerSpoolCommand '%s' for output device '%s'\n",
238 psCmd, pszOutput);
239 if (!*psCmd)
240 psCmdP = pszOutput;
241 else
243 while (*psCmdP && isspace(*psCmdP))
245 psCmdP++;
247 if (!*psCmdP)
248 return -1;
250 if (*psCmdP == '|')
252 int fds[2];
253 if (pipe(fds))
254 return -1;
255 if (fork() == 0)
257 psCmdP++;
259 TRACE(print, "In child need to exec %s\n",psCmdP);
260 close(0);
261 dup2(fds[0],0);
262 close (fds[1]);
263 system(psCmdP);
264 exit(0);
267 close (fds[0]);
268 fd = fds[1];
269 TRACE(print,"Need to execute a cmnd and pipe the output to it\n");
271 else
273 TRACE(print, "Just assume its a file\n");
275 if ((fd = open(psCmdP, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
277 ERR(print, "Failed to create spool file %s, errno = %d\n",
278 psCmdP, errno);
281 return fd;
284 static int FreePrintJob(HANDLE16 hJob)
286 int nRet = SP_ERROR;
287 PPRINTJOB pPrintJob;
289 pPrintJob = FindPrintJobFromHandle(hJob);
290 if (pPrintJob != NULL)
292 gPrintJobsTable[pPrintJob->nIndex] = NULL;
293 free(pPrintJob->pszOutput);
294 free(pPrintJob->pszTitle);
295 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
296 free(pPrintJob);
297 nRet = SP_OK;
299 return nRet;
302 /**********************************************************************
303 * OpenJob (GDI.240)
306 HANDLE16 WINAPI OpenJob(LPSTR lpOutput, LPSTR lpTitle, HDC16 hDC)
308 HANDLE16 hHandle = (HANDLE16)SP_ERROR;
309 PPRINTJOB pPrintJob;
311 TRACE(print, "'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
313 pPrintJob = gPrintJobsTable[0];
314 if (pPrintJob == NULL)
316 int fd;
318 /* Try an create a spool file */
319 fd = CreateSpoolFile(lpOutput);
320 if (fd >= 0)
322 hHandle = 1;
324 pPrintJob = xmalloc(sizeof(PRINTJOB));
325 memset(pPrintJob, 0, sizeof(PRINTJOB));
327 pPrintJob->pszOutput = strdup(lpOutput);
328 if(lpTitle)
329 pPrintJob->pszTitle = strdup(lpTitle);
330 pPrintJob->hDC = hDC;
331 pPrintJob->fd = fd;
332 pPrintJob->nIndex = 0;
333 pPrintJob->hHandle = hHandle;
334 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
337 TRACE(print, "return %04x\n", hHandle);
338 return hHandle;
341 /**********************************************************************
342 * CloseJob (GDI.243)
345 int WINAPI CloseJob(HANDLE16 hJob)
347 int nRet = SP_ERROR;
348 PPRINTJOB pPrintJob = NULL;
350 TRACE(print, "%04x\n", hJob);
352 pPrintJob = FindPrintJobFromHandle(hJob);
353 if (pPrintJob != NULL)
355 /* Close the spool file */
356 close(pPrintJob->fd);
357 FreePrintJob(hJob);
358 nRet = 1;
360 return nRet;
363 /**********************************************************************
364 * WriteSpool (GDI.241)
367 int WINAPI WriteSpool(HANDLE16 hJob, LPSTR lpData, WORD cch)
369 int nRet = SP_ERROR;
370 PPRINTJOB pPrintJob = NULL;
372 TRACE(print, "%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
374 pPrintJob = FindPrintJobFromHandle(hJob);
375 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
377 if (write(pPrintJob->fd, lpData, cch) != cch)
378 nRet = SP_OUTOFDISK;
379 else
380 nRet = cch;
381 if (pPrintJob->hDC == 0) {
382 TRACE(print, "hDC == 0 so no QueryAbort\n");
384 else if (!(QueryAbort(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
386 CloseJob(hJob); /* printing aborted */
387 nRet = SP_APPABORT;
390 return nRet;
393 /**********************************************************************
394 * WriteDialog (GDI.242)
397 int WINAPI WriteDialog(HANDLE16 hJob, LPSTR lpMsg, WORD cchMsg)
399 int nRet = 0;
401 TRACE(print, "%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
403 nRet = MessageBox16(0, lpMsg, "Printing Error", MB_OKCANCEL);
404 return nRet;
408 /**********************************************************************
409 * DeleteJob (GDI.244)
412 int WINAPI DeleteJob(HANDLE16 hJob, WORD wNotUsed)
414 int nRet;
416 TRACE(print, "%04x\n", hJob);
418 nRet = FreePrintJob(hJob);
419 return nRet;
423 * The following two function would allow a page to be sent to the printer
424 * when it has been processed. For simplicity they havn't been implemented.
425 * This means a whole job has to be processed before it is sent to the printer.
428 /**********************************************************************
429 * StartSpoolPage (GDI.246)
432 int WINAPI StartSpoolPage(HANDLE16 hJob)
434 FIXME(print, "StartSpoolPage GDI.246 unimplemented\n");
435 return 1;
440 /**********************************************************************
441 * EndSpoolPage (GDI.247)
444 int WINAPI EndSpoolPage(HANDLE16 hJob)
446 FIXME(print, "EndSpoolPage GDI.247 unimplemented\n");
447 return 1;
451 /**********************************************************************
452 * GetSpoolJob (GDI.245)
455 DWORD WINAPI GetSpoolJob(int nOption, LONG param)
457 DWORD retval = 0;
458 TRACE(print, "In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
459 return retval;