Add config check and thread safe wrappers for the XRender library.
[wine.git] / graphics / env.c
blobd42f5f09c1fd1f3b70f9b3da1d08b97aaf047828
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"
15 DEFAULT_DEBUG_CHANNEL(gdi);
17 typedef struct {
18 ATOM atom;
19 HGLOBAL16 handle;
20 } ENVTABLE;
22 static ENVTABLE EnvTable[20];
24 static ENVTABLE *SearchEnvTable(ATOM atom)
26 INT16 i;
28 for (i = 19; i >= 0; i--) {
29 if (EnvTable[i].atom == atom)
30 return &EnvTable[i];
32 return NULL;
35 static ATOM GDI_GetNullPortAtom(void)
37 static ATOM NullPortAtom = 0;
38 if (!NullPortAtom)
40 char NullPort[256];
42 GetProfileStringA( "windows", "nullport", "none",
43 NullPort, sizeof(NullPort) );
44 NullPortAtom = AddAtomA( NullPort );
46 return NullPortAtom;
49 static ATOM PortNameToAtom(LPCSTR lpPortName, BOOL16 add)
51 char buffer[256];
53 strncpy( buffer, lpPortName, sizeof(buffer) );
54 buffer[sizeof(buffer)-1] = 0;
56 if (buffer[0] && buffer[strlen(buffer)-1] == ':') buffer[strlen(buffer)-1] = 0;
58 if (add)
59 return AddAtomA(buffer);
60 else
61 return FindAtomA(buffer);
65 /***********************************************************************
66 * GetEnvironment (GDI.133)
68 INT16 WINAPI GetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nMaxSize)
70 ATOM atom;
71 LPCSTR p;
72 ENVTABLE *env;
73 WORD size;
75 TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nMaxSize);
77 if (!(atom = PortNameToAtom(lpPortName, FALSE)))
78 return 0;
79 if (atom == GDI_GetNullPortAtom())
80 if (!(atom = FindAtomA((LPCSTR)lpdev)))
81 return 0;
82 if (!(env = SearchEnvTable(atom)))
83 return 0;
84 size = GlobalSize16(env->handle);
85 if (!lpdev) return 0;
86 if (size < nMaxSize) nMaxSize = size;
87 if (!(p = GlobalLock16(env->handle))) return 0;
88 memcpy(lpdev, p, nMaxSize);
89 GlobalUnlock16(env->handle);
90 return nMaxSize;
94 /***********************************************************************
95 * SetEnvironment (GDI.132)
97 INT16 WINAPI SetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nCount)
99 ATOM atom;
100 BOOL16 nullport = FALSE;
101 LPSTR p;
102 ENVTABLE *env;
103 HGLOBAL16 handle;
105 TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nCount);
107 if ((atom = PortNameToAtom(lpPortName, FALSE))) {
108 if (atom == GDI_GetNullPortAtom()) {
109 nullport = TRUE;
110 atom = FindAtomA((LPCSTR)lpdev);
112 env = SearchEnvTable(atom);
113 GlobalFree16(env->handle);
114 env->atom = 0;
116 if (nCount) { /* store DEVMODE struct */
117 if (nullport)
118 p = (LPSTR)lpdev;
119 else
120 p = (LPSTR)lpPortName;
122 if ((atom = PortNameToAtom(p, TRUE))
123 && (env = SearchEnvTable(0))
124 && (handle = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, nCount))) {
125 if (!(p = GlobalLock16(handle))) {
126 GlobalFree16(handle);
127 return 0;
129 env->atom = atom;
130 env->handle = handle;
131 memcpy(p, lpdev, nCount);
132 GlobalUnlock16(handle);
133 return handle;
135 else return 0;
137 else return -1;