OSDN Git Service

#37287 (2.2.0.40) cmd1.c内のC4457警告に対応。 / Deal C4457 warning in cmd1.c.
[hengband/hengband.git] / src / main-x11.c
index a5ec992..c7f4999 100644 (file)
@@ -1,4 +1,4 @@
-/* File: main-x11.c */
+/* File: main-x11.c */
 
 /*
  * Copyright (c) 1997 Ben Harrison, and others
@@ -9,17 +9,17 @@
  */
 
 
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
 /*
- * ÆüËܸì(EUC-JAPAN)Âбþ (-DJP)
- *    ¡¦´Á»ú¥Õ¥©¥ó¥È¤Î°·¤¤¤òÄɲÃ
- *    ¡¦ÆüËܸì¤ò´Þ¤àʸ»úÎó¤Îɽ¼¨¥ë¡¼¥Á¥ó XDrawMultiString() ¤ÎÄɲÃ
- *    ¡¦ÆüËܸì¤Îɽ¼¨Éý¤Ï¡¤¥Õ¥©¥ó¥È¤Î¾ðÊó¤Ë¤è¤é¤¹ASCII¥Õ¥©¥ó¥È¤Î2Çܤ˸ÇÄê
+ * 日本語(EUC-JAPAN)対応 (-DJP)
+ *    ・漢字フォントの扱いを追加
+ *    ・日本語を含む文字列の表示ルーチン XDrawMultiString() の追加
+ *    ・日本語の表示幅は,フォントの情報によらすASCIIフォントの2倍に固定
  *
- * Ì¤Âбþ
- *      EUCȾ³Ñ¤Î°·¤¤
+ * 未対応
+ *      EUC半角の扱い
  *
- * 1996/6/7  Íû ¹¸¿­ (ri@kuis.kyoto-u.ac.jp)
+ * 1996/6/7  李 晃伸 (ri@kuis.kyoto-u.ac.jp)
  */
 #endif
 /*
@@ -126,7 +126,7 @@ char *XSetIMValues(XIM, ...); /* Hack for XFree86 4.0 */
 #include <X11/Xatom.h>
 #endif /* __MAKEDEPEND__ */
 
-
+#include <iconv.h>
 /*
  * Include some helpful X11 code.
  */
@@ -456,7 +456,7 @@ static infowin *Infowin = (infowin*)(NULL);
 static infowin *Focuswin = (infowin*)(NULL);
 #endif
 static infoclr *Infoclr = (infoclr*)(NULL);
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
 static infofnt *Infofnt = (infofnt*)(NULL);
 static infofnt *Infokfnt = (infofnt*)(NULL);
 #else
@@ -469,7 +469,7 @@ static infofnt *Infofnt = (infofnt*)(NULL);
 /**** Generic code ****/
 
 
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
 #define Infokfnt_set(I) \
        (Infokfnt = (I))
 #endif
@@ -1228,7 +1228,7 @@ static errr Infofnt_nuke(void)
 {
        infofnt *ifnt = Infofnt;
 
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        infofnt *ikfnt = Infokfnt;
 #endif
        /* Deal with 'name' */
@@ -1238,21 +1238,26 @@ static errr Infofnt_nuke(void)
                string_free(ifnt->name);
        }
 
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        if (ikfnt->name)
        {
                /* Free the name */
                string_free(ikfnt->name);
        }
 #endif
+
        /* Nuke info if needed */
        if (ifnt->nuke)
        {
                /* Free the font */
+#ifdef USE_FONTSET
+               XFreeFontSet(Metadpy->dpy, ifnt->info);
+#else
                XFreeFont(Metadpy->dpy, ifnt->info);
+#endif
        }
 
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        if (ikfnt->nuke)
        {
                /* Free the font */
@@ -1269,7 +1274,7 @@ static errr Infofnt_nuke(void)
 /*
  * Prepare a new 'infofnt'
  */
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
 static errr Infofnt_prepare(XFontStruct *info, XFontStruct *kinfo)
 #else
 #ifdef USE_FONTSET
@@ -1282,7 +1287,7 @@ static errr Infofnt_prepare(XFontStruct *info)
 {
        infofnt *ifnt = Infofnt;
 
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        infofnt *ikfnt = Infokfnt;
 #endif
        XCharStruct *cs;
@@ -1305,7 +1310,7 @@ static errr Infofnt_prepare(XFontStruct *info)
                if(ascent < (*fontinfo)->ascent) ascent = (*fontinfo)->ascent;
                if(descent < (*fontinfo)->descent) descent = (*fontinfo)->descent;
                if(((*fontinfo)->max_byte1) > 0){
-                       /* Â¿¥Ð¥¤¥Èʸ»ú¤Î¾ì¹ç¤ÏÉýȾʬ(ü¿ôÀÚ¤ê¾å¤²)¤Çɾ²Á¤¹¤ë */
+                       /* 多バイト文字の場合は幅半分(端数切り上げ)で評価する */
                        if(width < (cs->width+1)/2) width = (cs->width+1)/2;
                }else{
                        if(width < cs->width) width = cs->width;
@@ -1331,7 +1336,7 @@ static errr Infofnt_prepare(XFontStruct *info)
        else
                ifnt->twid = ifnt->wid;
 
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
     /* Assign the struct */
     ikfnt->info = kinfo;
  
@@ -1362,7 +1367,7 @@ static errr Infofnt_prepare(XFontStruct *info)
 /*
  * Initialize a new 'infofnt'.
  */
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
 static errr Infofnt_init_real(XFontStruct *info, XFontStruct *kinfo)
 #else
 #ifdef USE_FONTSET
@@ -1376,17 +1381,17 @@ static errr Infofnt_init_real(XFontStruct *info)
        /* Wipe the thing */
        (void)WIPE(Infofnt, infofnt);
 
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        WIPE(Infokfnt, infofnt);
 #endif
        /* No nuking */
        Infofnt->nuke = 0;
 
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        Infokfnt->nuke = 0;
 #endif
        /* Attempt to prepare it */
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        return (Infofnt_prepare (info, kinfo));
 #else
        return (Infofnt_prepare(info));
@@ -1403,7 +1408,7 @@ static errr Infofnt_init_real(XFontStruct *info)
  * Inputs:
  *     name: The name of the requested Font
  */
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
 static void Infofnt_init_data(cptr name, cptr kname)
 #else
 static void Infofnt_init_data(cptr name)
@@ -1420,7 +1425,7 @@ static void Infofnt_init_data(cptr name)
 #endif
 
 
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        XFontStruct *kinfo;
 #endif
        /*** Load the info Fresh, using the name ***/
@@ -1428,7 +1433,7 @@ static void Infofnt_init_data(cptr name)
        /* If the name is not given, report an error */
        if (!name || !*name) quit("Missing font!");
 
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        if (!kname || !*kname) quit("Missing kanji font!");
 #endif
        /* Attempt to load the font */
@@ -1443,7 +1448,7 @@ static void Infofnt_init_data(cptr name)
        }
 #else
        info = XLoadQueryFont(Metadpy->dpy, name);
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        kinfo = XLoadQueryFont(Metadpy->dpy, kname);
 #endif
 #endif
@@ -1451,7 +1456,7 @@ static void Infofnt_init_data(cptr name)
 
        /* The load failed, try to recover */
        if (!info) quit_fmt("Failed to find font:\"%s\"", name);
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        if (!kinfo) quit_fmt("Failed to find font:\"%s\"", kname);
 #endif
 
@@ -1462,11 +1467,11 @@ static void Infofnt_init_data(cptr name)
        /* Wipe the thing */
        (void)WIPE(Infofnt, infofnt);
 
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        WIPE(Infokfnt, infofnt);
 #endif
        /* Attempt to prepare it */
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        if (Infofnt_prepare(info, kinfo))
 #else
        if (Infofnt_prepare(info))
@@ -1478,7 +1483,7 @@ static void Infofnt_init_data(cptr name)
                XFreeFontSet(Metadpy->dpy, info);
 #else
                XFreeFont(Metadpy->dpy, info);
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
                XFreeFont(Metadpy->dpy, kinfo);
 #endif
 #endif
@@ -1488,21 +1493,21 @@ static void Infofnt_init_data(cptr name)
 
        /* Save a copy of the font name */
        Infofnt->name = string_make(name);
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        Infokfnt->name = string_make(kname);
 #endif
 
        /* Mark it as nukable */
        Infofnt->nuke = 1;
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        Infokfnt->nuke = 1;
 #endif
 }
 
 
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
 /*
- * EUCÆüËܸ쥳¡¼¥É¤ò´Þ¤àʸ»úÎó¤òɽ¼¨¤¹¤ë (Xlib)
+ * EUC日本語コードを含む文字列を表示する (Xlib)
  */
 static void
 XDrawMultiString(display,d,gc, x, y, string, len, afont, 
@@ -1531,48 +1536,48 @@ XDrawMultiString(display,d,gc, x, y, string, len, afont,
 #ifdef TOFU      
       if ( (*str) == 0x7f ) {
          
-         /* 0x7F¤Ï¢£¤Ç·è¤áÂǤÁ */
+         /* 0x7Fは■で決め打ち */
          
-         /* Ï¢Â³¤¹¤ë0x7F¤ÎŤµ¤ò¸¡½Ð */
+         /* 連続する0x7Fの長さを検出 */
          slen = 0;
          while ( str < endp && (*str) == 0x7f ) {
              slen++; 
              str++;
          }
          
-         /* ÉÁ²è */
+         /* 描画 */
          XFillRectangle( display, d, gc, x, y-afont_ascent, 
                          slen * afont_width, afont_height);
  
-         /* ¥Ý¥¤¥ó¥¿¤ò¿Ê¤á¤ë */
+         /* ポインタを進める */
          x += afont_width * slen;
       } 
       else  
 #endif
       if ( iskanji(*str) ) {
          
-         /* UJIS¤Î»Ï¤Þ¤ê */
+         /* UJISの始まり */
          
-         /* Ï¢Â³¤¹¤ëUJISʸ»ú¤ÎŤµ¤ò¸¡½Ð */
+         /* 連続するUJIS文字の長さを検出 */
          slen = 0;
          while ( str < endp && *str && iskanji(*str) ) {
              kanji[slen].byte1 = *str++ & 0x7f;
              kanji[slen++].byte2 = *str++ & 0x7f;
          }
          
-         /* ÉÁ²è */
+         /* 描画 */
          XSetFont( display, gc, kfont->fid );
          XDrawImageString16( display, d, gc, x, y, kanji, slen );
 
  
-         /* ¥Ý¥¤¥ó¥¿¤ò¿Ê¤á¤ë */
+         /* ポインタを進める */
          x += kfont_width * slen;
          
       } else {
          
-         /* Èó´Á»ú(=ASCII¤È²¾Äê)¤Î»Ï¤Þ¤ê */
+         /* 非漢字(=ASCIIと仮定)の始まり */
          
-         /* Ï¢Â³¤¹¤ëASCIIʸ»ú¤ò¸¡½Ð */
+         /* 連続するASCII文字を検出 */
          p = str;
          slen = 0;
          while ( str < endp && *str && !iskanji(*str) ) {
@@ -1583,11 +1588,11 @@ XDrawMultiString(display,d,gc, x, y, string, len, afont,
              slen++;
          }
          
-         /* ÉÁ²è */
+         /* 描画 */
          XSetFont( display, gc, afont->fid );
          XDrawImageString( display, d, gc, x, y, p, slen );
          
-         /* ¥Ý¥¤¥ó¥¿¤ò¿Ê¤á¤ë */
+         /* ポインタを進める */
          x += afont_width * slen;
       }
     }
@@ -1624,19 +1629,17 @@ static errr Infofnt_text_std(int x, int y, cptr str, int len)
 
        /*** Actually draw 'str' onto the infowin ***/
 
-#if 1
-#ifndef JP
+#ifndef USE_FONTSET
        /* Be sure the correct font is ready */
        XSetFont(Metadpy->dpy, Infoclr->gc, Infofnt->info->fid);
 #endif
-#endif
 
        /*** Handle the fake mono we can enforce on fonts ***/
 
        /* Monotize the font */
        if (Infofnt->mono)
        {
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
                /* Be sure the correct font is ready */
                XSetFont(Metadpy->dpy, Infoclr->gc, Infofnt->info->fid);
 #endif
@@ -1653,8 +1656,8 @@ static errr Infofnt_text_std(int x, int y, cptr str, int len)
        else
        {
                /* Note that the Infoclr is set up to contain the Infofnt */
-#ifdef _JP
-               /* ´Á»ú¥Õ¥©¥ó¥È¤Îɽ¼¨Éý¤Ï ASCII¥Õ¥©¥ó¥È¤Î2Çܤ˸ÇÄê */
+#ifdef USE_JP_FONTSTRUCT
+               /* 漢字フォントの表示幅は ASCIIフォントの2倍に固定 */
                XDrawMultiString(Metadpy->dpy, Infowin->win, Infoclr->gc,
                                 x, y, str, len,
                                 Infofnt->info, Infofnt->wid, Infofnt->hgt,
@@ -1662,8 +1665,18 @@ static errr Infofnt_text_std(int x, int y, cptr str, int len)
                                 Infokfnt->info, Infofnt->wid * 2);
 #else
 #ifdef USE_FONTSET
+
+               iconv_t cd = iconv_open("UTF-8", "EUC-JP");
+               size_t inlen = len;
+               size_t outlen = len * 2;
+               char *kanji = malloc(outlen);
+               char *sp = str; char *kp = kanji;
+               size_t n = iconv(cd, &sp, &inlen, &kp, &outlen);
+               iconv_close(cd);
+
                XmbDrawImageString(Metadpy->dpy, Infowin->win, Infofnt->info,
-                                  Infoclr->gc, x, y, str, len);
+                               Infoclr->gc, x, y, kanji, kp-kanji);
+               free(kanji);
 #else
                XDrawImageString(Metadpy->dpy, Infowin->win, Infoclr->gc,
                                 x, y, str, len);
@@ -1757,7 +1770,7 @@ struct term_data
        term t;
 
        infofnt *fnt;
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        infofnt *kfnt;
 #endif
 
@@ -1768,15 +1781,11 @@ struct term_data
 
        XImage *tiles;
 
-#ifdef USE_TRANSPARENCY
-
        /* Tempory storage for overlaying tiles. */
        XImage *TmpImage;
 
 #endif
 
-#endif
-
 };
 
 
@@ -2133,23 +2142,152 @@ static void copy_x11_end(const Time time)
        }
 }
 
+
+static Atom xa_targets, xa_timestamp, xa_text, xa_compound_text;
+
+/*
+ * Set the required variable atoms at start-up to avoid errors later.
+ */
+static void set_atoms(void)
+{
+       xa_targets = XInternAtom(DPY, "TARGETS", False);
+       xa_timestamp = XInternAtom(DPY, "TIMESTAMP", False);
+       xa_text = XInternAtom(DPY, "TEXT", False);
+       xa_compound_text = XInternAtom(DPY, "COMPOUND_TEXT", False);
+}
+
+
+static Atom request_target = 0;
+
 /*
  * Send a message to request that the PRIMARY buffer be sent here.
  */
-static void paste_x11_request(const Time time)
+static void paste_x11_request(Atom target, const Time time)
 {
+       /*
+        * It's from some sample programs on the web.
+        * What does it mean? -- XXX
+        */
+       Atom property = XInternAtom(DPY, "__COPY_TEXT", False);
+
        /* Check the owner. */
-       if (XGetSelectionOwner(Metadpy->dpy, XA_PRIMARY) == None)
+       if (XGetSelectionOwner(DPY, XA_PRIMARY) == None)
        {
                /* No selection. */
                /* bell("No selection found."); */
                return;
        }
 
-       /* Request the event as a STRING. */
-       XConvertSelection(DPY, XA_PRIMARY, XA_STRING, XA_STRING, WIN, time);
+       request_target = target;
+    
+       /* Request the event */
+       XConvertSelection(DPY, XA_PRIMARY, target, property, WIN, time);
 }
 
+
+/*
+ * Add the contents of the PRIMARY buffer to the input queue.
+ *
+ * Hack - This doesn't use the "time" of the event, and so accepts anything a
+ * client tries to send it.
+ */
+static void paste_x11_accept(const XSelectionEvent *ptr)
+{
+       unsigned long left;
+       const long offset = 0;
+       const long length = 32000;
+       XTextProperty xtextproperty;
+       errr err = 0;
+
+       /*
+        * It's from some sample programs on the web.
+        * What does it mean? -- XXX
+        */
+       Atom property = XInternAtom(DPY, "__COPY_TEXT", False);
+
+
+       /* Failure. */
+       if (ptr->property == None)
+       {
+               if (request_target == xa_compound_text)
+               {
+                       /* Re-request as STRING */
+                       paste_x11_request(XA_STRING, ptr->time);
+               }
+               else
+               {
+                       request_target = 0;
+                       plog("Paste failure (remote client could not send).");
+               }
+               return;
+       }
+
+       if (ptr->selection != XA_PRIMARY)
+       {
+               plog("Paste failure (remote client did not send primary selection).");
+               return;
+       }
+
+       if (ptr->target != request_target)
+       {
+               plog("Paste failure (selection in unknown format).");
+               return;
+       }
+
+       /* Get text */
+       if (XGetWindowProperty(Metadpy->dpy, Infowin->win, property, offset,
+                              length, TRUE, request_target,
+                              &xtextproperty.encoding,
+                              &xtextproperty.format,
+                              &xtextproperty.nitems,
+                              &left,
+                              &xtextproperty.value)
+           != Success)
+       {
+               /* Failure */
+               return;
+       }
+
+       if (request_target == xa_compound_text)
+       {
+               char **list;
+               int count;
+               
+               XmbTextPropertyToTextList(DPY, &xtextproperty, &list, &count);
+
+               if (list)
+               {
+                       int i;
+
+                       for (i = 0; i < count; i++)
+                       {
+                               /* Paste the text. */
+                               err = type_string(list[i], 0);
+
+                               if (err) break;
+                       }
+
+                       /* Free the string */
+                       XFreeStringList(list);
+               }
+       }
+       else /* if (request_target == XA_STRING) */
+       {
+               /* Paste the text. */
+               err = type_string((char *)xtextproperty.value, xtextproperty.nitems);
+       }
+
+       /* Free the data pasted. */
+       XFree(xtextproperty.value); 
+
+       /* No room. */
+       if (err)
+       {
+               plog("Paste failure (too much text selected).");
+       }
+}
+
+
 /*
  * Add a character to a string in preparation for sending it to another
  * client as a STRING.
@@ -2157,24 +2295,17 @@ static void paste_x11_request(const Time time)
  * receiving this format (although the standard specifies a restricted set).
  * Strings do not have a colour.
  */
-static int add_char_string(char *buf, byte a, char c)
-{
-       (void)a;
-
-       *buf = c;
-       return 1;
-}
-
-static bool paste_x11_send_text(XSelectionRequestEvent *rq, int (*add)(char *, byte, char))
+static bool paste_x11_send_text(XSelectionRequestEvent *rq)
 {
        char buf[1024];
+       char *list[1000];
        co_ord max, min;
-       int x,y,l;
+       int x,y,l,n;
        byte a;
        char c;
 
        /* Too old, or incorrect call. */
-       if (rq->time < s_ptr->time || !add) return FALSE;
+       if (rq->time < s_ptr->time) return FALSE;
 
        /* Work out which way around to paste. */
        sort_co_ord(&min, &max, &s_ptr->init, &s_ptr->cur);
@@ -2189,47 +2320,114 @@ static bool paste_x11_send_text(XSelectionRequestEvent *rq, int (*add)(char *, b
        /* Delete the old value of the property. */
        XDeleteProperty(DPY, rq->requestor, rq->property);
 
-       for (y = 0; y < Term->hgt; y++)
+       for (n = 0, y = 0; y < Term->hgt; y++)
        {
+#ifdef JP
+               int kanji = 0;
+#endif
                if (y < min.y) continue;
                if (y > max.y) break;
 
-               for (x = l = 0; x < Term->wid; x++)
+               for (l = 0, x = 0; x < Term->wid; x++)
                {
+#ifdef JP
+                       if (x > max.x) break;
+
+                       /* Find the character. */
+                       Term_what(x, y, &a, &c);
+
+                       if (1 == kanji) kanji = 2;
+                       else if (iskanji(c)) kanji = 1;
+                       else kanji = 0;
+
                        if (x < min.x) continue;
+
+                       /*
+                        * A single kanji character was divided in two...
+                        * Delete the garbage.
+                        */
+                       if ((2 == kanji && x == min.x) ||
+                           (1 == kanji && x == max.x))
+                               c = ' ';
+#else
                        if (x > max.x) break;
+                       if (x < min.x) continue;
 
                        /* Find the character. */
                        Term_what(x, y, &a, &c);
+#endif
 
                        /* Add it. */
-                       l += (*add)(buf+l, a, c);
+                       buf[l] = c;
+                       l++;
                }
 
-#if 0
-               /* Terminate all but the last line in an appropriate way. */
-               if (y != max.y) l += (*add)(buf+l, TERM_WHITE, '\n');
-#else
-               /* Terminate all line unless single line message. */
-               if (min.y != max.y) l += (*add)(buf+l, TERM_WHITE, '\n');
-#endif
+               /* Ignore trailing spaces */
+               while (buf[l-1] == ' ') l--;
+
+               /* Terminate all line unless it's single line. */
+               if (min.y != max.y)
+               {
+                       buf[l] = '\n';
+                       l++;
+               }
+
+               /* End of string */
+               buf[l] = '\0';
 
-               /* Send the (non-empty) string. */
-               XChangeProperty(DPY, rq->requestor, rq->property, rq->target, 8,
-                       PropModeAppend, (byte*)buf, l);
+               list[n++] = (char *)string_make(buf);
        }
-       return TRUE;
-}
 
-static Atom xa_targets, xa_timestamp;
+       /* End of the list */
+       list[n] = NULL;
 
-/*
- * Set the required variable atoms at start-up to avoid errors later.
- */
-static void set_atoms(void)
-{
-       xa_targets = XInternAtom(DPY, "TARGETS", False);
-       xa_timestamp = XInternAtom(DPY, "TIMESTAMP", False);
+
+       if (rq->target == XA_STRING)
+       {
+               for (n = 0; list[n]; n++)
+               {
+                       /* Send the (non-empty) string. */
+                       XChangeProperty(DPY, rq->requestor, rq->property, rq->target, 8,
+                                       PropModeAppend, (unsigned char *)list[n], strlen(list[n]));
+               }
+       }
+
+       else if (rq->target == xa_text || 
+                rq->target == xa_compound_text)
+       {
+               XTextProperty text_prop;
+               XICCEncodingStyle style;
+
+               if (rq->target == xa_text)
+                       style = XStdICCTextStyle;
+               else /* if (rq->target == xa_compound_text) */
+                       style = XCompoundTextStyle;
+
+               if (Success ==
+                   XmbTextListToTextProperty(DPY, list, n, style, &text_prop))
+               {
+                       /* Send the compound text */
+                       XChangeProperty(DPY,
+                                       rq->requestor,
+                                       rq->property,
+                                       text_prop.encoding,
+                                       text_prop.format,
+                                       PropModeAppend,
+                                       text_prop.value,
+                                       text_prop.nitems);
+                               
+                       /* Free the data. */
+                       XFree(text_prop.value);
+               }
+       }
+
+       /* Free the list of strings */
+       for (n = 0; list[n]; n++)
+       {
+               string_free(list[n]);
+       }
+
+       return TRUE;
 }
 
 /*
@@ -2253,16 +2451,20 @@ static void paste_x11_send(XSelectionRequestEvent *rq)
         * Note that this currently rejects MULTIPLE targets.
         */
 
-       if (rq->target == XA_STRING)
+       if (rq->target == XA_STRING ||
+           rq->target == xa_text ||
+           rq->target == xa_compound_text)
        {
-               if (!paste_x11_send_text(rq, add_char_string))
+               if (!paste_x11_send_text(rq))
                        ptr->property = None;
        }
        else if (rq->target == xa_targets)
        {
-               Atom target_list[2];
+               Atom target_list[4];
                target_list[0] = XA_STRING;
-               target_list[1] = xa_targets;
+               target_list[1] = xa_text;
+               target_list[2] = xa_compound_text;
+               target_list[3] = xa_targets;
                XChangeProperty(DPY, rq->requestor, rq->property, rq->target,
                        (8 * sizeof(target_list[0])), PropModeReplace,
                        (unsigned char *)target_list,
@@ -2283,76 +2485,6 @@ static void paste_x11_send(XSelectionRequestEvent *rq)
        XSendEvent(DPY, rq->requestor, FALSE, NoEventMask, &event);
 }
 
-/*
- * Add the contents of the PRIMARY buffer to the input queue.
- *
- * Hack - This doesn't use the "time" of the event, and so accepts anything a
- * client tries to send it.
- */
-static void paste_x11_accept(const XSelectionEvent *ptr)
-{
-       unsigned long offset, left;
-
-       /* Failure. */
-       if (ptr->property == None)
-       {
-               /* bell("Paste failure (remote client could not send)."); */
-               return;
-       }
-
-       if (ptr->selection != XA_PRIMARY)
-       {
-               /* bell("Paste failure (remote client did not send primary selection)."); */
-               return;
-       }
-
-       if (ptr->target != XA_STRING)
-       {
-               /* bell("Paste failure (selection in unknown format)."); */
-               return;
-       }
-
-       for (offset = 0; ; offset += left)
-       {
-               errr err;
-
-               /* A pointer for the pasted information. */
-               unsigned char *data;
-
-               Atom type;
-               int fmt;
-               unsigned long nitems;
-
-               /* Set data to the string, and catch errors. */
-               if (XGetWindowProperty(Metadpy->dpy, Infowin->win, XA_STRING, offset,
-                       32767, TRUE, XA_STRING, &type, &fmt, &nitems, &left, &data)
-                       != Success) break;
-
-               /* Paste the text. */
-               err = type_string((char*)data, nitems);
-
-               /* Free the data pasted. */
-               XFree(data);
-
-               /* No room. */
-               if (err == PARSE_ERROR_OUT_OF_MEMORY)
-               {
-                       /* bell("Paste failure (too much text selected)."); */
-                       break;
-               }
-               /* Paranoia? - strange errors. */
-               else if (err)
-               {
-                       break;
-               }
-
-               /* Pasted everything. */
-               if (!left) return;
-       }
-
-       /* An error has occurred, so free the last bit of data before returning. */
-       XFree(data);
-}
 
 /*
  * Handle various events conditional on presses of a mouse button.
@@ -2365,7 +2497,7 @@ static void handle_button(Time time, int x, int y, int button,
 
        if (press && button == 1) copy_x11_start(x, y);
        if (!press && button == 1) copy_x11_end(time);
-       if (!press && button == 2) paste_x11_request(time);
+       if (!press && button == 2) paste_x11_request(xa_compound_text, time);
 }
 
 
@@ -2756,9 +2888,8 @@ static errr Term_xtra_x11_sound(int v)
        if (!sound_file[v]) return (1);
        
        sprintf(buf,"./playwave.sh %s\n", sound_file[v]);
-       system(buf);
        
-       return (0);
+       return (system(buf) < 0);
        
 }
 #endif /* USE_SOUND */
@@ -2779,7 +2910,7 @@ static errr Term_xtra_x11_level(int v)
 
                /* Activate the font */
                Infofnt_set(td->fnt);
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
                Infokfnt_set(td->kfnt);
 #endif
        }
@@ -2982,18 +3113,13 @@ static errr Term_text_x11(int x, int y, int n, byte a, cptr s)
 /*
  * Draw some graphical characters.
  */
-# ifdef USE_TRANSPARENCY
 static errr Term_pict_x11(int x, int y, int n, const byte *ap, const char *cp, const byte *tap, const char *tcp)
-# else /* USE_TRANSPARENCY */
-static errr Term_pict_x11(int x, int y, int n, const byte *ap, const char *cp)
-# endif /* USE_TRANSPARENCY */
 {
        int i, x1, y1;
 
        byte a;
        char c;
 
-#ifdef USE_TRANSPARENCY
        byte ta;
        char tc;
 
@@ -3001,7 +3127,6 @@ static errr Term_pict_x11(int x, int y, int n, const byte *ap, const char *cp)
        int k,l;
 
        unsigned long pixel, blank;
-#endif /* USE_TRANSPARENCY */
 
        term_data *td = (term_data*)(Term->data);
 
@@ -3034,8 +3159,6 @@ static errr Term_pict_x11(int x, int y, int n, const byte *ap, const char *cp)
                        continue;
                }
 
-#ifdef USE_TRANSPARENCY
-
                ta = *tap++;
                tc = *tcp++;
 
@@ -3088,18 +3211,6 @@ static errr Term_pict_x11(int x, int y, int n, const byte *ap, const char *cp)
                             0, 0, x, y,
                             td->fnt->twid, td->fnt->hgt);
                }
-
-#else /* USE_TRANSPARENCY */
-
-               /* Draw object / terrain */
-               XPutImage(Metadpy->dpy, td->win->win,
-                         clr[0]->gc,
-                         td->tiles,
-                         x1, y1,
-                         x, y,
-                         td->fnt->twid, td->fnt->hgt);
-
-#endif /* USE_TRANSPARENCY */
        }
 
        /* Redraw the selection if any, as it may have been obscured. (later) */
@@ -3207,7 +3318,7 @@ static errr term_data_init(term_data *td, int i)
        cptr name = angband_term_name[i];
 
        cptr font;
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        cptr kfont;
 #endif
 
@@ -3300,7 +3411,7 @@ static errr term_data_init(term_data *td, int i)
                }
        }
 
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        /* Window specific font name */
        sprintf(buf, "ANGBAND_X11_KFONT_%d", i);
 
@@ -3406,7 +3517,7 @@ static errr term_data_init(term_data *td, int i)
 
 
        /* Prepare the standard font */
-#ifdef _JP
+#ifdef USE_JP_FONTSTRUCT
        MAKE(td->fnt, infofnt);
        Infofnt_set(td->fnt);
        MAKE(td->kfnt, infofnt);
@@ -3563,11 +3674,7 @@ errr init_x11(int argc, char *argv[])
        int pict_wid = 0;
        int pict_hgt = 0;
 
-#ifdef USE_TRANSPARENCY
-
        char *TmpData;
-#endif /* USE_TRANSPARENCY */
-
 #endif /* USE_GRAPHICS */
 
 
@@ -3624,25 +3731,32 @@ errr init_x11(int argc, char *argv[])
        }
 
 #ifdef USE_LOCALE
+
+#ifdef JP
+       /* Get locale information from environment variables */
        setlocale(LC_ALL, "");
+
 #ifdef DEFAULT_LOCALE
        if(!strcmp(setlocale(LC_ALL, NULL), "C")){
                printf("try default locale \"%s\"\n", DEFAULT_LOCALE);
                setlocale(LC_ALL, DEFAULT_LOCALE);
        }
 #endif
+
+       if(!strcmp(setlocale(LC_ALL, NULL), "C"))
        {
-               char *current_locale = setlocale(LC_ALL, NULL);
-/*             printf("set locale to \"%s\"\n", current_locale); */
-               if(!strcmp(current_locale, "C")){
-                       printf("WARNING: Locale is not supported. Non-english font may be displayed incorrectly.\n");
-               }
+               printf("WARNING: Locale is not supported. Non-english font may be displayed incorrectly.\n");
        }
 
        if(!XSupportsLocale()){
                printf("can't support locale in X\n");
                setlocale(LC_ALL, "C");
        }
+#else
+       /* Set locale to "C" without using environment variables */
+       setlocale(LC_ALL, "C");
+#endif /* JP */
+
 #endif /* USE_LOCALE */
 
 
@@ -3799,7 +3913,6 @@ errr init_x11(int argc, char *argv[])
                                    td->fnt->twid, td->fnt->hgt);
                }
 
-#ifdef USE_TRANSPARENCY
                /* Initialize the transparency masks */
                for (i = 0; i < num_term; i++)
                {
@@ -3824,8 +3937,6 @@ errr init_x11(int argc, char *argv[])
                                td->fnt->twid, td->fnt->hgt, 8, 0);
 
                }
-#endif /* USE_TRANSPARENCY */
-
 
                /* Free tiles_raw? XXX XXX */
        }