rename esc to cmd to avoid confusion
[fbvnc.git] / fbvnc.c
blobe9efdd6f000352e1b6fbfeb8070f9d36407ed88a
1 /*
2 * fbvnc - A small linux framebuffer vnc viewer
4 * Copyright (C) 1999 Milan Pikula
5 * Copyright (C) 2009 Ali Gholami Rudi
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License, as published by the
9 * Free Software Foundation.
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <termios.h>
15 #include <sys/time.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18 #include <string.h>
20 #include "fbvnc.h"
22 static void term_setup(struct termios *ti)
24 struct termios termios;
25 char *hide = "\x1b[?25l";
26 char *clear = "\x1b[2J\x1b[H";
27 char *msg = "\t\t\t*** fbvnc ***\r\n";
29 write(STDIN_FILENO, hide, strlen(hide));
30 write(STDOUT_FILENO, clear, strlen(clear));
31 write(STDOUT_FILENO, msg, strlen(msg));
32 tcgetattr (0, &termios);
33 *ti = termios;
34 cfmakeraw(&termios);
35 tcsetattr(0, TCSANOW, &termios);
38 static void term_cleanup(struct termios *ti)
40 char *show = "\x1b[?25h";
41 tcsetattr(0, TCSANOW, ti);
42 write(STDIN_FILENO, show, strlen(show));
45 static void mainloop(int fd)
47 fd_set sel_in;
48 struct timeval tv;
49 int pending = 0;
50 int retval = 0;
51 int update = 1;
52 int redraw = 1;
53 while(1) {
54 if (must_redraw())
55 redraw = 1;
56 if (redraw || ((update || !retval) && !pending)) {
57 if (request_vnc_refresh(fd, !redraw) == -1)
58 break;
59 pending = 1;
60 update = 0;
61 redraw = 0;
63 tv.tv_sec = 0;
64 tv.tv_usec = 500000;
65 FD_ZERO(&sel_in);
66 FD_SET(fd, &sel_in);
67 FD_SET(0, &sel_in);
68 retval = select(fd + 1, &sel_in, NULL, NULL, &tv);
69 if (!retval)
70 continue;
71 if (FD_ISSET(0, &sel_in)) {
72 if (parse_kbd_in(0, fd) == -1)
73 break;
74 update = 1;
76 if (FD_ISSET(fd, &sel_in)) {
77 if (parse_vnc_in(fd) == -1)
78 break;
79 pending = 0;
84 int main(int argc, char * argv[])
86 int port = DEF_PORT;
87 char * host = "127.0.0.1";
88 struct termios ti;
89 int fd;
90 if (argc >= 2)
91 host = argv[1];
92 if (argc >= 3)
93 port = atoi(argv[2]);
94 if ((fd = vncproto_init(host, port)) == -1)
95 return -1;
96 term_setup(&ti);
98 mainloop(fd);
100 term_cleanup(&ti);
101 vncproto_free();
102 return 0;