blob: e4af5a65ccc2f79f87fa11d243b3cda72e828a58 [file] [log] [blame]
Alexandre Julliard3db94ef1997-09-28 17:43:24 +00001/*
2 * Driver Environment functions
3 *
4 * Note: This has NOTHING to do with the task/process environment!
5 *
6 * Copyright 1997 Marcus Meissner
Alexandre Julliardf90efa91998-06-14 15:24:15 +00007 * Copyright 1998 Andreas Mohr
Alexandre Julliard3db94ef1997-09-28 17:43:24 +00008 */
9#include <stdio.h>
Alexandre Julliardf90efa91998-06-14 15:24:15 +000010#include <string.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000011#include "config.h"
12#include "gdi.h"
Alexandre Julliard61fece01999-06-26 19:09:08 +000013#include "debugtools.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000014#include "heap.h"
Alexandre Julliard3db94ef1997-09-28 17:43:24 +000015
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000016DEFAULT_DEBUG_CHANNEL(gdi)
17
Alexandre Julliardf90efa91998-06-14 15:24:15 +000018typedef struct {
19 ATOM atom;
20 HGLOBAL16 handle;
21} ENVTABLE;
22
23static ENVTABLE EnvTable[20];
24
25static ENVTABLE *SearchEnvTable(ATOM atom)
26{
27 INT16 i;
28
Alexandre Julliard642d3131998-07-12 19:29:36 +000029 for (i = 19; i >= 0; i--) {
30 if (EnvTable[i].atom == atom)
31 return &EnvTable[i];
Alexandre Julliardf90efa91998-06-14 15:24:15 +000032 }
33 return NULL;
34}
35
36static ATOM GDI_GetNullPortAtom(void)
37{
38 static ATOM NullPortAtom = 0;
39 if (!NullPortAtom)
40 {
41 char NullPort[256];
42
Alexandre Julliarda3960291999-02-26 11:11:13 +000043 GetProfileStringA( "windows", "nullport", "none",
Alexandre Julliardf90efa91998-06-14 15:24:15 +000044 NullPort, sizeof(NullPort) );
Alexandre Julliarda3960291999-02-26 11:11:13 +000045 NullPortAtom = AddAtomA( NullPort );
Alexandre Julliardf90efa91998-06-14 15:24:15 +000046 }
47 return NullPortAtom;
48}
49
50static ATOM PortNameToAtom(LPCSTR lpPortName, BOOL16 add)
51{
Alexandre Julliard642d3131998-07-12 19:29:36 +000052 char *p;
Alexandre Julliarda3960291999-02-26 11:11:13 +000053 BOOL needfree = FALSE;
Alexandre Julliard642d3131998-07-12 19:29:36 +000054 ATOM ret;
Alexandre Julliardf90efa91998-06-14 15:24:15 +000055
56 if (lpPortName[strlen(lpPortName) - 1] == ':') {
Alexandre Julliard642d3131998-07-12 19:29:36 +000057 p = HEAP_strdupA(GetProcessHeap(), 0, lpPortName);
58 p[strlen(lpPortName) - 1] = '\0';
59 needfree = TRUE;
Alexandre Julliardf90efa91998-06-14 15:24:15 +000060 }
61 else
Alexandre Julliard642d3131998-07-12 19:29:36 +000062 p = (char *)lpPortName;
Alexandre Julliardf90efa91998-06-14 15:24:15 +000063
64 if (add)
Alexandre Julliarda3960291999-02-26 11:11:13 +000065 ret = AddAtomA(p);
Alexandre Julliardf90efa91998-06-14 15:24:15 +000066 else
Alexandre Julliarda3960291999-02-26 11:11:13 +000067 ret = FindAtomA(p);
Alexandre Julliard642d3131998-07-12 19:29:36 +000068
69 if(needfree) HeapFree(GetProcessHeap(), 0, p);
70
71 return ret;
Alexandre Julliardf90efa91998-06-14 15:24:15 +000072}
73
74
Alexandre Julliard3db94ef1997-09-28 17:43:24 +000075/***********************************************************************
76 * GetEnvironment (GDI.134)
77 */
Huw D M Daviese39b6761999-05-17 16:20:51 +000078INT16 WINAPI GetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nMaxSize)
Alexandre Julliard3db94ef1997-09-28 17:43:24 +000079{
Alexandre Julliardf90efa91998-06-14 15:24:15 +000080 ATOM atom;
81 LPCSTR p;
82 ENVTABLE *env;
83 WORD size;
84
Alexandre Julliard61fece01999-06-26 19:09:08 +000085 TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nMaxSize);
Alexandre Julliardf90efa91998-06-14 15:24:15 +000086
87 if (!(atom = PortNameToAtom(lpPortName, FALSE)))
88 return 0;
89 if (atom == GDI_GetNullPortAtom())
Alexandre Julliarda3960291999-02-26 11:11:13 +000090 if (!(atom = FindAtomA((LPCSTR)lpdev)))
Alexandre Julliardf90efa91998-06-14 15:24:15 +000091 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;
Alexandre Julliard3db94ef1997-09-28 17:43:24 +0000101}
102
Alexandre Julliardf90efa91998-06-14 15:24:15 +0000103
Alexandre Julliard3db94ef1997-09-28 17:43:24 +0000104/***********************************************************************
105 * SetEnvironment (GDI.132)
106 */
Huw D M Daviese39b6761999-05-17 16:20:51 +0000107INT16 WINAPI SetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nCount)
Alexandre Julliard3db94ef1997-09-28 17:43:24 +0000108{
Alexandre Julliardf90efa91998-06-14 15:24:15 +0000109 ATOM atom;
110 BOOL16 nullport = FALSE;
111 LPSTR p;
112 ENVTABLE *env;
113 HGLOBAL16 handle;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000114
Alexandre Julliard61fece01999-06-26 19:09:08 +0000115 TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nCount);
Alexandre Julliardf90efa91998-06-14 15:24:15 +0000116
117 if ((atom = PortNameToAtom(lpPortName, FALSE))) {
118 if (atom == GDI_GetNullPortAtom()) {
119 nullport = TRUE;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000120 atom = FindAtomA((LPCSTR)lpdev);
Alexandre Julliardf90efa91998-06-14 15:24:15 +0000121 }
122 env = SearchEnvTable(atom);
123 GlobalFree16(env->handle);
124 env->atom = 0;
125 }
126 if (nCount) { /* store DEVMODE struct */
127 if (nullport)
128 p = (LPSTR)lpdev;
129 else
130 p = (LPSTR)lpPortName;
131
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;
138 }
139 env->atom = atom;
140 env->handle = handle;
141 memcpy(p, lpdev, nCount);
142 GlobalUnlock16(handle);
143 return handle;
144 }
145 else return 0;
146 }
147 else return -1;
Alexandre Julliard3db94ef1997-09-28 17:43:24 +0000148}