[go: up one dir, main page]

File: init.c

package info (click to toggle)
libggigcp 1.0.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,580 kB
  • ctags: 497
  • sloc: sh: 9,172; ansic: 2,396; makefile: 176
file content (369 lines) | stat: -rw-r--r-- 8,790 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
69
70
71
72
73
74
75
76
77
78
79
80
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* $Id$
   ******************************************************************************

   LIBGCP: extension initialization.

   Copyright (C) 2000-2001 Christoph Egger	[Christoph_Egger@t-online.de]

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

   ******************************************************************************
 */

#include <stdio.h>
#include <string.h>

#include "config.h"
#include <ggi/internal/gcp.h>
#include <ggi/internal/gcp_debug.h>

#include <ggi/gg.h> /* We use LibGG to manage config files */
#include <ggi/internal/gg_replace.h>	/* for snprintf() */


/* Static variables 
 */
static int 	 _gcpLibIsUp       = 0;
static void	*_gcpConfigHandle  = NULL;
static char      _gcpconfstub[512] = GCPCONFDIR;
static char     *_gcpconfdir       = _gcpconfstub+GCPTAGLEN;

uint32_t _gcpDebug = 0;

/* Extension ID. Defaulting to -1 should make segfault on abuse more likely...
   This is exported so that sublibs can use it.
 */
ggi_extid _gcpID = -1;

#define SUBLIB_PREFIX	"GCPdl_"


/* 
 * Returns the directory where global config files are kept
 */

const char *gcpGetConfDir(void)
{
#if defined(__WIN32__) && !defined(__CYGWIN__)
	/* On Win32 we allow overriding of the compiled in path. */
	const char *envdir = getenv("GGI_CONFDIR");
	if (envdir) return envdir;
#endif
	return _gcpconfdir;
}


/* Dummy function which returns -1
   We use this to reset the function pointers */
static int dummyfunc(void)
{
	DPRINT_CORE("dummyfunc() of LibGCP called\n");

	return GGI_ENOFUNC;
}	/* dummyfunc */



/* Reset all function pointers to dummyfunc */
static void clearfuncs(gcppriv *priv)
{
        /* Color palette manipulation
         */

        /* RGBA */
	priv->MapRGBAColor = (void *) dummyfunc;
	priv->UnmapRGBAPixel = (void *) dummyfunc;

	priv->PackRGBAColors = (void *) dummyfunc;
	priv->UnpackRGBAPixels = (void *) dummyfunc;
	priv->GetRGBAPalette = (void *) dummyfunc;
	priv->SetRGBAPalette = (void *) dummyfunc;


        /* YUV */
	priv->MapYUVColor = (void *) dummyfunc;
	priv->UnmapYUVPixel = (void *) dummyfunc;

	priv->PackYUVColors = (void *) dummyfunc;
	priv->UnpackYUVPixels = (void *) dummyfunc;

	priv->GetYUVPalette = (void *) dummyfunc;
	priv->SetYUVPalette = (void *) dummyfunc;


        /* CMYK */
	priv->MapCMYKColor = (void *) dummyfunc;
	priv->UnmapCMYKPixel = (void *) dummyfunc;

	priv->PackCMYKColors = (void *) dummyfunc;
	priv->UnpackCMYKPixels = (void *) dummyfunc;

	priv->GetCMYKPalette = (void *) dummyfunc;
	priv->SetCMYKPalette = (void *) dummyfunc;


        /* HSV */
	priv->MapHSVColor = (void *) dummyfunc;
	priv->UnmapHSVPixel = (void *) dummyfunc;

	priv->PackHSVColors = (void *) dummyfunc;
	priv->UnpackHSVPixels = (void *) dummyfunc;

	priv->GetHSVPalette = (void *) dummyfunc;
	priv->SetHSVPalette = (void *) dummyfunc;


	/* Kodak PhotoCD */
	priv->MapYCCColor = (void *) dummyfunc;
	priv->UnmapYCCPixel = (void *) dummyfunc;

	priv->PackYCCColors = (void *) dummyfunc;
	priv->UnpackYCCPixels = (void *) dummyfunc;

	priv->GetYCCPalette = (void *) dummyfunc;
	priv->SetYCCPalette = (void *) dummyfunc;



        /* Gamma map manipulation
         */

        /* RGBA */
	priv->GetRGBAGamma = (void *) dummyfunc;
	priv->SetRGBAGamma = (void *) dummyfunc;

	priv->GetRGBAGammaMap = (void *) dummyfunc;
	priv->SetRGBAGammaMap = (void *) dummyfunc;


        /* YUV */
	priv->GetYUVGamma = (void *) dummyfunc;
	priv->SetYUVGamma = (void *) dummyfunc;

	priv->GetYUVGammaMap = (void *) dummyfunc;
	priv->SetYUVGammaMap = (void *) dummyfunc;


        /* CMYK */
	priv->GetCMYKGamma = (void *) dummyfunc;
	priv->SetCMYKGamma = (void *) dummyfunc;

	priv->GetCMYKGammaMap = (void *) dummyfunc;
	priv->SetCMYKGammaMap = (void *) dummyfunc;


        /* HSV */
	priv->GetHSVGamma = (void *) dummyfunc;
	priv->SetHSVGamma = (void *) dummyfunc;

	priv->GetHSVGammaMap = (void *) dummyfunc;
	priv->SetHSVGammaMap = (void *) dummyfunc;


        /* Kodak PhotoCD */
	priv->GetYCCGamma = (void *) dummyfunc;
	priv->SetYCCGamma = (void *) dummyfunc;

	priv->GetYCCGammaMap = (void *) dummyfunc;
	priv->SetYCCGammaMap = (void *) dummyfunc;



        /* Blending manipulation
         */

        /* Color blending */
	priv->BlendColor = (void *) dummyfunc;


	/* Luminance manipulation
	 */
	priv->GetLuminance = (void *) dummyfunc;


	/* Lightness manipulation
	 */

	priv->SetIntensity = (void *) dummyfunc;
	priv->GetIntensity = (void *) dummyfunc;

}	/* clearfuncs */


/* This function is called by LibGGI when the API-list changes */
static int changed(ggi_visual_t vis, int whatchanged)
{
	DPRINT("changed(%p, 0x%x) of LibGCP called\n",
		vis, whatchanged);

	switch (whatchanged) {
	case GGI_CHG_APILIST: {
		int i;
		ggi_dlhandle *lib;
		char api[GGI_MAX_APILEN], args[GGI_MAX_APILEN];

		clearfuncs(LIBGGI_GCPEXT(vis));

		for (i = 0; ggiGetAPI(vis, i, api, args) == 0; i++) {
			ggstrlcat(api,"-gcp", sizeof(api));
			DPRINT_LIBS("Trying #%d: %s(%s)\n", i, api, args);

			lib = ggiExtensionLoadDL(vis, _gcpConfigHandle,
					api, args, NULL, SUBLIB_PREFIX);
			if (lib != NULL) break;
		}	/* for */
		}
		break;
	}	/* switch */

	return 0;
}	/* changed */



void _ggigcpInitBuiltins(void);
void _ggigcpExitBuiltins(void);


/* Initialize the extension
 */
int gcpInit(void)
{
	int err;
	const char *confdir;
	char *conffile;
	char *str;

	_gcpLibIsUp++;
	if (_gcpLibIsUp > 1) return 0; /* Initialize only at first call. */
	
	str = getenv("GCP_DEBUGSYNC");
        if (str != NULL) {
                _gcpDebug |= DEBUG_SYNC;
        }
	
        str = getenv("GCP_DEBUG");
        if (str != NULL) {
                _gcpDebug |= atoi(str) & DEBUG_ALL;
                DPRINT_CORE("%s Debugging=%d\n",
                            DEBUG_ISSYNC ? "sync" : "async",
                            _gcpDebug);
        }
	
	confdir = gcpGetConfDir();
	conffile = malloc(strlen(confdir) + 1 + strlen(GCPCONFFILE)+1);
	if (!conffile) {
		fprintf(stderr, "LibGCP: unable to allocate memory for config filename.\n");
		_gcpLibIsUp--;
		return GGI_ENOMEM;
	}

	snprintf(conffile, strlen(confdir) + strlen(GCPCONFFILE) + 2,
		"%s/%s", confdir, GCPCONFFILE);

	err = ggLoadConfig(conffile, &_gcpConfigHandle);
	if (err != GGI_OK) {
		fprintf(stderr,"LibGCP: couldn't open %s\n",
			conffile);
		_gcpLibIsUp--;
		free(conffile);
		return err;
	}
	free(conffile);


	_gcpID = ggiExtensionRegister("GCP", sizeof(gcppriv), changed);

	if (_gcpID < 0) {
		fprintf(stderr,
			"LibGCP: failed to register as extension.\n");
		_gcpLibIsUp--;
		ggFreeConfig(_gcpConfigHandle);
		_gcpConfigHandle = NULL;
		return _gcpID;
	}	/* if */

	_ggigcpInitBuiltins();

	return 0;
}	/* gcpInit */



/* Deinitialize the extension
 */
int gcpExit(void)
{
	int rc;
    
	if (!_gcpLibIsUp) return GGI_ENOTALLOC;

	if (_gcpLibIsUp > 1) {
		/* Exit only at last call */
		_gcpLibIsUp--;
		return 0;
	}	/* if */
	
	_ggigcpExitBuiltins();

	rc = ggiExtensionUnregister(_gcpID);
	ggFreeConfig(_gcpConfigHandle);
	_gcpConfigHandle = NULL;

	_gcpLibIsUp = 0;

	return rc;
}	/* gcpExit */



/* Attach the extension to a visual
 */
int gcpAttach(ggi_visual_t vis)
{
	int rc;

	DPRINT_CORE("gcpAttach(%p) called\n", vis);

	rc = ggiExtensionAttach(vis, _gcpID);
	if (rc == 0) {
		/* We are actually creating the primary instance. */
		memset(LIBGGI_GCPEXT(vis), 0, sizeof(gcppriv));
		LIBGGI_GCPEXT(vis)->priv = NULL;

		/* Now fake an "API change" so the right libs get loaded */
		changed(vis, GGI_CHG_APILIST);
	}	/* if */

	return rc;
}	/* gcpAttach */



/* Detach the extension from a visual
 */
int gcpDetach(ggi_visual_t vis)
{
	DPRINT_CORE("gcpDetach(%p) called\n", vis);

	return ggiExtensionDetach(vis, _gcpID);
}	/* gcpDetach */


/* This should be included by exactly _one_ file in each extension library. */
#include <ggi/internal/ggilibinit.h>