1 /* File: main-x11.c */
4 * Copyright (c) 1997 Ben Harrison, and others
6 * This software may be copied and distributed for educational, research,
7 * and not for profit purposes provided that this copyright and statement
8 * are included in all such copies.
13 * This file helps Angband work with UNIX/X11 computers.
15 * To use this file, compile with "USE_X11" defined, and link against all
16 * the various "X11" libraries which may be needed.
18 * See also "main-xaw.c".
20 * Part of this file provides a user interface package composed of several
21 * pseudo-objects, including "metadpy" (a display), "infowin" (a window),
22 * "infoclr" (a color), and "infofnt" (a font). Actually, the package was
23 * originally much more interesting, but it was bastardized to keep this
26 * The rest of this file is an implementation of "main-xxx.c" for X11.
28 * Most of this file is by Ben Harrison (benh@phial.com).
32 * The following shell script can be used to launch Angband, assuming that
33 * it was extracted into "~/Angband", and compiled using "USE_X11", on a
34 * Linux machine, with a 1280x1024 screen, using 6 windows (with the given
35 * characteristics), with gamma correction of 1.8 -> (1 / 1.8) * 256 = 142,
36 * and without graphics (add "-g" for graphics). Just copy this comment
37 * into a file, remove the leading " * " characters (and the head/tail of
38 * this comment), and make the file executable.
44 * echo "Launching angband..."
48 * setenv ANGBAND_X11_FONT_0 10x20
49 * setenv ANGBAND_X11_AT_X_0 5
50 * setenv ANGBAND_X11_AT_Y_0 510
53 * setenv ANGBAND_X11_FONT_1 8x13
54 * setenv ANGBAND_X11_AT_X_1 5
55 * setenv ANGBAND_X11_AT_Y_1 22
56 * setenv ANGBAND_X11_ROWS_1 35
59 * setenv ANGBAND_X11_FONT_2 8x13
60 * setenv ANGBAND_X11_AT_X_2 635
61 * setenv ANGBAND_X11_AT_Y_2 182
62 * setenv ANGBAND_X11_ROWS_3 23
65 * setenv ANGBAND_X11_FONT_3 8x13
66 * setenv ANGBAND_X11_AT_X_3 635
67 * setenv ANGBAND_X11_AT_Y_3 22
68 * setenv ANGBAND_X11_ROWS_3 12
70 * # Monster recall window
71 * setenv ANGBAND_X11_FONT_4 6x13
72 * setenv ANGBAND_X11_AT_X_4 817
73 * setenv ANGBAND_X11_AT_Y_4 847
74 * setenv ANGBAND_X11_COLS_4 76
75 * setenv ANGBAND_X11_ROWS_4 11
77 * # Object recall window
78 * setenv ANGBAND_X11_FONT_5 6x13
79 * setenv ANGBAND_X11_AT_X_5 817
80 * setenv ANGBAND_X11_AT_Y_5 520
81 * setenv ANGBAND_X11_COLS_5 76
82 * setenv ANGBAND_X11_ROWS_5 24
84 * # The build directory
88 * setenv ANGBAND_X11_GAMMA 142
91 * ./src/angband -mx11 -- -n6 &
95 #include "system/angband.h"
96 #include "cmd-io/macro-util.h"
97 #include "game-option/runtime-arguments.h"
98 #include "game-option/special-options.h"
99 #include "io/files-util.h"
100 #include "main/sound-definitions-table.h"
101 #include "main/sound-of-music.h"
102 #include "main/x11-type-string.h"
103 #include "system/system-variables.h"
104 #include "term/gameterm.h"
105 #include "term/term-color-types.h"
106 #include "util/int-char-converter.h"
109 * Available graphic modes
111 #define GRAPHICS_NONE 0
112 #define GRAPHICS_ORIGINAL 1
113 #define GRAPHICS_ADAM_BOLT 2
114 #define GRAPHICS_HENGBAND 3
116 #ifndef __MAKEDEPEND__
117 #include <X11/Xlib.h>
118 #include <X11/Xutil.h>
119 #include <X11/keysym.h>
120 #include <X11/keysymdef.h>
122 #include <X11/Xlocale.h>
124 #include <X11/Xatom.h>
125 #endif /* __MAKEDEPEND__ */
129 #include <X11/Xft/Xft.h>
133 * Include some helpful X11 code.
135 #include "maid-x11.c"
141 * 1) On a monochrome (or "fake-monochrome") display, all colors
142 * will be "cast" to "fg," except for the bg color, which is,
143 * obviously, cast to "bg". Thus, one can ignore this setting.
145 * 2) Because of the inner functioning of the color allocation
146 * routines, colors may be specified as (a) a typical color name,
147 * (b) a hexidecimal color specification (preceded by a pound sign),
148 * or (c) by strings such as "fg", "bg", "zg".
150 * 3) Due to the workings of the init routines, many colors
151 * may also be dealt with by their actual pixel values. Note that
152 * the pixel with all bits set is "zg = (1<<metadpy->depth)-1", which
153 * is not necessarily either black or white.
156 /**** Generic Types ****/
159 * An X11 pixell specifier
162 typedef XftColor Pixell;
164 typedef unsigned long Pixell;
168 * The structures defined below
170 typedef struct metadpy metadpy;
171 typedef struct infowin infowin;
172 typedef struct infoclr infoclr;
173 typedef struct infofnt infofnt;
176 * A structure summarizing a given Display.
178 * - The Display itself
179 * - The default Screen for the display
180 * - The virtual root (usually just the root)
181 * - The default colormap (from a macro)
183 * - The "name" of the display
185 * - The socket to listen to for events
187 * - The width of the display screen (from a macro)
188 * - The height of the display screen (from a macro)
189 * - The bit depth of the display screen (from a macro)
191 * - The black Pixell (from a macro)
192 * - The white Pixell (from a macro)
194 * - The background Pixell (default: black)
195 * - The foreground Pixell (default: white)
196 * - The maximal Pixell (Equals: ((2 ^ depth)-1), is usually ugly)
198 * - Bit Flag: Force all colors to black and white (default: !color)
199 * - Bit Flag: Allow the use of color (default: depth > 1)
200 * - Bit Flag: We created 'dpy', and so should nuke it when done.
235 * A Structure summarizing Window Information.
237 * I assume that a window is at most 30000 pixels on a side.
238 * I assume that the root windw is also at most 30000 square.
241 * - The current Input Event Mask
243 * - The location of the window
244 * - The width, height of the window
245 * - The border width of this window
247 * - Byte: 1st Extra byte
249 * - Bit Flag: This window is currently Mapped
250 * - Bit Flag: This window needs to be redrawn
251 * - Bit Flag: This window has been resized
253 * - Bit Flag: We should nuke 'win' when done with it
255 * - Bit Flag: 1st extra flag
256 * - Bit Flag: 2nd extra flag
257 * - Bit Flag: 3rd extra flag
258 * - Bit Flag: 4th extra flag
294 * A Structure summarizing Operation+Color Information
296 * - The actual GC corresponding to this info
298 * - The Foreground Pixell Value
299 * - The Background Pixell Value
301 * - Num (0-15): The operation code (As in Clear, Xor, etc)
302 * - Bit Flag: The GC is in stipple mode
303 * - Bit Flag: Destroy 'gc' at Nuke time.
320 * A Structure to Hold Font Information
322 * - The 'XFontStruct*' (yields the 'Font')
326 * - The default character width
327 * - The default character height
328 * - The default character ascent
330 * - Byte: Pixel offset used during fake mono
332 * - Flag: Force monospacing via 'wid'
333 * - Flag: Nuke info when done
355 /* Set current metadpy (Metadpy) to 'M' */
356 #define Metadpy_set(M) \
359 /* Initialize 'M' using Display 'D' */
360 #define Metadpy_init_dpy(D) \
361 Metadpy_init_2(D,cNULL)
363 /* Initialize 'M' using a Display named 'N' */
364 #define Metadpy_init_name(N) \
365 Metadpy_init_2((Display*)(NULL),N)
367 /* Initialize 'M' using the standard Display */
368 #define Metadpy_init() \
369 Metadpy_init_name("")
371 /* Init an infowin by giving father as an (info_win*) (or NULL), and data */
372 #define Infowin_init_dad(D,X,Y,W,H,B,FG,BG) \
373 Infowin_init_data(((D) ? ((D)->win) : (Window)(None)), \
376 /* Init a top level infowin by pos,size,bord,Colors */
377 #define Infowin_init_top(X,Y,W,H,B,FG,BG) \
378 Infowin_init_data(None,X,Y,W,H,B,FG,BG)
380 /* Request a new standard window by giving Dad infowin and X,Y,W,H */
381 #define Infowin_init_std(D,X,Y,W,H,B) \
382 Infowin_init_dad(D,X,Y,W,H,B,Metadpy->fg,Metadpy->bg)
384 /* Set the current Infowin */
385 #define Infowin_set(I) \
388 /* Set the current Infoclr */
389 #define Infoclr_set(C) \
392 #define Infoclr_init_ppo(F,B,O,M) \
393 Infoclr_init_data(F,B,O,M)
395 #define Infoclr_init_cco(F,B,O,M) \
396 Infoclr_init_ppo(Infoclr_Pixell(F),Infoclr_Pixell(B),O,M)
398 #define Infoclr_init_ppn(F,B,O,M) \
399 Infoclr_init_ppo(F,B,Infoclr_Opcode(O),M)
401 #define Infoclr_init_ccn(F,B,O,M) \
402 Infoclr_init_cco(F,B,Infoclr_Opcode(O),M)
404 /* Set the current infofnt */
405 #define Infofnt_set(I) \
408 /* Errr: Expose Infowin */
409 #define Infowin_expose() \
410 (!(Infowin->redraw = 1))
412 /* Errr: Unxpose Infowin */
413 #define Infowin_unexpose() \
414 (Infowin->redraw = 0)
417 * The "default" values
419 static metadpy metadpy_default;
422 * The "current" variables
424 static metadpy *Metadpy = &metadpy_default;
425 static infowin *Infowin = (infowin*)(NULL);
427 static infowin *Focuswin = (infowin*)(NULL);
429 static infoclr *Infoclr = (infoclr*)(NULL);
430 static infofnt *Infofnt = (infofnt*)(NULL);
433 * Init the current metadpy, with various initialization stuff.
436 * dpy: The Display* to use (if NULL, create it)
437 * name: The name of the Display (if NULL, the current)
440 * If 'name' is NULL, but 'dpy' is set, extract name from dpy
441 * If 'dpy' is NULL, then Create the named Display
442 * If 'name' is NULL, and so is 'dpy', use current Display
444 * Return -1 if no Display given, and none can be opened.
446 static errr Metadpy_init_2(Display *dpy, concptr name)
448 metadpy *m = Metadpy;
451 dpy = XOpenDisplay(name);
452 if (!dpy) return (-1);
462 m->screen = DefaultScreenOfDisplay(dpy);
463 m->root = RootWindowOfScreen(m->screen);
464 m->cmap = DefaultColormapOfScreen(m->screen);
465 m->name = DisplayString(dpy);
466 m->fd = ConnectionNumber(Metadpy->dpy);
467 m->width = WidthOfScreen(m->screen);
468 m->height = HeightOfScreen(m->screen);
469 m->depth = DefaultDepthOfScreen(m->screen);
472 Visual *vis = DefaultVisual(dpy, 0);
473 XftColorAllocName(dpy, vis, m->cmap, "black", &m->black);
474 XftColorAllocName(dpy, vis, m->cmap, "white", &m->white);
476 m->black = BlackPixelOfScreen(m->screen);
477 m->white = WhitePixelOfScreen(m->screen);
484 m->zg = (1 << m->depth) - 1;
487 m->color = ((m->depth > 1) ? 1 : 0);
488 m->mono = ((m->color) ? 0 : 1);
493 * General Flush/ Sync/ Discard routine
495 static errr Metadpy_update(int flush, int sync, int discard)
497 if (flush) XFlush(Metadpy->dpy);
498 if (sync) XSync(Metadpy->dpy, discard);
507 static errr Metadpy_do_beep(void)
509 XBell(Metadpy->dpy, 100);
514 * Set the name (in the title bar) of Infowin
516 static errr Infowin_set_name(concptr name)
523 st = XStringListToTextProperty(&bp, 1, &tp);
524 if (st) XSetWMName(Metadpy->dpy, Infowin->win, &tp);
529 * Prepare a new 'infowin'.
531 static errr Infowin_prepare(Window xid)
533 infowin *iwin = Infowin;
535 XWindowAttributes xwa;
537 unsigned int w, h, b, d;
539 XGetGeometry(Metadpy->dpy, xid, &tmp_win, &x, &y, &w, &h, &b, &d);
542 Visual *vis = DefaultVisual(Metadpy->dpy, 0);
543 if (vis->class != TrueColor) {
544 quit_fmt("Display does not support truecolor.\n");
546 iwin->draw = XftDrawCreate(Metadpy->dpy, iwin->win, vis, Metadpy->cmap);
555 XGetWindowAttributes(Metadpy->dpy, xid, &xwa);
556 iwin->mask = xwa.your_event_mask;
557 iwin->mapped = ((xwa.map_state == IsUnmapped) ? 0 : 1);
563 * Init an infowin by giving some data.
566 * dad: The Window that should own this Window (if any)
567 * x,y: The position of this Window
568 * w,h: The size of this Window
569 * b,d: The border width and pixel depth
572 * If 'dad == None' assume 'dad == root'
574 static errr Infowin_init_data(Window dad, int x, int y, int w, int h,
575 int b, Pixell fg, Pixell bg)
578 (void)WIPE(Infowin, infowin);
579 if (dad == None) dad = Metadpy->root;
582 xid = XCreateSimpleWindow(Metadpy->dpy, dad, x, y, w, h, b, fg.pixel, bg.pixel);
584 xid = XCreateSimpleWindow(Metadpy->dpy, dad, x, y, w, h, b, fg, bg);
587 XSelectInput(Metadpy->dpy, xid, 0L);
589 return (Infowin_prepare(xid));
593 * Modify the event mask of an Infowin
595 static errr Infowin_set_mask(long mask)
597 Infowin->mask = mask;
598 XSelectInput(Metadpy->dpy, Infowin->win, Infowin->mask);
603 * Request that Infowin be mapped
605 static errr Infowin_map(void)
607 XMapWindow(Metadpy->dpy, Infowin->win);
612 * Request that Infowin be raised
614 static errr Infowin_raise(void)
616 XRaiseWindow(Metadpy->dpy, Infowin->win);
621 * Request that Infowin be moved to a new location
623 static errr Infowin_impell(int x, int y)
625 XMoveWindow(Metadpy->dpy, Infowin->win, x, y);
632 static errr Infowin_resize(int w, int h)
634 XResizeWindow(Metadpy->dpy, Infowin->win, w, h);
639 * Visually clear Infowin
641 static errr Infowin_wipe(void)
643 XClearWindow(Metadpy->dpy, Infowin->win);
648 * A NULL terminated pair list of legal "operation names"
650 * Pairs of values, first is texttual name, second is the string
651 * holding the decimal value that the operation corresponds to.
653 static concptr opcode_pairs[] =
672 "+copyInverted", "12",
679 * Parse a word into an operation "code"
682 * str: A string, hopefully representing an Operation
685 * 0-15: if 'str' is a valid Operation
686 * -1: if 'str' could not be parsed
688 static int Infoclr_Opcode(concptr str)
691 for (i = 0; opcode_pairs[i*2]; ++i)
693 if (streq(opcode_pairs[i*2], str))
695 return (atoi(opcode_pairs[i*2+1]));
703 * Initialize an infoclr with some data
706 * fg: The Pixell for the requested Foreground (see above)
707 * bg: The Pixell for the requested Background (see above)
708 * op: The Opcode for the requested Operation (see above)
709 * stip: The stipple mode
711 static errr Infoclr_init_data(Pixell fg, Pixell bg, int op, int stip)
713 infoclr *iclr = Infoclr;
718 unsigned long gc_mask;
722 if (bg > Metadpy->zg) return (-1);
723 if (fg > Metadpy->zg) return (-1);
724 if ((op < 0) || (op > 15)) return (-1);
729 if (op == 6) gcv.background = 0;
730 if (op == 6) gcv.foreground = (bg ^ fg);
732 gcv.fill_style = (stip ? FillStippled : FillSolid);
733 gcv.graphics_exposures = False;
734 gc_mask = (GCFunction | GCBackground | GCForeground | GCFillStyle | GCGraphicsExposures);
735 gc = XCreateGC(Metadpy->dpy, Metadpy->root, gc_mask, &gcv);
738 (void)WIPE(iclr, infoclr);
748 iclr->stip = stip ? 1 : 0;
753 * Change the 'fg' for an infoclr
756 * fg: The Pixell for the requested Foreground (see above)
758 static errr Infoclr_change_fg(Pixell fg)
760 infoclr *iclr = Infoclr;
765 if (fg > Metadpy->zg) return (-1);
767 XSetForeground(Metadpy->dpy, iclr->gc, fg);
774 * Prepare a new 'infofnt'
777 static errr Infofnt_prepare(XftFont *info)
779 static errr Infofnt_prepare(XFontSet info)
782 infofnt *ifnt = Infofnt;
786 XFontStruct **fontinfo;
789 int ascent, descent, width;
796 ifnt->asc = info->ascent;
797 ifnt->hgt = info->ascent + info->descent;
798 const char *text = "A";
800 XftTextExtentsUtf8(Metadpy->dpy, info, (FcChar8*)text, strlen(text), &extent);
801 ifnt->wid = extent.xOff;
803 n_fonts = XFontsOfFontSet(info, &fontinfo, &fontname);
805 ascent = descent = width = 0;
806 while(n_fonts-- > 0){
807 cs = &((*fontinfo)->max_bounds);
808 if(ascent < (*fontinfo)->ascent) ascent = (*fontinfo)->ascent;
809 if(descent < (*fontinfo)->descent) descent = (*fontinfo)->descent;
810 if(((*fontinfo)->max_byte1) > 0){
811 /* 多バイト文字の場合は幅半分(端数切り上げ)で評価する */
812 if(width < (cs->width+1)/2) width = (cs->width+1)/2;
814 if(width < cs->width) width = cs->width;
820 ifnt->hgt = ascent + descent;
825 ifnt->twid = 2 * ifnt->wid;
827 ifnt->twid = ifnt->wid;
833 * Init an infofnt by its Name
836 * name: The name of the requested Font
838 static void Infofnt_init_data(concptr name)
850 if (!name || !*name) quit("Missing font!");
853 info = XftFontOpenName(Metadpy->dpy, 0, name);
854 /* TODO: error handling */
856 info = XCreateFontSet(Metadpy->dpy, name, &missing_list, &missing_count, &default_font);
857 if(missing_count > 0){
858 printf("missing font(s): \n");
859 while(missing_count-- > 0){
860 printf("\t%s\n", missing_list[missing_count]);
862 XFreeStringList(missing_list);
866 if (!info) quit_fmt("Failed to find font:\"%s\"", name);
868 (void)WIPE(Infofnt, infofnt);
869 if (Infofnt_prepare(info))
872 XftFontClose(Metadpy->dpy, info);
874 XFreeFontSet(Metadpy->dpy, info);
876 quit_fmt("Failed to prepare font:\"%s\"", name);
879 Infofnt->name = string_make(name);
886 static errr Infofnt_text_std(int x, int y, concptr str, int len)
888 if (!str || !*str) return (-1);
890 if (len < 0) len = strlen(str);
892 y = (y * Infofnt->hgt) + Infofnt->asc + Infowin->oy;
893 x = (x * Infofnt->wid) + Infowin->ox;
898 for (i = 0; i < len; ++i)
900 XDrawImageString(Metadpy->dpy, Infowin->win, Infoclr->gc, x + i * Infofnt->wid + Infofnt->off, y, str + i, 1);
906 iconv_t cd = iconv_open("UTF-8", "EUC-JP");
908 size_t outlen = len * 2;
909 char *kanji = malloc(outlen);
910 char *sp; char *kp = kanji;
912 angband_strcpy(sbuf, str, sizeof(sbuf));
914 iconv(cd, &sp, &inlen, &kp, &outlen);
918 XftDraw* draw = Infowin->draw;
923 r.width = Infofnt->wid*len;
924 r.height = Infofnt->hgt;
925 XftDrawSetClipRectangles(draw, x, y-Infofnt->asc, &r, 1);
926 XftDrawRect(draw, &Infoclr->bg, x, y-Infofnt->asc, Infofnt->wid*len, Infofnt->hgt);
927 XftDrawStringUtf8(draw, &Infoclr->fg, Infofnt->info, x, y,
928 (FcChar8*)kanji, kp - kanji);
929 XftDrawSetClip(draw, 0);
931 XmbDrawImageString(Metadpy->dpy, Infowin->win, Infofnt->info,
932 Infoclr->gc, x, y, kanji, kp-kanji);
941 * Painting where text would be
943 static errr Infofnt_text_non(int x, int y, concptr str, int len)
946 if (len < 0) len = strlen(str);
948 w = len * Infofnt->wid;
949 x = x * Infofnt->wid + Infowin->ox;
951 y = y * h + Infowin->oy;
954 XftDrawRect(Infowin->draw, &Infoclr->fg, x, y, w, h);
956 XFillRectangle(Metadpy->dpy, Infowin->win, Infoclr->gc, x, y, w, h);
963 * Angband specific code follows... (ANGBAND)
967 * Hack -- cursor color
974 static infoclr *clr[256];
977 * Color info (unused, red, green, blue).
979 static byte color_table[256][4];
984 typedef struct term_data term_data;
987 * A structure for each "term"
1001 * The number of term data structures
1003 #define MAX_TERM_DATA 8
1006 * The array of term data structures
1008 static term_data data[MAX_TERM_DATA];
1011 /* Use short names for the most commonly used elements of various structures. */
1012 #define DPY (Metadpy->dpy)
1013 #define WIN (Infowin->win)
1015 /* Describe a set of co-ordinates. */
1016 typedef struct co_ord co_ord;
1024 * A special structure to store information about the text currently
1027 typedef struct x11_selection_type x11_selection_type;
1028 struct x11_selection_type
1030 bool select; /* The selection is currently in use. */
1031 bool drawn; /* The selection is currently displayed. */
1032 term *t; /* The window where the selection is found. */
1033 co_ord init; /* The starting co-ordinates. */
1034 co_ord cur; /* The end co-ordinates (the current ones if still copying). */
1035 co_ord old; /* The previous end co-ordinates. */
1036 Time time; /* The time at which the selection was finalised. */
1039 static x11_selection_type s_ptr[1];
1042 * Process a keypress event
1044 * Also appears in "main-xaw.c".
1046 static void react_keypress(XKeyEvent *xev)
1048 int i, n, mc, ms, mo, mx;
1050 XKeyEvent *ev = (XKeyEvent*)(xev);
1056 int valid_keysym = TRUE;
1060 if(Focuswin && Focuswin->xic){
1062 n = XmbLookupString(Focuswin->xic, ev, buf, 125, &ks, &status);
1063 if(status == XBufferOverflow){
1064 printf("Input is too long, and dropped\n");
1067 if(status != XLookupKeySym && status != XLookupBoth){
1068 valid_keysym = FALSE;
1071 n = XLookupString(ev, buf, 125, &ks, NULL);
1074 n = XLookupString(ev, buf, 125, &ks, NULL);
1081 for (i = 0; buf[i]; i++) Term_keypress(buf[i]);
1087 if (IsModifierKey(ks)) return;
1090 mc = (ev->state & ControlMask) ? TRUE : FALSE;
1091 ms = (ev->state & ShiftMask) ? TRUE : FALSE;
1092 mo = (ev->state & Mod1Mask) ? TRUE : FALSE;
1093 mx = (ev->state & Mod2Mask) ? TRUE : FALSE;
1094 if (n && !mo && !mx && !IsSpecialKey(ks))
1096 for (i = 0; buf[i]; i++) Term_keypress(buf[i]);
1105 Term_keypress(ESCAPE);
1111 Term_keypress('\r');
1117 Term_keypress('\t');
1123 Term_keypress(0x7f);
1128 Term_keypress('\010');
1135 sprintf(msg, "%c%s%s%s%s_%lX%c", 31,
1136 mc ? "N" : "", ms ? "S" : "",
1137 mo ? "O" : "", mx ? "M" : "",
1138 (unsigned long)(ks), 13);
1142 sprintf(msg, "%c%s%s%s%sK_%X%c", 31,
1143 mc ? "N" : "", ms ? "S" : "",
1144 mo ? "O" : "", mx ? "M" : "",
1148 for (i = 0; msg[i]; i++) Term_keypress(msg[i]);
1150 if (n && (macro_find_exact(msg) < 0))
1152 macro_add(msg, buf);
1157 * Find the square a particular pixel is part of.
1159 static void pixel_to_square(int * const x, int * const y,
1160 const int ox, const int oy)
1162 (*x) = (ox - Infowin->ox) / Infofnt->wid;
1163 (*y) = (oy - Infowin->oy) / Infofnt->hgt;
1167 * Find the pixel at the top-left corner of a square.
1169 static void square_to_pixel(int * const x, int * const y,
1170 const int ox, const int oy)
1172 (*x) = ox * Infofnt->wid + Infowin->ox;
1173 (*y) = oy * Infofnt->hgt + Infowin->oy;
1177 * Convert co-ordinates from starting corner/opposite corner to minimum/maximum.
1179 static void sort_co_ord(co_ord *min, co_ord *max,
1180 const co_ord *b, const co_ord *a)
1182 min->x = MIN(a->x, b->x);
1183 min->y = MIN(a->y, b->y);
1184 max->x = MAX(a->x, b->x);
1185 max->y = MAX(a->y, b->y);
1189 * Remove the selection by redrawing it.
1191 static void mark_selection_clear(int x1, int y1, int x2, int y2)
1193 Term_redraw_section(x1,y1,x2,y2);
1197 * Select an area by drawing a grey box around it.
1198 * NB. These two functions can cause flicker as the selection is modified,
1199 * as the game redraws the entire marked section.
1201 static void mark_selection_mark(int x1, int y1, int x2, int y2)
1203 square_to_pixel(&x1, &y1, x1, y1);
1204 square_to_pixel(&x2, &y2, x2, y2);
1206 XftDrawRect(Infowin->draw, &clr[2]->fg, x1, y1, x2-x1+Infofnt->wid - 1, y2-y1+Infofnt->hgt - 1);
1208 XDrawRectangle(Metadpy->dpy, Infowin->win, clr[2]->gc, x1, y1,
1209 x2-x1+Infofnt->wid - 1, y2-y1+Infofnt->hgt - 1);
1214 * Mark a selection by drawing boxes around it (for now).
1216 static void mark_selection(void)
1220 bool draw = s_ptr->select;
1221 bool clear = s_ptr->drawn;
1222 if (s_ptr->t != old) Term_activate(s_ptr->t);
1226 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->old);
1227 mark_selection_clear(min.x, min.y, max.x, max.y);
1231 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->cur);
1232 mark_selection_mark(min.x, min.y, max.x, max.y);
1235 if (s_ptr->t != old) Term_activate(old);
1237 s_ptr->old.x = s_ptr->cur.x;
1238 s_ptr->old.y = s_ptr->cur.y;
1239 s_ptr->drawn = s_ptr->select;
1243 * Forget a selection for one reason or another.
1245 static void copy_x11_release(void)
1247 s_ptr->select = FALSE;
1252 * Start to select some text on the screen.
1254 static void copy_x11_start(int x, int y)
1256 if (s_ptr->select) copy_x11_release();
1259 s_ptr->init.x = s_ptr->cur.x = s_ptr->old.x = x;
1260 s_ptr->init.y = s_ptr->cur.y = s_ptr->old.y = y;
1264 * Respond to movement of the mouse when selecting text.
1266 static void copy_x11_cont(int x, int y, unsigned int buttons)
1268 x = MIN(MAX(x, 0), Term->wid-1);
1269 y = MIN(MAX(y, 0), Term->hgt-1);
1270 if (~buttons & Button1Mask) return;
1271 if (s_ptr->t != Term) return;
1272 if (x == s_ptr->old.x && y == s_ptr->old.y && s_ptr->select) return;
1274 s_ptr->select = TRUE;
1281 * Respond to release of the left mouse button by putting the selected text in
1282 * the primary buffer.
1284 static void copy_x11_end(const Time time)
1286 if (!s_ptr->select) return;
1287 if (s_ptr->t != Term) return;
1290 XSetSelectionOwner(Metadpy->dpy, XA_PRIMARY, Infowin->win, time);
1291 if (XGetSelectionOwner(Metadpy->dpy, XA_PRIMARY) != Infowin->win)
1293 s_ptr->select = FALSE;
1298 static Atom xa_targets, xa_timestamp, xa_text, xa_compound_text;
1301 * Set the required variable atoms at start-up to avoid errors later.
1303 static void set_atoms(void)
1305 xa_targets = XInternAtom(DPY, "TARGETS", False);
1306 xa_timestamp = XInternAtom(DPY, "TIMESTAMP", False);
1307 xa_text = XInternAtom(DPY, "TEXT", False);
1308 xa_compound_text = XInternAtom(DPY, "COMPOUND_TEXT", False);
1311 static Atom request_target = 0;
1314 * Send a message to request that the PRIMARY buffer be sent here.
1316 static void paste_x11_request(Atom target, const Time time)
1318 Atom property = XInternAtom(DPY, "__COPY_TEXT", False);
1319 if (XGetSelectionOwner(DPY, XA_PRIMARY) == None)
1322 /* bell("No selection found."); */
1326 request_target = target;
1327 XConvertSelection(DPY, XA_PRIMARY, target, property, WIN, time);
1331 * Add the contents of the PRIMARY buffer to the input queue.
1333 * Hack - This doesn't use the "time" of the event, and so accepts anything a
1334 * client tries to send it.
1336 static void paste_x11_accept(const XSelectionEvent *ptr)
1339 const long offset = 0;
1340 const long length = 32000;
1341 XTextProperty xtextproperty;
1343 Atom property = XInternAtom(DPY, "__COPY_TEXT", False);
1344 if (ptr->property == None)
1346 if (request_target == xa_compound_text)
1348 paste_x11_request(XA_STRING, ptr->time);
1353 plog("Paste failure (remote client could not send).");
1359 if (ptr->selection != XA_PRIMARY)
1361 plog("Paste failure (remote client did not send primary selection).");
1365 if (ptr->target != request_target)
1367 plog("Paste failure (selection in unknown format).");
1371 if (XGetWindowProperty(Metadpy->dpy, Infowin->win, property, offset,
1372 length, TRUE, request_target,
1373 &xtextproperty.encoding,
1374 &xtextproperty.format,
1375 &xtextproperty.nitems,
1377 &xtextproperty.value)
1383 if (request_target == xa_compound_text)
1388 XmbTextPropertyToTextList(DPY, &xtextproperty, &list, &count);
1394 for (i = 0; i < count; i++)
1396 err = type_string(list[i], 0);
1400 XFreeStringList(list);
1405 err = type_string((char *)xtextproperty.value, xtextproperty.nitems);
1408 XFree(xtextproperty.value);
1411 plog("Paste failure (too much text selected).");
1416 * Add a character to a string in preparation for sending it to another
1417 * client as a STRING.
1418 * This doesn't change anything, as clients tend not to have difficulty in
1419 * receiving this format (although the standard specifies a restricted set).
1420 * Strings do not have a colour.
1422 static bool paste_x11_send_text(XSelectionRequestEvent *rq)
1432 if (rq->time < s_ptr->time) return FALSE;
1434 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->cur);
1435 if (XGetSelectionOwner(DPY, XA_PRIMARY) != WIN)
1440 XDeleteProperty(DPY, rq->requestor, rq->property);
1442 for (n = 0, y = 0; y < Term->hgt; y++)
1447 if (y < min.y) continue;
1448 if (y > max.y) break;
1450 for (l = 0, x = 0; x < Term->wid; x++)
1453 if (x > max.x) break;
1455 Term_what(x, y, &a, &c);
1456 if (1 == kanji) kanji = 2;
1457 else if (iskanji(c)) kanji = 1;
1460 if (x < min.x) continue;
1463 * A single kanji character was divided in two...
1464 * Delete the garbage.
1466 if ((2 == kanji && x == min.x) ||
1467 (1 == kanji && x == max.x))
1470 if (x > max.x) break;
1471 if (x < min.x) continue;
1473 Term_what(x, y, &a, &c);
1480 while (buf[l-1] == ' ') l--;
1489 list[n++] = (char *)string_make(buf);
1493 if (rq->target == XA_STRING)
1495 for (n = 0; list[n]; n++)
1497 XChangeProperty(DPY, rq->requestor, rq->property, rq->target, 8,
1498 PropModeAppend, (unsigned char *)list[n], strlen(list[n]));
1501 else if (rq->target == xa_text ||
1502 rq->target == xa_compound_text)
1504 XTextProperty text_prop;
1505 XICCEncodingStyle style;
1507 if (rq->target == xa_text)
1508 style = XStdICCTextStyle;
1510 style = XCompoundTextStyle;
1513 XmbTextListToTextProperty(DPY, list, n, style, &text_prop))
1515 XChangeProperty(DPY,
1523 XFree(text_prop.value);
1527 for (n = 0; list[n]; n++)
1529 string_free(list[n]);
1536 * Send some text requested by another X client.
1538 static void paste_x11_send(XSelectionRequestEvent *rq)
1541 XSelectionEvent *ptr = &(event.xselection);
1543 ptr->type = SelectionNotify;
1544 ptr->property = rq->property;
1545 ptr->display = rq->display;
1546 ptr->requestor = rq->requestor;
1547 ptr->selection = rq->selection;
1548 ptr->target = rq->target;
1549 ptr->time = rq->time;
1552 * Paste the appropriate information for each target type.
1553 * Note that this currently rejects MULTIPLE targets.
1556 if (rq->target == XA_STRING ||
1557 rq->target == xa_text ||
1558 rq->target == xa_compound_text)
1560 if (!paste_x11_send_text(rq))
1561 ptr->property = None;
1563 else if (rq->target == xa_targets)
1565 Atom target_list[4];
1566 target_list[0] = XA_STRING;
1567 target_list[1] = xa_text;
1568 target_list[2] = xa_compound_text;
1569 target_list[3] = xa_targets;
1570 XChangeProperty(DPY, rq->requestor, rq->property, rq->target,
1571 (8 * sizeof(target_list[0])), PropModeReplace,
1572 (unsigned char *)target_list,
1573 (sizeof(target_list) / sizeof(target_list[0])));
1575 else if (rq->target == xa_timestamp)
1577 XChangeProperty(DPY, rq->requestor, rq->property, rq->target,
1578 (8 * sizeof(Time)), PropModeReplace,
1579 (unsigned char *)s_ptr->time, 1);
1583 ptr->property = None;
1586 XSendEvent(DPY, rq->requestor, FALSE, NoEventMask, &event);
1590 * Handle various events conditional on presses of a mouse button.
1592 static void handle_button(Time time, int x, int y, int button, bool press)
1594 pixel_to_square(&x, &y, x, y);
1596 if (press && button == 1) copy_x11_start(x, y);
1597 if (!press && button == 1) copy_x11_end(time);
1598 if (!press && button == 2) paste_x11_request(xa_compound_text, time);
1604 static errr CheckEvent(bool wait)
1606 term_data *old_td = (term_data*)(Term->data);
1608 XEvent xev_body, *xev = &xev_body;
1610 term_data *td = NULL;
1611 infowin *iwin = NULL;
1620 if (!wait && !XPending(Metadpy->dpy)) return (1);
1622 if (s_ptr->select && !s_ptr->drawn) mark_selection();
1624 XNextEvent(Metadpy->dpy, xev);
1627 } while (XFilterEvent(xev, xev->xany.window));
1630 if (xev->type == MappingNotify)
1632 XRefreshKeyboardMapping(&xev->xmapping);
1636 for (i = 0; i < MAX_TERM_DATA; i++)
1638 if (!data[i].win) continue;
1639 if (xev->xany.window == data[i].win->win)
1647 if (!td || !iwin) return (0);
1649 Term_activate(&td->t);
1656 bool press = (xev->type == ButtonPress);
1657 int x = xev->xbutton.x;
1658 int y = xev->xbutton.y;
1660 if (xev->xbutton.button == Button1) z = 1;
1661 else if (xev->xbutton.button == Button2) z = 2;
1662 else if (xev->xbutton.button == Button3) z = 3;
1663 else if (xev->xbutton.button == Button4) z = 4;
1664 else if (xev->xbutton.button == Button5) z = 5;
1667 handle_button(xev->xbutton.time, x, y, z, press);
1677 int x = xev->xmotion.x;
1678 int y = xev->xmotion.y;
1679 unsigned int z = xev->xmotion.state;
1680 pixel_to_square(&x, &y, x, y);
1681 copy_x11_cont(x, y, z);
1684 case SelectionNotify:
1686 paste_x11_accept(&(xev->xselection));
1689 case SelectionRequest:
1691 paste_x11_send(&(xev->xselectionrequest));
1694 case SelectionClear:
1696 s_ptr->select = FALSE;
1706 Term_activate(&old_td->t);
1707 react_keypress(&(xev->xkey));
1713 x1 = (xev->xexpose.x - Infowin->ox)/Infofnt->wid;
1714 x2 = (xev->xexpose.x + xev->xexpose.width -
1715 Infowin->ox)/Infofnt->wid;
1717 y1 = (xev->xexpose.y - Infowin->oy)/Infofnt->hgt;
1718 y2 = (xev->xexpose.y + xev->xexpose.height -
1719 Infowin->oy)/Infofnt->hgt;
1721 Term_redraw_section(x1, y1, x2, y2);
1726 Infowin->mapped = 1;
1727 Term->mapped_flag = TRUE;
1732 Infowin->mapped = 0;
1733 Term->mapped_flag = FALSE;
1736 case ConfigureNotify:
1738 int cols, rows, wid, hgt;
1739 int ox = Infowin->ox;
1740 int oy = Infowin->oy;
1741 Infowin->x = xev->xconfigure.x;
1742 Infowin->y = xev->xconfigure.y;
1743 Infowin->w = xev->xconfigure.width;
1744 Infowin->h = xev->xconfigure.height;
1745 cols = ((Infowin->w - (ox + ox)) / td->fnt->wid);
1746 rows = ((Infowin->h - (oy + oy)) / td->fnt->hgt);
1747 if (cols < 1) cols = 1;
1748 if (rows < 1) rows = 1;
1752 if (cols < 80) cols = 80;
1753 if (rows < 24) rows = 24;
1756 wid = cols * td->fnt->wid + (ox + ox);
1757 hgt = rows * td->fnt->hgt + (oy + oy);
1758 Term_resize(cols, rows);
1759 if ((Infowin->w != wid) || (Infowin->h != hgt))
1761 Infowin_set(td->win);
1762 Infowin_resize(wid, hgt);
1771 XSetICFocus(iwin->xic);
1780 XUnsetICFocus(iwin->xic);
1788 Term_activate(&old_td->t);
1789 Infowin_set(old_td->win);
1794 * An array of sound file names
1796 static concptr sound_file[SOUND_MAX];
1799 * Check for existance of a file
1801 static bool check_file(concptr s)
1805 fff = fopen(s, "r");
1806 if (!fff) return (FALSE);
1815 static void init_sound(void)
1820 char dir_xtra_sound[1024];
1821 path_build(dir_xtra_sound, sizeof(dir_xtra_sound), ANGBAND_DIR_XTRA, "sound");
1822 for (i = 1; i < SOUND_MAX; i++)
1824 sprintf(wav, "%s.wav", angband_sound_name[i]);
1825 path_build(buf, sizeof(buf), dir_xtra_sound, wav);
1826 if (check_file(buf)) sound_file[i] = string_make(buf);
1834 * Hack -- make a sound
1836 static errr Term_xtra_x11_sound(int v)
1839 if (!use_sound) return (1);
1840 if ((v < 0) || (v >= SOUND_MAX)) return (1);
1841 if (!sound_file[v]) return (1);
1843 sprintf(buf,"./playwave.sh %s\n", sound_file[v]);
1844 return (system(buf) < 0);
1849 * Handle "activation" of a term
1851 static errr Term_xtra_x11_level(int v)
1853 term_data *td = (term_data*)(Term->data);
1856 Infowin_set(td->win);
1857 Infofnt_set(td->fnt);
1866 static errr Term_xtra_x11_react(void)
1872 for (i = 0; i < 256; i++)
1874 if ((color_table[i][0] != angband_color_table[i][0]) ||
1875 (color_table[i][1] != angband_color_table[i][1]) ||
1876 (color_table[i][2] != angband_color_table[i][2]) ||
1877 (color_table[i][3] != angband_color_table[i][3]))
1880 color_table[i][0] = angband_color_table[i][0];
1881 color_table[i][1] = angband_color_table[i][1];
1882 color_table[i][2] = angband_color_table[i][2];
1883 color_table[i][3] = angband_color_table[i][3];
1884 pixel = create_pixel(Metadpy->dpy,
1888 Infoclr_set(clr[i]);
1889 Infoclr_change_fg(pixel);
1898 * Handle a "special request"
1900 static errr Term_xtra_x11(int n, int v)
1904 case TERM_XTRA_NOISE: Metadpy_do_beep(); return (0);
1905 case TERM_XTRA_SOUND: return (Term_xtra_x11_sound(v));
1907 case TERM_XTRA_FRESH: Metadpy_update(1, 1, 0); return (0);
1909 case TERM_XTRA_FRESH: Metadpy_update(1, 0, 0); return (0);
1911 case TERM_XTRA_BORED: return (CheckEvent(0));
1912 case TERM_XTRA_EVENT: return (CheckEvent(v));
1913 case TERM_XTRA_FLUSH: while (!CheckEvent(FALSE)); return (0);
1914 case TERM_XTRA_LEVEL: return (Term_xtra_x11_level(v));
1915 case TERM_XTRA_CLEAR: Infowin_wipe(); s_ptr->drawn = FALSE; return (0);
1916 case TERM_XTRA_DELAY: usleep(1000 * v); return (0);
1917 case TERM_XTRA_REACT: return (Term_xtra_x11_react());
1924 * Draw the cursor as an inverted rectangle.
1926 * Consider a rectangular outline like "main-mac.c". XXX XXX
1928 static errr Term_curs_x11(int x, int y)
1933 XftDrawRect(Infowin->draw, &xor->fg,
1934 x * Infofnt->wid + Infowin->ox,
1935 y * Infofnt->hgt + Infowin->oy,
1936 Infofnt->wid - 1, Infofnt->hgt - 1);
1937 XftDrawRect(Infowin->draw, &xor->fg,
1938 x * Infofnt->wid + Infowin->ox + 1,
1939 y * Infofnt->hgt + Infowin->oy + 1,
1940 Infofnt->wid - 3, Infofnt->hgt - 3);
1942 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
1943 x * Infofnt->wid + Infowin->ox,
1944 y * Infofnt->hgt + Infowin->oy,
1945 Infofnt->wid - 1, Infofnt->hgt - 1);
1946 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
1947 x * Infofnt->wid + Infowin->ox + 1,
1948 y * Infofnt->hgt + Infowin->oy + 1,
1949 Infofnt->wid - 3, Infofnt->hgt - 3);
1955 Infofnt_text_non(x, y, " ", 1);
1963 * Draw the double width cursor
1965 static errr Term_bigcurs_x11(int x, int y)
1970 XftDrawRect(Infowin->draw, &xor->fg,
1971 x * Infofnt->wid + Infowin->ox,
1972 y * Infofnt->hgt + Infowin->oy,
1973 Infofnt->twid - 1, Infofnt->hgt - 1);
1974 XftDrawRect(Infowin->draw, &xor->fg,
1975 x * Infofnt->wid + Infowin->ox + 1,
1976 y * Infofnt->hgt + Infowin->oy + 1,
1977 Infofnt->twid - 3, Infofnt->hgt - 3);
1979 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
1980 x * Infofnt->wid + Infowin->ox,
1981 y * Infofnt->hgt + Infowin->oy,
1982 Infofnt->twid - 1, Infofnt->hgt - 1);
1983 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
1984 x * Infofnt->wid + Infowin->ox + 1,
1985 y * Infofnt->hgt + Infowin->oy + 1,
1986 Infofnt->twid - 3, Infofnt->hgt - 3);
1992 Infofnt_text_non(x, y, " ", 2);
1999 * Erase some characters.
2001 static errr Term_wipe_x11(int x, int y, int n)
2003 Infoclr_set(clr[TERM_DARK]);
2004 Infofnt_text_non(x, y, "", n);
2005 s_ptr->drawn = FALSE;
2010 * Draw some textual characters.
2012 static errr Term_text_x11(TERM_LEN x, TERM_LEN y, int n, TERM_COLOR a, concptr s)
2014 Infoclr_set(clr[a]);
2015 Infofnt_text_std(x, y, s, n);
2016 s_ptr->drawn = FALSE;
2022 * Draw some graphical characters.
2024 static errr Term_pict_x11(TERM_LEN x, TERM_LEN y, int n, const TERM_COLOR *ap, const char *cp, const TERM_COLOR *tap, const char *tcp)
2037 unsigned long pixel, blank;
2039 term_data *td = (term_data*)(Term->data);
2046 for (i = 0; i < n; ++i, x += td->fnt->wid)
2050 x1 = (c&0x7F) * td->fnt->twid;
2051 y1 = (a&0x7F) * td->fnt->hgt;
2052 if (td->tiles->width < x1 + td->fnt->wid ||
2053 td->tiles->height < y1 + td->fnt->hgt)
2055 XFillRectangle(Metadpy->dpy, td->win->win, clr[0]->gc, x, y, td->fnt->twid, td->fnt->hgt);
2062 x2 = (tc&0x7F) * td->fnt->twid;
2063 y2 = (ta&0x7F) * td->fnt->hgt;
2065 if (((x1 == x2) && (y1 == y2)) ||
2066 !(((byte)ta & 0x80) && ((byte)tc & 0x80)) ||
2067 td->tiles->width < x2 + td->fnt->wid ||
2068 td->tiles->height < y2 + td->fnt->hgt)
2070 XPutImage(Metadpy->dpy, td->win->win,
2075 td->fnt->twid, td->fnt->hgt);
2079 blank = XGetPixel(td->tiles, 0, td->fnt->hgt * 6);
2080 for (k = 0; k < td->fnt->twid; k++)
2082 for (l = 0; l < td->fnt->hgt; l++)
2084 if ((pixel = XGetPixel(td->tiles, x1 + k, y1 + l)) == blank)
2086 pixel = XGetPixel(td->tiles, x2 + k, y2 + l);
2089 XPutPixel(td->TmpImage, k, l, pixel);
2093 XPutImage(Metadpy->dpy, td->win->win,
2097 td->fnt->twid, td->fnt->hgt);
2101 s_ptr->drawn = FALSE;
2107 static void IMDestroyCallback(XIM, XPointer, XPointer);
2110 IMInstantiateCallback(Display *display, XPointer unused1, XPointer unused2)
2113 XIMCallback ximcallback;
2114 XIMStyles *xim_styles = NULL;
2120 xim = XOpenIM(display, NULL, NULL, NULL);
2122 printf("can't open IM\n");
2126 ximcallback.callback = IMDestroyCallback;
2127 ximcallback.client_data = NULL;
2128 XSetIMValues(xim, XNDestroyCallback, &ximcallback, NULL);
2129 XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL);
2130 for (i = 0; i < xim_styles->count_styles; i++){
2131 if(xim_styles->supported_styles[i] == (XIMPreeditNothing | XIMStatusNothing)) break;
2133 if(i >= xim_styles->count_styles){
2134 printf("Sorry, your IM does not support 'Root' preedit style...\n");
2142 for (i = 0; i < MAX_TERM_DATA; i++)
2144 infowin *iwin = data[i].win;
2145 if (!iwin) continue;
2146 iwin->xic = XCreateIC(xim, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing), XNClientWindow, iwin->win, XNFocusWindow, iwin->win, NULL);
2149 printf("Can't create input context for Term%d\n", i);
2153 if(XGetICValues(iwin->xic, XNFilterEvents, &iwin->xic_mask, NULL) != NULL)
2155 iwin->xic_mask = 0L;
2158 XSelectInput(Metadpy->dpy, iwin->win, iwin->mask | iwin->xic_mask);
2164 static void IMDestroyCallback(XIM xim, XPointer client_data, XPointer call_data)
2170 if (call_data == NULL){
2171 XRegisterIMInstantiateCallback(Metadpy->dpy, NULL, NULL, NULL, IMInstantiateCallback, NULL);
2174 for(i = 0; i < MAX_TERM_DATA; i++)
2176 infowin *iwin = data[i].win;
2179 XSelectInput(Metadpy->dpy, iwin->win, iwin->mask);
2180 iwin->xic_mask = 0L;
2185 Metadpy->xim = NULL;
2189 static char force_lower(char a)
2191 return ((isupper((a))) ? tolower((a)) : (a))
2195 * Initialize a term_data
2197 static errr term_data_init(term_data *td, int i)
2201 concptr name = angband_term_name[i];
2231 sprintf(buf, "ANGBAND_X11_FONT_%d", i);
2233 if (!font) font = getenv("ANGBAND_X11_FONT");
2241 font = DEFAULT_X11_FONT_0;
2246 font = DEFAULT_X11_FONT_1;
2251 font = DEFAULT_X11_FONT_2;
2256 font = DEFAULT_X11_FONT_3;
2261 font = DEFAULT_X11_FONT_4;
2266 font = DEFAULT_X11_FONT_5;
2271 font = DEFAULT_X11_FONT_6;
2276 font = DEFAULT_X11_FONT_7;
2281 font = DEFAULT_X11_FONT;
2286 sprintf(buf, "ANGBAND_X11_AT_X_%d", i);
2288 x = (str != NULL) ? atoi(str) : -1;
2290 sprintf(buf, "ANGBAND_X11_AT_Y_%d", i);
2292 y = (str != NULL) ? atoi(str) : -1;
2294 sprintf(buf, "ANGBAND_X11_COLS_%d", i);
2296 val = (str != NULL) ? atoi(str) : -1;
2297 if (val > 0) cols = val;
2299 sprintf(buf, "ANGBAND_X11_ROWS_%d", i);
2301 val = (str != NULL) ? atoi(str) : -1;
2302 if (val > 0) rows = val;
2306 if (cols < 80) cols = 80;
2307 if (rows < 24) rows = 24;
2310 sprintf(buf, "ANGBAND_X11_IBOX_%d", i);
2312 val = (str != NULL) ? atoi(str) : -1;
2313 if (val > 0) ox = val;
2315 sprintf(buf, "ANGBAND_X11_IBOY_%d", i);
2317 val = (str != NULL) ? atoi(str) : -1;
2318 if (val > 0) oy = val;
2320 MAKE(td->fnt, infofnt);
2321 Infofnt_set(td->fnt);
2322 Infofnt_init_data(font);
2324 num = ((i == 0) ? 1024 : 16);
2325 wid = cols * td->fnt->wid + (ox + ox);
2326 hgt = rows * td->fnt->hgt + (oy + oy);
2327 MAKE(td->win, infowin);
2328 Infowin_set(td->win);
2329 Infowin_init_top(x, y, wid, hgt, 0,
2330 Metadpy->fg, Metadpy->bg);
2332 #if defined(USE_XIM)
2333 Infowin_set_mask(ExposureMask | StructureNotifyMask | KeyPressMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask);
2335 Infowin_set_mask(ExposureMask | StructureNotifyMask | KeyPressMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask);
2338 Infowin_set_name(name);
2341 ch = XAllocClassHint();
2343 if (ch == NULL) quit("XAllocClassHint failed");
2345 strcpy(res_name, name);
2346 res_name[0] = force_lower(res_name[0]);
2347 ch->res_name = res_name;
2349 strcpy(res_class, "Angband");
2350 ch->res_class = res_class;
2352 XSetClassHint(Metadpy->dpy, Infowin->win, ch);
2353 sh = XAllocSizeHints();
2354 if (sh == NULL) quit("XAllocSizeHints failed");
2358 sh->flags = PMinSize | PMaxSize;
2359 sh->min_width = 80 * td->fnt->wid + (ox + ox);
2360 sh->min_height = 24 * td->fnt->hgt + (oy + oy);
2361 sh->max_width = 255 * td->fnt->wid + (ox + ox);
2362 sh->max_height = 255 * td->fnt->hgt + (oy + oy);
2366 sh->flags = PMinSize | PMaxSize;
2367 sh->min_width = td->fnt->wid + (ox + ox);
2368 sh->min_height = td->fnt->hgt + (oy + oy);
2369 sh->max_width = 256 * td->fnt->wid + (ox + ox);
2370 sh->max_height = 256 * td->fnt->hgt + (oy + oy);
2373 sh->flags |= PResizeInc;
2374 sh->width_inc = td->fnt->wid;
2375 sh->height_inc = td->fnt->hgt;
2376 sh->flags |= PBaseSize;
2377 sh->base_width = (ox + ox);
2378 sh->base_height = (oy + oy);
2379 XSetWMNormalHints(Metadpy->dpy, Infowin->win, sh);
2383 wh = XAllocWMHints();
2384 if(wh == NULL) quit("XAllocWMHints failed");
2385 wh->flags = InputHint;
2387 XSetWMHints(Metadpy->dpy, Infowin->win, wh);
2390 if ((x >= 0) && (y >= 0)) Infowin_impell(x, y);
2392 term_init(t, cols, rows, num);
2393 t->soft_cursor = TRUE;
2394 t->attr_blank = TERM_WHITE;
2395 t->char_blank = ' ';
2396 t->xtra_hook = Term_xtra_x11;
2397 t->curs_hook = Term_curs_x11;
2398 t->bigcurs_hook = Term_bigcurs_x11;
2399 t->wipe_hook = Term_wipe_x11;
2400 t->text_hook = Term_text_x11;
2407 * Initialization function for an "X11" module to Angband
2409 errr init_x11(int argc, char *argv[])
2412 concptr dpy_name = "";
2416 char filename[1024];
2424 for (i = 1; i < argc; i++)
2426 if (prefix(argv[i], "-d"))
2428 dpy_name = &argv[i][2];
2433 if (prefix(argv[i], "-s"))
2435 smoothRescaling = FALSE;
2439 if (prefix(argv[i], "-a"))
2441 arg_graphics = GRAPHICS_ADAM_BOLT;
2445 if (prefix(argv[i], "-o"))
2447 arg_graphics = GRAPHICS_ORIGINAL;
2452 if (prefix(argv[i], "-b"))
2454 arg_bigtile = use_bigtile = TRUE;
2458 if (prefix(argv[i], "-n"))
2460 num_term = atoi(&argv[i][2]);
2461 if (num_term > MAX_TERM_DATA) num_term = MAX_TERM_DATA;
2462 else if (num_term < 1) num_term = 1;
2466 if (prefix(argv[i], "--"))
2471 plog_fmt("Ignoring option: %s", argv[i]);
2477 setlocale(LC_ALL, "");
2479 #ifdef DEFAULT_LOCALE
2480 if(!strcmp(setlocale(LC_ALL, NULL), "C")){
2481 printf("try default locale \"%s\"\n", DEFAULT_LOCALE);
2482 setlocale(LC_ALL, DEFAULT_LOCALE);
2486 if(!strcmp(setlocale(LC_ALL, NULL), "C"))
2488 printf("WARNING: Locale is not supported. Non-english font may be displayed incorrectly.\n");
2491 if(!XSupportsLocale()){
2492 printf("can't support locale in X\n");
2493 setlocale(LC_ALL, "C");
2496 setlocale(LC_ALL, "C");
2499 #endif /* USE_LOCALE */
2501 if (Metadpy_init_name(dpy_name)) return (-1);
2505 Infoclr_init_ppn(Metadpy->fg, Metadpy->bg, "xor", 0);
2506 for (i = 0; i < 256; ++i)
2509 MAKE(clr[i], infoclr);
2510 Infoclr_set(clr[i]);
2511 color_table[i][0] = angband_color_table[i][0];
2512 color_table[i][1] = angband_color_table[i][1];
2513 color_table[i][2] = angband_color_table[i][2];
2514 color_table[i][3] = angband_color_table[i][3];
2515 pixel = ((i == 0) ? Metadpy->bg : Metadpy->fg);
2518 pixel = create_pixel(Metadpy->dpy,
2524 Infoclr_init_ppn(pixel, Metadpy->bg, "cpy", 0);
2528 for (i = 0; i < num_term; i++)
2530 term_data *td = &data[i];
2531 term_data_init(td, i);
2532 angband_term[i] = Term;
2535 Infowin_set(data[0].win);
2537 Term_activate(&data[0].t);
2542 p = XSetLocaleModifiers("");
2544 p = XSetLocaleModifiers("@im=");
2547 XRegisterIMInstantiateCallback(Metadpy->dpy, NULL, NULL, NULL, IMInstantiateCallback, NULL);
2550 if (arg_sound) init_sound();
2553 switch (arg_graphics)
2555 case GRAPHICS_ORIGINAL:
2556 path_build(filename, sizeof(filename), ANGBAND_DIR_XTRA, "graf/8x8.bmp");
2557 if (0 == fd_close(fd_open(filename, O_RDONLY)))
2559 use_graphics = TRUE;
2560 pict_wid = pict_hgt = 8;
2561 ANGBAND_GRAF = "old";
2564 case GRAPHICS_ADAM_BOLT:
2565 path_build(filename, sizeof(filename), ANGBAND_DIR_XTRA, "graf/16x16.bmp");
2566 if (0 == fd_close(fd_open(filename, O_RDONLY)))
2568 use_graphics = TRUE;
2569 pict_wid = pict_hgt = 16;
2570 ANGBAND_GRAF = "new";
2577 Display *dpy = Metadpy->dpy;
2579 tiles_raw = ReadBMP(dpy, filename);
2580 for (i = 0; i < num_term; i++)
2582 term_data *td = &data[i];
2584 t->pict_hook = Term_pict_x11;
2585 t->higher_pict = TRUE;
2587 ResizeImage(dpy, tiles_raw,
2589 td->fnt->twid, td->fnt->hgt);
2592 for (i = 0; i < num_term; i++)
2594 term_data *td = &data[i];
2596 int depth = DefaultDepth(dpy, DefaultScreen(dpy));
2597 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
2600 jj = (depth - 1) >> 2;
2601 while (jj >>= 1) ii <<= 1;
2602 total = td->fnt->twid * td->fnt->hgt * ii;
2603 TmpData = (char *)malloc(total);
2604 td->TmpImage = XCreateImage(dpy,visual,depth,
2605 ZPixmap, 0, TmpData,
2606 td->fnt->twid, td->fnt->hgt, 8, 0);
2609 #endif /* ! USE_XFT */
2613 #endif /* USE_X11 */