1 /* 2 * Copyright 2004 Ivan Leo Puoti 3 * Copyright 2010 Christian Costa 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2.1 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * 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 #include "config.h" 21 #include "wine/port.h" 22 23 #include "initguid.h" 24 #include "d3drm_private.h" 25 26 /*********************************************************************** 27 * DllMain (D3DRM.@) 28 */ 29 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved) 30 { 31 switch(reason) 32 { 33 case DLL_WINE_PREATTACH: 34 return FALSE; /* prefer native version */ 35 case DLL_PROCESS_ATTACH: 36 DisableThreadLibraryCalls( inst ); 37 break; 38 } 39 return TRUE; 40 } 41 42 void d3drm_object_init(struct d3drm_object *object, const char *classname) 43 { 44 object->ref = 1; 45 object->appdata = 0; 46 list_init(&object->destroy_callbacks); 47 object->classname = classname; 48 object->name = NULL; 49 } 50 51 struct destroy_callback 52 { 53 struct list entry; 54 D3DRMOBJECTCALLBACK cb; 55 void *ctx; 56 }; 57 58 HRESULT d3drm_object_add_destroy_callback(struct d3drm_object *object, D3DRMOBJECTCALLBACK cb, void *ctx) 59 { 60 struct destroy_callback *callback; 61 62 if (!cb) 63 return D3DRMERR_BADVALUE; 64 65 if (!(callback = heap_alloc(sizeof(*callback)))) 66 return E_OUTOFMEMORY; 67 68 callback->cb = cb; 69 callback->ctx = ctx; 70 71 list_add_head(&object->destroy_callbacks, &callback->entry); 72 return D3DRM_OK; 73 } 74 75 HRESULT d3drm_object_delete_destroy_callback(struct d3drm_object *object, D3DRMOBJECTCALLBACK cb, void *ctx) 76 { 77 struct destroy_callback *callback; 78 79 if (!cb) 80 return D3DRMERR_BADVALUE; 81 82 LIST_FOR_EACH_ENTRY(callback, &object->destroy_callbacks, struct destroy_callback, entry) 83 { 84 if (callback->cb == cb && callback->ctx == ctx) 85 { 86 list_remove(&callback->entry); 87 heap_free(callback); 88 break; 89 } 90 } 91 92 return D3DRM_OK; 93 } 94 95 HRESULT d3drm_object_get_class_name(struct d3drm_object *object, DWORD *size, char *name) 96 { 97 DWORD req_size; 98 99 if (!size) 100 return E_INVALIDARG; 101 102 req_size = strlen(object->classname) + 1; 103 if (name && *size < req_size) 104 return E_INVALIDARG; 105 106 *size = req_size; 107 108 if (name) 109 memcpy(name, object->classname, req_size); 110 111 return D3DRM_OK; 112 } 113 114 HRESULT d3drm_object_get_name(struct d3drm_object *object, DWORD *size, char *name) 115 { 116 DWORD req_size; 117 118 if (!size) 119 return E_INVALIDARG; 120 121 req_size = object->name ? strlen(object->name) + 1 : 0; 122 if (name && *size < req_size) 123 return E_INVALIDARG; 124 125 if (name) 126 { 127 if (object->name) 128 memcpy(name, object->name, req_size); 129 else if (*size) 130 *name = 0; 131 } 132 133 *size = req_size; 134 135 return D3DRM_OK; 136 } 137 138 HRESULT d3drm_object_set_name(struct d3drm_object *object, const char *name) 139 { 140 DWORD req_size; 141 142 heap_free(object->name); 143 object->name = NULL; 144 145 if (name) 146 { 147 req_size = strlen(name) + 1; 148 if (!(object->name = heap_alloc(req_size))) 149 return E_OUTOFMEMORY; 150 memcpy(object->name, name, req_size); 151 } 152 153 return D3DRM_OK; 154 } 155 156 void d3drm_object_cleanup(IDirect3DRMObject *iface, struct d3drm_object *object) 157 { 158 struct destroy_callback *callback, *callback2; 159 160 LIST_FOR_EACH_ENTRY_SAFE(callback, callback2, &object->destroy_callbacks, struct destroy_callback, entry) 161 { 162 callback->cb(iface, callback->ctx); 163 list_remove(&callback->entry); 164 heap_free(callback); 165 } 166 167 heap_free(object->name); 168 object->name = NULL; 169 } 170