Release 980315
[wine/gsoc_dplay.git] / graphics / win16drv / init.c
blob8b1a896789c50c9f24f54062f393daf3a29ac9e4
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 "debug.h"
26 #define SUPPORT_REALIZED_FONTS 1
27 #pragma pack(1)
28 typedef struct
30 SHORT nSize;
31 SEGPTR lpindata;
32 SEGPTR lpFont;
33 SEGPTR lpXForm;
34 SEGPTR lpDrawMode;
35 } EXTTEXTDATA, *LPEXTTEXTDATA;
36 #pragma pack(4)
38 SEGPTR win16drv_SegPtr_TextXForm;
39 LPTEXTXFORM16 win16drv_TextXFormP;
40 SEGPTR win16drv_SegPtr_DrawMode;
41 LPDRAWMODE win16drv_DrawModeP;
44 static BOOL32 WIN16DRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
45 LPCSTR output, const DEVMODE16* initData );
46 static INT32 WIN16DRV_Escape( DC *dc, INT32 nEscape, INT32 cbInput,
47 SEGPTR lpInData, SEGPTR lpOutData );
49 static const DC_FUNCTIONS WIN16DRV_Funcs =
51 NULL, /* pArc */
52 NULL, /* pBitBlt */
53 NULL, /* pChord */
54 WIN16DRV_CreateDC, /* pCreateDC */
55 NULL, /* pDeleteDC */
56 NULL, /* pDeleteObject */
57 WIN16DRV_Ellipse, /* pEllipse */
58 WIN16DRV_EnumDeviceFonts, /* pEnumDeviceFonts */
59 WIN16DRV_Escape, /* pEscape */
60 NULL, /* pExcludeClipRect */
61 NULL, /* pExcludeVisRect */
62 NULL, /* pExtFloodFill */
63 WIN16DRV_ExtTextOut, /* pExtTextOut */
64 WIN16DRV_GetCharWidth, /* pGetCharWidth */
65 NULL, /* pGetPixel */
66 WIN16DRV_GetTextExtentPoint, /* pGetTextExtentPoint */
67 WIN16DRV_GetTextMetrics, /* pGetTextMetrics */
68 NULL, /* pIntersectClipRect */
69 NULL, /* pIntersectVisRect */
70 WIN16DRV_LineTo, /* pLineTo */
71 WIN16DRV_MoveToEx, /* pMoveToEx */
72 NULL, /* pOffsetClipRgn */
73 NULL, /* pOffsetViewportOrgEx */
74 NULL, /* pOffsetWindowOrgEx */
75 NULL, /* pPaintRgn */
76 WIN16DRV_PatBlt, /* pPatBlt */
77 NULL, /* pPie */
78 NULL, /* pPolyPolygon */
79 WIN16DRV_Polygon, /* pPolygon */
80 WIN16DRV_Polyline, /* pPolyline */
81 NULL, /* pRealizePalette */
82 WIN16DRV_Rectangle, /* pRectangle */
83 NULL, /* pRestoreDC */
84 NULL, /* pRoundRect */
85 NULL, /* pSaveDC */
86 NULL, /* pScaleViewportExtEx */
87 NULL, /* pScaleWindowExtEx */
88 NULL, /* pSelectClipRgn */
89 WIN16DRV_SelectObject, /* pSelectObject */
90 NULL, /* pSelectPalette */
91 NULL, /* pSetBkColor */
92 NULL, /* pSetBkMode */
93 NULL, /* pSetDeviceClipping */
94 NULL, /* pSetDIBitsToDevice */
95 NULL, /* pSetMapMode */
96 NULL, /* pSetMapperFlags */
97 NULL, /* pSetPixel */
98 NULL, /* pSetPolyFillMode */
99 NULL, /* pSetROP2 */
100 NULL, /* pSetRelAbs */
101 NULL, /* pSetStretchBltMode */
102 NULL, /* pSetTextAlign */
103 NULL, /* pSetTextCharacterExtra */
104 NULL, /* pSetTextColor */
105 NULL, /* pSetTextJustification */
106 NULL, /* pSetViewportExtEx */
107 NULL, /* pSetViewportOrgEx */
108 NULL, /* pSetWindowExtEx */
109 NULL, /* pSetWindowOrgEx */
110 NULL, /* pStretchBlt */
111 NULL /* pStretchDIBits */
118 /**********************************************************************
119 * WIN16DRV_Init
121 BOOL32 WIN16DRV_Init(void)
123 return DRIVER_RegisterDriver( NULL /* generic driver */, &WIN16DRV_Funcs );
127 /* Tempory functions, for initialising structures */
128 /* These values should be calculated, not hardcoded */
129 void InitTextXForm(LPTEXTXFORM16 lpTextXForm)
131 lpTextXForm->txfHeight = 0x0001;
132 lpTextXForm->txfWidth = 0x000c;
133 lpTextXForm->txfEscapement = 0x0000;
134 lpTextXForm->txfOrientation = 0x0000;
135 lpTextXForm->txfWeight = 0x0190;
136 lpTextXForm->txfItalic = 0x00;
137 lpTextXForm->txfUnderline = 0x00;
138 lpTextXForm->txfStrikeOut = 0x00;
139 lpTextXForm->txfOutPrecision = 0x02;
140 lpTextXForm->txfClipPrecision = 0x01;
141 lpTextXForm->txfAccelerator = 0x0001;
142 lpTextXForm->txfOverhang = 0x0000;
146 void InitDrawMode(LPDRAWMODE lpDrawMode)
148 lpDrawMode->Rop2 = 0x000d;
149 lpDrawMode->bkMode = 0x0001;
150 lpDrawMode->bkColor = 0x3fffffff;
151 lpDrawMode->TextColor = 0x20000000;
152 lpDrawMode->TBreakExtra = 0x0000;
153 lpDrawMode->BreakExtra = 0x0000;
154 lpDrawMode->BreakErr = 0x0000;
155 lpDrawMode->BreakRem = 0x0000;
156 lpDrawMode->BreakCount = 0x0000;
157 lpDrawMode->CharExtra = 0x0000;
158 lpDrawMode->LbkColor = 0x00ffffff;
159 lpDrawMode->LTextColor = 0x00000000;
162 BOOL32 WIN16DRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device, LPCSTR output,
163 const DEVMODE16* initData )
165 LOADED_PRINTER_DRIVER *pLPD;
166 WORD wRet;
167 DeviceCaps *printerDevCaps;
168 int nPDEVICEsize;
169 PDEVICE_HEADER *pPDH;
170 WIN16DRV_PDEVICE *physDev;
171 char printerEnabled[20];
172 PROFILE_GetWineIniString( "wine", "printer", "off",
173 printerEnabled, sizeof(printerEnabled) );
174 if (lstrcmpi32A(printerEnabled,"on"))
176 printf("WIN16DRV_CreateDC disabled in wine.conf file\n");
177 return FALSE;
180 TRACE(win16drv, "In creatdc for (%s,%s,%s) initData 0x%p\n",driver, device, output, initData);
182 physDev = (WIN16DRV_PDEVICE *)HeapAlloc( SystemHeap, 0, sizeof(*physDev) );
183 if (!physDev) return FALSE;
184 dc->physDev = physDev;
186 pLPD = LoadPrinterDriver(driver);
187 if (pLPD == NULL)
189 WARN(win16drv, "Failed to find printer driver\n");
190 HeapFree( SystemHeap, 0, physDev );
191 return FALSE;
193 TRACE(win16drv, "windevCreateDC pLPD 0x%p\n", pLPD);
195 /* Now Get the device capabilities from the printer driver */
197 printerDevCaps = (DeviceCaps *) malloc(sizeof(DeviceCaps));
198 memset(printerDevCaps, 0, sizeof(DeviceCaps));
200 /* Get GDIINFO which is the same as a DeviceCaps structure */
201 wRet = PRTDRV_Enable(printerDevCaps, GETGDIINFO, device, driver, output,NULL);
203 /* Add this to the DC */
204 dc->w.devCaps = printerDevCaps;
205 dc->w.hVisRgn = CreateRectRgn32(0, 0, dc->w.devCaps->horzRes, dc->w.devCaps->vertRes);
206 dc->w.bitsPerPixel = dc->w.devCaps->bitsPixel;
208 printf("Got devcaps width %d height %d bits %d planes %d\n",
209 dc->w.devCaps->horzRes,
210 dc->w.devCaps->vertRes,
211 dc->w.devCaps->bitsPixel,
212 dc->w.devCaps->planes);
214 /* Now we allocate enough memory for the PDEVICE structure */
215 /* The size of this varies between printer drivers */
216 /* This PDEVICE is used by the printer DRIVER not by the GDI so must */
217 /* be accessable from 16 bit code */
218 nPDEVICEsize = dc->w.devCaps->pdeviceSize + sizeof(PDEVICE_HEADER);
220 /* TTD Shouldn't really do pointer arithmetic on segment points */
221 physDev->segptrPDEVICE = WIN16_GlobalLock16(GlobalAlloc16(GHND, nPDEVICEsize))+sizeof(PDEVICE_HEADER);
222 *((BYTE *)PTR_SEG_TO_LIN(physDev->segptrPDEVICE)+0) = 'N';
223 *((BYTE *)PTR_SEG_TO_LIN(physDev->segptrPDEVICE)+1) = 'B';
225 /* Set up the header */
226 pPDH = (PDEVICE_HEADER *)((BYTE*)PTR_SEG_TO_LIN(physDev->segptrPDEVICE) - sizeof(PDEVICE_HEADER));
227 pPDH->pLPD = pLPD;
229 TRACE(win16drv, "PDEVICE allocated %08lx\n",(DWORD)(physDev->segptrPDEVICE));
231 /* Now get the printer driver to initialise this data */
232 wRet = PRTDRV_Enable((LPVOID)physDev->segptrPDEVICE, INITPDEVICE, device, driver, output, NULL);
234 physDev->FontInfo = NULL;
235 physDev->BrushInfo = NULL;
236 physDev->PenInfo = NULL;
237 win16drv_SegPtr_TextXForm = WIN16_GlobalLock16(GlobalAlloc16(GHND, sizeof(TEXTXFORM16)));
238 win16drv_TextXFormP = PTR_SEG_TO_LIN(win16drv_SegPtr_TextXForm);
240 InitTextXForm(win16drv_TextXFormP);
242 /* TTD Lots more to do here */
243 win16drv_SegPtr_DrawMode = WIN16_GlobalLock16(GlobalAlloc16(GHND, sizeof(DRAWMODE)));
244 win16drv_DrawModeP = PTR_SEG_TO_LIN(win16drv_SegPtr_DrawMode);
246 InitDrawMode(win16drv_DrawModeP);
248 return TRUE;
251 BOOL32 WIN16DRV_PatBlt( struct tagDC *dc, INT32 left, INT32 top,
252 INT32 width, INT32 height, DWORD rop )
255 WIN16DRV_PDEVICE *physDev = (WIN16DRV_PDEVICE *)dc->physDev;
256 BOOL32 bRet = 0;
258 bRet = PRTDRV_StretchBlt( physDev->segptrPDEVICE, left, top, width, height, (SEGPTR)NULL, 0, 0, width, height,
259 PATCOPY, physDev->BrushInfo, win16drv_SegPtr_DrawMode, NULL);
261 return bRet;
264 * Escape (GDI.38)
266 static INT32 WIN16DRV_Escape( DC *dc, INT32 nEscape, INT32 cbInput,
267 SEGPTR lpInData, SEGPTR lpOutData )
269 WIN16DRV_PDEVICE *physDev = (WIN16DRV_PDEVICE *)dc->physDev;
270 int nRet = 0;
272 /* We should really process the nEscape parameter, but for now just
273 pass it all to the driver */
274 if (dc != NULL && physDev->segptrPDEVICE != 0)
276 switch(nEscape)
278 case ENABLEPAIRKERNING:
279 fprintf(stderr,"Escape: ENABLEPAIRKERNING ignored.\n");
280 nRet = 1;
281 break;
282 case GETPAIRKERNTABLE:
283 fprintf(stderr,"Escape: GETPAIRKERNTABLE ignored.\n");
284 nRet = 0;
285 break;
286 case SETABORTPROC:
287 printf("Escape: SetAbortProc ignored should be stored in dc somewhere\n");
288 /* Make calling application believe this worked */
289 nRet = 1;
290 break;
292 case NEXTBAND:
294 LPPOINT16 newInData = SEGPTR_NEW(POINT16);
296 nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
297 SEGPTR_GET(newInData), lpOutData);
298 SEGPTR_FREE(newInData);
299 break;
302 case GETEXTENDEDTEXTMETRICS:
304 EXTTEXTDATA *textData = SEGPTR_NEW(EXTTEXTDATA);
306 textData->nSize = cbInput;
307 textData->lpindata = lpInData;
308 textData->lpFont = SEGPTR_GET( physDev->FontInfo );
309 textData->lpXForm = win16drv_SegPtr_TextXForm;
310 textData->lpDrawMode = win16drv_SegPtr_DrawMode;
311 nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
312 SEGPTR_GET(textData), lpOutData);
313 SEGPTR_FREE(textData);
315 break;
316 case STARTDOC:
317 nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
318 lpInData, lpOutData);
319 if (nRet != -1)
321 HDC32 *tmpHdc = SEGPTR_NEW(HDC32);
323 #define SETPRINTERDC SETABORTPROC
325 *tmpHdc = dc->hSelf;
326 PRTDRV_Control(physDev->segptrPDEVICE, SETPRINTERDC,
327 SEGPTR_GET(tmpHdc), (SEGPTR)NULL);
328 SEGPTR_FREE(tmpHdc);
330 break;
331 default:
332 nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
333 lpInData, lpOutData);
334 break;
337 else
338 fprintf(stderr, "Escape(nEscape = %04x)\n", nEscape);
339 return nRet;
345 /****************** misc. printer releated functions */
348 * The following function should implement a queing system
350 #ifndef HPQ
351 #define HPQ WORD
352 #endif
353 struct hpq
355 struct hpq *next;
356 int tag;
357 int key;
360 static struct hpq *hpqueue;
362 HPQ WINAPI CreatePQ(int size)
364 printf("CreatePQ: %d\n",size);
365 return 1;
367 int WINAPI DeletePQ(HPQ hPQ)
369 printf("DeletePQ: %x\n", hPQ);
370 return 0;
372 int WINAPI ExtractPQ(HPQ hPQ)
374 struct hpq *queue, *prev, *current, *currentPrev;
375 int key = 0, tag = -1;
376 currentPrev = prev = NULL;
377 queue = current = hpqueue;
378 if (current)
379 key = current->key;
381 while (current)
383 currentPrev = current;
384 current = current->next;
385 if (current)
387 if (current->key < key)
389 queue = current;
390 prev = currentPrev;
394 if (queue)
396 tag = queue->tag;
398 if (prev)
399 prev->next = queue->next;
400 else
401 hpqueue = queue->next;
402 free(queue);
405 printf("ExtractPQ: %x got tag %d key %d\n", hPQ, tag, key);
407 return tag;
410 int WINAPI InsertPQ(HPQ hPQ, int tag, int key)
412 struct hpq *queueItem = malloc(sizeof(struct hpq));
413 queueItem->next = hpqueue;
414 hpqueue = queueItem;
415 queueItem->key = key;
416 queueItem->tag = tag;
418 printf("InsertPQ: %x %d %d\n", hPQ, tag, key);
419 return TRUE;
421 int WINAPI MinPQ(HPQ hPQ)
423 printf("MinPQ: %x\n", hPQ);
424 return 0;
426 int WINAPI SizePQ(HPQ hPQ, int sizechange)
428 printf("SizePQ: %x %d\n", hPQ, sizechange);
429 return -1;
433 * The following functions implement part of the spooling process to
434 * print manager. I would like to see wine have a version of print managers
435 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
436 * now.
438 typedef struct PRINTJOB
440 char *pszOutput;
441 char *pszTitle;
442 HDC16 hDC;
443 HANDLE16 hHandle;
444 int nIndex;
445 int fd;
446 } PRINTJOB, *PPRINTJOB;
448 #define MAX_PRINT_JOBS 1
449 #define SP_OK 1
451 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
454 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
456 return gPrintJobsTable[0];
459 /* TTD Need to do some DOS->UNIX file conversion here */
460 static int CreateSpoolFile(LPSTR pszOutput)
462 int fd=-1;
463 char psCmd[1024];
464 char *psCmdP = psCmd;
466 /* TTD convert the 'output device' into a spool file name */
468 if (pszOutput == NULL || *pszOutput == '\0')
469 return -1;
471 PROFILE_GetWineIniString( "spooler", pszOutput, "",
472 psCmd, sizeof(psCmd) );
473 printf("Got printerSpoolCommand \"%s\" for output device \"%s\"\n",psCmd, pszOutput);
474 if (!*psCmd)
475 psCmdP = pszOutput;
476 else
478 while (*psCmdP && isspace(*psCmdP))
480 psCmdP++;
482 if (!*psCmdP)
483 return -1;
485 if (*psCmdP == '|')
487 int fds[2];
488 if (pipe(fds))
489 return -1;
490 if (fork() == 0)
492 psCmdP++;
494 printf("In child need to exec %s\n",psCmdP);
495 close(0);
496 dup2(fds[0],0);
497 close (fds[1]);
498 system(psCmdP);
499 exit(0);
502 close (fds[0]);
503 fd = fds[1];
504 printf("Need to execute a command and pipe the output to it\n");
506 else
508 printf("Just assume its a file\n");
510 if ((fd = open(psCmdP, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
512 printf("Failed to create spool file %s, errno = %d\n", psCmdP, errno);
515 return fd;
518 static int FreePrintJob(HANDLE16 hJob)
520 int nRet = SP_ERROR;
521 PPRINTJOB pPrintJob;
523 pPrintJob = FindPrintJobFromHandle(hJob);
524 if (pPrintJob != NULL)
526 gPrintJobsTable[pPrintJob->nIndex] = NULL;
527 free(pPrintJob->pszOutput);
528 free(pPrintJob->pszTitle);
529 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
530 free(pPrintJob);
531 nRet = SP_OK;
533 return nRet;
536 HANDLE16 WINAPI OpenJob(LPSTR lpOutput, LPSTR lpTitle, HDC16 hDC)
538 HANDLE16 hHandle = (HANDLE16)SP_ERROR;
539 PPRINTJOB pPrintJob;
541 TRACE(win16drv, "\"%s\" \"%s\" %04x\n", lpOutput, lpTitle, hDC);
543 pPrintJob = gPrintJobsTable[0];
544 if (pPrintJob == NULL)
546 int fd;
548 /* Try an create a spool file */
549 fd = CreateSpoolFile(lpOutput);
550 if (fd >= 0)
552 hHandle = 1;
554 pPrintJob = malloc(sizeof(PRINTJOB));
555 memset(pPrintJob, 0, sizeof(PRINTJOB));
557 pPrintJob->pszOutput = strdup(lpOutput);
558 pPrintJob->pszTitle = strdup(lpTitle);
559 pPrintJob->hDC = hDC;
560 pPrintJob->fd = fd;
561 pPrintJob->nIndex = 0;
562 pPrintJob->hHandle = hHandle;
563 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
566 TRACE(win16drv, "return %04x\n", hHandle);
567 return hHandle;
570 int WINAPI CloseJob(HANDLE16 hJob)
572 int nRet = SP_ERROR;
573 PPRINTJOB pPrintJob = NULL;
575 TRACE(win16drv, "%04x\n", hJob);
577 pPrintJob = FindPrintJobFromHandle(hJob);
578 if (pPrintJob != NULL)
580 /* Close the spool file */
581 close(pPrintJob->fd);
582 FreePrintJob(hJob);
583 nRet = 1;
585 return nRet;
588 int WINAPI WriteSpool(HANDLE16 hJob, LPSTR lpData, WORD cch)
590 int nRet = SP_ERROR;
591 PPRINTJOB pPrintJob = NULL;
593 TRACE(win16drv, "%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
595 pPrintJob = FindPrintJobFromHandle(hJob);
596 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
598 if (write(pPrintJob->fd, lpData, cch) != cch)
599 nRet = SP_OUTOFDISK;
600 else
601 nRet = cch;
603 return nRet;
606 int WINAPI WriteDialog(HANDLE16 hJob, LPSTR lpMsg, WORD cchMsg)
608 int nRet = 0;
610 TRACE(win16drv, "%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
612 nRet = MessageBox16(0, lpMsg, "Printing Error", MB_OKCANCEL);
613 return nRet;
616 int WINAPI DeleteJob(HANDLE16 hJob, WORD wNotUsed)
618 int nRet;
620 TRACE(win16drv, "%04x\n", hJob);
622 nRet = FreePrintJob(hJob);
623 return nRet;
627 * The following two function would allow a page to be sent to the printer
628 * when it has been processed. For simplicity they havn't been implemented.
629 * This means a whole job has to be processed before it is sent to the printer.
631 int WINAPI StartSpoolPage(HANDLE16 hJob)
633 FIXME(win16drv, "StartSpoolPage GDI.246 unimplemented\n");
634 return 1;
637 int WINAPI EndSpoolPage(HANDLE16 hJob)
639 FIXME(win16drv, "EndSpoolPage GDI.247 unimplemented\n");
640 return 1;
644 DWORD WINAPI GetSpoolJob(int nOption, LONG param)
646 DWORD retval = 0;
647 TRACE(win16drv, "In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
648 return retval;