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)
15 * ¡¦´Á»ú¥Õ¥©¥ó¥È¤Î°·¤¤¤òÄɲÃ
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 &
115 #ifndef __MAKEDEPEND__
116 #include <X11/Xlib.h>
117 #include <X11/Xutil.h>
118 #include <X11/keysym.h>
119 #include <X11/keysymdef.h>
121 #include <X11/Xlocale.h>
124 char *XSetIMValues(XIM, ...); /* Hack for XFree86 4.0 */
126 #include <X11/Xatom.h>
127 #endif /* __MAKEDEPEND__ */
131 * Include some helpful X11 code.
133 #include "maid-x11.c"
137 * Hack -- avoid some compiler warnings
139 #define IGNORE_UNUSED_FUNCTIONS
145 * 1) On a monochrome (or "fake-monochrome") display, all colors
146 * will be "cast" to "fg," except for the bg color, which is,
147 * obviously, cast to "bg". Thus, one can ignore this setting.
149 * 2) Because of the inner functioning of the color allocation
150 * routines, colors may be specified as (a) a typical color name,
151 * (b) a hexidecimal color specification (preceded by a pound sign),
152 * or (c) by strings such as "fg", "bg", "zg".
154 * 3) Due to the workings of the init routines, many colors
155 * may also be dealt with by their actual pixel values. Note that
156 * the pixel with all bits set is "zg = (1<<metadpy->depth)-1", which
157 * is not necessarily either black or white.
162 /**** Generic Types ****/
166 * An X11 pixell specifier
168 typedef unsigned long Pixell;
171 * The structures defined below
173 typedef struct metadpy metadpy;
174 typedef struct infowin infowin;
175 typedef struct infoclr infoclr;
176 typedef struct infofnt infofnt;
180 * A structure summarizing a given Display.
182 * - The Display itself
183 * - The default Screen for the display
184 * - The virtual root (usually just the root)
185 * - The default colormap (from a macro)
187 * - The "name" of the display
189 * - The socket to listen to for events
191 * - The width of the display screen (from a macro)
192 * - The height of the display screen (from a macro)
193 * - The bit depth of the display screen (from a macro)
195 * - The black Pixell (from a macro)
196 * - The white Pixell (from a macro)
198 * - The background Pixell (default: black)
199 * - The foreground Pixell (default: white)
200 * - The maximal Pixell (Equals: ((2 ^ depth)-1), is usually ugly)
202 * - Bit Flag: Force all colors to black and white (default: !color)
203 * - Bit Flag: Allow the use of color (default: depth > 1)
204 * - Bit Flag: We created 'dpy', and so should nuke it when done.
239 * A Structure summarizing Window Information.
241 * I assume that a window is at most 30000 pixels on a side.
242 * I assume that the root windw is also at most 30000 square.
245 * - The current Input Event Mask
247 * - The location of the window
248 * - The width, height of the window
249 * - The border width of this window
251 * - Byte: 1st Extra byte
253 * - Bit Flag: This window is currently Mapped
254 * - Bit Flag: This window needs to be redrawn
255 * - Bit Flag: This window has been resized
257 * - Bit Flag: We should nuke 'win' when done with it
259 * - Bit Flag: 1st extra flag
260 * - Bit Flag: 2nd extra flag
261 * - Bit Flag: 3rd extra flag
262 * - Bit Flag: 4th extra flag
300 * A Structure summarizing Operation+Color Information
302 * - The actual GC corresponding to this info
304 * - The Foreground Pixell Value
305 * - The Background Pixell Value
307 * - Num (0-15): The operation code (As in Clear, Xor, etc)
308 * - Bit Flag: The GC is in stipple mode
309 * - Bit Flag: Destroy 'gc' at Nuke time.
326 * A Structure to Hold Font Information
328 * - The 'XFontStruct*' (yields the 'Font')
332 * - The default character width
333 * - The default character height
334 * - The default character ascent
336 * - Byte: Pixel offset used during fake mono
338 * - Flag: Force monospacing via 'wid'
339 * - Flag: Nuke info when done
365 /**** Generic Macros ****/
369 /* Set current metadpy (Metadpy) to 'M' */
370 #define Metadpy_set(M) \
374 /* Initialize 'M' using Display 'D' */
375 #define Metadpy_init_dpy(D) \
376 Metadpy_init_2(D,cNULL)
378 /* Initialize 'M' using a Display named 'N' */
379 #define Metadpy_init_name(N) \
380 Metadpy_init_2((Display*)(NULL),N)
382 /* Initialize 'M' using the standard Display */
383 #define Metadpy_init() \
384 Metadpy_init_name("")
387 /* Init an infowin by giving father as an (info_win*) (or NULL), and data */
388 #define Infowin_init_dad(D,X,Y,W,H,B,FG,BG) \
389 Infowin_init_data(((D) ? ((D)->win) : (Window)(None)), \
393 /* Init a top level infowin by pos,size,bord,Colors */
394 #define Infowin_init_top(X,Y,W,H,B,FG,BG) \
395 Infowin_init_data(None,X,Y,W,H,B,FG,BG)
398 /* Request a new standard window by giving Dad infowin and X,Y,W,H */
399 #define Infowin_init_std(D,X,Y,W,H,B) \
400 Infowin_init_dad(D,X,Y,W,H,B,Metadpy->fg,Metadpy->bg)
403 /* Set the current Infowin */
404 #define Infowin_set(I) \
408 /* Set the current Infoclr */
409 #define Infoclr_set(C) \
413 #define Infoclr_init_ppo(F,B,O,M) \
414 Infoclr_init_data(F,B,O,M)
416 #define Infoclr_init_cco(F,B,O,M) \
417 Infoclr_init_ppo(Infoclr_Pixell(F),Infoclr_Pixell(B),O,M)
419 #define Infoclr_init_ppn(F,B,O,M) \
420 Infoclr_init_ppo(F,B,Infoclr_Opcode(O),M)
422 #define Infoclr_init_ccn(F,B,O,M) \
423 Infoclr_init_cco(F,B,Infoclr_Opcode(O),M)
426 /* Set the current infofnt */
427 #define Infofnt_set(I) \
431 /* Errr: Expose Infowin */
432 #define Infowin_expose() \
433 (!(Infowin->redraw = 1))
435 /* Errr: Unxpose Infowin */
436 #define Infowin_unexpose() \
437 (Infowin->redraw = 0)
441 /**** Generic Globals ****/
445 * The "default" values
447 static metadpy metadpy_default;
451 * The "current" variables
453 static metadpy *Metadpy = &metadpy_default;
454 static infowin *Infowin = (infowin*)(NULL);
456 static infowin *Focuswin = (infowin*)(NULL);
458 static infoclr *Infoclr = (infoclr*)(NULL);
459 #ifdef USE_JP_FONTSTRUCT
460 static infofnt *Infofnt = (infofnt*)(NULL);
461 static infofnt *Infokfnt = (infofnt*)(NULL);
463 static infofnt *Infofnt = (infofnt*)(NULL);
469 /**** Generic code ****/
472 #ifdef USE_JP_FONTSTRUCT
473 #define Infokfnt_set(I) \
477 * Init the current metadpy, with various initialization stuff.
480 * dpy: The Display* to use (if NULL, create it)
481 * name: The name of the Display (if NULL, the current)
484 * If 'name' is NULL, but 'dpy' is set, extract name from dpy
485 * If 'dpy' is NULL, then Create the named Display
486 * If 'name' is NULL, and so is 'dpy', use current Display
488 * Return -1 if no Display given, and none can be opened.
490 static errr Metadpy_init_2(Display *dpy, cptr name)
492 metadpy *m = Metadpy;
494 /*** Open the display if needed ***/
496 /* If no Display given, attempt to Create one */
499 /* Attempt to open the display */
500 dpy = XOpenDisplay(name);
503 if (!dpy) return (-1);
505 /* We will have to nuke it when done */
509 /* Since the Display was given, use it */
512 /* We will not have to nuke it when done */
517 /*** Save some information ***/
519 /* Save the Display itself */
522 /* Get the Screen and Virtual Root Window */
523 m->screen = DefaultScreenOfDisplay(dpy);
524 m->root = RootWindowOfScreen(m->screen);
526 /* Get the default colormap */
527 m->cmap = DefaultColormapOfScreen(m->screen);
529 /* Extract the true name of the display */
530 m->name = DisplayString(dpy);
533 m->fd = ConnectionNumber(Metadpy->dpy);
535 /* Save the Size and Depth of the screen */
536 m->width = WidthOfScreen(m->screen);
537 m->height = HeightOfScreen(m->screen);
538 m->depth = DefaultDepthOfScreen(m->screen);
540 /* Save the Standard Colors */
541 m->black = BlackPixelOfScreen(m->screen);
542 m->white = WhitePixelOfScreen(m->screen);
544 /*** Make some clever Guesses ***/
546 /* Guess at the desired 'fg' and 'bg' Pixell's */
550 /* Calculate the Maximum allowed Pixel value. */
551 m->zg = (1 << m->depth) - 1;
553 /* Save various default Flag Settings */
554 m->color = ((m->depth > 1) ? 1 : 0);
555 m->mono = ((m->color) ? 0 : 1);
557 /* Return "success" */
562 #ifndef IGNORE_UNUSED_FUNCTIONS
565 * Nuke the current metadpy
567 static errr Metadpy_nuke(void)
569 metadpy *m = Metadpy;
572 /* If required, Free the Display */
575 /* Close the Display */
576 XCloseDisplay(m->dpy);
578 /* Forget the Display */
579 m->dpy = (Display*)(NULL);
581 /* Do not nuke it again */
589 #endif /* IGNORE_UNUSED_FUNCTIONS */
593 * General Flush/ Sync/ Discard routine
595 static errr Metadpy_update(int flush, int sync, int discard)
597 /* Flush if desired */
598 if (flush) XFlush(Metadpy->dpy);
600 /* Sync if desired, using 'discard' */
601 if (sync) XSync(Metadpy->dpy, discard);
611 static errr Metadpy_do_beep(void)
613 /* Make a simple beep */
614 XBell(Metadpy->dpy, 100);
622 * Set the name (in the title bar) of Infowin
624 static errr Infowin_set_name(cptr name)
631 st = XStringListToTextProperty(&bp, 1, &tp);
632 if (st) XSetWMName(Metadpy->dpy, Infowin->win, &tp);
637 #ifndef IGNORE_UNUSED_FUNCTIONS
640 * Set the icon name of Infowin
642 static errr Infowin_set_icon_name(cptr name)
649 st = XStringListToTextProperty(&bp, 1, &tp);
650 if (st) XSetWMIconName(Metadpy->dpy, Infowin->win, &tp);
658 static errr Infowin_nuke(void)
660 infowin *iwin = Infowin;
662 /* Nuke if requested */
665 /* Destory the old window */
666 XDestroyWindow(Metadpy->dpy, iwin->win);
673 #endif /* IGNORE_UNUSED_FUNCTIONS */
677 * Prepare a new 'infowin'.
679 static errr Infowin_prepare(Window xid)
681 infowin *iwin = Infowin;
684 XWindowAttributes xwa;
686 unsigned int w, h, b, d;
691 /* Check For Error XXX Extract some ACTUAL data from 'xid' */
692 XGetGeometry(Metadpy->dpy, xid, &tmp_win, &x, &y, &w, &h, &b, &d);
694 /* Apply the above info */
701 /* Check Error XXX Extract some more ACTUAL data */
702 XGetWindowAttributes(Metadpy->dpy, xid, &xwa);
704 /* Apply the above info */
705 iwin->mask = xwa.your_event_mask;
706 iwin->mapped = ((xwa.map_state == IsUnmapped) ? 0 : 1);
708 /* And assume that we are exposed */
716 #ifndef IGNORE_UNUSED_FUNCTIONS
719 * Initialize a new 'infowin'.
721 static errr Infowin_init_real(Window xid)
724 (void)WIPE(Infowin, infowin);
726 /* Start out non-nukable */
729 /* Attempt to Prepare ourself */
730 return (Infowin_prepare(xid));
733 #endif /* IGNORE_UNUSED_FUNCTIONS */
737 * Init an infowin by giving some data.
740 * dad: The Window that should own this Window (if any)
741 * x,y: The position of this Window
742 * w,h: The size of this Window
743 * b,d: The border width and pixel depth
746 * If 'dad == None' assume 'dad == root'
748 static errr Infowin_init_data(Window dad, int x, int y, int w, int h,
749 int b, Pixell fg, Pixell bg)
754 (void)WIPE(Infowin, infowin);
757 /*** Error Check XXX ***/
760 /*** Create the Window 'xid' from data ***/
762 /* What happened here? XXX XXX XXX */
764 /* If no parent given, depend on root */
767 /* #ifdef USE_GRAPHICS
769 xid = XCreateWindow(Metadpy->dpy, Metadpy->root, x, y, w, h, b, 8, InputOutput, CopyFromParent, 0, 0);
780 /* Create the Window XXX Error Check */
781 xid = XCreateSimpleWindow(Metadpy->dpy, dad, x, y, w, h, b, fg, bg);
783 /* Start out selecting No events */
784 XSelectInput(Metadpy->dpy, xid, 0L);
787 /*** Prepare the new infowin ***/
789 /* Mark it as nukable */
792 /* Attempt to Initialize the infowin */
793 return (Infowin_prepare(xid));
799 * Modify the event mask of an Infowin
801 static errr Infowin_set_mask(long mask)
803 /* Save the new setting */
804 Infowin->mask = mask;
806 /* Execute the Mapping */
807 XSelectInput(Metadpy->dpy, Infowin->win, Infowin->mask);
815 * Request that Infowin be mapped
817 static errr Infowin_map(void)
819 /* Execute the Mapping */
820 XMapWindow(Metadpy->dpy, Infowin->win);
827 #ifndef IGNORE_UNUSED_FUNCTIONS
830 * Request that Infowin be unmapped
832 static errr Infowin_unmap(void)
834 /* Execute the Un-Mapping */
835 XUnmapWindow(Metadpy->dpy, Infowin->win);
841 #endif /* IGNORE_UNUSED_FUNCTIONS */
845 * Request that Infowin be raised
847 static errr Infowin_raise(void)
849 /* Raise towards visibility */
850 XRaiseWindow(Metadpy->dpy, Infowin->win);
857 #ifndef IGNORE_UNUSED_FUNCTIONS
860 * Request that Infowin be lowered
862 static errr Infowin_lower(void)
864 /* Lower towards invisibility */
865 XLowerWindow(Metadpy->dpy, Infowin->win);
871 #endif /* IGNORE_UNUSED_FUNCTIONS */
875 * Request that Infowin be moved to a new location
877 static errr Infowin_impell(int x, int y)
879 /* Execute the request */
880 XMoveWindow(Metadpy->dpy, Infowin->win, x, y);
890 static errr Infowin_resize(int w, int h)
892 /* Execute the request */
893 XResizeWindow(Metadpy->dpy, Infowin->win, w, h);
900 #ifndef IGNORE_UNUSED_FUNCTIONS
903 * Move and Resize an infowin
905 static errr Infowin_locate(int x, int y, int w, int h)
907 /* Execute the request */
908 XMoveResizeWindow(Metadpy->dpy, Infowin->win, x, y, w, h);
914 #endif /* IGNORE_UNUSED_FUNCTIONS */
918 * Visually clear Infowin
920 static errr Infowin_wipe(void)
922 /* Execute the request */
923 XClearWindow(Metadpy->dpy, Infowin->win);
930 #ifndef IGNORE_UNUSED_FUNCTIONS
933 * Visually Paint Infowin with the current color
935 static errr Infowin_fill(void)
937 /* Execute the request */
938 XFillRectangle(Metadpy->dpy, Infowin->win, Infoclr->gc,
939 0, 0, Infowin->w, Infowin->h);
945 #endif /* IGNORE_UNUSED_FUNCTIONS */
949 * A NULL terminated pair list of legal "operation names"
951 * Pairs of values, first is texttual name, second is the string
952 * holding the decimal value that the operation corresponds to.
954 static cptr opcode_pairs[] =
973 "+copyInverted", "12",
981 * Parse a word into an operation "code"
984 * str: A string, hopefully representing an Operation
987 * 0-15: if 'str' is a valid Operation
988 * -1: if 'str' could not be parsed
990 static int Infoclr_Opcode(cptr str)
994 /* Scan through all legal operation names */
995 for (i = 0; opcode_pairs[i*2]; ++i)
997 /* Is this the right oprname? */
998 if (streq(opcode_pairs[i*2], str))
1000 /* Convert the second element in the pair into a Code */
1001 return (atoi(opcode_pairs[i*2+1]));
1005 /* The code was not found, return -1 */
1010 #ifndef IGNORE_UNUSED_FUNCTIONS
1013 * Request a Pixell by name. Note: uses 'Metadpy'.
1016 * name: The name of the color to try to load (see below)
1019 * The Pixell value that metched the given name
1020 * 'Metadpy->fg' if the name was unparseable
1022 * Valid forms for 'name':
1023 * 'fg', 'bg', 'zg', '<name>' and '#<code>'
1025 static Pixell Infoclr_Pixell(cptr name)
1029 /* Attempt to Parse the name */
1030 if (name && name[0])
1032 /* The 'bg' color is available */
1033 if (streq(name, "bg")) return (Metadpy->bg);
1035 /* The 'fg' color is available */
1036 if (streq(name, "fg")) return (Metadpy->fg);
1038 /* The 'zg' color is available */
1039 if (streq(name, "zg")) return (Metadpy->zg);
1041 /* The 'white' color is available */
1042 if (streq(name, "white")) return (Metadpy->white);
1044 /* The 'black' color is available */
1045 if (streq(name, "black")) return (Metadpy->black);
1047 /* Attempt to parse 'name' into 'scrn' */
1048 if (!(XParseColor(Metadpy->dpy, Metadpy->cmap, name, &scrn)))
1050 plog_fmt("Warning: Couldn't parse color '%s'\n", name);
1053 /* Attempt to Allocate the Parsed color */
1054 if (!(XAllocColor(Metadpy->dpy, Metadpy->cmap, &scrn)))
1056 plog_fmt("Warning: Couldn't allocate color '%s'\n", name);
1059 /* The Pixel was Allocated correctly */
1060 else return (scrn.pixel);
1063 /* Warn about the Default being Used */
1064 plog_fmt("Warning: Using 'fg' for unknown color '%s'\n", name);
1066 /* Default to the 'Foreground' color */
1067 return (Metadpy->fg);
1072 * Initialize a new 'infoclr' with a real GC.
1074 static errr Infoclr_init_1(GC gc)
1076 infoclr *iclr = Infoclr;
1078 /* Wipe the iclr clean */
1079 (void)WIPE(iclr, infoclr);
1090 * Nuke an old 'infoclr'.
1092 static errr Infoclr_nuke(void)
1094 infoclr *iclr = Infoclr;
1096 /* Deal with 'GC' */
1100 XFreeGC(Metadpy->dpy, iclr->gc);
1103 /* Forget the current */
1104 Infoclr = (infoclr*)(NULL);
1110 #endif /* IGNORE_UNUSED_FUNCTIONS */
1114 * Initialize an infoclr with some data
1117 * fg: The Pixell for the requested Foreground (see above)
1118 * bg: The Pixell for the requested Background (see above)
1119 * op: The Opcode for the requested Operation (see above)
1120 * stip: The stipple mode
1122 static errr Infoclr_init_data(Pixell fg, Pixell bg, int op, int stip)
1124 infoclr *iclr = Infoclr;
1128 unsigned long gc_mask;
1132 /*** Simple error checking of opr and clr ***/
1134 /* Check the 'Pixells' for realism */
1135 if (bg > Metadpy->zg) return (-1);
1136 if (fg > Metadpy->zg) return (-1);
1138 /* Check the data for trueness */
1139 if ((op < 0) || (op > 15)) return (-1);
1142 /*** Create the requested 'GC' ***/
1144 /* Assign the proper GC function */
1147 /* Assign the proper GC background */
1148 gcv.background = bg;
1150 /* Assign the proper GC foreground */
1151 gcv.foreground = fg;
1153 /* Hack -- Handle XOR (xor is code 6) by hacking bg and fg */
1154 if (op == 6) gcv.background = 0;
1155 if (op == 6) gcv.foreground = (bg ^ fg);
1157 /* Assign the proper GC Fill Style */
1158 gcv.fill_style = (stip ? FillStippled : FillSolid);
1160 /* Turn off 'Give exposure events for pixmap copying' */
1161 gcv.graphics_exposures = False;
1163 /* Set up the GC mask */
1164 gc_mask = (GCFunction | GCBackground | GCForeground |
1165 GCFillStyle | GCGraphicsExposures);
1167 /* Create the GC detailed above */
1168 gc = XCreateGC(Metadpy->dpy, Metadpy->root, gc_mask, &gcv);
1171 /*** Initialize ***/
1173 /* Wipe the iclr clean */
1174 (void)WIPE(iclr, infoclr);
1179 /* Nuke it when done */
1182 /* Assign the parms */
1186 iclr->stip = stip ? 1 : 0;
1195 * Change the 'fg' for an infoclr
1198 * fg: The Pixell for the requested Foreground (see above)
1200 static errr Infoclr_change_fg(Pixell fg)
1202 infoclr *iclr = Infoclr;
1205 /*** Simple error checking of opr and clr ***/
1207 /* Check the 'Pixells' for realism */
1208 if (fg > Metadpy->zg) return (-1);
1214 XSetForeground(Metadpy->dpy, iclr->gc, fg);
1222 #ifndef IGNORE_UNUSED_FUNCTIONS
1225 * Nuke an old 'infofnt'.
1227 static errr Infofnt_nuke(void)
1229 infofnt *ifnt = Infofnt;
1231 #ifdef USE_JP_FONTSTRUCT
1232 infofnt *ikfnt = Infokfnt;
1234 /* Deal with 'name' */
1238 string_free(ifnt->name);
1241 #ifdef USE_JP_FONTSTRUCT
1245 string_free(ikfnt->name);
1249 /* Nuke info if needed */
1254 XFreeFontSet(Metadpy->dpy, ifnt->info);
1256 XFreeFont(Metadpy->dpy, ifnt->info);
1260 #ifdef USE_JP_FONTSTRUCT
1264 XFreeFont(Metadpy->dpy, ikfnt->info);
1271 #endif /* IGNORE_UNUSED_FUNCTIONS */
1275 * Prepare a new 'infofnt'
1277 #ifdef USE_JP_FONTSTRUCT
1278 static errr Infofnt_prepare(XFontStruct *info, XFontStruct *kinfo)
1281 static errr Infofnt_prepare(XFontSet info)
1283 static errr Infofnt_prepare(XFontStruct *info)
1288 infofnt *ifnt = Infofnt;
1290 #ifdef USE_JP_FONTSTRUCT
1291 infofnt *ikfnt = Infokfnt;
1295 XFontStruct **fontinfo;
1298 int ascent, descent, width;
1301 /* Assign the struct */
1305 n_fonts = XFontsOfFontSet(info, &fontinfo, &fontname);
1307 ascent = descent = width = 0;
1308 while(n_fonts-- > 0){
1309 cs = &((*fontinfo)->max_bounds);
1310 if(ascent < (*fontinfo)->ascent) ascent = (*fontinfo)->ascent;
1311 if(descent < (*fontinfo)->descent) descent = (*fontinfo)->descent;
1312 if(((*fontinfo)->max_byte1) > 0){
1313 /* ¿¥Ð¥¤¥Èʸ»ú¤Î¾ì¹ç¤ÏÉýȾʬ(ü¿ôÀÚ¤ê¾å¤²)¤Çɾ²Á¤¹¤ë */
1314 if(width < (cs->width+1)/2) width = (cs->width+1)/2;
1316 if(width < cs->width) width = cs->width;
1322 ifnt->hgt = ascent + descent;
1325 /* Jump into the max bouonds thing */
1326 cs = &(info->max_bounds);
1328 /* Extract default sizing info */
1329 ifnt->asc = info->ascent;
1330 ifnt->hgt = info->ascent + info->descent;
1331 ifnt->wid = cs->width;
1335 ifnt->twid = 2 * ifnt->wid;
1337 ifnt->twid = ifnt->wid;
1339 #ifdef USE_JP_FONTSTRUCT
1340 /* Assign the struct */
1341 ikfnt->info = kinfo;
1343 /* Jump into the max bouonds thing */
1344 cs = &(kinfo->max_bounds);
1346 /* Extract default sizing info */
1347 ikfnt->asc = kinfo->ascent;
1348 ikfnt->hgt = kinfo->ascent + kinfo->descent;
1349 ikfnt->wid = cs->width;
1352 #ifdef OBSOLETE_SIZING_METHOD
1353 /* Extract default sizing info */
1354 ifnt->asc = cs->ascent;
1355 ifnt->hgt = (cs->ascent + cs->descent);
1356 ifnt->wid = cs->width;
1365 #ifndef IGNORE_UNUSED_FUNCTIONS
1368 * Initialize a new 'infofnt'.
1370 #ifdef USE_JP_FONTSTRUCT
1371 static errr Infofnt_init_real(XFontStruct *info, XFontStruct *kinfo)
1374 static errr Infofnt_init_real(XFontSet info)
1376 static errr Infofnt_init_real(XFontStruct *info)
1381 /* Wipe the thing */
1382 (void)WIPE(Infofnt, infofnt);
1384 #ifdef USE_JP_FONTSTRUCT
1385 WIPE(Infokfnt, infofnt);
1390 #ifdef USE_JP_FONTSTRUCT
1393 /* Attempt to prepare it */
1394 #ifdef USE_JP_FONTSTRUCT
1395 return (Infofnt_prepare (info, kinfo));
1397 return (Infofnt_prepare(info));
1402 #endif /* IGNORE_UNUSED_FUNCTIONS */
1406 * Init an infofnt by its Name
1409 * name: The name of the requested Font
1411 #ifdef USE_JP_FONTSTRUCT
1412 static void Infofnt_init_data(cptr name, cptr kname)
1414 static void Infofnt_init_data(cptr name)
1420 char **missing_list;
1428 #ifdef USE_JP_FONTSTRUCT
1431 /*** Load the info Fresh, using the name ***/
1433 /* If the name is not given, report an error */
1434 if (!name || !*name) quit("Missing font!");
1436 #ifdef USE_JP_FONTSTRUCT
1437 if (!kname || !*kname) quit("Missing kanji font!");
1439 /* Attempt to load the font */
1441 info = XCreateFontSet(Metadpy->dpy, name, &missing_list, &missing_count, &default_font);
1442 if(missing_count > 0){
1443 printf("missing font(s): \n");
1444 while(missing_count-- > 0){
1445 printf("\t%s\n", missing_list[missing_count]);
1447 XFreeStringList(missing_list);
1450 info = XLoadQueryFont(Metadpy->dpy, name);
1451 #ifdef USE_JP_FONTSTRUCT
1452 kinfo = XLoadQueryFont(Metadpy->dpy, kname);
1457 /* The load failed, try to recover */
1458 if (!info) quit_fmt("Failed to find font:\"%s\"", name);
1459 #ifdef USE_JP_FONTSTRUCT
1460 if (!kinfo) quit_fmt("Failed to find font:\"%s\"", kname);
1465 /*** Init the font ***/
1467 /* Wipe the thing */
1468 (void)WIPE(Infofnt, infofnt);
1470 #ifdef USE_JP_FONTSTRUCT
1471 WIPE(Infokfnt, infofnt);
1473 /* Attempt to prepare it */
1474 #ifdef USE_JP_FONTSTRUCT
1475 if (Infofnt_prepare(info, kinfo))
1477 if (Infofnt_prepare(info))
1483 XFreeFontSet(Metadpy->dpy, info);
1485 XFreeFont(Metadpy->dpy, info);
1486 #ifdef USE_JP_FONTSTRUCT
1487 XFreeFont(Metadpy->dpy, kinfo);
1491 quit_fmt("Failed to prepare font:\"%s\"", name);
1494 /* Save a copy of the font name */
1495 Infofnt->name = string_make(name);
1496 #ifdef USE_JP_FONTSTRUCT
1497 Infokfnt->name = string_make(kname);
1500 /* Mark it as nukable */
1502 #ifdef USE_JP_FONTSTRUCT
1508 #ifdef USE_JP_FONTSTRUCT
1510 * EUCÆüËܸ쥳¡¼¥É¤ò´Þ¤àʸ»úÎó¤òɽ¼¨¤¹¤ë (Xlib)
1513 XDrawMultiString(display,d,gc, x, y, string, len, afont,
1514 afont_width, afont_height, afont_ascent, kfont, kfont_width)
1522 int afont_width, afont_height, afont_ascent;
1529 unsigned char *endp;
1532 endp = string + len;
1534 while ( str < endp && *str ) {
1537 if ( (*str) == 0x7f ) {
1539 /* 0x7F¤Ï¢£¤Ç·è¤áÂǤÁ */
1541 /* Ϣ³¤¹¤ë0x7F¤ÎŤµ¤ò¸¡½Ð */
1543 while ( str < endp && (*str) == 0x7f ) {
1549 XFillRectangle( display, d, gc, x, y-afont_ascent,
1550 slen * afont_width, afont_height);
1552 /* ¥Ý¥¤¥ó¥¿¤ò¿Ê¤á¤ë */
1553 x += afont_width * slen;
1557 if ( iskanji(*str) ) {
1561 /* Ϣ³¤¹¤ëUJISʸ»ú¤ÎŤµ¤ò¸¡½Ð */
1563 while ( str < endp && *str && iskanji(*str) ) {
1564 kanji[slen].byte1 = *str++ & 0x7f;
1565 kanji[slen++].byte2 = *str++ & 0x7f;
1569 XSetFont( display, gc, kfont->fid );
1570 XDrawImageString16( display, d, gc, x, y, kanji, slen );
1573 /* ¥Ý¥¤¥ó¥¿¤ò¿Ê¤á¤ë */
1574 x += kfont_width * slen;
1578 /* Èó´Á»ú(=ASCII¤È²¾Äê)¤Î»Ï¤Þ¤ê */
1580 /* Ϣ³¤¹¤ëASCIIʸ»ú¤ò¸¡½Ð */
1583 while ( str < endp && *str && !iskanji(*str) ) {
1585 if (*str == 0x7f)break;
1592 XSetFont( display, gc, afont->fid );
1593 XDrawImageString( display, d, gc, x, y, p, slen );
1595 /* ¥Ý¥¤¥ó¥¿¤ò¿Ê¤á¤ë */
1596 x += afont_width * slen;
1605 static errr Infofnt_text_std(int x, int y, cptr str, int len)
1610 /*** Do a brief info analysis ***/
1612 /* Do nothing if the string is null */
1613 if (!str || !*str) return (-1);
1615 /* Get the length of the string */
1616 if (len < 0) len = strlen(str);
1618 /*** Decide where to place the string, vertically ***/
1620 /* Ignore Vertical Justifications */
1621 y = (y * Infofnt->hgt) + Infofnt->asc + Infowin->oy;
1624 /*** Decide where to place the string, horizontally ***/
1626 /* Line up with x at left edge of column 'x' */
1627 x = (x * Infofnt->wid) + Infowin->ox;
1630 /*** Actually draw 'str' onto the infowin ***/
1633 /* Be sure the correct font is ready */
1634 XSetFont(Metadpy->dpy, Infoclr->gc, Infofnt->info->fid);
1637 /*** Handle the fake mono we can enforce on fonts ***/
1639 /* Monotize the font */
1642 #ifdef USE_JP_FONTSTRUCT
1643 /* Be sure the correct font is ready */
1644 XSetFont(Metadpy->dpy, Infoclr->gc, Infofnt->info->fid);
1646 /* Do each character */
1647 for (i = 0; i < len; ++i)
1649 /* Note that the Infoclr is set up to contain the Infofnt */
1650 XDrawImageString(Metadpy->dpy, Infowin->win, Infoclr->gc,
1651 x + i * Infofnt->wid + Infofnt->off, y, str + i, 1);
1655 /* Assume monoospaced font */
1658 /* Note that the Infoclr is set up to contain the Infofnt */
1659 #ifdef USE_JP_FONTSTRUCT
1660 /* ´Á»ú¥Õ¥©¥ó¥È¤Îɽ¼¨Éý¤Ï ASCII¥Õ¥©¥ó¥È¤Î2Çܤ˸ÇÄê */
1661 XDrawMultiString(Metadpy->dpy, Infowin->win, Infoclr->gc,
1663 Infofnt->info, Infofnt->wid, Infofnt->hgt,
1665 Infokfnt->info, Infofnt->wid * 2);
1668 XmbDrawImageString(Metadpy->dpy, Infowin->win, Infofnt->info,
1669 Infoclr->gc, x, y, str, len);
1671 XDrawImageString(Metadpy->dpy, Infowin->win, Infoclr->gc,
1685 * Painting where text would be
1687 static errr Infofnt_text_non(int x, int y, cptr str, int len)
1692 /*** Find the width ***/
1694 /* Negative length is a flag to count the characters in str */
1695 if (len < 0) len = strlen(str);
1697 /* The total width will be 'len' chars * standard width */
1698 w = len * Infofnt->wid;
1701 /*** Find the X dimensions ***/
1703 /* Line up with x at left edge of column 'x' */
1704 x = x * Infofnt->wid + Infowin->ox;
1707 /*** Find other dimensions ***/
1709 /* Simply do 'Infofnt->hgt' (a single row) high */
1712 /* Simply do "at top" in row 'y' */
1713 y = y * h + Infowin->oy;
1716 /*** Actually 'paint' the area ***/
1718 /* Just do a Fill Rectangle */
1719 XFillRectangle(Metadpy->dpy, Infowin->win, Infoclr->gc, x, y, w, h);
1727 /*************************************************************************/
1731 * Angband specific code follows... (ANGBAND)
1736 * Hack -- cursor color
1738 static infoclr *xor;
1741 * Actual color table
1743 static infoclr *clr[256];
1746 * Color info (unused, red, green, blue).
1748 static byte color_table[256][4];
1753 typedef struct term_data term_data;
1756 * A structure for each "term"
1763 #ifdef USE_JP_FONTSTRUCT
1774 /* Tempory storage for overlaying tiles. */
1783 * The number of term data structures
1785 #define MAX_TERM_DATA 8
1788 * The array of term data structures
1790 static term_data data[MAX_TERM_DATA];
1793 /* Use short names for the most commonly used elements of various structures. */
1794 #define DPY (Metadpy->dpy)
1795 #define WIN (Infowin->win)
1798 /* Describe a set of co-ordinates. */
1799 typedef struct co_ord co_ord;
1808 * A special structure to store information about the text currently
1811 typedef struct x11_selection_type x11_selection_type;
1812 struct x11_selection_type
1814 bool select; /* The selection is currently in use. */
1815 bool drawn; /* The selection is currently displayed. */
1816 term *t; /* The window where the selection is found. */
1817 co_ord init; /* The starting co-ordinates. */
1818 co_ord cur; /* The end co-ordinates (the current ones if still copying). */
1819 co_ord old; /* The previous end co-ordinates. */
1820 Time time; /* The time at which the selection was finalised. */
1823 static x11_selection_type s_ptr[1];
1827 * Process a keypress event
1829 * Also appears in "main-xaw.c".
1831 static void react_keypress(XKeyEvent *xev)
1833 int i, n, mc, ms, mo;
1837 XKeyEvent *ev = (XKeyEvent*)(xev);
1845 int valid_keysym = TRUE;
1848 /* Check for "normal" keypresses */
1850 if(Focuswin && Focuswin->xic){
1852 n = XmbLookupString(Focuswin->xic, ev, buf, 125, &ks, &status);
1853 if(status == XBufferOverflow){
1854 printf("Input is too long, and dropped\n");
1857 if(status != XLookupKeySym && status != XLookupBoth){
1858 valid_keysym = FALSE;
1861 n = XLookupString(ev, buf, 125, &ks, NULL);
1864 n = XLookupString(ev, buf, 125, &ks, NULL);
1872 /* Enqueue the normal key(s) */
1873 for (i = 0; buf[i]; i++) Term_keypress(buf[i]);
1880 /* Hack -- Ignore "modifier keys" */
1881 if (IsModifierKey(ks)) return;
1884 /* Hack -- convert into an unsigned int */
1887 /* Extract four "modifier flags" */
1888 mc = (ev->state & ControlMask) ? TRUE : FALSE;
1889 ms = (ev->state & ShiftMask) ? TRUE : FALSE;
1890 mo = (ev->state & Mod1Mask) ? TRUE : FALSE;
1893 /* Normal keys with no modifiers */
1894 if (n && !mo && !IsSpecialKey(ks))
1896 /* Enqueue the normal key(s) */
1897 for (i = 0; buf[i]; i++) Term_keypress(buf[i]);
1904 /* Handle a few standard keys (bypass modifiers) XXX XXX XXX */
1909 Term_keypress(ESCAPE);
1915 Term_keypress('\r');
1921 Term_keypress('\t');
1927 Term_keypress(0x7f);
1932 Term_keypress('\010');
1938 /* Hack -- Use the KeySym */
1941 sprintf(msg, "%c%s%s%s_%lX%c", 31,
1942 mc ? "N" : "", ms ? "S" : "",
1944 (unsigned long)(ks), 13);
1947 /* Hack -- Use the Keycode */
1950 sprintf(msg, "%c%s%s%sK_%X%c", 31,
1951 mc ? "N" : "", ms ? "S" : "",
1956 /* Enqueue the "macro trigger" string */
1957 for (i = 0; msg[i]; i++) Term_keypress(msg[i]);
1960 /* Hack -- auto-define macros as needed */
1961 if (n && (macro_find_exact(msg) < 0))
1963 /* Create a macro */
1964 macro_add(msg, buf);
1970 * Find the square a particular pixel is part of.
1972 static void pixel_to_square(int * const x, int * const y,
1973 const int ox, const int oy)
1975 (*x) = (ox - Infowin->ox) / Infofnt->wid;
1976 (*y) = (oy - Infowin->oy) / Infofnt->hgt;
1980 * Find the pixel at the top-left corner of a square.
1982 static void square_to_pixel(int * const x, int * const y,
1983 const int ox, const int oy)
1985 (*x) = ox * Infofnt->wid + Infowin->ox;
1986 (*y) = oy * Infofnt->hgt + Infowin->oy;
1990 * Convert co-ordinates from starting corner/opposite corner to minimum/maximum.
1992 static void sort_co_ord(co_ord *min, co_ord *max,
1993 const co_ord *b, const co_ord *a)
1995 min->x = MIN(a->x, b->x);
1996 min->y = MIN(a->y, b->y);
1997 max->x = MAX(a->x, b->x);
1998 max->y = MAX(a->y, b->y);
2002 * Remove the selection by redrawing it.
2004 static void mark_selection_clear(int x1, int y1, int x2, int y2)
2006 Term_redraw_section(x1,y1,x2,y2);
2010 * Select an area by drawing a grey box around it.
2011 * NB. These two functions can cause flicker as the selection is modified,
2012 * as the game redraws the entire marked section.
2014 static void mark_selection_mark(int x1, int y1, int x2, int y2)
2016 square_to_pixel(&x1, &y1, x1, y1);
2017 square_to_pixel(&x2, &y2, x2, y2);
2018 XDrawRectangle(Metadpy->dpy, Infowin->win, clr[2]->gc, x1, y1,
2019 x2-x1+Infofnt->wid - 1, y2-y1+Infofnt->hgt - 1);
2023 * Mark a selection by drawing boxes around it (for now).
2025 static void mark_selection(void)
2029 bool draw = s_ptr->select;
2030 bool clear = s_ptr->drawn;
2032 /* Open the correct term if necessary. */
2033 if (s_ptr->t != old) Term_activate(s_ptr->t);
2037 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->old);
2038 mark_selection_clear(min.x, min.y, max.x, max.y);
2042 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->cur);
2043 mark_selection_mark(min.x, min.y, max.x, max.y);
2046 /* Finish on the current term. */
2047 if (s_ptr->t != old) Term_activate(old);
2049 s_ptr->old.x = s_ptr->cur.x;
2050 s_ptr->old.y = s_ptr->cur.y;
2051 s_ptr->drawn = s_ptr->select;
2055 * Forget a selection for one reason or another.
2057 static void copy_x11_release(void)
2059 /* Deselect the current selection. */
2060 s_ptr->select = FALSE;
2062 /* Remove its graphical represesntation. */
2067 * Start to select some text on the screen.
2069 static void copy_x11_start(int x, int y)
2071 if (s_ptr->select) copy_x11_release();
2073 /* Remember where the selection started. */
2075 s_ptr->init.x = s_ptr->cur.x = s_ptr->old.x = x;
2076 s_ptr->init.y = s_ptr->cur.y = s_ptr->old.y = y;
2080 * Respond to movement of the mouse when selecting text.
2082 static void copy_x11_cont(int x, int y, unsigned int buttons)
2084 /* Use the nearest square within bounds if the mouse is outside. */
2085 x = MIN(MAX(x, 0), Term->wid-1);
2086 y = MIN(MAX(y, 0), Term->hgt-1);
2088 /* The left mouse button isn't pressed. */
2089 if (~buttons & Button1Mask) return;
2091 /* Not a selection in this window. */
2092 if (s_ptr->t != Term) return;
2094 /* Not enough movement. */
2095 if (x == s_ptr->old.x && y == s_ptr->old.y && s_ptr->select) return;
2097 /* Something is being selected. */
2098 s_ptr->select = TRUE;
2100 /* Track the selection. */
2104 /* Hack - display it inefficiently. */
2109 * Respond to release of the left mouse button by putting the selected text in
2110 * the primary buffer.
2112 static void copy_x11_end(const Time time)
2115 if (!s_ptr->select) return;
2117 /* Not a selection in this window. */
2118 if (s_ptr->t != Term) return;
2120 /* Remember when the selection was finalised. */
2123 /* Acquire the primary selection. */
2124 XSetSelectionOwner(Metadpy->dpy, XA_PRIMARY, Infowin->win, time);
2125 if (XGetSelectionOwner(Metadpy->dpy, XA_PRIMARY) != Infowin->win)
2127 /* Failed to acquire the selection, so forget it. */
2128 /* bell("Failed to acquire primary buffer."); */
2129 s_ptr->select = FALSE;
2135 static Atom xa_targets, xa_timestamp, xa_text, xa_compound_text;
2138 * Set the required variable atoms at start-up to avoid errors later.
2140 static void set_atoms(void)
2142 xa_targets = XInternAtom(DPY, "TARGETS", False);
2143 xa_timestamp = XInternAtom(DPY, "TIMESTAMP", False);
2144 xa_text = XInternAtom(DPY, "TEXT", False);
2145 xa_compound_text = XInternAtom(DPY, "COMPOUND_TEXT", False);
2149 static Atom request_target = 0;
2152 * Send a message to request that the PRIMARY buffer be sent here.
2154 static void paste_x11_request(Atom target, const Time time)
2157 * It's from some sample programs on the web.
2158 * What does it mean? -- XXX
2160 Atom property = XInternAtom(DPY, "__COPY_TEXT", False);
2162 /* Check the owner. */
2163 if (XGetSelectionOwner(DPY, XA_PRIMARY) == None)
2166 /* bell("No selection found."); */
2170 request_target = target;
2172 /* Request the event */
2173 XConvertSelection(DPY, XA_PRIMARY, target, property, WIN, time);
2178 * Add the contents of the PRIMARY buffer to the input queue.
2180 * Hack - This doesn't use the "time" of the event, and so accepts anything a
2181 * client tries to send it.
2183 static void paste_x11_accept(const XSelectionEvent *ptr)
2186 const long offset = 0;
2187 const long length = 32000;
2188 XTextProperty xtextproperty;
2192 * It's from some sample programs on the web.
2193 * What does it mean? -- XXX
2195 Atom property = XInternAtom(DPY, "__COPY_TEXT", False);
2199 if (ptr->property == None)
2201 if (request_target == xa_compound_text)
2203 /* Re-request as STRING */
2204 paste_x11_request(XA_STRING, ptr->time);
2209 plog("Paste failure (remote client could not send).");
2214 if (ptr->selection != XA_PRIMARY)
2216 plog("Paste failure (remote client did not send primary selection).");
2220 if (ptr->target != request_target)
2222 plog("Paste failure (selection in unknown format).");
2227 if (XGetWindowProperty(Metadpy->dpy, Infowin->win, property, offset,
2228 length, TRUE, request_target,
2229 &xtextproperty.encoding,
2230 &xtextproperty.format,
2231 &xtextproperty.nitems,
2233 &xtextproperty.value)
2240 if (request_target == xa_compound_text)
2245 XmbTextPropertyToTextList(DPY, &xtextproperty, &list, &count);
2251 for (i = 0; i < count; i++)
2253 /* Paste the text. */
2254 err = type_string(list[i], 0);
2259 /* Free the string */
2260 XFreeStringList(list);
2263 else /* if (request_target == XA_STRING) */
2265 /* Paste the text. */
2266 err = type_string((char *)xtextproperty.value, xtextproperty.nitems);
2269 /* Free the data pasted. */
2270 XFree(xtextproperty.value);
2275 plog("Paste failure (too much text selected).");
2281 * Add a character to a string in preparation for sending it to another
2282 * client as a STRING.
2283 * This doesn't change anything, as clients tend not to have difficulty in
2284 * receiving this format (although the standard specifies a restricted set).
2285 * Strings do not have a colour.
2287 static bool paste_x11_send_text(XSelectionRequestEvent *rq)
2296 /* Too old, or incorrect call. */
2297 if (rq->time < s_ptr->time) return FALSE;
2299 /* Work out which way around to paste. */
2300 sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->cur);
2303 if (XGetSelectionOwner(DPY, XA_PRIMARY) != WIN)
2305 /* bell("Someone stole my selection!"); */
2309 /* Delete the old value of the property. */
2310 XDeleteProperty(DPY, rq->requestor, rq->property);
2312 for (n = 0, y = 0; y < Term->hgt; y++)
2317 if (y < min.y) continue;
2318 if (y > max.y) break;
2320 for (l = 0, x = 0; x < Term->wid; x++)
2323 if (x > max.x) break;
2325 /* Find the character. */
2326 Term_what(x, y, &a, &c);
2328 if (1 == kanji) kanji = 2;
2329 else if (iskanji(c)) kanji = 1;
2332 if (x < min.x) continue;
2335 * A single kanji character was divided in two...
2336 * Delete the garbage.
2338 if ((2 == kanji && x == min.x) ||
2339 (1 == kanji && x == max.x))
2342 if (x > max.x) break;
2343 if (x < min.x) continue;
2345 /* Find the character. */
2346 Term_what(x, y, &a, &c);
2354 /* Ignore trailing spaces */
2355 while (buf[l-1] == ' ') l--;
2357 /* Terminate all line unless it's single line. */
2367 list[n++] = (char *)string_make(buf);
2370 /* End of the list */
2374 if (rq->target == XA_STRING)
2376 for (n = 0; list[n]; n++)
2378 /* Send the (non-empty) string. */
2379 XChangeProperty(DPY, rq->requestor, rq->property, rq->target, 8,
2380 PropModeAppend, (unsigned char *)list[n], strlen(list[n]));
2384 else if (rq->target == xa_text ||
2385 rq->target == xa_compound_text)
2387 XTextProperty text_prop;
2388 XICCEncodingStyle style;
2390 if (rq->target == xa_text)
2391 style = XStdICCTextStyle;
2392 else /* if (rq->target == xa_compound_text) */
2393 style = XCompoundTextStyle;
2396 XmbTextListToTextProperty(DPY, list, n, style, &text_prop))
2398 /* Send the compound text */
2399 XChangeProperty(DPY,
2408 /* Free the data. */
2409 XFree(text_prop.value);
2413 /* Free the list of strings */
2414 for (n = 0; list[n]; n++)
2416 string_free(list[n]);
2423 * Send some text requested by another X client.
2425 static void paste_x11_send(XSelectionRequestEvent *rq)
2428 XSelectionEvent *ptr = &(event.xselection);
2430 /* Set the event parameters. */
2431 ptr->type = SelectionNotify;
2432 ptr->property = rq->property;
2433 ptr->display = rq->display;
2434 ptr->requestor = rq->requestor;
2435 ptr->selection = rq->selection;
2436 ptr->target = rq->target;
2437 ptr->time = rq->time;
2439 /* Paste the appropriate information for each target type.
2440 * Note that this currently rejects MULTIPLE targets.
2443 if (rq->target == XA_STRING ||
2444 rq->target == xa_text ||
2445 rq->target == xa_compound_text)
2447 if (!paste_x11_send_text(rq))
2448 ptr->property = None;
2450 else if (rq->target == xa_targets)
2452 Atom target_list[4];
2453 target_list[0] = XA_STRING;
2454 target_list[1] = xa_text;
2455 target_list[2] = xa_compound_text;
2456 target_list[3] = xa_targets;
2457 XChangeProperty(DPY, rq->requestor, rq->property, rq->target,
2458 (8 * sizeof(target_list[0])), PropModeReplace,
2459 (unsigned char *)target_list,
2460 (sizeof(target_list) / sizeof(target_list[0])));
2462 else if (rq->target == xa_timestamp)
2464 XChangeProperty(DPY, rq->requestor, rq->property, rq->target,
2465 (8 * sizeof(Time)), PropModeReplace,
2466 (unsigned char *)s_ptr->time, 1);
2470 ptr->property = None;
2473 /* Send whatever event we're left with. */
2474 XSendEvent(DPY, rq->requestor, FALSE, NoEventMask, &event);
2479 * Handle various events conditional on presses of a mouse button.
2481 static void handle_button(Time time, int x, int y, int button,
2484 /* The co-ordinates are only used in Angband format. */
2485 pixel_to_square(&x, &y, x, y);
2487 if (press && button == 1) copy_x11_start(x, y);
2488 if (!press && button == 1) copy_x11_end(time);
2489 if (!press && button == 2) paste_x11_request(xa_compound_text, time);
2496 static errr CheckEvent(bool wait)
2498 term_data *old_td = (term_data*)(Term->data);
2500 XEvent xev_body, *xev = &xev_body;
2502 term_data *td = NULL;
2503 infowin *iwin = NULL;
2511 /* Do not wait unless requested */
2512 if (!wait && !XPending(Metadpy->dpy)) return (1);
2515 * Hack - redraw the selection, if needed.
2516 * This doesn't actually check that one of its squares was drawn to,
2517 * only that this may have happened.
2519 if (s_ptr->select && !s_ptr->drawn) mark_selection();
2521 /* Load the Event */
2522 XNextEvent(Metadpy->dpy, xev);
2525 /* #define DEBUG_EVENT */
2528 printf("event: type=%d", xev->type);
2531 printf("(KeyPress), keycode=%X", xev->xkey.keycode);
2534 printf("(FocusIn)");
2537 printf("(FocusOut)");
2539 case ReparentNotify:
2540 printf("(ReparentNotify)");
2542 case ConfigureNotify:
2543 printf("(ConfigureNotify)");
2546 printf("(MapNotify)");
2552 printf("(ClientMessage)");
2558 if (XFilterEvent(xev, xev->xany.window)
2559 /*XFilterEvent(xev, (data[0].win)->win)*/){
2561 printf(", [filtered by IM]\n");
2563 goto redo_checkevent;
2570 /* Notice new keymaps */
2571 if (xev->type == MappingNotify)
2573 XRefreshKeyboardMapping(&xev->xmapping);
2578 /* Scan the windows */
2579 for (i = 0; i < MAX_TERM_DATA; i++)
2581 if (!data[i].win) continue;
2582 if (xev->xany.window == data[i].win->win)
2590 /* Unknown window */
2591 if (!td || !iwin) return (0);
2594 /* Hack -- activate the Term */
2595 Term_activate(&td->t);
2597 /* Hack -- activate the window */
2601 /* Switch on the Type */
2607 bool press = (xev->type == ButtonPress);
2609 /* Where is the mouse */
2610 int x = xev->xbutton.x;
2611 int y = xev->xbutton.y;
2615 /* Which button is involved */
2616 if (xev->xbutton.button == Button1) z = 1;
2617 else if (xev->xbutton.button == Button2) z = 2;
2618 else if (xev->xbutton.button == Button3) z = 3;
2619 else if (xev->xbutton.button == Button4) z = 4;
2620 else if (xev->xbutton.button == Button5) z = 5;
2624 handle_button(xev->xbutton.time, x, y, z, press);
2639 /* Where is the mouse */
2640 int x = xev->xmotion.x;
2641 int y = xev->xmotion.y;
2642 unsigned int z = xev->xmotion.state;
2644 /* Convert to co-ordinates Angband understands. */
2645 pixel_to_square(&x, &y, x, y);
2647 /* Highlight the current square, if appropriate. */
2648 /* highlight_square(window, y, x); */
2650 /* Alter the selection if appropriate. */
2651 copy_x11_cont(x, y, z);
2658 case SelectionNotify:
2660 paste_x11_accept(&(xev->xselection));
2664 case SelectionRequest:
2666 paste_x11_send(&(xev->xselectionrequest));
2670 case SelectionClear:
2672 s_ptr->select = FALSE;
2685 /* Hack -- use "old" term */
2686 Term_activate(&old_td->t);
2688 /* Process the key */
2689 react_keypress(&(xev->xkey));
2698 /* Ignore "extra" exposes */
2699 /*if (xev->xexpose.count) break;*/
2701 /* Clear the window */
2704 x1 = (xev->xexpose.x - Infowin->ox)/Infofnt->wid;
2705 x2 = (xev->xexpose.x + xev->xexpose.width -
2706 Infowin->ox)/Infofnt->wid;
2708 y1 = (xev->xexpose.y - Infowin->oy)/Infofnt->hgt;
2709 y2 = (xev->xexpose.y + xev->xexpose.height -
2710 Infowin->oy)/Infofnt->hgt;
2712 Term_redraw_section(x1, y1, x2, y2);
2722 Infowin->mapped = 1;
2723 Term->mapped_flag = TRUE;
2729 Infowin->mapped = 0;
2730 Term->mapped_flag = FALSE;
2734 /* Move and/or Resize */
2735 case ConfigureNotify:
2737 int cols, rows, wid, hgt;
2739 int ox = Infowin->ox;
2740 int oy = Infowin->oy;
2742 /* Save the new Window Parms */
2743 Infowin->x = xev->xconfigure.x;
2744 Infowin->y = xev->xconfigure.y;
2745 Infowin->w = xev->xconfigure.width;
2746 Infowin->h = xev->xconfigure.height;
2748 /* Determine "proper" number of rows/cols */
2749 cols = ((Infowin->w - (ox + ox)) / td->fnt->wid);
2750 rows = ((Infowin->h - (oy + oy)) / td->fnt->hgt);
2752 /* Hack -- minimal size */
2753 if (cols < 1) cols = 1;
2754 if (rows < 1) rows = 1;
2758 /* Hack the main window must be at least 80x24 */
2759 if (cols < 80) cols = 80;
2760 if (rows < 24) rows = 24;
2763 /* Desired size of window */
2764 wid = cols * td->fnt->wid + (ox + ox);
2765 hgt = rows * td->fnt->hgt + (oy + oy);
2767 /* Resize the Term (if needed) */
2768 Term_resize(cols, rows);
2770 /* Resize the windows if any "change" is needed */
2771 if ((Infowin->w != wid) || (Infowin->h != hgt))
2774 Infowin_set(td->win);
2775 Infowin_resize(wid, hgt);
2784 XSetICFocus(iwin->xic);
2792 XUnsetICFocus(iwin->xic);
2794 /* Focuswin = NULL;*/
2801 /* Hack -- Activate the old term */
2802 Term_activate(&old_td->t);
2804 /* Hack -- Activate the proper window */
2805 Infowin_set(old_td->win);
2816 * An array of sound file names
2818 static cptr sound_file[SOUND_MAX];
2821 * Check for existance of a file
2823 static bool check_file(cptr s)
2827 fff = fopen(s, "r");
2828 if (!fff) return (FALSE);
2837 static void init_sound(void)
2842 char dir_xtra_sound[1024];
2844 /* Build the "sound" path */
2845 path_build(dir_xtra_sound, sizeof(dir_xtra_sound), ANGBAND_DIR_XTRA, "sound");
2847 /* Prepare the sounds */
2848 for (i = 1; i < SOUND_MAX; i++)
2850 /* Extract name of sound file */
2851 sprintf(wav, "%s.wav", angband_sound_name[i]);
2853 /* Access the sound */
2854 path_build(buf, sizeof(buf), dir_xtra_sound, wav);
2856 /* Save the sound filename, if it exists */
2857 if (check_file(buf)) sound_file[i] = string_make(buf);
2864 * Hack -- make a sound
2866 static errr Term_xtra_x11_sound(int v)
2870 /* Sound disabled */
2871 if (!use_sound) return (1);
2874 if ((v < 0) || (v >= SOUND_MAX)) return (1);
2877 if (!sound_file[v]) return (1);
2879 sprintf(buf,"./playwave.sh %s\n", sound_file[v]);
2885 #endif /* USE_SOUND */
2889 * Handle "activation" of a term
2891 static errr Term_xtra_x11_level(int v)
2893 term_data *td = (term_data*)(Term->data);
2895 /* Handle "activate" */
2898 /* Activate the window */
2899 Infowin_set(td->win);
2901 /* Activate the font */
2902 Infofnt_set(td->fnt);
2903 #ifdef USE_JP_FONTSTRUCT
2904 Infokfnt_set(td->kfnt);
2916 static errr Term_xtra_x11_react(void)
2922 /* Check the colors */
2923 for (i = 0; i < 256; i++)
2925 if ((color_table[i][0] != angband_color_table[i][0]) ||
2926 (color_table[i][1] != angband_color_table[i][1]) ||
2927 (color_table[i][2] != angband_color_table[i][2]) ||
2928 (color_table[i][3] != angband_color_table[i][3]))
2932 /* Save new values */
2933 color_table[i][0] = angband_color_table[i][0];
2934 color_table[i][1] = angband_color_table[i][1];
2935 color_table[i][2] = angband_color_table[i][2];
2936 color_table[i][3] = angband_color_table[i][3];
2939 pixel = create_pixel(Metadpy->dpy,
2944 /* Change the foreground */
2945 Infoclr_set(clr[i]);
2946 Infoclr_change_fg(pixel);
2957 * Handle a "special request"
2959 static errr Term_xtra_x11(int n, int v)
2961 /* Handle a subset of the legal requests */
2965 case TERM_XTRA_NOISE: Metadpy_do_beep(); return (0);
2968 /* Make a special sound */
2969 case TERM_XTRA_SOUND: return (Term_xtra_x11_sound(v));
2972 /* Flush the output XXX XXX */
2973 case TERM_XTRA_FRESH: Metadpy_update(1, 0, 0); return (0);
2975 /* Process random events XXX */
2976 case TERM_XTRA_BORED: return (CheckEvent(0));
2978 /* Process Events XXX */
2979 case TERM_XTRA_EVENT: return (CheckEvent(v));
2981 /* Flush the events XXX */
2982 case TERM_XTRA_FLUSH: while (!CheckEvent(FALSE)); return (0);
2984 /* Handle change in the "level" */
2985 case TERM_XTRA_LEVEL: return (Term_xtra_x11_level(v));
2987 /* Clear the screen */
2988 case TERM_XTRA_CLEAR: Infowin_wipe(); s_ptr->drawn = FALSE; return (0);
2990 /* Delay for some milliseconds */
2991 case TERM_XTRA_DELAY: usleep(1000 * v); return (0);
2993 /* React to changes */
2994 case TERM_XTRA_REACT: return (Term_xtra_x11_react());
3003 * Draw the cursor as an inverted rectangle.
3005 * Consider a rectangular outline like "main-mac.c". XXX XXX
3007 static errr Term_curs_x11(int x, int y)
3011 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
3012 x * Infofnt->wid + Infowin->ox,
3013 y * Infofnt->hgt + Infowin->oy,
3014 Infofnt->wid - 1, Infofnt->hgt - 1);
3015 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
3016 x * Infofnt->wid + Infowin->ox + 1,
3017 y * Infofnt->hgt + Infowin->oy + 1,
3018 Infofnt->wid - 3, Infofnt->hgt - 3);
3022 /* Draw the cursor */
3025 /* Hilite the cursor character */
3026 Infofnt_text_non(x, y, " ", 1);
3035 * Draw the double width cursor
3037 static errr Term_bigcurs_x11(int x, int y)
3041 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
3042 x * Infofnt->wid + Infowin->ox,
3043 y * Infofnt->hgt + Infowin->oy,
3044 Infofnt->twid - 1, Infofnt->hgt - 1);
3045 XDrawRectangle(Metadpy->dpy, Infowin->win, xor->gc,
3046 x * Infofnt->wid + Infowin->ox + 1,
3047 y * Infofnt->hgt + Infowin->oy + 1,
3048 Infofnt->twid - 3, Infofnt->hgt - 3);
3052 /* Draw the cursor */
3055 /* Hilite the cursor character */
3056 Infofnt_text_non(x, y, " ", 2);
3064 * Erase some characters.
3066 static errr Term_wipe_x11(int x, int y, int n)
3068 /* Erase (use black) */
3069 Infoclr_set(clr[TERM_DARK]);
3071 /* Mega-Hack -- Erase some space */
3072 Infofnt_text_non(x, y, "", n);
3074 /* Redraw the selection if any, as it may have been obscured. (later) */
3075 s_ptr->drawn = FALSE;
3083 * Draw some textual characters.
3085 static errr Term_text_x11(int x, int y, int n, byte a, cptr s)
3088 Infoclr_set(clr[a]);
3091 Infofnt_text_std(x, y, s, n);
3093 /* Redraw the selection if any, as it may have been obscured. (later) */
3094 s_ptr->drawn = FALSE;
3104 * Draw some graphical characters.
3106 static errr Term_pict_x11(int x, int y, int n, const byte *ap, const char *cp, const byte *tap, const char *tcp)
3119 unsigned long pixel, blank;
3121 term_data *td = (term_data*)(Term->data);
3126 /* Add in affect of window boundaries */
3130 for (i = 0; i < n; ++i, x += td->fnt->wid)
3135 /* For extra speed - cache these values */
3136 x1 = (c&0x7F) * td->fnt->twid;
3137 y1 = (a&0x7F) * td->fnt->hgt;
3139 /* Illegal tile index */
3140 if (td->tiles->width < x1 + td->fnt->wid ||
3141 td->tiles->height < y1 + td->fnt->hgt)
3143 /* Draw black square */
3144 XFillRectangle(Metadpy->dpy, td->win->win, clr[0]->gc,
3146 td->fnt->twid, td->fnt->hgt);
3148 /* Skip drawing tile */
3155 /* For extra speed - cache these values */
3156 x2 = (tc&0x7F) * td->fnt->twid;
3157 y2 = (ta&0x7F) * td->fnt->hgt;
3159 /* Optimise the common case */
3160 if (((x1 == x2) && (y1 == y2)) ||
3161 !(((byte)ta & 0x80) && ((byte)tc & 0x80)) ||
3162 td->tiles->width < x2 + td->fnt->wid ||
3163 td->tiles->height < y2 + td->fnt->hgt)
3165 /* Draw object / terrain */
3166 XPutImage(Metadpy->dpy, td->win->win,
3171 td->fnt->twid, td->fnt->hgt);
3176 /* Mega Hack^2 - assume the top left corner is "black" */
3177 blank = XGetPixel(td->tiles, 0, td->fnt->hgt * 6);
3179 for (k = 0; k < td->fnt->twid; k++)
3181 for (l = 0; l < td->fnt->hgt; l++)
3183 /* If mask set... */
3184 if ((pixel = XGetPixel(td->tiles, x1 + k, y1 + l)) == blank)
3186 /* Output from the terrain */
3187 pixel = XGetPixel(td->tiles, x2 + k, y2 + l);
3190 /* Store into the temp storage. */
3191 XPutPixel(td->TmpImage, k, l, pixel);
3196 /* Draw to screen */
3198 XPutImage(Metadpy->dpy, td->win->win,
3202 td->fnt->twid, td->fnt->hgt);
3206 /* Redraw the selection if any, as it may have been obscured. (later) */
3207 s_ptr->drawn = FALSE;
3213 #endif /* USE_GRAPHICS */
3216 static void IMDestroyCallback(XIM, XPointer, XPointer);
3219 IMInstantiateCallback(Display *display, XPointer unused1, XPointer unused2)
3222 XIMCallback ximcallback;
3223 XIMStyles *xim_styles = NULL;
3230 xim = XOpenIM(display, NULL, NULL, NULL);
3232 printf("can't open IM\n");
3236 /* initialize destroy callback */
3237 ximcallback.callback = IMDestroyCallback;
3238 ximcallback.client_data = NULL;
3239 XSetIMValues(xim, XNDestroyCallback, &ximcallback, NULL);
3241 /* set style (only "Root" is supported yet...) */
3242 XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL);
3243 for (i = 0; i < xim_styles->count_styles; i++){
3244 if(xim_styles->supported_styles[i] == (XIMPreeditNothing | XIMStatusNothing)) break;
3246 if(i >= xim_styles->count_styles){
3247 printf("Sorry, your IM does not support 'Root' preedit style...\n");
3255 for (i = 0; i < MAX_TERM_DATA; i++)
3257 infowin *iwin = data[i].win;
3258 if (!iwin) continue;
3259 iwin->xic = XCreateIC(xim, XNInputStyle, (XIMPreeditNothing | XIMStatusNothing), XNClientWindow, iwin->win, XNFocusWindow, iwin->win, NULL);
3261 printf("Can't create input context for Term%d\n", i);
3264 if(XGetICValues(iwin->xic, XNFilterEvents, &iwin->xic_mask, NULL) != NULL){
3265 /* printf("Can't get XNFilterEvents\n"); */
3266 iwin->xic_mask = 0L;
3268 XSelectInput(Metadpy->dpy, iwin->win, iwin->mask | iwin->xic_mask);
3274 static void IMDestroyCallback(XIM xim, XPointer client_data, XPointer call_data)
3282 if (call_data == NULL){
3283 XRegisterIMInstantiateCallback(Metadpy->dpy, NULL, NULL, NULL, IMInstantiateCallback, NULL);
3286 for(i = 0; i < MAX_TERM_DATA; i++)
3288 infowin *iwin = data[i].win;
3291 XSelectInput(Metadpy->dpy, iwin->win, iwin->mask);
3292 iwin->xic_mask = 0L;
3297 Metadpy->xim = NULL;
3302 * Initialize a term_data
3304 static errr term_data_init(term_data *td, int i)
3308 cptr name = angband_term_name[i];
3311 #ifdef USE_JP_FONTSTRUCT
3343 /* Window specific font name */
3344 sprintf(buf, "ANGBAND_X11_FONT_%d", i);
3346 /* Check environment for that font */
3349 /* Check environment for "base" font */
3350 if (!font) font = getenv("ANGBAND_X11_FONT");
3352 /* No environment variables, use default font */
3359 font = DEFAULT_X11_FONT_0;
3364 font = DEFAULT_X11_FONT_1;
3369 font = DEFAULT_X11_FONT_2;
3374 font = DEFAULT_X11_FONT_3;
3379 font = DEFAULT_X11_FONT_4;
3384 font = DEFAULT_X11_FONT_5;
3389 font = DEFAULT_X11_FONT_6;
3394 font = DEFAULT_X11_FONT_7;
3399 font = DEFAULT_X11_FONT;
3404 #ifdef USE_JP_FONTSTRUCT
3405 /* Window specific font name */
3406 sprintf(buf, "ANGBAND_X11_KFONT_%d", i);
3408 /* Check environment for that font */
3409 kfont = getenv(buf);
3411 /* Check environment for "base" font */
3412 if (!kfont) kfont = getenv("ANGBAND_X11_KFONT");
3414 /* No environment variables, use default font */
3421 kfont = DEFAULT_X11_KFONT_0;
3426 kfont = DEFAULT_X11_KFONT_1;
3431 kfont = DEFAULT_X11_KFONT_2;
3436 kfont = DEFAULT_X11_KFONT_3;
3441 kfont = DEFAULT_X11_KFONT_4;
3446 kfont = DEFAULT_X11_KFONT_5;
3451 kfont = DEFAULT_X11_KFONT_6;
3456 kfont = DEFAULT_X11_KFONT_7;
3461 kfont = DEFAULT_X11_KFONT;
3466 /* Window specific location (x) */
3467 sprintf(buf, "ANGBAND_X11_AT_X_%d", i);
3469 x = (str != NULL) ? atoi(str) : -1;
3471 /* Window specific location (y) */
3472 sprintf(buf, "ANGBAND_X11_AT_Y_%d", i);
3474 y = (str != NULL) ? atoi(str) : -1;
3477 /* Window specific cols */
3478 sprintf(buf, "ANGBAND_X11_COLS_%d", i);
3480 val = (str != NULL) ? atoi(str) : -1;
3481 if (val > 0) cols = val;
3483 /* Window specific rows */
3484 sprintf(buf, "ANGBAND_X11_ROWS_%d", i);
3486 val = (str != NULL) ? atoi(str) : -1;
3487 if (val > 0) rows = val;
3489 /* Hack the main window must be at least 80x24 */
3492 if (cols < 80) cols = 80;
3493 if (rows < 24) rows = 24;
3496 /* Window specific inner border offset (ox) */
3497 sprintf(buf, "ANGBAND_X11_IBOX_%d", i);
3499 val = (str != NULL) ? atoi(str) : -1;
3500 if (val > 0) ox = val;
3502 /* Window specific inner border offset (oy) */
3503 sprintf(buf, "ANGBAND_X11_IBOY_%d", i);
3505 val = (str != NULL) ? atoi(str) : -1;
3506 if (val > 0) oy = val;
3509 /* Prepare the standard font */
3510 #ifdef USE_JP_FONTSTRUCT
3511 MAKE(td->fnt, infofnt);
3512 Infofnt_set(td->fnt);
3513 MAKE(td->kfnt, infofnt);
3514 Infokfnt_set(td->kfnt);
3515 Infofnt_init_data(font, kfont);
3517 MAKE(td->fnt, infofnt);
3518 Infofnt_set(td->fnt);
3519 Infofnt_init_data(font);
3523 /* Hack -- key buffer size */
3524 num = ((i == 0) ? 1024 : 16);
3526 /* Assume full size windows */
3527 wid = cols * td->fnt->wid + (ox + ox);
3528 hgt = rows * td->fnt->hgt + (oy + oy);
3530 /* Create a top-window */
3531 MAKE(td->win, infowin);
3532 Infowin_set(td->win);
3533 Infowin_init_top(x, y, wid, hgt, 0,
3534 Metadpy->fg, Metadpy->bg);
3536 /* Ask for certain events */
3537 #if defined(USE_XIM)
3538 Infowin_set_mask(ExposureMask | StructureNotifyMask | KeyPressMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask);
3540 Infowin_set_mask(ExposureMask | StructureNotifyMask | KeyPressMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask);
3543 /* Set the window name */
3544 Infowin_set_name(name);
3546 /* Save the inner border */
3550 /* Make Class Hints */
3551 ch = XAllocClassHint();
3553 if (ch == NULL) quit("XAllocClassHint failed");
3555 strcpy(res_name, name);
3556 res_name[0] = FORCELOWER(res_name[0]);
3557 ch->res_name = res_name;
3559 strcpy(res_class, "Angband");
3560 ch->res_class = res_class;
3562 XSetClassHint(Metadpy->dpy, Infowin->win, ch);
3564 /* Make Size Hints */
3565 sh = XAllocSizeHints();
3568 if (sh == NULL) quit("XAllocSizeHints failed");
3570 /* Main window has a differing minimum size */
3573 /* Main window min size is 80x24 */
3574 sh->flags = PMinSize | PMaxSize;
3575 sh->min_width = 80 * td->fnt->wid + (ox + ox);
3576 sh->min_height = 24 * td->fnt->hgt + (oy + oy);
3577 sh->max_width = 255 * td->fnt->wid + (ox + ox);
3578 sh->max_height = 255 * td->fnt->hgt + (oy + oy);
3581 /* Other windows can be shrunk to 1x1 */
3585 sh->flags = PMinSize | PMaxSize;
3586 sh->min_width = td->fnt->wid + (ox + ox);
3587 sh->min_height = td->fnt->hgt + (oy + oy);
3588 sh->max_width = 256 * td->fnt->wid + (ox + ox);
3589 sh->max_height = 256 * td->fnt->hgt + (oy + oy);
3592 /* Resize increment */
3593 sh->flags |= PResizeInc;
3594 sh->width_inc = td->fnt->wid;
3595 sh->height_inc = td->fnt->hgt;
3597 /* Base window size */
3598 sh->flags |= PBaseSize;
3599 sh->base_width = (ox + ox);
3600 sh->base_height = (oy + oy);
3602 /* Use the size hints */
3603 XSetWMNormalHints(Metadpy->dpy, Infowin->win, sh);
3605 /* Map the window */
3610 wh = XAllocWMHints();
3611 if(wh == NULL) quit("XAllocWMHints failed");
3612 wh->flags = InputHint;
3614 XSetWMHints(Metadpy->dpy, Infowin->win, wh);
3617 /* Move the window to requested location */
3618 if ((x >= 0) && (y >= 0)) Infowin_impell(x, y);
3621 /* Initialize the term */
3622 term_init(t, cols, rows, num);
3624 /* Use a "soft" cursor */
3625 t->soft_cursor = TRUE;
3627 /* Erase with "white space" */
3628 t->attr_blank = TERM_WHITE;
3629 t->char_blank = ' ';
3632 t->xtra_hook = Term_xtra_x11;
3633 t->curs_hook = Term_curs_x11;
3634 t->bigcurs_hook = Term_bigcurs_x11;
3635 t->wipe_hook = Term_wipe_x11;
3636 t->text_hook = Term_text_x11;
3641 /* Activate (important) */
3650 * Initialization function for an "X11" module to Angband
3652 errr init_x11(int argc, char *argv[])
3662 char filename[1024];
3668 #endif /* USE_GRAPHICS */
3672 for (i = 1; i < argc; i++)
3674 if (prefix(argv[i], "-d"))
3676 dpy_name = &argv[i][2];
3681 if (prefix(argv[i], "-s"))
3683 smoothRescaling = FALSE;
3687 if (prefix(argv[i], "-a"))
3689 arg_graphics = GRAPHICS_ADAM_BOLT;
3693 if (prefix(argv[i], "-o"))
3695 arg_graphics = GRAPHICS_ORIGINAL;
3699 if (prefix(argv[i], "-b"))
3701 arg_bigtile = use_bigtile = TRUE;
3704 #endif /* USE_GRAPHICS */
3706 if (prefix(argv[i], "-n"))
3708 num_term = atoi(&argv[i][2]);
3709 if (num_term > MAX_TERM_DATA) num_term = MAX_TERM_DATA;
3710 else if (num_term < 1) num_term = 1;
3714 if (prefix(argv[i], "--"))
3720 plog_fmt("Ignoring option: %s", argv[i]);
3724 setlocale(LC_ALL, "");
3725 #ifdef DEFAULT_LOCALE
3726 if(!strcmp(setlocale(LC_ALL, NULL), "C")){
3727 printf("try default locale \"%s\"\n", DEFAULT_LOCALE);
3728 setlocale(LC_ALL, DEFAULT_LOCALE);
3733 if(!strcmp(setlocale(LC_ALL, NULL), "C"))
3735 printf("WARNING: Locale is not supported. Non-english font may be displayed incorrectly.\n");
3739 if(!XSupportsLocale()){
3740 printf("can't support locale in X\n");
3741 setlocale(LC_ALL, "C");
3743 #endif /* USE_LOCALE */
3746 /* Init the Metadpy if possible */
3747 if (Metadpy_init_name(dpy_name)) return (-1);
3750 /* Prepare cursor color */
3753 Infoclr_init_ppn(Metadpy->fg, Metadpy->bg, "xor", 0);
3756 /* Prepare normal colors */
3757 for (i = 0; i < 256; ++i)
3761 MAKE(clr[i], infoclr);
3763 Infoclr_set(clr[i]);
3765 /* Acquire Angband colors */
3766 color_table[i][0] = angband_color_table[i][0];
3767 color_table[i][1] = angband_color_table[i][1];
3768 color_table[i][2] = angband_color_table[i][2];
3769 color_table[i][3] = angband_color_table[i][3];
3771 /* Default to monochrome */
3772 pixel = ((i == 0) ? Metadpy->bg : Metadpy->fg);
3778 pixel = create_pixel(Metadpy->dpy,
3784 /* Initialize the color */
3785 Infoclr_init_ppn(pixel, Metadpy->bg, "cpy", 0);
3789 /* Prepare required atoms. */
3793 /* Initialize the windows */
3794 for (i = 0; i < num_term; i++)
3796 term_data *td = &data[i];
3798 /* Initialize the term_data */
3799 term_data_init(td, i);
3801 /* Save global entry */
3802 angband_term[i] = Term;
3805 /* Raise the "Angband" window */
3806 Infowin_set(data[0].win);
3809 /* Activate the "Angband" window screen */
3810 Term_activate(&data[0].t);
3815 p = XSetLocaleModifiers("");
3817 p = XSetLocaleModifiers("@im=");
3819 /* printf("XMODIFIERS=\"%s\"\n", p); */
3821 XRegisterIMInstantiateCallback(Metadpy->dpy, NULL, NULL, NULL, IMInstantiateCallback, NULL);
3825 /* initialize sound */
3826 if (arg_sound) init_sound();
3832 switch (arg_graphics)
3834 case GRAPHICS_ORIGINAL:
3835 /* Try the "8x8.bmp" file */
3836 path_build(filename, sizeof(filename), ANGBAND_DIR_XTRA, "graf/8x8.bmp");
3838 /* Use the "8x8.bmp" file if it exists */
3839 if (0 == fd_close(fd_open(filename, O_RDONLY)))
3842 use_graphics = TRUE;
3844 pict_wid = pict_hgt = 8;
3846 ANGBAND_GRAF = "old";
3851 case GRAPHICS_ADAM_BOLT:
3852 /* Try the "16x16.bmp" file */
3853 path_build(filename, sizeof(filename), ANGBAND_DIR_XTRA, "graf/16x16.bmp");
3855 /* Use the "16x16.bmp" file if it exists */
3856 if (0 == fd_close(fd_open(filename, O_RDONLY)))
3859 use_graphics = TRUE;
3861 pict_wid = pict_hgt = 16;
3863 ANGBAND_GRAF = "new";
3872 Display *dpy = Metadpy->dpy;
3876 /* Load the graphical tiles */
3877 tiles_raw = ReadBMP(dpy, filename);
3879 /* Initialize the windows */
3880 for (i = 0; i < num_term; i++)
3882 term_data *td = &data[i];
3887 t->pict_hook = Term_pict_x11;
3889 /* Use graphics sometimes */
3890 t->higher_pict = TRUE;
3894 ResizeImage(dpy, tiles_raw,
3896 td->fnt->twid, td->fnt->hgt);
3899 /* Initialize the transparency masks */
3900 for (i = 0; i < num_term; i++)
3902 term_data *td = &data[i];
3904 int depth = DefaultDepth(dpy, DefaultScreen(dpy));
3905 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
3909 /* Determine total bytes needed for image */
3911 jj = (depth - 1) >> 2;
3912 while (jj >>= 1) ii <<= 1;
3913 total = td->fnt->twid * td->fnt->hgt * ii;
3916 TmpData = (char *)malloc(total);
3918 td->TmpImage = XCreateImage(dpy,visual,depth,
3919 ZPixmap, 0, TmpData,
3920 td->fnt->twid, td->fnt->hgt, 8, 0);
3924 /* Free tiles_raw? XXX XXX */
3927 #endif /* USE_GRAPHICS */
3934 #endif /* USE_X11 */