Fix compilation errors on FreeBSD.
[wine/dibdrv.git] / graphics / env.c
blob9180248ded81f5fa355fbf61ec5b5810efb049c8
1 /*
2 * Driver Environment functions
4 * Note: This has NOTHING to do with the task/process environment!
6 * Copyright 1997 Marcus Meissner
7 * Copyright 1998 Andreas Mohr
8 */
10 #include "config.h"
12 #include <stdio.h>
13 #include <string.h>
15 #include "gdi.h"
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(gdi);
20 typedef struct {
21 ATOM atom;
22 HGLOBAL16 handle;
23 } ENVTABLE;
25 static ENVTABLE EnvTable[20];
27 static ENVTABLE *SearchEnvTable(ATOM atom)
29 INT16 i;
31 for (i = 19; i >= 0; i--) {
32 if (EnvTable[i].atom == atom)
33 return &EnvTable[i];
35 return NULL;
38 static ATOM GDI_GetNullPortAtom(void)
40 static ATOM NullPortAtom = 0;
41 if (!NullPortAtom)
43 char NullPort[256];
45 GetProfileStringA( "windows", "nullport", "none",
46 NullPort, sizeof(NullPort) );
47 NullPortAtom = AddAtomA( NullPort );
49 return NullPortAtom;
52 static ATOM PortNameToAtom(LPCSTR lpPortName, BOOL16 add)
54 char buffer[256];
56 strncpy( buffer, lpPortName, sizeof(buffer) );
57 buffer[sizeof(buffer)-1] = 0;
59 if (buffer[0] && buffer[strlen(buffer)-1] == ':') buffer[strlen(buffer)-1] = 0;
61 if (add)
62 return AddAtomA(buffer);
63 else
64 return FindAtomA(buffer);
68 /***********************************************************************
69 * GetEnvironment (GDI.133)
71 INT16 WINAPI GetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nMaxSize)
73 ATOM atom;
74 LPCSTR p;
75 ENVTABLE *env;
76 WORD size;
78 TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nMaxSize);
80 if (!(atom = PortNameToAtom(lpPortName, FALSE)))
81 return 0;
82 if (atom == GDI_GetNullPortAtom())
83 if (!(atom = FindAtomA((LPCSTR)lpdev)))
84 return 0;
85 if (!(env = SearchEnvTable(atom)))
86 return 0;
87 size = GlobalSize16(env->handle);
88 if (!lpdev) return 0;
89 if (size < nMaxSize) nMaxSize = size;
90 if (!(p = GlobalLock16(env->handle))) return 0;
91 memcpy(lpdev, p, nMaxSize);
92 GlobalUnlock16(env->handle);
93 return nMaxSize;
97 /***********************************************************************
98 * SetEnvironment (GDI.132)
100 INT16 WINAPI SetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nCount)
102 ATOM atom;
103 BOOL16 nullport = FALSE;
104 LPSTR p;
105 ENVTABLE *env;
106 HGLOBAL16 handle;
108 TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nCount);
110 if ((atom = PortNameToAtom(lpPortName, FALSE))) {
111 if (atom == GDI_GetNullPortAtom()) {
112 nullport = TRUE;
113 atom = FindAtomA((LPCSTR)lpdev);
115 env = SearchEnvTable(atom);
116 GlobalFree16(env->handle);
117 env->atom = 0;
119 if (nCount) { /* store DEVMODE struct */
120 if (nullport)
121 p = (LPSTR)lpdev;
122 else
123 p = (LPSTR)lpPortName;
125 if ((atom = PortNameToAtom(p, TRUE))
126 && (env = SearchEnvTable(0))
127 && (handle = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, nCount))) {
128 if (!(p = GlobalLock16(handle))) {
129 GlobalFree16(handle);
130 return 0;
132 env->atom = atom;
133 env->handle = handle;
134 memcpy(p, lpdev, nCount);
135 GlobalUnlock16(handle);
136 return handle;
138 else return 0;
140 else return -1;