joncampbell123 / dosbox-x

DOSBox-X fork of the DOSBox project
GNU General Public License v2.0
2.66k stars 378 forks source link

Support for cross-platform clipboard #1938

Open Wengier opened 3 years ago

Wengier commented 3 years ago

Right now clipboard support (copy, paste etc) is only supported in the Windows platform, and for Linux and macOS the feature is simply disabled. This is certainly not the ideal behaviour, and I have heard some Linux user would like to see clipboard support, so I hope there exists a light-weighted cross-platform library (similar to TinyFileDialogs used for dialog box) so that cross-platform clipboard support can be added for DOSBox-X, not just on Windows.

Wengier commented 3 years ago

@rderooy Have you checked the clipboard feature yourself on Linux then? Does it work properly for you?

rderooy commented 3 years ago

To be honest, I had not yet gotten around to trying it. Been busy with other stuff for the last week or so.

I just gave it a quick try. First this seems to be exclusive to SDL2 for now right?

What I was not able to copy out of dosbox-x was the border characters used for the dosbox-x intro screen. If I select any of those box (border drawing) characters, paste did not do anything. So perhaps it only works for the lower ascii characters? Also taking into account that the on-screen characters can vary depending on codepage used. But all the characters should exist in unicode, even those box drawing characters.

joncampbell123 commented 3 years ago

@rderooy The code page conversion code currently used for filenames could be used for converting to/from unicode on the clipboard.

Wengier commented 3 years ago

@joncampbell123 Thanks for the hint. I have implemented UTF-8 to ASCII conversion using the conversion code.

@rderooy Please check out the latest code and see how the border characters work when pasting. Thanks!

Wengier commented 3 years ago

@rderooy Also, mouse copy/paste feature is specific to SDL2 builds indeed, but pasting from the clipboard feature (mappable from the mapper editor) is available for SDL1 builds too. For Linux SDL2 builds there is a "Shared clipboard functions" menu group (under "Main") as in the Windows builds.

Wengier commented 3 years ago

As for the avcodec, as I said in the newly-opened Issue #1947, it could be disabled by default in some way until the problem is solved.

rderooy commented 3 years ago

@Wengier I can paste into dosbox-x some individual box characters, but try to paste this: As I said, some individual characters are fine, but as a group it just ends up as junk.

U+250x ─ ━ │ ┃ ┄ ┅ ┆ ┇ ┈ ┉ ┊ ┋ ┌ ┍ ┎ ┏
U+251x ┐ ┑ ┒ ┓ └ ┕ ┖ ┗ ┘ ┙ ┚ ┛ ├ ┝ ┞ ┟
U+252x ┠ ┡ ┢ ┣ ┤ ┥ ┦ ┧ ┨ ┩ ┪ ┫ ┬ ┭ ┮ ┯
U+253x ┰ ┱ ┲ ┳ ┴ ┵ ┶ ┷ ┸ ┹ ┺ ┻ ┼ ┽ ┾ ┿
U+254x ╀ ╁ ╂ ╃ ╄ ╅ ╆ ╇ ╈ ╉ ╊ ╋ ╌ ╍ ╎ ╏
U+255x ═ ║ ╒ ╓ ╔ ╕ ╖ ╗ ╘ ╙ ╚ ╛ ╜ ╝ ╞ ╟
U+256x ╠ ╡ ╢ ╣ ╤ ╥ ╦ ╧ ╨ ╩ ╪ ╫ ╬ ╭ ╮ ╯
U+257x ╰ ╱ ╲ ╳ ╴ ╵ ╶ ╷ ╸ ╹ ╺ ╻ ╼ ╽ ╾ ╿

U+258x ▀ ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ ▉ ▊ ▋ ▌ ▍ ▎ ▏
U+259x ▐ ░ ▒ ▓ ▔ ▕ ▖ ▗ ▘ ▙ ▚ ▛ ▜ ▝ ▞ ▟

This comes from: https://en.wikipedia.org/wiki/Box-drawing_character and I cleaned it up a bit by removing the tab characters with spaces, in case the tabs where causing problems. With spaces in between the result is indeed better than with tabs.

Also, I still cannot copy box drawing characters out-of dosbox-x.

rderooy commented 3 years ago

This is pasted line-by-line as linefeed seems to be ignored when pasting and it becomes an even bigger mess. edit_001

This is with the default CP 437

rderooy commented 3 years ago

Also regarding the SDL1 build, the "Shared clipboard functions" menu is missing, and shift + right-mouse-button does nothing.

Wengier commented 3 years ago

@rderooy I will check that out. Meanwhile, for Linux SDL1 build, the only available function is the Paste Clipboard function, mappable from the key mapper, so I don't think adding such as menu group is really useful for this build.

Wengier commented 3 years ago

@rderooy For Linux SDL1 build, I have decided to add a single menu item "Paste from the Clipboard" under the Main menu in the latest code, instead of the "Shared clipboard functions" menu group.

Wengier commented 3 years ago

@rderooy I have improved the pasting for box characters in the latest code. It appears that I had to do some checks myself. Basically it can still paste characters supported by the current codepage in DOSBox-X, but it will hopefully no longer paste junks as before. Please check it out.

rderooy commented 3 years ago

@Wengier

Improvements

no change

regression

I say when pasting works, because I found that trying to use the paste function with the mouse, when the mouse is active in dosbox-x, such as simply loading edit, does not work most of the time. And on the rare occasion that it does something, what you get is not correct. I used the paste option from the menu yesterday to get that screenshot, but the paste option is now gone. Pasting with the mouse at the DOS prompt works better, since no mouse emulation is active at that point.

Wengier commented 3 years ago

@rderooy The paste menu option was added to the Linux SDL1 build. When I did this, I accidently hid it from the Linux SDL2 build. I have already restored it for Linux SDL2 build (now both SDL1 and SDL2 builds have it). So please check out the latest code. (But please note that even if it was not shown from the menu, it could still be accessed with the shortcut defined in the key mapper).

Also, I decided to convert tabs to 4 spaces in the latest code. As for the linefeed, I think it was deliberately ignored so that the pasting looks better from the DOS shell. If this causes issue(s) please let me know. Thanks!

Wengier commented 3 years ago

@rderooy As for mouse issue you mentioned, I think pasting it in edit does work here, although not at times apparently because of the interferes from the mouse emulation. In such cases I would suggest you to use the menu option for pasting instead, which should by now be available for both Linux SDL1 and SDL2 builds.

rderooy commented 3 years ago

@Wengier Thanks!

I can confirm that the paste menu option is now available and works in both SDL1 and SDL2 builds.

Here is the current state when pasting line-by-line in the SDL2 build: edit_000

I still think that ignoring the newline is not the right thing to do. I cannot think of any other place that newline is ignored.

rderooy commented 3 years ago

Here is codepage 437

   0 1 2 3 4 5 6 7 8 9 A B C D E F

0    ☺ ☻ ♥ ♦ ♣ ♠ • ◘ ○ ◙ ♂ ♀ ♪ ♫ ☼
1  ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ → ← ∟ ↔ ▲ ▼
2    ! " # $ % & ' ( ) * + , - . /
3  0 1 2 3 4 5 6 7 8 9 : ; < = > ?
4  @ A B C D E F G H I J K L M N O
5  P Q R S T U V W X Y Z [ \ ] ^ _
6  ` a b c d e f g h i j k l m n o
7  p q r s t u v w x y z { | } ~ ⌂
8  Ç ü é â ä à å ç ê ë è ï î ì Ä Å
9  É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧ ƒ
A  á í ó ú ñ Ñ ª º ¿ ⌐ ¬ ½ ¼ ¡ « »
B  ░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛ ┐
C  └ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬ ╧
D  ╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐ ▀
E  α ß Γ π Σ σ µ τ Φ Θ Ω δ ∞ φ ε ∩
F  ≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■

And here it is pasted line-by-line. Some characters are missing, or cause edit to open search or file menus. Also sometimes even spaces in between the characters are missing. Pasting too fast perhaps and overrunning the keyboard buffer?

edit_001

Wengier commented 3 years ago

@rderooy I think the code was trying to ignore LF and will accept CR, but it appears that it did not work well as you said. So I have already fixed this. Please check out the latest code.

As for missing characters when pasting as a group, I believe they are caused by the pasting speed indeed. The pasting speed can be set with the clip_paste_speed config option, which defaulted to 20. I have now increased it to 30 by default so that the speed should be somewhat slower. Please check it out as well.

Thanks for testing and please let me know how the new code works.

Wengier commented 3 years ago

@rderooy Have you checked it out yet?

rderooy commented 3 years ago

@Wengier Yes, just did not have the time to comment. With the new timing, pasting works better but not perfect. I did find that increasing the emulated CPU speed does allow a larger paste to succeed. So perhaps the paste speed should be proportional to the emulated CPU speed.

With some testing I found that selecting the 386DX33 (6075 cycles) from the CPU speed menu almost allows a larger text block to be correctly pasted. Selecting one step up, the 486DX33 (12010 cycles) allows a larger block to be pasted without corruption.

Also, some characters are interpreted as control characters. But I guess that is to be expected...

edit_002

rderooy commented 3 years ago

Selecting the above table in DOSBox-X and pasting it on the host gives me this:

   0 1 2 3 4 5 6 7 8 9 A B C D E F

0                   
1                    
2    ! " # $ % & ' ( ) * + , - . /
3  0 1 2 3 4 5 6 7 8 9 : ; < = > ?
4  @ A B C D E F G H I J K L M N O
5  P Q R S T U V W X Y Z [ \ ] ^ _
6  ` a b c d e f g h i j k l m n o
7  p q r s t u v w x y z { | } ~
8  � � � � � � � � � � � � � � � �
9  � � � � � � � � � � � � � � � �
A  � � � � � � � � � � � � � � � �
B  � � � � � � � � � � � � � � � �
C  � � � � � � � � � � � � � � � �
D  � � � � � � � � � � � � � � � �
E  � � � � � � � � � � � � � � � �
F  � � � � � � � � � � � � � � �
Wengier commented 3 years ago

@rderooy I am not sure exactly how the clipboard paste speed should be proportional to the emulated CPU speed, as different pasting speed may be needed for different programs anyway. Meanwhile, you can always manually adjust the pasting speed with that option.

On the other hand, you are right that code page conversion should be done for Linux clipboard copy as well, not just Linux clipboard paste. I have already improved the Linux clipboard copy function in the latest code (https://github.com/joncampbell123/dosbox-x/commit/062d134d03765426597d4c5b394c7770eebc4268). Please check it out and see how it works. Thanks.

rderooy commented 3 years ago

@Wengier

Something weird is going on. When I use the right-mouse copy function, and copy it into a web browser input field like this, there is no change:

   0 1 2 3 4 5 6 7 8 9 A B C D E F

0                   
1                     
2    ! " # $ % & ' ( ) * + , - . /
3  0 1 2 3 4 5 6 7 8 9 : ; < = > ?
4  @ A B C D E F G H I J K L M N O
5  P Q R S T U V W X Y Z [ \ ] ^ _
6  ` a b c d e f g h i j k l m n o
7  p q r s t u v w x y z { | } ~
8  � � � � � � � � � � � � � � � �
9  � � � � � � � � � � � � � � � �
A  � � � � � � � � � � � � � � � �
B  � � � � � � � � � � � � � � � �
C  � � � � � � � � � � � � � � � �
D  � � � � � � � � � � � � � � � �
E  � � � � � � � � � � � � � � � �
F  � � � � � � � � � � � � � � �

When I open a text editor, and try to paste, it pastes literally nothing. When I start the editor (gedit) from a terminal, it prints the following message when I paste: (gedit:31455): Gdk-WARNING **: 10:59:03.252: Error converting selection from UTF8_STRING

I also sometimes get this error: (gedit:33594): Gtk-WARNING **: 11:16:46.698: Error converting from text/plain;charset=utf-8 to UTF-8: invalid UTF-8

But when I paste the copied text back into dosbox-x (even after closing and restarting it), it pastes just fine.

Here is an example of what I get when trying to examine the clipboard contents. This is with the first 2 rows missing from the data copied to avoid the control characters:

$ xclip -o -t TARGETS -selection clipboard
TARGETS
UTF8_STRING
$ xclip -o -t UTF8_STRING -selection clipboard
F  � � � � � � � � � � � � � � �

But sometimes, I get more TARGETS for some reason.

$ xclip -o -t TARGETS -selection clipboard
text/plain
text/plain;charset=utf-8
STRING
TEXT
COMPOUND_TEXT
UTF8_STRING
TARGETS
TIMESTAMP
$ xclip -o -t UTF8_STRING -selection clipboard
(gedit:33594): Gtk-WARNING **: 11:16:46.698: Error converting from text/plain;charset=utf-8 to UTF-8: invalid UTF-8
$ xclip -o -t STRING -selection clipboard
(gedit:33594): Gtk-WARNING **: 11:16:46.698: Error converting from text/plain;charset=utf-8 to UTF-8: invalid UTF-8
$ xclip -o -t STRING -selection clipboard UTF8_                         ^C
$ xclip -o -t text/plain -selection clipboard
(gedit:33594): Gtk-WARNING **: 11:16:46.698: Error converting from text/plain;charset=utf-8 to UTF-8: invalid UTF-8
Wengier commented 3 years ago

@rderooy This is the result I got when I copied the content from the DOSBox-X window (Linux SDL2) and then pasted here:

+---------------------------------+ 0 | 0 @ P ` p Ç É á ░ └ ╨ α ≡ | 1 | ! 1 A Q a q ü æ í ▒ ┴ ╤ ß ± | 2 | " 2 B R b r é Æ ó ▓ ┬ ╥ Γ ≥ | 3 | # 3 C S c s â ô ú │ ├ ╙ π ≤ | 4 | $ 4 D T d t ä ö ñ ┤ ─ ╘ Σ ⌠ | 5 | % 5 E U e u à ò Ñ ╡ ┼ ╒ σ ⌡ | 6 | & 6 F V f v å û ª ╢ ╞ ╓ µ ÷ | 7 | ' 7 G W g w ç ù º ╖ ╟ ╫ τ ≈ | 8 | ( 8 H X h x ê ÿ ¿ ╕ ╚ ╪ Φ ° |

9 | ) 9 I Y i y ë Ö ⌐ ╣ ╔ ┘ Θ ∙ | A | * : J Z j z è Ü ¬ ║ ╩ ┌ Ω · | B | + ; K [ k { ï ¢ ½ ╗ ╦ █ δ √ | C | , < L \ l | î £ ¼ ╝ ╠ ▄ ∞ ⁿ | D | - = M ] m } ì ¥ ¡ ╜ ═ ▌ φ ² | E | . > N ^ n ~ Ä ₧ « ╛ ╬ ▐ ε ■ | F | / ? O _ o  Å ƒ » ┐ ╧ ▀ ∩   | +---------------------------------+

Note that I had to copy half of them and then the other half. Copy all of them at once and paste here did not seem to work for some reason (but it worked if I pasted into the DOSBox-X window). Similar for gedit - pasting in two steps seem to work here, but not one for all, or else it will print the log message "Error converting selection from UTF8_STRING" as you said. Not sure why.

Wengier commented 3 years ago

I have now added a new menu item "Stop clipboard pasting" to allow the users to stop the pasting in the middle while pasting long clipboard content. Because of this I have added a "Shared clipboard functions" for Linux SDL1 build too which will contain the items "Paste from the clipboard" along with this one.

Wengier commented 3 years ago

@rderooy I have improved the clipboard copy code, and it seems to work better this time - I can now copy the entire content of the chart from the DOSBox-X window as a whole this time:

0 1 2 3 4 5 6 7 8 9 A B C D E F

+---------------------------------+ 0 | 0 @ P ` p Ç É á ░ └ ╨ α ≡ | 1 | ! 1 A Q a q ü æ í ▒ ┴ ╤ ß ± | 2 | " 2 B R b r é Æ ó ▓ ┬ ╥ Γ ≥ | 3 | # 3 C S c s â ô ú │ ├ ╙ π ≤ | 4 | $ 4 D T d t ä ö ñ ┤ ─ ╘ Σ ⌠ | 5 | % 5 E U e u à ò Ñ ╡ ┼ ╒ σ ⌡ | 6 | & 6 F V f v å û ª ╢ ╞ ╓ µ ÷ | 7 | ' 7 G W g w ç ù º ╖ ╟ ╫ τ ≈ | 8 | ( 8 H X h x ê ÿ ¿ ╕ ╚ ╪ Φ ° | 9 | ) 9 I Y i y ë Ö ⌐ ╣ ╔ ┘ Θ ∙ | A | * : J Z j z è Ü ¬ ║ ╩ ┌ Ω · | B | + ; K [ k { ï ¢ ½ ╗ ╦ █ δ √ | C | , < L \ l | î £ ¼ ╝ ╠ ▄ ∞ ⁿ | D | - = M ] m } ì ¥ ¡ ╜ ═ ▌ φ ² | E | . > N ^ n ~ Ä ₧ « ╛ ╬ ▐ ε ■ | F | / ? O _ o  Å ƒ » ┐ ╧ ▀ ∩   | +---------------------------------+

Please check it out.

rderooy commented 3 years ago

@Wengier I had a moment to try it, and can confirm that it now works fine, at least with CP437.

In addition, also CP850 and 858 work fine!

Wengier commented 3 years ago

@rderooy Glad to hear that. I have also added a menu item “Copy all text on the DOS screen" in the menu group to copy all screen text to the clipboard, and in such case there is no need to select the text.

Wengier commented 3 years ago

I have now added default shortcuts HOST(F11/F12)+A and HOST(F11/F12)+V to copy all text on the DOS screen to the clipboard and to paste text to the DOS screen respectively.

Wengier commented 3 years ago

@Carpintonto The official DOSBox-X version 0.83.7 that has support for the feature is now released. You can get the official Linux Flatpak from https://flathub.org/apps/details/com.dosbox_x.DOSBox-X

Thanks a lot for encouraging me to add support this feature for Linux too! Feel free to let me know if you find any issues.