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.
12 * This file helps Angband work with UNIX/X11 computers.
14 * To use this file, compile with "USE_X11" defined, and link against all
15 * the various "X11" libraries which may be needed.
17 * See also "main-xaw.c".
19 * Part of this file provides a user interface package composed of several
20 * pseudo-objects, including "metadpy" (a display), "infowin" (a window),
21 * "infoclr" (a color), and "infofnt" (a font). Actually, the package was
22 * originally much more interesting, but it was bastardized to keep this
25 * The rest of this file is an implementation of "main-xxx.c" for X11.
27 * Most of this file is by Ben Harrison (benh@phial.com).
31 * The following shell script can be used to launch Angband, assuming that
32 * it was extracted into "~/Angband", and compiled using "USE_X11", on a
33 * Linux machine, with a 1280x1024 screen, using 6 windows (with the given
34 * characteristics), with gamma correction of 1.8 -> (1 / 1.8) * 256 = 142,
35 * and without graphics (add "-g" for graphics). Just copy this comment
36 * into a file, remove the leading " * " characters (and the head/tail of
37 * this comment), and make the file executable.
43 * echo "Launching angband..."
47 * setenv ANGBAND_X11_FONT_0 10x20
48 * setenv ANGBAND_X11_AT_X_0 5
49 * setenv ANGBAND_X11_AT_Y_0 510
52 * setenv ANGBAND_X11_FONT_1 8x13
53 * setenv ANGBAND_X11_AT_X_1 5
54 * setenv ANGBAND_X11_AT_Y_1 22
55 * setenv ANGBAND_X11_ROWS_1 35
58 * setenv ANGBAND_X11_FONT_2 8x13
59 * setenv ANGBAND_X11_AT_X_2 635
60 * setenv ANGBAND_X11_AT_Y_2 182
61 * setenv ANGBAND_X11_ROWS_3 23
64 * setenv ANGBAND_X11_FONT_3 8x13
65 * setenv ANGBAND_X11_AT_X_3 635
66 * setenv ANGBAND_X11_AT_Y_3 22
67 * setenv ANGBAND_X11_ROWS_3 12
69 * # Monster recall window
70 * setenv ANGBAND_X11_FONT_4 6x13
71 * setenv ANGBAND_X11_AT_X_4 817
72 * setenv ANGBAND_X11_AT_Y_4 847
73 * setenv ANGBAND_X11_COLS_4 76
74 * setenv ANGBAND_X11_ROWS_4 11
76 * # Object recall window
77 * setenv ANGBAND_X11_FONT_5 6x13
78 * setenv ANGBAND_X11_AT_X_5 817
79 * setenv ANGBAND_X11_AT_Y_5 520
80 * setenv ANGBAND_X11_COLS_5 76
81 * setenv ANGBAND_X11_ROWS_5 24
83 * # The build directory
87 * setenv ANGBAND_X11_GAMMA 142
90 * ./src/angband -mx11 -- -n6 &
94 #include "cmd-io/macro-util.h"
95 #include "game-option/runtime-arguments.h"
96 #include "game-option/special-options.h"
97 #include "io/files-util.h"
98 #include "locale/japanese.h"
99 #include "locale/utf-8.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/angband.h"
104 #include "system/system-variables.h"
105 #include "term/gameterm.h"
106 #include "term/term-color-types.h"
107 #include "util/angband-files.h"
108 #include "util/bit-flags-calculator.h"
109 #include "util/int-char-converter.h"
110 #include "util/string-processor.h"
113 * Available graphic modes
116 #define GRAPHICS_NONE 0
117 #define GRAPHICS_ORIGINAL 1
118 #define GRAPHICS_ADAM_BOLT 2
119 #define GRAPHICS_HENGBAND 3
123 #ifndef __MAKEDEPEND__
124 #include <X11/Xlib.h>
125 #include <X11/Xutil.h>
126 #include <X11/keysym.h>
127 #include <X11/keysymdef.h>
129 #include <X11/Xlocale.h>
131 #include <X11/Xatom.h>
132 #endif /* __MAKEDEPEND__ */
135 #include <X11/Xft/Xft.h>
139 * Include some helpful X11 code.
141 #include "maid-x11.cpp"
146 * 1) On a monochrome (or "fake-monochrome") display, all colors
147 * will be "cast" to "fg," except for the bg color, which is,
148 * obviously, cast to "bg". Thus, one can ignore this setting.
150 * 2) Because of the inner functioning of the color allocation
151 * routines, colors may be specified as (a) a typical color name,
152 * (b) a hexidecimal color specification (preceded by a pound sign),
153 * or (c) by strings such as "fg", "bg", "zg".
155 * 3) Due to the workings of the init routines, many colors
156 * may also be dealt with by their actual pixel values. Note that
157 * the pixel with all bits set is "zg = (1<<metadpy->depth)-1", which
158 * is not necessarily either black or white.
161 /**** Generic Types ****/
164 * An X11 pixell specifier
167 typedef XftColor Pixell;
169 typedef unsigned long Pixell;
173 * The structures defined below
175 typedef struct metadpy metadpy;
176 typedef struct infowin infowin;
177 typedef struct infoclr infoclr;
178 typedef struct infofnt infofnt;
181 * A structure summarizing a given Display.
183 * - The Display itself
184 * - The default Screen for the display
185 * - The virtual root (usually just the root)
186 * - The default colormap (from a macro)
188 * - The "name" of the display
190 * - The socket to listen to for events
192 * - The width of the display screen (from a macro)
193 * - The height of the display screen (from a macro)
194 * - The bit depth of the display screen (from a macro)
196 * - The black Pixell (from a macro)
197 * - The white Pixell (from a macro)
199 * - The background Pixell (default: black)
200 * - The foreground Pixell (default: white)
201 * - The maximal Pixell (Equals: ((2 ^ depth)-1), is usually ugly)
203 * - Bit Flag: Force all colors to black and white (default: !color)
204 * - Bit Flag: Allow the use of color (default: depth > 1)
205 * - Bit Flag: We created 'dpy', and so should nuke it when done.
239 * A Structure summarizing Window Information.
241 * I assume that a window is at most 30000 pixels on a side.
242 * I assume that the root windw is also at most 30000 square.
245 * - The current Input Event Mask
247 * - The location of the window
248 * - The width, height of the window
249 * - The border width of this window
251 * - Byte: 1st Extra byte
253 * - Bit Flag: This window is currently Mapped
254 * - Bit Flag: This window needs to be redrawn
255 * - Bit Flag: This window has been resized
257 * - Bit Flag: We should nuke 'win' when done with it
259 * - Bit Flag: 1st extra flag
260 * - Bit Flag: 2nd extra flag
261 * - Bit Flag: 3rd extra flag
262 * - Bit Flag: 4th extra flag
297 * A Structure summarizing Operation+Color Information
299 * - The actual GC corresponding to this info
301 * - The Foreground Pixell Value
302 * - The Background Pixell Value
304 * - Num (0-15): The operation code (As in Clear, Xor, etc)
305 * - Bit Flag: The GC is in stipple mode
306 * - 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
356 /* Set current metadpy (Metadpy) to 'M' */
357 #define Metadpy_set(M) Metadpy = M
359 /* Initialize 'M' using Display 'D' */
360 #define Metadpy_init_dpy(D) Metadpy_init_2(D, cNULL)
362 /* Initialize 'M' using a Display named 'N' */
363 #define Metadpy_init_name(N) Metadpy_init_2((Display *)(NULL), N)
365 /* Initialize 'M' using the standard Display */
366 #define Metadpy_init() Metadpy_init_name("")
368 /* Init an infowin by giving father as an (info_win*) (or NULL), and data */
369 #define Infowin_init_dad(D, X, Y, W, H, B, FG, BG) Infowin_init_data(((D) ? ((D)->win) : (Window)(None)), X, Y, W, H, B, FG, BG)
371 /* Init a top level infowin by pos,size,bord,Colors */
372 #define Infowin_init_top(X, Y, W, H, B, FG, BG) Infowin_init_data(None, X, Y, W, H, B, FG, BG)
374 /* Request a new standard window by giving Dad infowin and X,Y,W,H */
375 #define Infowin_init_std(D, X, Y, W, H, B) Infowin_init_dad(D, X, Y, W, H, B, Metadpy->fg, Metadpy->bg)
377 /* Set the current Infowin */
378 #define Infowin_set(I) (Infowin = (I))
380 /* Set the current Infoclr */
381 #define Infoclr_set(C) (Infoclr = (C))
383 #define Infoclr_init_ppo(F, B, O, M) Infoclr_init_data(F, B, O, M)
385 #define Infoclr_init_cco(F, B, O, M) Infoclr_init_ppo(Infoclr_Pixell(F), Infoclr_Pixell(B), O, M)
387 #define Infoclr_init_ppn(F, B, O, M) Infoclr_init_ppo(F, B, Infoclr_Opcode(O), M)
389 #define Infoclr_init_ccn(F, B, O, M) Infoclr_init_cco(F, B, Infoclr_Opcode(O), M)
391 /* Set the current infofnt */
392 #define Infofnt_set(I) (Infofnt = (I))
394 /* Errr: Expose Infowin */
395 #define Infowin_expose() (!(Infowin->redraw = 1))
397 /* Errr: Unxpose Infowin */
398 #define Infowin_unexpose() (Infowin->redraw = 0)
401 * The "default" values
403 static metadpy metadpy_default;
406 * The "current" variables
408 static metadpy *Metadpy = &metadpy_default;
409 static infowin *Infowin = (infowin *)(NULL);
411 static infowin *Focuswin = (infowin *)(NULL);
413 static infoclr *Infoclr = (infoclr *)(NULL);
414 static infofnt *Infofnt = (infofnt *)(NULL);
417 * Init the current metadpy, with various initialization stuff.
420 * dpy: The Display* to use (if NULL, create it)
421 * name: The name of the Display (if NULL, the current)
424 * If 'name' is NULL, but 'dpy' is set, extract name from dpy
425 * If 'dpy' is NULL, then Create the named Display
426 * If 'name' is NULL, and so is 'dpy', use current Display
428 * Return -1 if no Display given, and none can be opened.
430 static errr Metadpy_init_2(Display *dpy, concptr name)
432 metadpy *m = Metadpy;
434 dpy = XOpenDisplay(name);
444 m->screen = DefaultScreenOfDisplay(dpy);
445 m->root = RootWindowOfScreen(m->screen);
446 m->cmap = DefaultColormapOfScreen(m->screen);
447 m->name = DisplayString(dpy);
448 m->fd = ConnectionNumber(Metadpy->dpy);
449 m->width = WidthOfScreen(m->screen);
450 m->height = HeightOfScreen(m->screen);
451 m->depth = DefaultDepthOfScreen(m->screen);
454 Visual *vis = DefaultVisual(dpy, 0);
455 XftColorAllocName(dpy, vis, m->cmap, "black", &m->black);
456 XftColorAllocName(dpy, vis, m->cmap, "white", &m->white);
458 m->black = BlackPixelOfScreen(m->screen);
459 m->white = WhitePixelOfScreen(m->screen);
466 m->zg = (1 << m->depth) - 1;
469 m->color = ((m->depth > 1) ? 1 : 0);
470 m->mono = ((m->color) ? 0 : 1);
475 * General Flush/ Sync/ Discard routine
477 static errr Metadpy_update(int flush, int sync, int discard)
480 XFlush(Metadpy->dpy);
482 XSync(Metadpy->dpy, discard);
490 static errr Metadpy_do_beep(void)
492 XBell(Metadpy->dpy, 100);
497 * Set the name (in the title bar) of Infowin
499 static errr Infowin_set_name(concptr name)
506 st = XStringListToTextProperty(&bp, 1, &tp);
508 XSetWMName(Metadpy->dpy, Infowin->win, &tp);
513 * Prepare a new 'infowin'.
515 static errr Infowin_prepare(Window xid)
517 infowin *iwin = Infowin;
519 XWindowAttributes xwa;
521 unsigned int w, h, b, d;
523 XGetGeometry(Metadpy->dpy, xid, &tmp_win, &x, &y, &w, &h, &b, &d);
526 Visual *vis = DefaultVisual(Metadpy->dpy, 0);
527 if (vis->c_class != TrueColor) {
528 quit_fmt("Display does not support truecolor.\n");
530 iwin->draw = XftDrawCreate(Metadpy->dpy, iwin->win, vis, Metadpy->cmap);
539 XGetWindowAttributes(Metadpy->dpy, xid, &xwa);
540 iwin->mask = xwa.your_event_mask;
541 iwin->mapped = ((xwa.map_state == IsUnmapped) ? 0 : 1);
547 * Init an infowin by giving some data.
550 * dad: The Window that should own this Window (if any)
551 * x,y: The position of this Window
552 * w,h: The size of this Window
553 * b,d: The border width and pixel depth
556 * If 'dad == None' assume 'dad == root'
558 static errr Infowin_init_data(Window dad, int x, int y, int w, int h, int b, Pixell fg, Pixell bg)
561 (void)WIPE(Infowin, infowin);
566 xid = XCreateSimpleWindow(Metadpy->dpy, dad, x, y, w, h, b, fg.pixel, bg.pixel);
568 xid = XCreateSimpleWindow(Metadpy->dpy, dad, x, y, w, h, b, fg, bg);
571 XSelectInput(Metadpy->dpy, xid, 0L);
573 return (Infowin_prepare(xid));
577 * Modify the event mask of an Infowin
579 static errr Infowin_set_mask(long mask)
581 Infowin->mask = mask;
582 XSelectInput(Metadpy->dpy, Infowin->win, Infowin->mask);
587 * Request that Infowin be mapped
589 static errr Infowin_map(void)
591 XMapWindow(Metadpy->dpy, Infowin->win);
596 * Request that Infowin be raised
598 static errr Infowin_raise(void)
600 XRaiseWindow(Metadpy->dpy, Infowin->win);
605 * Request that Infowin be moved to a new location
607 static errr Infowin_impell(int x, int y)
609 XMoveWindow(Metadpy->dpy, Infowin->win, x, y);
616 static errr Infowin_resize(int w, int h)
618 XResizeWindow(Metadpy->dpy, Infowin->win, w, h);
623 * Visually clear Infowin
625 static errr Infowin_wipe(void)
627 XClearWindow(Metadpy->dpy, Infowin->win);
632 * A NULL terminated pair list of legal "operation names"
634 * Pairs of values, first is texttual name, second is the string
635 * holding the decimal value that the operation corresponds to.
638 static concptr opcode_pairs[] =
657 "+copyInverted", "12",
665 * Parse a word into an operation "code"
668 * str: A string, hopefully representing an Operation
671 * 0-15: if 'str' is a valid Operation
672 * -1: if 'str' could not be parsed
674 static int Infoclr_Opcode(concptr str)
677 for (i = 0; opcode_pairs[i * 2]; ++i) {
678 if (streq(opcode_pairs[i * 2], str)) {
679 return (atoi(opcode_pairs[i * 2 + 1]));
687 * Initialize an infoclr with some data
690 * fg: The Pixell for the requested Foreground (see above)
691 * bg: The Pixell for the requested Background (see above)
692 * op: The Opcode for the requested Operation (see above)
693 * stip: The stipple mode
695 static errr Infoclr_init_data(Pixell fg, Pixell bg, int op, int stip)
697 infoclr *iclr = Infoclr;
702 unsigned long gc_mask;
706 if (bg > Metadpy->zg)
708 if (fg > Metadpy->zg)
710 if ((op < 0) || (op > 15))
719 gcv.foreground = (bg ^ fg);
721 gcv.fill_style = (stip ? FillStippled : FillSolid);
722 gcv.graphics_exposures = False;
723 gc_mask = (GCFunction | GCBackground | GCForeground | GCFillStyle | GCGraphicsExposures);
724 gc = XCreateGC(Metadpy->dpy, Metadpy->root, gc_mask, &gcv);
727 (void)WIPE(iclr, infoclr);
737 iclr->stip = stip ? 1 : 0;
742 * Change the 'fg' for an infoclr
745 * fg: The Pixell for the requested Foreground (see above)
747 static errr Infoclr_change_fg(Pixell fg)
749 infoclr *iclr = Infoclr;
754 if (fg > Metadpy->zg)
757 XSetForeground(Metadpy->dpy, iclr->gc, fg);
764 * Prepare a new 'infofnt'
767 static errr Infofnt_prepare(XftFont *info)
769 static errr Infofnt_prepare(XFontSet info)
772 infofnt *ifnt = Infofnt;
776 XFontStruct **fontinfo;
779 int ascent, descent, width;
785 ifnt->asc = info->ascent;
786 ifnt->hgt = info->height;
787 const char *text = "A";
789 XftTextExtentsUtf8(Metadpy->dpy, info, (FcChar8 *)text, strlen(text), &extent);
790 ifnt->wid = extent.xOff;
792 n_fonts = XFontsOfFontSet(info, &fontinfo, &fontname);
794 ascent = descent = width = 0;
795 while (n_fonts-- > 0) {
796 cs = &((*fontinfo)->max_bounds);
797 if (ascent < (*fontinfo)->ascent)
798 ascent = (*fontinfo)->ascent;
799 if (descent < (*fontinfo)->descent)
800 descent = (*fontinfo)->descent;
801 if (((*fontinfo)->max_byte1) > 0) {
802 /* 多バイト文字の場合は幅半分(端数切り上げ)で評価する */
803 if (width < (cs->width + 1) / 2)
804 width = (cs->width + 1) / 2;
806 if (width < cs->width)
813 ifnt->hgt = ascent + descent;
818 ifnt->twid = 2 * ifnt->wid;
820 ifnt->twid = ifnt->wid;
826 * Init an infofnt by its Name
829 * name: The name of the requested Font
831 static void Infofnt_init_data(concptr name)
844 quit("Missing font!");
847 info = XftFontOpenName(Metadpy->dpy, 0, name);
848 /* TODO: error handling */
850 info = XCreateFontSet(Metadpy->dpy, name, &missing_list, &missing_count, &default_font);
851 if (missing_count > 0) {
852 printf("missing font(s): \n");
853 while (missing_count-- > 0) {
854 printf("\t%s\n", missing_list[missing_count]);
856 XFreeStringList(missing_list);
861 quit_fmt("Failed to find font:\"%s\"", name);
863 (void)WIPE(Infofnt, infofnt);
864 if (Infofnt_prepare(info)) {
866 XftFontClose(Metadpy->dpy, info);
868 XFreeFontSet(Metadpy->dpy, info);
870 quit_fmt("Failed to prepare font:\"%s\"", name);
873 Infofnt->name = string_make(name);
878 static void Infofnt_text_std_xft_draw_str(int x, int y, concptr str, concptr str_end)
881 while (str < str_end) {
882 const int byte_len = utf8_next_char_byte_length(str);
884 if (byte_len == 0 || str + byte_len > str_end) {
888 XftDrawStringUtf8(Infowin->draw, &Infoclr->fg, Infofnt->info, x + Infofnt->wid * offset, y, (const FcChar8 *)str, byte_len);
889 offset += (byte_len > 1 ? 2 : 1);
898 static errr Infofnt_text_std(int x, int y, concptr str, int len)
906 y = (y * Infofnt->hgt) + Infofnt->asc + Infowin->oy;
907 x = (x * Infofnt->wid) + Infowin->ox;
911 for (i = 0; i < len; ++i) {
912 XDrawImageString(Metadpy->dpy, Infowin->win, Infoclr->gc, x + i * Infofnt->wid + Infofnt->off, y, str + i, 1);
918 int utf8_len = euc_to_utf8(str, len, utf8_buf, sizeof(utf8_buf));
925 XftDraw *draw = Infowin->draw;
930 r.width = Infofnt->wid * len;
931 r.height = Infofnt->hgt;
932 XftDrawSetClipRectangles(draw, x, y - Infofnt->asc, &r, 1);
933 XftDrawRect(draw, &Infoclr->bg, x, y - Infofnt->asc, Infofnt->wid * len, Infofnt->hgt);
934 Infofnt_text_std_xft_draw_str(x, y, _(utf8_buf, str), _(utf8_buf + utf8_len, str + len));
935 XftDrawSetClip(draw, 0);
937 XmbDrawImageString(Metadpy->dpy, Infowin->win, Infofnt->info, Infoclr->gc, x, y, _(utf8_buf, str), _(utf8_len, len));
945 * Painting where text would be
947 static errr Infofnt_text_non(int x, int y, concptr str, int len)
953 w = len * Infofnt->wid;
954 x = x * Infofnt->wid + Infowin->ox;
956 y = y * h + Infowin->oy;
959 XftDrawRect(Infowin->draw, &Infoclr->fg, x, y, w, h);
961 XFillRectangle(Metadpy->dpy, Infowin->win, Infoclr->gc, x, y, w, h);
968 * Angband specific code follows... (ANGBAND)
972 * Hack -- cursor color
974 static infoclr * xor_ ;
979 static infoclr *clr[256];
982 * Color info (unused, red, green, blue).
984 static byte color_table[256][4];
989 typedef struct term_data term_data;
992 * A structure for each "term"
1005 * The number of term data structures
1007 #define MAX_TERM_DATA 8
1010 * The array of term data structures
1012 static term_data data[MAX_TERM_DATA];
1014 /* Use short names for the most commonly used elements of various structures. */
1015 #define DPY (Metadpy->dpy)
1016 #define WIN (Infowin->win)
1018 /* Describe a set of co-ordinates. */
1019 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 {
1031 bool select; /* The selection is currently in use. */
1032 bool drawn; /* The selection is currently displayed. */
1033 term_type *t; /* The window where the selection is found. */
1034 co_ord init; /* The starting co-ordinates. */
1035 co_ord cur; /* The end co-ordinates (the current ones if still copying). */
1036 co_ord old; /* The previous end co-ordinates. */
1037 Time time; /* The time at which the selection was finalised. */
1040 static x11_selection_type s_ptr[1];
1043 static void send_key(const char key)
1045 // Windows ドライバと同様、自前でキューを操作する。
1046 // 逆順に term_key_push() する方法だと長い日本語を入力したときにテキストの
1049 // キーバッファが一杯なら入力を捨てる
1050 const int head_nxt = Term->key_head + 1 == Term->key_size ? 0 : Term->key_head + 1;
1051 if (head_nxt == Term->key_tail) {
1052 plog_fmt("key buffer overflow, ignoring key 0x%02X", key);
1056 Term->key_queue[Term->key_head] = key;
1057 Term->key_head = head_nxt;
1061 static void send_keys(const char *const keys)
1063 for (const char *p = keys; *p != '\0'; ++p)
1068 * Process a keypress event
1070 * Also appears in "main-xaw.c".
1072 static void react_keypress(XKeyEvent *xev)
1074 int n, mc, ms, mo, mx;
1076 XKeyEvent *ev = (XKeyEvent *)(xev);
1082 int valid_keysym = true;
1086 if (Focuswin && Focuswin->xic) {
1088 n = XmbLookupString(Focuswin->xic, ev, buf, 125, &ks, &status);
1089 if (status == XBufferOverflow) {
1090 printf("Input is too long, and dropped\n");
1093 if (status != XLookupKeySym && status != XLookupBoth) {
1094 valid_keysym = false;
1097 n = XLookupString(ev, buf, 125, &ks, NULL);
1100 n = XLookupString(ev, buf, 125, &ks, NULL);
1106 if (!valid_keysym) { /* XIMからの入力時のみ false になる */
1108 char euc_buf[sizeof(buf)];
1109 /* strlen + 1 を渡して文字列終端('\0')を含めて変換する */
1110 if (utf8_to_euc(buf, strlen(buf) + 1, euc_buf, sizeof(euc_buf)) < 0) {
1114 send_keys(_(euc_buf, buf));
1119 if (IsModifierKey(ks))
1123 mc = any_bits(ev->state, ControlMask);
1124 ms = any_bits(ev->state, ShiftMask);
1125 mo = any_bits(ev->state, Mod1Mask);
1126 mx = any_bits(ev->state, Mod2Mask);
1127 if (n && !mo && !mx && !IsSpecialKey(ks)) {
1152 case XK_BackSpace: {
1159 sprintf(msg, "%c%s%s%s%s_%lX%c", 31, mc ? "N" : "", ms ? "S" : "", mo ? "O" : "", mx ? "M" : "", (unsigned long)(ks), 13);
1161 sprintf(msg, "%c%s%s%s%sK_%X%c", 31, mc ? "N" : "", ms ? "S" : "", mo ? "O" : "", mx ? "M" : "", ev->keycode, 13);
1166 if (n && (macro_find_exact(msg) < 0)) {
1167 macro_add(msg, buf);
1172 * Find the square a particular pixel is part of.
1174 static void pixel_to_square(int *const x, int *const y, const int ox, const int oy)
1176 (*x) = (ox - Infowin->ox) / Infofnt->wid;
1177 (*y) = (oy - Infowin->oy) / Infofnt->hgt;
1181 * Find the pixel at the top-left corner of a square.
1183 static void square_to_pixel(int *const x, int *const y, const int ox, const int oy)
1185 (*x) = ox * Infofnt->wid + Infowin->ox;
1186 (*y) = oy * Infofnt->hgt + Infowin->oy;
1190 * Convert co-ordinates from starting corner/opposite corner to minimum/maximum.
1192 static void sort_co_ord(co_ord *min, co_ord *max, const co_ord *b, const co_ord *a)
1194 min->x = MIN(a->x, b->x);
1195 min->y = MIN(a->y, b->y);
1196 max->x = MAX(a->x, b->x);
1197 max->y = MAX(a->y, b->y);
1201 * Remove the selection by redrawing it.
1203 static void mark_selection_clear(int x1, int y1, int x2, int y2) { term_redraw_section(x1, y1, x2, y2); }
1206 * Select an area by drawing a grey box around it.
1207 * NB. These two functions can cause flicker as the selection is modified,
1208 * as the game redraws the entire marked section.
1210 static void mark_selection_mark(int x1, int y1, int x2, int y2)
1212 square_to_pixel(&x1, &y1, x1, y1);
1213 square_to_pixel(&x2, &y2, x2, y2);
1215 XftDrawRect(Infowin->draw, &clr[2]->fg, x1, y1, x2 - x1 + Infofnt->wid - 1, y2 - y1 + Infofnt->hgt - 1);
1217 XDrawRectangle(Metadpy->dpy, Infowin->win, clr[2]->gc, x1, y1, x2 - x1 + Infofnt->wid - 1, y2 - y1 + Infofnt->hgt - 1);
1222 * Mark a selection by drawing boxes around it (for now).
1224 static void mark_selection(void)
1227 term_type *old = Term;
1228 bool draw = s_ptr->select;
1229 bool clear = s_ptr->drawn;
1230 if (s_ptr->t != old)
1231 term_activate(s_ptr->t);
1234 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->old);
1235 mark_selection_clear(min.x, min.y, max.x, max.y);
1238 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->cur);
1239 mark_selection_mark(min.x, min.y, max.x, max.y);
1242 if (s_ptr->t != old)
1245 s_ptr->old.x = s_ptr->cur.x;
1246 s_ptr->old.y = s_ptr->cur.y;
1247 s_ptr->drawn = s_ptr->select;
1251 * Forget a selection for one reason or another.
1253 static void copy_x11_release(void)
1255 s_ptr->select = false;
1260 * Start to select some text on the screen.
1262 static void copy_x11_start(int x, int y)
1268 s_ptr->init.x = s_ptr->cur.x = s_ptr->old.x = x;
1269 s_ptr->init.y = s_ptr->cur.y = s_ptr->old.y = y;
1273 * Respond to movement of the mouse when selecting text.
1275 static void copy_x11_cont(int x, int y, unsigned int buttons)
1277 x = MIN(MAX(x, 0), Term->wid - 1);
1278 y = MIN(MAX(y, 0), Term->hgt - 1);
1279 if (~buttons & Button1Mask)
1281 if (s_ptr->t != Term)
1283 if (x == s_ptr->old.x && y == s_ptr->old.y && s_ptr->select)
1286 s_ptr->select = true;
1293 * Respond to release of the left mouse button by putting the selected text in
1294 * the primary buffer.
1296 static void copy_x11_end(const Time time)
1300 if (s_ptr->t != Term)
1304 XSetSelectionOwner(Metadpy->dpy, XA_PRIMARY, Infowin->win, time);
1305 if (XGetSelectionOwner(Metadpy->dpy, XA_PRIMARY) != Infowin->win) {
1306 s_ptr->select = false;
1311 static Atom xa_targets, xa_timestamp, xa_text, xa_compound_text, xa_utf8;
1314 * Set the required variable atoms at start-up to avoid errors later.
1316 static void set_atoms(void)
1318 xa_targets = XInternAtom(DPY, "TARGETS", False);
1319 xa_timestamp = XInternAtom(DPY, "TIMESTAMP", False);
1320 xa_text = XInternAtom(DPY, "TEXT", False);
1321 xa_compound_text = XInternAtom(DPY, "COMPOUND_TEXT", False);
1322 xa_utf8 = XInternAtom(DPY, "UTF8_STRING", False);
1325 static Atom request_target = 0;
1328 * Send a message to request that the PRIMARY buffer be sent here.
1330 static void paste_x11_request(Atom target, const Time time)
1332 Atom property = XInternAtom(DPY, "__COPY_TEXT", False);
1333 if (XGetSelectionOwner(DPY, XA_PRIMARY) == None) {
1335 /* bell("No selection found."); */
1339 request_target = target;
1340 XConvertSelection(DPY, XA_PRIMARY, target, property, WIN, time);
1344 * Add the contents of the PRIMARY buffer to the input queue.
1346 * Hack - This doesn't use the "time" of the event, and so accepts anything a
1347 * client tries to send it.
1349 static void paste_x11_accept(const XSelectionEvent *ptr)
1352 const long offset = 0;
1353 const long length = 32000;
1354 XTextProperty xtextproperty;
1356 Atom property = XInternAtom(DPY, "__COPY_TEXT", False);
1357 if (ptr->property == None) {
1358 if (request_target == xa_compound_text) {
1359 paste_x11_request(XA_STRING, ptr->time);
1362 plog("Paste failure (remote client could not send).");
1368 if (ptr->selection != XA_PRIMARY) {
1369 plog("Paste failure (remote client did not send primary selection).");
1373 if (ptr->target != request_target) {
1374 plog("Paste failure (selection in unknown format).");
1378 if (XGetWindowProperty(Metadpy->dpy, Infowin->win, property, offset, length, true, request_target, &xtextproperty.encoding, &xtextproperty.format,
1379 &xtextproperty.nitems, &left, &xtextproperty.value)
1384 if (request_target == xa_compound_text) {
1388 XmbTextPropertyToTextList(DPY, &xtextproperty, &list, &count);
1393 for (i = 0; i < count; i++) {
1394 err = type_string(list[i], 0);
1399 XFreeStringList(list);
1402 err = type_string((char *)xtextproperty.value, xtextproperty.nitems);
1405 XFree(xtextproperty.value);
1407 plog("Paste failure (too much text selected).");
1412 * Add a character to a string in preparation for sending it to another
1413 * client as a STRING.
1414 * This doesn't change anything, as clients tend not to have difficulty in
1415 * receiving this format (although the standard specifies a restricted set).
1416 * Strings do not have a colour.
1418 static bool paste_x11_send_text(XSelectionRequestEvent *rq)
1427 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->cur);
1428 if (XGetSelectionOwner(DPY, XA_PRIMARY) != WIN) {
1432 for (y = 0; y < Term->hgt; y++) {
1441 for (l = 0, x = 0; x < Term->wid; x++) {
1446 term_what(x, y, &a, &c);
1449 else if (iskanji(c))
1458 * A single kanji character was divided in two...
1459 * Delete the garbage.
1461 if ((2 == kanji && x == min.x) || (1 == kanji && x == max.x))
1469 term_what(x, y, &a, &c);
1477 while (l >= 1 && buf[l - 1] == ' ')
1480 // 複数行の場合、各行末に改行を付加。
1481 if (min.y != max.y) {
1489 char utf8_buf[2048];
1490 const int len = euc_to_utf8(buf, l, utf8_buf, sizeof(utf8_buf));
1492 if (_(len, l) > 0) {
1493 XChangeProperty(DPY, rq->requestor, rq->property, xa_utf8, 8, PropModeAppend, (unsigned char *)_(utf8_buf, buf), _(len, l));
1501 * Send some text requested by another X client.
1503 static void paste_x11_send(XSelectionRequestEvent *rq)
1506 XSelectionEvent *ptr = &(event.xselection);
1508 ptr->type = SelectionNotify;
1509 ptr->property = rq->property;
1510 ptr->display = rq->display;
1511 ptr->requestor = rq->requestor;
1512 ptr->selection = rq->selection;
1513 ptr->target = rq->target;
1514 ptr->time = rq->time;
1517 * Paste the appropriate information for each target type.
1518 * Note that this currently rejects MULTIPLE targets.
1521 if (rq->target == xa_utf8) {
1522 if (!paste_x11_send_text(rq))
1523 ptr->property = None;
1524 } else if (rq->target == xa_targets) {
1525 Atom target_list[] = { xa_targets, xa_utf8 };
1527 DPY, rq->requestor, rq->property, XA_ATOM, 32, PropModeReplace, (unsigned char *)target_list, (sizeof(target_list) / sizeof(target_list[0])));
1529 ptr->property = None;
1532 XSendEvent(DPY, rq->requestor, false, NoEventMask, &event);
1536 * Handle various events conditional on presses of a mouse button.
1538 static void handle_button(Time time, int x, int y, int button, bool press)
1540 pixel_to_square(&x, &y, x, y);
1542 if (press && button == 1)
1543 copy_x11_start(x, y);
1544 if (!press && button == 1)
1546 if (!press && button == 2)
1547 paste_x11_request(xa_compound_text, time);
1553 static errr CheckEvent(bool wait)
1555 term_data *old_td = (term_data *)(Term->data);
1557 XEvent xev_body, *xev = &xev_body;
1559 term_data *td = NULL;
1560 infowin *iwin = NULL;
1568 if (!wait && !XPending(Metadpy->dpy))
1571 if (s_ptr->select && !s_ptr->drawn)
1574 XNextEvent(Metadpy->dpy, xev);
1577 } while (XFilterEvent(xev, xev->xany.window));
1580 if (xev->type == MappingNotify) {
1581 XRefreshKeyboardMapping(&xev->xmapping);
1585 for (i = 0; i < MAX_TERM_DATA; i++) {
1588 if (xev->xany.window == data[i].win->win) {
1598 term_activate(&td->t);
1600 switch (xev->type) {
1602 case ButtonRelease: {
1603 bool press = (xev->type == ButtonPress);
1604 int x = xev->xbutton.x;
1605 int y = xev->xbutton.y;
1607 if (xev->xbutton.button == Button1)
1609 else if (xev->xbutton.button == Button2)
1611 else if (xev->xbutton.button == Button3)
1613 else if (xev->xbutton.button == Button4)
1615 else if (xev->xbutton.button == Button5)
1620 handle_button(xev->xbutton.time, x, y, z, press);
1627 case MotionNotify: {
1628 int x = xev->xmotion.x;
1629 int y = xev->xmotion.y;
1630 unsigned int z = xev->xmotion.state;
1631 pixel_to_square(&x, &y, x, y);
1632 copy_x11_cont(x, y, z);
1635 case SelectionNotify: {
1636 paste_x11_accept(&(xev->xselection));
1639 case SelectionRequest: {
1640 paste_x11_send(&(xev->xselectionrequest));
1643 case SelectionClear: {
1644 s_ptr->select = false;
1652 term_activate(&old_td->t);
1653 react_keypress(&(xev->xkey));
1658 x1 = (xev->xexpose.x - Infowin->ox) / Infofnt->wid;
1659 x2 = (xev->xexpose.x + xev->xexpose.width - Infowin->ox) / Infofnt->wid;
1661 y1 = (xev->xexpose.y - Infowin->oy) / Infofnt->hgt;
1662 y2 = (xev->xexpose.y + xev->xexpose.height - Infowin->oy) / Infofnt->hgt;
1664 term_redraw_section(x1, y1, x2, y2);
1668 Infowin->mapped = 1;
1669 Term->mapped_flag = true;
1673 Infowin->mapped = 0;
1674 Term->mapped_flag = false;
1677 case ConfigureNotify: {
1678 int cols, rows, wid, hgt;
1679 int ox = Infowin->ox;
1680 int oy = Infowin->oy;
1681 Infowin->x = xev->xconfigure.x;
1682 Infowin->y = xev->xconfigure.y;
1683 Infowin->w = xev->xconfigure.width;
1684 Infowin->h = xev->xconfigure.height;
1685 cols = ((Infowin->w - (ox + ox)) / td->fnt->wid);
1686 rows = ((Infowin->h - (oy + oy)) / td->fnt->hgt);
1692 if (td == &data[0]) {
1699 wid = cols * td->fnt->wid + (ox + ox);
1700 hgt = rows * td->fnt->hgt + (oy + oy);
1701 term_resize(cols, rows);
1702 if ((Infowin->w != wid) || (Infowin->h != hgt)) {
1703 Infowin_set(td->win);
1704 Infowin_resize(wid, hgt);
1712 XSetICFocus(iwin->xic);
1719 XUnsetICFocus(iwin->xic);
1727 term_activate(&old_td->t);
1728 Infowin_set(old_td->win);
1733 * An array of sound file names
1735 static concptr sound_file[SOUND_MAX];
1738 * Check for existance of a file
1740 static bool check_file(concptr s)
1744 fff = fopen(s, "r");
1755 static void init_sound(void)
1760 char dir_xtra_sound[1024];
1761 path_build(dir_xtra_sound, sizeof(dir_xtra_sound), ANGBAND_DIR_XTRA, "sound");
1762 for (i = 1; i < SOUND_MAX; i++) {
1763 sprintf(wav, "%s.wav", angband_sound_name[i]);
1764 path_build(buf, sizeof(buf), dir_xtra_sound, wav);
1765 if (check_file(buf))
1766 sound_file[i] = string_make(buf);
1774 * Hack -- make a sound
1776 static errr Term_xtra_x11_sound(int v)
1781 if ((v < 0) || (v >= SOUND_MAX))
1786 sprintf(buf, "./playwave.sh %s\n", sound_file[v]);
1787 return (system(buf) < 0);
1791 * Handle "activation" of a term
1793 static errr Term_xtra_x11_level(int v)
1795 term_data *td = (term_data *)(Term->data);
1797 Infowin_set(td->win);
1798 Infofnt_set(td->fnt);
1807 static errr Term_xtra_x11_react(void)
1811 if (Metadpy->color) {
1812 for (i = 0; i < 256; i++) {
1813 if ((color_table[i][0] != angband_color_table[i][0]) || (color_table[i][1] != angband_color_table[i][1])
1814 || (color_table[i][2] != angband_color_table[i][2]) || (color_table[i][3] != angband_color_table[i][3])) {
1816 color_table[i][0] = angband_color_table[i][0];
1817 color_table[i][1] = angband_color_table[i][1];
1818 color_table[i][2] = angband_color_table[i][2];
1819 color_table[i][3] = angband_color_table[i][3];
1820 pixel = create_pixel(Metadpy->dpy, color_table[i][1], color_table[i][2], color_table[i][3]);
1821 Infoclr_set(clr[i]);
1822 Infoclr_change_fg(pixel);
1831 * Handle a "special request"
1833 static errr Term_xtra_x11(int n, int v)
1836 case TERM_XTRA_NOISE:
1839 case TERM_XTRA_SOUND:
1840 return (Term_xtra_x11_sound(v));
1842 case TERM_XTRA_FRESH:
1843 Metadpy_update(1, 1, 0);
1846 case TERM_XTRA_FRESH:
1847 Metadpy_update(1, 0, 0);
1850 case TERM_XTRA_BORED:
1851 return (CheckEvent(0));
1852 case TERM_XTRA_EVENT:
1853 return (CheckEvent(v));
1854 case TERM_XTRA_FLUSH:
1855 while (!CheckEvent(false))
1858 case TERM_XTRA_LEVEL:
1859 return (Term_xtra_x11_level(v));
1860 case TERM_XTRA_CLEAR:
1862 s_ptr->drawn = false;
1864 case TERM_XTRA_DELAY:
1867 case TERM_XTRA_REACT:
1868 return (Term_xtra_x11_react());
1875 * Draw the cursor as an inverted rectangle.
1877 * Consider a rectangular outline like "main-mac.c". XXX XXX
1879 static errr Term_curs_x11(int x, int y)
1883 XftDrawRect(Infowin->draw, &xor_->fg, x * Infofnt->wid + Infowin->ox, y * Infofnt->hgt + Infowin->oy, Infofnt->wid - 1, Infofnt->hgt - 1);
1884 XftDrawRect(Infowin->draw, &xor_->fg, x * Infofnt->wid + Infowin->ox + 1, y * Infofnt->hgt + Infowin->oy + 1, Infofnt->wid - 3, Infofnt->hgt - 3);
1886 XDrawRectangle(Metadpy->dpy, Infowin->win, xor_->gc, x * Infofnt->wid + Infowin->ox, y * Infofnt->hgt + Infowin->oy, Infofnt->wid - 1, Infofnt->hgt - 1);
1888 Metadpy->dpy, Infowin->win, xor_->gc, x * Infofnt->wid + Infowin->ox + 1, y * Infofnt->hgt + Infowin->oy + 1, Infofnt->wid - 3, Infofnt->hgt - 3);
1892 Infofnt_text_non(x, y, " ", 1);
1899 * Draw the double width cursor
1901 static errr Term_bigcurs_x11(int x, int y)
1905 XftDrawRect(Infowin->draw, &xor_->fg, x * Infofnt->wid + Infowin->ox, y * Infofnt->hgt + Infowin->oy, Infofnt->twid - 1, Infofnt->hgt - 1);
1906 XftDrawRect(Infowin->draw, &xor_->fg, x * Infofnt->wid + Infowin->ox + 1, y * Infofnt->hgt + Infowin->oy + 1, Infofnt->twid - 3, Infofnt->hgt - 3);
1909 Metadpy->dpy, Infowin->win, xor_->gc, x * Infofnt->wid + Infowin->ox, y * Infofnt->hgt + Infowin->oy, Infofnt->twid - 1, Infofnt->hgt - 1);
1911 Metadpy->dpy, Infowin->win, xor_->gc, x * Infofnt->wid + Infowin->ox + 1, y * Infofnt->hgt + Infowin->oy + 1, Infofnt->twid - 3, Infofnt->hgt - 3);
1915 Infofnt_text_non(x, y, " ", 2);
1922 * Erase some characters.
1924 static errr Term_wipe_x11(int x, int y, int n)
1926 Infoclr_set(clr[TERM_DARK]);
1927 Infofnt_text_non(x, y, "", n);
1928 s_ptr->drawn = false;
1933 * Draw some textual characters.
1935 static errr Term_text_x11(TERM_LEN x, TERM_LEN y, int n, TERM_COLOR a, concptr s)
1937 Infoclr_set(clr[a]);
1938 Infofnt_text_std(x, y, s, n);
1939 s_ptr->drawn = false;
1945 * Draw some graphical characters.
1947 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)
1960 unsigned long pixel, blank;
1962 term_data *td = (term_data *)(Term->data);
1969 for (i = 0; i < n; ++i, x += td->fnt->wid) {
1972 x1 = (c & 0x7F) * td->fnt->twid;
1973 y1 = (a & 0x7F) * td->fnt->hgt;
1974 if (td->tiles->width < x1 + td->fnt->wid || td->tiles->height < y1 + td->fnt->hgt) {
1975 XFillRectangle(Metadpy->dpy, td->win->win, clr[0]->gc, x, y, td->fnt->twid, td->fnt->hgt);
1982 x2 = (tc & 0x7F) * td->fnt->twid;
1983 y2 = (ta & 0x7F) * td->fnt->hgt;
1985 if (((x1 == x2) && (y1 == y2)) || !(((byte)ta & 0x80) && ((byte)tc & 0x80)) || td->tiles->width < x2 + td->fnt->wid
1986 || td->tiles->height < y2 + td->fnt->hgt) {
1987 XPutImage(Metadpy->dpy, td->win->win, clr[0]->gc, td->tiles, x1, y1, x, y, td->fnt->twid, td->fnt->hgt);
1989 blank = XGetPixel(td->tiles, 0, td->fnt->hgt * 6);
1990 for (k = 0; k < td->fnt->twid; k++) {
1991 for (l = 0; l < td->fnt->hgt; l++) {
1992 if ((pixel = XGetPixel(td->tiles, x1 + k, y1 + l)) == blank) {
1993 pixel = XGetPixel(td->tiles, x2 + k, y2 + l);
1996 XPutPixel(td->TmpImage, k, l, pixel);
2000 XPutImage(Metadpy->dpy, td->win->win, clr[0]->gc, td->TmpImage, 0, 0, x, y, td->fnt->twid, td->fnt->hgt);
2004 s_ptr->drawn = false;
2010 static void IMDestroyCallback(XIM, XPointer, XPointer);
2012 static void IMInstantiateCallback(Display *display, XPointer unused1, XPointer unused2)
2015 XIMCallback ximcallback;
2016 XIMStyles *xim_styles = NULL;
2022 xim = XOpenIM(display, NULL, NULL, NULL);
2024 printf("can't open IM\n");
2028 ximcallback.callback = IMDestroyCallback;
2029 ximcallback.client_data = NULL;
2030 XSetIMValues(xim, XNDestroyCallback, &ximcallback, NULL);
2031 XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL);
2032 for (i = 0; i < xim_styles->count_styles; i++) {
2033 if (xim_styles->supported_styles[i] == (XIMPreeditNothing | XIMStatusNothing))
2036 if (i >= xim_styles->count_styles) {
2037 printf("Sorry, your IM does not support 'Root' preedit style...\n");
2045 for (i = 0; i < MAX_TERM_DATA; i++) {
2046 infowin *iwin = data[i].win;
2049 iwin->xic = XCreateIC(xim, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing), XNClientWindow, iwin->win, XNFocusWindow, iwin->win, NULL);
2051 printf("Can't create input context for Term%d\n", i);
2055 if (XGetICValues(iwin->xic, XNFilterEvents, &iwin->xic_mask, NULL) != NULL) {
2056 iwin->xic_mask = 0L;
2059 XSelectInput(Metadpy->dpy, iwin->win, iwin->mask | iwin->xic_mask);
2065 static void IMDestroyCallback(XIM xim, XPointer client_data, XPointer call_data)
2071 if (call_data == NULL) {
2072 XRegisterIMInstantiateCallback(Metadpy->dpy, NULL, NULL, NULL, IMInstantiateCallback, NULL);
2075 for (i = 0; i < MAX_TERM_DATA; i++) {
2076 infowin *iwin = data[i].win;
2079 if (iwin->xic_mask) {
2080 XSelectInput(Metadpy->dpy, iwin->win, iwin->mask);
2081 iwin->xic_mask = 0L;
2086 Metadpy->xim = NULL;
2090 static char force_lower(char a) { return ((isupper((a))) ? tolower((a)) : (a)); }
2093 * Initialize a term_data
2095 static errr term_data_init(term_data *td, int i)
2097 term_type *t = &td->t;
2099 concptr name = angband_term_name[i];
2129 sprintf(buf, "ANGBAND_X11_FONT_%d", i);
2132 font = getenv("ANGBAND_X11_FONT");
2137 font = DEFAULT_X11_FONT_0;
2140 font = DEFAULT_X11_FONT_1;
2143 font = DEFAULT_X11_FONT_2;
2146 font = DEFAULT_X11_FONT_3;
2149 font = DEFAULT_X11_FONT_4;
2152 font = DEFAULT_X11_FONT_5;
2155 font = DEFAULT_X11_FONT_6;
2158 font = DEFAULT_X11_FONT_7;
2161 font = DEFAULT_X11_FONT;
2166 sprintf(buf, "ANGBAND_X11_AT_X_%d", i);
2168 x = (str != NULL) ? atoi(str) : -1;
2170 sprintf(buf, "ANGBAND_X11_AT_Y_%d", i);
2172 y = (str != NULL) ? atoi(str) : -1;
2174 sprintf(buf, "ANGBAND_X11_COLS_%d", i);
2176 val = (str != NULL) ? atoi(str) : -1;
2180 sprintf(buf, "ANGBAND_X11_ROWS_%d", i);
2182 val = (str != NULL) ? atoi(str) : -1;
2193 sprintf(buf, "ANGBAND_X11_IBOX_%d", i);
2195 val = (str != NULL) ? atoi(str) : -1;
2199 sprintf(buf, "ANGBAND_X11_IBOY_%d", i);
2201 val = (str != NULL) ? atoi(str) : -1;
2205 MAKE(td->fnt, infofnt);
2206 Infofnt_set(td->fnt);
2207 Infofnt_init_data(font);
2209 num = ((i == 0) ? 1024 : 16);
2210 wid = cols * td->fnt->wid + (ox + ox);
2211 hgt = rows * td->fnt->hgt + (oy + oy);
2212 MAKE(td->win, infowin);
2213 Infowin_set(td->win);
2214 Infowin_init_top(x, y, wid, hgt, 0, Metadpy->fg, Metadpy->bg);
2216 #if defined(USE_XIM)
2217 Infowin_set_mask(ExposureMask | StructureNotifyMask | KeyPressMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask);
2219 Infowin_set_mask(ExposureMask | StructureNotifyMask | KeyPressMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask);
2222 Infowin_set_name(name);
2225 ch = XAllocClassHint();
2228 quit("XAllocClassHint failed");
2230 strcpy(res_name, name);
2231 res_name[0] = force_lower(res_name[0]);
2232 ch->res_name = res_name;
2234 strcpy(res_class, "Angband");
2235 ch->res_class = res_class;
2237 XSetClassHint(Metadpy->dpy, Infowin->win, ch);
2238 sh = XAllocSizeHints();
2240 quit("XAllocSizeHints failed");
2243 sh->flags = PMinSize | PMaxSize;
2244 sh->min_width = 80 * td->fnt->wid + (ox + ox);
2245 sh->min_height = 24 * td->fnt->hgt + (oy + oy);
2246 sh->max_width = 255 * td->fnt->wid + (ox + ox);
2247 sh->max_height = 255 * td->fnt->hgt + (oy + oy);
2249 sh->flags = PMinSize | PMaxSize;
2250 sh->min_width = td->fnt->wid + (ox + ox);
2251 sh->min_height = td->fnt->hgt + (oy + oy);
2252 sh->max_width = 256 * td->fnt->wid + (ox + ox);
2253 sh->max_height = 256 * td->fnt->hgt + (oy + oy);
2256 sh->flags |= PResizeInc;
2257 sh->width_inc = td->fnt->wid;
2258 sh->height_inc = td->fnt->hgt;
2259 sh->flags |= PBaseSize;
2260 sh->base_width = (ox + ox);
2261 sh->base_height = (oy + oy);
2262 XSetWMNormalHints(Metadpy->dpy, Infowin->win, sh);
2266 wh = XAllocWMHints();
2268 quit("XAllocWMHints failed");
2269 wh->flags = InputHint;
2271 XSetWMHints(Metadpy->dpy, Infowin->win, wh);
2274 if ((x >= 0) && (y >= 0))
2275 Infowin_impell(x, y);
2277 term_init(t, cols, rows, num);
2278 t->soft_cursor = true;
2279 t->attr_blank = TERM_WHITE;
2280 t->char_blank = ' ';
2281 t->xtra_hook = Term_xtra_x11;
2282 t->curs_hook = Term_curs_x11;
2283 t->bigcurs_hook = Term_bigcurs_x11;
2284 t->wipe_hook = Term_wipe_x11;
2285 t->text_hook = Term_text_x11;
2292 * Initialization function for an "X11" module to Angband
2294 errr init_x11(int argc, char *argv[])
2297 concptr dpy_name = "";
2301 char filename[1024];
2309 for (i = 1; i < argc; i++) {
2310 if (prefix(argv[i], "-d")) {
2311 dpy_name = &argv[i][2];
2316 if (prefix(argv[i], "-s")) {
2317 smoothRescaling = false;
2321 if (prefix(argv[i], "-a")) {
2322 arg_graphics = GRAPHICS_ADAM_BOLT;
2326 if (prefix(argv[i], "-o")) {
2327 arg_graphics = GRAPHICS_ORIGINAL;
2332 if (prefix(argv[i], "-b")) {
2333 arg_bigtile = use_bigtile = true;
2337 if (prefix(argv[i], "-n")) {
2338 num_term = atoi(&argv[i][2]);
2339 if (num_term > MAX_TERM_DATA)
2340 num_term = MAX_TERM_DATA;
2341 else if (num_term < 1)
2346 if (prefix(argv[i], "--")) {
2350 plog_fmt("Ignoring option: %s", argv[i]);
2356 setlocale(LC_ALL, "");
2358 #ifdef DEFAULT_LOCALE
2359 if (!strcmp(setlocale(LC_ALL, NULL), "C")) {
2360 printf("try default locale \"%s\"\n", DEFAULT_LOCALE);
2361 setlocale(LC_ALL, DEFAULT_LOCALE);
2365 if (!strcmp(setlocale(LC_ALL, NULL), "C")) {
2366 printf("WARNING: Locale is not supported. Non-english font may be displayed incorrectly.\n");
2369 if (!XSupportsLocale()) {
2370 printf("can't support locale in X\n");
2371 setlocale(LC_ALL, "C");
2374 setlocale(LC_ALL, "C");
2377 #endif /* USE_LOCALE */
2379 if (Metadpy_init_name(dpy_name))
2382 MAKE(xor_, infoclr);
2384 Infoclr_init_ppn(Metadpy->fg, Metadpy->bg, "xor", 0);
2385 for (i = 0; i < 256; ++i) {
2387 MAKE(clr[i], infoclr);
2388 Infoclr_set(clr[i]);
2389 color_table[i][0] = angband_color_table[i][0];
2390 color_table[i][1] = angband_color_table[i][1];
2391 color_table[i][2] = angband_color_table[i][2];
2392 color_table[i][3] = angband_color_table[i][3];
2393 pixel = ((i == 0) ? Metadpy->bg : Metadpy->fg);
2394 if (Metadpy->color) {
2395 pixel = create_pixel(Metadpy->dpy, color_table[i][1], color_table[i][2], color_table[i][3]);
2398 Infoclr_init_ppn(pixel, Metadpy->bg, "cpy", 0);
2402 for (i = 0; i < num_term; i++) {
2403 term_data *td = &data[i];
2404 term_data_init(td, i);
2405 angband_term[i] = Term;
2408 Infowin_set(data[0].win);
2410 term_activate(&data[0].t);
2415 p = XSetLocaleModifiers("");
2417 p = XSetLocaleModifiers("@im=");
2420 XRegisterIMInstantiateCallback(Metadpy->dpy, NULL, NULL, NULL, IMInstantiateCallback, NULL);
2427 switch (arg_graphics) {
2428 case GRAPHICS_ORIGINAL:
2429 path_build(filename, sizeof(filename), ANGBAND_DIR_XTRA, "graf/8x8.bmp");
2430 if (0 == fd_close(fd_open(filename, O_RDONLY))) {
2431 use_graphics = true;
2432 pict_wid = pict_hgt = 8;
2433 ANGBAND_GRAF = "old";
2436 case GRAPHICS_ADAM_BOLT:
2437 path_build(filename, sizeof(filename), ANGBAND_DIR_XTRA, "graf/16x16.bmp");
2438 if (0 == fd_close(fd_open(filename, O_RDONLY))) {
2439 use_graphics = true;
2440 pict_wid = pict_hgt = 16;
2441 ANGBAND_GRAF = "new";
2447 Display *dpy = Metadpy->dpy;
2449 tiles_raw = ReadBMP(dpy, filename);
2450 for (i = 0; i < num_term; i++) {
2451 term_data *td = &data[i];
2452 term_type *t = &td->t;
2453 t->pict_hook = Term_pict_x11;
2454 t->higher_pict = true;
2455 td->tiles = ResizeImage(dpy, tiles_raw, pict_wid, pict_hgt, td->fnt->twid, td->fnt->hgt);
2458 for (i = 0; i < num_term; i++) {
2459 term_data *td = &data[i];
2461 int depth = DefaultDepth(dpy, DefaultScreen(dpy));
2462 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
2465 jj = (depth - 1) >> 2;
2468 total = td->fnt->twid * td->fnt->hgt * ii;
2469 TmpData = (char *)malloc(total);
2470 td->TmpImage = XCreateImage(dpy, visual, depth, ZPixmap, 0, TmpData, td->fnt->twid, td->fnt->hgt, 8, 0);
2473 #endif /* ! USE_XFT */
2477 #endif /* USE_X11 */