File: clipboard_unix.go

package info (click to toggle)
golang-github-zyedidia-clipboard 1.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, bullseye, forky, sid, trixie
  • size: 100 kB
  • sloc: makefile: 3
file content (193 lines) | stat: -rw-r--r-- 4,774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2013 @atotto. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build freebsd linux netbsd openbsd solaris dragonfly

package clipboard

import (
	"errors"
	"os"
	"os/exec"
)

const (
	xsel               = "xsel"
	xclip              = "xclip"
	wlcopy             = "wl-copy"
	wlpaste            = "wl-paste"
	termuxClipboardGet = "termux-clipboard-get"
	termuxClipboardSet = "termux-clipboard-set"
)

var (
	pasteCmdArgs map[string][]string
	copyCmdArgs  map[string][]string

	xselPasteArgs = map[string][]string{
		"primary":   []string{xsel, "--output"},
		"clipboard": []string{xsel, "--output", "--clipboard"},
	}
	xselCopyArgs = map[string][]string{
		"primary":   []string{xsel, "--input"},
		"clipboard": []string{xsel, "--input", "--clipboard"},
	}

	xclipPasteArgs = map[string][]string{
		"primary":   []string{xclip, "-out"},
		"clipboard": []string{xclip, "-out", "-selection", "clipboard"},
	}
	xclipCopyArgs = map[string][]string{
		"primary":   []string{xclip, "-in"},
		"clipboard": []string{xclip, "-in", "-selection", "clipboard"},
	}

	wlpasteArgs = map[string][]string{
		"primary":   []string{wlpaste, "--no-newline", "--primary"},
		"clipboard": []string{wlpaste, "--no-newline"},
	}
	wlcopyArgs = map[string][]string{
		"primary":   []string{wlcopy, "--primary"},
		"clipboard": []string{wlcopy},
	}

	termuxPasteArgs = map[string][]string{
		"primary":   []string{termuxClipboardGet},
		"clipboard": []string{termuxClipboardGet},
	}
	termuxCopyArgs = map[string][]string{
		"primary":   []string{termuxClipboardSet},
		"clipboard": []string{termuxClipboardSet},
	}

	missingCommands = errors.New("No clipboard utilities available. Please install xsel, xclip, wl-clipboard or Termux:API add-on for termux-clipboard-get/set.")

	internalClipboards map[string]string
)

// verify install makes sure that the given exec is installed
// and operating correctly. In some cases, xclip/xsel can be installed
// but not working because an X server is not running.
func verifyInstall(cmd string) bool {
	if _, err := exec.LookPath(cmd); err == nil {
		// first check if to read from the clipboard
		// if this is possible, then xclip/xsel is working properly
		pasteCmd := getPasteCommand("primary")
		_, err := pasteCmd.Output()
		if err == nil {
			return true
		}

		// If the previous command didn't work, this could be because the clipboard
		// has no contents. Now we will try to copy an empty string into the clipboard.
		copyCmd := getCopyCommand("primary")
		in, err := copyCmd.StdinPipe()
		if err != nil {
			return false
		}

		if err := copyCmd.Start(); err != nil {
			return false
		}
		if _, err := in.Write([]byte{}); err != nil {
			return false
		}
		if err := in.Close(); err != nil {
			return false
		}
		return copyCmd.Wait() == nil
	}

	// if exec was not found in path we know it is not installed
	return false
}

func initialize() error {
	if os.Getenv("WAYLAND_DISPLAY") != "" {
		pasteCmdArgs = wlpasteArgs
		copyCmdArgs = wlcopyArgs

		if _, err := exec.LookPath(wlcopy); err == nil {
			if _, err := exec.LookPath(wlpaste); err == nil {
				return nil
			}
		}
	}

	pasteCmdArgs = xclipPasteArgs
	copyCmdArgs = xclipCopyArgs

	if verifyInstall(xclip) {
		return nil
	}

	pasteCmdArgs = xselPasteArgs
	copyCmdArgs = xselCopyArgs

	if verifyInstall(xsel) {
		return nil
	}

	pasteCmdArgs = termuxPasteArgs
	copyCmdArgs = termuxCopyArgs

	if _, err := exec.LookPath(termuxClipboardSet); err == nil {
		if _, err := exec.LookPath(termuxClipboardGet); err == nil {
			return nil
		}
	}

	if internalClipboards == nil {
		internalClipboards = make(map[string]string)
	}
	Unsupported = true

	return errors.New("System clipboard not found, install xclip/xsel")
}

func getPasteCommand(register string) *exec.Cmd {
	return exec.Command(pasteCmdArgs[register][0], pasteCmdArgs[register][1:]...)
}

func getCopyCommand(register string) *exec.Cmd {
	return exec.Command(copyCmdArgs[register][0], copyCmdArgs[register][1:]...)
}

func readAll(register string) (string, error) {
	if Unsupported {
		if text, ok := internalClipboards[register]; ok {
			return text, nil
		}
		return "", nil
	}
	pasteCmd := getPasteCommand(register)
	out, err := pasteCmd.Output()
	if err != nil {
		return "", err
	}
	return string(out), nil
}

func writeAll(text, register string) error {
	if Unsupported {
		internalClipboards[register] = text
		return nil
	}
	copyCmd := getCopyCommand(register)
	in, err := copyCmd.StdinPipe()
	if err != nil {
		return err
	}

	if err := copyCmd.Start(); err != nil {
		return err
	}
	if _, err := in.Write([]byte(text)); err != nil {
		return err
	}
	if err := in.Close(); err != nil {
		return err
	}
	return copyCmd.Wait()
}