xref: /reactos/dll/win32/mscms/handle.c (revision 8123ffac)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * MSCMS - Color Management System for Wine
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * Copyright 2004, 2005, 2008 Hans Leidekker
5c2c66affSColin Finck  *
6c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
7c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
8c2c66affSColin Finck  * License as published by the Free Software Foundation; either
9c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
10c2c66affSColin Finck  *
11c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
12c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14c2c66affSColin Finck  * Lesser General Public License for more details.
15c2c66affSColin Finck  *
16c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
17c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
18c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19c2c66affSColin Finck  */
20c2c66affSColin Finck 
21*8123ffacSAmine Khaldi #include "config.h"
22*8123ffacSAmine Khaldi #include "wine/debug.h"
23c2c66affSColin Finck 
24*8123ffacSAmine Khaldi #include <stdarg.h>
25c2c66affSColin Finck 
26*8123ffacSAmine Khaldi #include "windef.h"
27*8123ffacSAmine Khaldi #include "winbase.h"
28*8123ffacSAmine Khaldi #include "wingdi.h"
29*8123ffacSAmine Khaldi #include "winuser.h"
30*8123ffacSAmine Khaldi #include "icm.h"
31c2c66affSColin Finck 
32*8123ffacSAmine Khaldi #include "mscms_priv.h"
33c2c66affSColin Finck 
34c2c66affSColin Finck #ifdef HAVE_LCMS2
35c2c66affSColin Finck 
36c2c66affSColin Finck static CRITICAL_SECTION mscms_handle_cs;
37c2c66affSColin Finck static CRITICAL_SECTION_DEBUG mscms_handle_cs_debug =
38c2c66affSColin Finck {
39c2c66affSColin Finck     0, 0, &mscms_handle_cs,
40c2c66affSColin Finck     { &mscms_handle_cs_debug.ProcessLocksList,
41c2c66affSColin Finck       &mscms_handle_cs_debug.ProcessLocksList },
42c2c66affSColin Finck       0, 0, { (DWORD_PTR)(__FILE__ ": mscms_handle_cs") }
43c2c66affSColin Finck };
44c2c66affSColin Finck static CRITICAL_SECTION mscms_handle_cs = { &mscms_handle_cs_debug, -1, 0, 0, 0, 0 };
45c2c66affSColin Finck 
46c2c66affSColin Finck static struct profile *profiletable;
47c2c66affSColin Finck static struct transform *transformtable;
48c2c66affSColin Finck 
49c2c66affSColin Finck static unsigned int num_profile_handles;
50c2c66affSColin Finck static unsigned int num_transform_handles;
51c2c66affSColin Finck 
52c2c66affSColin Finck WINE_DEFAULT_DEBUG_CHANNEL(mscms);
53c2c66affSColin Finck 
free_handle_tables(void)54c2c66affSColin Finck void free_handle_tables( void )
55c2c66affSColin Finck {
56c2c66affSColin Finck     HeapFree( GetProcessHeap(), 0, profiletable );
57c2c66affSColin Finck     profiletable = NULL;
58c2c66affSColin Finck     num_profile_handles = 0;
59c2c66affSColin Finck 
60c2c66affSColin Finck     HeapFree( GetProcessHeap(), 0, transformtable );
61c2c66affSColin Finck     transformtable = NULL;
62c2c66affSColin Finck     num_transform_handles = 0;
63c2c66affSColin Finck 
64c2c66affSColin Finck     DeleteCriticalSection( &mscms_handle_cs );
65c2c66affSColin Finck }
66c2c66affSColin Finck 
grab_profile(HPROFILE handle)67c2c66affSColin Finck struct profile *grab_profile( HPROFILE handle )
68c2c66affSColin Finck {
69c2c66affSColin Finck     DWORD_PTR index;
70c2c66affSColin Finck 
71c2c66affSColin Finck     EnterCriticalSection( &mscms_handle_cs );
72c2c66affSColin Finck 
73c2c66affSColin Finck     index = (DWORD_PTR)handle - 1;
74c2c66affSColin Finck     if (index > num_profile_handles)
75c2c66affSColin Finck     {
76c2c66affSColin Finck         LeaveCriticalSection( &mscms_handle_cs );
77c2c66affSColin Finck         return NULL;
78c2c66affSColin Finck     }
79c2c66affSColin Finck     return &profiletable[index];
80c2c66affSColin Finck }
81c2c66affSColin Finck 
release_profile(struct profile * profile)82c2c66affSColin Finck void release_profile( struct profile *profile )
83c2c66affSColin Finck {
84c2c66affSColin Finck     LeaveCriticalSection( &mscms_handle_cs );
85c2c66affSColin Finck }
86c2c66affSColin Finck 
grab_transform(HTRANSFORM handle)87c2c66affSColin Finck struct transform *grab_transform( HTRANSFORM handle )
88c2c66affSColin Finck {
89c2c66affSColin Finck     DWORD_PTR index;
90c2c66affSColin Finck 
91c2c66affSColin Finck     EnterCriticalSection( &mscms_handle_cs );
92c2c66affSColin Finck 
93c2c66affSColin Finck     index = (DWORD_PTR)handle - 1;
94c2c66affSColin Finck     if (index > num_transform_handles)
95c2c66affSColin Finck     {
96c2c66affSColin Finck         LeaveCriticalSection( &mscms_handle_cs );
97c2c66affSColin Finck         return NULL;
98c2c66affSColin Finck     }
99c2c66affSColin Finck     return &transformtable[index];
100c2c66affSColin Finck }
101c2c66affSColin Finck 
release_transform(struct transform * transform)102c2c66affSColin Finck void release_transform( struct transform *transform )
103c2c66affSColin Finck {
104c2c66affSColin Finck     LeaveCriticalSection( &mscms_handle_cs );
105c2c66affSColin Finck }
106c2c66affSColin Finck 
alloc_profile_handle(void)107c2c66affSColin Finck static HPROFILE alloc_profile_handle( void )
108c2c66affSColin Finck {
109c2c66affSColin Finck     DWORD_PTR index;
110c2c66affSColin Finck     struct profile *p;
111c2c66affSColin Finck     unsigned int count = 128;
112c2c66affSColin Finck 
113c2c66affSColin Finck     for (index = 0; index < num_profile_handles; index++)
114c2c66affSColin Finck     {
115c2c66affSColin Finck         if (!profiletable[index].data) return (HPROFILE)(index + 1);
116c2c66affSColin Finck     }
117c2c66affSColin Finck     if (!profiletable)
118c2c66affSColin Finck     {
119c2c66affSColin Finck         p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, count * sizeof(struct profile) );
120c2c66affSColin Finck     }
121c2c66affSColin Finck     else
122c2c66affSColin Finck     {
123c2c66affSColin Finck         count = num_profile_handles * 2;
124c2c66affSColin Finck         p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, profiletable, count * sizeof(struct profile) );
125c2c66affSColin Finck     }
126c2c66affSColin Finck     if (!p) return NULL;
127c2c66affSColin Finck 
128c2c66affSColin Finck     profiletable = p;
129c2c66affSColin Finck     num_profile_handles = count;
130c2c66affSColin Finck 
131c2c66affSColin Finck     return (HPROFILE)(index + 1);
132c2c66affSColin Finck }
133c2c66affSColin Finck 
create_profile(struct profile * profile)134c2c66affSColin Finck HPROFILE create_profile( struct profile *profile )
135c2c66affSColin Finck {
136c2c66affSColin Finck     HPROFILE handle;
137c2c66affSColin Finck 
138c2c66affSColin Finck     EnterCriticalSection( &mscms_handle_cs );
139c2c66affSColin Finck 
140c2c66affSColin Finck     if ((handle = alloc_profile_handle()))
141c2c66affSColin Finck     {
142c2c66affSColin Finck         DWORD_PTR index = (DWORD_PTR)handle - 1;
143c2c66affSColin Finck         profiletable[index] = *profile;
144c2c66affSColin Finck     }
145c2c66affSColin Finck     LeaveCriticalSection( &mscms_handle_cs );
146c2c66affSColin Finck     return handle;
147c2c66affSColin Finck }
148c2c66affSColin Finck 
close_profile(HPROFILE handle)149c2c66affSColin Finck BOOL close_profile( HPROFILE handle )
150c2c66affSColin Finck {
151c2c66affSColin Finck     DWORD_PTR index;
152c2c66affSColin Finck     struct profile *profile;
153c2c66affSColin Finck 
154c2c66affSColin Finck     EnterCriticalSection( &mscms_handle_cs );
155c2c66affSColin Finck 
156c2c66affSColin Finck     index = (DWORD_PTR)handle - 1;
157c2c66affSColin Finck     if (index > num_profile_handles)
158c2c66affSColin Finck     {
159c2c66affSColin Finck         LeaveCriticalSection( &mscms_handle_cs );
160c2c66affSColin Finck         return FALSE;
161c2c66affSColin Finck     }
162c2c66affSColin Finck     profile = &profiletable[index];
163c2c66affSColin Finck 
164c2c66affSColin Finck     if (profile->file != INVALID_HANDLE_VALUE)
165c2c66affSColin Finck     {
166c2c66affSColin Finck         if (profile->access & PROFILE_READWRITE)
167c2c66affSColin Finck         {
168c2c66affSColin Finck             DWORD written;
169c2c66affSColin Finck 
170c2c66affSColin Finck             if (SetFilePointer( profile->file, 0, NULL, FILE_BEGIN ) ||
171c2c66affSColin Finck                 !WriteFile( profile->file, profile->data, profile->size, &written, NULL ) ||
172c2c66affSColin Finck                 written != profile->size)
173c2c66affSColin Finck             {
174c2c66affSColin Finck                 ERR( "Unable to write color profile\n" );
175c2c66affSColin Finck             }
176c2c66affSColin Finck         }
177c2c66affSColin Finck         CloseHandle( profile->file );
178c2c66affSColin Finck     }
179c2c66affSColin Finck     cmsCloseProfile( profile->cmsprofile );
180c2c66affSColin Finck     HeapFree( GetProcessHeap(), 0, profile->data );
181c2c66affSColin Finck 
182c2c66affSColin Finck     memset( profile, 0, sizeof(struct profile) );
183c2c66affSColin Finck 
184c2c66affSColin Finck     LeaveCriticalSection( &mscms_handle_cs );
185c2c66affSColin Finck     return TRUE;
186c2c66affSColin Finck }
187c2c66affSColin Finck 
alloc_transform_handle(void)188c2c66affSColin Finck static HTRANSFORM alloc_transform_handle( void )
189c2c66affSColin Finck {
190c2c66affSColin Finck     DWORD_PTR index;
191c2c66affSColin Finck     struct transform *p;
192c2c66affSColin Finck     unsigned int count = 128;
193c2c66affSColin Finck 
194c2c66affSColin Finck     for (index = 0; index < num_transform_handles; index++)
195c2c66affSColin Finck     {
196c2c66affSColin Finck         if (!transformtable[index].cmstransform) return (HTRANSFORM)(index + 1);
197c2c66affSColin Finck     }
198c2c66affSColin Finck     if (!transformtable)
199c2c66affSColin Finck     {
200c2c66affSColin Finck         p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, count * sizeof(struct transform) );
201c2c66affSColin Finck     }
202c2c66affSColin Finck     else
203c2c66affSColin Finck     {
204c2c66affSColin Finck         count = num_transform_handles * 2;
205c2c66affSColin Finck         p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, transformtable, count * sizeof(struct transform) );
206c2c66affSColin Finck     }
207c2c66affSColin Finck     if (!p) return NULL;
208c2c66affSColin Finck 
209c2c66affSColin Finck     transformtable = p;
210c2c66affSColin Finck     num_transform_handles = count;
211c2c66affSColin Finck 
212c2c66affSColin Finck     return (HTRANSFORM)(index + 1);
213c2c66affSColin Finck }
214c2c66affSColin Finck 
create_transform(struct transform * transform)215c2c66affSColin Finck HTRANSFORM create_transform( struct transform *transform )
216c2c66affSColin Finck {
217c2c66affSColin Finck     HTRANSFORM handle;
218c2c66affSColin Finck 
219c2c66affSColin Finck     EnterCriticalSection( &mscms_handle_cs );
220c2c66affSColin Finck 
221c2c66affSColin Finck     if ((handle = alloc_transform_handle()))
222c2c66affSColin Finck     {
223c2c66affSColin Finck         DWORD_PTR index = (DWORD_PTR)handle - 1;
224c2c66affSColin Finck         transformtable[index] = *transform;
225c2c66affSColin Finck     }
226c2c66affSColin Finck     LeaveCriticalSection( &mscms_handle_cs );
227c2c66affSColin Finck     return handle;
228c2c66affSColin Finck }
229c2c66affSColin Finck 
close_transform(HTRANSFORM handle)230c2c66affSColin Finck BOOL close_transform( HTRANSFORM handle )
231c2c66affSColin Finck {
232c2c66affSColin Finck     DWORD_PTR index;
233c2c66affSColin Finck     struct transform *transform;
234c2c66affSColin Finck 
235c2c66affSColin Finck     EnterCriticalSection( &mscms_handle_cs );
236c2c66affSColin Finck 
237c2c66affSColin Finck     index = (DWORD_PTR)handle - 1;
238c2c66affSColin Finck     if (index > num_transform_handles)
239c2c66affSColin Finck     {
240c2c66affSColin Finck         LeaveCriticalSection( &mscms_handle_cs );
241c2c66affSColin Finck         return FALSE;
242c2c66affSColin Finck     }
243c2c66affSColin Finck     transform = &transformtable[index];
244c2c66affSColin Finck 
245c2c66affSColin Finck     cmsDeleteTransform( transform->cmstransform );
246c2c66affSColin Finck     memset( transform, 0, sizeof(struct transform) );
247c2c66affSColin Finck 
248c2c66affSColin Finck     LeaveCriticalSection( &mscms_handle_cs );
249c2c66affSColin Finck     return TRUE;
250c2c66affSColin Finck }
251c2c66affSColin Finck 
252c2c66affSColin Finck #endif /* HAVE_LCMS2 */
253