2 # Copyright (C) 2015 Mathieu Duponchelle <mathieu.duponchelle@opencreed.com>
4 # bash/zsh completion support for gst-launch
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Library General Public
8 # License as published by the Free Software Foundation; either
9 # version 2 of the License, or (at your option) any later version.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Library General Public License for more details.
16 # You should have received a copy of the GNU Library General Public
17 # License along with this library; if not, write to the
18 # Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 # Boston, MA 02110-1301, USA.
21 _GST_HELPERDIR="${BASH_SOURCE[0]%/*}/../helpers"
23 if [[ ! -d "$_GST_HELPERDIR" ]]; then
24 _GST_HELPERDIR="$(pkg-config --variable=helpersdir gstreamer-1.0)"
26 _GST_HELPERDIR=`cd "$_GST_HELPERDIR"; pwd`
30 . "$_GST_HELPERDIR"/gst
32 _GST_HELPER="$_GST_HELPERDIR/gst-completion-helper-1.0"
34 _gst_launch_all_arguments ()
36 COMPREPLY=( $(compgen -W "$(gst-launch-1.0 --help-all | grep -oh '[[:graph:]]*--[[:graph:]]*' | cut -d'=' -f1)" -- $cur) )
39 _gst_complete_compatible_elements ()
41 COMPREPLY=( $(compgen -W "$($_GST_HELPER --compatible-with $previous_element)" -- $cur) )
44 _gst_complete_all_elements ()
46 COMPREPLY=( $(compgen -W "$($_GST_HELPER -l)" -- $cur) )
49 _gst_complete_element_properties ()
51 COMPREPLY=( $(compgen -W "$($_GST_HELPER --element-properties $previous_element)" -- $cur) )
54 _gstlaunch___exclude_ () { _gst_mandatory_argument; }
58 local i=1 command function_exists previous_element have_previous_element=0 completion_func
60 while [[ $i -ne $COMP_CWORD ]];
63 var="${COMP_WORDS[i]}"
64 if [[ "$var" == "--"* ]]
72 while [[ $i -ne $COMP_CWORD ]];
75 var="${COMP_WORDS[i]}"
77 if [[ "$var" == "--"* ]]
83 $(gst-inspect-1.0 --exists $var)
86 previous_element="$var"
87 have_previous_element=1
92 if [[ "$command" == "--gst"* ]]; then
93 completion_func="_${command//-/_}"
95 completion_func="_gstlaunch_${command//-/_}"
98 # Seems like bash doesn't like "exclude" in function names
99 if [[ "$completion_func" == "_gstlaunch___exclude" ]]
101 completion_func="_gstlaunch___exclude_"
104 declare -f $completion_func >/dev/null 2>&1
108 if [[ "$cur" == "--"* ]]; then
109 _gst_launch_all_arguments
110 elif [ $function_exists -eq 0 ]
113 elif [ $have_previous_element -ne 0 ] && [[ "$prev" == "!" ]]
115 _gst_complete_compatible_elements
116 elif [ $have_previous_element -ne 0 ]
118 _gst_complete_element_properties
120 _gst_complete_all_elements
124 _gst_launch_func_wrap ()
127 cur="${COMP_WORDS[COMP_CWORD]}"
128 prev="${COMP_WORDS[COMP_CWORD-1]}"
132 # Setup completion for certain functions defined above by setting common
133 # variables and workarounds.
134 # This is NOT a public function; use at your own risk.
135 _gst_launch_complete ()
137 local wrapper="__launch_wrap${2}"
138 eval "$wrapper () { _gst_launch_func_wrap $2 ; }"
139 complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
140 || complete -o default -o nospace -F $wrapper $1
143 _gst_launch_complete gst-launch-1.0 _gst_launch_main