Release 980201
[wine.git] / graphics / win16drv / init.c
blobe81e5655c05c7e59630dae02ee97d676b4fa58ef
1 /*
2 * Windows Device Context initialisation functions
4 * Copyright 1996 John Harvey
5 * 1998 Huw Davies
6 */
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <ctype.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include "windows.h"
16 #include "win16drv.h"
17 #include "gdi.h"
18 #include "bitmap.h"
19 #include "heap.h"
20 #include "color.h"
21 #include "font.h"
22 #include "callback.h"
23 #include "options.h"
24 #include "stddebug.h"
25 #include "debug.h"
27 #define SUPPORT_REALIZED_FONTS 1
28 #pragma pack(1)
29 typedef struct
31 SHORT nSize;
32 SEGPTR lpindata;
33 SEGPTR lpFont;
34 SEGPTR lpXForm;
35 SEGPTR lpDrawMode;
36 } EXTTEXTDATA, *LPEXTTEXTDATA;
37 #pragma pack(4)
39 SEGPTR win16drv_SegPtr_TextXForm;
40 LPTEXTXFORM16 win16drv_TextXFormP;
41 SEGPTR win16drv_SegPtr_DrawMode;
42 LPDRAWMODE win16drv_DrawModeP;
45 static BOOL32 WIN16DRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
46 LPCSTR output, const DEVMODE16* initData );
47 static INT32 WIN16DRV_Escape( DC *dc, INT32 nEscape, INT32 cbInput,
48 SEGPTR lpInData, SEGPTR lpOutData );
50 static const DC_FUNCTIONS WIN16DRV_Funcs =
52 NULL, /* pArc */
53 NULL, /* pBitBlt */
54 NULL, /* pChord */
55 WIN16DRV_CreateDC, /* pCreateDC */
56 NULL, /* pDeleteDC */
57 NULL, /* pDeleteObject */
58 WIN16DRV_Ellipse, /* pEllipse */
59 WIN16DRV_EnumDeviceFonts, /* pEnumDeviceFonts */
60 WIN16DRV_Escape, /* pEscape */
61 NULL, /* pExcludeClipRect */
62 NULL, /* pExcludeVisRect */
63 NULL, /* pExtFloodFill */
64 WIN16DRV_ExtTextOut, /* pExtTextOut */
65 WIN16DRV_GetCharWidth, /* pGetCharWidth */
66 NULL, /* pGetPixel */
67 WIN16DRV_GetTextExtentPoint, /* pGetTextExtentPoint */
68 WIN16DRV_GetTextMetrics, /* pGetTextMetrics */
69 NULL, /* pIntersectClipRect */
70 NULL, /* pIntersectVisRect */
71 WIN16DRV_LineTo, /* pLineTo */
72 WIN16DRV_MoveToEx, /* pMoveToEx */
73 NULL, /* pOffsetClipRgn */
74 NULL, /* pOffsetViewportOrgEx */
75 NULL, /* pOffsetWindowOrgEx */
76 NULL, /* pPaintRgn */
77 WIN16DRV_PatBlt, /* pPatBlt */
78 NULL, /* pPie */
79 NULL, /* pPolyPolygon */
80 WIN16DRV_Polygon, /* pPolygon */
81 WIN16DRV_Polyline, /* pPolyline */
82 NULL, /* pRealizePalette */
83 WIN16DRV_Rectangle, /* pRectangle */
84 NULL, /* pRestoreDC */
85 NULL, /* pRoundRect */
86 NULL, /* pSaveDC */
87 NULL, /* pScaleViewportExtEx */
88 NULL, /* pScaleWindowExtEx */
89 NULL, /* pSelectClipRgn */
90 WIN16DRV_SelectObject, /* pSelectObject */
91 NULL, /* pSelectPalette */
92 NULL, /* pSetBkColor */
93 NULL, /* pSetBkMode */
94 NULL, /* pSetDeviceClipping */
95 NULL, /* pSetDIBitsToDevice */
96 NULL, /* pSetMapMode */
97 NULL, /* pSetMapperFlags */
98 NULL, /* pSetPixel */
99 NULL, /* pSetPolyFillMode */
100 NULL, /* pSetROP2 */
101 NULL, /* pSetRelAbs */
102 NULL, /* pSetStretchBltMode */
103 NULL, /* pSetTextAlign */
104 NULL, /* pSetTextCharacterExtra */
105 NULL, /* pSetTextColor */
106 NULL, /* pSetTextJustification */
107 NULL, /* pSetViewportExtEx */
108 NULL, /* pSetViewportOrgEx */
109 NULL, /* pSetWindowExtEx */
110 NULL, /* pSetWindowOrgEx */
111 NULL, /* pStretchBlt */
112 NULL /* pStretchDIBits */
119 /**********************************************************************
120 * WIN16DRV_Init
122 BOOL32 WIN16DRV_Init(void)
124 return DRIVER_RegisterDriver( NULL /* generic driver */, &WIN16DRV_Funcs );
128 /* Tempory functions, for initialising structures */
129 /* These values should be calculated, not hardcoded */
130 void InitTextXForm(LPTEXTXFORM16 lpTextXForm)
132 lpTextXForm->txfHeight = 0x0001;
133 lpTextXForm->txfWidth = 0x000c;
134 lpTextXForm->txfEscapement = 0x0000;
135 lpTextXForm->txfOrientation = 0x0000;
136 lpTextXForm->txfWeight = 0x0190;
137 lpTextXForm->txfItalic = 0x00;
138 lpTextXForm->txfUnderline = 0x00;
139 lpTextXForm->txfStrikeOut = 0x00;
140 lpTextXForm->txfOutPrecision = 0x02;
141 lpTextXForm->txfClipPrecision = 0x01;
142 lpTextXForm->txfAccelerator = 0x0001;
143 lpTextXForm->txfOverhang = 0x0000;
147 void InitDrawMode(LPDRAWMODE lpDrawMode)
149 lpDrawMode->Rop2 = 0x000d;
150 lpDrawMode->bkMode = 0x0001;
151 lpDrawMode->bkColor = 0x3fffffff;
152 lpDrawMode->TextColor = 0x20000000;
153 lpDrawMode->TBreakExtra = 0x0000;
154 lpDrawMode->BreakExtra = 0x0000;
155 lpDrawMode->BreakErr = 0x0000;
156 lpDrawMode->BreakRem = 0x0000;
157 lpDrawMode->BreakCount = 0x0000;
158 lpDrawMode->CharExtra = 0x0000;
159 lpDrawMode->LbkColor = 0x00ffffff;
160 lpDrawMode->LTextColor = 0x00000000;
163 BOOL32 WIN16DRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device, LPCSTR output,
164 const DEVMODE16* initData )
166 LOADED_PRINTER_DRIVER *pLPD;
167 WORD wRet;
168 DeviceCaps *printerDevCaps;
169 int nPDEVICEsize;
170 PDEVICE_HEADER *pPDH;
171 WIN16DRV_PDEVICE *physDev;
172 char printerEnabled[20];
173 PROFILE_GetWineIniString( "wine", "printer", "off",
174 printerEnabled, sizeof(printerEnabled) );
175 if (lstrcmpi32A(printerEnabled,"on"))
177 printf("WIN16DRV_CreateDC disabled in wine.conf file\n");
178 return FALSE;
181 dprintf_win16drv(stddeb, "In creatdc for (%s,%s,%s) initData 0x%p\n",driver, device, output, initData);
183 physDev = (WIN16DRV_PDEVICE *)HeapAlloc( SystemHeap, 0, sizeof(*physDev) );
184 if (!physDev) return FALSE;
185 dc->physDev = physDev;
187 pLPD = LoadPrinterDriver(driver);
188 if (pLPD == NULL)
190 dprintf_win16drv(stddeb, "LPGDI_CreateDC: Failed to find printer driver\n");
191 HeapFree( SystemHeap, 0, physDev );
192 return FALSE;
194 dprintf_win16drv(stddeb, "windevCreateDC pLPD 0x%p\n", pLPD);
196 /* Now Get the device capabilities from the printer driver */
198 printerDevCaps = (DeviceCaps *) malloc(sizeof(DeviceCaps));
199 memset(printerDevCaps, 0, sizeof(DeviceCaps));
201 /* Get GDIINFO which is the same as a DeviceCaps structure */
202 wRet = PRTDRV_Enable(printerDevCaps, GETGDIINFO, device, driver, output,NULL);
204 /* Add this to the DC */
205 dc->w.devCaps = printerDevCaps;
206 dc->w.hVisRgn = CreateRectRgn32(0, 0, dc->w.devCaps->horzRes, dc->w.devCaps->vertRes);
207 dc->w.bitsPerPixel = dc->w.devCaps->bitsPixel;
209 printf("Got devcaps width %d height %d bits %d planes %d\n",
210 dc->w.devCaps->horzRes,
211 dc->w.devCaps->vertRes,
212 dc->w.devCaps->bitsPixel,
213 dc->w.devCaps->planes);
215 /* Now we allocate enough memory for the PDEVICE structure */
216 /* The size of this varies between printer drivers */
217 /* This PDEVICE is used by the printer DRIVER not by the GDI so must */
218 /* be accessable from 16 bit code */
219 nPDEVICEsize = dc->w.devCaps->pdeviceSize + sizeof(PDEVICE_HEADER);
221 /* TTD Shouldn't really do pointer arithmetic on segment points */
222 physDev->segptrPDEVICE = WIN16_GlobalLock16(GlobalAlloc16(GHND, nPDEVICEsize))+sizeof(PDEVICE_HEADER);
223 *((BYTE *)PTR_SEG_TO_LIN(physDev->segptrPDEVICE)+0) = 'N';
224 *((BYTE *)PTR_SEG_TO_LIN(physDev->segptrPDEVICE)+1) = 'B';
226 /* Set up the header */
227 pPDH = (PDEVICE_HEADER *)((BYTE*)PTR_SEG_TO_LIN(physDev->segptrPDEVICE) - sizeof(PDEVICE_HEADER));
228 pPDH->pLPD = pLPD;
230 dprintf_win16drv(stddeb, "PRTDRV_Enable: PDEVICE allocated %08lx\n",(DWORD)(physDev->segptrPDEVICE));
232 /* Now get the printer driver to initialise this data */
233 wRet = PRTDRV_Enable((LPVOID)physDev->segptrPDEVICE, INITPDEVICE, device, driver, output, NULL);
235 physDev->FontInfo = NULL;
236 physDev->BrushInfo = NULL;
237 physDev->PenInfo = NULL;
238 win16drv_SegPtr_TextXForm = WIN16_GlobalLock16(GlobalAlloc16(GHND, sizeof(TEXTXFORM16)));
239 win16drv_TextXFormP = PTR_SEG_TO_LIN(win16drv_SegPtr_TextXForm);
241 InitTextXForm(win16drv_TextXFormP);
243 /* TTD Lots more to do here */
244 win16drv_SegPtr_DrawMode = WIN16_GlobalLock16(GlobalAlloc16(GHND, sizeof(DRAWMODE)));
245 win16drv_DrawModeP = PTR_SEG_TO_LIN(win16drv_SegPtr_DrawMode);
247 InitDrawMode(win16drv_DrawModeP);
249 return TRUE;
252 BOOL32 WIN16DRV_PatBlt( struct tagDC *dc, INT32 left, INT32 top,
253 INT32 width, INT32 height, DWORD rop )
256 WIN16DRV_PDEVICE *physDev = (WIN16DRV_PDEVICE *)dc->physDev;
257 BOOL32 bRet = 0;
259 bRet = PRTDRV_StretchBlt( physDev->segptrPDEVICE, left, top, width, height, (SEGPTR)NULL, 0, 0, width, height,
260 PATCOPY, physDev->BrushInfo, win16drv_SegPtr_DrawMode, NULL);
262 return bRet;
265 * Escape (GDI.38)
267 static INT32 WIN16DRV_Escape( DC *dc, INT32 nEscape, INT32 cbInput,
268 SEGPTR lpInData, SEGPTR lpOutData )
270 WIN16DRV_PDEVICE *physDev = (WIN16DRV_PDEVICE *)dc->physDev;
271 int nRet = 0;
273 /* We should really process the nEscape parameter, but for now just
274 pass it all to the driver */
275 if (dc != NULL && physDev->segptrPDEVICE != 0)
277 switch(nEscape)
279 case ENABLEPAIRKERNING:
280 fprintf(stderr,"Escape: ENABLEPAIRKERNING ignored.\n");
281 nRet = 1;
282 break;
283 case GETPAIRKERNTABLE:
284 fprintf(stderr,"Escape: GETPAIRKERNTABLE ignored.\n");
285 nRet = 0;
286 break;
287 case SETABORTPROC:
288 printf("Escape: SetAbortProc ignored should be stored in dc somewhere\n");
289 /* Make calling application believe this worked */
290 nRet = 1;
291 break;
293 case NEXTBAND:
295 LPPOINT16 newInData = SEGPTR_NEW(POINT16);
297 nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
298 SEGPTR_GET(newInData), lpOutData);
299 SEGPTR_FREE(newInData);
300 break;
303 case GETEXTENDEDTEXTMETRICS:
305 EXTTEXTDATA *textData = SEGPTR_NEW(EXTTEXTDATA);
307 textData->nSize = cbInput;
308 textData->lpindata = lpInData;
309 textData->lpFont = SEGPTR_GET( physDev->FontInfo );
310 textData->lpXForm = win16drv_SegPtr_TextXForm;
311 textData->lpDrawMode = win16drv_SegPtr_DrawMode;
312 nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
313 SEGPTR_GET(textData), lpOutData);
314 SEGPTR_FREE(textData);
316 break;
317 case STARTDOC:
318 nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
319 lpInData, lpOutData);
320 if (nRet != -1)
322 HDC32 *tmpHdc = SEGPTR_NEW(HDC32);
324 #define SETPRINTERDC SETABORTPROC
326 *tmpHdc = dc->hSelf;
327 PRTDRV_Control(physDev->segptrPDEVICE, SETPRINTERDC,
328 SEGPTR_GET(tmpHdc), (SEGPTR)NULL);
329 SEGPTR_FREE(tmpHdc);
331 break;
332 default:
333 nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
334 lpInData, lpOutData);
335 break;
338 else
339 fprintf(stderr, "Escape(nEscape = %04x)\n", nEscape);
340 return nRet;
346 /****************** misc. printer releated functions */
349 * The following function should implement a queing system
351 #ifndef HPQ
352 #define HPQ WORD
353 #endif
354 struct hpq
356 struct hpq *next;
357 int tag;
358 int key;
361 static struct hpq *hpqueue;
363 HPQ WINAPI CreatePQ(int size)
365 printf("CreatePQ: %d\n",size);
366 return 1;
368 int WINAPI DeletePQ(HPQ hPQ)
370 printf("DeletePQ: %x\n", hPQ);
371 return 0;
373 int WINAPI ExtractPQ(HPQ hPQ)
375 struct hpq *queue, *prev, *current, *currentPrev;
376 int key = 0, tag = -1;
377 currentPrev = prev = NULL;
378 queue = current = hpqueue;
379 if (current)
380 key = current->key;
382 while (current)
384 currentPrev = current;
385 current = current->next;
386 if (current)
388 if (current->key < key)
390 queue = current;
391 prev = currentPrev;
395 if (queue)
397 tag = queue->tag;
399 if (prev)
400 prev->next = queue->next;
401 else
402 hpqueue = queue->next;
403 free(queue);
406 printf("ExtractPQ: %x got tag %d key %d\n", hPQ, tag, key);
408 return tag;
411 int WINAPI InsertPQ(HPQ hPQ, int tag, int key)
413 struct hpq *queueItem = malloc(sizeof(struct hpq));
414 queueItem->next = hpqueue;
415 hpqueue = queueItem;
416 queueItem->key = key;
417 queueItem->tag = tag;
419 printf("InsertPQ: %x %d %d\n", hPQ, tag, key);
420 return TRUE;
422 int WINAPI MinPQ(HPQ hPQ)
424 printf("MinPQ: %x\n", hPQ);
425 return 0;
427 int WINAPI SizePQ(HPQ hPQ, int sizechange)
429 printf("SizePQ: %x %d\n", hPQ, sizechange);
430 return -1;
434 * The following functions implement part of the spooling process to
435 * print manager. I would like to see wine have a version of print managers
436 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
437 * now.
439 typedef struct PRINTJOB
441 char *pszOutput;
442 char *pszTitle;
443 HDC16 hDC;
444 HANDLE16 hHandle;
445 int nIndex;
446 int fd;
447 } PRINTJOB, *PPRINTJOB;
449 #define MAX_PRINT_JOBS 1
450 #define SP_OK 1
452 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
455 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
457 return gPrintJobsTable[0];
460 /* TTD Need to do some DOS->UNIX file conversion here */
461 static int CreateSpoolFile(LPSTR pszOutput)
463 int fd=-1;
464 char psCmd[1024];
465 char *psCmdP = psCmd;
467 /* TTD convert the 'output device' into a spool file name */
469 if (pszOutput == NULL || *pszOutput == '\0')
470 return -1;
472 PROFILE_GetWineIniString( "spooler", pszOutput, "",
473 psCmd, sizeof(psCmd) );
474 printf("Got printerSpoolCommand \"%s\"\n",psCmd);
475 if (!*psCmd)
476 psCmdP = pszOutput;
477 else
479 while (*psCmdP && isspace(*psCmdP))
481 psCmdP++;
483 if (!*psCmdP)
484 return -1;
486 if (*psCmdP == '|')
488 int fds[2];
489 if (pipe(fds))
490 return -1;
491 if (fork() == 0)
493 psCmdP++;
495 printf("In child need to exec %s\n",psCmdP);
496 close(0);
497 dup2(fds[0],0);
498 close (fds[1]);
499 system(psCmdP);
500 exit(0);
503 close (fds[0]);
504 fd = fds[1];
505 printf("Need to execute a command and pipe the output to it\n");
507 else
509 printf("Just assume its a file\n");
511 if ((fd = open(psCmdP, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
513 printf("Failed to create spool file %s, errno = %d\n", psCmdP, errno);
516 return fd;
519 static int FreePrintJob(HANDLE16 hJob)
521 int nRet = SP_ERROR;
522 PPRINTJOB pPrintJob;
524 pPrintJob = FindPrintJobFromHandle(hJob);
525 if (pPrintJob != NULL)
527 gPrintJobsTable[pPrintJob->nIndex] = NULL;
528 free(pPrintJob->pszOutput);
529 free(pPrintJob->pszTitle);
530 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
531 free(pPrintJob);
532 nRet = SP_OK;
534 return nRet;
537 HANDLE16 WINAPI OpenJob(LPSTR lpOutput, LPSTR lpTitle, HDC16 hDC)
539 HANDLE16 hHandle = (HANDLE16)SP_ERROR;
540 PPRINTJOB pPrintJob;
542 dprintf_win16drv(stddeb, "OpenJob: \"%s\" \"%s\" %04x\n", lpOutput, lpTitle, hDC);
544 pPrintJob = gPrintJobsTable[0];
545 if (pPrintJob == NULL)
547 int fd;
549 /* Try an create a spool file */
550 fd = CreateSpoolFile(lpOutput);
551 if (fd >= 0)
553 hHandle = 1;
555 pPrintJob = malloc(sizeof(PRINTJOB));
556 memset(pPrintJob, 0, sizeof(PRINTJOB));
558 pPrintJob->pszOutput = strdup(lpOutput);
559 pPrintJob->pszTitle = strdup(lpTitle);
560 pPrintJob->hDC = hDC;
561 pPrintJob->fd = fd;
562 pPrintJob->nIndex = 0;
563 pPrintJob->hHandle = hHandle;
564 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
567 dprintf_win16drv(stddeb, "OpenJob: return %04x\n", hHandle);
568 return hHandle;
571 int WINAPI CloseJob(HANDLE16 hJob)
573 int nRet = SP_ERROR;
574 PPRINTJOB pPrintJob = NULL;
576 dprintf_win16drv(stddeb, "CloseJob: %04x\n", hJob);
578 pPrintJob = FindPrintJobFromHandle(hJob);
579 if (pPrintJob != NULL)
581 /* Close the spool file */
582 close(pPrintJob->fd);
583 FreePrintJob(hJob);
584 nRet = 1;
586 return nRet;
589 int WINAPI WriteSpool(HANDLE16 hJob, LPSTR lpData, WORD cch)
591 int nRet = SP_ERROR;
592 PPRINTJOB pPrintJob = NULL;
594 dprintf_win16drv(stddeb, "WriteSpool: %04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
596 pPrintJob = FindPrintJobFromHandle(hJob);
597 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
599 if (write(pPrintJob->fd, lpData, cch) != cch)
600 nRet = SP_OUTOFDISK;
601 else
602 nRet = cch;
604 return nRet;
607 int WINAPI WriteDialog(HANDLE16 hJob, LPSTR lpMsg, WORD cchMsg)
609 int nRet = 0;
611 dprintf_win16drv(stddeb, "WriteDialog: %04x %04x \"%s\"\n", hJob, cchMsg, lpMsg);
613 nRet = MessageBox16(0, lpMsg, "Printing Error", MB_OKCANCEL);
614 return nRet;
617 int WINAPI DeleteJob(HANDLE16 hJob, WORD wNotUsed)
619 int nRet;
621 dprintf_win16drv(stddeb, "DeleteJob: %04x\n", hJob);
623 nRet = FreePrintJob(hJob);
624 return nRet;
628 * The following two function would allow a page to be sent to the printer
629 * when it has been processed. For simplicity they havn't been implemented.
630 * This means a whole job has to be processed before it is sent to the printer.
632 int WINAPI StartSpoolPage(HANDLE16 hJob)
634 dprintf_win16drv(stddeb, "StartSpoolPage GDI.246 unimplemented\n");
635 return 1;
638 int WINAPI EndSpoolPage(HANDLE16 hJob)
640 dprintf_win16drv(stddeb, "EndSpoolPage GDI.247 unimplemented\n");
641 return 1;
645 DWORD WINAPI GetSpoolJob(int nOption, LONG param)
647 DWORD retval = 0;
648 dprintf_win16drv(stddeb, "In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
649 return retval;