Adapted to cursor/icon handling changes.
[wine/multimedia.git] / graphics / env.c
blobd788f7ea6cf58e3ab6f1e9a6ac321647d356646e
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 "debug.h"
14 #include "heap.h"
16 typedef struct {
17 ATOM atom;
18 HGLOBAL16 handle;
19 } ENVTABLE;
21 static ENVTABLE EnvTable[20];
23 static ENVTABLE *SearchEnvTable(ATOM atom)
25 INT16 i;
27 for (i = 19; i >= 0; i--) {
28 if (EnvTable[i].atom == atom)
29 return &EnvTable[i];
31 return NULL;
34 static ATOM GDI_GetNullPortAtom(void)
36 static ATOM NullPortAtom = 0;
37 if (!NullPortAtom)
39 char NullPort[256];
41 GetProfileStringA( "windows", "nullport", "none",
42 NullPort, sizeof(NullPort) );
43 NullPortAtom = AddAtomA( NullPort );
45 return NullPortAtom;
48 static ATOM PortNameToAtom(LPCSTR lpPortName, BOOL16 add)
50 char *p;
51 BOOL needfree = FALSE;
52 ATOM ret;
54 if (lpPortName[strlen(lpPortName) - 1] == ':') {
55 p = HEAP_strdupA(GetProcessHeap(), 0, lpPortName);
56 p[strlen(lpPortName) - 1] = '\0';
57 needfree = TRUE;
59 else
60 p = (char *)lpPortName;
62 if (add)
63 ret = AddAtomA(p);
64 else
65 ret = FindAtomA(p);
67 if(needfree) HeapFree(GetProcessHeap(), 0, p);
69 return ret;
73 /***********************************************************************
74 * GetEnvironment (GDI.134)
76 INT16 WINAPI GetEnvironment16(LPCSTR lpPortName, LPDEVMODE16 lpdev, UINT16 nMaxSize)
78 ATOM atom;
79 LPCSTR p;
80 ENVTABLE *env;
81 WORD size;
83 TRACE(gdi, "('%s', %p, %d)\n", lpPortName, lpdev, nMaxSize);
85 if (!(atom = PortNameToAtom(lpPortName, FALSE)))
86 return 0;
87 if (atom == GDI_GetNullPortAtom())
88 if (!(atom = FindAtomA((LPCSTR)lpdev)))
89 return 0;
90 if (!(env = SearchEnvTable(atom)))
91 return 0;
92 size = GlobalSize16(env->handle);
93 if (!lpdev) return 0;
94 if (size < nMaxSize) nMaxSize = size;
95 if (!(p = GlobalLock16(env->handle))) return 0;
96 memcpy(lpdev, p, nMaxSize);
97 GlobalUnlock16(env->handle);
98 return nMaxSize;
102 /***********************************************************************
103 * SetEnvironment (GDI.132)
105 INT16 WINAPI SetEnvironment16(LPCSTR lpPortName, LPDEVMODE16 lpdev, UINT16 nCount)
107 ATOM atom;
108 BOOL16 nullport = FALSE;
109 LPSTR p;
110 ENVTABLE *env;
111 HGLOBAL16 handle;
113 TRACE(gdi, "('%s', %p, %d)\n", lpPortName, lpdev, nCount);
115 if ((atom = PortNameToAtom(lpPortName, FALSE))) {
116 if (atom == GDI_GetNullPortAtom()) {
117 nullport = TRUE;
118 atom = FindAtomA((LPCSTR)lpdev);
120 env = SearchEnvTable(atom);
121 GlobalFree16(env->handle);
122 env->atom = 0;
124 if (nCount) { /* store DEVMODE struct */
125 if (nullport)
126 p = (LPSTR)lpdev;
127 else
128 p = (LPSTR)lpPortName;
130 if ((atom = PortNameToAtom(p, TRUE))
131 && (env = SearchEnvTable(0))
132 && (handle = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, nCount))) {
133 if (!(p = GlobalLock16(handle))) {
134 GlobalFree16(handle);
135 return 0;
137 env->atom = atom;
138 env->handle = handle;
139 memcpy(p, lpdev, nCount);
140 GlobalUnlock16(handle);
141 return handle;
143 else return 0;
145 else return -1;