Added support for DE200C VFD
[lcdproc-de200c.git] / clients / lcdvc / vc_link.c
blob653eee6ddefedfdc509e792d1526d34c96f98bab
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <errno.h>
9 #include "lcdvc.h"
10 #include "vc_link.h"
11 #include "shared/report.h"
12 #include "shared/sockets.h"
14 int vcs0, vcsa;
15 unsigned short vc_width = 0, vc_height = 0;
16 unsigned short vc_cursor_x = 0, vc_cursor_y = 0;
17 char *vc_buf = NULL;
20 int open_vcs(void)
22 /* Open the /dev/vcsX and /dev/vcsaX devices */
23 vcs0 = open(vcs_device, O_RDONLY);
24 if (vcs0 < 0) {
25 report(RPT_ERR, "Could not open %s: %s", vcs_device, strerror(errno));
26 return -1;
28 vcsa = open(vcsa_device, O_RDONLY);
29 if (vcsa < 0) {
30 report(RPT_ERR, "Could not open %s: %s", vcsa_device, strerror(errno));
31 return -1;
33 return 0;
37 int read_vcdata(void)
39 unsigned short new_vc_height;
40 unsigned short new_vc_width;
42 int bytes_read;
43 unsigned char buf[20];
45 /* Read size and cursor position from /dev/vcsa */
46 lseek(vcsa, 0, SEEK_SET);
47 bytes_read = read(vcsa, buf, 4);
48 if (bytes_read != 4) {
49 report(RPT_ERR, "Could not read from %s", vcsa_device);
50 return -1;
52 new_vc_height = buf[0];
53 new_vc_width = buf[1];
54 vc_cursor_x = buf[2];
55 vc_cursor_y = buf[3];
57 /* Screen resize or initial buffer allocation ? */
58 if ((new_vc_width != vc_width) || (new_vc_height != vc_height)) {
59 vc_width = new_vc_width;
60 vc_height = new_vc_height;
62 if (vc_width * vc_height > 0) {
63 vc_buf = realloc(vc_buf, vc_width * vc_height);
65 if (vc_buf == NULL) {
66 report(RPT_ERR, "malloc failure: %s", strerror(errno));
67 return -1;
69 memset(vc_buf, ' ', vc_width * vc_height);
73 /* Read characters from /dev/cvs0 */
74 lseek(vcs0, 0, SEEK_SET);
75 bytes_read = read(vcs0, vc_buf, vc_width * vc_height);
76 if (bytes_read != vc_width * vc_height) {
77 report(RPT_ERR, "Could not read from %s", vcs_device);
78 return -1;
80 return 0;