aboutsummaryrefslogtreecommitdiff
path: root/kvmx-clipboard
blob: 3005c51be84eca6fd1dc520a5d52fcc3ea1846da (plain)
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
#!/usr/bin/env bash
#
# kvmx-clipboard manage clipboard sharing between host and guests
#
# Copyright (C) 2018 Silvio Rhatto - rhatto at riseup.net
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# Parameters
CONTENT=""

function __kvmx_clipboard_receive {
  local VM="$1"

  if [ "$VM" == "host" ]; then
    #CONTENT="`xclip -o`"
    CONTENT="`xsel -o`"
  else
    #CONTENT="`kvmx ssh $VM -X 'DISPLAY=:0 xclip -o'`"
    CONTENT="`kvmx ssh $VM -X 'xsel -o --display :0'`"
  fi
}

function __kvmx_clipboard_send {
  local VM="$1"

  if [ "$VM" == "host" ]; then
    #echo $CONTENT | xclip -i
    echo $CONTENT | xsel -i
  else
    #echo $CONTENT | kvmx ssh $VM -X 'DISPLAY=:0 xclip -i -l 1'
    echo $CONTENT | kvmx ssh $VM -X 'xsel -i --display :0'
  fi
}

function __kvmx_clipboard_copy {
  local ORIG="$1"
  local DEST="$2"

  __kvmx_clipboard_receive $ORIG
  __kvmx_clipboard_send    $DEST
}

function __kvmx_clipboard_list {
  # That's crude, but fast
  instances="`ps aux | grep qemu-system | grep -- '-name ' | sed -e 's/^.*-name //' -e 's/ .*//'`"
  instances="host $instances"

  echo "KVMX Clipboard"
  echo ""

  n="0"

  # Buld menu entries
  for orig_instance in $instances; do
    for dest_instance in $instances; do
      if [ "$orig_instance" == "$dest_instance" ]; then
        continue
      fi

      echo "$n: $orig_instance -> $dest_instance"

      let n++
    done
  done
}

function __kvmx_clipboard_menu {
  __kvmx_clipboard_list

  echo ""
  read -rep "Your choice: " n

  choice="$(__kvmx_clipboard_list | grep ^$n: | sed -e "s/^$n://" -e "s/->//")"

  if [ ! -z "$choice" ]; then
    __kvmx_clipboard_copy $choice
  fi
}

# Main
if [ -z "$2" ]; then
  __kvmx_clipboard_menu
else
  __kvmx_clipboard_copy $1 $2
fi