tgui-bash - Termux:GUI in bash
tgui-bash filepath ...
tgui-bash -h ...
source tgui-bash
Sets up a connection to the Termux:GUI plugin through a small C helper, defines functions to interact with the plugin and finally sources the parameter into the script, so the functions are available.
Can be used in a shebang (#!/bin/tgui-bash
) or a script can self-exec (! [ -v tgc_activity_tid ] && exec tgui-bash "${BASH_SOURCE[0]}" "$@"
).
Using set -eo pipefail
is advised to make your script exit when the connection to the plugin gets broken.
For development, you should use set -u
to make sure you spelled the constant names correctly.
This library also uses the EXIT trap handler, so make sure to just add to it, but not overwrite it.
-h
Shows this manual or the tutorial in a browser of your choice. The following positional arguments are taken as answers to the questions.
All environment variables that affect bash will affect this program.
For passing arrays to functions and returning arrays, bash namerefs are used.
Reference parameters are marked with & and accept a variable name.
Colors are specified in RGBA in hex with aabbggrr
.
Boolean values use the literal text true
and false
, as they are directly used for the JSON messages to the plugin.
Images should be in PNG or JPEG format and base64 encoded (base64 -w 0 file
or image_generating_command | base64 -w 0
).
Public functions and variables start with tg_
, private functions and variables with tg__
.
Private functions and variables can change between versions, public ones should be stable.
More documentation for the functions as defined in the protocol is available here.
Name | Description | Parameters | Return code |
---|---|---|---|
tg_global_turn_screen_on | Turns the screen on. | ||
tg_global_is_locked | Returns whether the screen is locked. | 0: locked 1: unlocked |
|
tg_global_version | Returns the version code of the plugin to stdout. | ||
tg_msg_recv_event | Returns an event if one is available, else an empty string. | ||
tg_msg_recv_event_blocking | Waits for an event and returns it. |
Name | Description | Parameters |
---|---|---|
tg_activity_new | Creates a new Activity. See the tgc_activity_* constants for keys and their effect. |
|
tg_activity_finish | Finishes an Activity. | The Activity id of the Activity. |
tg_activity_to_back | Moves the Task of an Activity to the back. | The Activity id of the Activity. |
tg_activity_theme | Sets the theme of the Activity. |
|
tg_activity_description | Sets the Task description and icon. |
|
tg_activity_pip_params | Sets the aspect ratio for picture-in-picture mode. |
|
tg_activity_input | Sets how the Activity responds to the soft keyboard. "resize" resizes the Activity to fit the keyboard, "pan", pans the Activity upward. |
|
tg_activity_pip | Makes an Activity enter or leave picture-in-picture-mode. |
|
tg_activity_pip_auto | Set if an Activity should go into pip automatically if the user leaves. |
|
tg_activity_keep_screen_on | Sets if showing the Activity should keep the screen from turning off. |
|
tg_activity_orientation | Sets the Orientation of the Activity. |
|
tg_activity_position | Sets the screen position for an overlay Activity. |
|
tg_activity_configuration | Gets the configuration for the Activity as a string. Get the values with the tg_configuration_* functions. |
|
tg_activity_request_unlock | Requests the user to unlock the screen or unlocks it if the screen isn't protected. |
|
tg_activity_hide_soft_keyboard | Hides the software keyboard. |
|
tg_activity_intercept_back_button | Sets whether the back button should be intercepted. See the constant tg_actvivity_intercept fro more information. |
|
Name | Description | Parameters |
---|---|---|
tg_task_finish | Finishes all activites in a Task. | The Task id. |
tg_task_to_front | Makes a Task visible to the user again. | The Task id. |
These functions all get the configuration string as the first parameter.
Name | Description |
---|---|
tg_configuration_dark_mode | Whether dark mode is enabled, "true" or "false". "null" on Android versions prior to 10. |
tg_configuration_country | The country as a 2-letter string. |
tg_configuration_language | The language as a 2-letter string. |
tg_configuration_orientation | The screen orientation, either "landscape" or "portrait". |
tg_configuration_keyboardHidden | Whether a keyboard is currently available, as the string "true" or "false". |
tg_configuration_screenwidth | The current window width in dp. |
tg_configuration_screenheight | The current window height in dp. |
tg_configuration_fontscale | The current font scale value as a floating point number. |
tg_configuration_density | The display density as a float, such that screenwidth * density = screenwidth_in_px. |
All functions take the Activity id as the first parameter and
a parameter associative array reference as the second parameter.
The third parameter is an optional parent view id.
For root Views, specify ""
as the third parameter or leave it out.
The fourth parameter is the optional initial visibility of the View.
The id of the created View is returned on stdout.
The key for the parameter array are listed under the constants under tgc_create_*
.
Name | Description |
---|---|
tg_create_linear | Creates a LinearLayout. |
tg_create_frame | Creates a FrameLayout. |
tg_create_swipe_refresh | Creates a SwipeRefreshLayout. |
tg_create_text | Creates a TextView. |
tg_create_edit | Creates an EditText. |
tg_create_button | Creates a Button. |
tg_create_image | Creates an ImageView. |
tg_create_space | Creates a Space. |
tg_create_nested_scroll | Creates a NestedScrollView. |
tg_create_horizontal_scroll | Creates a HorizontalScrollView. |
tg_create_radio | Creates a RadioButton. |
tg_create_radio_group | Creates a RadioGroup. |
tg_create_checkbox | Creates a Checkbox. |
tg_create_toggle | Creates a ToggleButton. |
tg_create_switch | Creates a Switch. |
tg_create_spinner | Creates a Spinner. |
tg_create_progress | Creates a ProgressBar. |
tg_create_tab | Creates a TabLayout. |
tg_create_grid | Creates a GridLayout. |
tg_create_web | Creates a WebView. |
All these functions get the Activity id and the View id as the first and second parameters respectively.
Name | Description | Parameters |
---|---|---|
tg_view_show_cursor | Sets whether or not a cursor is shown in an EditText. |
|
tg_view_linear | Sets the LinearLayout parameters for a View in a LinearLayout. |
|
tg_view_grid | Sets the GridLayout parameters for a View in a GridLayout. |
|
tg_view_location | Sets position of a View. |
|
tg_view_vis | Sets the visibility of a Vew. |
|
tg_view_width | Sets the width of the view. |
|
tg_view_height | Sets the height of the view. |
|
tg_view_dimensions | Gets the current with and height of a View in pixels. | |
tg_view_delete | Deletes a View and its children from the Layout hierarchy. | |
tg_view_delete_children | Deletes the children of a View from the Layout hierarchy. | |
tg_view_margin | Sets the margin of a View. |
|
tg_view_padding | Sets the padding of a View. |
|
tg_view_bg_color | Sets the background color of a View. |
|
tg_view_text_color | Sets the text color of a View. |
|
tg_view_progress | Sets the progress of a ProgressBar. |
|
tg_view_refreshing | Sets whether a SwipeRefreshLayout is refreshing. |
|
tg_view_text | Sets the text of the View. |
|
tg_view_gravity | Sets the gravity of the text of the View. |
|
tg_view_text_size | Sets the text size. |
|
tg_view_get_text | gets the text of the View. | |
tg_view_checked | Sets a RadioButton, CheckBox, Switch or ToggleButton to checked or unchecked explicitly. |
|
tg_view_request_focus | Focuses a View and opens the soft keyboard if the View has Keyboard input. |
|
tg_view_get_scroll | Gets the x and y scroll position of an NestedScrollView or HorizontalScrollView. | |
tg_view_set_scroll | Sets the x and y scroll position of an NestedScrollView or HorizontalScrollView. |
|
tg_view_list | Set the list of a Spinner or TabLayout. |
|
tg_view_image | Sets the image for an ImageView. |
|
tg_view_select_tab | Selects a Tab in a TabLayout. The corresponding itemselected event is also send. |
|
tg_view_select_item | Selects a item in a Spinner. The corresponding itemselected event is also send. |
|
tg_view_clickable | Sets whether a View can be clicked by the user (if yes, emits a sound and animation when clicked and sends a click event (if click events are enabled))). |
|
Name | Description | Parameters |
---|---|---|
tg_remote_create_layout | Creates a new remote Layout. Returns the id for the remote Layout | |
tg_remote_delete_layout | Deletes a remote Layout. |
|
tg_remote_create_frame | Creates a FrameLayout in a remote Layout. Returns the View id in the Layout. |
|
tg_remote_create_text | Creates a TextView in a remote Layout. Returns the View id in the Layout. |
|
tg_remote_create_button | Creates a Button in a remote Layout. Returns the View id in the Layout. |
|
tg_remote_create_image | Creates an ImageView in a remote Layout. Returns the View id in the Layout. |
|
tg_remote_create_progress | Creates a ProgressBar in a remote Layout. Returns the View id in the Layout. |
|
tg_remote_bg_color | Sets the background color for a View in a remote Layout. |
|
tg_remote_progress | Sets values for a ProgressBar in a remote Layout. |
|
tg_remote_text | Sets the text of a remote View. |
|
tg_remote_text_size | Sets the text size of a remote View. |
|
tg_remote_text_color | Sets the Text color for a View in a remote Layout. |
|
tg_remote_vis | Sets the visibility for a View in a remote Layout. |
|
tg_remote_padding | Sets the padding in pixels for a View in a remote Layout. |
|
tg_remote_image | Sets the image of a remote ImageView. |
|
tg_widget_layout | Sets the layout of a Widget to a remote Layout. |
|
tg_not_create_channel | On Android 8.0 and higher this creates a NotificationChannel, if one with the given id doesn't exist. |
|
tg_not_create | Creates a notification. Returns the notification id used for further calls. |
|
tg_not_cancel | Cancels a notification. |
|
These functions change whether specific events are send for Views.
The first parameter is the Activity id of the View, the second the id of the View
and the third is the boolean for whether to send the event or not.
Name |
---|
tg_event_send_click |
tg_event_send_long_click |
tg_event_send_focus |
tg_event_send_touch |
tg_event_send_text |
Events are send as JSON, and are thus not usable directly in Bash.
That's where the dependency on jq
comes in: it is used to parse the events.
You can use jq
on the event data directly, but for some common tasks there are utility functions.
They all get the event data as their only parameter.
For more information about events, see the protocol specification.
Name | Description |
---|---|
tg_event_type | Returns the event type, which can then be compared to one of the tgc_ev_* constants. |
tg_event_value | Returns the event value, which is a JSON object for some events and a direct value for others. |
tg_event_aid | Returns the Activity id of the event, or "null" if not present. |
tg_event_id | Returns the View id of the event, or "null" if not present. |
All these functions get the Activity id and the View id as the first and second parameters respectively.
Name | Description | Parameters |
---|---|---|
tg_web_allow_js | Sets whether Javascript is allowed to run in the WebView. |
|
tg_web_allow_content | Sets whether content URIs are allowed in the WebView. |
|
tg_web_set_data | Sets the document. |
|
tg_web_load_uri | Loads a specific URI. |
|
tg_web_allow_navigation | Sets whether user/Javascript navigation is allowed in the WebView. |
|
tg_web_back | Goes back in the history. | |
tg_web_forward | Goes forward in the history. | |
tg_web_eval_js | Runs Javascript. |
|
Key for the tg_activity_new
options array. The value is a number.
Specify a Task id here if you want Activities to launch over each other in the same Task.
Key for the tg_activity_new
options array. The value is a boolean.
Set this to make the Activity a dialog.
Key for the tg_activity_new
options array. The value is a boolean.
Set this to false if you want your dialog to not be dismissed when the user taps on something else.
Key for the tg_activity_new
options array. The value is a boolean.
Set this to let the Activity start in picture-in-picture mode.
Key for the tg_activity_new
options array. The value is a boolean.
Set this to make the Activity stay visible and interactable on the lockscreen.
Make sure your interface is secure in this case, to not allow arbitrary command execution or file I/O.
Key for the tg_activity_new
options array. The value is a boolean.
This launches the Activity as an overlay over everything else, similar to picture-in-picture mode, but you can interact with all Views.
Key for the tg_activity_new
options array. The value is a boolean.
This option makes the back button send an event instead of finishing the Activity.
Key for the tg_create_*
parameter array.
For Button, TextView and EditText, this is the initial Text.
Key for the tg_create_*
parameter array.
For TextViews, this specifies whether the text can be selected. Default is false.
Key for the tg_create_*
parameter array.
For TextViews, this specifies whether links can be clicked or not. Default is false.
Key for the tg_create_*
parameter array.
For LinearLayout, this specifies if the Layout is vertical or horizontal.
If not specified, vertical is assumed.
Key for the tg_create_*
parameter array.
NestedScrollView and HorizontalScrollView snap to the nearest item if this is set to true.
Default is false.
Key for the tg_create_*
parameter array.
Makes the child of a HorizontalScrollView or a NestedScrollView automatically expand to the ScrollView size.
Default is false.
Key for the tg_create_*
parameter array.
Hides the scroll bar for HorizontalScrollView and NestedScrollView.
Default is false.
Key for the tg_create_*
parameter array.
Whether a RadioButton, CheckBox, Switch or ToggleButton should be checked.
Defaults to false.
Key for the tg_create_*
parameter array.
Whether an EditText should enable multiple lines to be entered.
Key for the tg_create_*
parameter array.
Whether the line below an EditText should be shown.
Key for the tg_create_*
parameter array.
For EditText this specifies the input type: can be one of "text", "textMultiLine", "phone", "date", "time", "datetime", "number", "numberDecimal", "numberPassword", "numberSigned", "numberDecimalSigned", "textEmailAddress", "textPassword". "text" is the default. Specifying singleline as true sets this to "text".
Key for the tg_create_*
parameter array.
Row count for GridLayout.
Key for the tg_create_*
parameter array.
Column count for GridLayout.
Key for the tg_create_*
parameter array.
Use this when creating a button to make all text automatically all caps (using small caps if possible).
Visibility constant for Views. This makes Views invisible and take up no space in the Layout.
Visibility constant for Views. This makes Views visible.
tgc_vis_hidden="1"
Visibility constant for Views. This makes Views invisible, but still take up space in the Layout.
Size constant for Views. This makes Views take as much space as needed for their content.
Size constant for Views. This makes Views take as much space as their parent.
Alignment for Views in GridLayout.
Alignment for Views in GridLayout.
Alignment for Views in GridLayout.
Alignment for Views in GridLayout.
Alignment for Views in GridLayout.
Alignment for Views in GridLayout.
Alignment for Views in GridLayout.
Direction constant.
Direction constant.
Direction constant.
Direction constant.
Text gravity constant. This makes text gravitate to the top/left.
Text gravity constant. This makes text gravitate to the center.
Text gravity constant. This makes text gravitate to the bottom/right.
Key for the tg_not_create
parameter array.
The id of the notification to update. If not specified, generates a new id.
Key for the tg_not_create
parameter array.
If true, the notification cannot be dismissed by the user,
but the notification is automatically dismissed when you close the connection to the plugin.
Key for the tg_not_create
parameter array.
The id of the remote Layout to use.
Key for the tg_not_create
parameter array.
The id of the remote Layout to use when the notification has been expanded.
Key for the tg_not_create
parameter array.
The id of the remote Layout to use when the notification is shown as a head-up notification.
Key for the tg_not_create
parameter array.
The notification title.
Key for the tg_not_create
parameter array.
The notification content text.
Key for the tg_not_create
parameter array.
A large image to display in the expanded view. You can only set either largeImage or largeText.
Key for the tg_not_create
parameter array.
A large block of text to display in the expanded view. HTML formatting is supported.
You can only set either largeImage or largeText.
Key for the tg_not_create
parameter array.
If true, the largeImage is shown as a thumbnail in the collapsed view.
Key for the tg_not_create
parameter array.
An image for the Notification in PNG or JPEG.
Defaults to the Termux:GUI app icon if left empty.
Key for the tg_not_create
parameter array.
If this call is used to update a notification, don't alert the user again.
Key for the tg_not_create
parameter array.
Shows the timestamp of the notification.
Key for the tg_not_create
parameter array.
The timestamp to use in form of milliseconds since start of the epoch.
Key for the tg_not_create
parameter array.
An array of strings with the names of actions.
Pressing actions will generate a notificationaction event.
For more information about events, see the protocol specification.
Constant | Description |
---|---|
behind | Keep the screen in the same orientation as whatever is behind this activity. Corresponds to ActivityInfo.SCREEN_ORIENTATION_BEHIND. |
fullSensor | Orientation is determined by a physical orientation sensor: the display will rotate based on how the user moves the device. This allows any of the 4 possible rotations, regardless of what the device will normally do (for example some devices won't normally use 180 degree rotation). Corresponds to ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR. |
fullUser | Respect the user's sensor-based rotation preference, but if sensor-based rotation is enabled then allow the screen to rotate in all 4 possible directions regardless of what the device will normally do (for example some devices won't normally use 180 degree rotation). Corresponds to ActivityInfo.SCREEN_ORIENTATION_FULL_USER. |
landscape | Would like to have the screen in a landscape orientation: that is, with the display wider than it is tall, ignoring sensor data. Corresponds to ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE. |
locked | Screen is locked to its current rotation, whatever that is. Corresponds to ActivityInfo.SCREEN_ORIENTATION_LOCKED. |
nosensor | Always ignore orientation determined by orientation sensor: the display will not rotate when the user moves the device. Corresponds to ActivityInfo.SCREEN_ORIENTATION_NOSENSOR. |
portrait | Would like to have the screen in a portrait orientation: that is, with the display taller than it is wide, ignoring sensor data. Corresponds to ActivityInfo.SCREEN_ORIENTATION_PORTRAIT. |
reverseLandscape | Would like to have the screen in landscape orientation, turned in the opposite direction from normal landscape. Corresponds to ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE. |
reversePortrait | Would like to have the screen in portrait orientation, turned in the opposite direction from normal portrait. Corresponds to ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT. |
sensor | Orientation is determined by a physical orientation sensor: the display will rotate based on how the user moves the device. Ignores user's setting to turn off sensor-based rotation. Corresponds to ActivityInfo.SCREEN_ORIENTATION_SENSOR. |
sensorLandscape | Would like to have the screen in landscape orientation, but can use the sensor to change which direction the screen is facing. Corresponds to ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE. |
sensorPortrait | Would like to have the screen in portrait orientation, but can use the sensor to change which direction the screen is facing. Corresponds to ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT. |
unspecified | No preference specified: let the system decide the best orientation. This will either be the orientation selected by the activity below, or the user's preferred orientation if this activity is the bottom of a task. If the user explicitly turned off sensor based orientation through settings sensor based device rotation will be ignored. If not by default sensor based orientation will be taken into account and the orientation will changed based on how the user rotates the device. Corresponds to ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED. |
user | Use the user's current preferred orientation of the handset. Corresponds to ActivityInfo.SCREEN_ORIENTATION_USER. |
userLandscape | Would like to have the screen in landscape orientation, but if the user has enabled sensor-based rotation then we can use the sensor to change which direction the screen is facing. Corresponds to ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE. |
userPortrait | Would like to have the screen in portrait orientation, but if the user has enabled sensor-based rotation then we can use the sensor to change which direction the screen is facing. Corresponds to ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT. |
Report bugs as GitHub issues: https://github.com/tareksander/termux-gui-bash/issues
Tarek Sander