Protect the backslashes we mean to have show from groff.
[wine.git] / graphics / env.c
blobe4af5a65ccc2f79f87fa11d243b3cda72e828a58
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 */
9 #include <stdio.h>
10 #include <string.h>
11 #include "config.h"
12 #include "gdi.h"
13 #include "debugtools.h"
14 #include "heap.h"
16 DEFAULT_DEBUG_CHANNEL(gdi)
18 typedef struct {
19 ATOM atom;
20 HGLOBAL16 handle;
21 } ENVTABLE;
23 static ENVTABLE EnvTable[20];
25 static ENVTABLE *SearchEnvTable(ATOM atom)
27 INT16 i;
29 for (i = 19; i >= 0; i--) {
30 if (EnvTable[i].atom == atom)
31 return &EnvTable[i];
33 return NULL;
36 static ATOM GDI_GetNullPortAtom(void)
38 static ATOM NullPortAtom = 0;
39 if (!NullPortAtom)
41 char NullPort[256];
43 GetProfileStringA( "windows", "nullport", "none",
44 NullPort, sizeof(NullPort) );
45 NullPortAtom = AddAtomA( NullPort );
47 return NullPortAtom;
50 static ATOM PortNameToAtom(LPCSTR lpPortName, BOOL16 add)
52 char *p;
53 BOOL needfree = FALSE;
54 ATOM ret;
56 if (lpPortName[strlen(lpPortName) - 1] == ':') {
57 p = HEAP_strdupA(GetProcessHeap(), 0, lpPortName);
58 p[strlen(lpPortName) - 1] = '\0';
59 needfree = TRUE;
61 else
62 p = (char *)lpPortName;
64 if (add)
65 ret = AddAtomA(p);
66 else
67 ret = FindAtomA(p);
69 if(needfree) HeapFree(GetProcessHeap(), 0, p);
71 return ret;
75 /***********************************************************************
76 * GetEnvironment (GDI.134)
78 INT16 WINAPI GetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nMaxSize)
80 ATOM atom;
81 LPCSTR p;
82 ENVTABLE *env;
83 WORD size;
85 TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nMaxSize);
87 if (!(atom = PortNameToAtom(lpPortName, FALSE)))
88 return 0;
89 if (atom == GDI_GetNullPortAtom())
90 if (!(atom = FindAtomA((LPCSTR)lpdev)))
91 return 0;
92 if (!(env = SearchEnvTable(atom)))
93 return 0;
94 size = GlobalSize16(env->handle);
95 if (!lpdev) return 0;
96 if (size < nMaxSize) nMaxSize = size;
97 if (!(p = GlobalLock16(env->handle))) return 0;
98 memcpy(lpdev, p, nMaxSize);
99 GlobalUnlock16(env->handle);
100 return nMaxSize;
104 /***********************************************************************
105 * SetEnvironment (GDI.132)
107 INT16 WINAPI SetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nCount)
109 ATOM atom;
110 BOOL16 nullport = FALSE;
111 LPSTR p;
112 ENVTABLE *env;
113 HGLOBAL16 handle;
115 TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nCount);
117 if ((atom = PortNameToAtom(lpPortName, FALSE))) {
118 if (atom == GDI_GetNullPortAtom()) {
119 nullport = TRUE;
120 atom = FindAtomA((LPCSTR)lpdev);
122 env = SearchEnvTable(atom);
123 GlobalFree16(env->handle);
124 env->atom = 0;
126 if (nCount) { /* store DEVMODE struct */
127 if (nullport)
128 p = (LPSTR)lpdev;
129 else
130 p = (LPSTR)lpPortName;
132 if ((atom = PortNameToAtom(p, TRUE))
133 && (env = SearchEnvTable(0))
134 && (handle = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, nCount))) {
135 if (!(p = GlobalLock16(handle))) {
136 GlobalFree16(handle);
137 return 0;
139 env->atom = atom;
140 env->handle = handle;
141 memcpy(p, lpdev, nCount);
142 GlobalUnlock16(handle);
143 return handle;
145 else return 0;
147 else return -1;