summaryrefslogtreecommitdiff
path: root/src/ask.c
blob: 5392911bb2a1d6da20e7f1070c48e8de6ca3d948 (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
/*
 * Copyright 2009 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "program.h"
#include "util/u_network.h"


gboolean ask_connect(struct program *p)
{
	uint16_t port = p->ask.port;
	int socket = -1;

	while (socket < 0) {
		/* after ten tries quit */
		if (port >= p->ask.port + 10)
			return false;

		socket = u_socket_connect(p->ask.host, port++);
	}

	/* store the actual port, minus one */
	p->ask.port = --port;

	p->rbug.socket = socket;
	p->rbug.con = rbug_from_socket(socket);

	rbug_glib_io_watch(p);

	main_window_create(p);

	return true;
}

static void connect(GtkWidget *widget, gpointer data)
{
	struct program *p = (struct program *)data;
	char *host;
	uint16_t port;

	(void)widget;

	host = gtk_editable_get_chars(GTK_EDITABLE(p->ask.entry_host), 0, -1);
	port = (uint16_t)gtk_adjustment_get_value(p->ask.adjustment_port);

	p->ask.host = host;
	p->ask.port = port;

	if (!ask_connect(p))
		return;

	gtk_widget_destroy(p->ask.window);

	p->ask.window = NULL;
	p->ask.entry_host = NULL;
	p->ask.entry_port = NULL;
}

static void destroy(GtkWidget *widget, gpointer data)
{
	struct program *p = (struct program *)data;
	(void)widget;

	if (!p->main.window)
		main_quit(p);
}

void ask_window_create(struct program *p)
{
	GtkBuilder *builder;
	GtkWidget *window;
	GtkWidget *button;
	GtkWidget *quit;
	GtkWidget *entry_host;
	GtkWidget *entry_port;
	GtkAdjustment *adjustment_port;

	builder = gtk_builder_new();

	gtk_builder_add_from_file(builder, "res/ask.xml", NULL);
	gtk_builder_connect_signals(builder, NULL);

	window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
	button = GTK_WIDGET(gtk_builder_get_object(builder, "connect"));
	quit = GTK_WIDGET(gtk_builder_get_object(builder, "quit"));
	entry_host = GTK_WIDGET(gtk_builder_get_object(builder, "entry_host"));
	entry_port = GTK_WIDGET(gtk_builder_get_object(builder, "entry_port"));
	adjustment_port = GTK_ADJUSTMENT(gtk_builder_get_object(builder, "adjustment_port"));

	/* manualy set up signals */
	g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(connect), p);
	g_signal_connect(G_OBJECT(quit), "clicked", G_CALLBACK(destroy), p);
	g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), p);

	gtk_widget_show(window);

	p->ask.window = window;
	p->ask.entry_host = entry_host;
	p->ask.entry_port = entry_port;
	p->ask.adjustment_port = adjustment_port;

	gtk_adjustment_set_value(adjustment_port, 13370);
}