HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / wslua / wslua_gui.c
blobd556a5f9626258d56fe0b9845aaaa64794c36996
1 /*
2 * wslua_gui.c
4 * (c) 2006, Luis E. Garcia Ontanon <luis@ontanon.org>
6 * $Id$
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "config.h"
29 #include <epan/emem.h>
31 #include "wslua.h"
33 /* WSLUA_MODULE Gui GUI support */
35 static const funnel_ops_t* ops = NULL;
37 struct _lua_menu_data {
38 lua_State* L;
39 int cb_ref;
42 static int menu_cb_error_handler(lua_State* L) {
43 const gchar* error = lua_tostring(L,1);
44 report_failure("Lua: Error During execution of Menu Callback:\n %s",error);
45 return 0;
48 WSLUA_FUNCTION wslua_gui_enabled(lua_State* L) { /* Checks whether the GUI facility is enabled. */
49 lua_pushboolean(L,GPOINTER_TO_INT(ops && ops->add_button));
50 WSLUA_RETURN(1); /* A boolean: true if it is enabled, false if it isn't. */
53 static void lua_menu_callback(gpointer data) {
54 struct _lua_menu_data* md = (struct _lua_menu_data *)data;
55 lua_State* L = md->L;
57 lua_settop(L,0);
58 lua_pushcfunction(L,menu_cb_error_handler);
59 lua_rawgeti(L, LUA_REGISTRYINDEX, md->cb_ref);
61 switch ( lua_pcall(L,0,0,1) ) {
62 case 0:
63 break;
64 case LUA_ERRRUN:
65 g_warning("Runtime error while calling menu callback");
66 break;
67 case LUA_ERRMEM:
68 g_warning("Memory alloc error while calling menu callback");
69 break;
70 default:
71 g_assert_not_reached();
72 break;
75 return;
78 WSLUA_FUNCTION wslua_register_menu(lua_State* L) { /* Register a menu item in one of the main menus. */
79 #define WSLUA_ARG_register_menu_NAME 1 /* The name of the menu item. The submenus are to be separated by '/'s. (string) */
80 #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) */
81 #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:
82 MENU_STAT_UNSORTED (Statistics),
83 MENU_STAT_GENERIC (Statistics, first section),
84 MENU_STAT_CONVERSATION (Statistics/Conversation List),
85 MENU_STAT_ENDPOINT (Statistics/Endpoint List),
86 MENU_STAT_RESPONSE (Statistics/Service Response Time),
87 MENU_STAT_TELEPHONY (Telephony), MENU_ANALYZE (Analyze),
88 MENU_ANALYZE_CONVERSATION (Analyze/Conversation Filter),
89 MENU_TOOLS_UNSORTED (Tools). (number) */
91 const gchar* name = luaL_checkstring(L,WSLUA_ARG_register_menu_NAME);
92 struct _lua_menu_data* md;
93 gboolean retap = FALSE;
94 register_stat_group_t group = (register_stat_group_t)luaL_optnumber(L,WSLUA_OPTARG_register_menu_GROUP,REGISTER_STAT_GROUP_GENERIC);
96 if ( group > REGISTER_TOOLS_GROUP_UNSORTED)
97 WSLUA_OPTARG_ERROR(register_menu,GROUP,"Must be a defined MENU_* (see init.lua)");
99 if(!name)
100 WSLUA_ARG_ERROR(register_menu,NAME,"Must be a string");
102 if (!lua_isfunction(L,WSLUA_ARG_register_menu_ACTION))
103 WSLUA_ARG_ERROR(register_menu,ACTION,"Must be a function");
105 md = (struct _lua_menu_data *)g_malloc(sizeof(struct _lua_menu_data));
106 md->L = L;
108 lua_pushvalue(L, 2);
109 md->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
110 lua_remove(L,2);
112 funnel_register_menu(name,
113 group,
114 lua_menu_callback,
116 retap);
118 WSLUA_RETURN(0);
124 struct _dlg_cb_data {
125 lua_State* L;
126 int func_ref;
129 static int dlg_cb_error_handler(lua_State* L) {
130 const gchar* error = lua_tostring(L,1);
131 report_failure("Lua: Error During execution of dialog callback:\n %s",error);
132 return 0;
135 static void lua_dialog_cb(gchar** user_input, void* data) {
136 struct _dlg_cb_data* dcbd = (struct _dlg_cb_data *)data;
137 int i = 0;
138 gchar* input;
139 lua_State* L = dcbd->L;
141 lua_settop(L,0);
142 lua_pushcfunction(L,dlg_cb_error_handler);
143 lua_rawgeti(L, LUA_REGISTRYINDEX, dcbd->func_ref);
145 for (i = 0; (input = user_input[i]) ; i++) {
146 lua_pushstring(L,input);
147 g_free(input);
150 g_free(user_input);
152 switch ( lua_pcall(L,i,0,1) ) {
153 case 0:
154 break;
155 case LUA_ERRRUN:
156 g_warning("Runtime error while calling dialog callback");
157 break;
158 case LUA_ERRMEM:
159 g_warning("Memory alloc error while calling dialog callback");
160 break;
161 default:
162 g_assert_not_reached();
163 break;
168 struct _close_cb_data {
169 lua_State* L;
170 int func_ref;
171 TextWindow wslua_tw;
175 static int text_win_close_cb_error_handler(lua_State* L) {
176 const gchar* error = lua_tostring(L,1);
177 report_failure("Lua: Error During execution of TextWindow close callback:\n %s",error);
178 return 0;
181 static void text_win_close_cb(void* data) {
182 struct _close_cb_data* cbd = (struct _close_cb_data *)data;
183 lua_State* L = cbd->L;
185 if (cbd->L) { /* close function is set */
187 lua_settop(L,0);
188 lua_pushcfunction(L,text_win_close_cb_error_handler);
189 lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->func_ref);
191 switch ( lua_pcall(L,0,0,1) ) {
192 case 0:
193 break;
194 case LUA_ERRRUN:
195 g_warning("Runtime error during execution of TextWindow close callback");
196 break;
197 case LUA_ERRMEM:
198 g_warning("Memory alloc error during execution of TextWindow close callback");
199 break;
200 default:
201 break;
205 if (cbd->wslua_tw->expired) {
206 g_free(cbd->wslua_tw);
207 } else {
208 cbd->wslua_tw->expired = TRUE;
213 WSLUA_FUNCTION wslua_new_dialog(lua_State* L) { /* Pops up a new dialog */
214 #define WSLUA_ARG_new_dialog_TITLE 1 /* Title of the dialog's window. */
215 #define WSLUA_ARG_new_dialog_ACTION 2 /* Action to be performed when OKd. */
216 /* WSLUA_MOREARGS new_dialog A series of strings to be used as labels of the dialog's fields */
218 const gchar* title;
219 int top = lua_gettop(L);
220 int i;
221 GPtrArray* labels;
222 struct _dlg_cb_data* dcbd;
224 if (! ops) {
225 luaL_error(L,"the GUI facility has to be enabled");
226 return 0;
229 if (!ops->new_dialog) {
230 WSLUA_ERROR(new_dialog,"GUI not available");
233 if (! (title = luaL_checkstring(L,WSLUA_ARG_new_dialog_TITLE)) ) {
234 WSLUA_ARG_ERROR(new_dialog,TITLE,"Must be a string");
237 if (! lua_isfunction(L,WSLUA_ARG_new_dialog_ACTION)) {
238 WSLUA_ARG_ERROR(new_dialog,ACTION,"Must be a function");
241 if (top < 3) {
242 WSLUA_ERROR(new_dialog,"At least one field required");
246 dcbd = (struct _dlg_cb_data *)g_malloc(sizeof(struct _dlg_cb_data));
247 dcbd->L = L;
249 lua_remove(L,1);
251 lua_pushvalue(L, 1);
252 dcbd->func_ref = luaL_ref(L, LUA_REGISTRYINDEX);
253 lua_remove(L,1);
255 labels = g_ptr_array_new();
257 top -= 2;
259 for (i = 1; i <= top; i++) {
260 gchar* label = (gchar *)luaL_checkstring(L,i);
262 /* XXX leaks labels on error */
263 if (! label)
264 WSLUA_ERROR(new_dialog,"All fields must be strings");
266 g_ptr_array_add(labels,label);
269 g_ptr_array_add(labels,NULL);
271 ops->new_dialog(title, (const gchar**)labels->pdata, lua_dialog_cb, dcbd);
273 g_ptr_array_free(labels,FALSE);
275 WSLUA_RETURN(0);
280 WSLUA_CLASS_DEFINE(ProgDlg,NOP,NOP); /* Manages a progress bar dialog. */
282 WSLUA_CONSTRUCTOR ProgDlg_new(lua_State* L) { /* Creates a new TextWindow. */
283 #define WSLUA_OPTARG_ProgDlg_new_TITLE 2 /* Title of the new window, defaults to "Progress". */
284 #define WSLUA_OPTARG_ProgDlg_new_TASK 3 /* Current task, defaults to "". */
285 ProgDlg pd = (ProgDlg)g_malloc(sizeof(struct _wslua_progdlg));
286 pd->title = g_strdup(luaL_optstring(L,WSLUA_OPTARG_ProgDlg_new_TITLE,"Progress"));
287 pd->task = g_strdup(luaL_optstring(L,WSLUA_OPTARG_ProgDlg_new_TASK,""));
288 pd->stopped = FALSE;
290 if (ops->new_progress_window) {
291 pd->pw = ops->new_progress_window(pd->title,pd->task,TRUE,&(pd->stopped));
292 } else {
293 WSLUA_ERROR(ProgDlg_new, "GUI not available");
296 pushProgDlg(L,pd);
298 WSLUA_RETURN(1); /* The newly created TextWindow object. */
301 WSLUA_METHOD ProgDlg_update(lua_State* L) { /* Appends text */
302 #define WSLUA_ARG_ProgDlg_update_PROGRESS 2 /* Part done ( e.g. 0.75 ). */
303 #define WSLUA_OPTARG_ProgDlg_update_TASK 3 /* Current task, defaults to "". */
304 ProgDlg pd = checkProgDlg(L,1);
305 double pr = lua_tonumber(L,WSLUA_ARG_ProgDlg_update_PROGRESS);
306 const gchar* task = luaL_optstring(L,WSLUA_OPTARG_ProgDlg_update_TASK,"");
308 if (!ops->update_progress) {
309 WSLUA_ERROR(ProgDlg_update,"GUI not available");
312 g_free(pd->task);
313 pd->task = g_strdup(task);
315 /* XXX, dead code: pd already dereferenced. should it be: !pd->task?
316 if (!pd) {
317 WSLUA_ERROR(ProgDlg_update,"Cannot be called for something not a ProgDlg");
318 } */
320 if (pr >= 0.0 && pr <= 1.0) {
321 ops->update_progress(pd->pw, (float) pr, task);
322 } else {
323 WSLUA_ERROR(ProgDlg_update,"Progress value out of range (must be between 0.0 and 1.0)");
326 return 0;
329 WSLUA_METHOD ProgDlg_stopped(lua_State* L) { /* Checks wheher the user has pressed the stop button. */
330 ProgDlg pd = checkProgDlg(L,1);
332 if (!pd) {
333 WSLUA_ERROR(ProgDlg_stopped,"Cannot be called for something not a ProgDlg");
336 lua_pushboolean(L,pd->stopped);
338 WSLUA_RETURN(1); /* true if the user has asked to stop the progress. */
343 WSLUA_METHOD ProgDlg_close(lua_State* L) { /* Appends text */
344 ProgDlg pd = checkProgDlg(L,1);
346 if (!ops->destroy_progress_window) {
347 WSLUA_ERROR(ProgDlg_close,"GUI not available");
350 if (!pd) {
351 WSLUA_ERROR(ProgDlg_update,"Cannot be called for something not a ProgDlg");
354 if (pd->pw) {
355 ops->destroy_progress_window(pd->pw);
356 pd->pw = NULL;
358 return 0;
362 static int ProgDlg__tostring(lua_State* L) {
363 ProgDlg pd = checkProgDlg(L,1);
365 if (pd) {
366 lua_pushstring(L,ep_strdup_printf("%sstopped",pd->stopped?"":"not "));
367 } else {
368 luaL_error(L, "ProgDlg__tostring has being passed something else!");
371 return 0;
374 /* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
375 static int ProgDlg__gc(lua_State* L) {
376 ProgDlg pd = checkProgDlg(L,1);
378 if (pd) {
379 if (pd->pw && ops->destroy_progress_window) {
380 ops->destroy_progress_window(pd->pw);
383 g_free(pd);
384 } else {
385 luaL_error(L, "ProgDlg__gc has being passed something else!");
388 return 0;
392 WSLUA_METHODS ProgDlg_methods[] = {
393 WSLUA_CLASS_FNREG(ProgDlg,new),
394 WSLUA_CLASS_FNREG(ProgDlg,update),
395 WSLUA_CLASS_FNREG(ProgDlg,stopped),
396 WSLUA_CLASS_FNREG(ProgDlg,close),
397 {0, 0}
400 WSLUA_META ProgDlg_meta[] = {
401 {"__tostring", ProgDlg__tostring},
402 {0, 0}
405 int ProgDlg_register(lua_State* L) {
407 ops = funnel_get_funnel_ops();
409 WSLUA_REGISTER_CLASS(ProgDlg);
411 return 1;
416 WSLUA_CLASS_DEFINE(TextWindow,NOP,NOP); /* Manages a text window. */
418 /* XXX: button and close callback data is being leaked */
419 /* XXX: lua callback function and TextWindow are not garbage collected because
420 they stay in LUA_REGISTRYINDEX forever */
422 WSLUA_CONSTRUCTOR TextWindow_new(lua_State* L) { /* Creates a new TextWindow. */
423 #define WSLUA_OPTARG_TextWindow_new_TITLE 1 /* Title of the new window. */
425 const gchar* title;
426 TextWindow tw = NULL;
427 struct _close_cb_data* default_cbd;
429 if (!ops->new_text_window || !ops->set_close_cb) {
430 WSLUA_ERROR(TextWindow_new,"GUI not available");
433 title = luaL_optstring(L,WSLUA_OPTARG_TextWindow_new_TITLE,"Untitled Window");
434 tw = (struct _wslua_tw *)g_malloc(sizeof(struct _wslua_tw));
435 tw->expired = FALSE;
436 tw->ws_tw = ops->new_text_window(title);
438 default_cbd = (struct _close_cb_data *)g_malloc(sizeof(struct _close_cb_data));
440 default_cbd->L = NULL;
441 default_cbd->func_ref = 0;
442 default_cbd->wslua_tw = tw;
444 ops->set_close_cb(tw->ws_tw,text_win_close_cb,default_cbd);
446 pushTextWindow(L,tw);
448 WSLUA_RETURN(1); /* The newly created TextWindow object. */
451 WSLUA_METHOD TextWindow_set_atclose(lua_State* L) { /* Set the function that will be called when the window closes */
452 #define WSLUA_ARG_TextWindow_at_close_ACTION 2 /* A function to be executed when the user closes the window */
454 TextWindow tw = checkTextWindow(L,1);
455 struct _close_cb_data* cbd;
457 if (!ops->set_close_cb) {
458 WSLUA_ERROR(TextWindow_set_atclose,"GUI not available");
461 if (!tw)
462 WSLUA_ERROR(TextWindow_at_close,"Cannot be called for something not a TextWindow");
464 lua_settop(L,2);
466 if (! lua_isfunction(L,2))
467 WSLUA_ARG_ERROR(TextWindow_at_close,ACTION,"Must be a function");
469 cbd = (struct _close_cb_data *)g_malloc(sizeof(struct _close_cb_data));
471 cbd->L = L;
472 cbd->func_ref = luaL_ref(L, LUA_REGISTRYINDEX);
473 cbd->wslua_tw = tw;
475 ops->set_close_cb(tw->ws_tw,text_win_close_cb,cbd);
477 WSLUA_RETURN(1); /* The TextWindow object. */
480 WSLUA_METHOD TextWindow_set(lua_State* L) { /* Sets the text. */
481 #define WSLUA_ARG_TextWindow_set_TEXT 2 /* The text to be used. */
483 TextWindow tw = checkTextWindow(L,1);
484 const gchar* text = luaL_checkstring(L,WSLUA_ARG_TextWindow_set_TEXT);
486 if (!ops->set_text)
487 WSLUA_ERROR(TextWindow_set,"GUI not available");
489 if (!tw)
490 WSLUA_ERROR(TextWindow_set,"Cannot be called for something not a TextWindow");
492 if (tw->expired)
493 WSLUA_ERROR(TextWindow_set,"Expired TextWindow");
495 if (!text)
496 WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Must be a string");
498 ops->set_text(tw->ws_tw,text);
500 WSLUA_RETURN(1); /* The TextWindow object. */
503 WSLUA_METHOD TextWindow_append(lua_State* L) { /* Appends text */
504 #define WSLUA_ARG_TextWindow_append_TEXT 2 /* The text to be appended */
505 TextWindow tw = checkTextWindow(L,1);
506 const gchar* text = luaL_checkstring(L,WSLUA_ARG_TextWindow_append_TEXT);
508 if (!ops->append_text)
509 WSLUA_ERROR(TextWindow_append,"GUI not available");
511 if (!tw)
512 WSLUA_ERROR(TextWindow_append,"Cannot be called for something not a TextWindow");
514 if (tw->expired)
515 WSLUA_ERROR(TextWindow_append,"Expired TextWindow");
517 if (!text)
518 WSLUA_ARG_ERROR(TextWindow_append,TEXT,"Must be a string");
520 ops->append_text(tw->ws_tw,text);
522 WSLUA_RETURN(1); /* The TextWindow object. */
525 WSLUA_METHOD TextWindow_prepend(lua_State* L) { /* Prepends text */
526 #define WSLUA_ARG_TextWindow_prepend_TEXT 2 /* The text to be appended */
527 TextWindow tw = checkTextWindow(L,1);
528 const gchar* text = luaL_checkstring(L,WSLUA_ARG_TextWindow_prepend_TEXT);
530 if (!ops->prepend_text)
531 WSLUA_ERROR(TextWindow_prepend,"GUI not available");
533 if (!tw)
534 WSLUA_ERROR(TextWindow_prepend,"Cannot be called for something not a TextWindow");
536 if (tw->expired)
537 WSLUA_ERROR(TextWindow_prepend,"Expired TextWindow");
539 if (!text)
540 WSLUA_ARG_ERROR(TextWindow_prepend,TEXT,"Must be a string");
542 ops->prepend_text(tw->ws_tw,text);
544 WSLUA_RETURN(1); /* The TextWindow object. */
547 WSLUA_METHOD TextWindow_clear(lua_State* L) { /* Erases all text in the window. */
548 TextWindow tw = checkTextWindow(L,1);
550 if (!ops->clear_text)
551 WSLUA_ERROR(TextWindow_clear,"GUI not available");
553 if (!tw)
554 WSLUA_ERROR(TextWindow_clear,"Cannot be called for something not a TextWindow");
556 if (tw->expired)
557 WSLUA_ERROR(TextWindow_clear,"Expired TextWindow");
559 ops->clear_text(tw->ws_tw);
561 WSLUA_RETURN(1); /* The TextWindow object. */
564 WSLUA_METHOD TextWindow_get_text(lua_State* L) { /* Get the text of the window */
565 TextWindow tw = checkTextWindow(L,1);
566 const gchar* text;
568 if (!ops->get_text)
569 WSLUA_ERROR(TextWindow_get_text,"GUI not available");
571 if (!tw)
572 WSLUA_ERROR(TextWindow_get_text,"Cannot be called for something not a TextWindow");
574 if (tw->expired)
575 WSLUA_ERROR(TextWindow_get_text,"Expired TextWindow");
577 text = ops->get_text(tw->ws_tw);
579 lua_pushstring(L,text);
580 WSLUA_RETURN(1); /* The TextWindow's text. */
583 /* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
584 static int TextWindow__gc(lua_State* L) {
585 TextWindow tw = checkTextWindow(L,1);
587 if (!tw)
588 return 0;
590 if (!tw->expired) {
591 tw->expired = TRUE;
592 if (ops->destroy_text_window) {
593 ops->destroy_text_window(tw->ws_tw);
595 } else {
596 g_free(tw);
600 return 0;
603 WSLUA_METHOD TextWindow_set_editable(lua_State* L) { /* Make this window editable */
604 #define WSLUA_OPTARG_TextWindow_set_editable_EDITABLE 2 /* A boolean flag, defaults to true */
606 TextWindow tw = checkTextWindow(L,1);
607 gboolean editable = wslua_optbool(L,WSLUA_OPTARG_TextWindow_set_editable_EDITABLE,TRUE);
609 if (!ops->set_editable)
610 WSLUA_ERROR(TextWindow_set_editable,"GUI not available");
612 if (!tw)
613 WSLUA_ERROR(TextWindow_set_editable,"Cannot be called for something not a TextWindow");
615 if (tw->expired)
616 WSLUA_ERROR(TextWindow_set_editable,"Expired TextWindow");
618 if (ops->set_editable)
619 ops->set_editable(tw->ws_tw,editable);
621 WSLUA_RETURN(1); /* The TextWindow object. */
624 typedef struct _wslua_bt_cb_t {
625 lua_State* L;
626 int func_ref;
627 int wslua_tw_ref;
628 } wslua_bt_cb_t;
630 static gboolean wslua_button_callback(funnel_text_window_t* ws_tw, void* data) {
631 wslua_bt_cb_t* cbd = (wslua_bt_cb_t *)data;
632 lua_State* L = cbd->L;
633 (void) ws_tw; /* ws_tw is unused since we need wslua_tw_ref and it is stored in cbd */
635 lua_settop(L,0);
636 lua_pushcfunction(L,dlg_cb_error_handler);
637 lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->func_ref);
638 lua_rawgeti(L, LUA_REGISTRYINDEX, cbd->wslua_tw_ref);
640 switch ( lua_pcall(L,1,0,1) ) {
641 case 0:
642 break;
643 case LUA_ERRRUN:
644 g_warning("Runtime error while calling button callback");
645 break;
646 case LUA_ERRMEM:
647 g_warning("Memory alloc error while calling button callback");
648 break;
649 default:
650 g_assert_not_reached();
651 break;
654 return TRUE;
657 WSLUA_METHOD TextWindow_add_button(lua_State* L) {
658 #define WSLUA_ARG_TextWindow_add_button_LABEL 2 /* The label of the button */
659 #define WSLUA_ARG_TextWindow_add_button_FUNCTION 3 /* The function to be called when clicked */
660 TextWindow tw = checkTextWindow(L,1);
661 const gchar* label = luaL_checkstring(L,WSLUA_ARG_TextWindow_add_button_LABEL);
663 funnel_bt_t* fbt;
664 wslua_bt_cb_t* cbd;
666 if (!ops->add_button)
667 WSLUA_ERROR(TextWindow_add_button,"GUI not available");
669 if (!tw)
670 WSLUA_ERROR(TextWindow_add_button,"Cannot be called for something not a TextWindow");
672 if (tw->expired)
673 WSLUA_ERROR(TextWindow_add_button,"Expired TextWindow");
675 if (! lua_isfunction(L,WSLUA_ARG_TextWindow_add_button_FUNCTION) )
676 WSLUA_ARG_ERROR(TextWindow_add_button,FUNCTION,"must be a function");
678 lua_settop(L,3);
680 if (ops->add_button) {
681 fbt = (funnel_bt_t *)g_malloc(sizeof(funnel_bt_t));
682 cbd = (wslua_bt_cb_t *)g_malloc(sizeof(wslua_bt_cb_t));
684 fbt->tw = tw->ws_tw;
685 fbt->func = wslua_button_callback;
686 fbt->data = cbd;
687 fbt->free_fcn = g_free;
688 fbt->free_data_fcn = g_free;
690 cbd->L = L;
691 cbd->func_ref = luaL_ref(L, LUA_REGISTRYINDEX);
692 cbd->wslua_tw_ref = luaL_ref(L, LUA_REGISTRYINDEX);
694 ops->add_button(tw->ws_tw,fbt,label);
697 WSLUA_RETURN(1); /* The TextWindow object. */
700 WSLUA_METHODS TextWindow_methods[] = {
701 WSLUA_CLASS_FNREG(TextWindow,new),
702 WSLUA_CLASS_FNREG(TextWindow,set),
703 WSLUA_CLASS_FNREG(TextWindow,new),
704 WSLUA_CLASS_FNREG(TextWindow,append),
705 WSLUA_CLASS_FNREG(TextWindow,prepend),
706 WSLUA_CLASS_FNREG(TextWindow,clear),
707 WSLUA_CLASS_FNREG(TextWindow,set_atclose),
708 WSLUA_CLASS_FNREG(TextWindow,set_editable),
709 WSLUA_CLASS_FNREG(TextWindow,get_text),
710 WSLUA_CLASS_FNREG(TextWindow,add_button),
711 {0, 0}
714 WSLUA_META TextWindow_meta[] = {
715 {"__tostring", TextWindow_get_text},
716 {0, 0}
719 int TextWindow_register(lua_State* L) {
721 ops = funnel_get_funnel_ops();
723 WSLUA_REGISTER_CLASS(TextWindow);
725 return 1;
729 WSLUA_FUNCTION wslua_retap_packets(lua_State* L) {
731 Rescan all packets and just run taps - don't reconstruct the display.
733 if ( ops->retap_packets ) {
734 ops->retap_packets();
735 } else {
736 WSLUA_ERROR(wslua_retap_packets, "GUI not available");
739 return 0;
743 WSLUA_FUNCTION wslua_copy_to_clipboard(lua_State* L) { /* Copy a string into the clipboard */
744 #define WSLUA_ARG_copy_to_clipboard_TEXT 1 /* The string to be copied into the clipboard. */
745 const char* copied_str = luaL_checkstring(L,WSLUA_ARG_copy_to_clipboard_TEXT);
746 GString* gstr;
747 if (!ops->copy_to_clipboard) {
748 WSLUA_ERROR(copy_to_clipboard, "GUI not available");
751 if (!copied_str) {
752 WSLUA_ARG_ERROR(copy_to_clipboard,TEXT,"Must be a string");
755 gstr = g_string_new(copied_str);
757 ops->copy_to_clipboard(gstr);
759 g_string_free(gstr,TRUE);
761 return 0;
764 WSLUA_FUNCTION wslua_open_capture_file(lua_State* L) { /* Open and display a capture file */
765 #define WSLUA_ARG_open_capture_file_FILENAME 1 /* The name of the file to be opened. */
766 #define WSLUA_ARG_open_capture_file_FILTER 2 /* A filter to be applied as the file gets opened. */
768 const char* fname = luaL_checkstring(L,WSLUA_ARG_open_capture_file_FILENAME);
769 const char* filter = luaL_optstring(L,WSLUA_ARG_open_capture_file_FILTER,NULL);
770 const char* error = NULL;
772 if (!ops->open_file) {
773 WSLUA_ERROR(open_capture_file, "GUI not available");
776 if (!fname) {
777 WSLUA_ARG_ERROR(open_capture_file,FILENAME,"Must be a string");
780 if (! ops->open_file(fname,filter,&error) ) {
781 lua_pushboolean(L,FALSE);
783 if (error)
784 lua_pushstring(L,error);
785 else
786 lua_pushnil(L);
788 return 2;
789 } else {
790 lua_pushboolean(L,TRUE);
791 return 1;
795 WSLUA_FUNCTION wslua_get_filter(lua_State* L) { /* Get the main filter text */
796 const char *filter_str = NULL;
798 if (!ops->get_filter) {
799 WSLUA_ERROR(get_filter, "GUI not available");
802 filter_str = ops->get_filter();
803 lua_pushstring(L,filter_str);
805 return 1;
808 WSLUA_FUNCTION wslua_set_filter(lua_State* L) { /* Set the main filter text */
809 #define WSLUA_ARG_set_filter_TEXT 1 /* The filter's text. */
810 const char* filter_str = luaL_checkstring(L,WSLUA_ARG_set_filter_TEXT);
812 if (!ops->set_filter) {
813 WSLUA_ERROR(set_filter, "GUI not available");
816 if (!filter_str) {
817 WSLUA_ARG_ERROR(set_filter,TEXT,"Must be a string");
820 ops->set_filter(filter_str);
822 return 0;
825 WSLUA_FUNCTION wslua_set_color_filter_slot(lua_State* L) { /* Set packet-coloring rule for the current session */
826 #define WSLUA_ARG_set_color_filter_slot_ROW 1 /* The index of the desired color in the temporary coloring rules list */
827 #define WSLUA_ARG_set_color_filter_slot_TEXT 2 /* Display filter for selecting packets to be colorized */
828 guint8 row = luaL_checkint(L,WSLUA_ARG_set_color_filter_slot_ROW);
829 const gchar* filter_str = luaL_checkstring(L,WSLUA_ARG_set_color_filter_slot_TEXT);
831 if (!ops->set_color_filter_slot) {
832 WSLUA_ERROR(set_color_filter_slot, "GUI not available");
835 if (!filter_str) {
836 WSLUA_ARG_ERROR(set_color_filter_slot,TEXT,"Must be a string");
839 ops->set_color_filter_slot(row, filter_str);
841 return 0;
844 WSLUA_FUNCTION wslua_apply_filter(lua_State* L) { /* Apply the filter in the main filter box */
845 if (!ops->apply_filter) {
846 WSLUA_ERROR(apply_filter, "GUI not available");
849 ops->apply_filter();
851 return 0;
855 WSLUA_FUNCTION wslua_reload(lua_State* L) { /* Reload the current capture file */
857 if (!ops->reload) {
858 WSLUA_ERROR(reload, "GUI not available");
861 ops->reload();
863 return 0;
867 WSLUA_FUNCTION wslua_browser_open_url(lua_State* L) { /* Open an url in a browser */
868 #define WSLUA_ARG_browser_open_url_URL 1 /* The url. */
869 const char* url = luaL_checkstring(L,WSLUA_ARG_browser_open_url_URL);
871 if (!ops->browser_open_url) {
872 WSLUA_ERROR(browser_open_url, "GUI not available");
875 if (!url) {
876 WSLUA_ARG_ERROR(browser_open_url,URL,"Must be a string");
879 ops->browser_open_url(url);
881 return 0;
884 WSLUA_FUNCTION wslua_browser_open_data_file(lua_State* L) { /* Open an file in a browser */
885 #define WSLUA_ARG_browser_open_data_file_FILENAME 1 /* The url. */
886 const char* file = luaL_checkstring(L,WSLUA_ARG_browser_open_data_file_FILENAME);
888 if (!ops->browser_open_data_file) {
889 WSLUA_ERROR(browser_open_data_file, "GUI not available");
892 if (!file) {
893 WSLUA_ARG_ERROR(browser_open_data_file,FILENAME,"Must be a string");
896 ops->browser_open_data_file(file);
898 return 0;