Implemented OleCreatePropertyFrame and
[wine] / graphics / env.c
1 /* 
2  * Driver Environment functions
3  *
4  * Note: This has NOTHING to do with the task/process environment!
5  *
6  * Copyright 1997 Marcus Meissner
7  * Copyright 1998 Andreas Mohr
8  */
9
10 #include "config.h"
11
12 #include <stdio.h>
13 #include <string.h>
14
15 #include "gdi.h"
16 #include "debugtools.h"
17
18 DEFAULT_DEBUG_CHANNEL(gdi);
19
20 typedef struct {
21         ATOM atom;
22         HGLOBAL16 handle;
23 } ENVTABLE;
24
25 static ENVTABLE EnvTable[20];
26
27 static ENVTABLE *SearchEnvTable(ATOM atom)
28 {
29     INT16 i;
30     
31     for (i = 19; i >= 0; i--) {
32       if (EnvTable[i].atom == atom)
33         return &EnvTable[i];
34     }
35     return NULL;
36 }
37
38 static ATOM GDI_GetNullPortAtom(void)
39 {
40     static ATOM NullPortAtom = 0;
41     if (!NullPortAtom)
42     {
43         char NullPort[256];
44         
45         GetProfileStringA( "windows", "nullport", "none",
46                              NullPort, sizeof(NullPort) );
47         NullPortAtom = AddAtomA( NullPort );
48     }
49     return NullPortAtom;
50 }
51
52 static ATOM PortNameToAtom(LPCSTR lpPortName, BOOL16 add)
53 {
54     char buffer[256];
55
56     strncpy( buffer, lpPortName, sizeof(buffer) );
57     buffer[sizeof(buffer)-1] = 0;
58
59     if (buffer[0] && buffer[strlen(buffer)-1] == ':') buffer[strlen(buffer)-1] = 0;
60
61     if (add)
62         return AddAtomA(buffer);
63     else
64         return FindAtomA(buffer);
65 }
66
67
68 /***********************************************************************
69  *           GetEnvironment   (GDI.133)
70  */
71 INT16 WINAPI GetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nMaxSize)
72 {
73     ATOM atom;
74     LPCSTR p;
75     ENVTABLE *env;
76     WORD size;
77
78     TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nMaxSize);
79
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;
94 }
95
96
97 /***********************************************************************
98  *          SetEnvironment   (GDI.132)
99  */
100 INT16 WINAPI SetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nCount)
101 {
102     ATOM atom; 
103     BOOL16 nullport = FALSE;
104     LPSTR p;
105     ENVTABLE *env;
106     HGLOBAL16 handle;
107
108     TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nCount);
109
110     if ((atom = PortNameToAtom(lpPortName, FALSE))) {
111         if (atom == GDI_GetNullPortAtom()) {
112             nullport = TRUE;
113             atom = FindAtomA((LPCSTR)lpdev);
114         }
115         env = SearchEnvTable(atom);
116         GlobalFree16(env->handle);
117         env->atom = 0;
118     }
119     if (nCount) { /* store DEVMODE struct */
120         if (nullport)
121             p = (LPSTR)lpdev;
122         else
123             p = (LPSTR)lpPortName;
124
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;
131             }
132             env->atom = atom;
133             env->handle = handle;
134             memcpy(p, lpdev, nCount);
135             GlobalUnlock16(handle);
136             return handle;
137         }
138         else return 0;
139     }
140     else return -1;
141 }