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"
107 #include "util/angband-files.h"
108 #include "util/string-processor.h"
111 * Available graphic modes
113 #define GRAPHICS_NONE 0
114 #define GRAPHICS_ORIGINAL 1
115 #define GRAPHICS_ADAM_BOLT 2
116 #define GRAPHICS_HENGBAND 3
118 #ifndef __MAKEDEPEND__
119 #include <X11/Xlib.h>
120 #include <X11/Xutil.h>
121 #include <X11/keysym.h>
122 #include <X11/keysymdef.h>
124 #include <X11/Xlocale.h>
126 #include <X11/Xatom.h>
127 #endif /* __MAKEDEPEND__ */
131 #include <X11/Xft/Xft.h>
135 * Include some helpful X11 code.
137 #include "maid-x11.c"
143 * 1) On a monochrome (or "fake-monochrome") display, all colors
144 * will be "cast" to "fg," except for the bg color, which is,
145 * obviously, cast to "bg". Thus, one can ignore this setting.
147 * 2) Because of the inner functioning of the color allocation
148 * routines, colors may be specified as (a) a typical color name,
149 * (b) a hexidecimal color specification (preceded by a pound sign),
150 * or (c) by strings such as "fg", "bg", "zg".
152 * 3) Due to the workings of the init routines, many colors
153 * may also be dealt with by their actual pixel values. Note that
154 * the pixel with all bits set is "zg = (1<<metadpy->depth)-1", which
155 * is not necessarily either black or white.
158 /**** Generic Types ****/
161 * An X11 pixell specifier
164 typedef XftColor Pixell;
166 typedef unsigned long Pixell;
170 * The structures defined below
172 typedef struct metadpy metadpy;
173 typedef struct infowin infowin;
174 typedef struct infoclr infoclr;
175 typedef struct infofnt infofnt;
178 * A structure summarizing a given Display.
180 * - The Display itself
181 * - The default Screen for the display
182 * - The virtual root (usually just the root)
183 * - The default colormap (from a macro)
185 * - The "name" of the display
187 * - The socket to listen to for events
189 * - The width of the display screen (from a macro)
190 * - The height of the display screen (from a macro)
191 * - The bit depth of the display screen (from a macro)
193 * - The black Pixell (from a macro)
194 * - The white Pixell (from a macro)
196 * - The background Pixell (default: black)
197 * - The foreground Pixell (default: white)
198 * - The maximal Pixell (Equals: ((2 ^ depth)-1), is usually ugly)
200 * - Bit Flag: Force all colors to black and white (default: !color)
201 * - Bit Flag: Allow the use of color (default: depth > 1)
202 * - Bit Flag: We created 'dpy', and so should nuke it when done.
237 * A Structure summarizing Window Information.
239 * I assume that a window is at most 30000 pixels on a side.
240 * I assume that the root windw is also at most 30000 square.
243 * - The current Input Event Mask
245 * - The location of the window
246 * - The width, height of the window
247 * - The border width of this window
249 * - Byte: 1st Extra byte
251 * - Bit Flag: This window is currently Mapped
252 * - Bit Flag: This window needs to be redrawn
253 * - Bit Flag: This window has been resized
255 * - Bit Flag: We should nuke 'win' when done with it
257 * - Bit Flag: 1st extra flag
258 * - Bit Flag: 2nd extra flag
259 * - Bit Flag: 3rd extra flag
260 * - Bit Flag: 4th extra flag
296 * A Structure summarizing Operation+Color Information
298 * - The actual GC corresponding to this info
300 * - The Foreground Pixell Value
301 * - The Background Pixell Value
303 * - Num (0-15): The operation code (As in Clear, Xor, etc)
304 * - Bit Flag: The GC is in stipple mode
305 * - Bit Flag: Destroy 'gc' at Nuke time.
322 * A Structure to Hold Font Information
324 * - The 'XFontStruct*' (yields the 'Font')
328 * - The default character width
329 * - The default character height
330 * - The default character ascent
332 * - Byte: Pixel offset used during fake mono
334 * - Flag: Force monospacing via 'wid'
335 * - Flag: Nuke info when done
357 /* Set current metadpy (Metadpy) to 'M' */
358 #define Metadpy_set(M) \
361 /* Initialize 'M' using Display 'D' */
362 #define Metadpy_init_dpy(D) \
363 Metadpy_init_2(D,cNULL)
365 /* Initialize 'M' using a Display named 'N' */
366 #define Metadpy_init_name(N) \
367 Metadpy_init_2((Display*)(NULL),N)
369 /* Initialize 'M' using the standard Display */
370 #define Metadpy_init() \
371 Metadpy_init_name("")
373 /* Init an infowin by giving father as an (info_win*) (or NULL), and data */
374 #define Infowin_init_dad(D,X,Y,W,H,B,FG,BG) \
375 Infowin_init_data(((D) ? ((D)->win) : (Window)(None)), \
378 /* Init a top level infowin by pos,size,bord,Colors */
379 #define Infowin_init_top(X,Y,W,H,B,FG,BG) \
380 Infowin_init_data(None,X,Y,W,H,B,FG,BG)
382 /* Request a new standard window by giving Dad infowin and X,Y,W,H */
383 #define Infowin_init_std(D,X,Y,W,H,B) \
384 Infowin_init_dad(D,X,Y,W,H,B,Metadpy->fg,Metadpy->bg)
386 /* Set the current Infowin */
387 #define Infowin_set(I) \
390 /* Set the current Infoclr */
391 #define Infoclr_set(C) \
394 #define Infoclr_init_ppo(F,B,O,M) \
395 Infoclr_init_data(F,B,O,M)
397 #define Infoclr_init_cco(F,B,O,M) \
398 Infoclr_init_ppo(Infoclr_Pixell(F),Infoclr_Pixell(B),O,M)
400 #define Infoclr_init_ppn(F,B,O,M) \
401 Infoclr_init_ppo(F,B,Infoclr_Opcode(O),M)
403 #define Infoclr_init_ccn(F,B,O,M) \
404 Infoclr_init_cco(F,B,Infoclr_Opcode(O),M)
406 /* Set the current infofnt */
407 #define Infofnt_set(I) \
410 /* Errr: Expose Infowin */
411 #define Infowin_expose() \
412 (!(Infowin->redraw = 1))
414 /* Errr: Unxpose Infowin */
415 #define Infowin_unexpose() \
416 (Infowin->redraw = 0)
419 * The "default" values
421 static metadpy metadpy_default;
424 * The "current" variables
426 static metadpy *Metadpy = &metadpy_default;
427 static infowin *Infowin = (infowin*)(NULL);
429 static infowin *Focuswin = (infowin*)(NULL);
431 static infoclr *Infoclr = (infoclr*)(NULL);
432 static infofnt *Infofnt = (infofnt*)(NULL);
435 * Init the current metadpy, with various initialization stuff.
438 * dpy: The Display* to use (if NULL, create it)
439 * name: The name of the Display (if NULL, the current)
442 * If 'name' is NULL, but 'dpy' is set, extract name from dpy
443 * If 'dpy' is NULL, then Create the named Display
444 * If 'name' is NULL, and so is 'dpy', use current Display
446 * Return -1 if no Display given, and none can be opened.
448 static errr Metadpy_init_2(Display *dpy, concptr name)
450 metadpy *m = Metadpy;
453 dpy = XOpenDisplay(name);
454 if (!dpy) return (-1);
464 m->screen = DefaultScreenOfDisplay(dpy);
465 m->root = RootWindowOfScreen(m->screen);
466 m->cmap = DefaultColormapOfScreen(m->screen);
467 m->name = DisplayString(dpy);
468 m->fd = ConnectionNumber(Metadpy->dpy);
469 m->width = WidthOfScreen(m->screen);
470 m->height = HeightOfScreen(m->screen);
471 m->depth = DefaultDepthOfScreen(m->screen);
474 Visual *vis = DefaultVisual(dpy, 0);
475 XftColorAllocName(dpy, vis, m->cmap, "black", &m->black);
476 XftColorAllocName(dpy, vis, m->cmap, "white", &m->white);
478 m->black = BlackPixelOfScreen(m->screen);
479 m->white = WhitePixelOfScreen(m->screen);
486 m->zg = (1 << m->depth) - 1;
489 m->color = ((m->depth > 1) ? 1 : 0);
490 m->mono = ((m->color) ? 0 : 1);
495 * General Flush/ Sync/ Discard routine
497 static errr Metadpy_update(int flush, int sync, int discard)
499 if (flush) XFlush(Metadpy->dpy);
500 if (sync) XSync(Metadpy->dpy, discard);
509 static errr Metadpy_do_beep(void)
511 XBell(Metadpy->dpy, 100);
516 * Set the name (in the title bar) of Infowin
518 static errr Infowin_set_name(concptr name)
525 st = XStringListToTextProperty(&bp, 1, &tp);
526 if (st) XSetWMName(Metadpy->dpy, Infowin->win, &tp);
531 * Prepare a new 'infowin'.
533 static errr Infowin_prepare(Window xid)
535 infowin *iwin = Infowin;
537 XWindowAttributes xwa;
539 unsigned int w, h, b, d;
541 XGetGeometry(Metadpy->dpy, xid, &tmp_win, &x, &y, &w, &h, &b, &d);
544 Visual *vis = DefaultVisual(Metadpy->dpy, 0);
545 if (vis->class != TrueColor) {
546 quit_fmt("Display does not support truecolor.\n");
548 iwin->draw = XftDrawCreate(Metadpy->dpy, iwin->win, vis, Metadpy->cmap);
557 XGetWindowAttributes(Metadpy->dpy, xid, &xwa);
558 iwin->mask = xwa.your_event_mask;
559 iwin->mapped = ((xwa.map_state == IsUnmapped) ? 0 : 1);
565 * Init an infowin by giving some data.
568 * dad: The Window that should own this Window (if any)
569 * x,y: The position of this Window
570 * w,h: The size of this Window
571 * b,d: The border width and pixel depth
574 * If 'dad == None' assume 'dad == root'
576 static errr Infowin_init_data(Window dad, int x, int y, int w, int h,
577 int b, Pixell fg, Pixell bg)
580 (void)WIPE(Infowin, infowin);
581 if (dad == None) dad = Metadpy->root;
584 xid = XCreateSimpleWindow(Metadpy->dpy, dad, x, y, w, h, b, fg.pixel, bg.pixel);
586 xid = XCreateSimpleWindow(Metadpy->dpy, dad, x, y, w, h, b, fg, bg);
589 XSelectInput(Metadpy->dpy, xid, 0L);
591 return (Infowin_prepare(xid));
595 * Modify the event mask of an Infowin
597 static errr Infowin_set_mask(long mask)
599 Infowin->mask = mask;
600 XSelectInput(Metadpy->dpy, Infowin->win, Infowin->mask);
605 * Request that Infowin be mapped
607 static errr Infowin_map(void)
609 XMapWindow(Metadpy->dpy, Infowin->win);
614 * Request that Infowin be raised
616 static errr Infowin_raise(void)
618 XRaiseWindow(Metadpy->dpy, Infowin->win);
623 * Request that Infowin be moved to a new location
625 static errr Infowin_impell(int x, int y)
627 XMoveWindow(Metadpy->dpy, Infowin->win, x, y);
634 static errr Infowin_resize(int w, int h)
636 XResizeWindow(Metadpy->dpy, Infowin->win, w, h);
641 * Visually clear Infowin
643 static errr Infowin_wipe(void)
645 XClearWindow(Metadpy->dpy, Infowin->win);
650 * A NULL terminated pair list of legal "operation names"
652 * Pairs of values, first is texttual name, second is the string
653 * holding the decimal value that the operation corresponds to.
655 static concptr opcode_pairs[] =
674 "+copyInverted", "12",
681 * Parse a word into an operation "code"
684 * str: A string, hopefully representing an Operation
687 * 0-15: if 'str' is a valid Operation
688 * -1: if 'str' could not be parsed
690 static int Infoclr_Opcode(concptr str)
693 for (i = 0; opcode_pairs[i*2]; ++i)
695 if (streq(opcode_pairs[i*2], str))
697 return (atoi(opcode_pairs[i*2+1]));
705 * Initialize an infoclr with some data
708 * fg: The Pixell for the requested Foreground (see above)
709 * bg: The Pixell for the requested Background (see above)
710 * op: The Opcode for the requested Operation (see above)
711 * stip: The stipple mode
713 static errr Infoclr_init_data(Pixell fg, Pixell bg, int op, int stip)
715 infoclr *iclr = Infoclr;
720 unsigned long gc_mask;
724 if (bg > Metadpy->zg) return (-1);
725 if (fg > Metadpy->zg) return (-1);
726 if ((op < 0) || (op > 15)) return (-1);
731 if (op == 6) gcv.background = 0;
732 if (op == 6) gcv.foreground = (bg ^ fg);
734 gcv.fill_style = (stip ? FillStippled : FillSolid);
735 gcv.graphics_exposures = False;
736 gc_mask = (GCFunction | GCBackground | GCForeground | GCFillStyle | GCGraphicsExposures);
737 gc = XCreateGC(Metadpy->dpy, Metadpy->root, gc_mask, &gcv);
740 (void)WIPE(iclr, infoclr);
750 iclr->stip = stip ? 1 : 0;
755 * Change the 'fg' for an infoclr
758 * fg: The Pixell for the requested Foreground (see above)
760 static errr Infoclr_change_fg(Pixell fg)
762 infoclr *iclr = Infoclr;
767 if (fg > Metadpy->zg) return (-1);
769 XSetForeground(Metadpy->dpy, iclr->gc, fg);
776 * Prepare a new 'infofnt'
779 static errr Infofnt_prepare(XftFont *info)
781 static errr Infofnt_prepare(XFontSet info)
784 infofnt *ifnt = Infofnt;
788 XFontStruct **fontinfo;
791 int ascent, descent, width;
798 ifnt->asc = info->ascent;
799 ifnt->hgt = info->ascent + info->descent;
800 const char *text = "A";
802 XftTextExtentsUtf8(Metadpy->dpy, info, (FcChar8*)text, strlen(text), &extent);
803 ifnt->wid = extent.xOff;
805 n_fonts = XFontsOfFontSet(info, &fontinfo, &fontname);
807 ascent = descent = width = 0;
808 while(n_fonts-- > 0){
809 cs = &((*fontinfo)->max_bounds);
810 if(ascent < (*fontinfo)->ascent) ascent = (*fontinfo)->ascent;
811 if(descent < (*fontinfo)->descent) descent = (*fontinfo)->descent;
812 if(((*fontinfo)->max_byte1) > 0){
813 /* 多バイト文字の場合は幅半分(端数切り上げ)で評価する */
814 if(width < (cs->width+1)/2) width = (cs->width+1)/2;
816 if(width < cs->width) width = cs->width;
822 ifnt->hgt = ascent + descent;
827 ifnt->twid = 2 * ifnt->wid;
829 ifnt->twid = ifnt->wid;
835 * Init an infofnt by its Name
838 * name: The name of the requested Font
840 static void Infofnt_init_data(concptr name)
852 if (!name || !*name) quit("Missing font!");
855 info = XftFontOpenName(Metadpy->dpy, 0, name);
856 /* TODO: error handling */
858 info = XCreateFontSet(Metadpy->dpy, name, &missing_list, &missing_count, &default_font);
859 if(missing_count > 0){
860 printf("missing font(s): \n");
861 while(missing_count-- > 0){
862 printf("\t%s\n", missing_list[missing_count]);
864 XFreeStringList(missing_list);
868 if (!info) quit_fmt("Failed to find font:\"%s\"", name);
870 (void)WIPE(Infofnt, infofnt);
871 if (Infofnt_prepare(info))
874 XftFontClose(Metadpy->dpy, info);
876 XFreeFontSet(Metadpy->dpy, info);
878 quit_fmt("Failed to prepare font:\"%s\"", name);
881 Infofnt->name = string_make(name);
888 static errr Infofnt_text_std(int x, int y, concptr str, int len)
890 if (!str || !*str) return (-1);
892 if (len < 0) len = strlen(str);
894 y = (y * Infofnt->hgt) + Infofnt->asc + Infowin->oy;
895 x = (x * Infofnt->wid) + Infowin->ox;
900 for (i = 0; i < len; ++i)
902 XDrawImageString(Metadpy->dpy, Infowin->win, Infoclr->gc, x + i * Infofnt->wid + Infofnt->off, y, str + i, 1);
908 iconv_t cd = iconv_open("UTF-8", "EUC-JP");
910 size_t outlen = len * 2;
911 char *kanji = malloc(outlen);
912 char *sp; char *kp = kanji;
914 angband_strcpy(sbuf, str, sizeof(sbuf));
916 iconv(cd, &sp, &inlen, &kp, &outlen);
920 XftDraw* draw = Infowin->draw;
925 r.width = Infofnt->wid*len;
926 r.height = Infofnt->hgt;
927 XftDrawSetClipRectangles(draw, x, y-Infofnt->asc, &r, 1);
928 XftDrawRect(draw, &Infoclr->bg, x, y-Infofnt->asc, Infofnt->wid*len, Infofnt->hgt);
929 XftDrawStringUtf8(draw, &Infoclr->fg, Infofnt->info, x, y,
930 (FcChar8*)kanji, kp - kanji);
931 XftDrawSetClip(draw, 0);
933 XmbDrawImageString(Metadpy->dpy, Infowin->win, Infofnt->info,
934 Infoclr->gc, x, y, kanji, kp-kanji);
943 * Painting where text would be
945 static errr Infofnt_text_non(int x, int y, concptr str, int len)
948 if (len < 0) len = strlen(str);
950 w = len * Infofnt->wid;
951 x = x * Infofnt->wid + Infowin->ox;
953 y = y * h + Infowin->oy;
956 XftDrawRect(Infowin->draw, &Infoclr->fg, x, y, w, h);
958 XFillRectangle(Metadpy->dpy, Infowin->win, Infoclr->gc, x, y, w, h);
965 * Angband specific code follows... (ANGBAND)
969 * Hack -- cursor color
976 static infoclr *clr[256];
979 * Color info (unused, red, green, blue).
981 static byte color_table[256][4];
986 typedef struct term_data term_data;
989 * A structure for each "term"
1003 * The number of term data structures
1005 #define MAX_TERM_DATA 8
1008 * The array of term data structures
1010 static term_data data[MAX_TERM_DATA];
1013 /* Use short names for the most commonly used elements of various structures. */
1014 #define DPY (Metadpy->dpy)
1015 #define WIN (Infowin->win)
1017 /* Describe a set of co-ordinates. */
1018 typedef struct co_ord co_ord;
1026 * A special structure to store information about the text currently
1029 typedef struct x11_selection_type x11_selection_type;
1030 struct x11_selection_type
1032 bool select; /* The selection is currently in use. */
1033 bool drawn; /* The selection is currently displayed. */
1034 term_type *t; /* The window where the selection is found. */
1035 co_ord init; /* The starting co-ordinates. */
1036 co_ord cur; /* The end co-ordinates (the current ones if still copying). */
1037 co_ord old; /* The previous end co-ordinates. */
1038 Time time; /* The time at which the selection was finalised. */
1041 static x11_selection_type s_ptr[1];
1047 static void convert_to_euc(char *buf)
1049 size_t inlen = strlen(buf);
1050 size_t outlen = inlen + 1;
1053 iconv_t iconvd = iconv_open("EUC-JP", "UTF-8");
1056 iconv(iconvd, &inbuf, &inlen, &outbuf, &outlen);
1057 iconv_close(iconvd);
1059 int i, l = strlen(tmp);
1060 for (i = 0; i < l; i++)
1067 * Push multiple keys reversal
1069 static void term_string_push(char *buf)
1071 int i, l = strlen(buf);
1072 for (i = l; i >= 0; i--)
1073 term_key_push(buf[i]);
1077 * Process a keypress event
1079 * Also appears in "main-xaw.c".
1081 static void react_keypress(XKeyEvent *xev)
1083 int n, mc, ms, mo, mx;
1085 XKeyEvent *ev = (XKeyEvent*)(xev);
1091 int valid_keysym = TRUE;
1095 if(Focuswin && Focuswin->xic){
1097 n = XmbLookupString(Focuswin->xic, ev, buf, 125, &ks, &status);
1098 if(status == XBufferOverflow){
1099 printf("Input is too long, and dropped\n");
1102 if(status != XLookupKeySym && status != XLookupBoth){
1103 valid_keysym = FALSE;
1106 n = XLookupString(ev, buf, 125, &ks, NULL);
1109 n = XLookupString(ev, buf, 125, &ks, NULL);
1115 if(!valid_keysym) { /* XIMからの入力時のみ FALSE になる */
1116 convert_to_euc(buf);
1117 term_string_push(buf);
1122 if (IsModifierKey(ks)) return;
1125 mc = (ev->state & ControlMask) ? TRUE : FALSE;
1126 ms = (ev->state & ShiftMask) ? TRUE : FALSE;
1127 mo = (ev->state & Mod1Mask) ? TRUE : FALSE;
1128 mx = (ev->state & Mod2Mask) ? TRUE : FALSE;
1129 if (n && !mo && !mx && !IsSpecialKey(ks))
1131 term_string_push(buf);
1139 term_key_push(ESCAPE);
1145 term_key_push('\r');
1151 term_key_push('\t');
1157 term_key_push(0x7f);
1162 term_key_push('\010');
1169 sprintf(msg, "%c%s%s%s%s_%lX%c", 31,
1170 mc ? "N" : "", ms ? "S" : "",
1171 mo ? "O" : "", mx ? "M" : "",
1172 (unsigned long)(ks), 13);
1176 sprintf(msg, "%c%s%s%s%sK_%X%c", 31,
1177 mc ? "N" : "", ms ? "S" : "",
1178 mo ? "O" : "", mx ? "M" : "",
1182 term_string_push(msg);
1184 if (n && (macro_find_exact(msg) < 0))
1186 macro_add(msg, buf);
1191 * Find the square a particular pixel is part of.
1193 static void pixel_to_square(int * const x, int * const y,
1194 const int ox, const int oy)
1196 (*x) = (ox - Infowin->ox) / Infofnt->wid;
1197 (*y) = (oy - Infowin->oy) / Infofnt->hgt;
1201 * Find the pixel at the top-left corner of a square.
1203 static void square_to_pixel(int * const x, int * const y,
1204 const int ox, const int oy)
1206 (*x) = ox * Infofnt->wid + Infowin->ox;
1207 (*y) = oy * Infofnt->hgt + Infowin->oy;
1211 * Convert co-ordinates from starting corner/opposite corner to minimum/maximum.
1213 static void sort_co_ord(co_ord *min, co_ord *max,
1214 const co_ord *b, const co_ord *a)
1216 min->x = MIN(a->x, b->x);
1217 min->y = MIN(a->y, b->y);
1218 max->x = MAX(a->x, b->x);
1219 max->y = MAX(a->y, b->y);
1223 * Remove the selection by redrawing it.
1225 static void mark_selection_clear(int x1, int y1, int x2, int y2)
1227 term_redraw_section(x1,y1,x2,y2);
1231 * Select an area by drawing a grey box around it.
1232 * NB. These two functions can cause flicker as the selection is modified,
1233 * as the game redraws the entire marked section.
1235 static void mark_selection_mark(int x1, int y1, int x2, int y2)
1237 square_to_pixel(&x1, &y1, x1, y1);
1238 square_to_pixel(&x2, &y2, x2, y2);
1240 XftDrawRect(Infowin->draw, &clr[2]->fg, x1, y1, x2-x1+Infofnt->wid - 1, y2-y1+Infofnt->hgt - 1);
1242 XDrawRectangle(Metadpy->dpy, Infowin->win, clr[2]->gc, x1, y1,
1243 x2-x1+Infofnt->wid - 1, y2-y1+Infofnt->hgt - 1);
1248 * Mark a selection by drawing boxes around it (for now).
1250 static void mark_selection(void)
1253 term_type *old = Term;
1254 bool draw = s_ptr->select;
1255 bool clear = s_ptr->drawn;
1256 if (s_ptr->t != old) term_activate(s_ptr->t);
1260 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->old);
1261 mark_selection_clear(min.x, min.y, max.x, max.y);
1265 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->cur);
1266 mark_selection_mark(min.x, min.y, max.x, max.y);
1269 if (s_ptr->t != old) term_activate(old);
1271 s_ptr->old.x = s_ptr->cur.x;
1272 s_ptr->old.y = s_ptr->cur.y;
1273 s_ptr->drawn = s_ptr->select;
1277 * Forget a selection for one reason or another.
1279 static void copy_x11_release(void)
1281 s_ptr->select = FALSE;
1286 * Start to select some text on the screen.
1288 static void copy_x11_start(int x, int y)
1290 if (s_ptr->select) copy_x11_release();
1293 s_ptr->init.x = s_ptr->cur.x = s_ptr->old.x = x;
1294 s_ptr->init.y = s_ptr->cur.y = s_ptr->old.y = y;
1298 * Respond to movement of the mouse when selecting text.
1300 static void copy_x11_cont(int x, int y, unsigned int buttons)
1302 x = MIN(MAX(x, 0), Term->wid-1);
1303 y = MIN(MAX(y, 0), Term->hgt-1);
1304 if (~buttons & Button1Mask) return;
1305 if (s_ptr->t != Term) return;
1306 if (x == s_ptr->old.x && y == s_ptr->old.y && s_ptr->select) return;
1308 s_ptr->select = TRUE;
1315 * Respond to release of the left mouse button by putting the selected text in
1316 * the primary buffer.
1318 static void copy_x11_end(const Time time)
1320 if (!s_ptr->select) return;
1321 if (s_ptr->t != Term) return;
1324 XSetSelectionOwner(Metadpy->dpy, XA_PRIMARY, Infowin->win, time);
1325 if (XGetSelectionOwner(Metadpy->dpy, XA_PRIMARY) != Infowin->win)
1327 s_ptr->select = FALSE;
1332 static Atom xa_targets, xa_timestamp, xa_text, xa_compound_text;
1335 * Set the required variable atoms at start-up to avoid errors later.
1337 static void set_atoms(void)
1339 xa_targets = XInternAtom(DPY, "TARGETS", False);
1340 xa_timestamp = XInternAtom(DPY, "TIMESTAMP", False);
1341 xa_text = XInternAtom(DPY, "TEXT", False);
1342 xa_compound_text = XInternAtom(DPY, "COMPOUND_TEXT", False);
1345 static Atom request_target = 0;
1348 * Send a message to request that the PRIMARY buffer be sent here.
1350 static void paste_x11_request(Atom target, const Time time)
1352 Atom property = XInternAtom(DPY, "__COPY_TEXT", False);
1353 if (XGetSelectionOwner(DPY, XA_PRIMARY) == None)
1356 /* bell("No selection found."); */
1360 request_target = target;
1361 XConvertSelection(DPY, XA_PRIMARY, target, property, WIN, time);
1365 * Add the contents of the PRIMARY buffer to the input queue.
1367 * Hack - This doesn't use the "time" of the event, and so accepts anything a
1368 * client tries to send it.
1370 static void paste_x11_accept(const XSelectionEvent *ptr)
1373 const long offset = 0;
1374 const long length = 32000;
1375 XTextProperty xtextproperty;
1377 Atom property = XInternAtom(DPY, "__COPY_TEXT", False);
1378 if (ptr->property == None)
1380 if (request_target == xa_compound_text)
1382 paste_x11_request(XA_STRING, ptr->time);
1387 plog("Paste failure (remote client could not send).");
1393 if (ptr->selection != XA_PRIMARY)
1395 plog("Paste failure (remote client did not send primary selection).");
1399 if (ptr->target != request_target)
1401 plog("Paste failure (selection in unknown format).");
1405 if (XGetWindowProperty(Metadpy->dpy, Infowin->win, property, offset,
1406 length, TRUE, request_target,
1407 &xtextproperty.encoding,
1408 &xtextproperty.format,
1409 &xtextproperty.nitems,
1411 &xtextproperty.value)
1417 if (request_target == xa_compound_text)
1422 XmbTextPropertyToTextList(DPY, &xtextproperty, &list, &count);
1428 for (i = 0; i < count; i++)
1430 err = type_string(list[i], 0);
1434 XFreeStringList(list);
1439 err = type_string((char *)xtextproperty.value, xtextproperty.nitems);
1442 XFree(xtextproperty.value);
1445 plog("Paste failure (too much text selected).");
1450 * Add a character to a string in preparation for sending it to another
1451 * client as a STRING.
1452 * This doesn't change anything, as clients tend not to have difficulty in
1453 * receiving this format (although the standard specifies a restricted set).
1454 * Strings do not have a colour.
1456 static bool paste_x11_send_text(XSelectionRequestEvent *rq)
1466 if (rq->time < s_ptr->time) return FALSE;
1468 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->cur);
1469 if (XGetSelectionOwner(DPY, XA_PRIMARY) != WIN)
1474 XDeleteProperty(DPY, rq->requestor, rq->property);
1476 for (n = 0, y = 0; y < Term->hgt; y++)
1481 if (y < min.y) continue;
1482 if (y > max.y) break;
1484 for (l = 0, x = 0; x < Term->wid; x++)
1487 if (x > max.x) break;
1489 term_what(x, y, &a, &c);
1490 if (1 == kanji) kanji = 2;
1491 else if (iskanji(c)) kanji = 1;
1494 if (x < min.x) continue;
1497 * A single kanji character was divided in two...
1498 * Delete the garbage.
1500 if ((2 == kanji && x == min.x) ||
1501 (1 == kanji && x == max.x))
1504 if (x > max.x) break;
1505 if (x < min.x) continue;
1507 term_what(x, y, &a, &c);
1514 while (buf[l-1] == ' ') l--;
1523 list[n++] = (char *)string_make(buf);
1527 if (rq->target == XA_STRING)
1529 for (n = 0; list[n]; n++)
1531 XChangeProperty(DPY, rq->requestor, rq->property, rq->target, 8,
1532 PropModeAppend, (unsigned char *)list[n], strlen(list[n]));
1535 else if (rq->target == xa_text ||
1536 rq->target == xa_compound_text)
1538 XTextProperty text_prop;
1539 XICCEncodingStyle style;
1541 if (rq->target == xa_text)
1542 style = XStdICCTextStyle;
1544 style = XCompoundTextStyle;
1547 XmbTextListToTextProperty(DPY, list, n, style, &text_prop))
1549 XChangeProperty(DPY,
1557 XFree(text_prop.value);
1561 for (n = 0; list[n]; n++)
1563 string_free(list[n]);
1570 * Send some text requested by another X client.
1572 static void paste_x11_send(XSelectionRequestEvent *rq)
1575 XSelectionEvent *ptr = &(event.xselection);
1577 ptr->type = SelectionNotify;
1578 ptr->property = rq->property;
1579 ptr->display = rq->display;
1580 ptr->requestor = rq->requestor;
1581 ptr->selection = rq->selection;
1582 ptr->target = rq->target;
1583 ptr->time = rq->time;
1586 * Paste the appropriate information for each target type.
1587 * Note that this currently rejects MULTIPLE targets.
1590 if (rq->target == XA_STRING ||
1591 rq->target == xa_text ||
1592 rq->target == xa_compound_text)
1594 if (!paste_x11_send_text(rq))
1595 ptr->property = None;
1597 else if (rq->target == xa_targets)
1599 Atom target_list[4];
1600 target_list[0] = XA_STRING;
1601 target_list[1] = xa_text;
1602 target_list[2] = xa_compound_text;
1603 target_list[3] = xa_targets;
1604 XChangeProperty(DPY, rq->requestor, rq->property, rq->target,
1605 (8 * sizeof(target_list[0])), PropModeReplace,
1606 (unsigned char *)target_list,
1607 (sizeof(target_list) / sizeof(target_list[0])));
1609 else if (rq->target == xa_timestamp)
1611 XChangeProperty(DPY, rq->requestor, rq->property, rq->target,
1612 (8 * sizeof(Time)), PropModeReplace,
1613 (unsigned char *)s_ptr->time, 1);
1617 ptr->property = None;
1620 XSendEvent(DPY, rq->requestor, FALSE, NoEventMask, &event);
1624 * Handle various events conditional on presses of a mouse button.
1626 static void handle_button(Time time, int x, int y, int button, bool press)
1628 pixel_to_square(&x, &y, x, y);
1630 if (press && button == 1) copy_x11_start(x, y);
1631 if (!press && button == 1) copy_x11_end(time);
1632 if (!press && button == 2) paste_x11_request(xa_compound_text, time);
1638 static errr CheckEvent(bool wait)
1640 term_data *old_td = (term_data*)(Term->data);
1642 XEvent xev_body, *xev = &xev_body;
1644 term_data *td = NULL;
1645 infowin *iwin = NULL;
1654 if (!wait && !XPending(Metadpy->dpy)) return (1);
1656 if (s_ptr->select && !s_ptr->drawn) mark_selection();
1658 XNextEvent(Metadpy->dpy, xev);
1661 } while (XFilterEvent(xev, xev->xany.window));
1664 if (xev->type == MappingNotify)
1666 XRefreshKeyboardMapping(&xev->xmapping);
1670 for (i = 0; i < MAX_TERM_DATA; i++)
1672 if (!data[i].win) continue;
1673 if (xev->xany.window == data[i].win->win)
1681 if (!td || !iwin) return (0);
1683 term_activate(&td->t);
1690 bool press = (xev->type == ButtonPress);
1691 int x = xev->xbutton.x;
1692 int y = xev->xbutton.y;
1694 if (xev->xbutton.button == Button1) z = 1;
1695 else if (xev->xbutton.button == Button2) z = 2;
1696 else if (xev->xbutton.button == Button3) z = 3;
1697 else if (xev->xbutton.button == Button4) z = 4;
1698 else if (xev->xbutton.button == Button5) z = 5;
1701 handle_button(xev->xbutton.time, x, y, z, press);
1711 int x = xev->xmotion.x;
1712 int y = xev->xmotion.y;
1713 unsigned int z = xev->xmotion.state;
1714 pixel_to_square(&x, &y, x, y);
1715 copy_x11_cont(x, y, z);
1718 case SelectionNotify:
1720 paste_x11_accept(&(xev->xselection));
1723 case SelectionRequest:
1725 paste_x11_send(&(xev->xselectionrequest));
1728 case SelectionClear:
1730 s_ptr->select = FALSE;
1740 term_activate(&old_td->t);
1741 react_keypress(&(xev->xkey));
1747 x1 = (xev->xexpose.x - Infowin->ox)/Infofnt->wid;
1748 x2 = (xev->xexpose.x + xev->xexpose.width -
1749 Infowin->ox)/Infofnt->wid;
1751 y1 = (xev->xexpose.y - Infowin->oy)/Infofnt->hgt;
1752 y2 = (xev->xexpose.y + xev->xexpose.height -
1753 Infowin->oy)/Infofnt->hgt;
1755 term_redraw_section(x1, y1, x2, y2);
1760 Infowin->mapped = 1;
1761 Term->mapped_flag = TRUE;
1766 Infowin->mapped = 0;
1767 Term->mapped_flag = FALSE;
1770 case ConfigureNotify:
1772 int cols, rows, wid, hgt;
1773 int ox = Infowin->ox;
1774 int oy = Infowin->oy;
1775 Infowin->x = xev->xconfigure.x;
1776 Infowin->y = xev->xconfigure.y;
1777 Infowin->w = xev->xconfigure.width;
1778 Infowin->h = xev->xconfigure.height;
1779 cols = ((Infowin->w - (ox + ox)) / td->fnt->wid);
1780 rows = ((Infowin->h - (oy + oy)) / td->fnt->hgt);
1781 if (cols < 1) cols = 1;
1782 if (rows < 1) rows = 1;
1786 if (cols < 80) cols = 80;
1787 if (rows < 24) rows = 24;
1790 wid = cols * td->fnt->wid + (ox + ox);
1791 hgt = rows * td->fnt->hgt + (oy + oy);
1792 term_resize(cols, rows);
1793 if ((Infowin->w != wid) || (Infowin->h != hgt))
1795 Infowin_set(td->win);
1796 Infowin_resize(wid, hgt);
1805 XSetICFocus(iwin->xic);
1814 XUnsetICFocus(iwin->xic);
1822 term_activate(&old_td->t);
1823 Infowin_set(old_td->win);
1828 * An array of sound file names
1830 static concptr sound_file[SOUND_MAX];
1833 * Check for existance of a file
1835 static bool check_file(concptr s)
1839 fff = fopen(s, "r");
1840 if (!fff) return (FALSE);
1849 static void init_sound(void)
1854 char dir_xtra_sound[1024];
1855 path_build(dir_xtra_sound, sizeof(dir_xtra_sound), ANGBAND_DIR_XTRA, "sound");
1856 for (i = 1; i < SOUND_MAX; i++)
1858 sprintf(wav, "%s.wav", angband_sound_name[i]);
1859 path_build(buf, sizeof(buf), dir_xtra_sound, wav);
1860 if (check_file(buf)) sound_file[i] = string_make(buf);
1868 * Hack -- make a sound
1870 static errr Term_xtra_x11_sound(int v)
1873 if (!use_sound) return (1);
1874 if ((v < 0) || (v >= SOUND_MAX)) return (1);
1875 if (!sound_file[v]) return (1);
1877 sprintf(buf,"./playwave.sh %s\n", sound_file[v]);
1878 return (system(buf) < 0);
1883 * Handle "activation" of a term
1885 static errr Term_xtra_x11_level(int v)
1887 term_data *td = (term_data*)(Term->data);
1890 Infowin_set(td->win);
1891 Infofnt_set(td->fnt);
1900 static errr Term_xtra_x11_react(void)
1906 for (i = 0; i < 256; i++)
1908 if ((color_table[i][0] != angband_color_table[i][0]) ||
1909 (color_table[i][1] != angband_color_table[i][1]) ||
1910 (color_table[i][2] != angband_color_table[i][2]) ||
1911 (color_table[i][3] != angband_color_table[i][3]))
1914 color_table[i][0] = angband_color_table[i][0];
1915 color_table[i][1] = angband_color_table[i][1];
1916 color_table[i][2] = angband_color_table[i][2];
1917 color_table[i][3] = angband_color_table[i][3];
1918 pixel = create_pixel(Metadpy->dpy,
1922 Infoclr_set(clr[i]);
1923 Infoclr_change_fg(pixel);
1932 * Handle a "special request"
1934 static errr Term_xtra_x11(int n, int v)
1938 case TERM_XTRA_NOISE: Metadpy_do_beep(); return (0);
1939 case TERM_XTRA_SOUND: return (Term_xtra_x11_sound(v));
1941 case TERM_XTRA_FRESH: Metadpy_update(1, 1, 0); return (0);
1943 case TERM_XTRA_FRESH: Metadpy_update(1, 0, 0); return (0);
1945 case TERM_XTRA_BORED: return (CheckEvent(0));
1946 case TERM_XTRA_EVENT: return (CheckEvent(v));
1947 case TERM_XTRA_FLUSH: while (!CheckEvent(FALSE)); return (0);
1948 case TERM_XTRA_LEVEL: return (Term_xtra_x11_level(v));
1949 case TERM_XTRA_CLEAR: Infowin_wipe(); s_ptr->drawn = FALSE; return (0);
1950 case TERM_XTRA_DELAY: usleep(1000 * v); return (0);
1951 case TERM_XTRA_REACT: return (Term_xtra_x11_react());
1958 * Draw the cursor as an inverted rectangle.
1960 * Consider a rectangular outline like "main-mac.c". XXX XXX
1962 static errr Term_curs_x11(int x, int y)
1967 XftDrawRect(Infowin->draw, &xor->fg,
1968 x * Infofnt->wid + Infowin->ox,
1969 y * Infofnt->hgt + Infowin->oy,
1970 Infofnt->wid - 1, Infofnt->hgt - 1);
1971 XftDrawRect(Infowin->draw, &xor->fg,
1972 x * Infofnt->wid + Infowin->ox + 1,
1973 y * Infofnt->hgt + Infowin->oy + 1,
1974 Infofnt->wid - 3, Infofnt->hgt - 3);
1976 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
1977 x * Infofnt->wid + Infowin->ox,
1978 y * Infofnt->hgt + Infowin->oy,
1979 Infofnt->wid - 1, Infofnt->hgt - 1);
1980 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
1981 x * Infofnt->wid + Infowin->ox + 1,
1982 y * Infofnt->hgt + Infowin->oy + 1,
1983 Infofnt->wid - 3, Infofnt->hgt - 3);
1989 Infofnt_text_non(x, y, " ", 1);
1997 * Draw the double width cursor
1999 static errr Term_bigcurs_x11(int x, int y)
2004 XftDrawRect(Infowin->draw, &xor->fg,
2005 x * Infofnt->wid + Infowin->ox,
2006 y * Infofnt->hgt + Infowin->oy,
2007 Infofnt->twid - 1, Infofnt->hgt - 1);
2008 XftDrawRect(Infowin->draw, &xor->fg,
2009 x * Infofnt->wid + Infowin->ox + 1,
2010 y * Infofnt->hgt + Infowin->oy + 1,
2011 Infofnt->twid - 3, Infofnt->hgt - 3);
2013 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
2014 x * Infofnt->wid + Infowin->ox,
2015 y * Infofnt->hgt + Infowin->oy,
2016 Infofnt->twid - 1, Infofnt->hgt - 1);
2017 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
2018 x * Infofnt->wid + Infowin->ox + 1,
2019 y * Infofnt->hgt + Infowin->oy + 1,
2020 Infofnt->twid - 3, Infofnt->hgt - 3);
2026 Infofnt_text_non(x, y, " ", 2);
2033 * Erase some characters.
2035 static errr Term_wipe_x11(int x, int y, int n)
2037 Infoclr_set(clr[TERM_DARK]);
2038 Infofnt_text_non(x, y, "", n);
2039 s_ptr->drawn = FALSE;
2044 * Draw some textual characters.
2046 static errr Term_text_x11(TERM_LEN x, TERM_LEN y, int n, TERM_COLOR a, concptr s)
2048 Infoclr_set(clr[a]);
2049 Infofnt_text_std(x, y, s, n);
2050 s_ptr->drawn = FALSE;
2056 * Draw some graphical characters.
2058 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)
2071 unsigned long pixel, blank;
2073 term_data *td = (term_data*)(Term->data);
2080 for (i = 0; i < n; ++i, x += td->fnt->wid)
2084 x1 = (c&0x7F) * td->fnt->twid;
2085 y1 = (a&0x7F) * td->fnt->hgt;
2086 if (td->tiles->width < x1 + td->fnt->wid ||
2087 td->tiles->height < y1 + td->fnt->hgt)
2089 XFillRectangle(Metadpy->dpy, td->win->win, clr[0]->gc, x, y, td->fnt->twid, td->fnt->hgt);
2096 x2 = (tc&0x7F) * td->fnt->twid;
2097 y2 = (ta&0x7F) * td->fnt->hgt;
2099 if (((x1 == x2) && (y1 == y2)) ||
2100 !(((byte)ta & 0x80) && ((byte)tc & 0x80)) ||
2101 td->tiles->width < x2 + td->fnt->wid ||
2102 td->tiles->height < y2 + td->fnt->hgt)
2104 XPutImage(Metadpy->dpy, td->win->win,
2109 td->fnt->twid, td->fnt->hgt);
2113 blank = XGetPixel(td->tiles, 0, td->fnt->hgt * 6);
2114 for (k = 0; k < td->fnt->twid; k++)
2116 for (l = 0; l < td->fnt->hgt; l++)
2118 if ((pixel = XGetPixel(td->tiles, x1 + k, y1 + l)) == blank)
2120 pixel = XGetPixel(td->tiles, x2 + k, y2 + l);
2123 XPutPixel(td->TmpImage, k, l, pixel);
2127 XPutImage(Metadpy->dpy, td->win->win,
2131 td->fnt->twid, td->fnt->hgt);
2135 s_ptr->drawn = FALSE;
2141 static void IMDestroyCallback(XIM, XPointer, XPointer);
2144 IMInstantiateCallback(Display *display, XPointer unused1, XPointer unused2)
2147 XIMCallback ximcallback;
2148 XIMStyles *xim_styles = NULL;
2154 xim = XOpenIM(display, NULL, NULL, NULL);
2156 printf("can't open IM\n");
2160 ximcallback.callback = IMDestroyCallback;
2161 ximcallback.client_data = NULL;
2162 XSetIMValues(xim, XNDestroyCallback, &ximcallback, NULL);
2163 XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL);
2164 for (i = 0; i < xim_styles->count_styles; i++){
2165 if(xim_styles->supported_styles[i] == (XIMPreeditNothing | XIMStatusNothing)) break;
2167 if(i >= xim_styles->count_styles){
2168 printf("Sorry, your IM does not support 'Root' preedit style...\n");
2176 for (i = 0; i < MAX_TERM_DATA; i++)
2178 infowin *iwin = data[i].win;
2179 if (!iwin) continue;
2180 iwin->xic = XCreateIC(xim, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing), XNClientWindow, iwin->win, XNFocusWindow, iwin->win, NULL);
2183 printf("Can't create input context for Term%d\n", i);
2187 if(XGetICValues(iwin->xic, XNFilterEvents, &iwin->xic_mask, NULL) != NULL)
2189 iwin->xic_mask = 0L;
2192 XSelectInput(Metadpy->dpy, iwin->win, iwin->mask | iwin->xic_mask);
2198 static void IMDestroyCallback(XIM xim, XPointer client_data, XPointer call_data)
2204 if (call_data == NULL){
2205 XRegisterIMInstantiateCallback(Metadpy->dpy, NULL, NULL, NULL, IMInstantiateCallback, NULL);
2208 for(i = 0; i < MAX_TERM_DATA; i++)
2210 infowin *iwin = data[i].win;
2213 XSelectInput(Metadpy->dpy, iwin->win, iwin->mask);
2214 iwin->xic_mask = 0L;
2219 Metadpy->xim = NULL;
2223 static char force_lower(char a)
2225 return ((isupper((a))) ? tolower((a)) : (a));
2229 * Initialize a term_data
2231 static errr term_data_init(term_data *td, int i)
2233 term_type *t = &td->t;
2235 concptr name = angband_term_name[i];
2265 sprintf(buf, "ANGBAND_X11_FONT_%d", i);
2267 if (!font) font = getenv("ANGBAND_X11_FONT");
2275 font = DEFAULT_X11_FONT_0;
2280 font = DEFAULT_X11_FONT_1;
2285 font = DEFAULT_X11_FONT_2;
2290 font = DEFAULT_X11_FONT_3;
2295 font = DEFAULT_X11_FONT_4;
2300 font = DEFAULT_X11_FONT_5;
2305 font = DEFAULT_X11_FONT_6;
2310 font = DEFAULT_X11_FONT_7;
2315 font = DEFAULT_X11_FONT;
2320 sprintf(buf, "ANGBAND_X11_AT_X_%d", i);
2322 x = (str != NULL) ? atoi(str) : -1;
2324 sprintf(buf, "ANGBAND_X11_AT_Y_%d", i);
2326 y = (str != NULL) ? atoi(str) : -1;
2328 sprintf(buf, "ANGBAND_X11_COLS_%d", i);
2330 val = (str != NULL) ? atoi(str) : -1;
2331 if (val > 0) cols = val;
2333 sprintf(buf, "ANGBAND_X11_ROWS_%d", i);
2335 val = (str != NULL) ? atoi(str) : -1;
2336 if (val > 0) rows = val;
2340 if (cols < 80) cols = 80;
2341 if (rows < 24) rows = 24;
2344 sprintf(buf, "ANGBAND_X11_IBOX_%d", i);
2346 val = (str != NULL) ? atoi(str) : -1;
2347 if (val > 0) ox = val;
2349 sprintf(buf, "ANGBAND_X11_IBOY_%d", i);
2351 val = (str != NULL) ? atoi(str) : -1;
2352 if (val > 0) oy = val;
2354 MAKE(td->fnt, infofnt);
2355 Infofnt_set(td->fnt);
2356 Infofnt_init_data(font);
2358 num = ((i == 0) ? 1024 : 16);
2359 wid = cols * td->fnt->wid + (ox + ox);
2360 hgt = rows * td->fnt->hgt + (oy + oy);
2361 MAKE(td->win, infowin);
2362 Infowin_set(td->win);
2363 Infowin_init_top(x, y, wid, hgt, 0,
2364 Metadpy->fg, Metadpy->bg);
2366 #if defined(USE_XIM)
2367 Infowin_set_mask(ExposureMask | StructureNotifyMask | KeyPressMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask);
2369 Infowin_set_mask(ExposureMask | StructureNotifyMask | KeyPressMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask);
2372 Infowin_set_name(name);
2375 ch = XAllocClassHint();
2377 if (ch == NULL) quit("XAllocClassHint failed");
2379 strcpy(res_name, name);
2380 res_name[0] = force_lower(res_name[0]);
2381 ch->res_name = res_name;
2383 strcpy(res_class, "Angband");
2384 ch->res_class = res_class;
2386 XSetClassHint(Metadpy->dpy, Infowin->win, ch);
2387 sh = XAllocSizeHints();
2388 if (sh == NULL) quit("XAllocSizeHints failed");
2392 sh->flags = PMinSize | PMaxSize;
2393 sh->min_width = 80 * td->fnt->wid + (ox + ox);
2394 sh->min_height = 24 * td->fnt->hgt + (oy + oy);
2395 sh->max_width = 255 * td->fnt->wid + (ox + ox);
2396 sh->max_height = 255 * td->fnt->hgt + (oy + oy);
2400 sh->flags = PMinSize | PMaxSize;
2401 sh->min_width = td->fnt->wid + (ox + ox);
2402 sh->min_height = td->fnt->hgt + (oy + oy);
2403 sh->max_width = 256 * td->fnt->wid + (ox + ox);
2404 sh->max_height = 256 * td->fnt->hgt + (oy + oy);
2407 sh->flags |= PResizeInc;
2408 sh->width_inc = td->fnt->wid;
2409 sh->height_inc = td->fnt->hgt;
2410 sh->flags |= PBaseSize;
2411 sh->base_width = (ox + ox);
2412 sh->base_height = (oy + oy);
2413 XSetWMNormalHints(Metadpy->dpy, Infowin->win, sh);
2417 wh = XAllocWMHints();
2418 if(wh == NULL) quit("XAllocWMHints failed");
2419 wh->flags = InputHint;
2421 XSetWMHints(Metadpy->dpy, Infowin->win, wh);
2424 if ((x >= 0) && (y >= 0)) Infowin_impell(x, y);
2426 term_init(t, cols, rows, num);
2427 t->soft_cursor = TRUE;
2428 t->attr_blank = TERM_WHITE;
2429 t->char_blank = ' ';
2430 t->xtra_hook = Term_xtra_x11;
2431 t->curs_hook = Term_curs_x11;
2432 t->bigcurs_hook = Term_bigcurs_x11;
2433 t->wipe_hook = Term_wipe_x11;
2434 t->text_hook = Term_text_x11;
2441 * Initialization function for an "X11" module to Angband
2443 errr init_x11(int argc, char *argv[])
2446 concptr dpy_name = "";
2450 char filename[1024];
2458 for (i = 1; i < argc; i++)
2460 if (prefix(argv[i], "-d"))
2462 dpy_name = &argv[i][2];
2467 if (prefix(argv[i], "-s"))
2469 smoothRescaling = FALSE;
2473 if (prefix(argv[i], "-a"))
2475 arg_graphics = GRAPHICS_ADAM_BOLT;
2479 if (prefix(argv[i], "-o"))
2481 arg_graphics = GRAPHICS_ORIGINAL;
2486 if (prefix(argv[i], "-b"))
2488 arg_bigtile = use_bigtile = TRUE;
2492 if (prefix(argv[i], "-n"))
2494 num_term = atoi(&argv[i][2]);
2495 if (num_term > MAX_TERM_DATA) num_term = MAX_TERM_DATA;
2496 else if (num_term < 1) num_term = 1;
2500 if (prefix(argv[i], "--"))
2505 plog_fmt("Ignoring option: %s", argv[i]);
2511 setlocale(LC_ALL, "");
2513 #ifdef DEFAULT_LOCALE
2514 if(!strcmp(setlocale(LC_ALL, NULL), "C")){
2515 printf("try default locale \"%s\"\n", DEFAULT_LOCALE);
2516 setlocale(LC_ALL, DEFAULT_LOCALE);
2520 if(!strcmp(setlocale(LC_ALL, NULL), "C"))
2522 printf("WARNING: Locale is not supported. Non-english font may be displayed incorrectly.\n");
2525 if(!XSupportsLocale()){
2526 printf("can't support locale in X\n");
2527 setlocale(LC_ALL, "C");
2530 setlocale(LC_ALL, "C");
2533 #endif /* USE_LOCALE */
2535 if (Metadpy_init_name(dpy_name)) return (-1);
2539 Infoclr_init_ppn(Metadpy->fg, Metadpy->bg, "xor", 0);
2540 for (i = 0; i < 256; ++i)
2543 MAKE(clr[i], infoclr);
2544 Infoclr_set(clr[i]);
2545 color_table[i][0] = angband_color_table[i][0];
2546 color_table[i][1] = angband_color_table[i][1];
2547 color_table[i][2] = angband_color_table[i][2];
2548 color_table[i][3] = angband_color_table[i][3];
2549 pixel = ((i == 0) ? Metadpy->bg : Metadpy->fg);
2552 pixel = create_pixel(Metadpy->dpy,
2558 Infoclr_init_ppn(pixel, Metadpy->bg, "cpy", 0);
2562 for (i = 0; i < num_term; i++)
2564 term_data *td = &data[i];
2565 term_data_init(td, i);
2566 angband_term[i] = Term;
2569 Infowin_set(data[0].win);
2571 term_activate(&data[0].t);
2576 p = XSetLocaleModifiers("");
2578 p = XSetLocaleModifiers("@im=");
2581 XRegisterIMInstantiateCallback(Metadpy->dpy, NULL, NULL, NULL, IMInstantiateCallback, NULL);
2584 if (arg_sound) init_sound();
2587 switch (arg_graphics)
2589 case GRAPHICS_ORIGINAL:
2590 path_build(filename, sizeof(filename), ANGBAND_DIR_XTRA, "graf/8x8.bmp");
2591 if (0 == fd_close(fd_open(filename, O_RDONLY)))
2593 use_graphics = TRUE;
2594 pict_wid = pict_hgt = 8;
2595 ANGBAND_GRAF = "old";
2598 case GRAPHICS_ADAM_BOLT:
2599 path_build(filename, sizeof(filename), ANGBAND_DIR_XTRA, "graf/16x16.bmp");
2600 if (0 == fd_close(fd_open(filename, O_RDONLY)))
2602 use_graphics = TRUE;
2603 pict_wid = pict_hgt = 16;
2604 ANGBAND_GRAF = "new";
2611 Display *dpy = Metadpy->dpy;
2613 tiles_raw = ReadBMP(dpy, filename);
2614 for (i = 0; i < num_term; i++)
2616 term_data *td = &data[i];
2617 term_type *t = &td->t;
2618 t->pict_hook = Term_pict_x11;
2619 t->higher_pict = TRUE;
2621 ResizeImage(dpy, tiles_raw,
2623 td->fnt->twid, td->fnt->hgt);
2626 for (i = 0; i < num_term; i++)
2628 term_data *td = &data[i];
2630 int depth = DefaultDepth(dpy, DefaultScreen(dpy));
2631 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
2634 jj = (depth - 1) >> 2;
2635 while (jj >>= 1) ii <<= 1;
2636 total = td->fnt->twid * td->fnt->hgt * ii;
2637 TmpData = (char *)malloc(total);
2638 td->TmpImage = XCreateImage(dpy,visual,depth,
2639 ZPixmap, 0, TmpData,
2640 td->fnt->twid, td->fnt->hgt, 8, 0);
2643 #endif /* ! USE_XFT */
2647 #endif /* USE_X11 */