summaryrefslogtreecommitdiff
path: root/lock
blob: f28a93b90cdac26d423bb3efb25d21b69fb43793 (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
#!/bin/bash

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

# 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"
    "Twitch"
    #"Spotify"
    "MPlayer"
    "VLC"
    "mpv"
    "Google Meet"
    "Microsoft Teams"
    "Zoom"
    #"Discord"
    #"Slack"
    #"Plex"
    "CCTV"
)

# Dependencies
AWK=/usr/bin/awk
GREP=/usr/bin/grep
XPROP=/usr/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 mplayer or vlc, we might need to check WM_CLASS(STRING), idk.
    $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)
    title=$(get_window_title $id)

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

# Debugging
if should_lock; then
    echo "Locking the screen..."
else
    echo "Not locking the screen..."
fi

# main()
if should_lock; then
    dunstify -u critical "Screen will close"
    sleep 15
    lock_screen
fi