haideralipunjabi / polybar-kdeconnect

KDEConnect module for Polybar
https://blog.haideralipunjabi.com/posts/making-modules-for-polybar-shell-python/
165 stars 11 forks source link

Nothing happens when clicking on the icon #17

Open Kabouik opened 4 years ago

Kabouik commented 4 years ago

I'm not sure how to trigger the rofi menu, should it show up when clicking on the icon? I see no errors when running polybar from terminal and clicking on the icon, but nothing happens.

[module/kdeconnect]
type = custom/script  
exec = "~/.config/polybar/scripts/polybar-kdeconnect.sh -d"
tail = true
format-padding = 2
#!/usr/bin/env bash

# CONFIGURATION
LOCATION=0
YOFFSET=0
XOFFSET=0
WIDTH=12
WIDTH_WIDE=24
THEME=solarized

# Color Settings of Icon shown in Polybar
COLOR_DISCONNECTED='#888888'       # Device Disconnected
COLOR_NEWDEVICE='#ffffff'          # New Device
COLOR_BATTERY_90='#ffffff'         # Battery >= 90
COLOR_BATTERY_80='#ffffff'         # Battery >= 80
COLOR_BATTERY_70='#ffffff'         # Battery >= 70
COLOR_BATTERY_60='#ffffff'         # Battery >= 60
COLOR_BATTERY_50='#ffffff'         # Battery >= 50
COLOR_BATTERY_LOW='#728cbb'        # Battery <  50

# Icons shown in Polybar
ICON_SMARTPHONE=''
ICON_TABLET=''
SEPERATOR='|'

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

show_devices (){
    IFS=$','
    devices=""
    for device in $(qdbus --literal org.kde.kdeconnect /modules/kdeconnect org.kde.kdeconnect.daemon.devices); do
        deviceid=$(echo "$device" | awk -F'["|"]' '{print $2}')
        devicename=$(qdbus org.kde.kdeconnect "/modules/kdeconnect/devices/$deviceid" org.kde.kdeconnect.device.name)
        devicetype=$(qdbus org.kde.kdeconnect "/modules/kdeconnect/devices/$deviceid" org.kde.kdeconnect.device.type)
        isreach="$(qdbus org.kde.kdeconnect "/modules/kdeconnect/devices/$deviceid" org.kde.kdeconnect.device.isReachable)"
        istrust="$(qdbus org.kde.kdeconnect "/modules/kdeconnect/devices/$deviceid" org.kde.kdeconnect.device.isTrusted)"
        if [ "$isreach" = "true" ] && [ "$istrust" = "true" ]
        then
            battery="$(qdbus org.kde.kdeconnect "/modules/kdeconnect/devices/$deviceid" org.kde.kdeconnect.device.battery.charge)"
            icon=$(get_icon "$battery" "$devicetype")
            devices+="%{A1:$DIR/polybar-kdeconnect.sh -n '$devicename' -i $deviceid -b $battery -m:}$icon%{A}$SEPERATOR"
        elif [ "$isreach" = "false" ] && [ "$istrust" = "true" ]
        then
            devices+="$(get_icon -1 "$devicetype")$SEPERATOR"
        else
            haspairing="$(qdbus org.kde.kdeconnect "/modules/kdeconnect/devices/$deviceid" org.kde.kdeconnect.device.hasPairingRequests)"
            if [ "$haspairing" = "true" ]
            then
                show_pmenu2 "$devicename" "$deviceid"
            fi
            icon=$(get_icon -2 "$devicetype")
            devices+="%{A1:$DIR/polybar-kdeconnect.sh -n $devicename -i $deviceid -p:}$icon%{A}$SEPERATOR"

        fi
    done
    echo "${devices::-1}"
}

show_menu () {
    menu="$(rofi -sep "|" -dmenu -i -p "$DEV_NAME" -location $LOCATION -yoffset $YOFFSET -xoffset $XOFFSET -theme $THEME -width $WIDTH -hide-scrollbar -line-padding 4 -padding 20 -lines 5 <<< "Battery: $DEV_BATTERY%|Ping|Find Device|Send File|Browse Files|Unpair")"
                case "$menu" in
                    *Ping) qdbus org.kde.kdeconnect "/modules/kdeconnect/devices/$DEV_ID/ping" org.kde.kdeconnect.device.ping.sendPing ;;
                    *'Find Device') qdbus org.kde.kdeconnect "/modules/kdeconnect/devices/$DEV_ID/findmyphone" org.kde.kdeconnect.device.findmyphone.ring ;;
                    *'Send File') qdbus org.kde.kdeconnect "/modules/kdeconnect/devices/$DEV_ID/share" org.kde.kdeconnect.device.share.shareUrl "file://$(zenity --file-selection)" ;;
                    *'Browse Files')
                        if "$(qdbus --literal org.kde.kdeconnect "/modules/kdeconnect/devices/$DEV_ID/sftp" org.kde.kdeconnect.device.sftp.isMounted)" == "false"; then
                            qdbus org.kde.kdeconnect "/modules/kdeconnect/devices/$DEV_ID/sftp" org.kde.kdeconnect.device.sftp.mount
                        fi
                        qdbus org.kde.kdeconnect "/modules/kdeconnect/devices/$DEV_ID/sftp" org.kde.kdeconnect.device.sftp.startBrowsing
                        ;;
                    *'Unpair' ) qdbus org.kde.kdeconnect "/modules/kdeconnect/devices/$DEV_ID" org.kde.kdeconnect.device.unpair
                esac
}

show_pmenu () {
    menu="$(rofi -sep "|" -dmenu -i -p "$DEV_NAME" -location $LOCATION -yoffset $YOFFSET -xoffset $XOFFSET -theme $THEME -width $WIDTH -hide-scrollbar -line-padding 1 -padding 20 -lines 1<<<"Pair Device")"
                case "$menu" in
                    *'Pair Device') qdbus org.kde.kdeconnect "/modules/kdeconnect/devices/$DEV_ID" org.kde.kdeconnect.device.requestPair
                esac
}

show_pmenu2 () {
    menu="$(rofi -sep "|" -dmenu -i -p "$1 has sent a pairing request" -location $LOCATION -yoffset $YOFFSET -xoffset $XOFFSET -theme $THEME -width $WIDTH_WIDE -hide-scrollbar -line-padding 4 -padding 20 -lines 2 <<< "Accept|Reject")"
                case "$menu" in
                    *'Accept') qdbus org.kde.kdeconnect "/modules/kdeconnect/devices/$2" org.kde.kdeconnect.device.acceptPairing ;;
                    *) qdbus org.kde.kdeconnect "/modules/kdeconnect/devices/$2" org.kde.kdeconnect.device.rejectPairing
                esac

}
get_icon () {
    if [ "$2" = "tablet" ]
    then
        icon=$ICON_TABLET
    else
        icon=$ICON_SMARTPHONE
    fi
    case $1 in
    "-1")     ICON="%{F$COLOR_DISCONNECTED}$icon%{F-}" ;;
    "-2")     ICON="%{F$COLOR_NEWDEVICE}$icon%{F-}" ;;
    5*)     ICON="%{F$COLOR_BATTERY_50}$icon%{F-}" ;;
    6*)     ICON="%{F$COLOR_BATTERY_60}$icon%{F-}" ;;
    7*)     ICON="%{F$COLOR_BATTERY_70}$icon%{F-}" ;;
    8*)     ICON="%{F$COLOR_BATTERY_80}$icon%{F-}" ;;
    9*|100) ICON="%{F$COLOR_BATTERY_90}$icon%{F-}" ;;
    *)      ICON="%{F$COLOR_BATTERY_LOW}$icon%{F-}" ;;
    esac
    echo $ICON
}

unset DEV_ID DEV_NAME DEV_BATTERY
while getopts 'di:n:b:mp' c
do
    # shellcheck disable=SC2220
    case $c in
        d) show_devices ;;
        i) DEV_ID=$OPTARG ;;
        n) DEV_NAME=$OPTARG ;;
        b) DEV_BATTERY=$OPTARG ;;
        m) show_menu  ;;
        p) show_pmenu ;;
    esac
done
haideralipunjabi commented 4 years ago

It should only show up if the device is connected. What is the color of the icon you are clicking?

Kabouik commented 4 years ago

Oh, I see, so I need to add my devices using kdeconnect-cli first I suppose. There is probably an issue with my phone because it is not listed when I run kdeconnect-cli -l on the computer. I'll report back here if the click action works when this other issue is fixed.

I changed the colours in your script but the icon shown is the icon corresponding to no device connected, indeed.

haideralipunjabi commented 4 years ago

An unpaired device is shown in Yellow Color (default colours). You can pair using that icon as well. A device has two boolean values associated with it, isReachable and isTrusted. An unpaired device will have isReachable = true and isTrusted = false.

octal-illumination commented 3 years ago

Hi I am having a similar issue here, and in my case, the device is connected. I have black icons shaped as circle square and triangle when device is paired and not reachable, red colored icons when the device is paired and reachable. events from kdeconnect app is working on either direction, but unfortunately no rofi menu as experienced by the OP. AM i doing something wrong?

red_threeshapes

black_threeshapes

My Polybar-kdeconnect config:

[module/kdeconnect]
type = custom/script
exec = "~/.config/polybar/scripts/kdeconnect/polybar-kdeconnect.sh -d"
tail = true

I can't figure out what I am doing wrong here. Please let me know if you need any other information.

haideralipunjabi commented 3 years ago

What happens when you execute the following in terminal:

rofi -sep "|" -dmenu -i -p DEMO -location 0 <<< "Row1|Row2"

octal-illumination commented 3 years ago

rofi -sep "|" -dmenu -i -p DEMO -location 0 <<< "Row1|Row2"

I get the rofi demo menu with Row1 and Row2

octal-illumination commented 3 years ago

Additionally i get grey icons once in a while when the device is paired and reachable, verified when i check with kdeconnect-cli -l, i get

- Device name: Device ID (paired and reachable)
1 device found

This is the same result i get when i have red icons. grey_threeshapes

Not sure why different colors for the same state. May be something else is wrong with my set up?

Update: okay, going through the script, i can see it indicates the battery level, which is 79% now. But still no menu or mobile icons though.

haideralipunjabi commented 3 years ago

Yeah, the colours change with the battery level of the device. But I still can't figure out why clicking isn't working for you

octal-illumination commented 3 years ago

Okay cool. I will try to play around and if i find something, I will post here, thanks

ghost commented 3 years ago

I'm having the same issue. Clicking does nothing. Icon is white, and kdeconnect-cli -l shows the device as paired and reachable. rofi -sep "|" -dmenu -i -p DEMO -location 0 <<< "Row1|Row2 brings up the rofi demo menu.

Has anyone found a solution?

k1ake commented 2 years ago

Have the same problem, but i found an issue: script does not assign properly DEV_ID and DEV_NAME, so you have to manually put it inside script or into your polybar config. So i solved that with adding that string in script: DEV_ID="$(kdeconnect-cli -a --id-only)"

appcreatorguy commented 2 years ago

Ok, i was having the same issue and found that due to my device name in KDE Connect having single quotes in it, the name getting passed to the script to bring up the rofi menu was being escaped, causing an error. Changing the device name in KDE Connect settings to remove the single quotes fixed this for me