minecraft-source/src/com/mojang/blaze3d/platform/ClipboardManager.java

56 lines
2.0 KiB
Java
Raw Normal View History

2020-07-22 06:23:34 +01:00
package com.mojang.blaze3d.platform;
2020-07-22 06:30:03 +01:00
import java.nio.Buffer;
2020-07-22 06:23:34 +01:00
import org.lwjgl.system.MemoryUtil;
2020-07-22 06:30:03 +01:00
import com.google.common.base.Charsets;
2020-07-22 06:23:34 +01:00
import org.lwjgl.glfw.GLFWErrorCallback;
2020-07-22 06:32:50 +01:00
import net.minecraft.client.StringDecomposer;
2020-07-22 06:23:34 +01:00
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallbackI;
2020-07-22 06:30:03 +01:00
import org.lwjgl.BufferUtils;
2020-07-22 06:23:34 +01:00
import java.nio.ByteBuffer;
public class ClipboardManager {
private final ByteBuffer clipboardScratchBuffer;
public ClipboardManager() {
2020-07-22 06:30:03 +01:00
this.clipboardScratchBuffer = BufferUtils.createByteBuffer(8192);
2020-07-22 06:23:34 +01:00
}
public String getClipboard(final long long1, final GLFWErrorCallbackI gLFWErrorCallbackI) {
final GLFWErrorCallback gLFWErrorCallback5 = GLFW.glfwSetErrorCallback(gLFWErrorCallbackI);
String string6 = GLFW.glfwGetClipboardString(long1);
2020-07-22 06:32:50 +01:00
string6 = ((string6 != null) ? StringDecomposer.filterBrokenSurrogates(string6) : "");
2020-07-22 06:23:34 +01:00
final GLFWErrorCallback gLFWErrorCallback6 = GLFW.glfwSetErrorCallback((GLFWErrorCallbackI)gLFWErrorCallback5);
if (gLFWErrorCallback6 != null) {
gLFWErrorCallback6.free();
}
return string6;
}
2020-07-22 06:30:03 +01:00
private static void pushClipboard(final long long1, final ByteBuffer byteBuffer, final byte[] arr) {
byteBuffer.clear();
byteBuffer.put(arr);
byteBuffer.put((byte)0);
byteBuffer.flip();
2020-07-22 06:23:34 +01:00
GLFW.glfwSetClipboardString(long1, byteBuffer);
}
public void setClipboard(final long long1, final String string) {
2020-07-22 06:30:03 +01:00
final byte[] arr5 = string.getBytes(Charsets.UTF_8);
final int integer6 = arr5.length + 1;
if (integer6 < this.clipboardScratchBuffer.capacity()) {
pushClipboard(long1, this.clipboardScratchBuffer, arr5);
2020-07-22 06:23:34 +01:00
}
else {
2020-07-22 06:30:03 +01:00
final ByteBuffer byteBuffer7 = MemoryUtil.memAlloc(integer6);
try {
pushClipboard(long1, byteBuffer7, arr5);
}
finally {
MemoryUtil.memFree((Buffer)byteBuffer7);
}
2020-07-22 06:23:34 +01:00
}
}
}