Got .lua fixed! Need last piece of code to close conky please [SOLVED]

Add functionality to your desktop
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Post Reply
User avatar
Logansfury
Level 8
Level 8
Posts: 2441
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Got .lua fixed! Need last piece of code to close conky please [SOLVED]

Post by Logansfury »

Hello everyone,

I had a brainstorm and took my .lua back to the online resources and changed my wording requesting the showing of the image and the playing of the .mp3 to help separate the actions and control them. It freaking worked!!!

I now have the script simultaneously displaying one random image and playing the corresponding .mp3!

Now all I need is a final pause, and then an automated close of the conky widget. I tried to get working code from online but it was no good. I was given this:

Code: Select all

    -- Function to check if playerctl is running
    function isPlayerctlRunning()
        local handle = io.popen("playerctl status 2>/dev/null")
        local result = handle:read("*a")
        handle:close()
        return result ~= ""
    end

    -- Monitor for media playback using playerctl
    while isPlayerctlRunning() do
        os.execute("sleep 1")
    end

    -- Set here the PATH to conky
    local eqconky = "/home/logansfury/.conky/Biohazzard/joke.conf"

    -- Search for the joke widget using wmctrl
    local handle = io.popen("wmctrl -l | grep joke")
    local wmctrl_output = handle:read("*a")
    handle:close()

    -- Get PID of conky
    local handle_ps = io.popen("ps aux | grep \"" .. eqconky .. "\" | grep -v grep | awk '{print $2}'")
    local pid = handle_ps:read("*a")
    handle_ps:close()

    -- Kill the process with the obtained PID
    os.execute("kill " .. pid)

    print("joke widget closed")
end
but it broke the script and the .png stopped displaying.

I then did some experimenting with bash scripts and found that playerctl does NOT detect mpg123 running.

Here is the current .lua, correctly executing image and sound:

Code: Select all

require 'cairo'
require 'imlib2'

home_path = os.getenv('HOME')

math.randomseed(os.time()) -- Seed the random number generator with the current time
local mp3_played = false
local random_number = math.random(1, 51)

function fDrawImage(cr, path, xx, yy, ww, hh, arc)
    local img = cairo_image_surface_create_from_png(path)
    local w_img = cairo_image_surface_get_width(img)
    local h_img = cairo_image_surface_get_height(img)
    cairo_translate(cr, xx, yy)
    if arc then
        cairo_rotate(cr, arc)
    end
    cairo_scale(cr, ww / w_img, hh / h_img)
    cairo_set_source_surface(cr, img, -w_img / 2, -h_img / 2)
    cairo_paint(cr)
    cairo_surface_destroy(img)
    collectgarbage()
end

function conky_main()
    if conky_window == nil then
        return
    end

    local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
    local cr = cairo_create(cs)

    -- Draw the image
    print("Drawing image...")  -- Debug statement
    fDrawImage(cr, home_path .. '/Pictures/microsoft_jokes/' .. tostring(random_number) .. '.png', 162, 107, 324, 214)
    print("Image drawn.")  -- Debug statement

    cairo_surface_destroy(cs)
    cairo_destroy(cr)

    -- Play the MP3 
    if not mp3_played then
        print("Playing MP3...")  -- Debug statement
        os.execute("mpg123 " .. home_path .. "/Music/eyegor/" .. tostring(random_number) .. ".mp3 &")
        mp3_played = true
        print("MP3 played.")  -- Debug statement
    end
end

can anyone show me how to correctly monitor the system for mpg123 to complete play of .mp3 file, then close the conky?

Thanks for reading :)
Last edited by Logansfury on Thu May 16, 2024 6:14 pm, edited 1 time in total.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 8
Level 8
Posts: 2441
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Got .lua fixed! Need last piece of code to close conky please

Post by Logansfury »

HOLY CRAP SQUARED!!!

I just got what looks like a working .lua code to monitor the running of mpg123 and react when it stops.

I believe I can run this in tandem with the other .lua an use it to shut down the conky.

Will report back!
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
shedyed
Level 4
Level 4
Posts: 344
Joined: Wed Feb 03, 2021 5:12 pm

Re: Got .lua fixed! Need last piece of code to close conky please

Post by shedyed »

As a trial remove the '&' after the filename where you have the mpg123 command.
This could eliminate the additional steps to see if the music is still playing.
When the song ends, put an exit 0 or return.
User avatar
Logansfury
Level 8
Level 8
Posts: 2441
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Got .lua fixed! Need last piece of code to close conky please [SOLVED]

Post by Logansfury »

I GOT IT!!!!

I CAN'T BELIEVE IT!! WAZOOOOO :D :D :D

I came up with the idea of using a bash script to monitor, detect the close of, and subsequently close the conky. I first tried to launch the .lua with lua_load and an ${exec ./stop.sh} in the conky.text portion of the .conf file, but I found this prevented the .lua from launching.

So I asked the resources how to launch a bash script from a .lua script and was given:

Code: Select all

-- Execute the Bash script
os.execute("./stop.sh &")
I added it to .lua, removed the exec from .conf, and test fired. Everything is perfect. The randomizer is generating true random numbers every time, the .png file and .mp3 are being executed almost simultaneously, and the new bash script is closing the conky and removing the image from screen after the mp3 play is done.

Here are the scripts:

joke.conf

Code: Select all

--[[
Bleys 2024 

]]
conky.config = {
-- — Conky settings

	background = false,
	total_run_times = 0,
	no_buffers = true,	

-- — Window specifications with Background

	own_window = true,
	own_window_type = dock,
	own_window_title = 'microsoft-joke',
    own_window_hints = 'skip_taskbar,skip_pager,undecorated,above',
    own_window_transparent = false,
	own_window_argb_visual = true,
	own_window_argb_value = 0,
	own_window_colour = '000000', 
	
	double_buffer = true,
	minimum_width = 324, minimum_height = 214,
	alignment = 'middle_middle',
	
	gap_x = 20,
	gap_y = 20,

    update_interval = 3600,

-- Lua Load
	lua_load = '/home/logansfury/.conky/Biohazzard/joke.lua',
	lua_draw_hook_post = 'main',		
};
conky.text = [[

]];
joke.lua

Code: Select all

require 'cairo'
require 'imlib2'

-- Execute the Bash script
os.execute("./stop.sh &")

home_path = os.getenv('HOME')

math.randomseed(os.time()) -- Seed the random number generator with the current time
local mp3_played = false
local random_number = math.random(1, 51)

function fDrawImage(cr, path, xx, yy, ww, hh, arc)
    local img = cairo_image_surface_create_from_png(path)
    local w_img = cairo_image_surface_get_width(img)
    local h_img = cairo_image_surface_get_height(img)
    cairo_translate(cr, xx, yy)
    if arc then
        cairo_rotate(cr, arc)
    end
    cairo_scale(cr, ww / w_img, hh / h_img)
    cairo_set_source_surface(cr, img, -w_img / 2, -h_img / 2)
    cairo_paint(cr)
    cairo_surface_destroy(img)
    collectgarbage()
end

function conky_main()
    if conky_window == nil then
        return
    end

    local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
    local cr = cairo_create(cs)

    -- Draw the image
    print("Drawing image...")  -- Debug statement
    fDrawImage(cr, home_path .. '/Pictures/microsoft_jokes/' .. tostring(random_number) .. '.png', 162, 107, 324, 214)
    print("Image drawn.")  -- Debug statement

    cairo_surface_destroy(cs)
    cairo_destroy(cr)

    -- Play the MP3 
    if not mp3_played then
        print("Playing MP3...")  -- Debug statement
        os.execute("mpg123 " .. home_path .. "/Music/eyegor/" .. tostring(random_number) .. ".mp3 &")
        mp3_played = true
        print("MP3 played.")  -- Debug statement
    end
end
stop.sh

Code: Select all

#!/bin/bash

# Pause for 2 seconds
sleep 2

echo "Running..."

# Function to check if mpg123 is running
isMpg123Playing() {
    pgrep -x "mpg123" >/dev/null
}

# Monitor for mpg123 playback
while isMpg123Playing; do
    sleep 1 # Wait for 1 second before checking again
done

# When mpg123 playback stops, do something else
echo "mpg123 playback stopped"

# Set here the PATH to joke conky
eqconky="/home/logansfury/.conky/Biohazzard/joke.conf"

# Search for the joke widget using wmctrl
VRESULT=$(wmctrl -l | grep microsoft-joke)

# Check if the joke widget is running
if [ -n "$VRESULT" ]; then
    # Get PID of conky
    pid=$(ps aux | grep "$eqconky" | grep -v grep | awk '{print $2}')
    
   
    echo "PID of joke widget: $pid"
    sleep 1s
    kill $pid
    echo "joke widget closed"
else
    echo "joke widget is not running."
fi
It is launched by an applet in my top panel that launches a bash script when clicked:
eyegore.sh

Code: Select all

#!/bin/bash

# reset variables
aud_var=0
vlc_var=0
cle_var=0

# Function to check if Audacious is playing
check_audacious_playing() {
    if pgrep -x "audacious" >/dev/null; then
        audacious_status=$(audtool playback-status)
        if [ "$audacious_status" == "playing" ]; then
            echo "Audacious is playing music."
            audtool --playback-pause
            aud_var=1
        else
            echo "Audacious is open but not playing."
        fi
    else
        echo "Audacious is not running."
    fi
}

# Check if Audacious is running
check_audacious_playing

# Function to check if VLC is playing
check_vlc_playing() {
    if pgrep -x "vlc" >/dev/null; then
        vlc_status=$(playerctl -p vlc status 2>/dev/null)
        if [ "$vlc_status" == "Playing" ]; then
            echo "VLC is playing media."
            dbus-send --session --type=method_call --dest=org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Pause
            vlc_var=1
        else
            echo "VLC is open but not playing."
        fi
    else
        echo "VLC is not running."
    fi
}

# Check if VLC is running and playing
check_vlc_playing

# Function to check if Clementine is playing
check_cle_playing() {
    # Get list of running media players
    players=$(playerctl -l)
    # Check if Clementine is in the list
    if echo "$players" | grep -q "clementine"; then
        # Check if the player is playing
        status=$(playerctl -p "clementine" status 2>/dev/null)
        if [ "$status" == "Playing" ]; then
        echo "Clementine is playing music."
        clementine --play-pause
        cle_var=1
        else
        echo "Clementine is open but not playing."
        fi
        else
        echo "Clementine is not running."
    fi
}

# Check if Clementine is running and playing
check_cle_playing

   cd "$HOME/.conky/Biohazzard"
   conky -c "$HOME/.conky/Biohazzard/joke.conf" 

# Pause for 1 second
sleep 1

# Check if aud_var is set to 1
if [ "$aud_var" -eq 1 ]; then
    # Resume the audio using audtool
    audtool --playback-pause
# Check if vlc_var is set to 1
elif [ "$vlc_var" -eq 1 ]; then
    # Play the audio using VLC through D-Bus
    dbus-send --session --type=method_call --dest=org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Play
# Check if cle_var is set to 1
elif [ "$cle_var" -eq 1 ]; then
#Resume play on Clementine
    clementine --play-pause
else
    # No audio to resume
    echo "No audio to resume"
fi
Here it is in action! Make sure to enable audio :)

https://imgur.com/43JoBnv

Final analysis:

No smoke or flames seem to be coming from my computer case.
I didn't get dizzy, pass out, or fall over during this. Not even ONCE.
I cannot friggin' believe it :D
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
Post Reply

Return to “Compiz, Conky, Docks & Widgets”