env.c 3.99 KB
Newer Older
1
/*
Alexandre Julliard's avatar
Alexandre Julliard committed
2 3 4 5 6
 * Driver Environment functions
 *
 * Note: This has NOTHING to do with the task/process environment!
 *
 * Copyright 1997 Marcus Meissner
Alexandre Julliard's avatar
Alexandre Julliard committed
7
 * Copyright 1998 Andreas Mohr
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
22
 */
23 24 25

#include "config.h"

26
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
27
#include <stdio.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
28
#include <string.h>
29

30 31 32 33
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "wine/wingdi16.h"
34
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
35

36
WINE_DEFAULT_DEBUG_CHANNEL(gdi);
37

Alexandre Julliard's avatar
Alexandre Julliard committed
38 39 40 41 42 43 44 45 46 47
typedef struct {
        ATOM atom;
	HGLOBAL16 handle;
} ENVTABLE;

static ENVTABLE EnvTable[20];

static ENVTABLE *SearchEnvTable(ATOM atom)
{
    INT16 i;
48

Alexandre Julliard's avatar
Alexandre Julliard committed
49 50 51
    for (i = 19; i >= 0; i--) {
      if (EnvTable[i].atom == atom)
	return &EnvTable[i];
Alexandre Julliard's avatar
Alexandre Julliard committed
52 53 54 55 56 57 58 59 60 61
    }
    return NULL;
}

static ATOM GDI_GetNullPortAtom(void)
{
    static ATOM NullPortAtom = 0;
    if (!NullPortAtom)
    {
        char NullPort[256];
62

63
        GetProfileStringA( "windows", "nullport", "none",
Alexandre Julliard's avatar
Alexandre Julliard committed
64
                             NullPort, sizeof(NullPort) );
65
        NullPortAtom = AddAtomA( NullPort );
Alexandre Julliard's avatar
Alexandre Julliard committed
66 67 68 69 70 71
    }
    return NullPortAtom;
}

static ATOM PortNameToAtom(LPCSTR lpPortName, BOOL16 add)
{
72
    char buffer[256];
Alexandre Julliard's avatar
Alexandre Julliard committed
73

74
    lstrcpynA( buffer, lpPortName, sizeof(buffer) );
75 76

    if (buffer[0] && buffer[strlen(buffer)-1] == ':') buffer[strlen(buffer)-1] = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
77 78

    if (add)
79
        return AddAtomA(buffer);
Alexandre Julliard's avatar
Alexandre Julliard committed
80
    else
81
        return FindAtomA(buffer);
Alexandre Julliard's avatar
Alexandre Julliard committed
82 83 84
}


Alexandre Julliard's avatar
Alexandre Julliard committed
85
/***********************************************************************
86
 *           GetEnvironment   (GDI.133)
Alexandre Julliard's avatar
Alexandre Julliard committed
87
 */
88
INT16 WINAPI GetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nMaxSize)
Alexandre Julliard's avatar
Alexandre Julliard committed
89
{
Alexandre Julliard's avatar
Alexandre Julliard committed
90 91 92 93 94
    ATOM atom;
    LPCSTR p;
    ENVTABLE *env;
    WORD size;

95
    TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nMaxSize);
Alexandre Julliard's avatar
Alexandre Julliard committed
96 97 98 99

    if (!(atom = PortNameToAtom(lpPortName, FALSE)))
	return 0;
    if (atom == GDI_GetNullPortAtom())
100
	if (!(atom = FindAtomA((LPCSTR)lpdev)))
Alexandre Julliard's avatar
Alexandre Julliard committed
101 102 103 104 105 106 107 108 109 110
	    return 0;
    if (!(env = SearchEnvTable(atom)))
	return 0;
    size = GlobalSize16(env->handle);
    if (!lpdev) return 0;
    if (size < nMaxSize) nMaxSize = size;
    if (!(p = GlobalLock16(env->handle))) return 0;
    memcpy(lpdev, p, nMaxSize);
    GlobalUnlock16(env->handle);
    return nMaxSize;
Alexandre Julliard's avatar
Alexandre Julliard committed
111 112
}

Alexandre Julliard's avatar
Alexandre Julliard committed
113

Alexandre Julliard's avatar
Alexandre Julliard committed
114 115 116
/***********************************************************************
 *          SetEnvironment   (GDI.132)
 */
117
INT16 WINAPI SetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nCount)
Alexandre Julliard's avatar
Alexandre Julliard committed
118
{
119
    ATOM atom;
Alexandre Julliard's avatar
Alexandre Julliard committed
120
    BOOL16 nullport = FALSE;
121 122
    LPCSTR port_name;
    LPSTR device_mode;
Alexandre Julliard's avatar
Alexandre Julliard committed
123 124 125
    ENVTABLE *env;
    HGLOBAL16 handle;

126
    TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nCount);
Alexandre Julliard's avatar
Alexandre Julliard committed
127 128 129 130

    if ((atom = PortNameToAtom(lpPortName, FALSE))) {
	if (atom == GDI_GetNullPortAtom()) {
	    nullport = TRUE;
131
	    atom = FindAtomA((LPCSTR)lpdev);
Alexandre Julliard's avatar
Alexandre Julliard committed
132 133 134 135 136 137 138
	}
	env = SearchEnvTable(atom);
        GlobalFree16(env->handle);
        env->atom = 0;
    }
    if (nCount) { /* store DEVMODE struct */
	if (nullport)
139
	    port_name = (LPSTR)lpdev;
Alexandre Julliard's avatar
Alexandre Julliard committed
140
        else
141
	    port_name = lpPortName;
Alexandre Julliard's avatar
Alexandre Julliard committed
142

143
        if ((atom = PortNameToAtom(port_name, TRUE))
Alexandre Julliard's avatar
Alexandre Julliard committed
144 145
	 && (env = SearchEnvTable(0))
	 && (handle = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, nCount))) {
146
	    if (!(device_mode = GlobalLock16(handle))) {
Alexandre Julliard's avatar
Alexandre Julliard committed
147 148 149 150 151
	        GlobalFree16(handle);
	        return 0;
	    }
	    env->atom = atom;
	    env->handle = handle;
152
            memcpy(device_mode, lpdev, nCount);
Alexandre Julliard's avatar
Alexandre Julliard committed
153 154 155 156 157 158
            GlobalUnlock16(handle);
            return handle;
	}
	else return 0;
    }
    else return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
159
}