Release 970509
[wine/multimedia.git] / graphics / win16drv / init.c
blobda9e60b8940a1a3e048abc2f8d18a564a1062c46
1 /*
2 * Windows Device Context initialisation functions
4 * Copyright 1996 John Harvey
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <errno.h>
13 #include "windows.h"
14 #include "module.h"
15 #include "win16drv.h"
16 #include "gdi.h"
17 #include "bitmap.h"
18 #include "heap.h"
19 #include "color.h"
20 #include "font.h"
21 #include "callback.h"
22 #include "stddebug.h"
23 #include "debug.h"
25 #define SUPPORT_REALIZED_FONTS 1
26 #pragma pack(1)
27 typedef struct
29 SHORT nSize;
30 LPVOID lpindata;
31 LPVOID lpFont;
32 LPVOID lpXForm;
33 LPVOID lpDrawMode;
34 } EXTTEXTDATA, *LPEXTTEXTDATA;
35 #pragma pack(4)
37 #if 0
38 static BOOL16 windrvExtTextOut16( DC *dc, INT16 x, INT16 y, UINT16 flags, const RECT16 * lprect,
39 LPCSTR str, UINT16 count, const INT16 *lpDx);
40 #endif
42 DRAWMODE DrawMode;
43 static BOOL32 WIN16DRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device,
44 LPCSTR output, const DEVMODE16* initData );
45 static INT32 WIN16DRV_Escape( DC *dc, INT32 nEscape, INT32 cbInput,
46 SEGPTR lpInData, SEGPTR lpOutData );
48 static const DC_FUNCTIONS WIN16DRV_Funcs =
50 NULL, /* pArc */
51 NULL, /* pBitBlt */
52 NULL, /* pChord */
53 WIN16DRV_CreateDC, /* pCreateDC */
54 NULL, /* pDeleteDC */
55 NULL, /* pDeleteObject */
56 NULL, /* pEllipse */
57 NULL, /* pEnumDeviceFonts */
58 WIN16DRV_Escape, /* pEscape */
59 NULL, /* pExcludeClipRect */
60 NULL, /* pExcludeVisRect */
61 NULL, /* pExtFloodFill */
62 WIN16DRV_ExtTextOut, /* pExtTextOut */
63 WIN16DRV_GetCharWidth, /* pGetCharWidth */
64 NULL, /* pGetPixel */
65 WIN16DRV_GetTextExtentPoint, /* pGetTextExtentPoint */
66 WIN16DRV_GetTextMetrics, /* pGetTextMetrics */
67 NULL, /* pIntersectClipRect */
68 NULL, /* pIntersectVisRect */
69 NULL, /* pLineTo */
70 NULL, /* pMoveToEx */
71 NULL, /* pOffsetClipRgn */
72 NULL, /* pOffsetViewportOrgEx */
73 NULL, /* pOffsetWindowOrgEx */
74 NULL, /* pPaintRgn */
75 NULL, /* pPatBlt */
76 NULL, /* pPie */
77 NULL, /* pPolyPolygon */
78 NULL, /* pPolygon */
79 NULL, /* pPolyline */
80 NULL, /* pRealizePalette */
81 NULL, /* pRectangle */
82 NULL, /* pRestoreDC */
83 NULL, /* pRoundRect */
84 NULL, /* pSaveDC */
85 NULL, /* pScaleViewportExtEx */
86 NULL, /* pScaleWindowExtEx */
87 NULL, /* pSelectClipRgn */
88 WIN16DRV_SelectObject, /* pSelectObject */
89 NULL, /* pSelectPalette */
90 NULL, /* pSetBkColor */
91 NULL, /* pSetBkMode */
92 NULL, /* pSetDeviceClipping */
93 NULL, /* pSetDIBitsToDevice */
94 NULL, /* pSetMapMode */
95 NULL, /* pSetMapperFlags */
96 NULL, /* pSetPixel */
97 NULL, /* pSetPolyFillMode */
98 NULL, /* pSetROP2 */
99 NULL, /* pSetRelAbs */
100 NULL, /* pSetStretchBltMode */
101 NULL, /* pSetTextAlign */
102 NULL, /* pSetTextCharacterExtra */
103 NULL, /* pSetTextColor */
104 NULL, /* pSetTextJustification */
105 NULL, /* pSetViewportExtEx */
106 NULL, /* pSetViewportOrgEx */
107 NULL, /* pSetWindowExtEx */
108 NULL, /* pSetWindowOrgEx */
109 NULL, /* pStretchBlt */
110 NULL /* pStretchDIBits */
117 /**********************************************************************
118 * WIN16DRV_Init
120 BOOL32 WIN16DRV_Init(void)
122 return DRIVER_RegisterDriver( NULL /* generic driver */, &WIN16DRV_Funcs );
126 /* Tempory functions, for initialising structures */
127 /* These values should be calculated, not hardcoded */
128 void InitTextXForm(LPTEXTXFORM16 lpTextXForm)
130 lpTextXForm->txfHeight = 0x0001;
131 lpTextXForm->txfWidth = 0x000c;
132 lpTextXForm->txfEscapement = 0x0000;
133 lpTextXForm->txfOrientation = 0x0000;
134 lpTextXForm->txfWeight = 0x0190;
135 lpTextXForm->txfItalic = 0x00;
136 lpTextXForm->txfUnderline = 0x00;
137 lpTextXForm->txfStrikeOut = 0x00;
138 lpTextXForm->txfOutPrecision = 0x02;
139 lpTextXForm->txfClipPrecision = 0x01;
140 lpTextXForm->txfAccelerator = 0x0001;
141 lpTextXForm->txfOverhang = 0x0000;
145 void InitDrawMode(LPDRAWMODE lpDrawMode)
147 lpDrawMode->Rop2 = 0x000d;
148 lpDrawMode->bkMode = 0x0001;
149 lpDrawMode->bkColor = 0x3fffffff;
150 lpDrawMode->TextColor = 0x20000000;
151 lpDrawMode->TBreakExtra = 0x0000;
152 lpDrawMode->BreakExtra = 0x0000;
153 lpDrawMode->BreakErr = 0x0000;
154 lpDrawMode->BreakRem = 0x0000;
155 lpDrawMode->BreakCount = 0x0000;
156 lpDrawMode->CharExtra = 0x0000;
157 lpDrawMode->LbkColor = 0x00ffffff;
158 lpDrawMode->LTextColor = 0x00000000;
162 * EnumCallback (GDI.158)
164 * This is the callback function used when EnumDFonts is called.
165 * (The printer drivers uses it to pass info on available fonts).
167 * lpvClientData is the pointer passed to EnumDFonts, which points to a WEPFC
168 * structure (WEPFC = WINE_ENUM_PRINTER_FONT_CALLBACK). This structure
169 * contains infomation on how to store the data passed .
171 * There are two modes:
172 * 1) Just count the number of fonts available.
173 * 2) Store all font data passed.
175 WORD WineEnumDFontCallback(LPLOGFONT16 lpLogFont, LPTEXTMETRIC16 lpTextMetrics,
176 WORD wFontType, LONG lpvClientData)
178 int wRet = 0;
179 WEPFC *pWEPFC = (WEPFC *)lpvClientData;
181 /* Make sure we have the right structure */
182 if (pWEPFC != NULL )
184 dprintf_win16drv(stddeb, "mode is 0x%x\n",pWEPFC->nMode);
186 switch (pWEPFC->nMode)
188 /* Count how many fonts */
189 case 1:
190 pWEPFC->nCount++;
191 break;
193 /* Store the fonts in the printer driver structure */
194 case 2:
196 PRINTER_FONTS_INFO *pPFI;
198 dprintf_win16drv(stddeb, "WineEnumDFontCallback: Found %s %x\n",
199 lpLogFont->lfFaceName, wFontType);
201 pPFI = &pWEPFC->pLPD->paPrinterFonts[pWEPFC->nCount];
202 memcpy(&(pPFI->lf), lpLogFont, sizeof(LOGFONT16));
203 memcpy(&(pPFI->tm), lpTextMetrics, sizeof(TEXTMETRIC16));
204 pWEPFC->nCount++;
207 break;
209 wRet = 1;
211 dprintf_win16drv(stddeb, "WineEnumDFontCallback: returnd %d\n", wRet);
212 return wRet;
215 BOOL32 WIN16DRV_CreateDC( DC *dc, LPCSTR driver, LPCSTR device, LPCSTR output,
216 const DEVMODE16* initData )
218 LOADED_PRINTER_DRIVER *pLPD;
219 WORD wRet;
220 DeviceCaps *printerDevCaps;
221 FARPROC16 pfnCallback;
222 int nPDEVICEsize;
223 PDEVICE_HEADER *pPDH;
224 WIN16DRV_PDEVICE *physDev;
225 LPDRAWMODE lpDrawMode = &DrawMode;
227 /* Realizing fonts */
228 TEXTXFORM16 TextXForm;
229 int nSize;
230 char printerEnabled[20];
231 PROFILE_GetWineIniString( "wine", "printer", "off",
232 printerEnabled, sizeof(printerEnabled) );
233 if (strcmp(printerEnabled,"on"))
235 printf("WIN16DRV_CreateDC disabled in wine.conf file\n");
236 return FALSE;
239 dprintf_win16drv(stddeb, "In creatdc for (%s,%s,%s) initData 0x%p\n",driver, device, output, initData);
241 physDev = (WIN16DRV_PDEVICE *)HeapAlloc( SystemHeap, 0, sizeof(*physDev) );
242 if (!physDev) return FALSE;
243 dc->physDev = physDev;
245 pLPD = LoadPrinterDriver(driver);
246 if (pLPD == NULL)
248 dprintf_win16drv(stddeb, "LPGDI_CreateDC: Failed to find printer driver\n");
249 HeapFree( SystemHeap, 0, physDev );
250 return FALSE;
252 dprintf_win16drv(stddeb, "windevCreateDC pLPD 0x%p\n", pLPD);
254 /* Now Get the device capabilities from the printer driver */
256 printerDevCaps = (DeviceCaps *) malloc(sizeof(DeviceCaps));
257 memset(printerDevCaps, 0, sizeof(DeviceCaps));
259 /* Get GDIINFO which is the same as a DeviceCaps structure */
260 wRet = PRTDRV_Enable(printerDevCaps, GETGDIINFO, device, driver, output,NULL);
262 /* Add this to the DC */
263 dc->w.devCaps = printerDevCaps;
264 dc->w.hVisRgn = CreateRectRgn32(0, 0, dc->w.devCaps->horzRes, dc->w.devCaps->vertRes);
265 dc->w.bitsPerPixel = dc->w.devCaps->bitsPixel;
267 printf("Got devcaps width %d height %d bits %d planes %d\n",
268 dc->w.devCaps->horzRes,
269 dc->w.devCaps->vertRes,
270 dc->w.devCaps->bitsPixel,
271 dc->w.devCaps->planes);
273 /* Now we allocate enough memory for the PDEVICE structure */
274 /* The size of this varies between printer drivers */
275 /* This PDEVICE is used by the printer DRIVER not by the GDI so must */
276 /* be accessable from 16 bit code */
277 nPDEVICEsize = dc->w.devCaps->pdeviceSize + sizeof(PDEVICE_HEADER);
279 /* TTD Shouldn't really do pointer arithmetic on segment points */
280 physDev->segptrPDEVICE = WIN16_GlobalLock16(GlobalAlloc16(GHND, nPDEVICEsize))+sizeof(PDEVICE_HEADER);
281 *(BYTE *)(PTR_SEG_TO_LIN(physDev->segptrPDEVICE)+0) = 'N';
282 *(BYTE *)(PTR_SEG_TO_LIN(physDev->segptrPDEVICE)+1) = 'B';
284 /* Set up the header */
285 pPDH = (PDEVICE_HEADER *)(PTR_SEG_TO_LIN(physDev->segptrPDEVICE) - sizeof(PDEVICE_HEADER));
286 pPDH->pLPD = pLPD;
288 dprintf_win16drv(stddeb, "PRTDRV_Enable: PDEVICE allocated %08lx\n",(DWORD)(physDev->segptrPDEVICE));
290 /* Now get the printer driver to initialise this data */
291 wRet = PRTDRV_Enable((LPVOID)physDev->segptrPDEVICE, INITPDEVICE, device, driver, output, NULL);
293 /* Now enumerate the fonts supported by the printer driver*/
294 /* GDI.158 is EnumCallback, which is called by the 16bit printer driver */
295 /* passing information on the available fonts */
296 if (pLPD->paPrinterFonts == NULL)
298 pfnCallback = MODULE_GetEntryPoint( GetModuleHandle16("GDI"), 158 );
300 if (pfnCallback != NULL)
302 WEPFC wepfc;
304 wepfc.nMode = 1;
305 wepfc.nCount = 0;
306 wepfc.pLPD = pLPD;
308 /* First count the number of fonts */
310 PRTDRV_EnumDFonts(physDev->segptrPDEVICE, NULL, pfnCallback,
311 (void *)&wepfc);
313 /* Allocate a buffer to store all of the fonts */
314 pLPD->nPrinterFonts = wepfc.nCount;
315 dprintf_win16drv(stddeb, "Got %d fonts\n",wepfc.nCount);
317 if (wepfc.nCount > 0)
320 pLPD->paPrinterFonts = malloc(sizeof(PRINTER_FONTS_INFO) * wepfc.nCount);
322 /* Now get all of the fonts */
323 wepfc.nMode = 2;
324 wepfc.nCount = 0;
325 PRTDRV_EnumDFonts(physDev->segptrPDEVICE, NULL, pfnCallback,
326 (void *)&wepfc);
331 /* Select the first font into the DC */
332 /* Set up the logfont */
333 memcpy(&physDev->lf,
334 &pLPD->paPrinterFonts[0].lf,
335 sizeof(LOGFONT16));
337 /* Set up the textmetrics */
338 memcpy(&physDev->tm,
339 &pLPD->paPrinterFonts[0].tm,
340 sizeof(TEXTMETRIC16));
342 #ifdef SUPPORT_REALIZED_FONTS
343 /* TTD should calculate this */
344 InitTextXForm(&TextXForm);
346 /* First get the size of the realized font */
347 nSize = PRTDRV_RealizeObject(physDev->segptrPDEVICE, OBJ_FONT,
348 &pLPD->paPrinterFonts[0], NULL,
349 NULL);
351 physDev->segptrFontInfo = WIN16_GlobalLock16(GlobalAlloc16(GHND, nSize));
352 /* Realize the font */
353 PRTDRV_RealizeObject(physDev->segptrPDEVICE, OBJ_FONT,
354 &pLPD->paPrinterFonts[0],
355 (LPVOID)physDev->segptrFontInfo,
356 &TextXForm);
357 /* Quick look at structure */
358 if (physDev->segptrFontInfo)
360 FONTINFO16 *p = (FONTINFO16 *)PTR_SEG_TO_LIN(physDev->segptrFontInfo);
362 dprintf_win16drv(stddeb, "T:%d VR:%d HR:%d, F:%d L:%d\n",
363 p->dfType,
364 p->dfVertRes, p->dfHorizRes,
365 p->dfFirstCHAR, p->dfLastCHAR
369 #endif
370 /* TTD Lots more to do here */
371 InitDrawMode(lpDrawMode);
373 return TRUE;
378 * Escape (GDI.38)
380 static INT32 WIN16DRV_Escape( DC *dc, INT32 nEscape, INT32 cbInput,
381 SEGPTR lpInData, SEGPTR lpOutData )
383 WIN16DRV_PDEVICE *physDev = (WIN16DRV_PDEVICE *)dc->physDev;
384 int nRet = 0;
386 /* We should really process the nEscape parameter, but for now just
387 pass it all to the driver */
388 if (dc != NULL && physDev->segptrPDEVICE != 0)
390 switch(nEscape)
392 case 0x9:
393 printf("Escape: SetAbortProc ignored\n");
394 break;
395 case 0x100:
397 LPEXTTEXTDATA textData = PTR_SEG_TO_LIN(lpInData);
398 printf("Got in data 0x%lx textData 0x%p\n",lpInData, textData);
399 printf("size %d in 0x%p:0x%p font 0x%p:0x%p xform 0x%p:0x%p drawm 0x%p:0x%p\n",
400 textData->nSize,
401 textData->lpindata,PTR_SEG_TO_LIN(textData->lpindata),
402 textData->lpFont,PTR_SEG_TO_LIN(textData->lpFont),
403 textData->lpXForm,PTR_SEG_TO_LIN(textData->lpXForm),
404 textData->lpDrawMode,PTR_SEG_TO_LIN(textData->lpDrawMode));
406 break;
407 default:
408 nRet = PRTDRV_Control(physDev->segptrPDEVICE, nEscape,
409 lpInData, lpOutData);
412 else
413 fprintf(stderr, "Escape(nEscape = %04x)\n", nEscape);
414 return nRet;
420 /****************** misc. printer releated functions */
423 * The following function should implement a queing system
425 #ifndef HPQ
426 #define HPQ WORD
427 #endif
428 struct hpq
430 struct hpq *next;
431 int tag;
432 int key;
435 static struct hpq *hpqueue;
437 HPQ
438 CreatePQ(int size)
440 printf("CreatePQ: %d\n",size);
441 return 1;
443 int
444 DeletePQ(HPQ hPQ)
446 printf("DeletePQ: %x\n", hPQ);
447 return 0;
449 int
450 ExtractPQ(HPQ hPQ)
452 struct hpq *queue, *prev, *current, *currentPrev;
453 int key = 0, tag = -1;
454 currentPrev = prev = NULL;
455 queue = current = hpqueue;
456 if (current)
457 key = current->key;
459 while (current)
461 currentPrev = current;
462 current = current->next;
463 if (current)
465 if (current->key < key)
467 queue = current;
468 prev = currentPrev;
472 if (queue)
474 tag = queue->tag;
476 if (prev)
477 prev->next = queue->next;
478 else
479 hpqueue = queue->next;
480 free(queue);
483 printf("ExtractPQ: %x got tag %d key %d\n", hPQ, tag, key);
485 return tag;
488 int
489 InsertPQ(HPQ hPQ, int tag, int key)
491 struct hpq *queueItem = malloc(sizeof(struct hpq));
492 queueItem->next = hpqueue;
493 hpqueue = queueItem;
494 queueItem->key = key;
495 queueItem->tag = tag;
497 printf("InsertPQ: %x %d %d\n", hPQ, tag, key);
498 return TRUE;
501 MinPQ(HPQ hPQ)
503 printf("MinPQ: %x\n", hPQ);
504 return 0;
507 SizePQ(HPQ hPQ, int sizechange)
509 printf("SizePQ: %x %d\n", hPQ, sizechange);
510 return -1;
514 * The following functions implement part of the spooling process to
515 * print manager. I would like to see wine have a version of print managers
516 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
517 * now.
519 typedef struct PRINTJOB
521 char *pszOutput;
522 char *pszTitle;
523 HDC16 hDC;
524 HANDLE16 hHandle;
525 int nIndex;
526 int fd;
527 } PRINTJOB, *PPRINTJOB;
529 #define MAX_PRINT_JOBS 1
530 #define SP_OK 1
532 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
535 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
537 return gPrintJobsTable[0];
540 /* TTD Need to do some DOS->UNIX file conversion here */
541 static int CreateSpoolFile(LPSTR pszOutput)
543 int fd;
544 char szSpoolFile[32];
546 /* TTD convert the 'output device' into a spool file name */
548 if (pszOutput == NULL || *pszOutput == '\0')
549 strcpy(szSpoolFile,"lp.out");
550 else
551 strcpy(szSpoolFile, pszOutput);
553 if ((fd = open(szSpoolFile, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
555 printf("Failed to create spool file %s, errno = %d\n", szSpoolFile, errno);
557 return fd;
560 static int FreePrintJob(HANDLE16 hJob)
562 int nRet = SP_ERROR;
563 PPRINTJOB pPrintJob;
565 pPrintJob = FindPrintJobFromHandle(hJob);
566 if (pPrintJob != NULL)
568 gPrintJobsTable[pPrintJob->nIndex] = NULL;
569 free(pPrintJob->pszOutput);
570 free(pPrintJob->pszTitle);
571 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
572 free(pPrintJob);
573 nRet = SP_OK;
575 return nRet;
578 HANDLE16 OpenJob(LPSTR lpOutput, LPSTR lpTitle, HDC16 hDC)
580 HANDLE16 hHandle = SP_ERROR;
581 PPRINTJOB pPrintJob;
583 dprintf_win16drv(stddeb, "OpenJob: \"%s\" \"%s\" %04x\n", lpOutput, lpTitle, hDC);
585 pPrintJob = gPrintJobsTable[0];
586 if (pPrintJob == NULL)
588 int fd;
590 /* Try an create a spool file */
591 fd = CreateSpoolFile(lpOutput);
592 if (fd >= 0)
594 hHandle = 1;
596 pPrintJob = malloc(sizeof(PRINTJOB));
597 memset(pPrintJob, 0, sizeof(PRINTJOB));
599 pPrintJob->pszOutput = strdup(lpOutput);
600 pPrintJob->pszTitle = strdup(lpTitle);
601 pPrintJob->hDC = hDC;
602 pPrintJob->fd = fd;
603 pPrintJob->nIndex = 0;
604 pPrintJob->hHandle = hHandle;
605 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
608 dprintf_win16drv(stddeb, "OpenJob: return %04x\n", hHandle);
609 return hHandle;
612 int CloseJob(HANDLE16 hJob)
614 int nRet = SP_ERROR;
615 PPRINTJOB pPrintJob = NULL;
617 dprintf_win16drv(stddeb, "CloseJob: %04x\n", hJob);
619 pPrintJob = FindPrintJobFromHandle(hJob);
620 if (pPrintJob != NULL)
622 /* Close the spool file */
623 close(pPrintJob->fd);
624 FreePrintJob(hJob);
625 nRet = 1;
627 return nRet;
630 int WriteSpool(HANDLE16 hJob, LPSTR lpData, WORD cch)
632 int nRet = SP_ERROR;
633 PPRINTJOB pPrintJob = NULL;
635 dprintf_win16drv(stddeb, "WriteSpool: %04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
637 pPrintJob = FindPrintJobFromHandle(hJob);
638 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
640 if (write(pPrintJob->fd, lpData, cch) != cch)
641 nRet = SP_OUTOFDISK;
642 else
643 nRet = cch;
645 return nRet;
648 int WriteDialog(HANDLE16 hJob, LPSTR lpMsg, WORD cchMsg)
650 int nRet = 0;
652 dprintf_win16drv(stddeb, "WriteDialog: %04x %04x \"%s\"\n", hJob, cchMsg, lpMsg);
654 nRet = MessageBox16(0, lpMsg, "Printing Error", MB_OKCANCEL);
655 return nRet;
658 int DeleteJob(HANDLE16 hJob, WORD wNotUsed)
660 int nRet;
662 dprintf_win16drv(stddeb, "DeleteJob: %04x\n", hJob);
664 nRet = FreePrintJob(hJob);
665 return nRet;
669 * The following two function would allow a page to be sent to the printer
670 * when it has been processed. For simplicity they havn't been implemented.
671 * This means a whole job has to be processed before it is sent to the printer.
673 int StartSpoolPage(HANDLE16 hJob)
675 dprintf_win16drv(stddeb, "StartSpoolPage GDI.246 unimplemented\n");
676 return 1;
679 int EndSpoolPage(HANDLE16 hJob)
681 dprintf_win16drv(stddeb, "EndSpoolPage GDI.247 unimplemented\n");
682 return 1;
686 DWORD GetSpoolJob(int nOption, LONG param)
688 DWORD retval = 0;
689 dprintf_win16drv(stddeb, "In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
690 return retval;