ctapi32.c 4.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * WINE ct-api wrapper
 *
 * Copyright 2007 Christian Eggers
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

21 22
#include "config.h"
#include "wine/port.h"
23
#include <string.h>
24 25 26 27 28
#include "wine/library.h"
#include "wine/debug.h"
#include "windef.h"
#include "winreg.h"
#include "winnls.h"
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#include "ctapi.h"

WINE_DEFAULT_DEBUG_CHANNEL(ctapi32);

#define FALLBACK_LIBCTAPI "libctapi.so"
static const WCHAR value_name[] = {'l','i','b','r','a','r','y',0};


static IS8 (*pCT_init)(IU16 ctn, IU16 pn) = NULL;
static IS8 (*pCT_data)(IU16 ctn, IU8 *dad, IU8 *sad, IU16 lenc, IU8 *command,
	IU16 *lenr, IU8 *response) = NULL;
static IS8 (*pCT_close)(IU16 ctn) = NULL;

static void *ctapi_handle = NULL;


static int load_functions(void) {
	char soname[MAX_PATH] = FALLBACK_LIBCTAPI, buffer[MAX_PATH];
	LONG result;
	HKEY key_handle;

	if (pCT_init) /* loaded already */
		return 0;

	/* Try to get name of low level library from registry */
        /* @@ Wine registry key: HKCU\Software\Wine\ctapi32 */
	result = RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Wine\\ctapi32", 0, KEY_READ, &key_handle);
	if (result == ERROR_SUCCESS) {
		DWORD type, size;
		WCHAR buffer_w[MAX_PATH];

		size = sizeof(buffer_w) - sizeof(WCHAR);  /* Leave space for null termination */
		result = RegQueryValueExW(key_handle, value_name, NULL, &type, (LPBYTE)buffer_w, &size);
		if ((result == ERROR_SUCCESS) && (type == REG_SZ)) {
			int len;

			/* Null termination */
			buffer_w[size / sizeof(WCHAR)] = '\0';
			len = WideCharToMultiByte(CP_UNIXCP, 0, buffer_w, -1, buffer, sizeof(buffer), NULL, NULL);
			if (len)
69
				memcpy(soname, buffer, len);
70 71 72 73 74 75 76 77 78 79
		}
		RegCloseKey(key_handle);
	}

	TRACE("Loading library '%s'\n", soname);
	ctapi_handle = wine_dlopen(soname, RTLD_NOW, NULL, 0);
	if (ctapi_handle) {
		TRACE("Successfully loaded '%s'\n", soname);
	}
	else {
80
		MESSAGE("Wine cannot find any usable hardware library, ctapi32.dll not working.\n");
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
		MESSAGE("Please create the key \"HKEY_CURRENT_USER\\Software\\Wine\\ctapi32\" in your registry\n");
		MESSAGE("and set the value \"library\" to your library name (e.g. \"libctapi-cyberjack.so.1\" or \"/usr/lib/readers/libctapi.so\").\n");
		return 1;
	}

#define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(ctapi_handle, #f, NULL, 0)) == NULL){WARN("Can't find symbol %s\n", #f); return 1;}
LOAD_FUNCPTR(CT_init);
LOAD_FUNCPTR(CT_data);
LOAD_FUNCPTR(CT_close);
#undef LOAD_FUNCPTR

	return 0;
}

static void unload_functions(void)
{
	pCT_close = NULL;
	pCT_data = NULL;
	pCT_init = NULL;
	if (ctapi_handle)
		wine_dlclose(ctapi_handle, NULL, 0);
}


/*
 *  ct-API specific functions
 */

IS8 WINAPI WIN_CT_init(IU16 ctn, IU16 pn)
{
	if (!pCT_init)
		return ERR_HOST;
	return pCT_init(ctn, pn);
}

IS8 WINAPI WIN_CT_data(IU16 ctn, IU8 *dad, IU8 *sad, IU16 lenc, IU8 *command, IU16 *lenr, IU8 *response)
{
	if (!pCT_data)
		return ERR_HOST;
	return pCT_data(ctn, dad, sad, lenc, command, lenr, response);
}

IS8 WINAPI WIN_CT_close(IU16 ctn)
{
	if (!pCT_close)
		return ERR_HOST;
	return pCT_close(ctn);
}

/*
 *  Dll Main function
 */
BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);

    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
        {
            DisableThreadLibraryCalls(hinstDLL);
            /* Try to load low-level library */
            if (load_functions() != 0)
		return FALSE;  /* error */
            break;
        }
        case DLL_PROCESS_DETACH:
        {
            unload_functions();
            break;
        }
    }

    return TRUE;
}