aboutsummaryrefslogtreecommitdiffstats
path: root/vnc-client.c
blob: a4c833e9f88251dffbd4566bb28d6fad759a239a (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <signal.h>

#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>

#include "vnc.h"

/* ------------------------------------------------------------------ */

static const char *progname;

static void usage(FILE *fp)
{
    fprintf(fp,
	    "This is a simple vnc client\n"
	    "\n"
	    "usage: %s [options] display\n"
	    "options:\n"
	    "   -h         Print this text.\n"
	    "   -d         Enable debug output.\n"
	    "   -p         Show mouse pointer.\n"
	    "   -f         Start in fullscreen mode.\n"
	    "   -c         Close on disconnect.\n"
	    "   -u <user>  Set username.\n"
	    "   -s <pass>  Set password.\n"
	    "\n"
	    "-- \n"
	    "(c) 2006 Gerd Hoffmann <kraxel@redhat.com>\n",
            progname);
}

int
main(int argc, char *argv[])
{
    unsigned long vnc_flags = VNC_FLAG_STANDALONE;
    char hostname[65], *username = NULL, *password = NULL;
    int port;
    int debug = 0;
    int c;

    progname = strrchr(argv[0], '/');
    if (progname == NULL)
        progname = argv[0];
    else
        ++progname;

    gtk_init(&argc, &argv);
    while ((c = getopt(argc, argv, "hdpfcu:s:")) != -1) {
        switch (c) {
	case 'd':
	    debug++;
	    break;
	case 'p':
	    vnc_flags |= VNC_FLAG_SHOW_MOUSE;
	    break;
	case 'f':
	    vnc_flags |= VNC_FLAG_FULLSCREEN;
	    break;
	case 'c':
	    vnc_flags |= VNC_FLAG_DISCONNECT_CLOSE;
	    break;
        case 'u':
            username = optarg;
            break;
        case 's':
            password = optarg;
            break;
        case 'h':
            usage(stdout);
            exit(0);
        case '?':
            fprintf(stderr, "Try `%s -h' for more information.\n", progname);
            exit(1);
        default:
            fprintf(stderr, "%s: getopt returned %d?\n", progname, c);
            exit(1);
        }
    }

    if (optind == argc) {
        *hostname = '\0';
        port = 5901;
    }
    else if (optind == argc - 1) {
        char *arg = argv[optind];
        int n, displayno;

        if (2 == sscanf(arg, "%64[^:]:%d%n", hostname, &displayno, &n))
	    port = displayno + 5900;
	else if (2 == sscanf(arg, "%64[^:]::%d%n", hostname, &port, &n))
            ;
        else {
            fprintf(stderr, "%s: malformed display name '%s'\n", progname, arg);
            exit(1);
        }
        if (arg[n] != '\0') {
            fprintf(stderr, "%s: malformed display name '%s'\n", progname, arg);
            exit(1);
        }
    }
    else {
        fprintf(stderr, "%s: wrong number of arguments.\n", progname);
        fprintf(stderr, "Try `%s -h' for more information.\n", progname);
        exit(1);
    }

    if (debug)
        printf("Connecting to host '%s' port %d\n", hostname, port);

    if (NULL == vnc_open(strlen(hostname) ? hostname : NULL,
			 port, vnc_flags, debug, username, password))
	exit(1);
    if (password) {
        while (*password) {
            *(password++) = '*';
        }
    }

    gtk_main();
    fprintf(stderr,"bye...\n");
    exit(0);
}