summaryrefslogtreecommitdiff
path: root/src/util.c
blob: 52f61483721cce47e7da8be12ca890e215079b85 (plain)
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
/*****************************************************************************/
/*

Copyright 1989, 1998  The Open Group

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.

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
OPEN GROUP 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.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.

*/
/**       Copyright 1988 by Evans & Sutherland Computer Corporation,        **/
/**                          Salt Lake City, Utah                           **/
/**                        Cambridge, Massachusetts                         **/
/**                                                                         **/
/**                           All Rights Reserved                           **/
/**                                                                         **/
/**    Permission to use, copy, modify, and distribute this software and    **/
/**    its documentation  for  any  purpose  and  without  fee is hereby    **/
/**    granted, provided that the above copyright notice appear  in  all    **/
/**    copies and that both  that  copyright  notice  and  this  permis-    **/
/**    sion  notice appear in supporting  documentation,  and  that  the    **/
/**    name of Evans & Sutherland not be used in advertising    **/
/**    in publicity pertaining to distribution of the  software  without    **/
/**    specific, written prior permission.                                  **/
/**                                                                         **/
/**    EVANS & SUTHERLAND DISCLAIMs ALL WARRANTIES WITH REGARD    **/
/**    TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES  OF  MERCHANT-    **/
/**    ABILITY  AND  FITNESS,  IN  NO  EVENT SHALL EVANS & SUTHERLAND    **/
/**    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL  DAM-    **/
/**    AGES 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.                                     **/
/*****************************************************************************/

/***********************************************************************
 *
 * utility routines for twm
 *
 * 28-Oct-87 Thomas E. LaStrange        File created
 *
 ***********************************************************************/

#include "twm.h"
#include "util.h"
#include "parse.h"
#include "screen.h"
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <X11/Xmu/Drawing.h>
#include <X11/Xmu/CharSet.h>

static Pixmap CreateXLogoPixmap(unsigned int *widthp, unsigned int *heightp);
static Pixmap CreateResizePixmap(unsigned int *widthp, unsigned int *heightp);
static Pixmap CreateDotPixmap(unsigned int *widthp, unsigned int *heightp);
static Pixmap CreateQuestionPixmap(unsigned int *widthp, unsigned int *heightp);
static Pixmap CreateMenuPixmap(unsigned int *widthp, unsigned int *heightp);

int HotX, HotY;

/**
 * move a window outline
 *
 *  \param root         window we are outlining
 *  \param x,y          upper left coordinate
 *  \param width,height size of the rectangle
 *  \param bw           border width of the frame
 *  \param th           title height
 */
void
MoveOutline(Window root, int x, int y, int width, int height, int bw, int th)
{
    static int lastx = 0;
    static int lasty = 0;
    static int lastWidth = 0;
    static int lastHeight = 0;
    static int lastBW = 0;
    static int lastTH = 0;
    int xl, xr, yt, yb, xinnerl, xinnerr, yinnert, yinnerb;
    int xthird, ythird;
    XSegment outline[18];
    XSegment *r;

    if (x == lastx && y == lasty && width == lastWidth && height == lastHeight
        && lastBW == bw && th == lastTH)
        return;

    r = outline;

#define DRAWIT() \
    if (lastWidth || lastHeight)                        \
    {                                                   \
        xl = lastx;                                     \
        xr = lastx + lastWidth - 1;                     \
        yt = lasty;                                     \
        yb = lasty + lastHeight - 1;                    \
        xinnerl = xl + lastBW;                          \
        xinnerr = xr - lastBW;                          \
        yinnert = yt + lastTH + lastBW;                 \
        yinnerb = yb - lastBW;                          \
        xthird = (xinnerr - xinnerl) / 3;               \
        ythird = (yinnerb - yinnert) / 3;               \
                                                        \
        r->x1 = (short)(xl);                            \
        r->y1 = (short)(yt);                            \
        r->x2 = (short)(xr);                            \
        r->y2 = (short)(yt);                            \
        r++;                                            \
                                                        \
        r->x1 = (short)(xl);                            \
        r->y1 = (short)(yb);                            \
        r->x2 = (short)(xr);                            \
        r->y2 = (short)(yb);                            \
        r++;                                            \
                                                        \
        r->x1 = (short)(xl);                            \
        r->y1 = (short)(yt);                            \
        r->x2 = (short)(xl);                            \
        r->y2 = (short)(yb);                            \
        r++;                                            \
                                                        \
        r->x1 = (short)(xr);                            \
        r->y1 = (short)(yt);                            \
        r->x2 = (short)(xr);                            \
        r->y2 = (short)(yb);                            \
        r++;                                            \
                                                        \
        r->x1 = (short)(xinnerl + xthird);              \
        r->y1 = (short)(yinnert);                       \
        r->x2 = (short)(r->x1);                         \
        r->y2 = (short)(yinnerb);                       \
        r++;                                            \
                                                        \
        r->x1 = (short)(xinnerl + (2 * xthird));        \
        r->y1 = (short)(yinnert);                       \
        r->x2 = (short)(r->x1);                         \
        r->y2 = (short)(yinnerb);                       \
        r++;                                            \
                                                        \
        r->x1 = (short)(xinnerl);                       \
        r->y1 = (short)(yinnert + ythird);              \
        r->x2 = (short)(xinnerr);                       \
        r->y2 = (short)(r->y1);                         \
        r++;                                            \
                                                        \
        r->x1 = (short)(xinnerl);                       \
        r->y1 = (short)(yinnert + (2 * ythird));        \
        r->x2 = (short)(xinnerr);                       \
        r->y2 = (short)(r->y1);                         \
        r++;                                            \
                                                        \
        if (lastTH != 0) {                              \
            r->x1 = (short)(xl);                        \
            r->y1 = (short)(yt + lastTH);               \
            r->x2 = (short)(xr);                        \
            r->y2 = (short)(r->y1);                     \
            r++;                                        \
        }                                               \
    }

    /* undraw the old one, if any */
    DRAWIT();

    lastx = x;
    lasty = y;
    lastWidth = width;
    lastHeight = height;
    lastBW = bw;
    lastTH = th;

    /* draw the new one, if any */
    DRAWIT();

#undef DRAWIT

    if (r != outline) {
        XDrawSegments(dpy, root, Scr->DrawGC, outline, (int) (r - outline));
    }
}

/**
 * zoom in or out of an icon
 *
 *  \param wf window to zoom from
 *  \param wt window to zoom to
 */
void
Zoom(Window wf, Window wt)
{
    int fx, fy, tx, ty;         /* from, to */
    unsigned int fw, fh, tw, th;        /* from, to */
    long dx, dy, dw, dh;
    long z;
    int j;
    unsigned udummy = 0;
    Window wdummy = None;

    if (!Scr->DoZoom || Scr->ZoomCount < 1)
        return;

    if (wf == None || wt == None)
        return;

    XGetGeometry(dpy, wf, &wdummy, &fx, &fy, &fw, &fh, &udummy, &udummy);
    XGetGeometry(dpy, wt, &wdummy, &tx, &ty, &tw, &th, &udummy, &udummy);

    dx = ((long) (tx - fx));    /* going from -> to */
    dy = ((long) (ty - fy));    /* going from -> to */
    dw = ((long) (tw - fw));    /* going from -> to */
    dh = ((long) (th - fh));    /* going from -> to */
    z = (long) (Scr->ZoomCount + 1);

    for (j = 0; j < 2; j++) {
        long i;

        XDrawRectangle(dpy, Scr->Root, Scr->DrawGC, fx, fy, fw, fh);
        for (i = 1; i < z; i++) {
            int x = fx + (int) ((dx * i) / z);
            int y = fy + (int) ((dy * i) / z);
            unsigned width = (unsigned) (((long) fw) + (dw * i) / z);
            unsigned height = (unsigned) (((long) fh) + (dh * i) / z);

            XDrawRectangle(dpy, Scr->Root, Scr->DrawGC, x, y, width, height);
        }
        XDrawRectangle(dpy, Scr->Root, Scr->DrawGC, tx, ty, tw, th);
    }
}

/**
 * expand the tilde character to HOME if it is the first
 * character of the filename
 *
 *      \return a pointer to the new name
 *
 *  \param name  the filename to expand
 */
char *
ExpandFilename(const char *name)
{
    char *newname;

    if (name[0] != '~')
        return strdup(name);

    newname = (char *) malloc((size_t) HomeLen + strlen(name) + 2);
    if (!newname) {
        twmWarning("unable to allocate %lu bytes to expand filename %s/%s",
                   (unsigned long) HomeLen + (unsigned long) strlen(name) + 2,
                   Home, &name[1]);
    }
    else {
        (void) sprintf(newname, "%s/%s", Home, &name[1]);
    }

    return newname;
}

/**
 * read in the bitmap file for the unknown icon
 *
 * \param name  the filename to read
 */
void
GetUnknownIcon(const char *name)
{
    int dummy = 0;
    unsigned udummy = 0;
    Window wdummy = None;

    if ((Scr->UnknownPm = GetBitmap(name)) != None) {
        XGetGeometry(dpy, Scr->UnknownPm, &wdummy, &dummy, &dummy,
                     (unsigned int *) &Scr->UnknownWidth,
                     (unsigned int *) &Scr->UnknownHeight, &udummy, &udummy);
    }
}

/**
 *      FindBitmap - read in a bitmap file and return size
 *
 *  \return pixmap associated with bitmap
 *
 *  \param name          filename to read
 *  \param[out] widthp   pointer to width of bitmap
 *  \param[out] heightp  pointer to height of bitmap
 */
Pixmap
FindBitmap(const char *name, unsigned *widthp, unsigned *heightp)
{
    char *bigname;
    Pixmap pm;

    if (!name)
        return None;

    /*
     * Names of the form :name refer to hardcoded images that are scaled to
     * look nice in title buttons.  Eventually, it would be nice to put in a
     * menu symbol as well....
     */
    if (name[0] == ':') {
        int i;
	/* *INDENT-OFF* */
        static struct {
            const char *name;
            Pixmap (*proc)(unsigned int *, unsigned int *);
        } pmtab[] = {
            { TBPM_DOT,         CreateDotPixmap },
            { TBPM_ICONIFY,     CreateDotPixmap },
            { TBPM_RESIZE,      CreateResizePixmap },
            { TBPM_XLOGO,       CreateXLogoPixmap },
            { TBPM_DELETE,      CreateXLogoPixmap },
            { TBPM_MENU,        CreateMenuPixmap },
            { TBPM_QUESTION,    CreateQuestionPixmap },
        };
	/* *INDENT-ON* */

        for (i = 0; (size_t) i < (sizeof pmtab) / (sizeof pmtab[0]); i++) {
            if (XmuCompareISOLatin1(pmtab[i].name, name) == 0)
                return (*pmtab[i].proc) (widthp, heightp);
        }
        twmWarning("no such built-in bitmap \"%s\"", name);
        return None;
    }

    /*
     * Generate a full pathname if any special prefix characters (such as ~)
     * are used.  If the bigname is different from name, bigname will need to
     * be freed.
     */
    bigname = ExpandFilename(name);
    if (!bigname)
        return None;

    /*
     * look along bitmapFilePath resource same as toolkit clients
     */
    pm = XmuLocateBitmapFile(ScreenOfDisplay(dpy, Scr->screen), bigname, NULL,
                             0, (int *) widthp, (int *) heightp, &HotX, &HotY);
    if (pm == None && Scr->IconDirectory && bigname[0] != '/') {
        free(bigname);
        /*
         * Attempt to find icon in old IconDirectory (now obsolete)
         */
        bigname = (char *)
            malloc(strlen(name) + strlen(Scr->IconDirectory) + 2);
        if (!bigname) {
            twmWarning("unable to allocate memory for \"%s/%s\"",
                       Scr->IconDirectory, name);
            return None;
        }
        (void) sprintf(bigname, "%s/%s", Scr->IconDirectory, name);
        if (XReadBitmapFile(dpy, Scr->Root, bigname, widthp, heightp, &pm,
                            &HotX, &HotY) != BitmapSuccess) {
            pm = None;
        }
    }
    free(bigname);
    if (pm == None) {
        twmWarning("unable to find bitmap \"%s\"", name);
    }

    return pm;
}

Pixmap
GetBitmap(const char *name)
{
    unsigned udummy = 0;

    return FindBitmap(name, &udummy, &udummy);
}

void
InsertRGBColormap(Atom a, XStandardColormap *maps, int nmaps, Bool replace)
{
    StdCmap *sc = NULL;

    if (replace) {              /* locate existing entry */
        for (sc = Scr->StdCmapInfo.head; sc; sc = sc->next) {
            if (sc->atom == a)
                break;
        }
    }

    if (!sc) {                  /* no existing, allocate new */
        sc = (StdCmap *) malloc(sizeof(StdCmap));
        if (!sc) {
            twmWarning("unable to allocate %lu bytes for StdCmap",
                       (unsigned long) sizeof(StdCmap));
            return;
        }
        replace = False;
    }

    if (replace) {              /* just update contents */
        if (sc->maps)
            XFree(sc->maps);
        if (sc == Scr->StdCmapInfo.mru)
            Scr->StdCmapInfo.mru = NULL;
    }
    else {                      /* else appending */
        sc->next = NULL;
        sc->atom = a;
        if (Scr->StdCmapInfo.tail) {
            Scr->StdCmapInfo.tail->next = sc;
        }
        else {
            Scr->StdCmapInfo.head = sc;
        }
        Scr->StdCmapInfo.tail = sc;
    }
    sc->nmaps = nmaps;
    sc->maps = maps;

    return;
}

void
RemoveRGBColormap(Atom a)
{
    StdCmap *sc, *prev;

    prev = NULL;
    for (sc = Scr->StdCmapInfo.head; sc; sc = sc->next) {
        if (sc->atom == a)
            break;
        prev = sc;
    }
    if (sc) {                   /* found one */
        if (sc->maps)
            XFree(sc->maps);
        if (prev)
            prev->next = sc->next;
        if (Scr->StdCmapInfo.head == sc)
            Scr->StdCmapInfo.head = sc->next;
        if (Scr->StdCmapInfo.tail == sc)
            Scr->StdCmapInfo.tail = prev;
        if (Scr->StdCmapInfo.mru == sc)
            Scr->StdCmapInfo.mru = NULL;
    }
    return;
}

void
LocateStandardColormaps(void)
{
    Atom *atoms;
    int natoms;
    int i;

    atoms = XListProperties(dpy, Scr->Root, &natoms);
    for (i = 0; i < natoms; i++) {
        XStandardColormap *maps = NULL;
        int nmaps;

        if (XGetRGBColormaps(dpy, Scr->Root, &maps, &nmaps, atoms[i])) {
            /* if got one, then append to current list */
            InsertRGBColormap(atoms[i], maps, nmaps, False);
        }
    }
    if (atoms)
        XFree(atoms);
    return;
}

void
GetColor(int kind, Pixel *what, const char *name)
{
    XColor color, junkcolor;
    Status stat = 0;
    Colormap cmap = Scr->TwmRoot.cmaps.cwins[0]->colormap->c;

    if (!Scr->FirstTime)
        return;

    if (Scr->Monochrome != kind)
        return;

    if (!XAllocNamedColor(dpy, cmap, name, &color, &junkcolor)) {
        /* if we could not allocate the color, let's see if this is a
         * standard colormap
         */
        XStandardColormap *stdcmap = NULL;

        /* parse the named color */
        if (name[0] != '#')
            stat = XParseColor(dpy, cmap, name, &color);
        if (!stat) {
            twmWarning("invalid color name \"%s\"", name);
            return;
        }

        /*
         * look through the list of standard colormaps (check cache first)
         */
        if (Scr->StdCmapInfo.mru && Scr->StdCmapInfo.mru->maps &&
            (Scr->StdCmapInfo.mru->maps[Scr->StdCmapInfo.mruindex].colormap ==
             cmap)) {
            stdcmap = &(Scr->StdCmapInfo.mru->maps[Scr->StdCmapInfo.mruindex]);
        }
        else {
            StdCmap *sc;

            for (sc = Scr->StdCmapInfo.head; sc; sc = sc->next) {
                int i;

                for (i = 0; i < sc->nmaps; i++) {
                    if (sc->maps[i].colormap == cmap) {
                        Scr->StdCmapInfo.mru = sc;
                        Scr->StdCmapInfo.mruindex = i;
                        stdcmap = &(sc->maps[i]);
                        goto gotit;
                    }
                }
            }
        }

 gotit:
        if (stdcmap) {
            color.pixel = (stdcmap->base_pixel +
                           ((Pixel) (((float) color.red / 65535.0) *
                                     (double) stdcmap->red_max + 0.5) *
                            stdcmap->red_mult) +
                           ((Pixel) (((float) color.green / 65535.0) *
                                     (double) stdcmap->green_max + 0.5) *
                            stdcmap->green_mult) +
                           ((Pixel) (((float) color.blue / 65535.0) *
                                     (double) stdcmap->blue_max + 0.5) *
                            stdcmap->blue_mult));
        }
        else {
            twmWarning("unable to allocate color \"%s\"", name);
            return;
        }
    }

    *what = color.pixel;
}

void
GetColorValue(int kind, XColor *what, const char *name)
{
    XColor junkcolor;
    Colormap cmap = Scr->TwmRoot.cmaps.cwins[0]->colormap->c;

    if (!Scr->FirstTime)
        return;

    if (Scr->Monochrome != kind)
        return;

    if (!XLookupColor(dpy, cmap, name, what, &junkcolor)) {
        twmWarning("invalid color name \"%s\"", name);
    }
    else {
        what->pixel = AllPlanes;
    }
}

static Boolean
FindFontSet(MyFont *font, const char *fontname)
{
    char **missing_charset_list_return;
    int missing_charset_count_return;
    char *def_string_return;
    XFontSetExtents *font_extents;
    XFontStruct **xfonts;
    char **font_names;

    if (use_fontset) {
        int ascent;
        int descent;
        int fnum;
        int i;

        if (font->fontset != NULL) {
            XFreeFontSet(dpy, font->fontset);
        }

        if ((font->fontset = XCreateFontSet(dpy, fontname,
                                            &missing_charset_list_return,
                                            &missing_charset_count_return,
                                            &def_string_return)) == NULL) {
            return False;
        }
        if (missing_charset_count_return) {
            twmVerbose("%d fonts are missing from fontset",
                       missing_charset_count_return);
            for (i = 0; i < missing_charset_count_return; i++) {
                twmVerbose("font for charset %s is lacking.",
                           missing_charset_list_return[i]);
            }
            XFreeStringList(missing_charset_list_return);
        }

        font_extents = XExtentsOfFontSet(font->fontset);
        fnum = XFontsOfFontSet(font->fontset, &xfonts, &font_names);
        for (i = 0, ascent = 0, descent = 0; i < fnum; i++) {
            if (ascent < (*xfonts)->ascent)
                ascent = (*xfonts)->ascent;
            if (descent < (*xfonts)->descent)
                descent = (*xfonts)->descent;
            xfonts++;
        }
        font->height = font_extents->max_logical_extent.height;
        font->y = ascent;
        font->ascent = ascent;
        font->descent = descent;
        twmMessage("created fontset with %d fonts (%d missing) for \"%s\"",
                   fnum, missing_charset_count_return,
                   fontname ? fontname : "NULL");
        return True;
    }

    if (font->font != NULL)
        XFreeFont(dpy, font->font);

    if ((font->font = XLoadQueryFont(dpy, fontname)) == NULL) {
        return False;
    }
    font->height = font->font->ascent + font->font->descent;
    font->y = font->font->ascent;
    font->ascent = font->font->ascent;
    font->descent = font->font->descent;
    return True;
}

/*
 * The following functions are sensible to 'use_fontset'.
 * When 'use_fontset' is True,
 *  - XFontSet-related internationalized functions are used
 *     so as multibyte languages can be displayed.
 * When 'use_fontset' is False,
 *  - XFontStruct-related conventional functions are used
 *     so as 8-bit characters can be displayed even when
 *     locale is not set properly.
 */
void
GetFont(MyFont *font)
{

    if (!FindFontSet(font, font->name)) {
        const char *what = "fonts";
        const char *deffontname = "fixed";

        if (use_fontset) {
            what = "fontsets";
        }
        else if (Scr->DefaultFont.name) {
            deffontname = Scr->DefaultFont.name;
        }
        if (!FindFontSet(font, deffontname)) {
            twmError("unable to open %s \"%s\" or \"%s\"",
                     what, font->name, deffontname);
        }
    }
}

void
DestroyFont(MyFont *font)
{
    if (!font) {
        return;
    }

    if (font->fontset) {
        XFreeFontSet(dpy, font->fontset);
        font->fontset = NULL;
    }

    if (font->font) {
        XFreeFont(dpy, font->font);
        font->font = NULL;
    }
}

int
MyFont_TextWidth(MyFont *font, const char *string, int len)
{
    XRectangle ink_rect;
    XRectangle logical_rect;

    if (use_fontset) {
        XmbTextExtents(font->fontset, string, len, &ink_rect, &logical_rect);
        return logical_rect.width;
    }
    return XTextWidth(font->font, string, len);
}

void
MyFont_DrawImageString(Display *dpy2, Drawable d, MyFont *font, GC gc,
                       int x, int y, const char *string, int len)
{
    if (use_fontset) {
        XmbDrawImageString(dpy2, d, font->fontset, gc, x, y, string, len);
        return;
    }
    XDrawImageString(dpy2, d, gc, x, y, string, len);
}

void
MyFont_DrawString(Display *dpy2, Drawable d, MyFont *font, GC gc,
                  int x, int y, const char *string, int len)
{
    if (use_fontset) {
        XmbDrawString(dpy2, d, font->fontset, gc, x, y, string, len);
        return;
    }
    XDrawString(dpy2, d, gc, x, y, string, len);
}

void
MyFont_ChangeGC(unsigned long fix_fore, unsigned long fix_back,
                MyFont *fix_font)
{
    Gcv.foreground = fix_fore;
    Gcv.background = fix_back;
    if (use_fontset) {
        XChangeGC(dpy, Scr->NormalGC, GCForeground | GCBackground, &Gcv);
        return;
    }
    Gcv.font = fix_font->font->fid;
    XChangeGC(dpy, Scr->NormalGC, GCFont | GCForeground | GCBackground, &Gcv);
}

/*
 * The following functions are internationalized substitutions
 * for XFetchName and XGetIconName using XGetWMName and
 * XGetWMIconName.
 *
 * Please note that the third arguments have to be freed using free(),
 * not XFree().
 */
Status
I18N_FetchName(Display *dpy2, Window w, char **winname)
{
    int status;
    XTextProperty text_prop;
    int rc = 0;

    *winname = NULL;

    status = XGetWMName(dpy2, w, &text_prop);
    if (status && text_prop.value && text_prop.nitems) {
        char **list = NULL;
        int num;

        status = XmbTextPropertyToTextList(dpy2, &text_prop, &list, &num);
        if (status >= Success && num && list && *list) {
            XFree(text_prop.value);
            *winname = strdup(*list);
            XFreeStringList(list);
            rc = 1;
        }
        else {
            char *value = NULL;

            /*
             * If the system's locale support is broken (e.g., missing useful
             * parts), the preceding Xmb call may fail.
             */
            if (XFetchName(dpy2, w, &value) && value != NULL) {
                *winname = strdup(value);
                XFree(value);
                rc = 1;
            }
        }
    }

    return rc;
}

Status
I18N_GetIconName(Display *dpy2, Window w, char **iconname)
{
    int status;
    XTextProperty text_prop;
    char **list;
    int num;

    status = XGetWMIconName(dpy2, w, &text_prop);
    if (!status || !text_prop.value || !text_prop.nitems)
        return 0;
    status = XmbTextPropertyToTextList(dpy2, &text_prop, &list, &num);
    if (status < Success || !num || !*list)
        return 0;
    XFree(text_prop.value);
    *iconname = (char *) strdup(*list);
    XFreeStringList(list);
    return 1;
}

/**
 * separate routine to set focus to make things more understandable
 * and easier to debug
 */
void
SetFocus(TwmWindow *tmp_win, Time time)
{
    Window w = (tmp_win ? tmp_win->w : PointerRoot);

#ifdef TRACE
    if (tmp_win) {
        twmMessage("Focusing on window \"%s\"", tmp_win->full_name);
    }
    else {
        twmMessage("Unfocusing; Scr->Focus was \"%s\"",
                   Scr->Focus ? Scr->Focus->full_name : "(nil)");
    }
#endif

    XSetInputFocus(dpy, w, RevertToPointerRoot, time);
}

static Pixmap
CreateXLogoPixmap(unsigned *widthp, unsigned *heightp)
{
    int h = Scr->TBInfo.width - Scr->TBInfo.border * 2;

    if (h < 0)
        h = 0;

    *widthp = *heightp = (unsigned int) h;
    if (Scr->tbpm.xlogo == None) {
        GC gc, gcBack;

        Scr->tbpm.xlogo =
            XCreatePixmap(dpy, Scr->Root, (unsigned) h, (unsigned) h, 1);
        gc = XCreateGC(dpy, Scr->tbpm.xlogo, 0L, NULL);
        XSetForeground(dpy, gc, 0);
        XFillRectangle(dpy, Scr->tbpm.xlogo, gc, 0, 0, (unsigned) h,
                       (unsigned) h);
        XSetForeground(dpy, gc, 1);
        gcBack = XCreateGC(dpy, Scr->tbpm.xlogo, 0L, NULL);
        XSetForeground(dpy, gcBack, 0);

        /*
         * draw the logo large so that it gets as dense as possible; then white
         * out the edges so that they look crisp
         */
        XmuDrawLogo(dpy, Scr->tbpm.xlogo, gc, gcBack, -1, -1,
                    (unsigned) (h + 2), (unsigned) (h + 2));
        XDrawRectangle(dpy, Scr->tbpm.xlogo, gcBack, 0, 0, (unsigned) (h - 1),
                       (unsigned) (h - 1));

        /*
         * done drawing
         */
        XFreeGC(dpy, gc);
        XFreeGC(dpy, gcBack);
    }
    return Scr->tbpm.xlogo;
}

static Pixmap
CreateResizePixmap(unsigned *widthp, unsigned *heightp)
{
    int h = Scr->TBInfo.width - Scr->TBInfo.border * 2;

    if (h < 1)
        h = 1;

    *widthp = *heightp = (unsigned int) h;
    if (Scr->tbpm.resize == None) {
        XPoint points[3];
        GC gc;
        int w;
        int lw;

        /*
         * create the pixmap
         */
        Scr->tbpm.resize =
            XCreatePixmap(dpy, Scr->Root, (unsigned) h, (unsigned) h, 1);
        gc = XCreateGC(dpy, Scr->tbpm.resize, 0L, NULL);
        XSetForeground(dpy, gc, 0);
        XFillRectangle(dpy, Scr->tbpm.resize, gc, 0, 0, (unsigned) h,
                       (unsigned) h);
        XSetForeground(dpy, gc, 1);
        lw = h / 16;
        if (lw == 1)
            lw = 0;
        XSetLineAttributes(dpy, gc, (unsigned) lw, LineSolid, CapButt,
                           JoinMiter);

        /*
         * draw the resize button,
         */
        w = (h * 2) / 3;
        points[0].x = (short) w;
        points[0].y = 0;
        points[1].x = (short) w;
        points[1].y = (short) w;
        points[2].x = 0;
        points[2].y = (short) w;
        XDrawLines(dpy, Scr->tbpm.resize, gc, points, 3, CoordModeOrigin);
        w = w / 2;
        points[0].x = (short) w;
        points[0].y = 0;
        points[1].x = (short) w;
        points[1].y = (short) w;
        points[2].x = 0;
        points[2].y = (short) w;
        XDrawLines(dpy, Scr->tbpm.resize, gc, points, 3, CoordModeOrigin);

        /*
         * done drawing
         */
        XFreeGC(dpy, gc);
    }
    return Scr->tbpm.resize;
}

static Pixmap
CreateDotPixmap(unsigned *widthp, unsigned *heightp)
{
    int h = Scr->TBInfo.width - Scr->TBInfo.border * 2;

    h = h * 3 / 4;
    if (h < 1)
        h = 1;
    if (!(h & 1))
        h--;
    *widthp = *heightp = (unsigned int) h;
    if (Scr->tbpm.remove == None) {
        GC gc;
        Pixmap pix;

        pix = Scr->tbpm.remove =
            XCreatePixmap(dpy, Scr->Root, (unsigned) h, (unsigned) h, 1);
        gc = XCreateGC(dpy, pix, 0L, NULL);
        XSetLineAttributes(dpy, gc, (unsigned) h, LineSolid, CapRound,
                           JoinRound);
        XSetForeground(dpy, gc, 0L);
        XFillRectangle(dpy, pix, gc, 0, 0, (unsigned) h, (unsigned) h);
        XSetForeground(dpy, gc, 1L);
        XDrawLine(dpy, pix, gc, h / 2, h / 2, h / 2, h / 2);
        XFreeGC(dpy, gc);
    }
    return Scr->tbpm.remove;
}

#define questionmark_width 8
#define questionmark_height 8
static char questionmark_bits[] = {
    0x38, 0x7c, 0x64, 0x30, 0x18, 0x00, 0x18, 0x18
};

static Pixmap
CreateQuestionPixmap(unsigned *widthp, unsigned *heightp)
{
    *widthp = questionmark_width;
    *heightp = questionmark_height;
    if (Scr->tbpm.question == None) {
        Scr->tbpm.question = XCreateBitmapFromData(dpy, Scr->Root,
                                                   questionmark_bits,
                                                   questionmark_width,
                                                   questionmark_height);
    }
    /*
     * this must succeed or else we are in deep trouble elsewhere
     */
    return Scr->tbpm.question;
}

static Pixmap
CreateMenuPixmap(unsigned *widthp, unsigned *heightp)
{
    return CreateMenuIcon(Scr->TBInfo.width - Scr->TBInfo.border * 2,
                          widthp, heightp);
}

Pixmap
CreateMenuIcon(int height, unsigned *widthp, unsigned *heightp)
{
    int h, w;

    h = height;
    w = h * 7 / 8;
    if (h < 1)
        h = 1;
    if (w < 1)
        w = 1;
    *widthp = (unsigned) w;
    *heightp = (unsigned) h;
    if (Scr->tbpm.menu == None) {
        Pixmap pix;
        GC gc;
        int ih, iw;
        int ix, iy;
        int mh, mw;
        int tw, th;
        int lw, lh;
        int lx, ly;
        int lines, dly;
        int off;
        int bw;

        pix = Scr->tbpm.menu =
            XCreatePixmap(dpy, Scr->Root, (unsigned) w, (unsigned) h, 1);
        gc = XCreateGC(dpy, pix, 0L, NULL);
        XSetForeground(dpy, gc, 0L);
        XFillRectangle(dpy, pix, gc, 0, 0, (unsigned) w, (unsigned) h);
        XSetForeground(dpy, gc, 1L);
        ix = 1;
        iy = 1;
        ih = h - iy * 2;
        iw = w - ix * 2;
        off = ih / 8;
        mh = ih - off;
        mw = iw - off;
        bw = mh / 16;
        if (bw == 0 && mw > 2)
            bw = 1;
        tw = mw - bw * 2;
        th = mh - bw * 2;
        XFillRectangle(dpy, pix, gc, ix, iy, (unsigned) mw, (unsigned) mh);
        XFillRectangle(dpy, pix, gc, ix + iw - mw, iy + ih - mh, (unsigned) mw,
                       (unsigned) mh);
        XSetForeground(dpy, gc, 0L);
        XFillRectangle(dpy, pix, gc, ix + bw, iy + bw, (unsigned) tw,
                       (unsigned) th);
        XSetForeground(dpy, gc, 1L);
        lw = tw / 2;
        if ((tw & 1) ^ (lw & 1))
            lw++;
        lx = ix + bw + (tw - lw) / 2;

        lh = th / 2 - bw;
        if ((lh & 1) ^ ((th - bw) & 1))
            lh++;
        ly = iy + bw + (th - bw - lh) / 2;

        lines = 3;
        if ((lh & 1) && lh < 6) {
            lines--;
        }
        dly = lh / (lines - 1);
        while (lines--) {
            XFillRectangle(dpy, pix, gc, lx, ly, (unsigned) lw, (unsigned) bw);
            ly += dly;
        }
        XFreeGC(dpy, gc);
    }
    return Scr->tbpm.menu;
}

void
Bell(int type _X_UNUSED, int percent, Window win _X_UNUSED)
{
#ifdef XKB
    XkbStdBell(dpy, win, percent, type);
#else
    XBell(dpy, percent);
#endif
    return;
}