aboutsummaryrefslogtreecommitdiff
path: root/gui_device.c
blob: 18a18fdb73e35943504c2e28edcb48fd1fc3e1e2 (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
/*
 *
 * Copyright (C) 2022 Jeremias Stotter <jeremias@stotter.eu>
 *
 * This file is part of network-simulator.
 *
 * network-simulator 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 3 of the License, or (at your option) any later version.
 *
 * network-simulator 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 network-simulator. If not, see <https://www.gnu.org/licenses/>. 
*/

#include <string.h>
#include <assert.h>

#include <gtk/gtk.h>

#include "template.h"
#include "device.h"
#include "interface.h"
#include "connection.h"
#include "gui.h"
#include "gui_device.h"
#include "helpers.h"

void close_device_window(GtkWindow* self, void* user_data) {
	gtk_window_destroy(self);
}

// Called if the user entered a command somewhere
void cmd_entry_changed(GtkEditable* self, gpointer user_data) {
	struct device* dev = (struct device*)user_data;
	assert(dev);
	GtkEntryBuffer* buffer = gtk_entry_get_buffer(GTK_ENTRY(self));
	const char* text = gtk_entry_buffer_get_text(buffer);
	unsigned int text_len = gtk_entry_buffer_get_length(buffer);
	append_cmdline(dev, text, text_len);
	device_cmdline(dev, text);
	actions_since_save++;
	append_cmd_history(dev, text);
	gtk_entry_buffer_set_text(buffer, "", 0);
}

void interface_selection_updated(GtkDropDown* self, void* pspec, gpointer user_data) {
	struct device* parent_device = (struct device*)user_data;
	assert(parent_device);
	// Get the slot index
	int* index = g_object_get_data(G_OBJECT(self), "int_index");
	if(!index) {
		fprintf(stderr, "Could not find the index for interface_selection_updated\n");
		return;
	}
	int template_index = gtk_drop_down_get_selected(GTK_DROP_DOWN(self));
	if(template_index == 0) {
		printf("DELETE INTERFACE\n");
		// delete if the user selects none
		delete_interface(&parent_device->interfaces[*index]);
	} else {
		template_index--;
		new_interface(parent_device, *index, &interface_templates[template_index]);
	}
	actions_since_save++;
	// Redraw the drawing area
	gtk_widget_queue_draw(main_drawing_area);

}

struct check_window_data {
	struct device* device;
	GtkWindow* window;
};

void check_window_open(gpointer window, gpointer check_data_void) {
	struct check_window_data* check_data = check_data_void;
	// Check if we already found the window
	if(check_data->window) return;
	if(g_object_get_data(G_OBJECT(window), "device") == check_data->device) check_data->window = window;
}

bool entry_key_pressed(GtkEventControllerKey* self,
					   guint keyval,
					   guint keycode,
					   GdkModifierType state,
					   gpointer user_data) {
	struct device* dev = user_data;
	switch(keyval) {
	case GDK_KEY_Down:
		dev->cmdline_current_index += 2;
	case GDK_KEY_Up:
		{
			GtkWidget* cmdline_entry = gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(self));
			assert(cmdline_entry);
			struct cmd_history* current = dev->history;
			for(int i = 0; i <= dev->cmdline_current_index && current->next; i++) {
				current = current->next;
			}
			if(current->text) {
				gtk_entry_buffer_set_text(gtk_entry_get_buffer(GTK_ENTRY(cmdline_entry)), current->text, -1);
				dev->cmdline_current_index--;
			}
			return true;
		}
		break;
	}
	return false;
}

void help_clicked(GtkButton* self, gpointer user_data) {
	GFile* help_file = g_file_new_for_uri((char*)user_data);
	GtkFileLauncher* help_file_launcher = gtk_file_launcher_new(help_file);
	gtk_file_launcher_launch(help_file_launcher, NULL, NULL, NULL, NULL);
	g_object_unref(help_file_launcher);
	g_object_unref(help_file);
}

// Show the gui for the device dev
void show_device_window(struct device* dev) {
	// Find out if the device window is already open
	GList* topwindows = gtk_window_list_toplevels();
	struct check_window_data check_data = {dev, NULL};
	g_list_foreach(topwindows, check_window_open, &check_data);
	if(check_data.window) {
		gtk_window_present(check_data.window);
		return;
	}

	GtkWidget* dev_window = gtk_window_new();
	gtk_window_set_default_size(GTK_WINDOW(dev_window), 600, 450);
	// Set the window title
	gtk_window_set_title(GTK_WINDOW(dev_window), dev->name);
	g_signal_connect(G_OBJECT(dev_window), "close-request", G_CALLBACK(close_device_window), NULL);
	// Create a notebook that is going to hold the different options for the device
	GtkWidget* main_notebook = gtk_notebook_new();
	gtk_window_set_child(GTK_WINDOW(dev_window), main_notebook);
	// === Commandline ===
	// Create the commandline output
	GtkWidget* cmdline_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
	GtkWidget* cmdline_text = gtk_text_view_new();
	gtk_text_view_set_buffer(GTK_TEXT_VIEW(cmdline_text), dev->cmd_buffer);
	gtk_text_view_set_monospace(GTK_TEXT_VIEW(cmdline_text), true);
	gtk_text_view_set_editable(GTK_TEXT_VIEW(cmdline_text), false);
	gtk_widget_set_vexpand(GTK_WIDGET(cmdline_text), true);
	GtkWidget* cmdline_text_scroll = gtk_scrolled_window_new();
	gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(cmdline_text_scroll), cmdline_text);
	gtk_box_append(GTK_BOX(cmdline_box), cmdline_text_scroll);
	
	GtkWidget* cmdline_bottom_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
	gtk_box_append(GTK_BOX(cmdline_box), cmdline_bottom_box);
	// Create a help button if needed
	if(dev->template->docs) {
		GtkWidget* help_button = gtk_button_new_from_icon_name("help-browser-symbolic");
		gtk_box_append(GTK_BOX(cmdline_bottom_box), help_button);
		g_signal_connect(G_OBJECT(help_button), "clicked", G_CALLBACK(help_clicked), dev->template->docs);
	}
	// Create the commandline input	
	GtkWidget* cmdline_entry = gtk_entry_new();
	gtk_widget_set_hexpand(cmdline_entry, true);
	gtk_box_append(GTK_BOX(cmdline_bottom_box), cmdline_entry);
	g_signal_connect(G_OBJECT(cmdline_entry), "activate", G_CALLBACK(cmd_entry_changed), dev);
	GtkEventController* input_event_controller = gtk_event_controller_key_new();
	g_signal_connect(G_OBJECT(input_event_controller), "key-pressed", G_CALLBACK(entry_key_pressed), dev);
	gtk_widget_add_controller(cmdline_entry, input_event_controller);
	// Add the cmdline to the notebook
	GtkWidget* cmdline_label = gtk_label_new("Command");
	gtk_notebook_append_page(GTK_NOTEBOOK(main_notebook), cmdline_box, cmdline_label);
	// === Interfaces ===
	GtkWidget* interfaces_box = gtk_flow_box_new();
	gtk_flow_box_set_homogeneous(GTK_FLOW_BOX(interfaces_box), true);
	gtk_flow_box_set_selection_mode(GTK_FLOW_BOX(interfaces_box), GTK_SELECTION_NONE);
	// For all the interface slots add a selection
	for(int i = 0; i < dev->template->slots; i++) {
		// A box for the label and dropdown
		GtkWidget* interface_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
		// Add the slot index label
		char slot_index[25] = "";
		snprintf(slot_index, 25, "<big>%d</big>", i);
		GtkWidget* slot_label = gtk_label_new(NULL);
		gtk_label_set_markup(GTK_LABEL(slot_label), slot_index);
		gtk_box_append(GTK_BOX(interface_box), slot_label);
		// Add the dropdown
		char** interface_template_list = malloc_or_abort(sizeof(char*) * (interface_template_n + 2));
		interface_template_list[0] = "None";
		// Fill up the dropdown
		for(int j = 0; j < interface_template_n; j++) {
			// Prepend a _ to the name of the device so that a template can not take the id none by mistake
		    interface_template_list[j+1] = (char*)interface_templates[j].name;
		}
		interface_template_list[interface_template_n+1] = NULL;
		
		GtkWidget* slot_selection = gtk_drop_down_new_from_strings((const char* const *)interface_template_list);
		free(interface_template_list);

		// Add the index to the dropdown so we can set things later
		int* index = malloc_or_abort(sizeof(int));
		*index = i;
		g_object_set_data_full(G_OBJECT(slot_selection), "int_index", index, free);

		// Set the interface that is in the slot
		if(dev->interfaces[i].template) {
			// The slot is used
			int template_index = (dev->interfaces[i].template - interface_templates) / sizeof(struct interface_template) + 1;
			gtk_drop_down_set_selected(GTK_DROP_DOWN(slot_selection), template_index);

		} else {
			// The slot is unused
		    gtk_drop_down_set_selected(GTK_DROP_DOWN(slot_selection), 0);
		}
		g_signal_connect(G_OBJECT(slot_selection), "notify::selected", G_CALLBACK(interface_selection_updated), dev);
		gtk_box_append(GTK_BOX(interface_box), slot_selection);
		// Add the interface to the flow box
		gtk_flow_box_append(GTK_FLOW_BOX(interfaces_box), interface_box);
	}
	// Add a scroll window for the interfaces
	GtkWidget* interface_scroll = gtk_scrolled_window_new();
	gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(interface_scroll), interfaces_box);
	// Add the interface selection to the notebook
	GtkWidget* interface_label = gtk_label_new("Interfaces");
	gtk_notebook_append_page(GTK_NOTEBOOK(main_notebook), interface_scroll, interface_label);
	
	// Show the window
	gtk_widget_set_visible(GTK_WIDGET(dev_window), true);
}
Jeremias Stotters git repositories generated by CGIT