aboutsummaryrefslogtreecommitdiffstats
path: root/epan/wslua/wslua_gui.c
blob: b73564eff93be676488b421ce60cd61212ae5cca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/*
 *  wslua_gui.c
 *
 * (c) 2006, Luis E. Garcia Ontanon <luis@ontanon.org>
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <epan/wmem/wmem.h>

#include "wslua.h"

/* WSLUA_MODULE Gui GUI support */

static const funnel_ops_t* ops = NULL;

struct _lua_menu_data {
    lua_State* L;
    int cb_ref;
};

static int menu_cb_error_handler(lua_State* L) {
    const gchar* error =  lua_tostring(L,1);
    report_failure("Lua: Error During execution of Menu Callback:\n %s",error);
    return 0;
}

WSLUA_FUNCTION wslua_gui_enabled(lua_State* L) { /* Checks whether the GUI facility is enabled. */
    lua_pushboolean(L,GPOINTER_TO_INT(ops && ops->add_button));
    WSLUA_RETURN(1); /* A boolean: true if it is enabled, false if it isn't. */
}

static void lua_menu_callback(gpointer data) {
    struct _lua_menu_data* md = (struct _lua_menu_data *)data;
    lua_State* L = md->L;

    lua_settop(L,0);
    lua_pushcfunction(L,menu_cb_error_handler);
    lua_rawgeti(L, LUA_REGISTRYINDEX, md->cb_ref);

    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;
    }

    return;
}

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_STAT_TELEPHONY_GSM (Telephony/GSM),
                                              * MENU_STAT_TELEPHONY_LTE (Telephony/LTE),
                                              * MENU_STAT_TELEPHONY_SCTP (Telephony/SCTP),
                                              * MENU_ANALYZE (Analyze),
                                              * MENU_ANALYZE_CONVERSATION (Analyze/Conversation Filter),
                                              * MENU_TOOLS_UNSORTED (Tools). (number) */

    const gchar* name = luaL_checkstring(L,WSLUA_ARG_register_menu_NAME);
    struct _lua_menu_data* md;
    gboolean retap = FALSE;
    register_stat_group_t group = (register_stat_group_t)wslua_optguint(L,WSLUA_OPTARG_register_menu_GROUP,REGISTER_STAT_GROUP_GENERIC);

    if ( group > REGISTER_TOOLS_GROUP_UNSORTED) {
        WSLUA_OPTARG_ERROR(register_menu,GROUP,"Must be a defined MENU_* (see init.lua)");
        return 0;
    }

    if (!lua_isfunction(L,WSLUA_ARG_register_menu_ACTION)) {
        WSLUA_ARG_ERROR(register_menu,ACTION,"Must be a function");
        return 0;
    }

    md = (struct _lua_menu_data *)g_malloc(sizeof(struct _lua_menu_data));
    md->L = L;

    lua_pushvalue(L, 2);
    md->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
    lua_remove(L,2);

    funnel_register_menu(name,
                         group,
                         lua_menu_callback,
                         md,
                         retap);

    WSLUA_RETURN(0);
}




struct _dlg_cb_data {
    lua_State* L;
    int func_ref;
};

static int dlg_cb_error_handler(lua_State* L) {
    const gchar* error =  lua_tostring(L,1);
    report_failure("Lua: Error During execution of dialog callback:\n %s",error);
    return 0;
}

static void lua_dialog_cb(gchar** user_input, void* data) {
    struct _dlg_cb_data* dcbd = (struct _dlg_cb_data *)data;
    int i = 0;
    gchar* input;
    lua_State* L = dcbd->L;

    lua_settop(L,0);
    lua_pushcfunction(L,dlg_cb_error_handler);
    lua_rawgeti(L, LUA_REGISTRYINDEX, dcbd->func_ref);

    for (i = 0; (input = user_input[i]) ; i++) {
        lua_pushstring(L,input);
        g_free(input);
    }

    g_free(user_input);

    switch ( lua_pcall(L,i,0,1) ) {
        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;
    }

}

struct _close_cb_data {
    lua_State* L;
    int func_ref;
    TextWindow wslua_tw;
};


static int text_win_close_cb_error_handler(lua_State* L) {
    const gchar* error =  lua_tostring(L,1);
    report_failure("Lua: Error During execution of TextWindow close callback:\n %s",error);
    return 0;
}

static void text_win_close_cb(void* data) {
    struct _close_cb_data* cbd = (struct _close_cb_data *)data;
    lua_State* L = cbd->L;

    if (cbd->L) { /* close function is set */

        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;
        }
    }

    if (cbd->wslua_tw->expired) {
        g_free(cbd->wslua_tw);
    } else {
        cbd->wslua_tw->expired = TRUE;
    }

}

WSLUA_FUNCTION wslua_new_dialog(lua_State* L) { /* Pops up a new dialog */
#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 OK'd. */
/* WSLUA_MOREARGS new_dialog A series of strings to be used as labels of the dialog's fields. */

    const gchar* title;
    int top = lua_gettop(L);
    int i;
    GPtrArray* labels;
    struct _dlg_cb_data* dcbd;

    if (! ops) {
        luaL_error(L,"the GUI facility has to be enabled");
        return 0;
    }

    if (!ops->new_dialog) {
        WSLUA_ERROR(new_dialog,"GUI not available");
        return 0;
    }

    title = luaL_checkstring(L,WSLUA_ARG_new_dialog_TITLE);

    if (! lua_isfunction(L,WSLUA_ARG_new_dialog_ACTION)) {
        WSLUA_ARG_ERROR(new_dialog,ACTION,"Must be a function");
        return 0;
    }

    if (top < 3) {
        WSLUA_ERROR(new_dialog,"At least one field required");
        return 0;
    }


    dcbd = (struct _dlg_cb_data *)g_malloc(sizeof(struct _dlg_cb_data));
    dcbd->L = L;

    lua_remove(L,1);

    lua_pushvalue(L, 1);
    dcbd->func_ref = luaL_ref(L, LUA_REGISTRYINDEX);
    lua_remove(L,1);

    labels = g_ptr_array_new();

    top -= 2;

    for (i = 1; i <= top; i++) {
        if (! lua_isstring(L,i)) {
            g_ptr_array_free(labels,TRUE);
            WSLUA_ERROR(new_dialog,"All fields must be strings");
            return 0;
        }

        g_ptr_array_add(labels,(gpointer)g_strdup(luaL_checkstring(L,i)));
    }

    g_ptr_array_add(labels,NULL);

    ops->new_dialog(title, (const gchar**)(labels->pdata), lua_dialog_cb, dcbd);

    g_ptr_array_free(labels,TRUE);

    WSLUA_RETURN(0);
}



WSLUA_CLASS_DEFINE(ProgDlg,FAIL_ON_NULL("ProgDlg"),NOP); /* Manages a progress bar dialog. */

WSLUA_CONSTRUCTOR ProgDlg_new(lua_State* L) { /* Creates a new `ProgDlg` progress dialog. */
#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 = (ProgDlg)g_malloc(sizeof(struct _wslua_progdlg));
    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,""));
    pd->stopped = FALSE;

    if (ops->new_progress_window) {
        pd->pw = ops->new_progress_window(pd->title,pd->task,TRUE,&(pd->stopped));
    } else {
        WSLUA_ERROR(ProgDlg_new, "GUI not available");
        return 0;
    }

    pushProgDlg(L,pd);

    WSLUA_RETURN(1); /* The newly created `ProgDlg` object. */
}

WSLUA_METHOD ProgDlg_update(lua_State* L) { /* Appends text. */
#define WSLUA_ARG_ProgDlg_update_PROGRESS 2  /* Part done ( e.g. 0.75 ). */
#define WSLUA_OPTARG_ProgDlg_update_TASK 3  /* Current task, defaults to "". */
    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,"");

    if (!ops->update_progress) {
        WSLUA_ERROR(ProgDlg_update,"GUI not available");
        return 0;
    }

    g_free(pd->task);
    pd->task = g_strdup(task);

    /* 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");
    } */

    if (pr >= 0.0 && pr <= 1.0) {
        ops->update_progress(pd->pw, (float) pr, task);
    } else {
        WSLUA_ERROR(ProgDlg_update,"Progress value out of range (must be between 0.0 and 1.0)");
        return 0;
    }

    return 0;
}

WSLUA_METHOD ProgDlg_stopped(lua_State* L) { /* Checks whether the user has pressed the stop button.  */
    ProgDlg pd = checkProgDlg(L,1);

    lua_pushboolean(L,pd->stopped);

    WSLUA_RETURN(1); /* true if the user has asked to stop the progress. */
}



WSLUA_METHOD ProgDlg_close(lua_State* L) { /* Closes the progress dialog. */
    ProgDlg pd = checkProgDlg(L,1);

    if (!ops->destroy_progress_window) {
        WSLUA_ERROR(ProgDlg_close,"GUI not available");
        return 0;
    }

    if (pd->pw) {
        ops->destroy_progress_window(pd->pw);
        pd->pw = NULL;
    }
    return 0;
}


static int ProgDlg__tostring(lua_State* L) {
    ProgDlg pd = checkProgDlg(L,1);

    lua_pushfstring(L, "%sstopped",pd->stopped?"":"not ");

    WSLUA_RETURN(1); /* A string specifying whether the Progress Dialog has stopped or not. */
}

/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int ProgDlg__gc(lua_State* L) {
    ProgDlg pd = toProgDlg(L,1);

    if (pd) {
        if (pd->pw && ops->destroy_progress_window) {
            ops->destroy_progress_window(pd->pw);
        }

        g_free(pd);
    } else {
        luaL_error(L, "ProgDlg__gc has being passed something else!");
    }

    return 0;
}


WSLUA_METHODS ProgDlg_methods[] = {
    WSLUA_CLASS_FNREG(ProgDlg,new),
    WSLUA_CLASS_FNREG(ProgDlg,update),
    WSLUA_CLASS_FNREG(ProgDlg,stopped),
    WSLUA_CLASS_FNREG(ProgDlg,close),
    { NULL, NULL }
};

WSLUA_META ProgDlg_meta[] = {
    WSLUA_CLASS_MTREG(ProgDlg,tostring),
    { NULL, NULL }
};

int ProgDlg_register(lua_State* L) {

    ops = funnel_get_funnel_ops();

    WSLUA_REGISTER_CLASS(ProgDlg);

    return 0;
}



WSLUA_CLASS_DEFINE(TextWindow,FAIL_ON_NULL_OR_EXPIRED("TextWindow"),NOP); /* Manages a text window. */

/* XXX: button and close callback data is being leaked */
/* XXX: lua callback function and TextWindow are not garbage collected because
   they stay in LUA_REGISTRYINDEX forever */

WSLUA_CONSTRUCTOR TextWindow_new(lua_State* L) { /* Creates a new `TextWindow` text window. */
#define WSLUA_OPTARG_TextWindow_new_TITLE 1 /* Title of the new window. */

    const gchar* title;
    TextWindow tw = NULL;
    struct _close_cb_data* default_cbd;

    if (!ops->new_text_window || !ops->set_close_cb) {
        WSLUA_ERROR(TextWindow_new,"GUI not available");
        return 0;
    }

    title = luaL_optstring(L,WSLUA_OPTARG_TextWindow_new_TITLE,"Untitled Window");
    tw = (struct _wslua_tw *)g_malloc(sizeof(struct _wslua_tw));
    tw->expired = FALSE;
    tw->ws_tw = ops->new_text_window(title);

    default_cbd = (struct _close_cb_data *)g_malloc(sizeof(struct _close_cb_data));

    default_cbd->L = NULL;
    default_cbd->func_ref = 0;
    default_cbd->wslua_tw = tw;

    ops->set_close_cb(tw->ws_tw,text_win_close_cb,default_cbd);

    pushTextWindow(L,tw);

    WSLUA_RETURN(1); /* The newly created `TextWindow` object. */
}

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. */

    TextWindow tw = checkTextWindow(L,1);
    struct _close_cb_data* cbd;

    if (!ops->set_close_cb) {
        WSLUA_ERROR(TextWindow_set_atclose,"GUI not available");
        return 0;
    }

    lua_settop(L,2);

    if (! lua_isfunction(L,2)) {
        WSLUA_ARG_ERROR(TextWindow_at_close,ACTION,"Must be a function");
        return 0;
    }

    cbd = (struct _close_cb_data *)g_malloc(sizeof(struct _close_cb_data));

    cbd->L = L;
    cbd->func_ref = luaL_ref(L, LUA_REGISTRYINDEX);
    cbd->wslua_tw = tw;

    ops->set_close_cb(tw->ws_tw,text_win_close_cb,cbd);

    /* XXX: this is a bad way to do this - should copy the object on to the stack first */
    WSLUA_RETURN(1); /* The `TextWindow` object. */
}

WSLUA_METHOD TextWindow_set(lua_State* L) { /* Sets the text. */
#define WSLUA_ARG_TextWindow_set_TEXT 2 /* The text to be used. */

    TextWindow tw = checkTextWindow(L,1);
    const gchar* text = luaL_checkstring(L,WSLUA_ARG_TextWindow_set_TEXT);

    if (!ops->set_text) {
        WSLUA_ERROR(TextWindow_set,"GUI not available");
        return 0;
    }

    ops->set_text(tw->ws_tw,text);

    /* XXX: this is a bad way to do this - should copy the object on to the stack first */
    WSLUA_RETURN(1); /* The `TextWindow` object. */
}

WSLUA_METHOD TextWindow_append(lua_State* L) { /* Appends text */
#define WSLUA_ARG_TextWindow_append_TEXT 2 /* The text to be appended */
    TextWindow tw = checkTextWindow(L,1);
    const gchar* text = luaL_checkstring(L,WSLUA_ARG_TextWindow_append_TEXT);

    if (!ops->append_text) {
        WSLUA_ERROR(TextWindow_append,"GUI not available");
        return 0;
    }

    ops->append_text(tw->ws_tw,text);

    /* XXX: this is a bad way to do this - should copy the object on to the stack first */
    WSLUA_RETURN(1); /* The `TextWindow` object. */
}

WSLUA_METHOD TextWindow_prepend(lua_State* L) { /* Prepends text */
#define WSLUA_ARG_TextWindow_prepend_TEXT 2 /* The text to be appended */
    TextWindow tw = checkTextWindow(L,1);
    const gchar* text = luaL_checkstring(L,WSLUA_ARG_TextWindow_prepend_TEXT);

    if (!ops->prepend_text) {
        WSLUA_ERROR(TextWindow_prepend,"GUI not available");
        return 0;
    }

    ops->prepend_text(tw->ws_tw,text);

    /* XXX: this is a bad way to do this - should copy the object on to the stack first */
    WSLUA_RETURN(1); /* The `TextWindow` object. */
}

WSLUA_METHOD TextWindow_clear(lua_State* L) { /* Erases all text in the window. */
    TextWindow tw = checkTextWindow(L,1);

    if (!ops->clear_text) {
        WSLUA_ERROR(TextWindow_clear,"GUI not available");
        return 0;
    }

    ops->clear_text(tw->ws_tw);

    /* XXX: this is a bad way to do this - should copy the object on to the stack first */
    WSLUA_RETURN(1); /* The TextWindow object. */
}

WSLUA_METHOD TextWindow_get_text(lua_State* L) { /* Get the text of the window */
    TextWindow tw = checkTextWindow(L,1);
    const gchar* text;

    if (!ops->get_text) {
        WSLUA_ERROR(TextWindow_get_text,"GUI not available");
        return 0;
    }

    text = ops->get_text(tw->ws_tw);

    lua_pushstring(L,text);
    WSLUA_RETURN(1); /* The `TextWindow`'s text. */
}

/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int TextWindow__gc(lua_State* L) {
    TextWindow tw = toTextWindow(L,1);

    if (!tw)
        return 0;

    if (!tw->expired) {
        tw->expired = TRUE;
        if (ops->destroy_text_window) {
            ops->destroy_text_window(tw->ws_tw);
        }
    } else {
        g_free(tw);
    }

    return 0;
}

WSLUA_METHOD TextWindow_set_editable(lua_State* L) { /* Make this text window editable. */
#define WSLUA_OPTARG_TextWindow_set_editable_EDITABLE 2 /* A boolean flag, defaults to true. */

    TextWindow tw = checkTextWindow(L,1);
    gboolean editable = wslua_optbool(L,WSLUA_OPTARG_TextWindow_set_editable_EDITABLE,TRUE);

    if (!ops->set_editable) {
        WSLUA_ERROR(TextWindow_set_editable,"GUI not available");
        return 0;
    }

    ops->set_editable(tw->ws_tw,editable);

    WSLUA_RETURN(1); /* The `TextWindow` object. */
}

typedef struct _wslua_bt_cb_t {
    lua_State* L;
    int func_ref;
    int wslua_tw_ref;
} wslua_bt_cb_t;

static gboolean wslua_button_callback(funnel_text_window_t* ws_tw, void* data) {
    wslua_bt_cb_t* cbd = (wslua_bt_cb_t *)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) {
    /* Adds a button to the text window. */
#define WSLUA_ARG_TextWindow_add_button_LABEL 2 /* The label of the button */
#define WSLUA_ARG_TextWindow_add_button_FUNCTION 3 /* The Lua function to be called when clicked */
    TextWindow tw = checkTextWindow(L,1);
    const gchar* label = luaL_checkstring(L,WSLUA_ARG_TextWindow_add_button_LABEL);

    funnel_bt_t* fbt;
    wslua_bt_cb_t* cbd;

    if (!ops->add_button) {
        WSLUA_ERROR(TextWindow_add_button,"GUI not available");
        return 0;
    }

    if (! lua_isfunction(L,WSLUA_ARG_TextWindow_add_button_FUNCTION) ) {
        WSLUA_ARG_ERROR(TextWindow_add_button,FUNCTION,"must be a function");
        return 0;
    }

    lua_settop(L,3);

    if (ops->add_button) {
        fbt = (funnel_bt_t *)g_malloc(sizeof(funnel_bt_t));
        cbd = (wslua_bt_cb_t *)g_malloc(sizeof(wslua_bt_cb_t));

        fbt->tw = tw->ws_tw;
        fbt->func = wslua_button_callback;
        fbt->data = cbd;
        fbt->free_fcn = g_free;
        fbt->free_data_fcn = g_free;

        cbd->L = L;
        cbd->func_ref = luaL_ref(L, LUA_REGISTRYINDEX);
        cbd->wslua_tw_ref = luaL_ref(L, LUA_REGISTRYINDEX);

        ops->add_button(tw->ws_tw,fbt,label);
    }

    WSLUA_RETURN(1); /* The `TextWindow` object. */
}

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),
    { NULL, NULL }
};

WSLUA_META TextWindow_meta[] = {
    {"__tostring", TextWindow_get_text},
    { NULL, NULL }
};

int TextWindow_register(lua_State* L) {

    ops = funnel_get_funnel_ops();

    WSLUA_REGISTER_CLASS(TextWindow);

    return 0;
}


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, "GUI not available");
    }

    return 0;
}


WSLUA_FUNCTION wslua_copy_to_clipboard(lua_State* L) { /* Copy a string into the clipboard. */
#define WSLUA_ARG_copy_to_clipboard_TEXT 1 /* The string to be copied into the clipboard. */
    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");
        return 0;
    }

    gstr = g_string_new(copied_str);

    ops->copy_to_clipboard(gstr);

    g_string_free(gstr,TRUE);

    return 0;
}

WSLUA_FUNCTION wslua_open_capture_file(lua_State* L) { /* Open and display a capture file. */
#define WSLUA_ARG_open_capture_file_FILENAME 1 /* The name of the file to be opened. */
#define WSLUA_ARG_open_capture_file_FILTER 2 /* A filter to be applied as the file gets opened. */

    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;

    if (!ops->open_file) {
        WSLUA_ERROR(open_capture_file, "GUI not available");
        return 0;
    }

    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;
    }
}

WSLUA_FUNCTION wslua_get_filter(lua_State* L) { /* Get the main filter text. */
    const char *filter_str = NULL;

    if (!ops->get_filter) {
        WSLUA_ERROR(get_filter, "GUI not available");
        return 0;
    }

    filter_str = ops->get_filter();
    lua_pushstring(L,filter_str);

    return 1;
}

WSLUA_FUNCTION wslua_set_filter(lua_State* L) { /* Set the main filter text. */
#define WSLUA_ARG_set_filter_TEXT 1 /* The filter's text. */
    const char* filter_str = luaL_checkstring(L,WSLUA_ARG_set_filter_TEXT);

    if (!ops->set_filter) {
        WSLUA_ERROR(set_filter, "GUI not available");
        return 0;
    }

    ops->set_filter(filter_str);

    return 0;
}

WSLUA_FUNCTION wslua_set_color_filter_slot(lua_State* L) { /* Set packet-coloring rule for the current session. */
#define WSLUA_ARG_set_color_filter_slot_ROW 1 /* The index of the desired color in the temporary coloring rules list. */
#define WSLUA_ARG_set_color_filter_slot_TEXT  2 /* Display filter for selecting packets to be colorized. */
    guint8 row = luaL_checkint(L,WSLUA_ARG_set_color_filter_slot_ROW);
    const gchar* filter_str = luaL_checkstring(L,WSLUA_ARG_set_color_filter_slot_TEXT);

    if (!ops->set_color_filter_slot) {
        WSLUA_ERROR(set_color_filter_slot, "GUI not available");
        return 0;
    }

    ops->set_color_filter_slot(row, filter_str);

    return 0;
}

WSLUA_FUNCTION wslua_apply_filter(lua_State* L) { /* Apply the filter in the main filter box. */
    if (!ops->apply_filter) {
        WSLUA_ERROR(apply_filter, "GUI not available");
        return 0;
    }

    ops->apply_filter();

    return 0;
}


WSLUA_FUNCTION wslua_reload(lua_State* L) { /* Reload the current capture file. */

    if (!ops->reload) {
        WSLUA_ERROR(reload, "GUI not available");
        return 0;
    }

    ops->reload();

    return 0;
}


WSLUA_FUNCTION wslua_browser_open_url(lua_State* L) { /* Open an url in a browser. */
#define WSLUA_ARG_browser_open_url_URL 1 /* The url. */
    const char* url = luaL_checkstring(L,WSLUA_ARG_browser_open_url_URL);

    if (!ops->browser_open_url) {
        WSLUA_ERROR(browser_open_url, "GUI not available");
        return 0;
    }

    ops->browser_open_url(url);

    return 0;
}

WSLUA_FUNCTION wslua_browser_open_data_file(lua_State* L) { /* Open a file in a browser. */
#define WSLUA_ARG_browser_open_data_file_FILENAME 1 /* The file name. */
    const char* file = luaL_checkstring(L,WSLUA_ARG_browser_open_data_file_FILENAME);

    if (!ops->browser_open_data_file) {
        WSLUA_ERROR(browser_open_data_file, "GUI not available");
        return 0;
    }

    ops->browser_open_data_file(file);

    return 0;
}