File: gtk.c

package info (click to toggle)
mtr 0.28-0.2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 404 kB
  • ctags: 368
  • sloc: ansic: 2,905; sh: 282; makefile: 60
file content (333 lines) | stat: -rw-r--r-- 8,335 bytes parent folder | download
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
/*
    mtr  --  a network diagnostic tool
    Copyright (C) 1997,1998  Matt Kimball
    Changes/additions Copyright (C) 1998 R.E.Wolff@BitWizard.nl

    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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <config.h>
#include <sys/time.h>

#ifndef NO_GTK
#include <stdlib.h>
#include <gtk/gtk.h>

#include "net.h"
#include "dns.h"
#include "mtr-gtk.h"

#include "img/mtr_icon.xpm"
#endif

extern char *Hostname;
extern float WaitTime;
extern float DeltaTime;

static int tag;

void gtk_do_init(int *argc, char ***argv) {
  static int done = 0;

  if(!done) {
    gtk_init(argc, argv);

    done = 1;
  }
}

int gtk_detect(int *argc, char ***argv) {
  if(getenv("DISPLAY") != NULL) {
    gtk_do_init(argc, argv);
    return TRUE;
  } else {
    return FALSE;
  }
}

gint Window_destroy(GtkWidget *Window, gpointer data) {
  gtk_main_quit();

  return FALSE;
}

gint Restart_clicked(GtkWidget *Button, gpointer data) {
  net_reset();
  gtk_redraw();

  return FALSE;
}

gint Host_activate(GtkWidget *Entry, gpointer data) {
  int addr;

  addr = dns_forward(gtk_entry_get_text(GTK_ENTRY(Entry)));
  if(addr)
    net_reopen(addr);

  return FALSE;
}

GdkPixmap *gtk_load_pixmap(char **pixmap) {
  return gdk_pixmap_colormap_create_from_xpm_d(NULL, 
					       gdk_colormap_get_system(), 
					       NULL, NULL, pixmap);
}

void Toolbar_fill(GtkWidget *Toolbar) {
  GtkWidget *Button;
  GtkWidget *Label;
  GtkWidget *Entry;

  Button = gtk_button_new_with_label("Quit");
  gtk_box_pack_end(GTK_BOX(Toolbar), Button, FALSE, FALSE, 0);
  gtk_signal_connect(GTK_OBJECT(Button), "clicked",
		     GTK_SIGNAL_FUNC(Window_destroy), NULL);
  gtk_widget_show(Button);

  Button = gtk_button_new_with_label("Restart");
  gtk_box_pack_end(GTK_BOX(Toolbar), Button, FALSE, FALSE, 0);
  gtk_signal_connect(GTK_OBJECT(Button), "clicked",
		     GTK_SIGNAL_FUNC(Restart_clicked), NULL);
  gtk_widget_show(Button);

  Label = gtk_label_new("Hostname");
  gtk_box_pack_start(GTK_BOX(Toolbar), Label, FALSE, FALSE, 0);
  gtk_widget_show(Label);

  Entry = gtk_entry_new();
  gtk_entry_set_text(GTK_ENTRY(Entry), Hostname);
  gtk_signal_connect(GTK_OBJECT(Entry), "activate",
		     GTK_SIGNAL_FUNC(Host_activate), NULL);
  gtk_box_pack_start(GTK_BOX(Toolbar), Entry, TRUE, TRUE, 0);
  gtk_widget_show(Entry);
}

char *Report_Text[] = { "Hostname", "Loss", "Rcv", "Snt", "Best", "Avg", "Worst", NULL };
int Report_Positions[] = { 10, 240, 280, 320, 360, 400, 440, 0 };
GtkWidget *Report;
GtkWidget *ReportBody;

GtkWidget *GetRow(int index) {
  int addr;
  char str[256], *name;
  GtkWidget *Row, *Label;

  Row = gtk_fixed_new();
  
  addr = net_addr(index);
  name = "???";
  if(addr != 0) {
    name = dns_lookup(addr);
    if(!name) {
      /* Actually this is not neccesary: 
	 dns_lookup always returns a printable string */
      name = strlongip (addr);
    }
  }

  Label = gtk_label_new(name);
  gtk_fixed_put(GTK_FIXED(Row), Label, Report_Positions[0], 0);
  gtk_widget_show(Label);

  return Row;
}

GtkWidget *Scrollarea_create() {
  GtkWidget *List;
  int count;

  for(count = 0; Report_Positions[count]; count++);

  List = GTK_WIDGET(gtk_clist_new_with_titles(count, Report_Text));
  gtk_clist_set_policy(GTK_CLIST(List), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  for(count = 0; Report_Positions[count + 1]; count++) {
    gtk_clist_set_column_width(GTK_CLIST(List), count, 
			       Report_Positions[count + 1] - 
			       Report_Positions[count]);
  }
  gtk_clist_set_column_width(GTK_CLIST(List), count, 0);
  for(count = 1; Report_Positions[count]; count++) {
    gtk_clist_set_column_justification(GTK_CLIST(List), count, GTK_JUSTIFY_RIGHT);
  }

  ReportBody = List;
  return List;
}

void gtk_add_row(GtkWidget *List) {
  int at;
  GtkWidget *Row, *Label;

  Row = gtk_fixed_new();
  
  for(at = 0; Report_Positions[at] != 0; at++) {
    Label = gtk_label_new("-");
    if(at) {
      gtk_widget_set_usize(Label, 40, 0);
      gtk_label_set_justify(GTK_LABEL(Label), GTK_JUSTIFY_RIGHT);
    }
    gtk_fixed_put(GTK_FIXED(Row), Label, Report_Positions[at], 0);
    gtk_widget_show(Label);
  }

  gtk_box_pack_start(GTK_BOX(List), Row, FALSE, FALSE, 0);
  gtk_widget_show(Row);
}

void gtk_set_field(GtkCList *List, int row, int ix, char *str) {
  gtk_clist_set_text(List, row, ix, str);
}

void gtk_set_field_num(GtkCList *List, int row, int ix, char *format, int num) {
  char str[32];

  sprintf(str, format, num);
  gtk_set_field(List, row, ix, str);
}

void gtk_update_row(GtkCList *List, int row) {
  int addr;
  char str[256], *name;

  addr = net_addr(row);
  name = "???";
  if(addr != 0) {
    name = dns_lookup(addr);
    if(!name) {
      sprintf(str, "%d.%d.%d.%d", (addr >> 24) & 0xff, (addr >> 16) & 0xff, 
	      (addr >> 8) & 0xff, addr & 0xff);
      name = str;
    }
  }
  gtk_set_field(List, row, 0, name);

  gtk_set_field_num(List, row, 1, "%d%%", net_percent(row));
  gtk_set_field_num(List, row, 2, "%d", net_returned(row));  
  gtk_set_field_num(List, row, 3, "%d", net_xmit(row));
  
  gtk_set_field_num(List, row, 4, "%d", net_best(row));
  gtk_set_field_num(List, row, 5, "%d", net_avg(row));  
  gtk_set_field_num(List, row, 6, "%d", net_worst(row));
  
}

void gtk_redraw() {
  int at;
  int max = net_max();

  gtk_clist_freeze(GTK_CLIST(ReportBody));

  while(GTK_CLIST(ReportBody)->rows < max) {
    gtk_clist_append(GTK_CLIST(ReportBody), Report_Text);
  }

  while(GTK_CLIST(ReportBody)->rows > max) {
    gtk_clist_remove(GTK_CLIST(ReportBody), GTK_CLIST(ReportBody)->rows - 1);
  }

  for(at = 0; at < max; at++) {
    gtk_update_row(GTK_CLIST(ReportBody), at);
  }

  gtk_clist_thaw(GTK_CLIST(ReportBody));
}

void Window_fill(GtkWidget *Window) {
  GtkWidget *VBox;
  GtkWidget *Toolbar;
  GtkWidget *List;

  gtk_window_set_title(GTK_WINDOW(Window), "My traceroute  [v" VERSION "]");
  gtk_widget_set_usize(Window, 540, 400); 
  gtk_container_border_width(GTK_CONTAINER(Window), 10);
  VBox = gtk_vbox_new(FALSE, 10);

  Toolbar = gtk_hbox_new(FALSE, 10);
  Toolbar_fill(Toolbar);
  gtk_box_pack_start(GTK_BOX(VBox), Toolbar, FALSE, FALSE, 0);
  gtk_widget_show(Toolbar);

  List = Scrollarea_create();
  gtk_box_pack_start(GTK_BOX(VBox), List, TRUE, TRUE, 0);
  gtk_widget_show(List);
  
  gtk_container_add(GTK_CONTAINER(Window), VBox);
  gtk_widget_show(VBox);
}

void gtk_open() {
  GtkWidget *Window;
  GdkPixmap *icon;

  int argc;
  char *args[2];
  char **argv;
  argc = 1;
  argv = args;
  argv[0] = "";
  argv[1] = NULL;
  gtk_do_init(&argc, &argv);

  Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  Window_fill(Window);

  gtk_signal_connect_object(GTK_OBJECT(Window), "delete_event",
			    GTK_SIGNAL_FUNC(gtk_widget_destroy), 
			    GTK_OBJECT(Window));
  gtk_signal_connect(GTK_OBJECT(Window), "destroy",
		     GTK_SIGNAL_FUNC(Window_destroy), NULL);

  icon = gtk_load_pixmap(mtr_icon);
  gtk_widget_show(Window);
  gdk_window_set_icon(Window->window, NULL, icon, NULL);
  gdk_window_set_icon_name(Window->window, "mtr");
}

void gtk_close() {
}

int gtk_keyaction() {
  return 0;
}

gint gtk_ping(gpointer data) {
  gtk_redraw();
  net_send_batch();
  gtk_timeout_remove (tag);
  tag = gtk_timeout_add(DeltaTime*1000, gtk_ping, NULL);
  return TRUE;
}

void gtk_net_data(gpointer data, gint fd, GdkInputCondition cond) {
  net_process_return();
}

void gtk_dns_data(gpointer data, gint fd, GdkInputCondition cond) {
  dns_ack();

  gtk_redraw();
}


void gtk_loop() {
  DeltaTime = WaitTime/10;
  tag = gtk_timeout_add(DeltaTime*1000, gtk_ping, NULL);
  gdk_input_add(net_waitfd(), GDK_INPUT_READ, gtk_net_data, NULL);
  gdk_input_add(dns_waitfd(), GDK_INPUT_READ, gtk_dns_data, NULL);

  gtk_main();
}