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 #ifdef USE_JP_FONTSTRUCT
14 * 日本語(EUC-JAPAN)対応 (-DJP)
16 * ・日本語を含む文字列の表示ルーチン XDrawMultiString() の追加
17 * ・日本語の表示幅は,フォントの情報によらすASCIIフォントの2倍に固定
22 * 1996/6/7 李 晃伸 (ri@kuis.kyoto-u.ac.jp)
26 * This file helps Angband work with UNIX/X11 computers.
28 * To use this file, compile with "USE_X11" defined, and link against all
29 * the various "X11" libraries which may be needed.
31 * See also "main-xaw.c".
33 * Part of this file provides a user interface package composed of several
34 * pseudo-objects, including "metadpy" (a display), "infowin" (a window),
35 * "infoclr" (a color), and "infofnt" (a font). Actually, the package was
36 * originally much more interesting, but it was bastardized to keep this
39 * The rest of this file is an implementation of "main-xxx.c" for X11.
41 * Most of this file is by Ben Harrison (benh@phial.com).
45 * The following shell script can be used to launch Angband, assuming that
46 * it was extracted into "~/Angband", and compiled using "USE_X11", on a
47 * Linux machine, with a 1280x1024 screen, using 6 windows (with the given
48 * characteristics), with gamma correction of 1.8 -> (1 / 1.8) * 256 = 142,
49 * and without graphics (add "-g" for graphics). Just copy this comment
50 * into a file, remove the leading " * " characters (and the head/tail of
51 * this comment), and make the file executable.
57 * echo "Launching angband..."
61 * setenv ANGBAND_X11_FONT_0 10x20
62 * setenv ANGBAND_X11_AT_X_0 5
63 * setenv ANGBAND_X11_AT_Y_0 510
66 * setenv ANGBAND_X11_FONT_1 8x13
67 * setenv ANGBAND_X11_AT_X_1 5
68 * setenv ANGBAND_X11_AT_Y_1 22
69 * setenv ANGBAND_X11_ROWS_1 35
72 * setenv ANGBAND_X11_FONT_2 8x13
73 * setenv ANGBAND_X11_AT_X_2 635
74 * setenv ANGBAND_X11_AT_Y_2 182
75 * setenv ANGBAND_X11_ROWS_3 23
78 * setenv ANGBAND_X11_FONT_3 8x13
79 * setenv ANGBAND_X11_AT_X_3 635
80 * setenv ANGBAND_X11_AT_Y_3 22
81 * setenv ANGBAND_X11_ROWS_3 12
83 * # Monster recall window
84 * setenv ANGBAND_X11_FONT_4 6x13
85 * setenv ANGBAND_X11_AT_X_4 817
86 * setenv ANGBAND_X11_AT_Y_4 847
87 * setenv ANGBAND_X11_COLS_4 76
88 * setenv ANGBAND_X11_ROWS_4 11
90 * # Object recall window
91 * setenv ANGBAND_X11_FONT_5 6x13
92 * setenv ANGBAND_X11_AT_X_5 817
93 * setenv ANGBAND_X11_AT_Y_5 520
94 * setenv ANGBAND_X11_COLS_5 76
95 * setenv ANGBAND_X11_ROWS_5 24
97 * # The build directory
101 * setenv ANGBAND_X11_GAMMA 142
104 * ./src/angband -mx11 -- -n6 &
116 * Available graphic modes
118 #define GRAPHICS_NONE 0
119 #define GRAPHICS_ORIGINAL 1
120 #define GRAPHICS_ADAM_BOLT 2
121 #define GRAPHICS_HENGBAND 3
125 #ifndef __MAKEDEPEND__
126 #include <X11/Xlib.h>
127 #include <X11/Xutil.h>
128 #include <X11/keysym.h>
129 #include <X11/keysymdef.h>
131 #include <X11/Xlocale.h>
133 #include <X11/Xatom.h>
134 #endif /* __MAKEDEPEND__ */
138 * Include some helpful X11 code.
140 #include "maid-x11.c"
144 * Hack -- avoid some compiler warnings
146 #define IGNORE_UNUSED_FUNCTIONS
152 * 1) On a monochrome (or "fake-monochrome") display, all colors
153 * will be "cast" to "fg," except for the bg color, which is,
154 * obviously, cast to "bg". Thus, one can ignore this setting.
156 * 2) Because of the inner functioning of the color allocation
157 * routines, colors may be specified as (a) a typical color name,
158 * (b) a hexidecimal color specification (preceded by a pound sign),
159 * or (c) by strings such as "fg", "bg", "zg".
161 * 3) Due to the workings of the init routines, many colors
162 * may also be dealt with by their actual pixel values. Note that
163 * the pixel with all bits set is "zg = (1<<metadpy->depth)-1", which
164 * is not necessarily either black or white.
169 /**** Generic Types ****/
173 * An X11 pixell specifier
175 typedef unsigned long Pixell;
178 * The structures defined below
180 typedef struct metadpy metadpy;
181 typedef struct infowin infowin;
182 typedef struct infoclr infoclr;
183 typedef struct infofnt infofnt;
187 * A structure summarizing a given Display.
189 * - The Display itself
190 * - The default Screen for the display
191 * - The virtual root (usually just the root)
192 * - The default colormap (from a macro)
194 * - The "name" of the display
196 * - The socket to listen to for events
198 * - The width of the display screen (from a macro)
199 * - The height of the display screen (from a macro)
200 * - The bit depth of the display screen (from a macro)
202 * - The black Pixell (from a macro)
203 * - The white Pixell (from a macro)
205 * - The background Pixell (default: black)
206 * - The foreground Pixell (default: white)
207 * - The maximal Pixell (Equals: ((2 ^ depth)-1), is usually ugly)
209 * - Bit Flag: Force all colors to black and white (default: !color)
210 * - Bit Flag: Allow the use of color (default: depth > 1)
211 * - Bit Flag: We created 'dpy', and so should nuke it when done.
246 * A Structure summarizing Window Information.
248 * I assume that a window is at most 30000 pixels on a side.
249 * I assume that the root windw is also at most 30000 square.
252 * - The current Input Event Mask
254 * - The location of the window
255 * - The width, height of the window
256 * - The border width of this window
258 * - Byte: 1st Extra byte
260 * - Bit Flag: This window is currently Mapped
261 * - Bit Flag: This window needs to be redrawn
262 * - Bit Flag: This window has been resized
264 * - Bit Flag: We should nuke 'win' when done with it
266 * - Bit Flag: 1st extra flag
267 * - Bit Flag: 2nd extra flag
268 * - Bit Flag: 3rd extra flag
269 * - Bit Flag: 4th extra flag
307 * A Structure summarizing Operation+Color Information
309 * - The actual GC corresponding to this info
311 * - The Foreground Pixell Value
312 * - The Background Pixell Value
314 * - Num (0-15): The operation code (As in Clear, Xor, etc)
315 * - Bit Flag: The GC is in stipple mode
316 * - Bit Flag: Destroy 'gc' at Nuke time.
333 * A Structure to Hold Font Information
335 * - The 'XFontStruct*' (yields the 'Font')
339 * - The default character width
340 * - The default character height
341 * - The default character ascent
343 * - Byte: Pixel offset used during fake mono
345 * - Flag: Force monospacing via 'wid'
346 * - Flag: Nuke info when done
372 /**** Generic Macros ****/
376 /* Set current metadpy (Metadpy) to 'M' */
377 #define Metadpy_set(M) \
381 /* Initialize 'M' using Display 'D' */
382 #define Metadpy_init_dpy(D) \
383 Metadpy_init_2(D,cNULL)
385 /* Initialize 'M' using a Display named 'N' */
386 #define Metadpy_init_name(N) \
387 Metadpy_init_2((Display*)(NULL),N)
389 /* Initialize 'M' using the standard Display */
390 #define Metadpy_init() \
391 Metadpy_init_name("")
394 /* Init an infowin by giving father as an (info_win*) (or NULL), and data */
395 #define Infowin_init_dad(D,X,Y,W,H,B,FG,BG) \
396 Infowin_init_data(((D) ? ((D)->win) : (Window)(None)), \
400 /* Init a top level infowin by pos,size,bord,Colors */
401 #define Infowin_init_top(X,Y,W,H,B,FG,BG) \
402 Infowin_init_data(None,X,Y,W,H,B,FG,BG)
405 /* Request a new standard window by giving Dad infowin and X,Y,W,H */
406 #define Infowin_init_std(D,X,Y,W,H,B) \
407 Infowin_init_dad(D,X,Y,W,H,B,Metadpy->fg,Metadpy->bg)
410 /* Set the current Infowin */
411 #define Infowin_set(I) \
415 /* Set the current Infoclr */
416 #define Infoclr_set(C) \
420 #define Infoclr_init_ppo(F,B,O,M) \
421 Infoclr_init_data(F,B,O,M)
423 #define Infoclr_init_cco(F,B,O,M) \
424 Infoclr_init_ppo(Infoclr_Pixell(F),Infoclr_Pixell(B),O,M)
426 #define Infoclr_init_ppn(F,B,O,M) \
427 Infoclr_init_ppo(F,B,Infoclr_Opcode(O),M)
429 #define Infoclr_init_ccn(F,B,O,M) \
430 Infoclr_init_cco(F,B,Infoclr_Opcode(O),M)
433 /* Set the current infofnt */
434 #define Infofnt_set(I) \
438 /* Errr: Expose Infowin */
439 #define Infowin_expose() \
440 (!(Infowin->redraw = 1))
442 /* Errr: Unxpose Infowin */
443 #define Infowin_unexpose() \
444 (Infowin->redraw = 0)
448 /**** Generic Globals ****/
452 * The "default" values
454 static metadpy metadpy_default;
458 * The "current" variables
460 static metadpy *Metadpy = &metadpy_default;
461 static infowin *Infowin = (infowin*)(NULL);
463 static infowin *Focuswin = (infowin*)(NULL);
465 static infoclr *Infoclr = (infoclr*)(NULL);
466 #ifdef USE_JP_FONTSTRUCT
467 static infofnt *Infofnt = (infofnt*)(NULL);
468 static infofnt *Infokfnt = (infofnt*)(NULL);
470 static infofnt *Infofnt = (infofnt*)(NULL);
476 /**** Generic code ****/
479 #ifdef USE_JP_FONTSTRUCT
480 #define Infokfnt_set(I) \
484 * Init the current metadpy, with various initialization stuff.
487 * dpy: The Display* to use (if NULL, create it)
488 * name: The name of the Display (if NULL, the current)
491 * If 'name' is NULL, but 'dpy' is set, extract name from dpy
492 * If 'dpy' is NULL, then Create the named Display
493 * If 'name' is NULL, and so is 'dpy', use current Display
495 * Return -1 if no Display given, and none can be opened.
497 static errr Metadpy_init_2(Display *dpy, concptr name)
499 metadpy *m = Metadpy;
501 /*** Open the display if needed ***/
503 /* If no Display given, attempt to Create one */
506 /* Attempt to open the display */
507 dpy = XOpenDisplay(name);
510 if (!dpy) return (-1);
512 /* We will have to nuke it when done */
516 /* Since the Display was given, use it */
519 /* We will not have to nuke it when done */
524 /*** Save some information ***/
526 /* Save the Display itself */
529 /* Get the Screen and Virtual Root Window */
530 m->screen = DefaultScreenOfDisplay(dpy);
531 m->root = RootWindowOfScreen(m->screen);
533 /* Get the default colormap */
534 m->cmap = DefaultColormapOfScreen(m->screen);
536 /* Extract the true name of the display */
537 m->name = DisplayString(dpy);
540 m->fd = ConnectionNumber(Metadpy->dpy);
542 /* Save the Size and Depth of the screen */
543 m->width = WidthOfScreen(m->screen);
544 m->height = HeightOfScreen(m->screen);
545 m->depth = DefaultDepthOfScreen(m->screen);
547 /* Save the Standard Colors */
548 m->black = BlackPixelOfScreen(m->screen);
549 m->white = WhitePixelOfScreen(m->screen);
551 /*** Make some clever Guesses ***/
553 /* Guess at the desired 'fg' and 'bg' Pixell's */
557 /* Calculate the Maximum allowed Pixel value. */
558 m->zg = (1 << m->depth) - 1;
560 /* Save various default Flag Settings */
561 m->color = ((m->depth > 1) ? 1 : 0);
562 m->mono = ((m->color) ? 0 : 1);
564 /* Return "success" */
569 #ifndef IGNORE_UNUSED_FUNCTIONS
572 * Nuke the current metadpy
574 static errr Metadpy_nuke(void)
576 metadpy *m = Metadpy;
579 /* If required, Free the Display */
582 /* Close the Display */
583 XCloseDisplay(m->dpy);
585 /* Forget the Display */
586 m->dpy = (Display*)(NULL);
588 /* Do not nuke it again */
596 #endif /* IGNORE_UNUSED_FUNCTIONS */
600 * General Flush/ Sync/ Discard routine
602 static errr Metadpy_update(int flush, int sync, int discard)
604 /* Flush if desired */
605 if (flush) XFlush(Metadpy->dpy);
607 /* Sync if desired, using 'discard' */
608 if (sync) XSync(Metadpy->dpy, discard);
618 static errr Metadpy_do_beep(void)
620 /* Make a simple beep */
621 XBell(Metadpy->dpy, 100);
629 * Set the name (in the title bar) of Infowin
631 static errr Infowin_set_name(concptr name)
638 st = XStringListToTextProperty(&bp, 1, &tp);
639 if (st) XSetWMName(Metadpy->dpy, Infowin->win, &tp);
644 #ifndef IGNORE_UNUSED_FUNCTIONS
647 * Set the icon name of Infowin
649 static errr Infowin_set_icon_name(concptr name)
656 st = XStringListToTextProperty(&bp, 1, &tp);
657 if (st) XSetWMIconName(Metadpy->dpy, Infowin->win, &tp);
665 static errr Infowin_nuke(void)
667 infowin *iwin = Infowin;
669 /* Nuke if requested */
672 /* Destory the old window */
673 XDestroyWindow(Metadpy->dpy, iwin->win);
680 #endif /* IGNORE_UNUSED_FUNCTIONS */
684 * Prepare a new 'infowin'.
686 static errr Infowin_prepare(Window xid)
688 infowin *iwin = Infowin;
691 XWindowAttributes xwa;
693 unsigned int w, h, b, d;
698 /* Check For Error XXX Extract some ACTUAL data from 'xid' */
699 XGetGeometry(Metadpy->dpy, xid, &tmp_win, &x, &y, &w, &h, &b, &d);
701 /* Apply the above info */
708 /* Check Error XXX Extract some more ACTUAL data */
709 XGetWindowAttributes(Metadpy->dpy, xid, &xwa);
711 /* Apply the above info */
712 iwin->mask = xwa.your_event_mask;
713 iwin->mapped = ((xwa.map_state == IsUnmapped) ? 0 : 1);
715 /* And assume that we are exposed */
723 #ifndef IGNORE_UNUSED_FUNCTIONS
726 * Initialize a new 'infowin'.
728 static errr Infowin_init_real(Window xid)
731 (void)WIPE(Infowin, infowin);
733 /* Start out non-nukable */
736 /* Attempt to Prepare ourself */
737 return (Infowin_prepare(xid));
740 #endif /* IGNORE_UNUSED_FUNCTIONS */
744 * Init an infowin by giving some data.
747 * dad: The Window that should own this Window (if any)
748 * x,y: The position of this Window
749 * w,h: The size of this Window
750 * b,d: The border width and pixel depth
753 * If 'dad == None' assume 'dad == root'
755 static errr Infowin_init_data(Window dad, int x, int y, int w, int h,
756 int b, Pixell fg, Pixell bg)
761 (void)WIPE(Infowin, infowin);
764 /*** Error Check XXX ***/
767 /*** Create the Window 'xid' from data ***/
769 /* What happened here? XXX XXX XXX */
771 /* If no parent given, depend on root */
774 /* Create the Window XXX Error Check */
775 xid = XCreateSimpleWindow(Metadpy->dpy, dad, x, y, w, h, b, fg, bg);
777 /* Start out selecting No events */
778 XSelectInput(Metadpy->dpy, xid, 0L);
781 /*** Prepare the new infowin ***/
783 /* Mark it as nukable */
786 /* Attempt to Initialize the infowin */
787 return (Infowin_prepare(xid));
793 * Modify the event mask of an Infowin
795 static errr Infowin_set_mask(long mask)
797 /* Save the new setting */
798 Infowin->mask = mask;
800 /* Execute the Mapping */
801 XSelectInput(Metadpy->dpy, Infowin->win, Infowin->mask);
809 * Request that Infowin be mapped
811 static errr Infowin_map(void)
813 /* Execute the Mapping */
814 XMapWindow(Metadpy->dpy, Infowin->win);
821 #ifndef IGNORE_UNUSED_FUNCTIONS
824 * Request that Infowin be unmapped
826 static errr Infowin_unmap(void)
828 /* Execute the Un-Mapping */
829 XUnmapWindow(Metadpy->dpy, Infowin->win);
835 #endif /* IGNORE_UNUSED_FUNCTIONS */
839 * Request that Infowin be raised
841 static errr Infowin_raise(void)
843 /* Raise towards visibility */
844 XRaiseWindow(Metadpy->dpy, Infowin->win);
851 #ifndef IGNORE_UNUSED_FUNCTIONS
854 * Request that Infowin be lowered
856 static errr Infowin_lower(void)
858 /* Lower towards invisibility */
859 XLowerWindow(Metadpy->dpy, Infowin->win);
865 #endif /* IGNORE_UNUSED_FUNCTIONS */
869 * Request that Infowin be moved to a new location
871 static errr Infowin_impell(int x, int y)
873 /* Execute the request */
874 XMoveWindow(Metadpy->dpy, Infowin->win, x, y);
884 static errr Infowin_resize(int w, int h)
886 /* Execute the request */
887 XResizeWindow(Metadpy->dpy, Infowin->win, w, h);
894 #ifndef IGNORE_UNUSED_FUNCTIONS
897 * Move and Resize an infowin
899 static errr Infowin_locate(int x, int y, int w, int h)
901 /* Execute the request */
902 XMoveResizeWindow(Metadpy->dpy, Infowin->win, x, y, w, h);
908 #endif /* IGNORE_UNUSED_FUNCTIONS */
912 * Visually clear Infowin
914 static errr Infowin_wipe(void)
916 /* Execute the request */
917 XClearWindow(Metadpy->dpy, Infowin->win);
924 #ifndef IGNORE_UNUSED_FUNCTIONS
927 * Visually Paint Infowin with the current color
929 static errr Infowin_fill(void)
931 /* Execute the request */
932 XFillRectangle(Metadpy->dpy, Infowin->win, Infoclr->gc,
933 0, 0, Infowin->w, Infowin->h);
939 #endif /* IGNORE_UNUSED_FUNCTIONS */
943 * A NULL terminated pair list of legal "operation names"
945 * Pairs of values, first is texttual name, second is the string
946 * holding the decimal value that the operation corresponds to.
948 static concptr opcode_pairs[] =
967 "+copyInverted", "12",
975 * Parse a word into an operation "code"
978 * str: A string, hopefully representing an Operation
981 * 0-15: if 'str' is a valid Operation
982 * -1: if 'str' could not be parsed
984 static int Infoclr_Opcode(concptr str)
988 /* Scan through all legal operation names */
989 for (i = 0; opcode_pairs[i*2]; ++i)
991 /* Is this the right oprname? */
992 if (streq(opcode_pairs[i*2], str))
994 /* Convert the second element in the pair into a Code */
995 return (atoi(opcode_pairs[i*2+1]));
999 /* The code was not found, return -1 */
1004 #ifndef IGNORE_UNUSED_FUNCTIONS
1007 * Request a Pixell by name. Note: uses 'Metadpy'.
1010 * name: The name of the color to try to load (see below)
1013 * The Pixell value that metched the given name
1014 * 'Metadpy->fg' if the name was unparseable
1016 * Valid forms for 'name':
1017 * 'fg', 'bg', 'zg', '<name>' and '#<code>'
1019 static Pixell Infoclr_Pixell(concptr name)
1023 /* Attempt to Parse the name */
1024 if (name && name[0])
1026 /* The 'bg' color is available */
1027 if (streq(name, "bg")) return (Metadpy->bg);
1029 /* The 'fg' color is available */
1030 if (streq(name, "fg")) return (Metadpy->fg);
1032 /* The 'zg' color is available */
1033 if (streq(name, "zg")) return (Metadpy->zg);
1035 /* The 'white' color is available */
1036 if (streq(name, "white")) return (Metadpy->white);
1038 /* The 'black' color is available */
1039 if (streq(name, "black")) return (Metadpy->black);
1041 /* Attempt to parse 'name' into 'scrn' */
1042 if (!(XParseColor(Metadpy->dpy, Metadpy->cmap, name, &scrn)))
1044 plog_fmt("Warning: Couldn't parse color '%s'\n", name);
1047 /* Attempt to Allocate the Parsed color */
1048 if (!(XAllocColor(Metadpy->dpy, Metadpy->cmap, &scrn)))
1050 plog_fmt("Warning: Couldn't allocate color '%s'\n", name);
1053 /* The Pixel was Allocated correctly */
1054 else return (scrn.pixel);
1057 /* Warn about the Default being Used */
1058 plog_fmt("Warning: Using 'fg' for unknown color '%s'\n", name);
1060 /* Default to the 'Foreground' color */
1061 return (Metadpy->fg);
1066 * Initialize a new 'infoclr' with a real GC.
1068 static errr Infoclr_init_1(GC gc)
1070 infoclr *iclr = Infoclr;
1072 /* Wipe the iclr clean */
1073 (void)WIPE(iclr, infoclr);
1084 * Nuke an old 'infoclr'.
1086 static errr Infoclr_nuke(void)
1088 infoclr *iclr = Infoclr;
1090 /* Deal with 'GC' */
1094 XFreeGC(Metadpy->dpy, iclr->gc);
1097 /* Forget the current */
1098 Infoclr = (infoclr*)(NULL);
1104 #endif /* IGNORE_UNUSED_FUNCTIONS */
1108 * Initialize an infoclr with some data
1111 * fg: The Pixell for the requested Foreground (see above)
1112 * bg: The Pixell for the requested Background (see above)
1113 * op: The Opcode for the requested Operation (see above)
1114 * stip: The stipple mode
1116 static errr Infoclr_init_data(Pixell fg, Pixell bg, int op, int stip)
1118 infoclr *iclr = Infoclr;
1122 unsigned long gc_mask;
1126 /*** Simple error checking of opr and clr ***/
1128 /* Check the 'Pixells' for realism */
1129 if (bg > Metadpy->zg) return (-1);
1130 if (fg > Metadpy->zg) return (-1);
1132 /* Check the data for trueness */
1133 if ((op < 0) || (op > 15)) return (-1);
1136 /*** Create the requested 'GC' ***/
1138 /* Assign the proper GC function */
1141 /* Assign the proper GC background */
1142 gcv.background = bg;
1144 /* Assign the proper GC foreground */
1145 gcv.foreground = fg;
1147 /* Hack -- Handle XOR (xor is code 6) by hacking bg and fg */
1148 if (op == 6) gcv.background = 0;
1149 if (op == 6) gcv.foreground = (bg ^ fg);
1151 /* Assign the proper GC Fill Style */
1152 gcv.fill_style = (stip ? FillStippled : FillSolid);
1154 /* Turn off 'Give exposure events for pixmap copying' */
1155 gcv.graphics_exposures = False;
1157 /* Set up the GC mask */
1158 gc_mask = (GCFunction | GCBackground | GCForeground |
1159 GCFillStyle | GCGraphicsExposures);
1161 /* Create the GC detailed above */
1162 gc = XCreateGC(Metadpy->dpy, Metadpy->root, gc_mask, &gcv);
1165 /*** Initialize ***/
1167 /* Wipe the iclr clean */
1168 (void)WIPE(iclr, infoclr);
1173 /* Nuke it when done */
1176 /* Assign the parms */
1180 iclr->stip = stip ? 1 : 0;
1189 * Change the 'fg' for an infoclr
1192 * fg: The Pixell for the requested Foreground (see above)
1194 static errr Infoclr_change_fg(Pixell fg)
1196 infoclr *iclr = Infoclr;
1199 /*** Simple error checking of opr and clr ***/
1201 /* Check the 'Pixells' for realism */
1202 if (fg > Metadpy->zg) return (-1);
1208 XSetForeground(Metadpy->dpy, iclr->gc, fg);
1216 #ifndef IGNORE_UNUSED_FUNCTIONS
1219 * Nuke an old 'infofnt'.
1221 static errr Infofnt_nuke(void)
1223 infofnt *ifnt = Infofnt;
1225 #ifdef USE_JP_FONTSTRUCT
1226 infofnt *ikfnt = Infokfnt;
1228 /* Deal with 'name' */
1232 string_free(ifnt->name);
1235 #ifdef USE_JP_FONTSTRUCT
1239 string_free(ikfnt->name);
1243 /* Nuke info if needed */
1248 XFreeFontSet(Metadpy->dpy, ifnt->info);
1250 XFreeFont(Metadpy->dpy, ifnt->info);
1254 #ifdef USE_JP_FONTSTRUCT
1258 XFreeFont(Metadpy->dpy, ikfnt->info);
1265 #endif /* IGNORE_UNUSED_FUNCTIONS */
1269 * Prepare a new 'infofnt'
1271 #ifdef USE_JP_FONTSTRUCT
1272 static errr Infofnt_prepare(XFontStruct *info, XFontStruct *kinfo)
1275 static errr Infofnt_prepare(XFontSet info)
1277 static errr Infofnt_prepare(XFontStruct *info)
1282 infofnt *ifnt = Infofnt;
1284 #ifdef USE_JP_FONTSTRUCT
1285 infofnt *ikfnt = Infokfnt;
1289 XFontStruct **fontinfo;
1292 int ascent, descent, width;
1295 /* Assign the struct */
1299 n_fonts = XFontsOfFontSet(info, &fontinfo, &fontname);
1301 ascent = descent = width = 0;
1302 while(n_fonts-- > 0){
1303 cs = &((*fontinfo)->max_bounds);
1304 if(ascent < (*fontinfo)->ascent) ascent = (*fontinfo)->ascent;
1305 if(descent < (*fontinfo)->descent) descent = (*fontinfo)->descent;
1306 if(((*fontinfo)->max_byte1) > 0){
1307 /* 多バイト文字の場合は幅半分(端数切り上げ)で評価する */
1308 if(width < (cs->width+1)/2) width = (cs->width+1)/2;
1310 if(width < cs->width) width = cs->width;
1316 ifnt->hgt = ascent + descent;
1319 /* Jump into the max bouonds thing */
1320 cs = &(info->max_bounds);
1322 /* Extract default sizing info */
1323 ifnt->asc = info->ascent;
1324 ifnt->hgt = info->ascent + info->descent;
1325 ifnt->wid = cs->width;
1329 ifnt->twid = 2 * ifnt->wid;
1331 ifnt->twid = ifnt->wid;
1333 #ifdef USE_JP_FONTSTRUCT
1334 /* Assign the struct */
1335 ikfnt->info = kinfo;
1337 /* Jump into the max bouonds thing */
1338 cs = &(kinfo->max_bounds);
1340 /* Extract default sizing info */
1341 ikfnt->asc = kinfo->ascent;
1342 ikfnt->hgt = kinfo->ascent + kinfo->descent;
1343 ikfnt->wid = cs->width;
1346 #ifdef OBSOLETE_SIZING_METHOD
1347 /* Extract default sizing info */
1348 ifnt->asc = cs->ascent;
1349 ifnt->hgt = (cs->ascent + cs->descent);
1350 ifnt->wid = cs->width;
1359 #ifndef IGNORE_UNUSED_FUNCTIONS
1362 * Initialize a new 'infofnt'.
1364 #ifdef USE_JP_FONTSTRUCT
1365 static errr Infofnt_init_real(XFontStruct *info, XFontStruct *kinfo)
1368 static errr Infofnt_init_real(XFontSet info)
1370 static errr Infofnt_init_real(XFontStruct *info)
1375 /* Wipe the thing */
1376 (void)WIPE(Infofnt, infofnt);
1378 #ifdef USE_JP_FONTSTRUCT
1379 WIPE(Infokfnt, infofnt);
1384 #ifdef USE_JP_FONTSTRUCT
1387 /* Attempt to prepare it */
1388 #ifdef USE_JP_FONTSTRUCT
1389 return (Infofnt_prepare (info, kinfo));
1391 return (Infofnt_prepare(info));
1396 #endif /* IGNORE_UNUSED_FUNCTIONS */
1400 * Init an infofnt by its Name
1403 * name: The name of the requested Font
1405 #ifdef USE_JP_FONTSTRUCT
1406 static void Infofnt_init_data(concptr name, concptr kname)
1408 static void Infofnt_init_data(concptr name)
1414 char **missing_list;
1422 #ifdef USE_JP_FONTSTRUCT
1425 /*** Load the info Fresh, using the name ***/
1427 /* If the name is not given, report an error */
1428 if (!name || !*name) quit("Missing font!");
1430 #ifdef USE_JP_FONTSTRUCT
1431 if (!kname || !*kname) quit("Missing kanji font!");
1433 /* Attempt to load the font */
1435 info = XCreateFontSet(Metadpy->dpy, name, &missing_list, &missing_count, &default_font);
1436 if(missing_count > 0){
1437 printf("missing font(s): \n");
1438 while(missing_count-- > 0){
1439 printf("\t%s\n", missing_list[missing_count]);
1441 XFreeStringList(missing_list);
1444 info = XLoadQueryFont(Metadpy->dpy, name);
1445 #ifdef USE_JP_FONTSTRUCT
1446 kinfo = XLoadQueryFont(Metadpy->dpy, kname);
1451 /* The load failed, try to recover */
1452 if (!info) quit_fmt("Failed to find font:\"%s\"", name);
1453 #ifdef USE_JP_FONTSTRUCT
1454 if (!kinfo) quit_fmt("Failed to find font:\"%s\"", kname);
1459 /*** Init the font ***/
1461 /* Wipe the thing */
1462 (void)WIPE(Infofnt, infofnt);
1464 #ifdef USE_JP_FONTSTRUCT
1465 WIPE(Infokfnt, infofnt);
1467 /* Attempt to prepare it */
1468 #ifdef USE_JP_FONTSTRUCT
1469 if (Infofnt_prepare(info, kinfo))
1471 if (Infofnt_prepare(info))
1477 XFreeFontSet(Metadpy->dpy, info);
1479 XFreeFont(Metadpy->dpy, info);
1480 #ifdef USE_JP_FONTSTRUCT
1481 XFreeFont(Metadpy->dpy, kinfo);
1485 quit_fmt("Failed to prepare font:\"%s\"", name);
1488 /* Save a copy of the font name */
1489 Infofnt->name = string_make(name);
1490 #ifdef USE_JP_FONTSTRUCT
1491 Infokfnt->name = string_make(kname);
1494 /* Mark it as nukable */
1496 #ifdef USE_JP_FONTSTRUCT
1502 #ifdef USE_JP_FONTSTRUCT
1504 * EUC日本語コードを含む文字列を表示する (Xlib)
1507 XDrawMultiString(display,d,gc, x, y, string, len, afont,
1508 afont_width, afont_height, afont_ascent, kfont, kfont_width)
1516 int afont_width, afont_height, afont_ascent;
1523 unsigned char *endp;
1526 endp = string + len;
1528 while ( str < endp && *str ) {
1531 if ( (*str) == 0x7f ) {
1535 /* 連続する0x7Fの長さを検出 */
1537 while ( str < endp && (*str) == 0x7f ) {
1543 XFillRectangle( display, d, gc, x, y-afont_ascent,
1544 slen * afont_width, afont_height);
1547 x += afont_width * slen;
1551 if ( iskanji(*str) ) {
1555 /* 連続するUJIS文字の長さを検出 */
1557 while ( str < endp && *str && iskanji(*str) ) {
1558 kanji[slen].byte1 = *str++ & 0x7f;
1559 kanji[slen++].byte2 = *str++ & 0x7f;
1563 XSetFont( display, gc, kfont->fid );
1564 XDrawImageString16( display, d, gc, x, y, kanji, slen );
1568 x += kfont_width * slen;
1572 /* 非漢字(=ASCIIと仮定)の始まり */
1574 /* 連続するASCII文字を検出 */
1577 while ( str < endp && *str && !iskanji(*str) ) {
1579 if (*str == 0x7f)break;
1586 XSetFont( display, gc, afont->fid );
1587 XDrawImageString( display, d, gc, x, y, p, slen );
1590 x += afont_width * slen;
1599 static errr Infofnt_text_std(int x, int y, concptr str, int len)
1604 /*** Do a brief info analysis ***/
1606 /* Do nothing if the string is null */
1607 if (!str || !*str) return (-1);
1609 /* Get the length of the string */
1610 if (len < 0) len = strlen(str);
1612 /*** Decide where to place the string, vertically ***/
1614 /* Ignore Vertical Justifications */
1615 y = (y * Infofnt->hgt) + Infofnt->asc + Infowin->oy;
1618 /*** Decide where to place the string, horizontally ***/
1620 /* Line up with x at left edge of column 'x' */
1621 x = (x * Infofnt->wid) + Infowin->ox;
1624 /*** Actually draw 'str' onto the infowin ***/
1627 /* Be sure the correct font is ready */
1628 XSetFont(Metadpy->dpy, Infoclr->gc, Infofnt->info->fid);
1631 /*** Handle the fake mono we can enforce on fonts ***/
1633 /* Monotize the font */
1636 #ifdef USE_JP_FONTSTRUCT
1637 /* Be sure the correct font is ready */
1638 XSetFont(Metadpy->dpy, Infoclr->gc, Infofnt->info->fid);
1640 /* Do each character */
1641 for (i = 0; i < len; ++i)
1643 /* Note that the Infoclr is set up to contain the Infofnt */
1644 XDrawImageString(Metadpy->dpy, Infowin->win, Infoclr->gc,
1645 x + i * Infofnt->wid + Infofnt->off, y, str + i, 1);
1649 /* Assume monoospaced font */
1652 /* Note that the Infoclr is set up to contain the Infofnt */
1653 #ifdef USE_JP_FONTSTRUCT
1654 /* 漢字フォントの表示幅は ASCIIフォントの2倍に固定 */
1655 XDrawMultiString(Metadpy->dpy, Infowin->win, Infoclr->gc,
1657 Infofnt->info, Infofnt->wid, Infofnt->hgt,
1659 Infokfnt->info, Infofnt->wid * 2);
1663 iconv_t cd = iconv_open("UTF-8", "EUC-JP");
1665 size_t outlen = len * 2;
1666 char *kanji = malloc(outlen);
1667 char *sp; char *kp = kanji;
1669 my_strcpy(sbuf, str, sizeof(sbuf));
1671 iconv(cd, &sp, &inlen, &kp, &outlen);
1674 XmbDrawImageString(Metadpy->dpy, Infowin->win, Infofnt->info,
1675 Infoclr->gc, x, y, kanji, kp-kanji);
1678 XDrawImageString(Metadpy->dpy, Infowin->win, Infoclr->gc,
1692 * Painting where text would be
1694 static errr Infofnt_text_non(int x, int y, concptr str, int len)
1699 /*** Find the width ***/
1701 /* Negative length is a flag to count the characters in str */
1702 if (len < 0) len = strlen(str);
1704 /* The total width will be 'len' chars * standard width */
1705 w = len * Infofnt->wid;
1708 /*** Find the X dimensions ***/
1710 /* Line up with x at left edge of column 'x' */
1711 x = x * Infofnt->wid + Infowin->ox;
1714 /*** Find other dimensions ***/
1716 /* Simply do 'Infofnt->hgt' (a single row) high */
1719 /* Simply do "at top" in row 'y' */
1720 y = y * h + Infowin->oy;
1723 /*** Actually 'paint' the area ***/
1725 /* Just do a Fill Rectangle */
1726 XFillRectangle(Metadpy->dpy, Infowin->win, Infoclr->gc, x, y, w, h);
1734 /*************************************************************************/
1738 * Angband specific code follows... (ANGBAND)
1743 * Hack -- cursor color
1745 static infoclr *xor;
1748 * Actual color table
1750 static infoclr *clr[256];
1753 * Color info (unused, red, green, blue).
1755 static byte color_table[256][4];
1760 typedef struct term_data term_data;
1763 * A structure for each "term"
1770 #ifdef USE_JP_FONTSTRUCT
1779 /* Tempory storage for overlaying tiles. */
1785 * The number of term data structures
1787 #define MAX_TERM_DATA 8
1790 * The array of term data structures
1792 static term_data data[MAX_TERM_DATA];
1795 /* Use short names for the most commonly used elements of various structures. */
1796 #define DPY (Metadpy->dpy)
1797 #define WIN (Infowin->win)
1800 /* Describe a set of co-ordinates. */
1801 typedef struct co_ord co_ord;
1810 * A special structure to store information about the text currently
1813 typedef struct x11_selection_type x11_selection_type;
1814 struct x11_selection_type
1816 bool select; /* The selection is currently in use. */
1817 bool drawn; /* The selection is currently displayed. */
1818 term *t; /* The window where the selection is found. */
1819 co_ord init; /* The starting co-ordinates. */
1820 co_ord cur; /* The end co-ordinates (the current ones if still copying). */
1821 co_ord old; /* The previous end co-ordinates. */
1822 Time time; /* The time at which the selection was finalised. */
1825 static x11_selection_type s_ptr[1];
1829 * Process a keypress event
1831 * Also appears in "main-xaw.c".
1833 static void react_keypress(XKeyEvent *xev)
1835 int i, n, mc, ms, mo, mx;
1839 XKeyEvent *ev = (XKeyEvent*)(xev);
1847 int valid_keysym = TRUE;
1850 /* Check for "normal" keypresses */
1852 if(Focuswin && Focuswin->xic){
1854 n = XmbLookupString(Focuswin->xic, ev, buf, 125, &ks, &status);
1855 if(status == XBufferOverflow){
1856 printf("Input is too long, and dropped\n");
1859 if(status != XLookupKeySym && status != XLookupBoth){
1860 valid_keysym = FALSE;
1863 n = XLookupString(ev, buf, 125, &ks, NULL);
1866 n = XLookupString(ev, buf, 125, &ks, NULL);
1874 /* Enqueue the normal key(s) */
1875 for (i = 0; buf[i]; i++) Term_keypress(buf[i]);
1882 /* Hack -- Ignore "modifier keys" */
1883 if (IsModifierKey(ks)) return;
1886 /* Hack -- convert into an unsigned int */
1889 /* Extract four "modifier flags" */
1890 mc = (ev->state & ControlMask) ? TRUE : FALSE;
1891 ms = (ev->state & ShiftMask) ? TRUE : FALSE;
1892 mo = (ev->state & Mod1Mask) ? TRUE : FALSE;
1893 mx = (ev->state & Mod2Mask) ? TRUE : FALSE;
1896 /* Normal keys with no modifiers */
1897 if (n && !mo && !mx && !IsSpecialKey(ks))
1899 /* Enqueue the normal key(s) */
1900 for (i = 0; buf[i]; i++) Term_keypress(buf[i]);
1907 /* Handle a few standard keys (bypass modifiers) XXX XXX XXX */
1912 Term_keypress(ESCAPE);
1918 Term_keypress('\r');
1924 Term_keypress('\t');
1930 Term_keypress(0x7f);
1935 Term_keypress('\010');
1941 /* Hack -- Use the KeySym */
1944 sprintf(msg, "%c%s%s%s%s_%lX%c", 31,
1945 mc ? "N" : "", ms ? "S" : "",
1946 mo ? "O" : "", mx ? "M" : "",
1947 (unsigned long)(ks), 13);
1950 /* Hack -- Use the Keycode */
1953 sprintf(msg, "%c%s%s%s%sK_%X%c", 31,
1954 mc ? "N" : "", ms ? "S" : "",
1955 mo ? "O" : "", mx ? "M" : "",
1959 /* Enqueue the "macro trigger" string */
1960 for (i = 0; msg[i]; i++) Term_keypress(msg[i]);
1963 /* Hack -- auto-define macros as needed */
1964 if (n && (macro_find_exact(msg) < 0))
1966 /* Create a macro */
1967 macro_add(msg, buf);
1973 * Find the square a particular pixel is part of.
1975 static void pixel_to_square(int * const x, int * const y,
1976 const int ox, const int oy)
1978 (*x) = (ox - Infowin->ox) / Infofnt->wid;
1979 (*y) = (oy - Infowin->oy) / Infofnt->hgt;
1983 * Find the pixel at the top-left corner of a square.
1985 static void square_to_pixel(int * const x, int * const y,
1986 const int ox, const int oy)
1988 (*x) = ox * Infofnt->wid + Infowin->ox;
1989 (*y) = oy * Infofnt->hgt + Infowin->oy;
1993 * Convert co-ordinates from starting corner/opposite corner to minimum/maximum.
1995 static void sort_co_ord(co_ord *min, co_ord *max,
1996 const co_ord *b, const co_ord *a)
1998 min->x = MIN(a->x, b->x);
1999 min->y = MIN(a->y, b->y);
2000 max->x = MAX(a->x, b->x);
2001 max->y = MAX(a->y, b->y);
2005 * Remove the selection by redrawing it.
2007 static void mark_selection_clear(int x1, int y1, int x2, int y2)
2009 Term_redraw_section(x1,y1,x2,y2);
2013 * Select an area by drawing a grey box around it.
2014 * NB. These two functions can cause flicker as the selection is modified,
2015 * as the game redraws the entire marked section.
2017 static void mark_selection_mark(int x1, int y1, int x2, int y2)
2019 square_to_pixel(&x1, &y1, x1, y1);
2020 square_to_pixel(&x2, &y2, x2, y2);
2021 XDrawRectangle(Metadpy->dpy, Infowin->win, clr[2]->gc, x1, y1,
2022 x2-x1+Infofnt->wid - 1, y2-y1+Infofnt->hgt - 1);
2026 * Mark a selection by drawing boxes around it (for now).
2028 static void mark_selection(void)
2032 bool draw = s_ptr->select;
2033 bool clear = s_ptr->drawn;
2035 /* Open the correct term if necessary. */
2036 if (s_ptr->t != old) Term_activate(s_ptr->t);
2040 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->old);
2041 mark_selection_clear(min.x, min.y, max.x, max.y);
2045 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->cur);
2046 mark_selection_mark(min.x, min.y, max.x, max.y);
2049 /* Finish on the current term. */
2050 if (s_ptr->t != old) Term_activate(old);
2052 s_ptr->old.x = s_ptr->cur.x;
2053 s_ptr->old.y = s_ptr->cur.y;
2054 s_ptr->drawn = s_ptr->select;
2058 * Forget a selection for one reason or another.
2060 static void copy_x11_release(void)
2062 /* Deselect the current selection. */
2063 s_ptr->select = FALSE;
2065 /* Remove its graphical represesntation. */
2070 * Start to select some text on the screen.
2072 static void copy_x11_start(int x, int y)
2074 if (s_ptr->select) copy_x11_release();
2076 /* Remember where the selection started. */
2078 s_ptr->init.x = s_ptr->cur.x = s_ptr->old.x = x;
2079 s_ptr->init.y = s_ptr->cur.y = s_ptr->old.y = y;
2083 * Respond to movement of the mouse when selecting text.
2085 static void copy_x11_cont(int x, int y, unsigned int buttons)
2087 /* Use the nearest square within bounds if the mouse is outside. */
2088 x = MIN(MAX(x, 0), Term->wid-1);
2089 y = MIN(MAX(y, 0), Term->hgt-1);
2091 /* The left mouse button isn't pressed. */
2092 if (~buttons & Button1Mask) return;
2094 /* Not a selection in this window. */
2095 if (s_ptr->t != Term) return;
2097 /* Not enough movement. */
2098 if (x == s_ptr->old.x && y == s_ptr->old.y && s_ptr->select) return;
2100 /* Something is being selected. */
2101 s_ptr->select = TRUE;
2103 /* Track the selection. */
2107 /* Hack - display it inefficiently. */
2112 * Respond to release of the left mouse button by putting the selected text in
2113 * the primary buffer.
2115 static void copy_x11_end(const Time time)
2118 if (!s_ptr->select) return;
2120 /* Not a selection in this window. */
2121 if (s_ptr->t != Term) return;
2123 /* Remember when the selection was finalised. */
2126 /* Acquire the primary selection. */
2127 XSetSelectionOwner(Metadpy->dpy, XA_PRIMARY, Infowin->win, time);
2128 if (XGetSelectionOwner(Metadpy->dpy, XA_PRIMARY) != Infowin->win)
2130 /* Failed to acquire the selection, so forget it. */
2131 /* bell("Failed to acquire primary buffer."); */
2132 s_ptr->select = FALSE;
2138 static Atom xa_targets, xa_timestamp, xa_text, xa_compound_text;
2141 * Set the required variable atoms at start-up to avoid errors later.
2143 static void set_atoms(void)
2145 xa_targets = XInternAtom(DPY, "TARGETS", False);
2146 xa_timestamp = XInternAtom(DPY, "TIMESTAMP", False);
2147 xa_text = XInternAtom(DPY, "TEXT", False);
2148 xa_compound_text = XInternAtom(DPY, "COMPOUND_TEXT", False);
2152 static Atom request_target = 0;
2155 * Send a message to request that the PRIMARY buffer be sent here.
2157 static void paste_x11_request(Atom target, const Time time)
2160 * It's from some sample programs on the web.
2161 * What does it mean? -- XXX
2163 Atom property = XInternAtom(DPY, "__COPY_TEXT", False);
2165 /* Check the owner. */
2166 if (XGetSelectionOwner(DPY, XA_PRIMARY) == None)
2169 /* bell("No selection found."); */
2173 request_target = target;
2175 /* Request the event */
2176 XConvertSelection(DPY, XA_PRIMARY, target, property, WIN, time);
2181 * Add the contents of the PRIMARY buffer to the input queue.
2183 * Hack - This doesn't use the "time" of the event, and so accepts anything a
2184 * client tries to send it.
2186 static void paste_x11_accept(const XSelectionEvent *ptr)
2189 const long offset = 0;
2190 const long length = 32000;
2191 XTextProperty xtextproperty;
2195 * It's from some sample programs on the web.
2196 * What does it mean? -- XXX
2198 Atom property = XInternAtom(DPY, "__COPY_TEXT", False);
2202 if (ptr->property == None)
2204 if (request_target == xa_compound_text)
2206 /* Re-request as STRING */
2207 paste_x11_request(XA_STRING, ptr->time);
2212 plog("Paste failure (remote client could not send).");
2217 if (ptr->selection != XA_PRIMARY)
2219 plog("Paste failure (remote client did not send primary selection).");
2223 if (ptr->target != request_target)
2225 plog("Paste failure (selection in unknown format).");
2230 if (XGetWindowProperty(Metadpy->dpy, Infowin->win, property, offset,
2231 length, TRUE, request_target,
2232 &xtextproperty.encoding,
2233 &xtextproperty.format,
2234 &xtextproperty.nitems,
2236 &xtextproperty.value)
2243 if (request_target == xa_compound_text)
2248 XmbTextPropertyToTextList(DPY, &xtextproperty, &list, &count);
2254 for (i = 0; i < count; i++)
2256 /* Paste the text. */
2257 err = type_string(list[i], 0);
2262 /* Free the string */
2263 XFreeStringList(list);
2266 else /* if (request_target == XA_STRING) */
2268 /* Paste the text. */
2269 err = type_string((char *)xtextproperty.value, xtextproperty.nitems);
2272 /* Free the data pasted. */
2273 XFree(xtextproperty.value);
2278 plog("Paste failure (too much text selected).");
2284 * Add a character to a string in preparation for sending it to another
2285 * client as a STRING.
2286 * This doesn't change anything, as clients tend not to have difficulty in
2287 * receiving this format (although the standard specifies a restricted set).
2288 * Strings do not have a colour.
2290 static bool paste_x11_send_text(XSelectionRequestEvent *rq)
2300 /* Too old, or incorrect call. */
2301 if (rq->time < s_ptr->time) return FALSE;
2303 /* Work out which way around to paste. */
2304 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->cur);
2307 if (XGetSelectionOwner(DPY, XA_PRIMARY) != WIN)
2309 /* bell("Someone stole my selection!"); */
2313 /* Delete the old value of the property. */
2314 XDeleteProperty(DPY, rq->requestor, rq->property);
2316 for (n = 0, y = 0; y < Term->hgt; y++)
2321 if (y < min.y) continue;
2322 if (y > max.y) break;
2324 for (l = 0, x = 0; x < Term->wid; x++)
2327 if (x > max.x) break;
2329 /* Find the character. */
2330 Term_what(x, y, &a, &c);
2332 if (1 == kanji) kanji = 2;
2333 else if (iskanji(c)) kanji = 1;
2336 if (x < min.x) continue;
2339 * A single kanji character was divided in two...
2340 * Delete the garbage.
2342 if ((2 == kanji && x == min.x) ||
2343 (1 == kanji && x == max.x))
2346 if (x > max.x) break;
2347 if (x < min.x) continue;
2349 /* Find the character. */
2350 Term_what(x, y, &a, &c);
2358 /* Ignore trailing spaces */
2359 while (buf[l-1] == ' ') l--;
2361 /* Terminate all line unless it's single line. */
2371 list[n++] = (char *)string_make(buf);
2374 /* End of the list */
2378 if (rq->target == XA_STRING)
2380 for (n = 0; list[n]; n++)
2382 /* Send the (non-empty) string. */
2383 XChangeProperty(DPY, rq->requestor, rq->property, rq->target, 8,
2384 PropModeAppend, (unsigned char *)list[n], strlen(list[n]));
2388 else if (rq->target == xa_text ||
2389 rq->target == xa_compound_text)
2391 XTextProperty text_prop;
2392 XICCEncodingStyle style;
2394 if (rq->target == xa_text)
2395 style = XStdICCTextStyle;
2396 else /* if (rq->target == xa_compound_text) */
2397 style = XCompoundTextStyle;
2400 XmbTextListToTextProperty(DPY, list, n, style, &text_prop))
2402 /* Send the compound text */
2403 XChangeProperty(DPY,
2412 /* Free the data. */
2413 XFree(text_prop.value);
2417 /* Free the list of strings */
2418 for (n = 0; list[n]; n++)
2420 string_free(list[n]);
2427 * Send some text requested by another X client.
2429 static void paste_x11_send(XSelectionRequestEvent *rq)
2432 XSelectionEvent *ptr = &(event.xselection);
2434 /* Set the event parameters. */
2435 ptr->type = SelectionNotify;
2436 ptr->property = rq->property;
2437 ptr->display = rq->display;
2438 ptr->requestor = rq->requestor;
2439 ptr->selection = rq->selection;
2440 ptr->target = rq->target;
2441 ptr->time = rq->time;
2443 /* Paste the appropriate information for each target type.
2444 * Note that this currently rejects MULTIPLE targets.
2447 if (rq->target == XA_STRING ||
2448 rq->target == xa_text ||
2449 rq->target == xa_compound_text)
2451 if (!paste_x11_send_text(rq))
2452 ptr->property = None;
2454 else if (rq->target == xa_targets)
2456 Atom target_list[4];
2457 target_list[0] = XA_STRING;
2458 target_list[1] = xa_text;
2459 target_list[2] = xa_compound_text;
2460 target_list[3] = xa_targets;
2461 XChangeProperty(DPY, rq->requestor, rq->property, rq->target,
2462 (8 * sizeof(target_list[0])), PropModeReplace,
2463 (unsigned char *)target_list,
2464 (sizeof(target_list) / sizeof(target_list[0])));
2466 else if (rq->target == xa_timestamp)
2468 XChangeProperty(DPY, rq->requestor, rq->property, rq->target,
2469 (8 * sizeof(Time)), PropModeReplace,
2470 (unsigned char *)s_ptr->time, 1);
2474 ptr->property = None;
2477 /* Send whatever event we're left with. */
2478 XSendEvent(DPY, rq->requestor, FALSE, NoEventMask, &event);
2483 * Handle various events conditional on presses of a mouse button.
2485 static void handle_button(Time time, int x, int y, int button,
2488 /* The co-ordinates are only used in Angband format. */
2489 pixel_to_square(&x, &y, x, y);
2491 if (press && button == 1) copy_x11_start(x, y);
2492 if (!press && button == 1) copy_x11_end(time);
2493 if (!press && button == 2) paste_x11_request(xa_compound_text, time);
2500 static errr CheckEvent(bool wait)
2502 term_data *old_td = (term_data*)(Term->data);
2504 XEvent xev_body, *xev = &xev_body;
2506 term_data *td = NULL;
2507 infowin *iwin = NULL;
2515 /* Do not wait unless requested */
2516 if (!wait && !XPending(Metadpy->dpy)) return (1);
2519 * Hack - redraw the selection, if needed.
2520 * This doesn't actually check that one of its squares was drawn to,
2521 * only that this may have happened.
2523 if (s_ptr->select && !s_ptr->drawn) mark_selection();
2525 /* Load the Event */
2526 XNextEvent(Metadpy->dpy, xev);
2529 /* #define DEBUG_EVENT */
2532 printf("event: type=%d", xev->type);
2535 printf("(KeyPress), keycode=%X", xev->xkey.keycode);
2538 printf("(FocusIn)");
2541 printf("(FocusOut)");
2543 case ReparentNotify:
2544 printf("(ReparentNotify)");
2546 case ConfigureNotify:
2547 printf("(ConfigureNotify)");
2550 printf("(MapNotify)");
2556 printf("(ClientMessage)");
2562 if (XFilterEvent(xev, xev->xany.window)
2563 /*XFilterEvent(xev, (data[0].win)->win)*/){
2565 printf(", [filtered by IM]\n");
2567 goto redo_checkevent;
2574 /* Notice new keymaps */
2575 if (xev->type == MappingNotify)
2577 XRefreshKeyboardMapping(&xev->xmapping);
2582 /* Scan the windows */
2583 for (i = 0; i < MAX_TERM_DATA; i++)
2585 if (!data[i].win) continue;
2586 if (xev->xany.window == data[i].win->win)
2594 /* Unknown window */
2595 if (!td || !iwin) return (0);
2598 /* Hack -- activate the Term */
2599 Term_activate(&td->t);
2601 /* Hack -- activate the window */
2605 /* Switch on the Type */
2611 bool press = (xev->type == ButtonPress);
2613 /* Where is the mouse */
2614 int x = xev->xbutton.x;
2615 int y = xev->xbutton.y;
2619 /* Which button is involved */
2620 if (xev->xbutton.button == Button1) z = 1;
2621 else if (xev->xbutton.button == Button2) z = 2;
2622 else if (xev->xbutton.button == Button3) z = 3;
2623 else if (xev->xbutton.button == Button4) z = 4;
2624 else if (xev->xbutton.button == Button5) z = 5;
2628 handle_button(xev->xbutton.time, x, y, z, press);
2643 /* Where is the mouse */
2644 int x = xev->xmotion.x;
2645 int y = xev->xmotion.y;
2646 unsigned int z = xev->xmotion.state;
2648 /* Convert to co-ordinates Angband understands. */
2649 pixel_to_square(&x, &y, x, y);
2651 /* Highlight the current square, if appropriate. */
2652 /* highlight_square(window, y, x); */
2654 /* Alter the selection if appropriate. */
2655 copy_x11_cont(x, y, z);
2662 case SelectionNotify:
2664 paste_x11_accept(&(xev->xselection));
2668 case SelectionRequest:
2670 paste_x11_send(&(xev->xselectionrequest));
2674 case SelectionClear:
2676 s_ptr->select = FALSE;
2689 /* Hack -- use "old" term */
2690 Term_activate(&old_td->t);
2692 /* Process the key */
2693 react_keypress(&(xev->xkey));
2702 /* Ignore "extra" exposes */
2703 /*if (xev->xexpose.count) break;*/
2705 /* Clear the window */
2708 x1 = (xev->xexpose.x - Infowin->ox)/Infofnt->wid;
2709 x2 = (xev->xexpose.x + xev->xexpose.width -
2710 Infowin->ox)/Infofnt->wid;
2712 y1 = (xev->xexpose.y - Infowin->oy)/Infofnt->hgt;
2713 y2 = (xev->xexpose.y + xev->xexpose.height -
2714 Infowin->oy)/Infofnt->hgt;
2716 Term_redraw_section(x1, y1, x2, y2);
2726 Infowin->mapped = 1;
2727 Term->mapped_flag = TRUE;
2733 Infowin->mapped = 0;
2734 Term->mapped_flag = FALSE;
2738 /* Move and/or Resize */
2739 case ConfigureNotify:
2741 int cols, rows, wid, hgt;
2743 int ox = Infowin->ox;
2744 int oy = Infowin->oy;
2746 /* Save the new Window Parms */
2747 Infowin->x = xev->xconfigure.x;
2748 Infowin->y = xev->xconfigure.y;
2749 Infowin->w = xev->xconfigure.width;
2750 Infowin->h = xev->xconfigure.height;
2752 /* Determine "proper" number of rows/cols */
2753 cols = ((Infowin->w - (ox + ox)) / td->fnt->wid);
2754 rows = ((Infowin->h - (oy + oy)) / td->fnt->hgt);
2756 /* Hack -- minimal size */
2757 if (cols < 1) cols = 1;
2758 if (rows < 1) rows = 1;
2762 /* Hack the main window must be at least 80x24 */
2763 if (cols < 80) cols = 80;
2764 if (rows < 24) rows = 24;
2767 /* Desired size of window */
2768 wid = cols * td->fnt->wid + (ox + ox);
2769 hgt = rows * td->fnt->hgt + (oy + oy);
2771 /* Resize the Term (if needed) */
2772 Term_resize(cols, rows);
2774 /* Resize the windows if any "change" is needed */
2775 if ((Infowin->w != wid) || (Infowin->h != hgt))
2778 Infowin_set(td->win);
2779 Infowin_resize(wid, hgt);
2788 XSetICFocus(iwin->xic);
2796 XUnsetICFocus(iwin->xic);
2798 /* Focuswin = NULL;*/
2805 /* Hack -- Activate the old term */
2806 Term_activate(&old_td->t);
2808 /* Hack -- Activate the proper window */
2809 Infowin_set(old_td->win);
2818 * Standard sound names
2820 static const concptr angband_sound_name[SOUND_MAX] =
2892 * An array of sound file names
2894 static concptr sound_file[SOUND_MAX];
2897 * Check for existance of a file
2899 static bool check_file(concptr s)
2903 fff = fopen(s, "r");
2904 if (!fff) return (FALSE);
2913 static void init_sound(void)
2918 char dir_xtra_sound[1024];
2920 /* Build the "sound" path */
2921 path_build(dir_xtra_sound, sizeof(dir_xtra_sound), ANGBAND_DIR_XTRA, "sound");
2923 /* Prepare the sounds */
2924 for (i = 1; i < SOUND_MAX; i++)
2926 /* Extract name of sound file */
2927 sprintf(wav, "%s.wav", angband_sound_name[i]);
2929 /* Access the sound */
2930 path_build(buf, sizeof(buf), dir_xtra_sound, wav);
2932 /* Save the sound filename, if it exists */
2933 if (check_file(buf)) sound_file[i] = string_make(buf);
2940 * Hack -- make a sound
2942 static errr Term_xtra_x11_sound(int v)
2946 /* Sound disabled */
2947 if (!use_sound) return (1);
2950 if ((v < 0) || (v >= SOUND_MAX)) return (1);
2953 if (!sound_file[v]) return (1);
2955 sprintf(buf,"./playwave.sh %s\n", sound_file[v]);
2957 return (system(buf) < 0);
2963 * Handle "activation" of a term
2965 static errr Term_xtra_x11_level(int v)
2967 term_data *td = (term_data*)(Term->data);
2969 /* Handle "activate" */
2972 /* Activate the window */
2973 Infowin_set(td->win);
2975 /* Activate the font */
2976 Infofnt_set(td->fnt);
2977 #ifdef USE_JP_FONTSTRUCT
2978 Infokfnt_set(td->kfnt);
2990 static errr Term_xtra_x11_react(void)
2996 /* Check the colors */
2997 for (i = 0; i < 256; i++)
2999 if ((color_table[i][0] != angband_color_table[i][0]) ||
3000 (color_table[i][1] != angband_color_table[i][1]) ||
3001 (color_table[i][2] != angband_color_table[i][2]) ||
3002 (color_table[i][3] != angband_color_table[i][3]))
3006 /* Save new values */
3007 color_table[i][0] = angband_color_table[i][0];
3008 color_table[i][1] = angband_color_table[i][1];
3009 color_table[i][2] = angband_color_table[i][2];
3010 color_table[i][3] = angband_color_table[i][3];
3013 pixel = create_pixel(Metadpy->dpy,
3018 /* Change the foreground */
3019 Infoclr_set(clr[i]);
3020 Infoclr_change_fg(pixel);
3031 * Handle a "special request"
3033 static errr Term_xtra_x11(int n, int v)
3035 /* Handle a subset of the legal requests */
3039 case TERM_XTRA_NOISE: Metadpy_do_beep(); return (0);
3041 /* Make a special sound */
3042 case TERM_XTRA_SOUND: return (Term_xtra_x11_sound(v));
3044 /* Flush the output XXX XXX */
3045 case TERM_XTRA_FRESH: Metadpy_update(1, 0, 0); return (0);
3047 /* Process random events XXX */
3048 case TERM_XTRA_BORED: return (CheckEvent(0));
3050 /* Process Events XXX */
3051 case TERM_XTRA_EVENT: return (CheckEvent(v));
3053 /* Flush the events XXX */
3054 case TERM_XTRA_FLUSH: while (!CheckEvent(FALSE)); return (0);
3056 /* Handle change in the "level" */
3057 case TERM_XTRA_LEVEL: return (Term_xtra_x11_level(v));
3059 /* Clear the screen */
3060 case TERM_XTRA_CLEAR: Infowin_wipe(); s_ptr->drawn = FALSE; return (0);
3062 /* Delay for some milliseconds */
3063 case TERM_XTRA_DELAY: usleep(1000 * v); return (0);
3065 /* React to changes */
3066 case TERM_XTRA_REACT: return (Term_xtra_x11_react());
3075 * Draw the cursor as an inverted rectangle.
3077 * Consider a rectangular outline like "main-mac.c". XXX XXX
3079 static errr Term_curs_x11(int x, int y)
3083 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
3084 x * Infofnt->wid + Infowin->ox,
3085 y * Infofnt->hgt + Infowin->oy,
3086 Infofnt->wid - 1, Infofnt->hgt - 1);
3087 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
3088 x * Infofnt->wid + Infowin->ox + 1,
3089 y * Infofnt->hgt + Infowin->oy + 1,
3090 Infofnt->wid - 3, Infofnt->hgt - 3);
3094 /* Draw the cursor */
3097 /* Hilite the cursor character */
3098 Infofnt_text_non(x, y, " ", 1);
3107 * Draw the double width cursor
3109 static errr Term_bigcurs_x11(int x, int y)
3113 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
3114 x * Infofnt->wid + Infowin->ox,
3115 y * Infofnt->hgt + Infowin->oy,
3116 Infofnt->twid - 1, Infofnt->hgt - 1);
3117 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
3118 x * Infofnt->wid + Infowin->ox + 1,
3119 y * Infofnt->hgt + Infowin->oy + 1,
3120 Infofnt->twid - 3, Infofnt->hgt - 3);
3124 /* Draw the cursor */
3127 /* Hilite the cursor character */
3128 Infofnt_text_non(x, y, " ", 2);
3136 * Erase some characters.
3138 static errr Term_wipe_x11(int x, int y, int n)
3140 /* Erase (use black) */
3141 Infoclr_set(clr[TERM_DARK]);
3143 /* Mega-Hack -- Erase some space */
3144 Infofnt_text_non(x, y, "", n);
3146 /* Redraw the selection if any, as it may have been obscured. (later) */
3147 s_ptr->drawn = FALSE;
3155 * Draw some textual characters.
3157 static errr Term_text_x11(TERM_LEN x, TERM_LEN y, int n, TERM_COLOR a, concptr s)
3160 Infoclr_set(clr[a]);
3163 Infofnt_text_std(x, y, s, n);
3165 /* Redraw the selection if any, as it may have been obscured. (later) */
3166 s_ptr->drawn = FALSE;
3174 * Draw some graphical characters.
3176 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)
3189 unsigned long pixel, blank;
3191 term_data *td = (term_data*)(Term->data);
3196 /* Add in affect of window boundaries */
3200 for (i = 0; i < n; ++i, x += td->fnt->wid)
3205 /* For extra speed - cache these values */
3206 x1 = (c&0x7F) * td->fnt->twid;
3207 y1 = (a&0x7F) * td->fnt->hgt;
3209 /* Illegal tile index */
3210 if (td->tiles->width < x1 + td->fnt->wid ||
3211 td->tiles->height < y1 + td->fnt->hgt)
3213 /* Draw black square */
3214 XFillRectangle(Metadpy->dpy, td->win->win, clr[0]->gc,
3216 td->fnt->twid, td->fnt->hgt);
3218 /* Skip drawing tile */
3225 /* For extra speed - cache these values */
3226 x2 = (tc&0x7F) * td->fnt->twid;
3227 y2 = (ta&0x7F) * td->fnt->hgt;
3229 /* Optimise the common case */
3230 if (((x1 == x2) && (y1 == y2)) ||
3231 !(((byte)ta & 0x80) && ((byte)tc & 0x80)) ||
3232 td->tiles->width < x2 + td->fnt->wid ||
3233 td->tiles->height < y2 + td->fnt->hgt)
3235 /* Draw object / terrain */
3236 XPutImage(Metadpy->dpy, td->win->win,
3241 td->fnt->twid, td->fnt->hgt);
3246 /* Mega Hack^2 - assume the top left corner is "black" */
3247 blank = XGetPixel(td->tiles, 0, td->fnt->hgt * 6);
3249 for (k = 0; k < td->fnt->twid; k++)
3251 for (l = 0; l < td->fnt->hgt; l++)
3253 /* If mask set... */
3254 if ((pixel = XGetPixel(td->tiles, x1 + k, y1 + l)) == blank)
3256 /* Output from the terrain */
3257 pixel = XGetPixel(td->tiles, x2 + k, y2 + l);
3260 /* Store into the temp storage. */
3261 XPutPixel(td->TmpImage, k, l, pixel);
3266 /* Draw to screen */
3268 XPutImage(Metadpy->dpy, td->win->win,
3272 td->fnt->twid, td->fnt->hgt);
3276 /* Redraw the selection if any, as it may have been obscured. (later) */
3277 s_ptr->drawn = FALSE;
3284 static void IMDestroyCallback(XIM, XPointer, XPointer);
3287 IMInstantiateCallback(Display *display, XPointer unused1, XPointer unused2)
3290 XIMCallback ximcallback;
3291 XIMStyles *xim_styles = NULL;
3298 xim = XOpenIM(display, NULL, NULL, NULL);
3300 printf("can't open IM\n");
3304 /* initialize destroy callback */
3305 ximcallback.callback = IMDestroyCallback;
3306 ximcallback.client_data = NULL;
3307 XSetIMValues(xim, XNDestroyCallback, &ximcallback, NULL);
3309 /* set style (only "Root" is supported yet...) */
3310 XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL);
3311 for (i = 0; i < xim_styles->count_styles; i++){
3312 if(xim_styles->supported_styles[i] == (XIMPreeditNothing | XIMStatusNothing)) break;
3314 if(i >= xim_styles->count_styles){
3315 printf("Sorry, your IM does not support 'Root' preedit style...\n");
3323 for (i = 0; i < MAX_TERM_DATA; i++)
3325 infowin *iwin = data[i].win;
3326 if (!iwin) continue;
3327 iwin->xic = XCreateIC(xim, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing), XNClientWindow, iwin->win, XNFocusWindow, iwin->win, NULL);
3329 printf("Can't create input context for Term%d\n", i);
3332 if(XGetICValues(iwin->xic, XNFilterEvents, &iwin->xic_mask, NULL) != NULL){
3333 /* printf("Can't get XNFilterEvents\n"); */
3334 iwin->xic_mask = 0L;
3336 XSelectInput(Metadpy->dpy, iwin->win, iwin->mask | iwin->xic_mask);
3342 static void IMDestroyCallback(XIM xim, XPointer client_data, XPointer call_data)
3350 if (call_data == NULL){
3351 XRegisterIMInstantiateCallback(Metadpy->dpy, NULL, NULL, NULL, IMInstantiateCallback, NULL);
3354 for(i = 0; i < MAX_TERM_DATA; i++)
3356 infowin *iwin = data[i].win;
3359 XSelectInput(Metadpy->dpy, iwin->win, iwin->mask);
3360 iwin->xic_mask = 0L;
3365 Metadpy->xim = NULL;
3370 * Initialize a term_data
3372 static errr term_data_init(term_data *td, int i)
3376 concptr name = angband_term_name[i];
3379 #ifdef USE_JP_FONTSTRUCT
3411 /* Window specific font name */
3412 sprintf(buf, "ANGBAND_X11_FONT_%d", i);
3414 /* Check environment for that font */
3417 /* Check environment for "base" font */
3418 if (!font) font = getenv("ANGBAND_X11_FONT");
3420 /* No environment variables, use default font */
3427 font = DEFAULT_X11_FONT_0;
3432 font = DEFAULT_X11_FONT_1;
3437 font = DEFAULT_X11_FONT_2;
3442 font = DEFAULT_X11_FONT_3;
3447 font = DEFAULT_X11_FONT_4;
3452 font = DEFAULT_X11_FONT_5;
3457 font = DEFAULT_X11_FONT_6;
3462 font = DEFAULT_X11_FONT_7;
3467 font = DEFAULT_X11_FONT;
3472 #ifdef USE_JP_FONTSTRUCT
3473 /* Window specific font name */
3474 sprintf(buf, "ANGBAND_X11_KFONT_%d", i);
3476 /* Check environment for that font */
3477 kfont = getenv(buf);
3479 /* Check environment for "base" font */
3480 if (!kfont) kfont = getenv("ANGBAND_X11_KFONT");
3482 /* No environment variables, use default font */
3489 kfont = DEFAULT_X11_KFONT_0;
3494 kfont = DEFAULT_X11_KFONT_1;
3499 kfont = DEFAULT_X11_KFONT_2;
3504 kfont = DEFAULT_X11_KFONT_3;
3509 kfont = DEFAULT_X11_KFONT_4;
3514 kfont = DEFAULT_X11_KFONT_5;
3519 kfont = DEFAULT_X11_KFONT_6;
3524 kfont = DEFAULT_X11_KFONT_7;
3529 kfont = DEFAULT_X11_KFONT;
3534 /* Window specific location (x) */
3535 sprintf(buf, "ANGBAND_X11_AT_X_%d", i);
3537 x = (str != NULL) ? atoi(str) : -1;
3539 /* Window specific location (y) */
3540 sprintf(buf, "ANGBAND_X11_AT_Y_%d", i);
3542 y = (str != NULL) ? atoi(str) : -1;
3545 /* Window specific cols */
3546 sprintf(buf, "ANGBAND_X11_COLS_%d", i);
3548 val = (str != NULL) ? atoi(str) : -1;
3549 if (val > 0) cols = val;
3551 /* Window specific rows */
3552 sprintf(buf, "ANGBAND_X11_ROWS_%d", i);
3554 val = (str != NULL) ? atoi(str) : -1;
3555 if (val > 0) rows = val;
3557 /* Hack the main window must be at least 80x24 */
3560 if (cols < 80) cols = 80;
3561 if (rows < 24) rows = 24;
3564 /* Window specific inner border offset (ox) */
3565 sprintf(buf, "ANGBAND_X11_IBOX_%d", i);
3567 val = (str != NULL) ? atoi(str) : -1;
3568 if (val > 0) ox = val;
3570 /* Window specific inner border offset (oy) */
3571 sprintf(buf, "ANGBAND_X11_IBOY_%d", i);
3573 val = (str != NULL) ? atoi(str) : -1;
3574 if (val > 0) oy = val;
3577 /* Prepare the standard font */
3578 #ifdef USE_JP_FONTSTRUCT
3579 MAKE(td->fnt, infofnt);
3580 Infofnt_set(td->fnt);
3581 MAKE(td->kfnt, infofnt);
3582 Infokfnt_set(td->kfnt);
3583 Infofnt_init_data(font, kfont);
3585 MAKE(td->fnt, infofnt);
3586 Infofnt_set(td->fnt);
3587 Infofnt_init_data(font);
3591 /* Hack -- key buffer size */
3592 num = ((i == 0) ? 1024 : 16);
3594 /* Assume full size windows */
3595 wid = cols * td->fnt->wid + (ox + ox);
3596 hgt = rows * td->fnt->hgt + (oy + oy);
3598 /* Create a top-window */
3599 MAKE(td->win, infowin);
3600 Infowin_set(td->win);
3601 Infowin_init_top(x, y, wid, hgt, 0,
3602 Metadpy->fg, Metadpy->bg);
3604 /* Ask for certain events */
3605 #if defined(USE_XIM)
3606 Infowin_set_mask(ExposureMask | StructureNotifyMask | KeyPressMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask);
3608 Infowin_set_mask(ExposureMask | StructureNotifyMask | KeyPressMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask);
3611 /* Set the window name */
3612 Infowin_set_name(name);
3614 /* Save the inner border */
3618 /* Make Class Hints */
3619 ch = XAllocClassHint();
3621 if (ch == NULL) quit("XAllocClassHint failed");
3623 strcpy(res_name, name);
3624 res_name[0] = FORCELOWER(res_name[0]);
3625 ch->res_name = res_name;
3627 strcpy(res_class, "Angband");
3628 ch->res_class = res_class;
3630 XSetClassHint(Metadpy->dpy, Infowin->win, ch);
3632 /* Make Size Hints */
3633 sh = XAllocSizeHints();
3636 if (sh == NULL) quit("XAllocSizeHints failed");
3638 /* Main window has a differing minimum size */
3641 /* Main window min size is 80x24 */
3642 sh->flags = PMinSize | PMaxSize;
3643 sh->min_width = 80 * td->fnt->wid + (ox + ox);
3644 sh->min_height = 24 * td->fnt->hgt + (oy + oy);
3645 sh->max_width = 255 * td->fnt->wid + (ox + ox);
3646 sh->max_height = 255 * td->fnt->hgt + (oy + oy);
3649 /* Other windows can be shrunk to 1x1 */
3653 sh->flags = PMinSize | PMaxSize;
3654 sh->min_width = td->fnt->wid + (ox + ox);
3655 sh->min_height = td->fnt->hgt + (oy + oy);
3656 sh->max_width = 256 * td->fnt->wid + (ox + ox);
3657 sh->max_height = 256 * td->fnt->hgt + (oy + oy);
3660 /* Resize increment */
3661 sh->flags |= PResizeInc;
3662 sh->width_inc = td->fnt->wid;
3663 sh->height_inc = td->fnt->hgt;
3665 /* Base window size */
3666 sh->flags |= PBaseSize;
3667 sh->base_width = (ox + ox);
3668 sh->base_height = (oy + oy);
3670 /* Use the size hints */
3671 XSetWMNormalHints(Metadpy->dpy, Infowin->win, sh);
3673 /* Map the window */
3678 wh = XAllocWMHints();
3679 if(wh == NULL) quit("XAllocWMHints failed");
3680 wh->flags = InputHint;
3682 XSetWMHints(Metadpy->dpy, Infowin->win, wh);
3685 /* Move the window to requested location */
3686 if ((x >= 0) && (y >= 0)) Infowin_impell(x, y);
3689 /* Initialize the term */
3690 term_init(t, cols, rows, num);
3692 /* Use a "soft" cursor */
3693 t->soft_cursor = TRUE;
3695 /* Erase with "white space" */
3696 t->attr_blank = TERM_WHITE;
3697 t->char_blank = ' ';
3700 t->xtra_hook = Term_xtra_x11;
3701 t->curs_hook = Term_curs_x11;
3702 t->bigcurs_hook = Term_bigcurs_x11;
3703 t->wipe_hook = Term_wipe_x11;
3704 t->text_hook = Term_text_x11;
3709 /* Activate (important) */
3718 * Initialization function for an "X11" module to Angband
3720 errr init_x11(int argc, char *argv[])
3724 concptr dpy_name = "";
3728 char filename[1024];
3736 for (i = 1; i < argc; i++)
3738 if (prefix(argv[i], "-d"))
3740 dpy_name = &argv[i][2];
3744 if (prefix(argv[i], "-s"))
3746 smoothRescaling = FALSE;
3750 if (prefix(argv[i], "-a"))
3752 arg_graphics = GRAPHICS_ADAM_BOLT;
3756 if (prefix(argv[i], "-o"))
3758 arg_graphics = GRAPHICS_ORIGINAL;
3762 if (prefix(argv[i], "-b"))
3764 arg_bigtile = use_bigtile = TRUE;
3768 if (prefix(argv[i], "-n"))
3770 num_term = atoi(&argv[i][2]);
3771 if (num_term > MAX_TERM_DATA) num_term = MAX_TERM_DATA;
3772 else if (num_term < 1) num_term = 1;
3776 if (prefix(argv[i], "--"))
3782 plog_fmt("Ignoring option: %s", argv[i]);
3788 /* Get locale information from environment variables */
3789 setlocale(LC_ALL, "");
3791 #ifdef DEFAULT_LOCALE
3792 if(!strcmp(setlocale(LC_ALL, NULL), "C")){
3793 printf("try default locale \"%s\"\n", DEFAULT_LOCALE);
3794 setlocale(LC_ALL, DEFAULT_LOCALE);
3798 if(!strcmp(setlocale(LC_ALL, NULL), "C"))
3800 printf("WARNING: Locale is not supported. Non-english font may be displayed incorrectly.\n");
3803 if(!XSupportsLocale()){
3804 printf("can't support locale in X\n");
3805 setlocale(LC_ALL, "C");
3808 /* Set locale to "C" without using environment variables */
3809 setlocale(LC_ALL, "C");
3812 #endif /* USE_LOCALE */
3815 /* Init the Metadpy if possible */
3816 if (Metadpy_init_name(dpy_name)) return (-1);
3819 /* Prepare cursor color */
3822 Infoclr_init_ppn(Metadpy->fg, Metadpy->bg, "xor", 0);
3825 /* Prepare normal colors */
3826 for (i = 0; i < 256; ++i)
3830 MAKE(clr[i], infoclr);
3832 Infoclr_set(clr[i]);
3834 /* Acquire Angband colors */
3835 color_table[i][0] = angband_color_table[i][0];
3836 color_table[i][1] = angband_color_table[i][1];
3837 color_table[i][2] = angband_color_table[i][2];
3838 color_table[i][3] = angband_color_table[i][3];
3840 /* Default to monochrome */
3841 pixel = ((i == 0) ? Metadpy->bg : Metadpy->fg);
3847 pixel = create_pixel(Metadpy->dpy,
3853 /* Initialize the color */
3854 Infoclr_init_ppn(pixel, Metadpy->bg, "cpy", 0);
3858 /* Prepare required atoms. */
3862 /* Initialize the windows */
3863 for (i = 0; i < num_term; i++)
3865 term_data *td = &data[i];
3867 /* Initialize the term_data */
3868 term_data_init(td, i);
3870 /* Save global entry */
3871 angband_term[i] = Term;
3874 /* Raise the "Angband" window */
3875 Infowin_set(data[0].win);
3878 /* Activate the "Angband" window screen */
3879 Term_activate(&data[0].t);
3884 p = XSetLocaleModifiers("");
3886 p = XSetLocaleModifiers("@im=");
3888 /* printf("XMODIFIERS=\"%s\"\n", p); */
3890 XRegisterIMInstantiateCallback(Metadpy->dpy, NULL, NULL, NULL, IMInstantiateCallback, NULL);
3893 /* initialize sound */
3894 if (arg_sound) init_sound();
3897 switch (arg_graphics)
3899 case GRAPHICS_ORIGINAL:
3900 /* Try the "8x8.bmp" file */
3901 path_build(filename, sizeof(filename), ANGBAND_DIR_XTRA, "graf/8x8.bmp");
3903 /* Use the "8x8.bmp" file if it exists */
3904 if (0 == fd_close(fd_open(filename, O_RDONLY)))
3907 use_graphics = TRUE;
3909 pict_wid = pict_hgt = 8;
3911 ANGBAND_GRAF = "old";
3916 case GRAPHICS_ADAM_BOLT:
3917 /* Try the "16x16.bmp" file */
3918 path_build(filename, sizeof(filename), ANGBAND_DIR_XTRA, "graf/16x16.bmp");
3920 /* Use the "16x16.bmp" file if it exists */
3921 if (0 == fd_close(fd_open(filename, O_RDONLY)))
3924 use_graphics = TRUE;
3926 pict_wid = pict_hgt = 16;
3928 ANGBAND_GRAF = "new";
3937 Display *dpy = Metadpy->dpy;
3941 /* Load the graphical tiles */
3942 tiles_raw = ReadBMP(dpy, filename);
3944 /* Initialize the windows */
3945 for (i = 0; i < num_term; i++)
3947 term_data *td = &data[i];
3952 t->pict_hook = Term_pict_x11;
3954 /* Use graphics sometimes */
3955 t->higher_pict = TRUE;
3959 ResizeImage(dpy, tiles_raw,
3961 td->fnt->twid, td->fnt->hgt);
3964 /* Initialize the transparency masks */
3965 for (i = 0; i < num_term; i++)
3967 term_data *td = &data[i];
3969 int depth = DefaultDepth(dpy, DefaultScreen(dpy));
3970 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
3974 /* Determine total bytes needed for image */
3976 jj = (depth - 1) >> 2;
3977 while (jj >>= 1) ii <<= 1;
3978 total = td->fnt->twid * td->fnt->hgt * ii;
3981 TmpData = (char *)malloc(total);
3983 td->TmpImage = XCreateImage(dpy,visual,depth,
3984 ZPixmap, 0, TmpData,
3985 td->fnt->twid, td->fnt->hgt, 8, 0);
3989 /* Free tiles_raw? XXX XXX */
3996 #endif /* USE_X11 */