2006-09-25 01:09:00 +00:00
/*
2006-09-27 17:12:42 +00:00
* wslua_gui . c
2008-04-10 15:26:22 +00:00
*
2008-08-05 21:03:46 +00:00
* ( c ) 2006 , Luis E . Garcia Ontanon < luis @ ontanon . org >
2008-04-10 15:26:22 +00:00
*
2006-09-25 01:09:00 +00:00
* Wireshark - Network traffic analyzer
* By Gerald Combs < gerald @ wireshark . org >
* Copyright 1998 Gerald Combs
*
2018-02-08 16:33:09 +00:00
* SPDX - License - Identifier : GPL - 2.0 - or - later
2006-09-25 01:09:00 +00:00
*/
2011-04-21 13:13:39 +00:00
# include "config.h"
2014-06-17 22:07:24 +00:00
# include <epan/wmem/wmem.h>
2013-09-13 08:49:38 +00:00
2006-09-25 01:09:00 +00:00
# include "wslua.h"
2020-03-30 15:57:16 +00:00
/* WSLUA_MODULE Gui GUI Support */
2006-10-18 17:59:49 +00:00
2006-09-25 01:09:00 +00:00
static const funnel_ops_t * ops = NULL ;
struct _lua_menu_data {
lua_State * L ;
int cb_ref ;
} ;
2010-10-21 12:41:15 +00:00
static int menu_cb_error_handler ( lua_State * L ) {
2006-09-25 01:09:00 +00:00
const gchar * error = lua_tostring ( L , 1 ) ;
2020-10-29 15:01:27 +00:00
report_failure ( " Lua: Error during execution of Menu callback: \n %s " , error ) ;
2008-04-10 15:26:22 +00:00
return 0 ;
2006-09-25 01:09:00 +00:00
}
2020-03-23 14:58:54 +00:00
WSLUA_FUNCTION wslua_gui_enabled ( lua_State * L ) { /* Checks if we're running inside a GUI (i.e. Wireshark) or not. */
2007-01-21 17:05:33 +00:00
lua_pushboolean ( L , GPOINTER_TO_INT ( ops & & ops - > add_button ) ) ;
2020-03-23 14:58:54 +00:00
WSLUA_RETURN ( 1 ) ; /* Boolean `true` if a GUI is available, `false` if it isn't. */
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
static void lua_menu_callback ( gpointer data ) {
2013-03-16 08:12:29 +00:00
struct _lua_menu_data * md = ( struct _lua_menu_data * ) data ;
2012-03-08 20:00:49 +00:00
lua_State * L = md - > L ;
2008-04-10 15:26:22 +00:00
2012-03-08 20:00:49 +00:00
lua_settop ( L , 0 ) ;
2006-10-19 17:02:23 +00:00
lua_pushcfunction ( L , menu_cb_error_handler ) ;
lua_rawgeti ( L , LUA_REGISTRYINDEX , md - > cb_ref ) ;
2008-04-10 15:26:22 +00:00
2012-03-08 20:00:49 +00:00
switch ( lua_pcall ( L , 0 , 0 , 1 ) ) {
2010-10-21 12:41:15 +00:00
case 0 :
break ;
case LUA_ERRRUN :
2021-06-14 23:06:02 +00:00
ws_warning ( " Runtime error while calling menu callback " ) ;
2010-10-21 12:41:15 +00:00
break ;
case LUA_ERRMEM :
2021-06-14 23:06:02 +00:00
ws_warning ( " Memory alloc error while calling menu callback " ) ;
2010-10-21 12:41:15 +00:00
break ;
2020-10-29 15:01:27 +00:00
case LUA_ERRERR :
2021-06-14 23:06:02 +00:00
ws_warning ( " Error while running the error handler function for menu callback " ) ;
2020-10-29 15:01:27 +00:00
break ;
2010-10-21 12:41:15 +00:00
default :
2021-03-20 01:58:26 +00:00
ws_assert_not_reached ( ) ;
2010-10-21 12:41:15 +00:00
break ;
2006-10-19 17:02:23 +00:00
}
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
return ;
}
2020-03-23 14:58:54 +00:00
WSLUA_FUNCTION wslua_register_menu ( lua_State * L ) { /* Register a menu item in one of the main menus. Requires a GUI. */
# define WSLUA_ARG_register_menu_NAME 1 /* The name of the menu item. Use slashes to separate submenus. (e.g. menu:Lua Scripts[My Fancy Statistics]). (string) */
# define WSLUA_ARG_register_menu_ACTION 2 /* The function to be called when the menu item is invoked. The function must take no arguments and return nothing. */
# define WSLUA_OPTARG_register_menu_GROUP 3 / * Where to place the item in the menu hierarchy. If omitted, defaults to MENU_STAT_GENERIC. One of:
* MENU_STAT_UNSORTED : menu : Statistics [ ]
* MENU_STAT_GENERIC : menu : Statistics [ ] , first section
* MENU_STAT_CONVERSATION : menu : Statistics [ Conversation List ]
* MENU_STAT_ENDPOINT : menu : Statistics [ Endpoint List ]
* MENU_STAT_RESPONSE : menu : Statistics [ Service Response Time ]
* MENU_STAT_TELEPHONY : menu : Telephony [ ]
* MENU_STAT_TELEPHONY_ANSI : menu : Telephony [ ANSI ]
* MENU_STAT_TELEPHONY_GSM : menu : Telephony [ GSM ]
* MENU_STAT_TELEPHONY_LTE : menu : Telephony [ LTE ]
* MENU_STAT_TELEPHONY_MTP3 : menu : Telephony [ MTP3 ]
* MENU_STAT_TELEPHONY_SCTP : menu : Telephony [ SCTP ]
* MENU_ANALYZE : menu : Analyze [ ]
* MENU_ANALYZE_CONVERSATION : menu : Analyze [ Conversation Filter ]
* MENU_TOOLS_UNSORTED : menu : Tools [ ] */
2006-09-27 17:12:42 +00:00
2006-09-25 01:09:00 +00:00
const gchar * name = luaL_checkstring ( L , WSLUA_ARG_register_menu_NAME ) ;
struct _lua_menu_data * md ;
gboolean retap = FALSE ;
2014-02-27 04:42:15 +00:00
register_stat_group_t group = ( register_stat_group_t ) wslua_optguint ( L , WSLUA_OPTARG_register_menu_GROUP , REGISTER_STAT_GROUP_GENERIC ) ;
2006-09-27 17:12:42 +00:00
2014-02-27 04:42:15 +00:00
if ( group > REGISTER_TOOLS_GROUP_UNSORTED ) {
2012-03-08 20:00:49 +00:00
WSLUA_OPTARG_ERROR ( register_menu , GROUP , " Must be a defined MENU_* (see init.lua) " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
}
2006-09-27 17:12:42 +00:00
2014-02-27 04:42:15 +00:00
if ( ! lua_isfunction ( L , WSLUA_ARG_register_menu_ACTION ) ) {
2012-03-08 20:00:49 +00:00
WSLUA_ARG_ERROR ( register_menu , ACTION , " Must be a function " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
}
2006-09-27 17:12:42 +00:00
2020-12-21 02:30:28 +00:00
md = g_new ( struct _lua_menu_data , 1 ) ;
2006-09-25 01:09:00 +00:00
md - > L = L ;
2006-09-27 17:12:42 +00:00
2012-03-08 20:00:49 +00:00
lua_pushvalue ( L , 2 ) ;
md - > cb_ref = luaL_ref ( L , LUA_REGISTRYINDEX ) ;
lua_remove ( L , 2 ) ;
2006-09-25 01:09:00 +00:00
2010-10-21 12:41:15 +00:00
funnel_register_menu ( name ,
group ,
lua_menu_callback ,
md ,
2019-11-04 09:52:25 +00:00
g_free ,
2010-10-21 12:41:15 +00:00
retap ) ;
2006-09-25 01:09:00 +00:00
WSLUA_RETURN ( 0 ) ;
}
2015-08-11 12:08:08 +00:00
void wslua_deregister_menus ( void ) {
funnel_deregister_menus ( lua_menu_callback ) ;
}
2006-09-25 01:09:00 +00:00
struct _dlg_cb_data {
lua_State * L ;
int func_ref ;
} ;
2010-10-21 12:41:15 +00:00
static int dlg_cb_error_handler ( lua_State * L ) {
2006-09-25 01:09:00 +00:00
const gchar * error = lua_tostring ( L , 1 ) ;
2020-10-29 15:01:27 +00:00
report_failure ( " Lua: Error during execution of Dialog callback: \n %s " , error ) ;
2006-09-25 01:09:00 +00:00
return 0 ;
}
2010-10-21 12:41:15 +00:00
static void lua_dialog_cb ( gchar * * user_input , void * data ) {
2013-03-16 08:12:29 +00:00
struct _dlg_cb_data * dcbd = ( struct _dlg_cb_data * ) data ;
2006-09-25 01:09:00 +00:00
int i = 0 ;
gchar * input ;
lua_State * L = dcbd - > L ;
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
lua_settop ( L , 0 ) ;
lua_pushcfunction ( L , dlg_cb_error_handler ) ;
lua_rawgeti ( L , LUA_REGISTRYINDEX , dcbd - > func_ref ) ;
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
for ( i = 0 ; ( input = user_input [ i ] ) ; i + + ) {
lua_pushstring ( L , input ) ;
g_free ( input ) ;
}
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
g_free ( user_input ) ;
2008-04-10 15:26:22 +00:00
2006-10-19 17:02:23 +00:00
switch ( lua_pcall ( L , i , 0 , 1 ) ) {
2006-09-25 01:09:00 +00:00
case 0 :
break ;
case LUA_ERRRUN :
2021-06-14 23:06:02 +00:00
ws_warning ( " Runtime error while calling dialog callback " ) ;
2006-09-25 01:09:00 +00:00
break ;
case LUA_ERRMEM :
2021-06-14 23:06:02 +00:00
ws_warning ( " Memory alloc error while calling dialog callback " ) ;
2006-09-25 01:09:00 +00:00
break ;
2020-10-29 15:01:27 +00:00
case LUA_ERRERR :
2021-06-14 23:06:02 +00:00
ws_warning ( " Error while running the error handler function for dialog callback " ) ;
2020-10-29 15:01:27 +00:00
break ;
2006-09-25 01:09:00 +00:00
default :
2021-03-20 01:58:26 +00:00
ws_assert_not_reached ( ) ;
2006-09-25 01:09:00 +00:00
break ;
}
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
}
2008-11-05 15:00:46 +00:00
struct _close_cb_data {
lua_State * L ;
int func_ref ;
TextWindow wslua_tw ;
} ;
2010-10-21 12:41:15 +00:00
static int text_win_close_cb_error_handler ( lua_State * L ) {
2008-11-05 15:00:46 +00:00
const gchar * error = lua_tostring ( L , 1 ) ;
2020-10-29 15:01:27 +00:00
report_failure ( " Lua: Error during execution of TextWindow close callback: \n %s " , error ) ;
2008-11-05 15:00:46 +00:00
return 0 ;
}
2010-10-21 12:41:15 +00:00
static void text_win_close_cb ( void * data ) {
2013-03-16 08:12:29 +00:00
struct _close_cb_data * cbd = ( struct _close_cb_data * ) data ;
2008-11-05 15:00:46 +00:00
lua_State * L = cbd - > L ;
if ( cbd - > L ) { /* close function is set */
2010-10-21 02:50:27 +00:00
2008-11-05 15:00:46 +00:00
lua_settop ( L , 0 ) ;
lua_pushcfunction ( L , text_win_close_cb_error_handler ) ;
lua_rawgeti ( L , LUA_REGISTRYINDEX , cbd - > func_ref ) ;
switch ( lua_pcall ( L , 0 , 0 , 1 ) ) {
case 0 :
break ;
case LUA_ERRRUN :
2021-06-14 23:06:02 +00:00
ws_warning ( " Runtime error during execution of TextWindow close callback " ) ;
2008-11-05 15:00:46 +00:00
break ;
case LUA_ERRMEM :
2021-06-14 23:06:02 +00:00
ws_warning ( " Memory alloc error during execution of TextWindow close callback " ) ;
2008-11-05 15:00:46 +00:00
break ;
2020-10-29 15:01:27 +00:00
case LUA_ERRERR :
2021-06-14 23:06:02 +00:00
ws_warning ( " Error while running the error handler function for TextWindow close callback " ) ;
2020-10-29 15:01:27 +00:00
break ;
2008-11-05 15:00:46 +00:00
default :
break ;
}
}
2010-10-21 02:50:27 +00:00
2008-11-05 15:00:46 +00:00
if ( cbd - > wslua_tw - > expired ) {
g_free ( cbd - > wslua_tw ) ;
2019-11-05 16:14:52 +00:00
g_free ( cbd ) ;
2008-11-05 15:00:46 +00:00
} else {
cbd - > wslua_tw - > expired = TRUE ;
}
}
2020-03-23 14:58:54 +00:00
WSLUA_FUNCTION wslua_new_dialog ( lua_State * L ) { /*
Displays a dialog , prompting for input . The dialog includes an btn : [ OK ] button and btn : [ Cancel ] button . Requires a GUI .
. An input dialog in action
image : : wsdg_graphics / wslua - new - dialog . png [ { small - screenshot - attrs } ]
= = = = = Example
[ source , lua ]
- - - -
if not gui_enabled ( ) then return end
- - Prompt for IP and port and then print them to stdout
local label_ip = " IP address "
local label_port = " Port "
local function print_ip ( ip , port )
print ( label_ip , ip )
print ( label_port , port )
end
new_dialog ( " Enter IP address " , print_ip , label_ip , label_port )
- - Prompt for 4 numbers and then print their product to stdout
new_dialog (
" Enter 4 numbers " ,
function ( a , b , c , d ) print ( a * b * c * d ) end ,
" a " , " b " , " c " , " d "
)
- - - -
*/
# define WSLUA_ARG_new_dialog_TITLE 1 /* The title of the dialog. */
# define WSLUA_ARG_new_dialog_ACTION 2 /* Action to be performed when the user presses btn:[OK]. */
/* WSLUA_MOREARGS new_dialog Strings to be used a labels of the dialog's fields. Each string creates a new labeled field. The first field is required. */
2010-10-21 12:41:15 +00:00
2006-09-25 01:09:00 +00:00
const gchar * title ;
int top = lua_gettop ( L ) ;
int i ;
GPtrArray * labels ;
struct _dlg_cb_data * dcbd ;
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
if ( ! ops ) {
luaL_error ( L , " the GUI facility has to be enabled " ) ;
return 0 ;
}
2008-04-10 15:26:22 +00:00
2011-06-25 20:47:13 +00:00
if ( ! ops - > new_dialog ) {
WSLUA_ERROR ( new_dialog , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2011-06-25 20:47:13 +00:00
}
2012-03-15 02:33:52 +00:00
2014-10-12 20:27:54 +00:00
title = luaL_checkstring ( L , WSLUA_ARG_new_dialog_TITLE ) ;
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
if ( ! lua_isfunction ( L , WSLUA_ARG_new_dialog_ACTION ) ) {
2009-05-08 16:40:38 +00:00
WSLUA_ARG_ERROR ( new_dialog , ACTION , " Must be a function " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2006-09-25 01:09:00 +00:00
}
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
if ( top < 3 ) {
2009-05-08 16:40:38 +00:00
WSLUA_ERROR ( new_dialog , " At least one field required " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2006-09-25 01:09:00 +00:00
}
2008-04-10 15:26:22 +00:00
2020-12-21 02:30:28 +00:00
dcbd = g_new ( struct _dlg_cb_data , 1 ) ;
2006-09-25 01:09:00 +00:00
dcbd - > L = L ;
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
lua_remove ( L , 1 ) ;
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
lua_pushvalue ( L , 1 ) ;
dcbd - > func_ref = luaL_ref ( L , LUA_REGISTRYINDEX ) ;
lua_remove ( L , 1 ) ;
2008-04-10 15:26:22 +00:00
2019-11-04 09:52:25 +00:00
labels = g_ptr_array_new_with_free_func ( g_free ) ;
2008-04-10 15:26:22 +00:00
2006-10-19 17:02:23 +00:00
top - = 2 ;
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
for ( i = 1 ; i < = top ; i + + ) {
2014-10-12 20:27:54 +00:00
if ( ! lua_isstring ( L , i ) ) {
2014-02-27 04:42:15 +00:00
g_ptr_array_free ( labels , TRUE ) ;
2017-12-15 20:38:30 +00:00
g_free ( dcbd ) ;
2012-03-08 20:00:49 +00:00
WSLUA_ERROR ( new_dialog , " All fields must be strings " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
}
2008-04-10 15:26:22 +00:00
2014-10-12 20:27:54 +00:00
g_ptr_array_add ( labels , ( gpointer ) g_strdup ( luaL_checkstring ( L , i ) ) ) ;
2006-09-25 01:09:00 +00:00
}
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
g_ptr_array_add ( labels , NULL ) ;
2008-04-10 15:26:22 +00:00
2019-11-04 09:52:25 +00:00
ops - > new_dialog ( title , ( const gchar * * ) ( labels - > pdata ) , lua_dialog_cb , dcbd , g_free ) ;
2008-04-10 15:26:22 +00:00
2014-02-27 04:42:15 +00:00
g_ptr_array_free ( labels , TRUE ) ;
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
WSLUA_RETURN ( 0 ) ;
}
2020-03-23 14:58:54 +00:00
WSLUA_CLASS_DEFINE ( ProgDlg , FAIL_ON_NULL ( " ProgDlg " ) ) ;
/*
Creates and manages a modal progress bar .
This is intended to be used with
http : //lua-users.org/wiki/CoroutinesTutorial[coroutines],
where a main UI thread controls the progress bar dialog while a background coroutine ( worker thread ) yields to the main thread between steps .
The main thread checks the status of the btn : [ Cancel ] button and if it ' s not set , returns control to the coroutine .
. A progress bar in action
image : : wsdg_graphics / wslua - progdlg . png [ { medium - screenshot - attrs } ]
The legacy ( GTK + ) user interface displayed this as a separate dialog , hence the “ Dlg ” suffix .
The Qt user interface shows a progress bar inside the main status bar .
*/
WSLUA_CONSTRUCTOR ProgDlg_new ( lua_State * L ) { /*
Creates and displays a new ` ProgDlg ` progress bar with a btn : [ Cancel ] button and optional title .
It is highly recommended that you wrap code that uses a ` ProgDlg ` instance because it does not automatically close itself upon encountering an error .
Requires a GUI .
= = = = = Example
[ source , lua ]
- - - -
if not gui_enabled ( ) then return end
local p = ProgDlg . new ( " Constructing " , " tacos " )
- - We have to wrap the ProgDlg code in a pcall in case some unexpected
- - error occurs .
local ok , errmsg = pcall ( function ( )
local co = coroutine . create (
function ( )
local limit = 100000
for i = 1 , limit do
print ( " co " , i )
coroutine . yield ( i / limit , " step " . . i . . " of " . . limit )
end
end
)
- - Whenever coroutine yields , check the status of the cancel button to determine
- - when to break . Wait up to 20 sec for coroutine to finish .
local start_time = os . time ( )
while coroutine . status ( co ) ~ = ' dead ' do
local elapsed = os . time ( ) - start_time
- - Quit if cancel button pressed or 20 seconds elapsed
if p : stopped ( ) or elapsed > 20 then
break
end
local res , val , val2 = coroutine . resume ( co )
if not res or res = = false then
if val then
debug ( val )
end
print ( ' coroutine error ' )
break
end
- - show progress in progress dialog
p : update ( val , val2 )
end
end )
p : close ( )
if not ok and errmsg then
report_failure ( errmsg )
end
- - - -
*/
# define WSLUA_OPTARG_ProgDlg_new_TITLE 1 /* Title of the progress bar. Defaults to "Progress". */
# define WSLUA_OPTARG_ProgDlg_new_TASK 2 /* Optional task name, which will be appended to the title. Defaults to the empty string (""). */
2013-03-16 08:12:29 +00:00
ProgDlg pd = ( ProgDlg ) g_malloc ( sizeof ( struct _wslua_progdlg ) ) ;
2010-10-21 12:41:15 +00:00
pd - > title = g_strdup ( luaL_optstring ( L , WSLUA_OPTARG_ProgDlg_new_TITLE , " Progress " ) ) ;
pd - > task = g_strdup ( luaL_optstring ( L , WSLUA_OPTARG_ProgDlg_new_TASK , " " ) ) ;
2008-08-11 01:33:31 +00:00
pd - > stopped = FALSE ;
2010-10-21 02:50:27 +00:00
2008-08-11 01:33:31 +00:00
if ( ops - > new_progress_window ) {
2015-07-03 20:52:49 +00:00
pd - > pw = ops - > new_progress_window ( ops - > ops_id , pd - > title , pd - > task , TRUE , & ( pd - > stopped ) ) ;
2011-06-25 20:47:13 +00:00
} else {
2017-12-15 20:38:30 +00:00
g_free ( pd ) ;
2012-03-15 02:33:52 +00:00
WSLUA_ERROR ( ProgDlg_new , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2008-08-11 01:33:31 +00:00
}
2010-10-21 02:50:27 +00:00
2008-08-11 01:33:31 +00:00
pushProgDlg ( L , pd ) ;
2010-10-21 02:50:27 +00:00
2014-03-23 15:01:12 +00:00
WSLUA_RETURN ( 1 ) ; /* The newly created `ProgDlg` object. */
2008-08-11 01:33:31 +00:00
}
2020-03-23 14:58:54 +00:00
WSLUA_METHOD ProgDlg_update ( lua_State * L ) { /* Sets the progress dialog's progress bar position based on percentage done. */
# define WSLUA_ARG_ProgDlg_update_PROGRESS 2 /* Progress value, e.g. 0.75. Value must be between 0.0 and 1.0 inclusive. */
# define WSLUA_OPTARG_ProgDlg_update_TASK 3 /* Task name. Currently ignored. Defaults to empty string (""). */
2010-10-21 12:41:15 +00:00
ProgDlg pd = checkProgDlg ( L , 1 ) ;
double pr = lua_tonumber ( L , WSLUA_ARG_ProgDlg_update_PROGRESS ) ;
const gchar * task = luaL_optstring ( L , WSLUA_OPTARG_ProgDlg_update_TASK , " " ) ;
2010-10-21 02:50:27 +00:00
2011-06-25 20:47:13 +00:00
if ( ! ops - > update_progress ) {
WSLUA_ERROR ( ProgDlg_update , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2011-06-25 20:47:13 +00:00
}
2012-03-15 02:33:52 +00:00
2008-08-11 01:33:31 +00:00
g_free ( pd - > task ) ;
pd - > task = g_strdup ( task ) ;
2012-03-08 20:00:49 +00:00
/* XXX, dead code: pd already dereferenced. should it be: !pd->task?
if ( ! pd ) {
WSLUA_ERROR ( ProgDlg_update , " Cannot be called for something not a ProgDlg " ) ;
2011-05-09 12:49:39 +00:00
} */
2010-10-21 02:50:27 +00:00
2012-05-21 07:42:09 +00:00
if ( pr > = 0.0 & & pr < = 1.0 ) {
2008-08-11 01:33:31 +00:00
ops - > update_progress ( pd - > pw , ( float ) pr , task ) ;
} else {
2010-10-21 12:41:15 +00:00
WSLUA_ERROR ( ProgDlg_update , " Progress value out of range (must be between 0.0 and 1.0) " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2008-08-11 01:33:31 +00:00
}
2010-10-21 02:50:27 +00:00
2008-08-11 01:33:31 +00:00
return 0 ;
}
2020-03-23 14:58:54 +00:00
WSLUA_METHOD ProgDlg_stopped ( lua_State * L ) { /* Checks whether the user has pressed the btn:[Cancel] button. */
2008-08-11 01:33:31 +00:00
ProgDlg pd = checkProgDlg ( L , 1 ) ;
2010-10-21 02:50:27 +00:00
2008-08-11 01:33:31 +00:00
lua_pushboolean ( L , pd - > stopped ) ;
2020-03-23 14:58:54 +00:00
WSLUA_RETURN ( 1 ) ; /* Boolean `true` if the user has asked to stop the operation, `false` otherwise. */
2010-10-21 02:50:27 +00:00
}
2008-08-11 01:33:31 +00:00
2010-10-21 12:41:15 +00:00
2020-03-23 14:58:54 +00:00
WSLUA_METHOD ProgDlg_close ( lua_State * L ) { /* Hides the progress bar. */
2008-08-11 01:33:31 +00:00
ProgDlg pd = checkProgDlg ( L , 1 ) ;
2010-10-21 02:50:27 +00:00
2011-06-25 20:47:13 +00:00
if ( ! ops - > destroy_progress_window ) {
WSLUA_ERROR ( ProgDlg_close , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2011-06-25 20:47:13 +00:00
}
2012-03-15 02:33:52 +00:00
2008-08-11 01:33:31 +00:00
if ( pd - > pw ) {
ops - > destroy_progress_window ( pd - > pw ) ;
pd - > pw = NULL ;
}
return 0 ;
}
2010-10-21 12:41:15 +00:00
static int ProgDlg__tostring ( lua_State * L ) {
2008-08-11 01:33:31 +00:00
ProgDlg pd = checkProgDlg ( L , 1 ) ;
2010-10-21 02:50:27 +00:00
2014-12-29 05:21:15 +00:00
lua_pushfstring ( L , " %sstopped " , pd - > stopped ? " " : " not " ) ;
2010-10-21 02:50:27 +00:00
2014-12-29 05:21:15 +00:00
WSLUA_RETURN ( 1 ) ; /* A string specifying whether the Progress Dialog has stopped or not. */
2008-08-11 01:33:31 +00:00
}
2013-02-25 22:05:28 +00:00
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
2010-10-21 12:41:15 +00:00
static int ProgDlg__gc ( lua_State * L ) {
2014-02-19 08:22:55 +00:00
ProgDlg pd = toProgDlg ( L , 1 ) ;
2010-10-21 02:50:27 +00:00
2008-08-11 01:33:31 +00:00
if ( pd ) {
2011-06-25 20:47:13 +00:00
if ( pd - > pw & & ops - > destroy_progress_window ) {
ops - > destroy_progress_window ( pd - > pw ) ;
}
2012-03-15 02:33:52 +00:00
2008-08-11 01:33:31 +00:00
g_free ( pd ) ;
} else {
luaL_error ( L , " ProgDlg__gc has being passed something else! " ) ;
}
2010-10-21 02:50:27 +00:00
2008-08-11 01:33:31 +00:00
return 0 ;
}
WSLUA_METHODS ProgDlg_methods [ ] = {
2013-02-15 01:38:45 +00:00
WSLUA_CLASS_FNREG ( ProgDlg , new ) ,
WSLUA_CLASS_FNREG ( ProgDlg , update ) ,
WSLUA_CLASS_FNREG ( ProgDlg , stopped ) ,
WSLUA_CLASS_FNREG ( ProgDlg , close ) ,
2014-02-19 08:22:55 +00:00
{ NULL , NULL }
2008-08-11 01:33:31 +00:00
} ;
WSLUA_META ProgDlg_meta [ ] = {
2014-02-19 08:22:55 +00:00
WSLUA_CLASS_MTREG ( ProgDlg , tostring ) ,
{ NULL , NULL }
2008-08-11 01:33:31 +00:00
} ;
2010-10-21 12:41:15 +00:00
int ProgDlg_register ( lua_State * L ) {
2008-08-11 01:33:31 +00:00
ops = funnel_get_funnel_ops ( ) ;
2010-10-21 02:50:27 +00:00
2012-03-08 20:00:49 +00:00
WSLUA_REGISTER_CLASS ( ProgDlg ) ;
2010-10-21 02:50:27 +00:00
2014-01-31 07:25:42 +00:00
return 0 ;
2008-08-11 01:33:31 +00:00
}
2020-03-23 14:58:54 +00:00
WSLUA_CLASS_DEFINE ( TextWindow , FAIL_ON_NULL_OR_EXPIRED ( " TextWindow " ) ) ; /*
Creates and manages a text window .
The text can be read - only or editable , and buttons can be added below the text .
. A text window in action
image : : wsdg_graphics / wslua - textwindow . png [ { medium - screenshot - attrs } ]
*/
2006-09-25 01:09:00 +00:00
2006-10-19 17:02:23 +00:00
/* XXX: button and close callback data is being leaked */
2010-10-21 02:50:27 +00:00
/* XXX: lua callback function and TextWindow are not garbage collected because
2008-11-05 15:00:46 +00:00
they stay in LUA_REGISTRYINDEX forever */
2006-10-19 17:02:23 +00:00
2020-03-23 14:58:54 +00:00
WSLUA_CONSTRUCTOR TextWindow_new ( lua_State * L ) { /*
Creates a new ` TextWindow ` text window and displays it .
Requires a GUI .
= = = = = Example
[ source , lua ]
- - - -
if not gui_enabled ( ) then return end
- - create new text window and initialize its text
local win = TextWindow . new ( " Log " )
win : set ( " Hello world! " )
- - add buttons to clear text window and to enable editing
win : add_button ( " Clear " , function ( ) win : clear ( ) end )
win : add_button ( " Enable edit " , function ( ) win : set_editable ( true ) end )
- - add button to change text to uppercase
win : add_button ( " Uppercase " , function ( )
local text = win : get_text ( )
if text ~ = " " then
win : set ( string . upper ( text ) )
end
end )
- - print " closing " to stdout when the user closes the text windw
win : set_atclose ( function ( ) print ( " closing " ) end )
- - - -
*/
# define WSLUA_OPTARG_TextWindow_new_TITLE 1 /* Title of the new window. Optional. Defaults to "Untitled Window". */
2010-10-21 12:41:15 +00:00
2006-09-25 01:09:00 +00:00
const gchar * title ;
2008-11-05 15:00:46 +00:00
TextWindow tw = NULL ;
struct _close_cb_data * default_cbd ;
2008-04-10 15:26:22 +00:00
2011-06-25 20:47:13 +00:00
if ( ! ops - > new_text_window | | ! ops - > set_close_cb ) {
WSLUA_ERROR ( TextWindow_new , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2011-06-25 20:47:13 +00:00
}
2012-03-15 02:33:52 +00:00
2020-03-23 14:58:54 +00:00
title = luaL_optstring ( L , WSLUA_OPTARG_TextWindow_new_TITLE , " Untitled Window " ) ;
2020-12-21 02:30:28 +00:00
tw = g_new ( struct _wslua_tw , 1 ) ;
2008-11-05 15:00:46 +00:00
tw - > expired = FALSE ;
tw - > ws_tw = ops - > new_text_window ( title ) ;
2010-10-21 02:50:27 +00:00
2020-12-21 02:30:28 +00:00
default_cbd = g_new ( struct _close_cb_data , 1 ) ;
2006-09-25 01:09:00 +00:00
2008-11-05 15:00:46 +00:00
default_cbd - > L = NULL ;
default_cbd - > func_ref = 0 ;
default_cbd - > wslua_tw = tw ;
2006-09-25 01:09:00 +00:00
2019-11-04 09:52:25 +00:00
tw - > close_cb_data = ( void * ) default_cbd ;
2008-11-05 15:00:46 +00:00
ops - > set_close_cb ( tw - > ws_tw , text_win_close_cb , default_cbd ) ;
2006-09-25 01:09:00 +00:00
2008-11-05 15:00:46 +00:00
pushTextWindow ( L , tw ) ;
2008-04-10 15:26:22 +00:00
2014-03-23 15:01:12 +00:00
WSLUA_RETURN ( 1 ) ; /* The newly created `TextWindow` object. */
2006-09-25 01:09:00 +00:00
}
2014-03-23 15:01:12 +00:00
WSLUA_METHOD TextWindow_set_atclose ( lua_State * L ) { /* Set the function that will be called when the text window closes. */
# define WSLUA_ARG_TextWindow_at_close_ACTION 2 /* A Lua function to be executed when the user closes the text window. */
2010-10-21 12:41:15 +00:00
2006-09-25 01:09:00 +00:00
TextWindow tw = checkTextWindow ( L , 1 ) ;
struct _close_cb_data * cbd ;
2011-06-25 20:47:13 +00:00
if ( ! ops - > set_close_cb ) {
WSLUA_ERROR ( TextWindow_set_atclose , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2011-06-25 20:47:13 +00:00
}
2012-03-15 02:33:52 +00:00
2006-10-19 17:02:23 +00:00
lua_settop ( L , 2 ) ;
2006-09-25 01:09:00 +00:00
2014-02-27 04:42:15 +00:00
if ( ! lua_isfunction ( L , 2 ) ) {
2009-05-08 16:40:38 +00:00
WSLUA_ARG_ERROR ( TextWindow_at_close , ACTION , " Must be a function " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
}
2008-04-10 15:26:22 +00:00
2020-12-21 02:30:28 +00:00
cbd = g_new ( struct _close_cb_data , 1 ) ;
2006-09-25 01:09:00 +00:00
cbd - > L = L ;
cbd - > func_ref = luaL_ref ( L , LUA_REGISTRYINDEX ) ;
2008-11-05 15:00:46 +00:00
cbd - > wslua_tw = tw ;
2008-04-10 15:26:22 +00:00
2019-11-04 09:52:25 +00:00
g_free ( tw - > close_cb_data ) ;
tw - > close_cb_data = ( void * ) cbd ;
2008-11-05 15:00:46 +00:00
ops - > set_close_cb ( tw - > ws_tw , text_win_close_cb , cbd ) ;
2008-04-10 15:26:22 +00:00
2014-03-23 15:01:12 +00:00
/* XXX: this is a bad way to do this - should copy the object on to the stack first */
WSLUA_RETURN ( 1 ) ; /* The `TextWindow` object. */
2006-09-25 01:09:00 +00:00
}
2020-03-23 14:58:54 +00:00
WSLUA_METHOD TextWindow_set ( lua_State * L ) { /* Sets the text to be displayed. */
# define WSLUA_ARG_TextWindow_set_TEXT 2 /* The text to be displayed. */
2010-10-21 12:41:15 +00:00
2006-09-25 01:09:00 +00:00
TextWindow tw = checkTextWindow ( L , 1 ) ;
2009-01-27 14:36:46 +00:00
const gchar * text = luaL_checkstring ( L , WSLUA_ARG_TextWindow_set_TEXT ) ;
2006-09-25 01:09:00 +00:00
2014-02-27 04:42:15 +00:00
if ( ! ops - > set_text ) {
2012-03-08 20:00:49 +00:00
WSLUA_ERROR ( TextWindow_set , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
}
2012-03-15 02:33:52 +00:00
2008-11-05 15:00:46 +00:00
ops - > set_text ( tw - > ws_tw , text ) ;
2008-04-10 15:26:22 +00:00
2014-03-23 15:01:12 +00:00
/* XXX: this is a bad way to do this - should copy the object on to the stack first */
WSLUA_RETURN ( 1 ) ; /* The `TextWindow` object. */
2006-09-25 01:09:00 +00:00
}
2020-03-23 14:58:54 +00:00
WSLUA_METHOD TextWindow_append ( lua_State * L ) { /* Appends text to the current window contents. */
# define WSLUA_ARG_TextWindow_append_TEXT 2 /* The text to be appended. */
2006-09-25 01:09:00 +00:00
TextWindow tw = checkTextWindow ( L , 1 ) ;
2009-01-27 14:36:46 +00:00
const gchar * text = luaL_checkstring ( L , WSLUA_ARG_TextWindow_append_TEXT ) ;
2008-04-10 15:26:22 +00:00
2014-02-27 04:42:15 +00:00
if ( ! ops - > append_text ) {
2012-03-08 20:00:49 +00:00
WSLUA_ERROR ( TextWindow_append , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
}
2012-03-15 02:33:52 +00:00
2008-11-05 15:00:46 +00:00
ops - > append_text ( tw - > ws_tw , text ) ;
2008-04-10 15:26:22 +00:00
2014-03-23 15:01:12 +00:00
/* XXX: this is a bad way to do this - should copy the object on to the stack first */
WSLUA_RETURN ( 1 ) ; /* The `TextWindow` object. */
2006-09-25 01:09:00 +00:00
}
2020-03-23 14:58:54 +00:00
WSLUA_METHOD TextWindow_prepend ( lua_State * L ) { /* Prepends text to the current window contents. */
# define WSLUA_ARG_TextWindow_prepend_TEXT 2 /* The text to be prepended. */
2006-09-25 01:09:00 +00:00
TextWindow tw = checkTextWindow ( L , 1 ) ;
2009-01-27 14:36:46 +00:00
const gchar * text = luaL_checkstring ( L , WSLUA_ARG_TextWindow_prepend_TEXT ) ;
2008-04-10 15:26:22 +00:00
2014-02-27 04:42:15 +00:00
if ( ! ops - > prepend_text ) {
2012-03-08 20:00:49 +00:00
WSLUA_ERROR ( TextWindow_prepend , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
}
2012-03-15 02:33:52 +00:00
2008-11-05 15:00:46 +00:00
ops - > prepend_text ( tw - > ws_tw , text ) ;
2008-04-10 15:26:22 +00:00
2014-03-23 15:01:12 +00:00
/* XXX: this is a bad way to do this - should copy the object on to the stack first */
WSLUA_RETURN ( 1 ) ; /* The `TextWindow` object. */
2006-09-25 01:09:00 +00:00
}
2020-03-23 14:58:54 +00:00
WSLUA_METHOD TextWindow_clear ( lua_State * L ) { /* Erases all of the text in the window. */
2006-09-25 01:09:00 +00:00
TextWindow tw = checkTextWindow ( L , 1 ) ;
2008-04-10 15:26:22 +00:00
2014-02-27 04:42:15 +00:00
if ( ! ops - > clear_text ) {
2012-03-08 20:00:49 +00:00
WSLUA_ERROR ( TextWindow_clear , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
}
2012-03-15 02:33:52 +00:00
2008-11-05 15:00:46 +00:00
ops - > clear_text ( tw - > ws_tw ) ;
2008-04-10 15:26:22 +00:00
2014-03-23 15:01:12 +00:00
/* XXX: this is a bad way to do this - should copy the object on to the stack first */
2020-03-23 14:58:54 +00:00
WSLUA_RETURN ( 1 ) ; /* The `TextWindow` object. */
2006-09-25 01:09:00 +00:00
}
2020-03-23 14:58:54 +00:00
WSLUA_METHOD TextWindow_get_text ( lua_State * L ) { /* Get the text of the window. */
2006-09-25 01:09:00 +00:00
TextWindow tw = checkTextWindow ( L , 1 ) ;
2012-03-08 20:00:49 +00:00
const gchar * text ;
2006-09-25 01:09:00 +00:00
2014-02-27 04:42:15 +00:00
if ( ! ops - > get_text ) {
2012-03-08 20:00:49 +00:00
WSLUA_ERROR ( TextWindow_get_text , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
}
2012-03-15 02:33:52 +00:00
2012-03-08 20:00:49 +00:00
text = ops - > get_text ( tw - > ws_tw ) ;
2006-09-25 01:09:00 +00:00
lua_pushstring ( L , text ) ;
2014-03-23 15:01:12 +00:00
WSLUA_RETURN ( 1 ) ; /* The `TextWindow`'s text. */
2006-09-25 01:09:00 +00:00
}
2020-03-23 14:58:54 +00:00
WSLUA_METHOD TextWindow_close ( lua_State * L ) { /* Close the window. */
2015-08-11 12:08:08 +00:00
TextWindow tw = checkTextWindow ( L , 1 ) ;
if ( ! ops - > destroy_text_window ) {
WSLUA_ERROR ( TextWindow_get_text , " GUI not available " ) ;
return 0 ;
}
ops - > destroy_text_window ( tw - > ws_tw ) ;
tw - > ws_tw = NULL ;
return 0 ;
}
2013-02-25 22:05:28 +00:00
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
2010-10-21 12:41:15 +00:00
static int TextWindow__gc ( lua_State * L ) {
2014-02-19 08:22:55 +00:00
TextWindow tw = toTextWindow ( L , 1 ) ;
2006-09-25 01:09:00 +00:00
2012-03-08 20:00:49 +00:00
if ( ! tw )
return 0 ;
2010-10-21 12:41:15 +00:00
2012-03-08 20:00:49 +00:00
if ( ! tw - > expired ) {
tw - > expired = TRUE ;
if ( ops - > destroy_text_window ) {
ops - > destroy_text_window ( tw - > ws_tw ) ;
}
} else {
2019-11-04 09:52:25 +00:00
g_free ( tw - > close_cb_data ) ;
2012-03-08 20:00:49 +00:00
g_free ( tw ) ;
}
2010-10-21 02:50:27 +00:00
2006-10-19 17:02:23 +00:00
return 0 ;
2006-09-25 01:09:00 +00:00
}
2014-03-23 15:01:12 +00:00
WSLUA_METHOD TextWindow_set_editable ( lua_State * L ) { /* Make this text window editable. */
2020-03-23 14:58:54 +00:00
# define WSLUA_OPTARG_TextWindow_set_editable_EDITABLE 2 /* `true` to make the text editable, `false` otherwise. Defaults to `true`. */
2006-09-25 01:09:00 +00:00
2012-03-08 20:00:49 +00:00
TextWindow tw = checkTextWindow ( L , 1 ) ;
gboolean editable = wslua_optbool ( L , WSLUA_OPTARG_TextWindow_set_editable_EDITABLE , TRUE ) ;
2010-10-21 12:41:15 +00:00
2014-02-27 04:42:15 +00:00
if ( ! ops - > set_editable ) {
2012-03-08 20:00:49 +00:00
WSLUA_ERROR ( TextWindow_set_editable , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
}
2012-03-15 02:33:52 +00:00
2014-02-19 08:22:55 +00:00
ops - > set_editable ( tw - > ws_tw , editable ) ;
2008-04-10 15:26:22 +00:00
2014-03-23 15:01:12 +00:00
WSLUA_RETURN ( 1 ) ; /* The `TextWindow` object. */
2006-09-25 01:09:00 +00:00
}
typedef struct _wslua_bt_cb_t {
2012-03-08 20:00:49 +00:00
lua_State * L ;
int func_ref ;
int wslua_tw_ref ;
2006-09-25 01:09:00 +00:00
} wslua_bt_cb_t ;
2010-10-21 12:41:15 +00:00
static gboolean wslua_button_callback ( funnel_text_window_t * ws_tw , void * data ) {
2013-03-16 08:12:29 +00:00
wslua_bt_cb_t * cbd = ( wslua_bt_cb_t * ) data ;
2012-03-08 20:00:49 +00:00
lua_State * L = cbd - > L ;
( void ) ws_tw ; /* ws_tw is unused since we need wslua_tw_ref and it is stored in cbd */
lua_settop ( L , 0 ) ;
lua_pushcfunction ( L , dlg_cb_error_handler ) ;
lua_rawgeti ( L , LUA_REGISTRYINDEX , cbd - > func_ref ) ;
lua_rawgeti ( L , LUA_REGISTRYINDEX , cbd - > wslua_tw_ref ) ;
switch ( lua_pcall ( L , 1 , 0 , 1 ) ) {
case 0 :
break ;
case LUA_ERRRUN :
2021-06-14 23:06:02 +00:00
ws_warning ( " Runtime error while calling button callback " ) ;
2012-03-08 20:00:49 +00:00
break ;
case LUA_ERRMEM :
2021-06-14 23:06:02 +00:00
ws_warning ( " Memory alloc error while calling button callback " ) ;
2012-03-08 20:00:49 +00:00
break ;
2020-10-29 15:01:27 +00:00
case LUA_ERRERR :
2021-06-14 23:06:02 +00:00
ws_warning ( " Error while running the error handler function for button callback " ) ;
2020-10-29 15:01:27 +00:00
break ;
2012-03-08 20:00:49 +00:00
default :
2021-03-20 01:58:26 +00:00
ws_assert_not_reached ( ) ;
2012-03-08 20:00:49 +00:00
break ;
}
return TRUE ;
2010-10-21 12:41:15 +00:00
}
WSLUA_METHOD TextWindow_add_button ( lua_State * L ) {
2020-03-23 14:58:54 +00:00
/* Adds a button with an action handler to the text window. */
# define WSLUA_ARG_TextWindow_add_button_LABEL 2 /* The button label. */
# define WSLUA_ARG_TextWindow_add_button_FUNCTION 3 /* The Lua function to be called when the button is pressed. */
2012-03-08 20:00:49 +00:00
TextWindow tw = checkTextWindow ( L , 1 ) ;
const gchar * label = luaL_checkstring ( L , WSLUA_ARG_TextWindow_add_button_LABEL ) ;
2008-04-10 15:26:22 +00:00
2012-03-08 20:00:49 +00:00
funnel_bt_t * fbt ;
wslua_bt_cb_t * cbd ;
2008-04-10 15:26:22 +00:00
2014-02-27 04:42:15 +00:00
if ( ! ops - > add_button ) {
2012-03-08 20:00:49 +00:00
WSLUA_ERROR ( TextWindow_add_button , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
}
2012-03-15 02:33:52 +00:00
2014-02-27 04:42:15 +00:00
if ( ! lua_isfunction ( L , WSLUA_ARG_TextWindow_add_button_FUNCTION ) ) {
2012-03-08 20:00:49 +00:00
WSLUA_ARG_ERROR ( TextWindow_add_button , FUNCTION , " must be a function " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
}
2006-09-25 01:09:00 +00:00
2012-03-08 20:00:49 +00:00
lua_settop ( L , 3 ) ;
2006-09-25 01:09:00 +00:00
2012-03-08 20:00:49 +00:00
if ( ops - > add_button ) {
2020-12-21 02:30:28 +00:00
fbt = g_new ( funnel_bt_t , 1 ) ;
cbd = g_new ( wslua_bt_cb_t , 1 ) ;
2008-04-10 15:26:22 +00:00
2012-03-08 20:00:49 +00:00
fbt - > tw = tw - > ws_tw ;
fbt - > func = wslua_button_callback ;
fbt - > data = cbd ;
fbt - > free_fcn = g_free ;
fbt - > free_data_fcn = g_free ;
2006-09-25 01:09:00 +00:00
2012-03-08 20:00:49 +00:00
cbd - > L = L ;
cbd - > func_ref = luaL_ref ( L , LUA_REGISTRYINDEX ) ;
cbd - > wslua_tw_ref = luaL_ref ( L , LUA_REGISTRYINDEX ) ;
2008-04-10 15:26:22 +00:00
2012-03-08 20:00:49 +00:00
ops - > add_button ( tw - > ws_tw , fbt , label ) ;
}
2010-10-21 02:50:27 +00:00
2014-03-23 15:01:12 +00:00
WSLUA_RETURN ( 1 ) ; /* The `TextWindow` object. */
2006-09-25 01:09:00 +00:00
}
WSLUA_METHODS TextWindow_methods [ ] = {
2012-03-08 20:00:49 +00:00
WSLUA_CLASS_FNREG ( TextWindow , new ) ,
WSLUA_CLASS_FNREG ( TextWindow , set ) ,
WSLUA_CLASS_FNREG ( TextWindow , append ) ,
WSLUA_CLASS_FNREG ( TextWindow , prepend ) ,
WSLUA_CLASS_FNREG ( TextWindow , clear ) ,
WSLUA_CLASS_FNREG ( TextWindow , set_atclose ) ,
WSLUA_CLASS_FNREG ( TextWindow , set_editable ) ,
WSLUA_CLASS_FNREG ( TextWindow , get_text ) ,
WSLUA_CLASS_FNREG ( TextWindow , add_button ) ,
2015-08-11 12:08:08 +00:00
WSLUA_CLASS_FNREG ( TextWindow , close ) ,
2014-02-19 08:22:55 +00:00
{ NULL , NULL }
2006-09-25 01:09:00 +00:00
} ;
WSLUA_META TextWindow_meta [ ] = {
{ " __tostring " , TextWindow_get_text } ,
2014-02-19 08:22:55 +00:00
{ NULL , NULL }
2006-09-25 01:09:00 +00:00
} ;
2010-10-21 12:41:15 +00:00
int TextWindow_register ( lua_State * L ) {
2006-09-25 01:09:00 +00:00
ops = funnel_get_funnel_ops ( ) ;
2008-04-10 15:26:22 +00:00
2012-03-08 20:00:49 +00:00
WSLUA_REGISTER_CLASS ( TextWindow ) ;
2008-04-10 15:26:22 +00:00
2014-01-31 07:25:42 +00:00
return 0 ;
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_FUNCTION wslua_retap_packets ( lua_State * L ) {
2012-03-08 20:00:49 +00:00
/*
2020-03-23 14:58:54 +00:00
Rescans all packets and runs each < < lua_class_Listener , tap listener > > without reconstructing the display .
2012-03-08 20:00:49 +00:00
*/
if ( ops - > retap_packets ) {
2015-07-03 20:52:49 +00:00
ops - > retap_packets ( ops - > ops_id ) ;
2012-03-08 20:00:49 +00:00
} else {
WSLUA_ERROR ( wslua_retap_packets , " GUI not available " ) ;
}
2008-04-10 15:26:22 +00:00
2012-03-08 20:00:49 +00:00
return 0 ;
2006-09-25 01:09:00 +00:00
}
2020-03-23 14:58:54 +00:00
WSLUA_FUNCTION wslua_copy_to_clipboard ( lua_State * L ) { /* Copy a string into the clipboard. Requires a GUI. */
2006-10-03 12:07:10 +00:00
# define WSLUA_ARG_copy_to_clipboard_TEXT 1 /* The string to be copied into the clipboard. */
2012-03-08 20:00:49 +00:00
const char * copied_str = luaL_checkstring ( L , WSLUA_ARG_copy_to_clipboard_TEXT ) ;
GString * gstr ;
if ( ! ops - > copy_to_clipboard ) {
WSLUA_ERROR ( copy_to_clipboard , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2012-03-08 20:00:49 +00:00
}
2006-10-03 12:07:10 +00:00
2012-03-08 20:00:49 +00:00
gstr = g_string_new ( copied_str ) ;
2006-10-03 12:07:10 +00:00
2012-03-08 20:00:49 +00:00
ops - > copy_to_clipboard ( gstr ) ;
2006-10-03 12:07:10 +00:00
2012-03-08 20:00:49 +00:00
g_string_free ( gstr , TRUE ) ;
2006-10-03 12:07:10 +00:00
2012-03-08 20:00:49 +00:00
return 0 ;
2006-10-03 12:07:10 +00:00
}
2020-03-23 14:58:54 +00:00
WSLUA_FUNCTION wslua_open_capture_file ( lua_State * L ) { /* Open and display a capture file. Requires a GUI. */
2006-10-03 12:07:10 +00:00
# define WSLUA_ARG_open_capture_file_FILENAME 1 /* The name of the file to be opened. */
2020-10-03 02:17:00 +00:00
# define WSLUA_ARG_open_capture_file_FILTER 2 /* The https://gitlab.com/wireshark/wireshark/-/wikis/DisplayFilters[display filter] to be applied once the file is opened. */
2008-04-10 15:26:22 +00:00
2012-03-08 20:00:49 +00:00
const char * fname = luaL_checkstring ( L , WSLUA_ARG_open_capture_file_FILENAME ) ;
const char * filter = luaL_optstring ( L , WSLUA_ARG_open_capture_file_FILTER , NULL ) ;
2015-01-18 10:22:19 +00:00
char * error = NULL ;
2006-10-03 12:07:10 +00:00
2012-03-08 20:00:49 +00:00
if ( ! ops - > open_file ) {
WSLUA_ERROR ( open_capture_file , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2012-03-08 20:00:49 +00:00
}
2008-04-10 15:26:22 +00:00
2015-07-03 20:52:49 +00:00
if ( ! ops - > open_file ( ops - > ops_id , fname , filter , & error ) ) {
2012-03-08 20:00:49 +00:00
lua_pushboolean ( L , FALSE ) ;
2010-10-21 12:41:15 +00:00
2015-01-18 10:22:19 +00:00
if ( error ) {
2012-03-08 20:00:49 +00:00
lua_pushstring ( L , error ) ;
2015-01-18 10:22:19 +00:00
g_free ( error ) ;
} else
2012-03-08 20:00:49 +00:00
lua_pushnil ( L ) ;
2010-10-21 12:41:15 +00:00
2012-03-08 20:00:49 +00:00
return 2 ;
} else {
lua_pushboolean ( L , TRUE ) ;
return 1 ;
}
2006-10-03 12:07:10 +00:00
}
2014-03-23 15:01:12 +00:00
WSLUA_FUNCTION wslua_get_filter ( lua_State * L ) { /* Get the main filter text. */
2012-09-15 13:58:57 +00:00
const char * filter_str = NULL ;
if ( ! ops - > get_filter ) {
WSLUA_ERROR ( get_filter , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2012-09-15 13:58:57 +00:00
}
2015-07-03 20:52:49 +00:00
filter_str = ops - > get_filter ( ops - > ops_id ) ;
2012-09-15 13:58:57 +00:00
lua_pushstring ( L , filter_str ) ;
return 1 ;
}
2014-03-23 15:01:12 +00:00
WSLUA_FUNCTION wslua_set_filter ( lua_State * L ) { /* Set the main filter text. */
2006-10-03 12:07:10 +00:00
# define WSLUA_ARG_set_filter_TEXT 1 /* The filter's text. */
2012-03-08 20:00:49 +00:00
const char * filter_str = luaL_checkstring ( L , WSLUA_ARG_set_filter_TEXT ) ;
2006-10-03 12:07:10 +00:00
2012-03-08 20:00:49 +00:00
if ( ! ops - > set_filter ) {
WSLUA_ERROR ( set_filter , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2012-03-08 20:00:49 +00:00
}
2006-10-03 12:07:10 +00:00
2015-07-03 20:52:49 +00:00
ops - > set_filter ( ops - > ops_id , filter_str ) ;
2006-10-03 12:07:10 +00:00
2012-03-08 20:00:49 +00:00
return 0 ;
2006-10-03 12:07:10 +00:00
}
2020-06-18 18:43:20 +00:00
WSLUA_FUNCTION wslua_get_color_filter_slot ( lua_State * L ) { /*
2020-10-03 02:17:00 +00:00
Gets the current https : //gitlab.com/wireshark/wireshark/-/wikis/ColoringRules[packet coloring rule] (by index) for the
2020-06-18 18:43:20 +00:00
current session . Wireshark reserves 10 slots for these coloring rules . Requires a GUI .
*/
# define WSLUA_ARG_get_color_filter_slot_ROW 1 / *
The index ( 1 - 10 ) of the desired color filter value in the temporary coloring rules list .
. Default background colors
[ cols = " 3 " , options = " header " ]
| = = =
| Index | RGB ( hex ) | Color
| 1 | ffc0c0 | { set : cellbgcolor : # ffc0c0 } pink 1
| 2 | ffc0ff | { set : cellbgcolor : # ffc0ff } pink 2
| 3 | e0c0e0 | { set : cellbgcolor : # e0c0e0 } purple 1
| 4 | c0c0ff | { set : cellbgcolor : # c0c0ff } purple 2
| 5 | c0e0e0 | { set : cellbgcolor : # c0e0e0 } green 1
| 6 | c0ffff | { set : cellbgcolor : # c0ffff } green 2
| 7 | c0ffc0 | { set : cellbgcolor : # c0ffc0 } green 3
| 8 | ffffc0 | { set : cellbgcolor : # ffffc0 } yellow 1
| 9 | e0e0c0 | { set : cellbgcolor : # e0e0c0 } yellow 2
| 10 | e0e0e0 | { set : cellbgcolor : # e0e0e0 } gray
| = = =
*/
guint8 row = ( guint8 ) luaL_checkinteger ( L , WSLUA_ARG_get_color_filter_slot_ROW ) ;
gchar * filter_str = NULL ;
if ( ! ops - > get_color_filter_slot ) {
WSLUA_ERROR ( get_color_filter_slot , " GUI not available " ) ;
return 0 ;
}
filter_str = ops - > get_color_filter_slot ( row ) ;
if ( filter_str = = NULL ) {
lua_pushnil ( L ) ;
} else {
lua_pushstring ( L , filter_str ) ;
g_free ( filter_str ) ;
}
return 1 ;
}
2020-03-23 14:58:54 +00:00
WSLUA_FUNCTION wslua_set_color_filter_slot ( lua_State * L ) { /*
2020-10-03 02:17:00 +00:00
Sets a https : //gitlab.com/wireshark/wireshark/-/wikis/ColoringRules[packet coloring rule] (by index) for the current session.
2020-03-23 14:58:54 +00:00
Wireshark reserves 10 slots for these coloring rules .
Requires a GUI .
*/
# define WSLUA_ARG_set_color_filter_slot_ROW 1 / *
2020-06-18 18:43:20 +00:00
The index ( 1 - 10 ) of the desired color in the temporary coloring rules list .
2020-03-23 14:58:54 +00:00
The default foreground is black and the default backgrounds are listed below .
// XXX We need get the colors working, e.g. by adding them to a stylesheet.
. Default background colors
[ cols = " 3 " , options = " header " ]
| = = =
| Index | RGB ( hex ) | Color
2020-06-18 18:43:20 +00:00
| 1 | ffc0c0 | { set : cellbgcolor : # ffc0c0 } pink 1
| 2 | ffc0ff | { set : cellbgcolor : # ffc0ff } pink 2
| 3 | e0c0e0 | { set : cellbgcolor : # e0c0e0 } purple 1
| 4 | c0c0ff | { set : cellbgcolor : # c0c0ff } purple 2
| 5 | c0e0e0 | { set : cellbgcolor : # c0e0e0 } green 1
| 6 | c0ffff | { set : cellbgcolor : # c0ffff } green 2
| 7 | c0ffc0 | { set : cellbgcolor : # c0ffc0 } green 3
| 8 | ffffc0 | { set : cellbgcolor : # ffffc0 } yellow 1
| 9 | e0e0c0 | { set : cellbgcolor : # e0e0c0 } yellow 2
| 10 | e0e0e0 | { set : cellbgcolor : # e0e0e0 } gray
2020-03-23 14:58:54 +00:00
| = = =
The color list can be set from the command line using two unofficial preferences : ` gui . colorized_frame . bg ` and ` gui . colorized_frame . fg ` , which require 10 hex RGB codes ( 6 hex digits each ) , e . g .
- - - -
wireshark - o gui . colorized_frame . bg : $ { RGB0 } , $ { RGB1 } , $ { RGB2 } , $ { RGB3 } , $ { RGB4 } , $ { RGB5 } , $ { RGB6 } , $ { RGB7 } , $ { RGB8 } , $ { RGB9 }
- - - -
For example , this command yields the same results as the table above ( and with all foregrounds set to black ) :
- - - -
wireshark - o gui . colorized_frame . bg : ffc0c0 , ffc0ff , e0c0e0 , c0c0ff , c0e0e0 , c0ffff , c0ffc0 , ffffc0 , e0e0c0 , e0e0e0 - o gui . colorized_frame . fg : 000000 , 000000 , 000000 , 000000 , 000000 , 000000 , 000000 , 000000
- - - -
*/
2020-10-03 02:17:00 +00:00
# define WSLUA_ARG_set_color_filter_slot_TEXT 2 / * The https: //gitlab.com/wireshark/wireshark/-/wikis/DisplayFilters[display filter] for selecting packets to be colorized
2020-03-23 14:58:54 +00:00
. */
2015-01-22 21:04:15 +00:00
guint8 row = ( guint8 ) luaL_checkinteger ( L , WSLUA_ARG_set_color_filter_slot_ROW ) ;
2012-03-08 20:00:49 +00:00
const gchar * filter_str = luaL_checkstring ( L , WSLUA_ARG_set_color_filter_slot_TEXT ) ;
2011-06-27 11:07:31 +00:00
2012-03-08 20:00:49 +00:00
if ( ! ops - > set_color_filter_slot ) {
WSLUA_ERROR ( set_color_filter_slot , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2012-03-08 20:00:49 +00:00
}
2011-06-27 11:07:31 +00:00
2012-03-08 20:00:49 +00:00
ops - > set_color_filter_slot ( row , filter_str ) ;
2011-06-27 11:07:31 +00:00
2012-03-08 20:00:49 +00:00
return 0 ;
2011-06-27 11:07:31 +00:00
}
2020-03-23 14:58:54 +00:00
WSLUA_FUNCTION wslua_apply_filter ( lua_State * L ) { /*
Apply the filter in the main filter box .
Requires a GUI .
[ WARNING ]
= = = =
Avoid calling this from within a dissector function or else an infinite loop can occur if it causes the dissector to be called again .
This function is best used in a button callback ( from a dialog or text window ) or menu callback .
= = = =
*/
2012-03-08 20:00:49 +00:00
if ( ! ops - > apply_filter ) {
WSLUA_ERROR ( apply_filter , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2012-03-08 20:00:49 +00:00
}
2006-10-03 14:27:45 +00:00
2015-07-03 20:52:49 +00:00
ops - > apply_filter ( ops - > ops_id ) ;
2006-10-03 14:27:45 +00:00
2012-03-08 20:00:49 +00:00
return 0 ;
2006-10-03 14:27:45 +00:00
}
2020-03-23 14:58:54 +00:00
WSLUA_FUNCTION wslua_reload ( lua_State * L ) { /* Reload the current capture file. Deprecated. Use reload_packets() instead. */
2006-10-03 14:27:45 +00:00
2016-01-03 12:48:39 +00:00
if ( ! ops - > reload_packets ) {
2012-03-08 20:00:49 +00:00
WSLUA_ERROR ( reload , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2012-03-08 20:00:49 +00:00
}
2006-10-03 14:27:45 +00:00
2016-01-03 12:48:39 +00:00
ops - > reload_packets ( ops - > ops_id ) ;
return 0 ;
}
2020-03-23 14:58:54 +00:00
WSLUA_FUNCTION wslua_reload_packets ( lua_State * L ) { /*
Reload the current capture file .
Requires a GUI .
[ WARNING ]
= = = =
Avoid calling this from within a dissector function or else an infinite loop can occur if it causes the dissector to be called again .
This function is best used in a button callback ( from a dialog or text window ) or menu callback .
= = = =
*/
2016-01-03 12:48:39 +00:00
if ( ! ops - > reload_packets ) {
WSLUA_ERROR ( reload , " GUI not available " ) ;
return 0 ;
}
ops - > reload_packets ( ops - > ops_id ) ;
return 0 ;
}
WSLUA_FUNCTION wslua_reload_lua_plugins ( lua_State * L ) { /* Reload all Lua plugins. */
if ( ! ops - > reload_lua_plugins ) {
WSLUA_ERROR ( reload_lua_plugins , " GUI not available " ) ;
return 0 ;
}
ops - > reload_lua_plugins ( ops - > ops_id ) ;
2010-10-21 12:41:15 +00:00
2012-03-08 20:00:49 +00:00
return 0 ;
2006-10-03 14:27:45 +00:00
}
2006-12-27 01:46:42 +00:00
2021-02-12 19:54:54 +00:00
WSLUA_FUNCTION wslua_browser_open_url ( lua_State * L ) { /*
Opens an URL in a web browser . Requires a GUI .
[ WARNING ]
= = = =
Do not pass an untrusted URL to this function .
It will be passed to the system ' s URL handler , which might execute malicious code , switch on your Bluetooth - connected foghorn , or any of a number of unexpected or harmful things .
= = = =
*/
2006-12-27 01:46:42 +00:00
# define WSLUA_ARG_browser_open_url_URL 1 /* The url. */
2012-03-08 20:00:49 +00:00
const char * url = luaL_checkstring ( L , WSLUA_ARG_browser_open_url_URL ) ;
2006-12-27 01:46:42 +00:00
2012-03-08 20:00:49 +00:00
if ( ! ops - > browser_open_url ) {
WSLUA_ERROR ( browser_open_url , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2012-03-08 20:00:49 +00:00
}
2006-12-27 01:46:42 +00:00
2012-03-08 20:00:49 +00:00
ops - > browser_open_url ( url ) ;
2006-12-27 01:46:42 +00:00
2012-03-08 20:00:49 +00:00
return 0 ;
2006-12-27 01:46:42 +00:00
}
2020-03-23 14:58:54 +00:00
WSLUA_FUNCTION wslua_browser_open_data_file ( lua_State * L ) { /*
Open a file located in the data directory ( specified in the Wireshark preferences ) in the web browser .
If the file does not exist , the function silently ignores the request .
Requires a GUI .
2021-02-12 19:54:54 +00:00
[ WARNING ]
= = = =
Do not pass an untrusted URL to this function .
It will be passed to the system ' s URL handler , which might execute malicious code , switch on your Bluetooth - connected foghorn , or any of a number of unexpected or harmful things .
= = = =
2020-03-23 14:58:54 +00:00
*/
2014-03-23 15:01:12 +00:00
# define WSLUA_ARG_browser_open_data_file_FILENAME 1 /* The file name. */
2012-03-08 20:00:49 +00:00
const char * file = luaL_checkstring ( L , WSLUA_ARG_browser_open_data_file_FILENAME ) ;
2006-12-27 01:46:42 +00:00
2012-03-08 20:00:49 +00:00
if ( ! ops - > browser_open_data_file ) {
WSLUA_ERROR ( browser_open_data_file , " GUI not available " ) ;
2014-02-27 04:42:15 +00:00
return 0 ;
2012-03-08 20:00:49 +00:00
}
2006-12-27 01:46:42 +00:00
2012-03-08 20:00:49 +00:00
ops - > browser_open_data_file ( file ) ;
2006-12-27 01:46:42 +00:00
2012-03-08 20:00:49 +00:00
return 0 ;
2006-12-27 01:46:42 +00:00
}
2015-02-13 18:25:19 +00:00
/*
2019-07-26 18:43:17 +00:00
* Editor modelines - https : //www.wireshark.org/tools/modelines.html
2015-02-13 18:25:19 +00:00
*
* Local variables :
* c - basic - offset : 4
* tab - width : 8
* indent - tabs - mode : nil
* End :
*
* vi : set shiftwidth = 4 tabstop = 8 expandtab :
* : indentSize = 4 : tabSize = 8 : noTabs = true :
*/