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 "game-option/runtime-arguments.h"
97 #include "game-option/special-options.h"
98 #include "io/files-util.h"
99 #include "main/sound-definitions-table.h"
100 #include "system/system-variables.h"
101 #include "term/gameterm.h"
102 #include "term/term-color-types.h"
105 * Available graphic modes
107 #define GRAPHICS_NONE 0
108 #define GRAPHICS_ORIGINAL 1
109 #define GRAPHICS_ADAM_BOLT 2
110 #define GRAPHICS_HENGBAND 3
112 #ifndef __MAKEDEPEND__
113 #include <X11/Xlib.h>
114 #include <X11/Xutil.h>
115 #include <X11/keysym.h>
116 #include <X11/keysymdef.h>
118 #include <X11/Xlocale.h>
120 #include <X11/Xatom.h>
121 #endif /* __MAKEDEPEND__ */
125 #include <X11/Xft/Xft.h>
129 * Include some helpful X11 code.
131 #include "maid-x11.c"
137 * 1) On a monochrome (or "fake-monochrome") display, all colors
138 * will be "cast" to "fg," except for the bg color, which is,
139 * obviously, cast to "bg". Thus, one can ignore this setting.
141 * 2) Because of the inner functioning of the color allocation
142 * routines, colors may be specified as (a) a typical color name,
143 * (b) a hexidecimal color specification (preceded by a pound sign),
144 * or (c) by strings such as "fg", "bg", "zg".
146 * 3) Due to the workings of the init routines, many colors
147 * may also be dealt with by their actual pixel values. Note that
148 * the pixel with all bits set is "zg = (1<<metadpy->depth)-1", which
149 * is not necessarily either black or white.
152 /**** Generic Types ****/
155 * An X11 pixell specifier
158 typedef XftColor Pixell;
160 typedef unsigned long Pixell;
164 * The structures defined below
166 typedef struct metadpy metadpy;
167 typedef struct infowin infowin;
168 typedef struct infoclr infoclr;
169 typedef struct infofnt infofnt;
172 * A structure summarizing a given Display.
174 * - The Display itself
175 * - The default Screen for the display
176 * - The virtual root (usually just the root)
177 * - The default colormap (from a macro)
179 * - The "name" of the display
181 * - The socket to listen to for events
183 * - The width of the display screen (from a macro)
184 * - The height of the display screen (from a macro)
185 * - The bit depth of the display screen (from a macro)
187 * - The black Pixell (from a macro)
188 * - The white Pixell (from a macro)
190 * - The background Pixell (default: black)
191 * - The foreground Pixell (default: white)
192 * - The maximal Pixell (Equals: ((2 ^ depth)-1), is usually ugly)
194 * - Bit Flag: Force all colors to black and white (default: !color)
195 * - Bit Flag: Allow the use of color (default: depth > 1)
196 * - Bit Flag: We created 'dpy', and so should nuke it when done.
231 * A Structure summarizing Window Information.
233 * I assume that a window is at most 30000 pixels on a side.
234 * I assume that the root windw is also at most 30000 square.
237 * - The current Input Event Mask
239 * - The location of the window
240 * - The width, height of the window
241 * - The border width of this window
243 * - Byte: 1st Extra byte
245 * - Bit Flag: This window is currently Mapped
246 * - Bit Flag: This window needs to be redrawn
247 * - Bit Flag: This window has been resized
249 * - Bit Flag: We should nuke 'win' when done with it
251 * - Bit Flag: 1st extra flag
252 * - Bit Flag: 2nd extra flag
253 * - Bit Flag: 3rd extra flag
254 * - Bit Flag: 4th extra flag
290 * A Structure summarizing Operation+Color Information
292 * - The actual GC corresponding to this info
294 * - The Foreground Pixell Value
295 * - The Background Pixell Value
297 * - Num (0-15): The operation code (As in Clear, Xor, etc)
298 * - Bit Flag: The GC is in stipple mode
299 * - Bit Flag: Destroy 'gc' at Nuke time.
316 * A Structure to Hold Font Information
318 * - The 'XFontStruct*' (yields the 'Font')
322 * - The default character width
323 * - The default character height
324 * - The default character ascent
326 * - Byte: Pixel offset used during fake mono
328 * - Flag: Force monospacing via 'wid'
329 * - Flag: Nuke info when done
351 /* Set current metadpy (Metadpy) to 'M' */
352 #define Metadpy_set(M) \
355 /* Initialize 'M' using Display 'D' */
356 #define Metadpy_init_dpy(D) \
357 Metadpy_init_2(D,cNULL)
359 /* Initialize 'M' using a Display named 'N' */
360 #define Metadpy_init_name(N) \
361 Metadpy_init_2((Display*)(NULL),N)
363 /* Initialize 'M' using the standard Display */
364 #define Metadpy_init() \
365 Metadpy_init_name("")
367 /* Init an infowin by giving father as an (info_win*) (or NULL), and data */
368 #define Infowin_init_dad(D,X,Y,W,H,B,FG,BG) \
369 Infowin_init_data(((D) ? ((D)->win) : (Window)(None)), \
372 /* Init a top level infowin by pos,size,bord,Colors */
373 #define Infowin_init_top(X,Y,W,H,B,FG,BG) \
374 Infowin_init_data(None,X,Y,W,H,B,FG,BG)
376 /* Request a new standard window by giving Dad infowin and X,Y,W,H */
377 #define Infowin_init_std(D,X,Y,W,H,B) \
378 Infowin_init_dad(D,X,Y,W,H,B,Metadpy->fg,Metadpy->bg)
380 /* Set the current Infowin */
381 #define Infowin_set(I) \
384 /* Set the current Infoclr */
385 #define Infoclr_set(C) \
388 #define Infoclr_init_ppo(F,B,O,M) \
389 Infoclr_init_data(F,B,O,M)
391 #define Infoclr_init_cco(F,B,O,M) \
392 Infoclr_init_ppo(Infoclr_Pixell(F),Infoclr_Pixell(B),O,M)
394 #define Infoclr_init_ppn(F,B,O,M) \
395 Infoclr_init_ppo(F,B,Infoclr_Opcode(O),M)
397 #define Infoclr_init_ccn(F,B,O,M) \
398 Infoclr_init_cco(F,B,Infoclr_Opcode(O),M)
400 /* Set the current infofnt */
401 #define Infofnt_set(I) \
404 /* Errr: Expose Infowin */
405 #define Infowin_expose() \
406 (!(Infowin->redraw = 1))
408 /* Errr: Unxpose Infowin */
409 #define Infowin_unexpose() \
410 (Infowin->redraw = 0)
413 * The "default" values
415 static metadpy metadpy_default;
418 * The "current" variables
420 static metadpy *Metadpy = &metadpy_default;
421 static infowin *Infowin = (infowin*)(NULL);
423 static infowin *Focuswin = (infowin*)(NULL);
425 static infoclr *Infoclr = (infoclr*)(NULL);
426 static infofnt *Infofnt = (infofnt*)(NULL);
429 * Init the current metadpy, with various initialization stuff.
432 * dpy: The Display* to use (if NULL, create it)
433 * name: The name of the Display (if NULL, the current)
436 * If 'name' is NULL, but 'dpy' is set, extract name from dpy
437 * If 'dpy' is NULL, then Create the named Display
438 * If 'name' is NULL, and so is 'dpy', use current Display
440 * Return -1 if no Display given, and none can be opened.
442 static errr Metadpy_init_2(Display *dpy, concptr name)
444 metadpy *m = Metadpy;
447 dpy = XOpenDisplay(name);
448 if (!dpy) return (-1);
458 m->screen = DefaultScreenOfDisplay(dpy);
459 m->root = RootWindowOfScreen(m->screen);
460 m->cmap = DefaultColormapOfScreen(m->screen);
461 m->name = DisplayString(dpy);
462 m->fd = ConnectionNumber(Metadpy->dpy);
463 m->width = WidthOfScreen(m->screen);
464 m->height = HeightOfScreen(m->screen);
465 m->depth = DefaultDepthOfScreen(m->screen);
468 Visual *vis = DefaultVisual(dpy, 0);
469 XftColorAllocName(dpy, vis, m->cmap, "black", &m->black);
470 XftColorAllocName(dpy, vis, m->cmap, "white", &m->white);
472 m->black = BlackPixelOfScreen(m->screen);
473 m->white = WhitePixelOfScreen(m->screen);
480 m->zg = (1 << m->depth) - 1;
483 m->color = ((m->depth > 1) ? 1 : 0);
484 m->mono = ((m->color) ? 0 : 1);
489 * General Flush/ Sync/ Discard routine
491 static errr Metadpy_update(int flush, int sync, int discard)
493 if (flush) XFlush(Metadpy->dpy);
494 if (sync) XSync(Metadpy->dpy, discard);
503 static errr Metadpy_do_beep(void)
505 XBell(Metadpy->dpy, 100);
510 * Set the name (in the title bar) of Infowin
512 static errr Infowin_set_name(concptr name)
519 st = XStringListToTextProperty(&bp, 1, &tp);
520 if (st) XSetWMName(Metadpy->dpy, Infowin->win, &tp);
525 * Prepare a new 'infowin'.
527 static errr Infowin_prepare(Window xid)
529 infowin *iwin = Infowin;
531 XWindowAttributes xwa;
533 unsigned int w, h, b, d;
535 XGetGeometry(Metadpy->dpy, xid, &tmp_win, &x, &y, &w, &h, &b, &d);
538 Visual *vis = DefaultVisual(Metadpy->dpy, 0);
539 if (vis->class != TrueColor) {
540 quit_fmt("Display does not support truecolor.\n");
542 iwin->draw = XftDrawCreate(Metadpy->dpy, iwin->win, vis, Metadpy->cmap);
551 XGetWindowAttributes(Metadpy->dpy, xid, &xwa);
552 iwin->mask = xwa.your_event_mask;
553 iwin->mapped = ((xwa.map_state == IsUnmapped) ? 0 : 1);
559 * Init an infowin by giving some data.
562 * dad: The Window that should own this Window (if any)
563 * x,y: The position of this Window
564 * w,h: The size of this Window
565 * b,d: The border width and pixel depth
568 * If 'dad == None' assume 'dad == root'
570 static errr Infowin_init_data(Window dad, int x, int y, int w, int h,
571 int b, Pixell fg, Pixell bg)
574 (void)WIPE(Infowin, infowin);
575 if (dad == None) dad = Metadpy->root;
578 xid = XCreateSimpleWindow(Metadpy->dpy, dad, x, y, w, h, b, fg.pixel, bg.pixel);
580 xid = XCreateSimpleWindow(Metadpy->dpy, dad, x, y, w, h, b, fg, bg);
583 XSelectInput(Metadpy->dpy, xid, 0L);
585 return (Infowin_prepare(xid));
589 * Modify the event mask of an Infowin
591 static errr Infowin_set_mask(long mask)
593 Infowin->mask = mask;
594 XSelectInput(Metadpy->dpy, Infowin->win, Infowin->mask);
599 * Request that Infowin be mapped
601 static errr Infowin_map(void)
603 XMapWindow(Metadpy->dpy, Infowin->win);
608 * Request that Infowin be raised
610 static errr Infowin_raise(void)
612 XRaiseWindow(Metadpy->dpy, Infowin->win);
617 * Request that Infowin be moved to a new location
619 static errr Infowin_impell(int x, int y)
621 XMoveWindow(Metadpy->dpy, Infowin->win, x, y);
628 static errr Infowin_resize(int w, int h)
630 XResizeWindow(Metadpy->dpy, Infowin->win, w, h);
635 * Visually clear Infowin
637 static errr Infowin_wipe(void)
639 XClearWindow(Metadpy->dpy, Infowin->win);
644 * A NULL terminated pair list of legal "operation names"
646 * Pairs of values, first is texttual name, second is the string
647 * holding the decimal value that the operation corresponds to.
649 static concptr opcode_pairs[] =
668 "+copyInverted", "12",
675 * Parse a word into an operation "code"
678 * str: A string, hopefully representing an Operation
681 * 0-15: if 'str' is a valid Operation
682 * -1: if 'str' could not be parsed
684 static int Infoclr_Opcode(concptr str)
687 for (i = 0; opcode_pairs[i*2]; ++i)
689 if (streq(opcode_pairs[i*2], str))
691 return (atoi(opcode_pairs[i*2+1]));
699 * Initialize an infoclr with some data
702 * fg: The Pixell for the requested Foreground (see above)
703 * bg: The Pixell for the requested Background (see above)
704 * op: The Opcode for the requested Operation (see above)
705 * stip: The stipple mode
707 static errr Infoclr_init_data(Pixell fg, Pixell bg, int op, int stip)
709 infoclr *iclr = Infoclr;
714 unsigned long gc_mask;
718 if (bg > Metadpy->zg) return (-1);
719 if (fg > Metadpy->zg) return (-1);
720 if ((op < 0) || (op > 15)) return (-1);
725 if (op == 6) gcv.background = 0;
726 if (op == 6) gcv.foreground = (bg ^ fg);
728 gcv.fill_style = (stip ? FillStippled : FillSolid);
729 gcv.graphics_exposures = False;
730 gc_mask = (GCFunction | GCBackground | GCForeground | GCFillStyle | GCGraphicsExposures);
731 gc = XCreateGC(Metadpy->dpy, Metadpy->root, gc_mask, &gcv);
734 (void)WIPE(iclr, infoclr);
744 iclr->stip = stip ? 1 : 0;
749 * Change the 'fg' for an infoclr
752 * fg: The Pixell for the requested Foreground (see above)
754 static errr Infoclr_change_fg(Pixell fg)
756 infoclr *iclr = Infoclr;
761 if (fg > Metadpy->zg) return (-1);
763 XSetForeground(Metadpy->dpy, iclr->gc, fg);
770 * Prepare a new 'infofnt'
773 static errr Infofnt_prepare(XftFont *info)
775 static errr Infofnt_prepare(XFontSet info)
778 infofnt *ifnt = Infofnt;
782 XFontStruct **fontinfo;
785 int ascent, descent, width;
792 ifnt->asc = info->ascent;
793 ifnt->hgt = info->ascent + info->descent;
794 const char *text = "A";
796 XftTextExtentsUtf8(Metadpy->dpy, info, (FcChar8*)text, strlen(text), &extent);
797 ifnt->wid = extent.xOff;
799 n_fonts = XFontsOfFontSet(info, &fontinfo, &fontname);
801 ascent = descent = width = 0;
802 while(n_fonts-- > 0){
803 cs = &((*fontinfo)->max_bounds);
804 if(ascent < (*fontinfo)->ascent) ascent = (*fontinfo)->ascent;
805 if(descent < (*fontinfo)->descent) descent = (*fontinfo)->descent;
806 if(((*fontinfo)->max_byte1) > 0){
807 /* 多バイト文字の場合は幅半分(端数切り上げ)で評価する */
808 if(width < (cs->width+1)/2) width = (cs->width+1)/2;
810 if(width < cs->width) width = cs->width;
816 ifnt->hgt = ascent + descent;
821 ifnt->twid = 2 * ifnt->wid;
823 ifnt->twid = ifnt->wid;
829 * Init an infofnt by its Name
832 * name: The name of the requested Font
834 static void Infofnt_init_data(concptr name)
846 if (!name || !*name) quit("Missing font!");
849 info = XftFontOpenName(Metadpy->dpy, 0, name);
850 /* TODO: error handling */
852 info = XCreateFontSet(Metadpy->dpy, name, &missing_list, &missing_count, &default_font);
853 if(missing_count > 0){
854 printf("missing font(s): \n");
855 while(missing_count-- > 0){
856 printf("\t%s\n", missing_list[missing_count]);
858 XFreeStringList(missing_list);
862 if (!info) quit_fmt("Failed to find font:\"%s\"", name);
864 (void)WIPE(Infofnt, infofnt);
865 if (Infofnt_prepare(info))
868 XftFontClose(Metadpy->dpy, info);
870 XFreeFontSet(Metadpy->dpy, info);
872 quit_fmt("Failed to prepare font:\"%s\"", name);
875 Infofnt->name = string_make(name);
882 static errr Infofnt_text_std(int x, int y, concptr str, int len)
884 if (!str || !*str) return (-1);
886 if (len < 0) len = strlen(str);
888 y = (y * Infofnt->hgt) + Infofnt->asc + Infowin->oy;
889 x = (x * Infofnt->wid) + Infowin->ox;
894 for (i = 0; i < len; ++i)
896 XDrawImageString(Metadpy->dpy, Infowin->win, Infoclr->gc, x + i * Infofnt->wid + Infofnt->off, y, str + i, 1);
902 iconv_t cd = iconv_open("UTF-8", "EUC-JP");
904 size_t outlen = len * 2;
905 char *kanji = malloc(outlen);
906 char *sp; char *kp = kanji;
908 angband_strcpy(sbuf, str, sizeof(sbuf));
910 iconv(cd, &sp, &inlen, &kp, &outlen);
914 XftDraw* draw = Infowin->draw;
919 r.width = Infofnt->wid*len;
920 r.height = Infofnt->hgt;
921 XftDrawSetClipRectangles(draw, x, y-Infofnt->asc, &r, 1);
922 XftDrawRect(draw, &Infoclr->bg, x, y-Infofnt->asc, Infofnt->wid*len, Infofnt->hgt);
923 XftDrawStringUtf8(draw, &Infoclr->fg, Infofnt->info, x, y,
924 (FcChar8*)kanji, kp - kanji);
925 XftDrawSetClip(draw, 0);
927 XmbDrawImageString(Metadpy->dpy, Infowin->win, Infofnt->info,
928 Infoclr->gc, x, y, kanji, kp-kanji);
937 * Painting where text would be
939 static errr Infofnt_text_non(int x, int y, concptr str, int len)
942 if (len < 0) len = strlen(str);
944 w = len * Infofnt->wid;
945 x = x * Infofnt->wid + Infowin->ox;
947 y = y * h + Infowin->oy;
950 XftDrawRect(Infowin->draw, &Infoclr->fg, x, y, w, h);
952 XFillRectangle(Metadpy->dpy, Infowin->win, Infoclr->gc, x, y, w, h);
959 * Angband specific code follows... (ANGBAND)
963 * Hack -- cursor color
970 static infoclr *clr[256];
973 * Color info (unused, red, green, blue).
975 static byte color_table[256][4];
980 typedef struct term_data term_data;
983 * A structure for each "term"
997 * The number of term data structures
999 #define MAX_TERM_DATA 8
1002 * The array of term data structures
1004 static term_data data[MAX_TERM_DATA];
1007 /* Use short names for the most commonly used elements of various structures. */
1008 #define DPY (Metadpy->dpy)
1009 #define WIN (Infowin->win)
1011 /* Describe a set of co-ordinates. */
1012 typedef struct co_ord co_ord;
1020 * A special structure to store information about the text currently
1023 typedef struct x11_selection_type x11_selection_type;
1024 struct x11_selection_type
1026 bool select; /* The selection is currently in use. */
1027 bool drawn; /* The selection is currently displayed. */
1028 term *t; /* The window where the selection is found. */
1029 co_ord init; /* The starting co-ordinates. */
1030 co_ord cur; /* The end co-ordinates (the current ones if still copying). */
1031 co_ord old; /* The previous end co-ordinates. */
1032 Time time; /* The time at which the selection was finalised. */
1035 static x11_selection_type s_ptr[1];
1038 * Process a keypress event
1040 * Also appears in "main-xaw.c".
1042 static void react_keypress(XKeyEvent *xev)
1044 int i, n, mc, ms, mo, mx;
1046 XKeyEvent *ev = (XKeyEvent*)(xev);
1052 int valid_keysym = TRUE;
1056 if(Focuswin && Focuswin->xic){
1058 n = XmbLookupString(Focuswin->xic, ev, buf, 125, &ks, &status);
1059 if(status == XBufferOverflow){
1060 printf("Input is too long, and dropped\n");
1063 if(status != XLookupKeySym && status != XLookupBoth){
1064 valid_keysym = FALSE;
1067 n = XLookupString(ev, buf, 125, &ks, NULL);
1070 n = XLookupString(ev, buf, 125, &ks, NULL);
1077 for (i = 0; buf[i]; i++) Term_keypress(buf[i]);
1083 if (IsModifierKey(ks)) return;
1086 mc = (ev->state & ControlMask) ? TRUE : FALSE;
1087 ms = (ev->state & ShiftMask) ? TRUE : FALSE;
1088 mo = (ev->state & Mod1Mask) ? TRUE : FALSE;
1089 mx = (ev->state & Mod2Mask) ? TRUE : FALSE;
1090 if (n && !mo && !mx && !IsSpecialKey(ks))
1092 for (i = 0; buf[i]; i++) Term_keypress(buf[i]);
1101 Term_keypress(ESCAPE);
1107 Term_keypress('\r');
1113 Term_keypress('\t');
1119 Term_keypress(0x7f);
1124 Term_keypress('\010');
1131 sprintf(msg, "%c%s%s%s%s_%lX%c", 31,
1132 mc ? "N" : "", ms ? "S" : "",
1133 mo ? "O" : "", mx ? "M" : "",
1134 (unsigned long)(ks), 13);
1138 sprintf(msg, "%c%s%s%s%sK_%X%c", 31,
1139 mc ? "N" : "", ms ? "S" : "",
1140 mo ? "O" : "", mx ? "M" : "",
1144 for (i = 0; msg[i]; i++) Term_keypress(msg[i]);
1146 if (n && (macro_find_exact(msg) < 0))
1148 macro_add(msg, buf);
1153 * Find the square a particular pixel is part of.
1155 static void pixel_to_square(int * const x, int * const y,
1156 const int ox, const int oy)
1158 (*x) = (ox - Infowin->ox) / Infofnt->wid;
1159 (*y) = (oy - Infowin->oy) / Infofnt->hgt;
1163 * Find the pixel at the top-left corner of a square.
1165 static void square_to_pixel(int * const x, int * const y,
1166 const int ox, const int oy)
1168 (*x) = ox * Infofnt->wid + Infowin->ox;
1169 (*y) = oy * Infofnt->hgt + Infowin->oy;
1173 * Convert co-ordinates from starting corner/opposite corner to minimum/maximum.
1175 static void sort_co_ord(co_ord *min, co_ord *max,
1176 const co_ord *b, const co_ord *a)
1178 min->x = MIN(a->x, b->x);
1179 min->y = MIN(a->y, b->y);
1180 max->x = MAX(a->x, b->x);
1181 max->y = MAX(a->y, b->y);
1185 * Remove the selection by redrawing it.
1187 static void mark_selection_clear(int x1, int y1, int x2, int y2)
1189 Term_redraw_section(x1,y1,x2,y2);
1193 * Select an area by drawing a grey box around it.
1194 * NB. These two functions can cause flicker as the selection is modified,
1195 * as the game redraws the entire marked section.
1197 static void mark_selection_mark(int x1, int y1, int x2, int y2)
1199 square_to_pixel(&x1, &y1, x1, y1);
1200 square_to_pixel(&x2, &y2, x2, y2);
1202 XftDrawRect(Infowin->draw, &clr[2]->fg, x1, y1, x2-x1+Infofnt->wid - 1, y2-y1+Infofnt->hgt - 1);
1204 XDrawRectangle(Metadpy->dpy, Infowin->win, clr[2]->gc, x1, y1,
1205 x2-x1+Infofnt->wid - 1, y2-y1+Infofnt->hgt - 1);
1210 * Mark a selection by drawing boxes around it (for now).
1212 static void mark_selection(void)
1216 bool draw = s_ptr->select;
1217 bool clear = s_ptr->drawn;
1218 if (s_ptr->t != old) Term_activate(s_ptr->t);
1222 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->old);
1223 mark_selection_clear(min.x, min.y, max.x, max.y);
1227 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->cur);
1228 mark_selection_mark(min.x, min.y, max.x, max.y);
1231 if (s_ptr->t != old) Term_activate(old);
1233 s_ptr->old.x = s_ptr->cur.x;
1234 s_ptr->old.y = s_ptr->cur.y;
1235 s_ptr->drawn = s_ptr->select;
1239 * Forget a selection for one reason or another.
1241 static void copy_x11_release(void)
1243 s_ptr->select = FALSE;
1248 * Start to select some text on the screen.
1250 static void copy_x11_start(int x, int y)
1252 if (s_ptr->select) copy_x11_release();
1255 s_ptr->init.x = s_ptr->cur.x = s_ptr->old.x = x;
1256 s_ptr->init.y = s_ptr->cur.y = s_ptr->old.y = y;
1260 * Respond to movement of the mouse when selecting text.
1262 static void copy_x11_cont(int x, int y, unsigned int buttons)
1264 x = MIN(MAX(x, 0), Term->wid-1);
1265 y = MIN(MAX(y, 0), Term->hgt-1);
1266 if (~buttons & Button1Mask) return;
1267 if (s_ptr->t != Term) return;
1268 if (x == s_ptr->old.x && y == s_ptr->old.y && s_ptr->select) return;
1270 s_ptr->select = TRUE;
1277 * Respond to release of the left mouse button by putting the selected text in
1278 * the primary buffer.
1280 static void copy_x11_end(const Time time)
1282 if (!s_ptr->select) return;
1283 if (s_ptr->t != Term) return;
1286 XSetSelectionOwner(Metadpy->dpy, XA_PRIMARY, Infowin->win, time);
1287 if (XGetSelectionOwner(Metadpy->dpy, XA_PRIMARY) != Infowin->win)
1289 s_ptr->select = FALSE;
1294 static Atom xa_targets, xa_timestamp, xa_text, xa_compound_text;
1297 * Set the required variable atoms at start-up to avoid errors later.
1299 static void set_atoms(void)
1301 xa_targets = XInternAtom(DPY, "TARGETS", False);
1302 xa_timestamp = XInternAtom(DPY, "TIMESTAMP", False);
1303 xa_text = XInternAtom(DPY, "TEXT", False);
1304 xa_compound_text = XInternAtom(DPY, "COMPOUND_TEXT", False);
1307 static Atom request_target = 0;
1310 * Send a message to request that the PRIMARY buffer be sent here.
1312 static void paste_x11_request(Atom target, const Time time)
1314 Atom property = XInternAtom(DPY, "__COPY_TEXT", False);
1315 if (XGetSelectionOwner(DPY, XA_PRIMARY) == None)
1318 /* bell("No selection found."); */
1322 request_target = target;
1323 XConvertSelection(DPY, XA_PRIMARY, target, property, WIN, time);
1327 * Add the contents of the PRIMARY buffer to the input queue.
1329 * Hack - This doesn't use the "time" of the event, and so accepts anything a
1330 * client tries to send it.
1332 static void paste_x11_accept(const XSelectionEvent *ptr)
1335 const long offset = 0;
1336 const long length = 32000;
1337 XTextProperty xtextproperty;
1339 Atom property = XInternAtom(DPY, "__COPY_TEXT", False);
1340 if (ptr->property == None)
1342 if (request_target == xa_compound_text)
1344 paste_x11_request(XA_STRING, ptr->time);
1349 plog("Paste failure (remote client could not send).");
1355 if (ptr->selection != XA_PRIMARY)
1357 plog("Paste failure (remote client did not send primary selection).");
1361 if (ptr->target != request_target)
1363 plog("Paste failure (selection in unknown format).");
1367 if (XGetWindowProperty(Metadpy->dpy, Infowin->win, property, offset,
1368 length, TRUE, request_target,
1369 &xtextproperty.encoding,
1370 &xtextproperty.format,
1371 &xtextproperty.nitems,
1373 &xtextproperty.value)
1379 if (request_target == xa_compound_text)
1384 XmbTextPropertyToTextList(DPY, &xtextproperty, &list, &count);
1390 for (i = 0; i < count; i++)
1392 err = type_string(list[i], 0);
1396 XFreeStringList(list);
1401 err = type_string((char *)xtextproperty.value, xtextproperty.nitems);
1404 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)
1428 if (rq->time < s_ptr->time) return FALSE;
1430 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->cur);
1431 if (XGetSelectionOwner(DPY, XA_PRIMARY) != WIN)
1436 XDeleteProperty(DPY, rq->requestor, rq->property);
1438 for (n = 0, y = 0; y < Term->hgt; y++)
1443 if (y < min.y) continue;
1444 if (y > max.y) break;
1446 for (l = 0, x = 0; x < Term->wid; x++)
1449 if (x > max.x) break;
1451 Term_what(x, y, &a, &c);
1452 if (1 == kanji) kanji = 2;
1453 else if (iskanji(c)) kanji = 1;
1456 if (x < min.x) continue;
1459 * A single kanji character was divided in two...
1460 * Delete the garbage.
1462 if ((2 == kanji && x == min.x) ||
1463 (1 == kanji && x == max.x))
1466 if (x > max.x) break;
1467 if (x < min.x) continue;
1469 Term_what(x, y, &a, &c);
1476 while (buf[l-1] == ' ') l--;
1485 list[n++] = (char *)string_make(buf);
1489 if (rq->target == XA_STRING)
1491 for (n = 0; list[n]; n++)
1493 XChangeProperty(DPY, rq->requestor, rq->property, rq->target, 8,
1494 PropModeAppend, (unsigned char *)list[n], strlen(list[n]));
1497 else if (rq->target == xa_text ||
1498 rq->target == xa_compound_text)
1500 XTextProperty text_prop;
1501 XICCEncodingStyle style;
1503 if (rq->target == xa_text)
1504 style = XStdICCTextStyle;
1506 style = XCompoundTextStyle;
1509 XmbTextListToTextProperty(DPY, list, n, style, &text_prop))
1511 XChangeProperty(DPY,
1519 XFree(text_prop.value);
1523 for (n = 0; list[n]; n++)
1525 string_free(list[n]);
1532 * Send some text requested by another X client.
1534 static void paste_x11_send(XSelectionRequestEvent *rq)
1537 XSelectionEvent *ptr = &(event.xselection);
1539 ptr->type = SelectionNotify;
1540 ptr->property = rq->property;
1541 ptr->display = rq->display;
1542 ptr->requestor = rq->requestor;
1543 ptr->selection = rq->selection;
1544 ptr->target = rq->target;
1545 ptr->time = rq->time;
1548 * Paste the appropriate information for each target type.
1549 * Note that this currently rejects MULTIPLE targets.
1552 if (rq->target == XA_STRING ||
1553 rq->target == xa_text ||
1554 rq->target == xa_compound_text)
1556 if (!paste_x11_send_text(rq))
1557 ptr->property = None;
1559 else if (rq->target == xa_targets)
1561 Atom target_list[4];
1562 target_list[0] = XA_STRING;
1563 target_list[1] = xa_text;
1564 target_list[2] = xa_compound_text;
1565 target_list[3] = xa_targets;
1566 XChangeProperty(DPY, rq->requestor, rq->property, rq->target,
1567 (8 * sizeof(target_list[0])), PropModeReplace,
1568 (unsigned char *)target_list,
1569 (sizeof(target_list) / sizeof(target_list[0])));
1571 else if (rq->target == xa_timestamp)
1573 XChangeProperty(DPY, rq->requestor, rq->property, rq->target,
1574 (8 * sizeof(Time)), PropModeReplace,
1575 (unsigned char *)s_ptr->time, 1);
1579 ptr->property = None;
1582 XSendEvent(DPY, rq->requestor, FALSE, NoEventMask, &event);
1586 * Handle various events conditional on presses of a mouse button.
1588 static void handle_button(Time time, int x, int y, int button, bool press)
1590 pixel_to_square(&x, &y, x, y);
1592 if (press && button == 1) copy_x11_start(x, y);
1593 if (!press && button == 1) copy_x11_end(time);
1594 if (!press && button == 2) paste_x11_request(xa_compound_text, time);
1600 static errr CheckEvent(bool wait)
1602 term_data *old_td = (term_data*)(Term->data);
1604 XEvent xev_body, *xev = &xev_body;
1606 term_data *td = NULL;
1607 infowin *iwin = NULL;
1616 if (!wait && !XPending(Metadpy->dpy)) return (1);
1618 if (s_ptr->select && !s_ptr->drawn) mark_selection();
1620 XNextEvent(Metadpy->dpy, xev);
1623 } while (XFilterEvent(xev, xev->xany.window));
1626 if (xev->type == MappingNotify)
1628 XRefreshKeyboardMapping(&xev->xmapping);
1632 for (i = 0; i < MAX_TERM_DATA; i++)
1634 if (!data[i].win) continue;
1635 if (xev->xany.window == data[i].win->win)
1643 if (!td || !iwin) return (0);
1645 Term_activate(&td->t);
1652 bool press = (xev->type == ButtonPress);
1653 int x = xev->xbutton.x;
1654 int y = xev->xbutton.y;
1656 if (xev->xbutton.button == Button1) z = 1;
1657 else if (xev->xbutton.button == Button2) z = 2;
1658 else if (xev->xbutton.button == Button3) z = 3;
1659 else if (xev->xbutton.button == Button4) z = 4;
1660 else if (xev->xbutton.button == Button5) z = 5;
1663 handle_button(xev->xbutton.time, x, y, z, press);
1673 int x = xev->xmotion.x;
1674 int y = xev->xmotion.y;
1675 unsigned int z = xev->xmotion.state;
1676 pixel_to_square(&x, &y, x, y);
1677 copy_x11_cont(x, y, z);
1680 case SelectionNotify:
1682 paste_x11_accept(&(xev->xselection));
1685 case SelectionRequest:
1687 paste_x11_send(&(xev->xselectionrequest));
1690 case SelectionClear:
1692 s_ptr->select = FALSE;
1702 Term_activate(&old_td->t);
1703 react_keypress(&(xev->xkey));
1709 x1 = (xev->xexpose.x - Infowin->ox)/Infofnt->wid;
1710 x2 = (xev->xexpose.x + xev->xexpose.width -
1711 Infowin->ox)/Infofnt->wid;
1713 y1 = (xev->xexpose.y - Infowin->oy)/Infofnt->hgt;
1714 y2 = (xev->xexpose.y + xev->xexpose.height -
1715 Infowin->oy)/Infofnt->hgt;
1717 Term_redraw_section(x1, y1, x2, y2);
1722 Infowin->mapped = 1;
1723 Term->mapped_flag = TRUE;
1728 Infowin->mapped = 0;
1729 Term->mapped_flag = FALSE;
1732 case ConfigureNotify:
1734 int cols, rows, wid, hgt;
1735 int ox = Infowin->ox;
1736 int oy = Infowin->oy;
1737 Infowin->x = xev->xconfigure.x;
1738 Infowin->y = xev->xconfigure.y;
1739 Infowin->w = xev->xconfigure.width;
1740 Infowin->h = xev->xconfigure.height;
1741 cols = ((Infowin->w - (ox + ox)) / td->fnt->wid);
1742 rows = ((Infowin->h - (oy + oy)) / td->fnt->hgt);
1743 if (cols < 1) cols = 1;
1744 if (rows < 1) rows = 1;
1748 if (cols < 80) cols = 80;
1749 if (rows < 24) rows = 24;
1752 wid = cols * td->fnt->wid + (ox + ox);
1753 hgt = rows * td->fnt->hgt + (oy + oy);
1754 Term_resize(cols, rows);
1755 if ((Infowin->w != wid) || (Infowin->h != hgt))
1757 Infowin_set(td->win);
1758 Infowin_resize(wid, hgt);
1767 XSetICFocus(iwin->xic);
1776 XUnsetICFocus(iwin->xic);
1784 Term_activate(&old_td->t);
1785 Infowin_set(old_td->win);
1790 * An array of sound file names
1792 static concptr sound_file[SOUND_MAX];
1795 * Check for existance of a file
1797 static bool check_file(concptr s)
1801 fff = fopen(s, "r");
1802 if (!fff) return (FALSE);
1811 static void init_sound(void)
1816 char dir_xtra_sound[1024];
1817 path_build(dir_xtra_sound, sizeof(dir_xtra_sound), ANGBAND_DIR_XTRA, "sound");
1818 for (i = 1; i < SOUND_MAX; i++)
1820 sprintf(wav, "%s.wav", angband_sound_name[i]);
1821 path_build(buf, sizeof(buf), dir_xtra_sound, wav);
1822 if (check_file(buf)) sound_file[i] = string_make(buf);
1830 * Hack -- make a sound
1832 static errr Term_xtra_x11_sound(int v)
1835 if (!use_sound) return (1);
1836 if ((v < 0) || (v >= SOUND_MAX)) return (1);
1837 if (!sound_file[v]) return (1);
1839 sprintf(buf,"./playwave.sh %s\n", sound_file[v]);
1840 return (system(buf) < 0);
1845 * Handle "activation" of a term
1847 static errr Term_xtra_x11_level(int v)
1849 term_data *td = (term_data*)(Term->data);
1852 Infowin_set(td->win);
1853 Infofnt_set(td->fnt);
1862 static errr Term_xtra_x11_react(void)
1868 for (i = 0; i < 256; i++)
1870 if ((color_table[i][0] != angband_color_table[i][0]) ||
1871 (color_table[i][1] != angband_color_table[i][1]) ||
1872 (color_table[i][2] != angband_color_table[i][2]) ||
1873 (color_table[i][3] != angband_color_table[i][3]))
1876 color_table[i][0] = angband_color_table[i][0];
1877 color_table[i][1] = angband_color_table[i][1];
1878 color_table[i][2] = angband_color_table[i][2];
1879 color_table[i][3] = angband_color_table[i][3];
1880 pixel = create_pixel(Metadpy->dpy,
1884 Infoclr_set(clr[i]);
1885 Infoclr_change_fg(pixel);
1894 * Handle a "special request"
1896 static errr Term_xtra_x11(int n, int v)
1900 case TERM_XTRA_NOISE: Metadpy_do_beep(); return (0);
1901 case TERM_XTRA_SOUND: return (Term_xtra_x11_sound(v));
1903 case TERM_XTRA_FRESH: Metadpy_update(1, 1, 0); return (0);
1905 case TERM_XTRA_FRESH: Metadpy_update(1, 0, 0); return (0);
1907 case TERM_XTRA_BORED: return (CheckEvent(0));
1908 case TERM_XTRA_EVENT: return (CheckEvent(v));
1909 case TERM_XTRA_FLUSH: while (!CheckEvent(FALSE)); return (0);
1910 case TERM_XTRA_LEVEL: return (Term_xtra_x11_level(v));
1911 case TERM_XTRA_CLEAR: Infowin_wipe(); s_ptr->drawn = FALSE; return (0);
1912 case TERM_XTRA_DELAY: usleep(1000 * v); return (0);
1913 case TERM_XTRA_REACT: return (Term_xtra_x11_react());
1920 * Draw the cursor as an inverted rectangle.
1922 * Consider a rectangular outline like "main-mac.c". XXX XXX
1924 static errr Term_curs_x11(int x, int y)
1929 XftDrawRect(Infowin->draw, &xor->fg,
1930 x * Infofnt->wid + Infowin->ox,
1931 y * Infofnt->hgt + Infowin->oy,
1932 Infofnt->wid - 1, Infofnt->hgt - 1);
1933 XftDrawRect(Infowin->draw, &xor->fg,
1934 x * Infofnt->wid + Infowin->ox + 1,
1935 y * Infofnt->hgt + Infowin->oy + 1,
1936 Infofnt->wid - 3, Infofnt->hgt - 3);
1938 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
1939 x * Infofnt->wid + Infowin->ox,
1940 y * Infofnt->hgt + Infowin->oy,
1941 Infofnt->wid - 1, Infofnt->hgt - 1);
1942 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
1943 x * Infofnt->wid + Infowin->ox + 1,
1944 y * Infofnt->hgt + Infowin->oy + 1,
1945 Infofnt->wid - 3, Infofnt->hgt - 3);
1951 Infofnt_text_non(x, y, " ", 1);
1959 * Draw the double width cursor
1961 static errr Term_bigcurs_x11(int x, int y)
1966 XftDrawRect(Infowin->draw, &xor->fg,
1967 x * Infofnt->wid + Infowin->ox,
1968 y * Infofnt->hgt + Infowin->oy,
1969 Infofnt->twid - 1, Infofnt->hgt - 1);
1970 XftDrawRect(Infowin->draw, &xor->fg,
1971 x * Infofnt->wid + Infowin->ox + 1,
1972 y * Infofnt->hgt + Infowin->oy + 1,
1973 Infofnt->twid - 3, Infofnt->hgt - 3);
1975 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
1976 x * Infofnt->wid + Infowin->ox,
1977 y * Infofnt->hgt + Infowin->oy,
1978 Infofnt->twid - 1, Infofnt->hgt - 1);
1979 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
1980 x * Infofnt->wid + Infowin->ox + 1,
1981 y * Infofnt->hgt + Infowin->oy + 1,
1982 Infofnt->twid - 3, Infofnt->hgt - 3);
1988 Infofnt_text_non(x, y, " ", 2);
1995 * Erase some characters.
1997 static errr Term_wipe_x11(int x, int y, int n)
1999 Infoclr_set(clr[TERM_DARK]);
2000 Infofnt_text_non(x, y, "", n);
2001 s_ptr->drawn = FALSE;
2006 * Draw some textual characters.
2008 static errr Term_text_x11(TERM_LEN x, TERM_LEN y, int n, TERM_COLOR a, concptr s)
2010 Infoclr_set(clr[a]);
2011 Infofnt_text_std(x, y, s, n);
2012 s_ptr->drawn = FALSE;
2018 * Draw some graphical characters.
2020 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)
2033 unsigned long pixel, blank;
2035 term_data *td = (term_data*)(Term->data);
2042 for (i = 0; i < n; ++i, x += td->fnt->wid)
2046 x1 = (c&0x7F) * td->fnt->twid;
2047 y1 = (a&0x7F) * td->fnt->hgt;
2048 if (td->tiles->width < x1 + td->fnt->wid ||
2049 td->tiles->height < y1 + td->fnt->hgt)
2051 XFillRectangle(Metadpy->dpy, td->win->win, clr[0]->gc, x, y, td->fnt->twid, td->fnt->hgt);
2058 x2 = (tc&0x7F) * td->fnt->twid;
2059 y2 = (ta&0x7F) * td->fnt->hgt;
2061 if (((x1 == x2) && (y1 == y2)) ||
2062 !(((byte)ta & 0x80) && ((byte)tc & 0x80)) ||
2063 td->tiles->width < x2 + td->fnt->wid ||
2064 td->tiles->height < y2 + td->fnt->hgt)
2066 XPutImage(Metadpy->dpy, td->win->win,
2071 td->fnt->twid, td->fnt->hgt);
2075 blank = XGetPixel(td->tiles, 0, td->fnt->hgt * 6);
2076 for (k = 0; k < td->fnt->twid; k++)
2078 for (l = 0; l < td->fnt->hgt; l++)
2080 if ((pixel = XGetPixel(td->tiles, x1 + k, y1 + l)) == blank)
2082 pixel = XGetPixel(td->tiles, x2 + k, y2 + l);
2085 XPutPixel(td->TmpImage, k, l, pixel);
2089 XPutImage(Metadpy->dpy, td->win->win,
2093 td->fnt->twid, td->fnt->hgt);
2097 s_ptr->drawn = FALSE;
2103 static void IMDestroyCallback(XIM, XPointer, XPointer);
2106 IMInstantiateCallback(Display *display, XPointer unused1, XPointer unused2)
2109 XIMCallback ximcallback;
2110 XIMStyles *xim_styles = NULL;
2116 xim = XOpenIM(display, NULL, NULL, NULL);
2118 printf("can't open IM\n");
2122 ximcallback.callback = IMDestroyCallback;
2123 ximcallback.client_data = NULL;
2124 XSetIMValues(xim, XNDestroyCallback, &ximcallback, NULL);
2125 XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL);
2126 for (i = 0; i < xim_styles->count_styles; i++){
2127 if(xim_styles->supported_styles[i] == (XIMPreeditNothing | XIMStatusNothing)) break;
2129 if(i >= xim_styles->count_styles){
2130 printf("Sorry, your IM does not support 'Root' preedit style...\n");
2138 for (i = 0; i < MAX_TERM_DATA; i++)
2140 infowin *iwin = data[i].win;
2141 if (!iwin) continue;
2142 iwin->xic = XCreateIC(xim, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing), XNClientWindow, iwin->win, XNFocusWindow, iwin->win, NULL);
2145 printf("Can't create input context for Term%d\n", i);
2149 if(XGetICValues(iwin->xic, XNFilterEvents, &iwin->xic_mask, NULL) != NULL)
2151 iwin->xic_mask = 0L;
2154 XSelectInput(Metadpy->dpy, iwin->win, iwin->mask | iwin->xic_mask);
2160 static void IMDestroyCallback(XIM xim, XPointer client_data, XPointer call_data)
2166 if (call_data == NULL){
2167 XRegisterIMInstantiateCallback(Metadpy->dpy, NULL, NULL, NULL, IMInstantiateCallback, NULL);
2170 for(i = 0; i < MAX_TERM_DATA; i++)
2172 infowin *iwin = data[i].win;
2175 XSelectInput(Metadpy->dpy, iwin->win, iwin->mask);
2176 iwin->xic_mask = 0L;
2181 Metadpy->xim = NULL;
2185 static char force_lower(char a)
2187 return ((isupper((a))) ? tolower((a)) : (a))
2191 * Initialize a term_data
2193 static errr term_data_init(term_data *td, int i)
2197 concptr name = angband_term_name[i];
2227 sprintf(buf, "ANGBAND_X11_FONT_%d", i);
2229 if (!font) font = getenv("ANGBAND_X11_FONT");
2237 font = DEFAULT_X11_FONT_0;
2242 font = DEFAULT_X11_FONT_1;
2247 font = DEFAULT_X11_FONT_2;
2252 font = DEFAULT_X11_FONT_3;
2257 font = DEFAULT_X11_FONT_4;
2262 font = DEFAULT_X11_FONT_5;
2267 font = DEFAULT_X11_FONT_6;
2272 font = DEFAULT_X11_FONT_7;
2277 font = DEFAULT_X11_FONT;
2282 sprintf(buf, "ANGBAND_X11_AT_X_%d", i);
2284 x = (str != NULL) ? atoi(str) : -1;
2286 sprintf(buf, "ANGBAND_X11_AT_Y_%d", i);
2288 y = (str != NULL) ? atoi(str) : -1;
2290 sprintf(buf, "ANGBAND_X11_COLS_%d", i);
2292 val = (str != NULL) ? atoi(str) : -1;
2293 if (val > 0) cols = val;
2295 sprintf(buf, "ANGBAND_X11_ROWS_%d", i);
2297 val = (str != NULL) ? atoi(str) : -1;
2298 if (val > 0) rows = val;
2302 if (cols < 80) cols = 80;
2303 if (rows < 24) rows = 24;
2306 sprintf(buf, "ANGBAND_X11_IBOX_%d", i);
2308 val = (str != NULL) ? atoi(str) : -1;
2309 if (val > 0) ox = val;
2311 sprintf(buf, "ANGBAND_X11_IBOY_%d", i);
2313 val = (str != NULL) ? atoi(str) : -1;
2314 if (val > 0) oy = val;
2316 MAKE(td->fnt, infofnt);
2317 Infofnt_set(td->fnt);
2318 Infofnt_init_data(font);
2320 num = ((i == 0) ? 1024 : 16);
2321 wid = cols * td->fnt->wid + (ox + ox);
2322 hgt = rows * td->fnt->hgt + (oy + oy);
2323 MAKE(td->win, infowin);
2324 Infowin_set(td->win);
2325 Infowin_init_top(x, y, wid, hgt, 0,
2326 Metadpy->fg, Metadpy->bg);
2328 #if defined(USE_XIM)
2329 Infowin_set_mask(ExposureMask | StructureNotifyMask | KeyPressMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask);
2331 Infowin_set_mask(ExposureMask | StructureNotifyMask | KeyPressMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask);
2334 Infowin_set_name(name);
2337 ch = XAllocClassHint();
2339 if (ch == NULL) quit("XAllocClassHint failed");
2341 strcpy(res_name, name);
2342 res_name[0] = force_lower(res_name[0]);
2343 ch->res_name = res_name;
2345 strcpy(res_class, "Angband");
2346 ch->res_class = res_class;
2348 XSetClassHint(Metadpy->dpy, Infowin->win, ch);
2349 sh = XAllocSizeHints();
2350 if (sh == NULL) quit("XAllocSizeHints failed");
2354 sh->flags = PMinSize | PMaxSize;
2355 sh->min_width = 80 * td->fnt->wid + (ox + ox);
2356 sh->min_height = 24 * td->fnt->hgt + (oy + oy);
2357 sh->max_width = 255 * td->fnt->wid + (ox + ox);
2358 sh->max_height = 255 * td->fnt->hgt + (oy + oy);
2362 sh->flags = PMinSize | PMaxSize;
2363 sh->min_width = td->fnt->wid + (ox + ox);
2364 sh->min_height = td->fnt->hgt + (oy + oy);
2365 sh->max_width = 256 * td->fnt->wid + (ox + ox);
2366 sh->max_height = 256 * td->fnt->hgt + (oy + oy);
2369 sh->flags |= PResizeInc;
2370 sh->width_inc = td->fnt->wid;
2371 sh->height_inc = td->fnt->hgt;
2372 sh->flags |= PBaseSize;
2373 sh->base_width = (ox + ox);
2374 sh->base_height = (oy + oy);
2375 XSetWMNormalHints(Metadpy->dpy, Infowin->win, sh);
2379 wh = XAllocWMHints();
2380 if(wh == NULL) quit("XAllocWMHints failed");
2381 wh->flags = InputHint;
2383 XSetWMHints(Metadpy->dpy, Infowin->win, wh);
2386 if ((x >= 0) && (y >= 0)) Infowin_impell(x, y);
2388 term_init(t, cols, rows, num);
2389 t->soft_cursor = TRUE;
2390 t->attr_blank = TERM_WHITE;
2391 t->char_blank = ' ';
2392 t->xtra_hook = Term_xtra_x11;
2393 t->curs_hook = Term_curs_x11;
2394 t->bigcurs_hook = Term_bigcurs_x11;
2395 t->wipe_hook = Term_wipe_x11;
2396 t->text_hook = Term_text_x11;
2403 * Initialization function for an "X11" module to Angband
2405 errr init_x11(int argc, char *argv[])
2408 concptr dpy_name = "";
2412 char filename[1024];
2420 for (i = 1; i < argc; i++)
2422 if (prefix(argv[i], "-d"))
2424 dpy_name = &argv[i][2];
2429 if (prefix(argv[i], "-s"))
2431 smoothRescaling = FALSE;
2435 if (prefix(argv[i], "-a"))
2437 arg_graphics = GRAPHICS_ADAM_BOLT;
2441 if (prefix(argv[i], "-o"))
2443 arg_graphics = GRAPHICS_ORIGINAL;
2448 if (prefix(argv[i], "-b"))
2450 arg_bigtile = use_bigtile = TRUE;
2454 if (prefix(argv[i], "-n"))
2456 num_term = atoi(&argv[i][2]);
2457 if (num_term > MAX_TERM_DATA) num_term = MAX_TERM_DATA;
2458 else if (num_term < 1) num_term = 1;
2462 if (prefix(argv[i], "--"))
2467 plog_fmt("Ignoring option: %s", argv[i]);
2473 setlocale(LC_ALL, "");
2475 #ifdef DEFAULT_LOCALE
2476 if(!strcmp(setlocale(LC_ALL, NULL), "C")){
2477 printf("try default locale \"%s\"\n", DEFAULT_LOCALE);
2478 setlocale(LC_ALL, DEFAULT_LOCALE);
2482 if(!strcmp(setlocale(LC_ALL, NULL), "C"))
2484 printf("WARNING: Locale is not supported. Non-english font may be displayed incorrectly.\n");
2487 if(!XSupportsLocale()){
2488 printf("can't support locale in X\n");
2489 setlocale(LC_ALL, "C");
2492 setlocale(LC_ALL, "C");
2495 #endif /* USE_LOCALE */
2497 if (Metadpy_init_name(dpy_name)) return (-1);
2501 Infoclr_init_ppn(Metadpy->fg, Metadpy->bg, "xor", 0);
2502 for (i = 0; i < 256; ++i)
2505 MAKE(clr[i], infoclr);
2506 Infoclr_set(clr[i]);
2507 color_table[i][0] = angband_color_table[i][0];
2508 color_table[i][1] = angband_color_table[i][1];
2509 color_table[i][2] = angband_color_table[i][2];
2510 color_table[i][3] = angband_color_table[i][3];
2511 pixel = ((i == 0) ? Metadpy->bg : Metadpy->fg);
2514 pixel = create_pixel(Metadpy->dpy,
2520 Infoclr_init_ppn(pixel, Metadpy->bg, "cpy", 0);
2524 for (i = 0; i < num_term; i++)
2526 term_data *td = &data[i];
2527 term_data_init(td, i);
2528 angband_term[i] = Term;
2531 Infowin_set(data[0].win);
2533 Term_activate(&data[0].t);
2538 p = XSetLocaleModifiers("");
2540 p = XSetLocaleModifiers("@im=");
2543 XRegisterIMInstantiateCallback(Metadpy->dpy, NULL, NULL, NULL, IMInstantiateCallback, NULL);
2546 if (arg_sound) init_sound();
2549 switch (arg_graphics)
2551 case GRAPHICS_ORIGINAL:
2552 path_build(filename, sizeof(filename), ANGBAND_DIR_XTRA, "graf/8x8.bmp");
2553 if (0 == fd_close(fd_open(filename, O_RDONLY)))
2555 use_graphics = TRUE;
2556 pict_wid = pict_hgt = 8;
2557 ANGBAND_GRAF = "old";
2560 case GRAPHICS_ADAM_BOLT:
2561 path_build(filename, sizeof(filename), ANGBAND_DIR_XTRA, "graf/16x16.bmp");
2562 if (0 == fd_close(fd_open(filename, O_RDONLY)))
2564 use_graphics = TRUE;
2565 pict_wid = pict_hgt = 16;
2566 ANGBAND_GRAF = "new";
2573 Display *dpy = Metadpy->dpy;
2575 tiles_raw = ReadBMP(dpy, filename);
2576 for (i = 0; i < num_term; i++)
2578 term_data *td = &data[i];
2580 t->pict_hook = Term_pict_x11;
2581 t->higher_pict = TRUE;
2583 ResizeImage(dpy, tiles_raw,
2585 td->fnt->twid, td->fnt->hgt);
2588 for (i = 0; i < num_term; i++)
2590 term_data *td = &data[i];
2592 int depth = DefaultDepth(dpy, DefaultScreen(dpy));
2593 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
2596 jj = (depth - 1) >> 2;
2597 while (jj >>= 1) ii <<= 1;
2598 total = td->fnt->twid * td->fnt->hgt * ii;
2599 TmpData = (char *)malloc(total);
2600 td->TmpImage = XCreateImage(dpy,visual,depth,
2601 ZPixmap, 0, TmpData,
2602 td->fnt->twid, td->fnt->hgt, 8, 0);
2605 #endif /* ! USE_XFT */
2609 #endif /* USE_X11 */