PSALM - demo3.sh
Not logged in
#!/usr/bin/env sh
#
# Demonstration on how to create a GTK GUI using Psalm with Bourne Shell (dash).
#
# Supposes the presence of the GNU coreutils 'mkfifo'. Requires Psalm 1.0.8 or higher.
#
# Run as follows:
#   ./demo3.sh
#
# December 3, 2022 by Peter van Eerten.
#------------------------------------------------------------------------------------

# Define the named pipes
IN=/dev/shm/psalm.in
OUT=/dev/shm/psalm.out

# Always delete old named pipes
rm -f ${IN} ${OUT}

# Create the named pipes for communication
mkfifo ${IN} ${OUT}

# Start psalm, redirect input and output to named pipes
./psalm <${IN} >${OUT} &

# Communication function
comm()
{
    echo ${@} > ${IN}
    read answer < ${OUT}
}

# 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 test "${cb}" != "${button}" -a "${cb}" != "${win}"
do
    comm "EXE gtk_main_iteration"
    comm "CALLBACK 1"; cb=${answer}
done

comm "EXIT"

# Cleanup
rm ${IN} ${OUT}

Return to PSALM