Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy server invite code to the clipboard automatically #9739

Closed
wants to merge 2 commits into from

Conversation

pelya
Copy link

@pelya pelya commented Dec 10, 2021

Motivation / Problem

When you create a server, you need to type the server invite code into email/chat letter by letter to send it to friends.

Description

With this patch, the invite code is copied to the clipboard automatically when the server registers with the masterserver.

Limitations

I am too lazy to add 'Copy to clipboard' button to the server info dialog, this is good enough for me.
This code works only with SDL2, other video backends will ignore the clipboard for now.

Checklist for review

@2TallTyler
Copy link
Member

I'm +1 to this feature, but not how it's hidden from anyone who hasn't seen this PR. 😃

Personally I would prefer a Copy to clipboard button, although it would need to work with all video drivers.

@Eilon
Copy link

Eilon commented Dec 10, 2021

Setting clipboard data is "destructive" in the sense that it clears out whatever the user may have already had in their clipboard, which the user may have deemed valuable. Also, as @2TallTyler mentions, how would anyone even know that this happened so that they could actually make use of it?

I think adding an explicit button would be a better design, even if it's more work.

Copy link
Contributor

@nielsmh nielsmh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to make a button for copying the code, that makes it clear that clicking this button will replace the user's clipboard contents.
Don't silently overwrite the user's clipboard.

@pelya
Copy link
Author

pelya commented Dec 12, 2021

I've added a 'copy to clipboard' button to GUI

@nielsmh nielsmh dismissed their stale review December 12, 2021 19:57

Button implemented

@LC-Zorg
Copy link

LC-Zorg commented Dec 12, 2021

Please, don't care too much about this. Perhaps my opinion will be parochial and overly conservative for you, but...
I don't understand what this invitation code play is for at all. For me, but I also think that for many other average player, it makes no sense at all. Just like showing MD5sum or GRF ID in the NewGRF information - this information is completely useless for the player and only creates questions that the player doesn't need to know the answers to. You want to simplify the interface, but adding a button to such an enigmatic function doesn't simplify it at all.
I see this as the same function as the server password now. The difference is that it is more difficult to remember, so much that you need to add another function, which is in this PR. In addition, this code is shown even where a password is not needed at all. I'm sorry, but I really don't understand this.

@2TallTyler
Copy link
Member

The invite code is for players to join their friends' games without having to list the game publicly. See #9434. But the value of this approach has nothing to do with this PR, which simply aims to make it easier to share the invite code.

@pelya
Copy link
Author

pelya commented Dec 13, 2021 via email

@nielsmh
Copy link
Contributor

nielsmh commented Dec 13, 2021

@LC-Zorg
The invite code is the "address" of the game. When you want a friend to join your game, instead of having to figure out your IP address and set up port forwarding, you send the invite code to your friend. Your friend then enters the invite code, and gets connected to your game. (There is a blog post with more technical details if you're interested.)

That's why the invite code is important: It's the easy way to let others join your private game.

@LC-Zorg
Copy link

LC-Zorg commented Dec 13, 2021

Ok, I don't use it and I don't think it will be any different in the future for me, but I can understand there is a need for some to hide the server. However, I think there are at least one maybe two significant issues with this feature that make its functionality significantly limited, and in its current form, at least from my point of view, it is more problematic than useful. It is independent of this PR, so I will write about it elsewhere. (#9746) (#9747)

If I understand the code correctly, this copy button is in the client list window in the server settings field. If so, I think there is no problem and it is a good solution. :) Previously, I thought this might be for the server list window, and that wouldn't be good.

Copy link
Member

@LordAro LordAro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like others I am generally +1 to this feature, but I don't understand the current implementation

Why is set_clipboard_text stored at all? Why not just call SDL_SetClipboardText directly in VideoDriver::SetClipboardContents ?

@pelya
Copy link
Author

pelya commented Jan 5, 2022 via email

Copy link
Contributor

@rubidium42 rubidium42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When actually squashing/merging, maybe note that it is SDL2 for now?

One tricky thing from the user's point of view is that is a button that does nothing (unless they use SDL2). That might be considered buggy behaviour, so does that make this patch bugged?
And what would be the best way to remedy this? Implementing it for the other platforms as well in this PR? Or ship it buggy and hope the maintainers fix those bugs? Or introduce some manner where the UI can ask the video driver whether it supports writing to the clipboard and then disable the button when the video driver doesn't support it?

@pelya
Copy link
Author

pelya commented Feb 4, 2022

Isn't SDL2 used for all release binaries that are published to openttd.org?
I guess we can surround this code with
#if SDL_VERSION_ATLEAST(2,0,0)

@glx22
Copy link
Contributor

glx22 commented Feb 4, 2022

SDL2 is used only on linux builds.

@pelya
Copy link
Author

pelya commented Feb 4, 2022 via email

@2TallTyler
Copy link
Member

I don't see a point shipping a build with a button that doesn't work for the majority of users. (I imagine Linux will be more prevalent among OpenTTD users than the average, but W3 Schools says it's used by about 4.3% of users.)

@pelya
Copy link
Author

pelya commented Feb 4, 2022 via email

@nielsmh
Copy link
Contributor

nielsmh commented Feb 5, 2022

I was about to say that the Win32 and Cocoa video drivers also support setting the clipboard, but I was mixing that up with the getting clipboard function, which is implemented in the OS code and not video driver.

bool GetClipboardContents(char *buffer, const char *last)
{
HGLOBAL cbuf;
const char *ptr;
if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
OpenClipboard(nullptr);
cbuf = GetClipboardData(CF_UNICODETEXT);
ptr = (const char*)GlobalLock(cbuf);
int out_len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)ptr, -1, buffer, (last - buffer) + 1, nullptr, nullptr);
GlobalUnlock(cbuf);
CloseClipboard();
if (out_len == 0) return false;
} else {
return false;
}
return true;
}

bool GetClipboardContents(char *buffer, const char *last)
{
NSPasteboard *pb = [ NSPasteboard generalPasteboard ];
NSArray *types = [ NSArray arrayWithObject:NSPasteboardTypeString ];
NSString *bestType = [ pb availableTypeFromArray:types ];
/* Clipboard has no text data available. */
if (bestType == nil) return false;
NSString *string = [ pb stringForType:NSPasteboardTypeString ];
if (string == nil || [ string length ] == 0) return false;
strecpy(buffer, [ string UTF8String ], last);
return true;
}

#ifndef WITH_COCOA
bool GetClipboardContents(char *buffer, const char *last)
{
#ifdef WITH_SDL2
if (SDL_HasClipboardText() == SDL_FALSE) {
return false;
}
char *clip = SDL_GetClipboardText();
if (clip != nullptr) {
strecpy(buffer, clip, last);
SDL_free(clip);
return true;
}
#endif
return false;
}
#endif

I think it would make the most sense to move this code to the video driver (because the concept of a clipboard is usually quite tightly bound to the "exterior" windowing system used), and then of course implement setting the clipboard for all platforms as well.

Copy link
Member

@LordAro LordAro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues with this at all, but it needs to support more than just SDL2 before being merged

@2TallTyler
Copy link
Member

As @pelya posted on TT-Forums a month ago, he will likely not be working on this any time in the near future.

@2TallTyler
Copy link
Member

This is a great feature but the PR can't be merged without supporting all OSs. Since @pelya has indicated that he's not willing/able to add that support, I'm closing the PR. Feel free to reopen if you disagree or have code that makes it work for other OSs. 🙂

@2TallTyler 2TallTyler closed this Nov 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants