File: XkwInit.c

package info (click to toggle)
kgames 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,596 kB
  • sloc: ansic: 31,589; yacc: 1,067; lex: 216; sh: 149; makefile: 9
file content (472 lines) | stat: -rw-r--r-- 12,001 bytes parent folder | download | duplicates (2)
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * Copyright © 2020 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include <Xkw/Xkw.h>
#include <X11/IntrinsicI.h>
#include <X11/StringDefs.h>
#include <X11/CoreP.h>
#include <X11/ShellP.h>
#include <Xkw/KSimpleP.h>
#include <Xkw/KSimpleMenuP.h>
#include <cairo/cairo-ft.h>
#include <cairo/cairo-xlib.h>
#include <X11/Xft/Xft.h>
#include <X11/Xregion.h>
#include <math.h>

const char _XtRRenderColor[] = "RenderColor";
const char _XtRXkwFont[] = "XkwFont";
const char _XtRDpi[] = "Dpi";
const char _XtRDouble[] = "Double";

const char _XtNdpi[] = "dpi";
const char _XtCDpi[] = "Dpi";

const char _XtNbackgroundColor[] = "backgroundColor";
const char _XtNforegroundColor[] = "foregroundColor";

#define	donestr(type, value, tstr)					\
    {									\
	if (toVal->addr != NULL) {					\
	    if (toVal->size < sizeof(type)) {				\
		toVal->size = sizeof(type);				\
		XtDisplayStringConversionWarning(dpy,			\
						 (char*) fromVal->addr, tstr); \
		return False;						\
	    }								\
	    *(type*)(toVal->addr) = (value);				\
	}								\
	else {								\
	    static type static_val;					\
	    static_val = (value);					\
	    toVal->addr = (XPointer)&static_val;			\
	}								\
	toVal->size = sizeof(type);					\
	return True;							\
    }

static Boolean
XkwCvtStringToRenderColor(Display *dpy,
			  XrmValue *args, Cardinal *num_args,
			  XrmValue *fromVal, XrmValue *toVal,
			  XtPointer *converter_data)
{
    char	    *spec;
    XRenderColor    renderColor;

    (void) args;
    (void) num_args;
    (void) converter_data;
    spec = (char *) fromVal->addr;
    if (strcasecmp (spec, XtDefaultForeground) == 0)
    {
	renderColor.red = 0;
	renderColor.green = 0;
	renderColor.blue = 0;
	renderColor.alpha = 0xffff;
    }
    else if (strcasecmp (spec, XtDefaultBackground) == 0)
    {
	renderColor.red = 0xffff;
	renderColor.green = 0xffff;
	renderColor.blue = 0xffff;
	renderColor.alpha = 0xffff;
    }
    else if (!XRenderParseColor (dpy, spec, &renderColor))
	return False;

    donestr (XRenderColor, renderColor, XtRRenderColor);
}

static Boolean
XkwCvtStringToXkwFont(Display *dpy,
		      XrmValue *args, Cardinal *num_args,
		      XrmValue *fromVal, XrmValue *toVal,
		      XtPointer *converter_data)
{
    char		*string_name = (char *) fromVal->addr;
    FcPattern		*pat, *match;
    FcResult		result;
    XkwFont	    	xkwFont;

    (void) args;
    (void) num_args;
    (void) converter_data;
    if (strncmp(string_name, "fixed", 5) == 0)
	string_name = "monospace-12";
    if (*string_name == '-') {
	pat = XftXlfdParse(string_name, False, False);
    } else {
	pat = FcNameParse ((FcChar8 *) string_name);
    }
    if (!pat)
	return False;

    FcConfigSubstitute (0, pat, FcMatchPattern);
    FcDefaultSubstitute (pat);

    match = FcFontMatch (0, pat, &result);
    FcPatternDestroy (pat);
    if (!match)
	return False;

    FcPatternGetDouble (match, FC_SIZE, 0, &xkwFont.size);
    xkwFont.font_face = cairo_ft_font_face_create_for_pattern (match);

    FcPatternDestroy (match);
    if (!xkwFont.font_face)
	return False;

    donestr (XkwFont, xkwFont, XtRXkwFont);
}

static void
XkwFreeXkwFont(XtAppContext app_context,
	       XrmValue *val,
	       XtPointer converter_data,
	       XrmValue *args,
	       Cardinal *num_args)
{
    XkwFont	*xkwFont = (XkwFont *) val;

    (void) app_context;
    (void) args;
    (void) num_args;
    (void) converter_data;
    cairo_font_face_destroy(xkwFont->font_face);
}

static Boolean
xkw_str_to_double(char *string, double *result)
{
    char *end;
    double d;

    if (!string)
	return False;
    d = strtod(string, &end);
    if (end == string)
	return False;
    *result = d;
    return True;
}

static Boolean
XkwCvtStringToDpi(Display *dpy,
		  XrmValue *args, Cardinal *num_args,
		  XrmValue *fromVal, XrmValue *toVal,
		  XtPointer *converter_data)
{
    double	dpi;

    (void) args;
    (void) num_args;
    (void) converter_data;
    if (!xkw_str_to_double((char *) fromVal->addr, &dpi)) {
	if (!xkw_str_to_double(XGetDefault(dpy, "Xft", "dpi"), &dpi)) {
	    int screen = DefaultScreen (dpy);
	    int	height_pix = DisplayHeight(dpy, screen);
	    int	height_mm = DisplayHeightMM(dpy, screen);

	    dpi = (double) height_pix / ((double) height_mm / 25.4);
	}
    }

    donestr (double, dpi, XtRDpi);
}

static Boolean
XkwCvtStringToDouble(Display *dpy,
		     XrmValue *args, Cardinal *num_args,
		     XrmValue *fromVal, XrmValue *toVal,
		     XtPointer *converter_data)
{
    double	d = NAN;

    (void) args;
    (void) num_args;
    (void) converter_data;
    (void) xkw_str_to_double((char *) fromVal->addr, &d);

    donestr (double, d, XtRDouble);
}

cairo_surface_t *
XkwGetSurface(Widget w)
{
    return cairo_xlib_surface_create(XtDisplay(w),
				     XtWindow(w),
				     XtScreen(w)->root_visual,
				     XtWidth(w),
				     XtHeight(w));
}

cairo_t *
XkwGetCairo(Widget w)
{
    Widget real = w;
    while (!XtIsWidget(real))
	real = XtParent(real);

    cairo_surface_t *s = XkwGetSurface(real);
    cairo_t *cr = cairo_create(s);
    cairo_translate(cr, XtX(w) - XtX(real), XtY(w) - XtY(real));
    cairo_surface_destroy (s);
    return cr;
}

static XRectangle
XkwDrawRect(Widget gw, Region region)
{
    int	x1 = 0;
    int x2 = XtWidth(gw);
    int y1 = 0;
    int y2 = XtHeight(gw);

    if (region) {
	int cx1 = region->extents.x1;
	int cx2 = region->extents.x2;
	int cy1 = region->extents.y1;
	int cy2 = region->extents.y2;

	if (cx1 > x1)
	    x1 = cx1;

	if (cy1 > y1)
	    y1 = cy1;

	if (cx2 < x2)
	    x2 = cx2;

	if (cy2 < y2)
	    y2 = cy2;

	if (x2 < x1)
	    x1 = x2 = 0;

	if (y2 < y1)
	    y1 = y2 = 0;
    }
    XRectangle ret = { .x = x1, .y = y1, .width = x2 - x1, .height = y2 - y1 };
    return ret;
}

static cairo_surface_t *
get_widget_surface(Widget gw, Boolean *temp, XRenderColor **fg, XRenderColor **bg)
{
    if (XtIsSubclass(gw, ksimpleWidgetClass)) {
	KSimpleWidget 	w = (KSimpleWidget) gw;
	if (w->ksimple.surface &&
	    (w->ksimple.surface_width != gw->core.width ||
	     w->ksimple.surface_height != gw->core.height)) {
	    cairo_surface_destroy(w->ksimple.surface);
	    w->ksimple.surface = NULL;
	}
	if (!w->ksimple.surface) {
	    w->ksimple.surface = XkwGetSurface(gw);
	    w->ksimple.surface_width = gw->core.width;
	    w->ksimple.surface_height = gw->core.height;
	}
	if (fg)
	    *fg = &w->ksimple.foreground;
	if (bg)
	    *bg = &w->ksimple.background;
	*temp = False;
	return w->ksimple.surface;
    } else if (XtIsSubclass(gw, ksimpleMenuWidgetClass)) {
	KSimpleMenuWidget w = (KSimpleMenuWidget) gw;
	if (w->ksimple_menu.surface &&
	    (w->ksimple_menu.surface_width != gw->core.width ||
	     w->ksimple_menu.surface_height != gw->core.height)) {
	    cairo_surface_destroy(w->ksimple_menu.surface);
	    w->ksimple_menu.surface = NULL;
	}
	if (!w->ksimple_menu.surface) {
	    w->ksimple_menu.surface = XkwGetSurface(gw);
	    w->ksimple_menu.surface_width = gw->core.width;
	    w->ksimple_menu.surface_height = gw->core.height;
	}
	if (fg)
	    *fg = &w->ksimple_menu.foreground;
	if (bg)
	    *bg = &w->ksimple_menu.background;
	*temp = False;
	return w->ksimple_menu.surface;
    } else {
	*temp = True;
	return XkwGetSurface(gw);
    }
}

cairo_t *
XkwDrawBegin(Widget gw, Region region)
{
    Widget		greal = gw;

    while (!XtIsWidget(greal))
	greal = XtParent(greal);

    XRenderColor	*fg = NULL;
    XRenderColor	*bg = NULL;
    Boolean		surface_created = False;
    cairo_surface_t	*surface = get_widget_surface(greal, &surface_created, &fg, &bg);

    XRectangle rect = XkwDrawRect(gw, region);

    cairo_surface_t *pix = cairo_surface_create_similar(surface,
							CAIRO_CONTENT_COLOR,
							rect.width, rect.height);
    if (surface_created)
	cairo_surface_destroy(surface);

    cairo_t *cr = cairo_create(pix);

    cairo_translate(cr, -rect.x, -rect.y);

    cairo_surface_destroy(pix);
    if (bg) {
	XkwSetSource(cr, bg);
	cairo_paint(cr);
    }
    if (fg)
	XkwSetSource(cr, fg);
    return cr;
}

void
XkwDrawEnd(Widget gw, Region region, cairo_t *cr)
{
    Widget		greal = gw;

    while (!XtIsWidget(greal))
	greal = XtParent(greal);

    Boolean		surface_created = False;
    cairo_surface_t	*surface = get_widget_surface(greal, &surface_created, NULL, NULL);

    cairo_t 		*dest = cairo_create(surface);

    if (surface_created)
	cairo_surface_destroy(surface);

    XRectangle rect = XkwDrawRect(gw, region);

    double x = rect.x;
    double y = rect.y;
    if (gw != greal) {
	x += XtX(gw);
	y += XtY(gw);
    }
    cairo_set_source_surface(dest, cairo_get_target(cr), x, y);
    if (region) {
	long i;
	BOX *b = region->rects;

	for (i = 0; i < region->numRects; i++) {
	    cairo_rectangle(dest, b->x1, b->y1, b->x2 - b->x1, b->y2 - b->y1);
	    b++;
	}
	cairo_fill(dest);
    } else {
	cairo_paint(dest);
    }
    cairo_destroy(cr);
    cairo_destroy(dest);
}

void
XkwSetSource(cairo_t *cr, XRenderColor *color)
{
    cairo_set_source_rgba(cr,
			  color->red / 65535.0,
			  color->green / 65535.0,
			  color->blue / 65535.0,
			  color->alpha / 65535.0);
}

void
XkwSetSourceInterp(cairo_t *cr, XRenderColor *a, XRenderColor *b)
{
    cairo_set_source_rgba(cr,
			  a->red / (2.0 * 65535.0) + b->red / (2.0 * 65535.0),
			  a->green / (2.0 * 65535.0) + b->green / (2.0 * 65535.0),
			  a->blue / (2.0 * 65535.0) + b->blue / (2.0 * 65535.0),
			  a->alpha / (2.0 * 65535.0) + b->alpha / (2.0 * 65535.0));
}

void
XkwInitializeWidgetSet(void)
{
    static Boolean init_done;

    if (!init_done) {
	init_done = True;
	XtSetTypeConverter(XtRString, XtRRenderColor,
			   XkwCvtStringToRenderColor,
			   NULL, 0,
			   XtCacheByDisplay, NULL);
	XtSetTypeConverter(XtRString, XtRXkwFont,
			   XkwCvtStringToXkwFont,
			   NULL, 0,
			   XtCacheAll, XkwFreeXkwFont);
	XtSetTypeConverter(XtRString, XtRDpi,
			   XkwCvtStringToDpi,
			   NULL, 0,
			   XtCacheByDisplay, NULL);
	XtSetTypeConverter(XtRString, XtRDouble,
			   XkwCvtStringToDouble,
			   NULL, 0,
			   XtCacheAll, NULL);
	XtAddConverter(XtRString, XtRJustify, XmuCvtStringToJustify, NULL, 0);
	XtSetTypeConverter(XtRJustify, XtRString, XmuCvtJustifyToString,
			   NULL, 0, XtCacheNone, NULL);
	XtAddConverter(XtRString, XtROrientation, XmuCvtStringToOrientation,
		       NULL, 0);
    }
}

Widget
XkwInitialize(const char *class,
	      XrmOptionDescRec *options,
	      Cardinal num_options,
	      int *argc,
	      _XtString *argv,
	      Boolean input,
	      char const * const *fallback_resources)
{
    XtAppContext app_con;
    Widget toplevel;

    Arg args[1];
    Cardinal num_args = 0;
    if (input) {
	XtSetArg(args[0], XtNinput, True);
	num_args = 1;
    }

    toplevel = XtAppInitialize (&app_con, class, options, num_options,
				argc, argv, (char **) fallback_resources,
				args, num_args);

    _XtGetProcessContext()->defaultAppContext = app_con;
    return toplevel;
}