env.c 3.15 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 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
Alexandre Julliard's avatar
Alexandre Julliard committed
8 9
 */
#include <stdio.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
10
#include <string.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
11 12
#include "config.h"
#include "gdi.h"
13
#include "debugtools.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
14

15
DEFAULT_DEBUG_CHANNEL(gdi);
16

Alexandre Julliard's avatar
Alexandre Julliard committed
17 18 19 20 21 22 23 24 25 26 27
typedef struct {
        ATOM atom;
	HGLOBAL16 handle;
} ENVTABLE;

static ENVTABLE EnvTable[20];

static ENVTABLE *SearchEnvTable(ATOM atom)
{
    INT16 i;
    
Alexandre Julliard's avatar
Alexandre Julliard committed
28 29 30
    for (i = 19; i >= 0; i--) {
      if (EnvTable[i].atom == atom)
	return &EnvTable[i];
Alexandre Julliard's avatar
Alexandre Julliard committed
31 32 33 34 35 36 37 38 39 40 41
    }
    return NULL;
}

static ATOM GDI_GetNullPortAtom(void)
{
    static ATOM NullPortAtom = 0;
    if (!NullPortAtom)
    {
        char NullPort[256];
        
42
        GetProfileStringA( "windows", "nullport", "none",
Alexandre Julliard's avatar
Alexandre Julliard committed
43
                             NullPort, sizeof(NullPort) );
44
        NullPortAtom = AddAtomA( NullPort );
Alexandre Julliard's avatar
Alexandre Julliard committed
45 46 47 48 49 50
    }
    return NullPortAtom;
}

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

53 54 55 56
    strncpy( buffer, lpPortName, sizeof(buffer) );
    buffer[sizeof(buffer)-1] = 0;

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

    if (add)
59
        return AddAtomA(buffer);
Alexandre Julliard's avatar
Alexandre Julliard committed
60
    else
61
        return FindAtomA(buffer);
Alexandre Julliard's avatar
Alexandre Julliard committed
62 63 64
}


Alexandre Julliard's avatar
Alexandre Julliard committed
65
/***********************************************************************
66
 *           GetEnvironment   (GDI.133)
Alexandre Julliard's avatar
Alexandre Julliard committed
67
 */
68
INT16 WINAPI GetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nMaxSize)
Alexandre Julliard's avatar
Alexandre Julliard committed
69
{
Alexandre Julliard's avatar
Alexandre Julliard committed
70 71 72 73 74
    ATOM atom;
    LPCSTR p;
    ENVTABLE *env;
    WORD size;

75
    TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nMaxSize);
Alexandre Julliard's avatar
Alexandre Julliard committed
76 77 78 79

    if (!(atom = PortNameToAtom(lpPortName, FALSE)))
	return 0;
    if (atom == GDI_GetNullPortAtom())
80
	if (!(atom = FindAtomA((LPCSTR)lpdev)))
Alexandre Julliard's avatar
Alexandre Julliard committed
81 82 83 84 85 86 87 88 89 90
	    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
91 92
}

Alexandre Julliard's avatar
Alexandre Julliard committed
93

Alexandre Julliard's avatar
Alexandre Julliard committed
94 95 96
/***********************************************************************
 *          SetEnvironment   (GDI.132)
 */
97
INT16 WINAPI SetEnvironment16(LPCSTR lpPortName, LPDEVMODEA lpdev, UINT16 nCount)
Alexandre Julliard's avatar
Alexandre Julliard committed
98
{
Alexandre Julliard's avatar
Alexandre Julliard committed
99 100 101 102 103 104
    ATOM atom; 
    BOOL16 nullport = FALSE;
    LPSTR p;
    ENVTABLE *env;
    HGLOBAL16 handle;

105
    TRACE("('%s', %p, %d)\n", lpPortName, lpdev, nCount);
Alexandre Julliard's avatar
Alexandre Julliard committed
106 107 108 109

    if ((atom = PortNameToAtom(lpPortName, FALSE))) {
	if (atom == GDI_GetNullPortAtom()) {
	    nullport = TRUE;
110
	    atom = FindAtomA((LPCSTR)lpdev);
Alexandre Julliard's avatar
Alexandre Julliard committed
111 112 113 114 115 116 117 118 119 120
	}
	env = SearchEnvTable(atom);
        GlobalFree16(env->handle);
        env->atom = 0;
    }
    if (nCount) { /* store DEVMODE struct */
	if (nullport)
	    p = (LPSTR)lpdev;
        else
	    p = (LPSTR)lpPortName;
Alexandre Julliard's avatar
Alexandre Julliard committed
121

Alexandre Julliard's avatar
Alexandre Julliard committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        if ((atom = PortNameToAtom(p, TRUE))
	 && (env = SearchEnvTable(0))
	 && (handle = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, nCount))) {
	    if (!(p = GlobalLock16(handle))) {
	        GlobalFree16(handle);
	        return 0;
	    }
	    env->atom = atom;
	    env->handle = handle;
            memcpy(p, lpdev, nCount);
            GlobalUnlock16(handle);
            return handle;
	}
	else return 0;
    }
    else return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
138
}