PSALM - demo4.bash
Not logged in
#!/usr/bin/env bash
#
# Demonstration on how to create a GTK GUI using Psalm with BASH using TCP.
#
# Supposes the presence of 'socat' on the system. Requires Psalm 1.0.8 or higher.
#
# When using a remote system, socat must be started specifying the IP address of that system, eg:
#   socat EXEC:./psalm TCP-LISTEN:50000,bind=1.2.3.4 &
#
# The script on this system should then setup the appropriate socket:
#   exec 2>/dev/null 3<>/dev/tcp/1.2.3.4/50000
#
# When Psalm is running on a remote system then extra measures must be taken to accomplish a GUI.
# For non-GUI programs it should be fine though.
#
# Run this script as follows:
#   ./demo4.bash
#
# December 16, 2022 by Peter van Eerten.
#------------------------------------------------------------------------------------

function comm
{
    echo "${@}" >&3
    read answer <&3
}

# Start psalm as TCP-server in the background using SOCAT.
socat EXEC:./psalm TCP-LISTEN:50000 &

# Setup channel for BASH, wait until succesfull.
false
while [[ ${?} -eq 1 ]]
do
    exec 2>/dev/null 3<>/dev/tcp/127.0.0.1/50000
done

# Send definitions first
comm "DEF libgtk-3.so.0 void gtk_init int int"
comm "DEF libgtk-3.so.0 long gtk_window_new int"
comm "DEF libgtk-3.so.0 void gtk_window_set_title int char*"
comm "DEF libgtk-3.so.0 long gtk_button_new_with_label char*"
comm "DEF libgtk-3.so.0 long gtk_fixed_new void"
comm "DEF libgtk-3.so.0 long gtk_fixed_put long long int int"
comm "DEF libgtk-3.so.0 long gtk_calendar_new void"
comm "DEF libgtk-3.so.0 void gtk_main_iteration void"
comm "DEF libgtk-3.so.0 void gtk_widget_show_all int"
comm "DEF libgtk-3.so.0 void gtk_widget_set_size_request long int int"
comm "DEF libgtk-3.so.0 void gtk_container_add long long"
comm "DEF libgobject-2.0.so.0 long g_signal_connect_data int char* address void* void* int"

# Design GUI
comm "EXE gtk_init 0 0"
comm "EXE gtk_window_new 0"; win=${answer}
comm "EXE gtk_window_set_title ${win} \"Hello Calendar\""
comm "EXE gtk_widget_set_size_request ${win} 300 300"
comm "EXE gtk_fixed_new"; fixed=${answer}
comm "EXE gtk_container_add ${win} ${fixed}"
comm "EXE gtk_button_new_with_label \"Press Me\""; button=${answer}
comm "EXE gtk_widget_set_size_request ${button} 100 30"
comm "EXE gtk_fixed_put ${fixed} ${button} 190 260"
comm "EXE gtk_calendar_new"; calendar=${answer}
comm "EXE gtk_widget_set_size_request ${calendar} 200 200"
comm "EXE gtk_fixed_put ${fixed} ${calendar} 10 10"
comm "EXE gtk_widget_show_all ${win}"

# Connect signals
comm "EXE g_signal_connect_data ${win} delete-event callback NULL NULL 0"
comm "EXE g_signal_connect_data ${button} clicked callback NULL NULL 0"

# Mainloop
while [[ $cb -ne $button && $cb -ne $win ]]
do
    comm "EXE gtk_main_iteration"
    comm "CALLBACK 1"; cb=${answer}
done

comm "EXIT"

# Cleanup
exec 3>&-
exec 3<&-

Return to PSALM