summaryrefslogtreecommitdiff
path: root/lock
blob: 367a916a59ede50160f51ff3ec8a2418e3614e63 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash

# This script is a workaround for xsecurelock not being able to lock the screen when a fullscreen window is in focus.

# Lock the screen with xsecurelock and take care of picom
lock_screen() {
    # kill the compositor so that xsecurelock doesn't complain
    pkill picom

    # xsecurelock configuration (you can find more flags here: https://github.com/google/xsecurelock)

    export XSECURELOCK_PASSWORD_PROMPT=time_hex
    export XSECURELOCK_FONT="Inconsolata:size=17"
    export XSECURELOCK_DATETIME_FORMAT="</%B %d> <%H:%M>"
    export XSECURELOCK_SHOW_DATETIME=1

    export XSECURELOCK_BACKGROUND_COLOR="#0a001f"
    export XSECURELOCK_AUTH_BACKGROUND_COLOR="#0a001f"
    export XSECURELOCK_AUTH_FOREGROUND_COLOR="#ace6f0"

    export XSECURELOCK_DISCARD_FIRST_KEYPRESS=0
    export XSECURELOCK_BURNIN_MITIGATION=0

    xsecurelock

    picom &
}

# Settings
declare -a LIST_OF_WINDOW_TITLES_THAT_PREVENT_LOCKING=(
    "YouTube"
    "Netflix"
    "Disney+"
    "Prime Video"
    "Hulu"
    "HBO Max"
    "Twitch"
    "Spotify"
    "Spotube"
    "MPlayer"
    "VLC"
    "mpv"
    "Zoom"
    "Google Meet"
    "Microsoft Teams"
    "Discord"
    "Slack"
    "Plex"
    "Kodi"
    "Jellyfish"
    "OBS"
    "CCTV"
)

# Dependencies
AWK=/bin/awk
GREP=/bin/grep
XPROP=/bin/xprop

# Find active window id
get_active_id() {
    $XPROP -root | $AWK '$1 ~ /_NET_ACTIVE_WINDOW/ { print $5 }'
}

# Determine a window's title text by it's ID
get_window_title() {
    # For some apps you may need to check WM_CLASS(STRING) instead
    $XPROP -id $1 | $AWK -F '=' '$1 ~ /_NET_WM_NAME\(UTF8_STRING\)/ { print $2 }'
}

# Determine if a window is fullscreen based on it's ID
is_fullscreen() {
    $XPROP -id $1 | $AWK -F '=' '$1 ~ /_NET_WM_STATE\(ATOM\)/ { for (i=2; i<=3; i++) if ($i ~ /FULLSCREEN/) exit 0; exit 1 }'
    return $?
}

# Determine if the locker command should run based on which windows are fullscreened.
should_lock() {

    id=$(get_active_id)
    if [[ $id == "" ]]; then
        echo "No active window found in the workspace"
        return 0
    fi
    echo "$id"

    title=$(get_window_title $id)
    echo "Active window:$title"

    if is_fullscreen $id; then
        echo "Fullscreen window detected:$title"
        return 1
    else
        for i in "${LIST_OF_WINDOW_TITLES_THAT_PREVENT_LOCKING[@]}"; do
            if [[ $title =~ $i ]]; then
                echo "$i was found in$title"
                return 1
            fi
        done
        return 0
    fi
}

# Debugging
#if should_lock; then
    #echo "Locking the screen..."
    #/bin/dunstify -u low "Locking the screen..."
#else
    #echo "Not locking the screen..."
    #/bin/dunstify -u low "Not locking the screen..."
#fi

# main()
if should_lock; then
    lock_screen
fi