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-10-18 17:59:49 +00:00
* $ Id $
2006-09-25 01:09:00 +00:00
*
* Wireshark - Network traffic analyzer
* By Gerald Combs < gerald @ wireshark . org >
* Copyright 1998 Gerald Combs
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation ; either version 2
* of the License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
*/
2011-04-21 13:13:39 +00:00
# ifdef HAVE_CONFIG_H
# include "config.h"
# endif
2006-09-25 01:09:00 +00:00
# include "wslua.h"
2006-10-24 13:16:57 +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 ) ;
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
}
2010-10-21 12:41:15 +00:00
WSLUA_FUNCTION wslua_gui_enabled ( lua_State * L ) { /* Checks whether the GUI facility is enabled. */
2007-01-21 17:05:33 +00:00
lua_pushboolean ( L , GPOINTER_TO_INT ( ops & & ops - > add_button ) ) ;
2006-09-25 01:09:00 +00:00
WSLUA_RETURN ( 1 ) ; /* A boolean: true if it is enabled, false if it isn't. */
}
2010-10-21 12:41:15 +00:00
static void lua_menu_callback ( gpointer data ) {
2006-09-25 01:09:00 +00:00
struct _lua_menu_data * md = data ;
2010-10-21 12:41:15 +00:00
lua_State * L = md - > L ;
2008-04-10 15:26:22 +00:00
2010-10-21 12:41:15 +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
2010-10-21 12:41:15 +00:00
switch ( lua_pcall ( L , 0 , 0 , 1 ) ) {
case 0 :
break ;
case LUA_ERRRUN :
g_warning ( " Runtime error while calling menu callback " ) ;
break ;
case LUA_ERRMEM :
g_warning ( " Memory alloc error while calling menu callback " ) ;
break ;
default :
g_assert_not_reached ( ) ;
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 ;
}
2010-10-21 12:41:15 +00:00
WSLUA_FUNCTION wslua_register_menu ( lua_State * L ) { /* Register a menu item in one of the main menus. */
# define WSLUA_ARG_register_menu_NAME 1 /* The name of the menu item. The submenus are to be separated by '/'s. (string) */
# define WSLUA_ARG_register_menu_ACTION 2 /* The function to be called when the menu item is invoked. (function taking no arguments and returning nothing) */
# define WSLUA_OPTARG_register_menu_GROUP 3 /* The menu group into which the menu item is to be inserted. If omitted, defaults to MENU_STAT_GENERIC. One of MENU_STAT_UNSORTED (Statistics), MENU_STAT_GENERIC (Statistics, first section), MENU_STAT_CONVERSATION (Statistics/Conversation List), MENU_STAT_ENDPOINT (Statistics/Endpoint List), MENU_STAT_RESPONSE (Statistics/Service Response Time), MENU_STAT_TELEPHONY (Telephony), MENU_ANALYZE (Analyze), MENU_ANALYZE_CONVERSATION (Analyze/Conversation Filter), MENU_TOOLS_UNSORTED (Tools). (number) */
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 ;
2010-10-21 12:41:15 +00:00
register_stat_group_t group = ( int ) luaL_optnumber ( L , WSLUA_OPTARG_register_menu_GROUP , REGISTER_STAT_GROUP_GENERIC ) ;
2006-09-27 17:12:42 +00:00
2010-10-21 12:41:15 +00:00
if ( group > REGISTER_TOOLS_GROUP_UNSORTED )
WSLUA_OPTARG_ERROR ( register_menu , GROUP , " Must be a defined MENU_* (see init.lua) " ) ;
2006-09-27 17:12:42 +00:00
2010-10-21 12:41:15 +00:00
if ( ! name )
WSLUA_ARG_ERROR ( register_menu , NAME , " Must be a string " ) ;
2008-04-10 15:26:22 +00:00
if ( ! lua_isfunction ( L , WSLUA_ARG_register_menu_ACTION ) )
2010-10-21 12:41:15 +00:00
WSLUA_ARG_ERROR ( register_menu , ACTION , " Must be a function " ) ;
2006-09-27 17:12:42 +00:00
2006-09-25 01:09:00 +00:00
md = g_malloc ( sizeof ( struct _lua_menu_data ) ) ;
md - > L = L ;
2006-09-27 17:12:42 +00:00
2010-10-21 12:41:15 +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 ,
retap ) ;
2006-09-25 01:09:00 +00:00
WSLUA_RETURN ( 0 ) ;
}
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 ) ;
report_failure ( " Lua: Error During execution of dialog callback: \n %s " , error ) ;
return 0 ;
}
2010-10-21 12:41:15 +00:00
static void lua_dialog_cb ( gchar * * user_input , void * data ) {
2006-09-25 01:09:00 +00:00
struct _dlg_cb_data * dcbd = data ;
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 :
g_warning ( " Runtime error while calling dialog callback " ) ;
break ;
case LUA_ERRMEM :
g_warning ( " Memory alloc error while calling dialog callback " ) ;
break ;
default :
g_assert_not_reached ( ) ;
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 ) ;
report_failure ( " Lua: Error During execution of TextWindow close callback: \n %s " , error ) ;
return 0 ;
}
2010-10-21 12:41:15 +00:00
static void text_win_close_cb ( void * data ) {
2008-11-05 15:00:46 +00:00
struct _close_cb_data * cbd = data ;
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 :
g_warning ( " Runtime error during execution of TextWindow close callback " ) ;
break ;
case LUA_ERRMEM :
g_warning ( " Memory alloc error during execution of TextWindow close callback " ) ;
break ;
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 ) ;
} else {
cbd - > wslua_tw - > expired = TRUE ;
}
}
2010-10-21 12:41:15 +00:00
WSLUA_FUNCTION wslua_new_dialog ( lua_State * L ) { /* Pops up a new dialog */
2006-09-25 01:09:00 +00:00
# define WSLUA_ARG_new_dialog_TITLE 1 /* Title of the dialog's window. */
# define WSLUA_ARG_new_dialog_ACTION 2 /* Action to be performed when OKd. */
/* WSLUA_MOREARGS new_dialog A series of strings to be used as labels of the dialog's fields */
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
2006-09-25 01:09:00 +00:00
if ( ! ( title = luaL_checkstring ( L , WSLUA_ARG_new_dialog_TITLE ) ) ) {
2009-05-08 16:40:38 +00:00
WSLUA_ARG_ERROR ( new_dialog , TITLE , " Must be a string " ) ;
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 ( ! 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 " ) ;
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 " ) ;
2006-09-25 01:09:00 +00:00
}
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
dcbd = g_malloc ( sizeof ( struct _dlg_cb_data ) ) ;
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
2006-09-25 01:09:00 +00:00
labels = g_ptr_array_new ( ) ;
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 + + ) {
gchar * label = ( void * ) luaL_checkstring ( L , i ) ;
2008-04-10 15:26:22 +00:00
2010-10-21 12:41:15 +00:00
/* XXX leaks labels on error */
if ( ! label )
WSLUA_ERROR ( new_dialog , " All fields must be strings " ) ;
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
g_ptr_array_add ( labels , label ) ;
}
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
2006-09-25 01:09:00 +00:00
ops - > new_dialog ( title , ( const gchar * * ) labels - > pdata , lua_dialog_cb , dcbd ) ;
2008-04-10 15:26:22 +00:00
2006-10-19 17:02:23 +00:00
g_ptr_array_free ( labels , FALSE ) ;
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
WSLUA_RETURN ( 0 ) ;
}
2008-08-11 01:33:31 +00:00
WSLUA_CLASS_DEFINE ( ProgDlg , NOP , NOP ) ; /* Manages a progress bar dialog. */
2010-10-21 12:41:15 +00:00
WSLUA_CONSTRUCTOR ProgDlg_new ( lua_State * L ) { /* Creates a new TextWindow. */
2008-08-11 01:33:31 +00:00
# define WSLUA_OPTARG_ProgDlg_new_TITLE 2 /* Title of the new window, defaults to "Progress". */
# define WSLUA_OPTARG_ProgDlg_new_TASK 3 /* Current task, defaults to "". */
ProgDlg pd = 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 ) {
pd - > pw = ops - > new_progress_window ( pd - > title , pd - > task , TRUE , & ( pd - > stopped ) ) ;
}
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
2010-10-21 12:41:15 +00:00
WSLUA_RETURN ( 1 ) ; /* The newly created TextWindow object. */
2008-08-11 01:33:31 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD ProgDlg_update ( lua_State * L ) { /* Appends text */
2008-08-11 01:33:31 +00:00
# define WSLUA_ARG_ProgDlg_update_PROGRESS 2 /* Part done ( e.g. 0.75 ). */
# define WSLUA_OPTARG_ProgDlg_update_TASK 3 /* Current task, defaults to "". */
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
2008-08-11 01:33:31 +00:00
g_free ( pd - > task ) ;
pd - > task = g_strdup ( task ) ;
2011-05-09 12:49:39 +00:00
/* XXX, dead code: pd already dereferenced. should it be: !pd->task?
2010-10-21 12:41:15 +00:00
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
2008-08-11 01:33:31 +00:00
if ( pr > = 0.0 | | pr < = 1.0 ) {
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) " ) ;
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 ;
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD ProgDlg_stopped ( lua_State * L ) { /* Checks wheher the user has pressed the stop button. */
2008-08-11 01:33:31 +00:00
ProgDlg pd = checkProgDlg ( L , 1 ) ;
2010-10-21 02:50:27 +00:00
2010-10-21 12:41:15 +00:00
if ( ! pd ) {
WSLUA_ERROR ( ProgDlg_stopped , " Cannot be called for something not a ProgDlg " ) ;
2008-08-11 01:33:31 +00:00
}
2010-10-21 02:50:27 +00:00
2008-08-11 01:33:31 +00:00
lua_pushboolean ( L , pd - > stopped ) ;
2010-10-21 12:41:15 +00:00
WSLUA_RETURN ( 1 ) ; /* true if the user has asked to stop the progress. */
2010-10-21 02:50:27 +00:00
}
2008-08-11 01:33:31 +00:00
2010-10-21 12:41:15 +00:00
WSLUA_METHOD ProgDlg_close ( lua_State * L ) { /* Appends text */
2008-08-11 01:33:31 +00:00
ProgDlg pd = checkProgDlg ( L , 1 ) ;
2010-10-21 02:50:27 +00:00
2010-10-21 12:41:15 +00:00
if ( ! pd ) {
WSLUA_ERROR ( ProgDlg_update , " Cannot be called for something not a ProgDlg " ) ;
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
2008-08-11 01:33:31 +00:00
if ( pd ) {
lua_pushstring ( L , ep_strdup_printf ( " %sstopped " , pd - > stopped ? " " : " not " ) ) ;
} else {
luaL_error ( L , " ProgDlg__tostring has being passed something else! " ) ;
}
2010-10-21 02:50:27 +00:00
2008-08-11 01:33:31 +00:00
return 0 ;
}
2010-10-21 12:41:15 +00:00
static int ProgDlg__gc ( lua_State * L ) {
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
if ( pd ) {
if ( pd - > pw ) ops - > destroy_progress_window ( pd - > pw ) ;
2010-10-21 02:50:27 +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 [ ] = {
{ " new " , ProgDlg_new } ,
{ " update " , ProgDlg_update } ,
{ " stopped " , ProgDlg_stopped } ,
{ " close " , ProgDlg_close } ,
{ 0 , 0 }
} ;
WSLUA_META ProgDlg_meta [ ] = {
{ " __tostring " , ProgDlg__tostring } ,
{ " __gc " , ProgDlg__gc } ,
{ 0 , 0 }
} ;
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
2010-10-21 12:41:15 +00:00
WSLUA_REGISTER_CLASS ( ProgDlg ) ;
2010-10-21 02:50:27 +00:00
2008-08-11 01:33:31 +00:00
return 1 ;
}
2006-09-25 01:09:00 +00:00
WSLUA_CLASS_DEFINE ( TextWindow , NOP , NOP ) ; /* Manages a text window. */
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
2010-10-21 12:41:15 +00:00
WSLUA_CONSTRUCTOR TextWindow_new ( lua_State * L ) { /* Creates a new TextWindow. */
2006-09-25 01:09:00 +00:00
# define WSLUA_OPTARG_TextWindow_new_TITLE 1 /* Title of the new 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
2008-11-05 15:00:46 +00:00
title = luaL_optstring ( L , WSLUA_OPTARG_TextWindow_new_TITLE , " Untitled Window " ) ;
tw = g_malloc ( sizeof ( struct _wslua_tw ) ) ;
tw - > expired = FALSE ;
tw - > ws_tw = ops - > new_text_window ( title ) ;
2010-10-21 02:50:27 +00:00
2008-11-05 15:00:46 +00:00
default_cbd = g_malloc ( sizeof ( struct _close_cb_data ) ) ;
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
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
2010-10-21 12:41:15 +00:00
WSLUA_RETURN ( 1 ) ; /* The newly created TextWindow object. */
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD TextWindow_set_atclose ( lua_State * L ) { /* Set the function that will be called when the window closes */
2006-09-25 01:09:00 +00:00
# define WSLUA_ARG_TextWindow_at_close_ACTION 2 /* A function to be executed when the user closes the 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 ;
2010-10-21 12:41:15 +00:00
if ( ! tw )
WSLUA_ERROR ( TextWindow_at_close , " Cannot be called for something not a TextWindow " ) ;
2006-09-25 01:09:00 +00:00
2006-10-19 17:02:23 +00:00
lua_settop ( L , 2 ) ;
2006-09-25 01:09:00 +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 " ) ;
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
cbd = g_malloc ( sizeof ( struct _close_cb_data ) ) ;
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
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
2010-10-21 12:41:15 +00:00
WSLUA_RETURN ( 1 ) ; /* The TextWindow object. */
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD TextWindow_set ( lua_State * L ) { /* Sets the text. */
2006-09-25 01:09:00 +00:00
# define WSLUA_ARG_TextWindow_set_TEXT 2 /* The text to be used. */
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
2010-10-21 12:41:15 +00:00
if ( ! tw )
WSLUA_ERROR ( TextWindow_set , " Cannot be called for something not a TextWindow " ) ;
2006-09-25 01:09:00 +00:00
2008-11-05 15:00:46 +00:00
if ( tw - > expired )
2010-10-21 12:41:15 +00:00
WSLUA_ARG_ERROR ( TextWindow_set , TEXT , " Expired TextWindow " ) ;
2010-10-21 02:50:27 +00:00
2006-09-25 01:09:00 +00:00
if ( ! text )
2010-10-21 12:41:15 +00:00
WSLUA_ARG_ERROR ( TextWindow_set , TEXT , " Must be a string " ) ;
2008-04-10 15:26:22 +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
2010-10-21 12:41:15 +00:00
WSLUA_RETURN ( 1 ) ; /* The TextWindow object. */
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD TextWindow_append ( lua_State * L ) { /* Appends text */
2008-04-10 15:26:22 +00:00
# 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
2010-10-21 12:41:15 +00:00
if ( ! tw )
WSLUA_ERROR ( TextWindow_append , " Cannot be called for something not a TextWindow " ) ;
2006-09-25 01:09:00 +00:00
2008-11-05 15:00:46 +00:00
if ( tw - > expired )
2010-10-21 12:41:15 +00:00
WSLUA_ARG_ERROR ( TextWindow_set , TEXT , " Expired TextWindow " ) ;
2010-10-21 02:50:27 +00:00
2010-10-21 12:41:15 +00:00
if ( ! text )
WSLUA_ARG_ERROR ( TextWindow_append , TEXT , " Must be a string " ) ;
2006-09-25 01:09:00 +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
2010-10-21 12:41:15 +00:00
WSLUA_RETURN ( 1 ) ; /* The TextWindow object. */
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD TextWindow_prepend ( lua_State * L ) { /* Prepends text */
2008-04-10 15:26:22 +00:00
# define WSLUA_ARG_TextWindow_prepend_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_prepend_TEXT ) ;
2008-04-10 15:26:22 +00:00
2010-10-21 12:41:15 +00:00
if ( ! tw )
WSLUA_ERROR ( TextWindow_prepend , " Cannot be called for something not a TextWindow " ) ;
2006-09-25 01:09:00 +00:00
2008-11-05 15:00:46 +00:00
if ( tw - > expired )
2010-10-21 12:41:15 +00:00
WSLUA_ARG_ERROR ( TextWindow_set , TEXT , " Expired TextWindow " ) ;
2010-10-21 02:50:27 +00:00
2010-10-21 12:41:15 +00:00
if ( ! text )
WSLUA_ARG_ERROR ( TextWindow_prepend , TEXT , " Must be a string " ) ;
2008-04-10 15:26:22 +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
2010-10-21 12:41:15 +00:00
WSLUA_RETURN ( 1 ) ; /* The TextWindow object. */
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD TextWindow_clear ( lua_State * L ) { /* Erases all 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
2010-10-21 12:41:15 +00:00
if ( ! tw )
WSLUA_ERROR ( TextWindow_clear , " Cannot be called for something not a TextWindow " ) ;
2006-09-25 01:09:00 +00:00
2008-11-05 15:00:46 +00:00
if ( tw - > expired )
2010-10-21 12:41:15 +00:00
WSLUA_ARG_ERROR ( TextWindow_set , TEXT , " Expired TextWindow " ) ;
2010-10-21 02:50:27 +00:00
2008-11-05 15:00:46 +00:00
ops - > clear_text ( tw - > ws_tw ) ;
2008-04-10 15:26:22 +00:00
2010-10-21 12:41:15 +00:00
WSLUA_RETURN ( 1 ) ; /* The TextWindow object. */
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +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 ) ;
2010-10-21 12:41:15 +00:00
const gchar * text ;
2006-09-25 01:09:00 +00:00
2010-10-21 12:41:15 +00:00
if ( ! tw )
WSLUA_ERROR ( TextWindow_get_text , " Cannot be called for something not a TextWindow " ) ;
2006-09-25 01:09:00 +00:00
2008-11-05 15:00:46 +00:00
if ( tw - > expired )
2010-10-21 12:41:15 +00:00
WSLUA_ARG_ERROR ( TextWindow_set , TEXT , " Expired TextWindow " ) ;
2010-10-21 02:50:27 +00:00
2010-10-21 12:41:15 +00:00
text = ops - > get_text ( tw - > ws_tw ) ;
2006-09-25 01:09:00 +00:00
lua_pushstring ( L , text ) ;
2010-10-21 12:41:15 +00:00
WSLUA_RETURN ( 1 ) ; /* The TextWindow's text. */
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
static int TextWindow__gc ( lua_State * L ) {
2006-09-25 01:09:00 +00:00
TextWindow tw = checkTextWindow ( L , 1 ) ;
2010-10-21 12:41:15 +00:00
if ( ! tw )
return 0 ;
if ( ! tw - > expired ) {
tw - > expired = TRUE ;
ops - > destroy_text_window ( tw - > ws_tw ) ;
} else {
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
}
2010-10-21 12:41:15 +00:00
WSLUA_METHOD TextWindow_set_editable ( lua_State * L ) { /* Make this window editable */
2006-10-17 18:20:44 +00:00
# define WSLUA_OPTARG_TextWindow_set_editable_EDITABLE 2 /* A boolean flag, defaults to true */
2006-09-25 01:09:00 +00:00
2010-10-21 12:41:15 +00:00
TextWindow tw = checkTextWindow ( L , 1 ) ;
gboolean editable = wslua_optbool ( L , WSLUA_OPTARG_TextWindow_set_editable_EDITABLE , TRUE ) ;
if ( ! tw )
WSLUA_ERROR ( TextWindow_set_editable , " Cannot be called for something not a TextWindow " ) ;
2006-09-25 01:09:00 +00:00
2008-11-05 15:00:46 +00:00
if ( tw - > expired )
2010-10-21 12:41:15 +00:00
WSLUA_ARG_ERROR ( TextWindow_set , TEXT , " Expired TextWindow " ) ;
2010-10-21 02:50:27 +00:00
2010-10-21 12:41:15 +00:00
if ( ops - > set_editable )
ops - > set_editable ( tw - > ws_tw , editable ) ;
2008-04-10 15:26:22 +00:00
2010-10-21 12:41:15 +00:00
WSLUA_RETURN ( 1 ) ; /* The TextWindow object. */
2006-09-25 01:09:00 +00:00
}
typedef struct _wslua_bt_cb_t {
lua_State * L ;
int func_ref ;
2008-11-05 15:00:46 +00:00
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 ) {
wslua_bt_cb_t * cbd = data ;
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 :
g_warning ( " Runtime error while calling button callback " ) ;
break ;
case LUA_ERRMEM :
g_warning ( " Memory alloc error while calling button callback " ) ;
break ;
default :
g_assert_not_reached ( ) ;
break ;
}
return TRUE ;
}
WSLUA_METHOD TextWindow_add_button ( lua_State * L ) {
2008-04-10 15:26:22 +00:00
# define WSLUA_ARG_TextWindow_add_button_LABEL 2 /* The label of the button */
# define WSLUA_ARG_TextWindow_add_button_FUNCTION 3 /* The function to be called when clicked */
2010-10-21 12:41:15 +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
2010-10-21 12:41:15 +00:00
funnel_bt_t * fbt ;
wslua_bt_cb_t * cbd ;
2008-04-10 15:26:22 +00:00
2010-10-21 12:41:15 +00:00
if ( ! tw )
WSLUA_ERROR ( TextWindow_add_button , " Cannot be called for something not a TextWindow " ) ;
2008-04-10 15:26:22 +00:00
2008-11-05 15:00:46 +00:00
if ( tw - > expired )
2010-10-21 12:41:15 +00:00
WSLUA_ARG_ERROR ( TextWindow_set , TEXT , " Expired TextWindow " ) ;
2006-09-25 01:09:00 +00:00
2010-10-21 12:41:15 +00:00
if ( ! lua_isfunction ( L , WSLUA_ARG_TextWindow_add_button_FUNCTION ) )
WSLUA_ARG_ERROR ( TextWindow_add_button , FUNCTION , " must be a function " ) ;
2006-09-25 01:09:00 +00:00
2010-10-21 12:41:15 +00:00
lua_settop ( L , 3 ) ;
2006-09-25 01:09:00 +00:00
2010-10-21 12:41:15 +00:00
if ( ops - > add_button ) {
fbt = g_malloc ( sizeof ( funnel_bt_t ) ) ;
cbd = g_malloc ( sizeof ( wslua_bt_cb_t ) ) ;
2008-04-10 15:26:22 +00:00
2010-10-21 12:41:15 +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
2010-10-21 12:41:15 +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
2010-10-21 12:41:15 +00:00
ops - > add_button ( tw - > ws_tw , fbt , label ) ;
}
2010-10-21 02:50:27 +00:00
2010-10-21 12:41:15 +00:00
WSLUA_RETURN ( 1 ) ; /* The TextWindow object. */
2006-09-25 01:09:00 +00:00
}
WSLUA_METHODS TextWindow_methods [ ] = {
WSLUA_CLASS_FNREG ( TextWindow , new ) ,
WSLUA_CLASS_FNREG ( TextWindow , set ) ,
WSLUA_CLASS_FNREG ( TextWindow , new ) ,
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 ) ,
2010-10-21 12:41:15 +00:00
{ 0 , 0 }
2006-09-25 01:09:00 +00:00
} ;
WSLUA_META TextWindow_meta [ ] = {
{ " __tostring " , TextWindow_get_text } ,
{ " __gc " , TextWindow__gc } ,
{ 0 , 0 }
} ;
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
2010-10-21 12:41:15 +00:00
WSLUA_REGISTER_CLASS ( TextWindow ) ;
2008-04-10 15:26:22 +00:00
2006-09-25 01:09:00 +00:00
return 1 ;
}
2010-10-21 12:41:15 +00:00
WSLUA_FUNCTION wslua_retap_packets ( lua_State * L ) {
/*
Rescan all packets and just run taps - don ' t reconstruct the display .
*/
if ( ops - > retap_packets ) {
ops - > retap_packets ( ) ;
} else {
WSLUA_ERROR ( wslua_retap_packets , " Does not work on TShark " ) ;
}
2008-04-10 15:26:22 +00:00
2010-10-21 12:41:15 +00:00
return 0 ;
2006-09-25 01:09:00 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_FUNCTION wslua_copy_to_clipboard ( lua_State * L ) { /* Copy a string into the clipboard */
2006-10-03 12:07:10 +00:00
# define WSLUA_ARG_copy_to_clipboard_TEXT 1 /* The string to be copied into the clipboard. */
2010-10-21 12:41:15 +00:00
const char * copied_str = luaL_checkstring ( L , WSLUA_ARG_copy_to_clipboard_TEXT ) ;
GString * gstr ;
if ( ! ops - > copy_to_clipboard ) {
WSLUA_ERROR ( wslua_copy_to_clipboard , " Does not work on TShark " ) ;
}
2006-10-03 12:07:10 +00:00
2010-10-21 12:41:15 +00:00
if ( ! copied_str ) {
WSLUA_ARG_ERROR ( copy_to_clipboard , TEXT , " Must be a string " ) ;
}
2006-10-03 12:07:10 +00:00
2010-10-21 12:41:15 +00:00
gstr = g_string_new ( copied_str ) ;
2006-10-03 12:07:10 +00:00
2010-10-21 12:41:15 +00:00
ops - > copy_to_clipboard ( gstr ) ;
2006-10-03 12:07:10 +00:00
2010-10-21 12:41:15 +00:00
g_string_free ( gstr , TRUE ) ;
2006-10-03 12:07:10 +00:00
2010-10-21 12:41:15 +00:00
return 0 ;
2006-10-03 12:07:10 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_FUNCTION wslua_open_capture_file ( lua_State * L ) { /* Open and display a capture file */
2006-10-03 12:07:10 +00:00
# define WSLUA_ARG_open_capture_file_FILENAME 1 /* The name of the file to be opened. */
2008-04-10 15:26:22 +00:00
# define WSLUA_ARG_open_capture_file_FILTER 2 /* A filter to be applied as the file gets opened. */
2010-10-21 12:41:15 +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 ) ;
const char * error = NULL ;
2006-10-03 12:07:10 +00:00
2010-10-21 12:41:15 +00:00
if ( ! ops - > open_file ) {
WSLUA_ERROR ( wslua_open_capture_file , " Does not work on TShark " ) ;
}
2008-04-10 15:26:22 +00:00
2010-10-21 12:41:15 +00:00
if ( ! fname ) {
WSLUA_ARG_ERROR ( open_capture_file , FILENAME , " Must be a string " ) ;
}
2008-04-10 15:26:22 +00:00
2010-10-21 12:41:15 +00:00
if ( ! ops - > open_file ( fname , filter , & error ) ) {
lua_pushboolean ( L , FALSE ) ;
if ( error )
lua_pushstring ( L , error ) ;
else
lua_pushnil ( L ) ;
return 2 ;
} else {
lua_pushboolean ( L , TRUE ) ;
return 1 ;
}
2006-10-03 12:07:10 +00:00
}
2010-10-21 12:41:15 +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. */
2010-10-21 12:41:15 +00:00
const char * filter_str = luaL_checkstring ( L , WSLUA_ARG_set_filter_TEXT ) ;
2006-10-03 12:07:10 +00:00
2010-10-21 12:41:15 +00:00
if ( ! ops - > set_filter ) {
WSLUA_ERROR ( wslua_set_filter , " Does not work on TShark " ) ;
}
2006-10-03 12:07:10 +00:00
2010-10-21 12:41:15 +00:00
if ( ! filter_str ) {
WSLUA_ARG_ERROR ( set_filter , TEXT , " Must be a string " ) ;
}
2006-10-03 12:07:10 +00:00
2010-10-21 12:41:15 +00:00
ops - > set_filter ( filter_str ) ;
2006-10-03 12:07:10 +00:00
2010-10-21 12:41:15 +00:00
return 0 ;
2006-10-03 12:07:10 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_FUNCTION wslua_apply_filter ( lua_State * L ) { /* Apply the filter in the main filter box */
if ( ! ops - > apply_filter ) {
WSLUA_ERROR ( wslua_apply_filter , " Does not work on TShark " ) ;
}
2006-10-03 14:27:45 +00:00
2010-10-21 12:41:15 +00:00
ops - > apply_filter ( ) ;
2006-10-03 14:27:45 +00:00
2010-10-21 12:41:15 +00:00
return 0 ;
2006-10-03 14:27:45 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_FUNCTION wslua_reload ( lua_State * L ) { /* Reload the current capture file */
2006-10-03 14:27:45 +00:00
2010-10-21 12:41:15 +00:00
if ( ! ops - > reload ) {
WSLUA_ERROR ( wslua_reload , " Does not work on TShark " ) ;
}
2006-10-03 14:27:45 +00:00
2010-10-21 12:41:15 +00:00
ops - > reload ( ) ;
return 0 ;
2006-10-03 14:27:45 +00:00
}
2006-12-27 01:46:42 +00:00
2010-10-21 12:41:15 +00:00
WSLUA_FUNCTION wslua_browser_open_url ( lua_State * L ) { /* Open an url in a browser */
2006-12-27 01:46:42 +00:00
# define WSLUA_ARG_browser_open_url_URL 1 /* The url. */
2010-10-21 12:41:15 +00:00
const char * url = luaL_checkstring ( L , WSLUA_ARG_browser_open_url_URL ) ;
2006-12-27 01:46:42 +00:00
2010-10-21 12:41:15 +00:00
if ( ! ops - > browser_open_url ) {
WSLUA_ERROR ( browser_open_url , " Does not work on TShark " ) ;
}
2006-12-27 01:46:42 +00:00
2010-10-21 12:41:15 +00:00
if ( ! url ) {
WSLUA_ARG_ERROR ( browser_open_url , URL , " Must be a string " ) ;
}
2006-12-27 01:46:42 +00:00
2010-10-21 12:41:15 +00:00
ops - > browser_open_url ( url ) ;
2006-12-27 01:46:42 +00:00
2010-10-21 12:41:15 +00:00
return 0 ;
2006-12-27 01:46:42 +00:00
}
2010-10-21 12:41:15 +00:00
WSLUA_FUNCTION wslua_browser_open_data_file ( lua_State * L ) { /* Open an file in a browser */
2006-12-27 01:46:42 +00:00
# define WSLUA_ARG_browser_open_data_file_FILENAME 1 /* The url. */
2010-10-21 12:41:15 +00:00
const char * file = luaL_checkstring ( L , WSLUA_ARG_browser_open_data_file_FILENAME ) ;
2006-12-27 01:46:42 +00:00
2010-10-21 12:41:15 +00:00
if ( ! ops - > browser_open_data_file ) {
WSLUA_ERROR ( browser_open_data_file , " Does not work on TShark " ) ;
}
2006-12-27 01:46:42 +00:00
2010-10-21 12:41:15 +00:00
if ( ! file ) {
WSLUA_ARG_ERROR ( browser_open_data_file , FILENAME , " Must be a string " ) ;
}
2006-12-27 01:46:42 +00:00
2010-10-21 12:41:15 +00:00
ops - > browser_open_data_file ( file ) ;
2006-12-27 01:46:42 +00:00
2010-10-21 12:41:15 +00:00
return 0 ;
2006-12-27 01:46:42 +00:00
}
2010-10-21 12:41:15 +00:00