Commit 8ef4f88a13aafc1dee0865c07f4a105ed5db31d6

Authored by Perry Werneck
1 parent 956af88f

Fixing segfault when the active terminal is closed.

src/include/pw3270/window.h
... ... @@ -59,6 +59,12 @@
59 59 GType pw3270ApplicationWindow_get_type();
60 60 GtkWidget * pw3270_application_window_new(GtkApplication * app, const gchar *session_file);
61 61  
  62 + /// @brief Set the active terminal widget.
  63 + void pw3270_application_window_set_active_terminal(GtkWidget *window, GtkWidget *terminal);
  64 +
  65 + /// @brief Get the active terminal widget.
  66 + GtkWidget * pw3270_application_window_get_active_terminal(GtkWidget *window);
  67 +
62 68 /// @brief Create a new terminal tab.
63 69 GtkWidget * pw3270_application_window_new_tab(GtkWidget *window, const gchar *session_file);
64 70  
... ...
src/objects/window/page.c
... ... @@ -88,7 +88,7 @@
88 88  
89 89 //----------------------------------------------------------------------------------------------------------------------------------
90 90  
91   - static gboolean on_terminal_focus(GtkWidget *terminal, GdkEvent G_GNUC_UNUSED(*event), GtkWindow * window);
  91 + static gboolean on_terminal_focus(GtkWidget *terminal, GdkEvent G_GNUC_UNUSED(*event), GtkWidget * window);
92 92 static void session_changed(GtkWidget *terminal, GtkWidget *label);
93 93 static void disconnected(GtkWidget *terminal, GtkWindow * window);
94 94 static void connected(GtkWidget *terminal, const gchar *host, GtkWindow * window);
... ... @@ -147,8 +147,11 @@
147 147  
148 148 }
149 149  
150   - static gboolean on_terminal_focus(GtkWidget *terminal, GdkEvent G_GNUC_UNUSED(*event), GtkWindow * window) {
  150 + static gboolean on_terminal_focus(GtkWidget *terminal, GdkEvent G_GNUC_UNUSED(*event), GtkWidget * window) {
151 151  
  152 + pw3270_application_window_set_active_terminal(window,terminal);
  153 +
  154 + /*
152 155 if(gtk_window_get_default_widget(window) == terminal) {
153 156 return FALSE;
154 157 }
... ... @@ -186,6 +189,7 @@
186 189 }
187 190  
188 191 g_strfreev(actions);
  192 + */
189 193  
190 194 return FALSE;
191 195 }
... ... @@ -233,14 +237,16 @@
233 237  
234 238 static void destroy(GtkWidget *terminal, GtkWindow * window) {
235 239  
236   - if(gtk_window_get_default_widget(window) != terminal) {
237   - debug("Terminal %p was destroyed (Default one is %p)",__FUNCTION__,gtk_window_get_default_widget(window));
  240 + if(pw3270_application_window_get_active_terminal(GTK_WIDGET(window)) != terminal) {
  241 + debug("Terminal %p was destroyed (Default one is %p)",__FUNCTION__,pw3270_application_window_get_active_terminal(GTK_WIDGET(window)));
238 242 return;
239 243 }
240 244  
241 245 debug("Default terminal %p was destroyed",__FUNCTION__);
242 246  
243   - gtk_window_set_default(window,NULL);
  247 + pw3270_application_window_set_active_terminal(GTK_WIDGET(window),NULL);
  248 +
  249 + /*
244 250 pw3270_window_set_subtitle(GTK_WIDGET(window), _("Disconnected from host"));
245 251  
246 252 // Update actions
... ... @@ -264,6 +270,7 @@
264 270 }
265 271  
266 272 g_strfreev(actions);
  273 + */
267 274  
268 275 }
269 276  
... ...
src/objects/window/private.h
... ... @@ -60,6 +60,7 @@
60 60  
61 61 GtkApplicationWindow parent;
62 62  
  63 + GtkWidget * terminal; ///< @brief The active terminal.
63 64 GtkNotebook * notebook;
64 65 GtkToolbar * toolbar;
65 66  
... ...
src/objects/window/window.c
... ... @@ -50,6 +50,9 @@
50 50  
51 51 debug("%s(%p)",__FUNCTION__,widget);
52 52  
  53 + pw3270_application_window_set_active_terminal(widget,NULL);
  54 +
  55 + /*
53 56 // Update actions
54 57 gchar ** actions = g_action_group_list_actions(G_ACTION_GROUP(widget));
55 58  
... ... @@ -67,8 +70,9 @@
67 70 }
68 71  
69 72 }
70   -
71 73 g_strfreev(actions);
  74 + */
  75 +
72 76  
73 77 // Destroy popups
74 78 for(ix = 0; ix < G_N_ELEMENTS(window->popups); ix++) {
... ... @@ -383,3 +387,60 @@
383 387 g_message("Generic action %s was activated",g_action_get_name(G_ACTION(action)));
384 388 }
385 389  
  390 + GtkWidget * pw3270_application_window_get_active_terminal(GtkWidget *widget) {
  391 + return PW3270_APPLICATION_WINDOW(widget)->terminal;
  392 + }
  393 +
  394 + void pw3270_application_window_set_active_terminal(GtkWidget *widget, GtkWidget *terminal) {
  395 +
  396 + pw3270ApplicationWindow * window = PW3270_APPLICATION_WINDOW(widget);
  397 +
  398 + if(window->terminal == terminal)
  399 + return;
  400 +
  401 + if(terminal && GTK_IS_V3270(terminal)) {
  402 +
  403 + window->terminal = terminal;
  404 +
  405 + // Store the active terminal widget.
  406 + gtk_widget_grab_default(terminal);
  407 + debug("Terminal %p is now default",terminal);
  408 +
  409 + // Change window title
  410 + g_autofree gchar * title = v3270_get_session_title(terminal);
  411 + gtk_window_set_title(GTK_WINDOW(window), title);
  412 +
  413 + pw3270_window_set_subtitle(window, v3270_is_connected(terminal) ? _("Connected to host") : _("Disconnected from host"));
  414 +
  415 + } else {
  416 +
  417 + terminal = NULL;
  418 + pw3270_window_set_subtitle(window, _("Disconnected from host"));
  419 +
  420 + }
  421 +
  422 + // Update actions
  423 + size_t ix;
  424 + gchar ** actions = g_action_group_list_actions(G_ACTION_GROUP(window));
  425 +
  426 + for(ix = 0; actions[ix]; ix++) {
  427 +
  428 +// debug("%s",actions[ix]);
  429 +
  430 + GAction * action = g_action_map_lookup_action(G_ACTION_MAP(window), actions[ix]);
  431 +
  432 + if(action) {
  433 +
  434 + if(V3270_IS_ACTION(action)) {
  435 + v3270_action_set_terminal_widget(action,terminal);
  436 + } else if(PW3270_IS_ACTION(action)) {
  437 + pw3270_action_set_terminal_widget(action,terminal);
  438 + }
  439 +
  440 + }
  441 +
  442 + }
  443 +
  444 + g_strfreev(actions);
  445 +
  446 + }
... ...