summaryrefslogtreecommitdiff
path: root/glcddrivers/g15daemon.c
blob: f041b2922c5a748c195bb917cb89106f5617a1bb (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
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
/*
* GraphLCD driver library
*
* g15daemon.c  -  pseudo device for the g15daemon meta driver
*                   Output goes to the g15daemon which then displays it
*
* This file is released under the GNU General Public License. Refer
* to the COPYING file distributed with this package.
*
*/

#include <fcntl.h>
#include <stdio.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <poll.h>
#include <arpa/inet.h>
#include <unistd.h>

#include "common.h"
#include "config.h"

#include "g15daemon.h"

#define G15SERVER_PORT 15550
#define G15SERVER_ADDR "127.0.0.1"

#define G15_WIDTH 160
#define G15_HEIGHT 43


static int g15_send(int sock, char *buf, int len)
{
    int total = 0;
    int retval = 0;
    int bytesleft = len;

    while (total < len) {
        retval = send(sock, buf+total, bytesleft, 0);
        if (retval == -1) {
            break;
        }
        bytesleft -= retval;
        total += retval;
    }
    return retval==-1?-1:0;
}

static int g15_recv(int sock, char *buf, int len)
{
    int total = 0;
    int retval = 0;
    int bytesleft = len;

    while (total < len) {
        retval = recv(sock, buf+total, bytesleft, 0);
        if (retval < 1) {
            break;
        }
        total += retval;
        bytesleft -= retval;
    }
    return total;
}

static int open_g15_daemon()
{
    int g15screen_fd;
    struct sockaddr_in serv_addr;

    char buffer[256];

    g15screen_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (g15screen_fd < 0)
        return -1;

    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family      = AF_INET;
    inet_aton (G15SERVER_ADDR, &serv_addr.sin_addr);
    serv_addr.sin_port        = htons(G15SERVER_PORT);

    if (connect(g15screen_fd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
        return -1;

    memset(buffer,0,256);
    if (g15_recv(g15screen_fd, buffer, 16)<0)
        return -1;

    /* here we check that we're really talking to the g15daemon */
    if (strcmp(buffer,"G15 daemon HELLO") != 0)
        return -1;

    /* we want to use a pixelbuffer */
    g15_send(g15screen_fd,"GBUF",4);

    return g15screen_fd;
}


namespace GLCD
{

cDriverG15daemon::cDriverG15daemon(cDriverConfig * config)
:   config(config),
    offbuff(0),
    sockfd(-1)
{
    oldConfig = new cDriverConfig(*config);
}

cDriverG15daemon::~cDriverG15daemon()
{
    delete oldConfig;
}

int cDriverG15daemon::Init()
{
    // default values
    width = config->width;
    if (width !=G15_WIDTH)
        width = G15_WIDTH;
    height = config->height;
    if (height !=G15_HEIGHT)
        height = G15_HEIGHT;

    for (unsigned int i = 0; i < config->options.size(); i++) {
        if (config->options[i].name == "") {
        }
    }

    screensize = 6880;

    if ((sockfd = open_g15_daemon())<0)
        return -1;
    // reserve memory to draw into
    offbuff = new char[6880];

    *oldConfig = *config;

    // clear display
    Refresh(true);

    syslog(LOG_INFO, "%s: g15daemon initialized.\n", config->name.c_str());
    return 0;
}

int cDriverG15daemon::DeInit()
{
    if (offbuff);
    delete[] offbuff;
    if (-1 != sockfd)
        close(sockfd);

    return 0;
}

int cDriverG15daemon::CheckSetup()
{
    if (config->device != oldConfig->device ||
        config->port != oldConfig->port ||
        config->width != oldConfig->width ||
        config->height != oldConfig->height)
    {
        DeInit();
        Init();
        return 0;
    }

    if (config->upsideDown != oldConfig->upsideDown ||
        config->invert != oldConfig->invert)
    {
        oldConfig->upsideDown = config->upsideDown;
        oldConfig->invert = config->invert;
        return 1;
    }
    return 0;
}

void cDriverG15daemon::SetPixel(int x, int y)
{
    if (x >= width || y >= height)
        return;

    if (config->upsideDown)
    {
        x = width - 1 - x;
        y = height - 1 - y;
    }

    offbuff[x + (width * y)] = 1;
}

void cDriverG15daemon::Clear()
{
    memset(offbuff, 0, screensize);
}

void cDriverG15daemon::Set8Pixels(int x, int y, unsigned char data)
{
    int n;

    x &= 0xFFF8;

    for (n = 0; n < 8; ++n)
    {
        if (data & (0x80 >> n))      // if bit is set
            SetPixel(x + n, y);
    }
}

void cDriverG15daemon::Refresh(bool refreshAll)
{
    g15_send(sockfd, offbuff, screensize);
}

} // end of namespace