d3drm_main.c 4.22 KB
Newer Older
1
/*
2
 * Copyright 2004 Ivan Leo Puoti
3
 * Copyright 2010 Christian Costa
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 19
 */

20 21
#include "config.h"
#include "wine/port.h"
22

23
#include "initguid.h"
24
#include "d3drm_private.h"
25 26 27 28

/***********************************************************************
 *		DllMain  (D3DRM.@)
 */
Henri Verbeet's avatar
Henri Verbeet committed
29
BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
30 31 32 33 34 35 36 37 38 39 40
{
    switch(reason)
    {
    case DLL_WINE_PREATTACH:
        return FALSE;  /* prefer native version */
    case DLL_PROCESS_ATTACH:
        DisableThreadLibraryCalls( inst );
        break;
    }
    return TRUE;
}
41

42
void d3drm_object_init(struct d3drm_object *object, const char *classname)
43 44 45 46
{
    object->ref = 1;
    object->appdata = 0;
    list_init(&object->destroy_callbacks);
47
    object->classname = classname;
48
    object->name = NULL;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
}

struct destroy_callback
{
    struct list entry;
    D3DRMOBJECTCALLBACK cb;
    void *ctx;
};

HRESULT d3drm_object_add_destroy_callback(struct d3drm_object *object, D3DRMOBJECTCALLBACK cb, void *ctx)
{
    struct destroy_callback *callback;

    if (!cb)
        return D3DRMERR_BADVALUE;

    callback = HeapAlloc(GetProcessHeap(), 0, sizeof(*callback));
    if (!callback)
        return E_OUTOFMEMORY;

    callback->cb = cb;
    callback->ctx = ctx;

    list_add_head(&object->destroy_callbacks, &callback->entry);
    return D3DRM_OK;
}

HRESULT d3drm_object_delete_destroy_callback(struct d3drm_object *object, D3DRMOBJECTCALLBACK cb, void *ctx)
{
78
    struct destroy_callback *callback;
79 80 81 82

    if (!cb)
        return D3DRMERR_BADVALUE;

83
    LIST_FOR_EACH_ENTRY(callback, &object->destroy_callbacks, struct destroy_callback, entry)
84 85 86 87 88
    {
        if (callback->cb == cb && callback->ctx == ctx)
        {
            list_remove(&callback->entry);
            HeapFree(GetProcessHeap(), 0, callback);
89
            break;
90 91 92 93 94 95
        }
    }

    return D3DRM_OK;
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
HRESULT d3drm_object_get_class_name(struct d3drm_object *object, DWORD *size, char *name)
{
    DWORD req_size;

    if (!size)
        return E_INVALIDARG;

    req_size = strlen(object->classname) + 1;
    if (name && *size < req_size)
        return E_INVALIDARG;

    *size = req_size;

    if (name)
        memcpy(name, object->classname, req_size);

    return D3DRM_OK;
}

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 156
HRESULT d3drm_object_get_name(struct d3drm_object *object, DWORD *size, char *name)
{
    DWORD req_size;

    if (!size)
        return E_INVALIDARG;

    req_size = object->name ? strlen(object->name) + 1 : 0;
    if (name && *size < req_size)
        return E_INVALIDARG;

    if (name)
    {
        if (object->name)
            memcpy(name, object->name, req_size);
        else if (*size)
            *name = 0;
    }

    *size = req_size;

    return D3DRM_OK;
}

HRESULT d3drm_object_set_name(struct d3drm_object *object, const char *name)
{
    DWORD req_size;

    HeapFree(GetProcessHeap(), 0, object->name);
    object->name = NULL;

    if (name)
    {
        req_size = strlen(name) + 1;
        if (!(object->name = HeapAlloc(GetProcessHeap(), 0, req_size)))
            return E_OUTOFMEMORY;
        memcpy(object->name, name, req_size);
    }

    return D3DRM_OK;
}

157 158 159 160 161 162 163 164 165 166
void d3drm_object_cleanup(IDirect3DRMObject *iface, struct d3drm_object *object)
{
    struct destroy_callback *callback, *callback2;

    LIST_FOR_EACH_ENTRY_SAFE(callback, callback2, &object->destroy_callbacks, struct destroy_callback, entry)
    {
        callback->cb(iface, callback->ctx);
        list_remove(&callback->entry);
        HeapFree(GetProcessHeap(), 0, callback);
    }
167 168 169

    HeapFree(GetProcessHeap(), 0, object->name);
    object->name = NULL;
170
}