OSC 52 によるクリップボード操作制御の文字コードをANSIからUTF-8に変更した
- 今のところ UTF-8 固定
- ticket #39745
@@ -194,31 +194,26 @@ | ||
194 | 194 | * クリップボードからwchar_t文字列を取得する |
195 | 195 | * 文字列長が必要なときはwcslen()すること |
196 | 196 | * @param hWnd |
197 | - * @param emtpy TRUEのときクリップボードを空にする | |
198 | - * @retval 文字列へのポインタ 使用後free()すること | |
199 | - * 文字がない(またはエラー時)はNULL | |
197 | + * @param emtpy TRUEのときクリップボードを空にする | |
198 | + * @retval NULL エラー | |
199 | + * @retval NULL以外 文字列へのポインタ 使用後free()すること | |
200 | 200 | */ |
201 | 201 | wchar_t *GetClipboardTextW(HWND hWnd, BOOL empty) |
202 | 202 | { |
203 | - UINT Cf; | |
204 | - wchar_t *str_w = NULL; | |
205 | - size_t str_w_len; | |
206 | - HGLOBAL TmpHandle; | |
207 | - | |
208 | - // TODO GetPriorityClipboardFormat() | |
209 | - if (IsClipboardFormatAvailable(CF_UNICODETEXT)) { | |
210 | - Cf = CF_UNICODETEXT; | |
203 | + static const UINT list[] = { | |
204 | + CF_UNICODETEXT, | |
205 | + CF_TEXT, | |
206 | + CF_OEMTEXT, | |
207 | + CF_HDROP, | |
208 | + }; | |
209 | + UINT Cf = GetPriorityClipboardFormat((UINT *)list, _countof(list)); | |
210 | + if (Cf == 0) { | |
211 | + // クリップボードが空だった | |
212 | + // 空文字を返す | |
213 | + return wcsdup(L""); | |
211 | 214 | } |
212 | - else if (IsClipboardFormatAvailable(CF_TEXT)) { | |
213 | - Cf = CF_TEXT; | |
214 | - } | |
215 | - else if (IsClipboardFormatAvailable(CF_OEMTEXT)) { | |
216 | - Cf = CF_OEMTEXT; | |
217 | - } | |
218 | - else if (IsClipboardFormatAvailable(CF_HDROP)) { | |
219 | - Cf = CF_HDROP; | |
220 | - } | |
221 | - else { | |
215 | + if (Cf == -1) { | |
216 | + // 扱えるデータがなかった | |
222 | 217 | return NULL; |
223 | 218 | } |
224 | 219 |
@@ -225,10 +220,12 @@ | ||
225 | 220 | if (!OpenClipboard(hWnd)) { |
226 | 221 | return NULL; |
227 | 222 | } |
228 | - TmpHandle = GetClipboardData(Cf); | |
223 | + HGLOBAL TmpHandle = GetClipboardData(Cf); | |
229 | 224 | if (TmpHandle == NULL) { |
230 | 225 | return NULL; |
231 | 226 | } |
227 | + wchar_t *str_w = NULL; | |
228 | + size_t str_w_len; | |
232 | 229 | if (Cf == CF_HDROP) { |
233 | 230 | HDROP hDrop = (HDROP)TmpHandle; |
234 | 231 | UINT count = DragQueryFileW(hDrop, (UINT)-1, NULL, 0); |
@@ -5071,13 +5071,8 @@ | ||
5071 | 5071 | |
5072 | 5072 | static void XsProcClipboard(PCHAR buff) |
5073 | 5073 | { |
5074 | - int len, blen; | |
5075 | - char *p, *cbbuff, hdr[20]; | |
5074 | + char *p; | |
5076 | 5075 | wchar_t *notify_buff, *notify_title; |
5077 | - HGLOBAL cbmem; | |
5078 | - int wide_len; | |
5079 | - HGLOBAL wide_cbmem; | |
5080 | - LPWSTR wide_buf; | |
5081 | 5076 | |
5082 | 5077 | p = buff; |
5083 | 5078 | while (strchr("cps01234567", *p)) { |
@@ -5087,6 +5082,7 @@ | ||
5087 | 5082 | if (*p++ == ';') { |
5088 | 5083 | if (*p == '?' && *(p+1) == 0) { // Read access |
5089 | 5084 | if (ts.CtrlFlag & CSF_CBREAD) { |
5085 | + char hdr[20]; | |
5090 | 5086 | if (ts.NotifyClipboardAccess) { |
5091 | 5087 | GetI18nStrWW("Tera Term", "MSG_CBACCESS_TITLE", |
5092 | 5088 | L"Clipboard Access", ts.UILanguageFileW, ¬ify_title); |
@@ -5112,27 +5108,15 @@ | ||
5112 | 5108 | } |
5113 | 5109 | } |
5114 | 5110 | else if (ts.CtrlFlag & CSF_CBWRITE) { // Write access |
5115 | - len = strlen(buff); | |
5116 | - blen = len * 3 / 4 + 1; | |
5117 | - | |
5118 | - if ((cbmem = GlobalAlloc(GMEM_MOVEABLE, blen)) == NULL) { | |
5119 | - return; | |
5120 | - }; | |
5121 | - if ((cbbuff = GlobalLock(cbmem)) == NULL) { | |
5122 | - GlobalFree(cbmem); | |
5123 | - return; | |
5124 | - } | |
5125 | - | |
5111 | + size_t len = strlen(buff); | |
5112 | + size_t blen = len * 3 / 4 + 1; | |
5113 | + char *cbbuff = malloc(blen); | |
5126 | 5114 | len = b64decode(cbbuff, blen, p); |
5127 | - | |
5128 | 5115 | if (len < 0 || len >= blen) { |
5129 | - GlobalUnlock(cbmem); | |
5130 | - GlobalFree(cbmem); | |
5116 | + free(cbbuff); | |
5131 | 5117 | return; |
5132 | 5118 | } |
5133 | - | |
5134 | 5119 | cbbuff[len] = 0; |
5135 | - GlobalUnlock(cbmem); | |
5136 | 5120 | |
5137 | 5121 | if (ts.NotifyClipboardAccess) { |
5138 | 5122 | wchar_t *buf; |
@@ -5147,22 +5131,16 @@ | ||
5147 | 5131 | free(notify_buff); |
5148 | 5132 | } |
5149 | 5133 | |
5150 | - wide_len = MultiByteToWideChar(CP_ACP, 0, cbbuff, -1, NULL, 0); | |
5151 | - wide_cbmem = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR) * wide_len); | |
5152 | - if (wide_cbmem) { | |
5153 | - wide_buf = (LPWSTR)GlobalLock(wide_cbmem); | |
5154 | - MultiByteToWideChar(CP_ACP, 0, cbbuff, -1, wide_buf, wide_len); | |
5155 | - GlobalUnlock(wide_cbmem); | |
5134 | + // cbbuff に入っている文字列をクリップボードにセットする | |
5135 | + { | |
5136 | + // wchar_t へ変換して設定 | |
5137 | + // とりあえずUTF-8 が入っている前提 | |
5138 | + // TODO 受信文字コードに合わせて変更すればok? | |
5139 | + wchar_t *cbbuffW = ToWcharU8(cbbuff); | |
5140 | + CBSetTextW(NULL, cbbuffW, 0); | |
5141 | + free(cbbuffW); | |
5156 | 5142 | } |
5157 | - | |
5158 | - if (OpenClipboard(NULL)) { | |
5159 | - EmptyClipboard(); | |
5160 | - SetClipboardData(CF_TEXT, cbmem); | |
5161 | - if (wide_buf) { | |
5162 | - SetClipboardData(CF_UNICODETEXT, wide_cbmem); | |
5163 | - } | |
5164 | - CloseClipboard(); | |
5165 | - } | |
5143 | + free(cbbuff); | |
5166 | 5144 | } |
5167 | 5145 | else if (ts.NotifyClipboardAccess) { |
5168 | 5146 | GetI18nStrWW("Tera Term", "MSG_CBACCESS_REJECT_TITLE", |