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