File: mx5000.c

package info (click to toggle)
lcdproc 0.5.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,064 kB
  • sloc: ansic: 59,645; sh: 1,740; perl: 681; makefile: 417
file content (406 lines) | stat: -rw-r--r-- 8,705 bytes parent folder | download
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/** \file server/drivers/mx5000.c
 * LCDd \c mx5000 driver for the Logitech MX5000 keyboard.
 */

/*
    Author: Christian Jodar (tian@gcstar.org)

    Copyright (C) 2008 Christian Jodar

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 */

/*  Based on mx5000 tools http://home.gna.org/mx5000tools/ */

/*
 * Driver status
 * 02/01/2008: First test version
 *
 *
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <libmx5000/mx5000.h>
#include <libmx5000/mx5000screencontent.h>

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "lcd.h"
#include "mx5000.h"
#include "shared/report.h"


/* Internal functions */

/*
 * Prepare a static screen
 */
MODULE_EXPORT int
mx5000_init (Driver *drvthis, char *args)
{
    PrivateData *p;

    /* Allocate and store private data */
    p = (PrivateData *) calloc(1, sizeof(PrivateData));
    if (p == NULL)
        return -1;
    if (drvthis->store_private_ptr(drvthis, p))
        return -1;

    /* initialize private data */
    p->fd = -1;
    p->sc = NULL;

    debug(RPT_INFO, "mx5000: init(%p,%s)", drvthis, args);

    /* Read config file */
    /* Which hiddev device should be used */
    strncpy(p->device, drvthis->config_get_string(drvthis->name, "Device", 0, DEFAULT_DEVICE), sizeof(p->device));
    p->device[sizeof(p->device)-1] = '\0';
    debug(RPT_INFO,"%s: using Device %s", drvthis->name, p->device);

    p->wait = drvthis->config_get_int(drvthis->name, "WaitAfterRefresh", 0, DEFAULT_WAIT);

    p->fd = mx5000_open_path(p->device);
    if (p->fd == -1)
    {
        report(RPT_ERR, "%s: open(%s) failed (%s)",
                drvthis->name, p->device, strerror(errno));
        return -1;
    }
    p->sc = mx5000_sc_new_static();
    if (! p->sc)
    {
        report(RPT_ERR, "%s: creation of screen failed (%s)",
                drvthis->name, strerror(errno));
        return -1;
    }

    report(RPT_DEBUG, "%s: init() done", drvthis->name);

    return 0;
}


/*
 * Clean-up
 */
MODULE_EXPORT void
mx5000_close (Driver *drvthis)
{
    PrivateData *p = drvthis->private_data;

    if (p != NULL)
    {
        // Just to be sure we flushed current command
        sleep(1);
        mx5000_reset(p->fd);
        if (p->sc)
	{
	    mx5000_sc_free(p->sc);
	}
        if (p->fd >= 0)
	{
	    close(p->fd);
	}
	free(p);
    }
    drvthis->store_private_ptr(drvthis, NULL);
}


/*
 * Returns the display width
 */
MODULE_EXPORT int
mx5000_width (Driver *drvthis)
{
    return SCREEN_WIDTH;
}


/*
 * Returns the display height
 */
MODULE_EXPORT int
mx5000_height (Driver *drvthis)
{
    return SCREEN_HEIGHT;
}


/*
 * Returns the display's character cell width
 */
MODULE_EXPORT int
mx5000_cellwidth(Driver *drvthis)
{
    return CELL_WIDTH;
}


/*
 * Returns the display's character cell height
 */
MODULE_EXPORT int
mx5000_cellheight(Driver *drvthis)
{
    return CELL_HEIGHT;
}


/*
 * Flushes all output to the lcd...
 */
MODULE_EXPORT void
mx5000_flush (Driver *drvthis)
{
    PrivateData *p = drvthis->private_data;

    /*
      We could only update the needed part. But it would be hard to really
      figure out what has been changed
    */

    if (p->changed)
    {
        mx5000_sc_send(p->sc, p->fd);
        p->changed = 0;
        // The screen seems to be unable to react quickly
        usleep(1000 * p->wait);
    }
}

/*
 * Prints a character on the lcd display, at position (x,y).
 * The upper-left is (1,1), and the lower right should be (16,4).
 */
MODULE_EXPORT void
mx5000_chr (Driver *drvthis, int x, int y, char c)
{
    PrivateData *p = drvthis->private_data;
    char str[2];
    // The position should be given in pixels
    int px, py;

    if ((x <= 0) || (y <= 0) || (x > SCREEN_WIDTH) || (y > SCREEN_HEIGHT))
    {
        return;
    }

    px = (x - 1) * CELL_WIDTH;
    py = y * CELL_HEIGHT;

    str[0] = c;
    str[1] = 0;
    mx5000_sc_add_text(p->sc, str, -1, STATIC, py, px);
    p->changed = 1;
}

/*
 * Prints a string on the lcd display, at position (x,y).  The
 * upper-left is (1,1), and the lower right should be (16,4).
 */
MODULE_EXPORT void
mx5000_string (Driver *drvthis, int x, int y, const char string[])
{
    PrivateData *p = drvthis->private_data;
    int actual_len;
    // The position should be given in pixels
    int px, py;

    if ((x <= 0) || (y <= 0) || (x > SCREEN_WIDTH) || (y > SCREEN_HEIGHT))
    {
        return;
    }

    actual_len = strlen(string);
    // If the string is too long, it is cut
    if ((actual_len + x - 1) > SCREEN_WIDTH)
    {
        actual_len = SCREEN_WIDTH - x + 1;
    }

    px = (x - 1) * CELL_WIDTH;
    py = y * CELL_HEIGHT;

    mx5000_sc_add_text(p->sc, string, actual_len, STATIC, py, px);
    p->changed = 1;
    //printf("Added %s on %d,%d\n",string,x,y);
}

/*
 * Sets the backlight on or off.
 * Not supported
 */
MODULE_EXPORT void
mx5000_backlight (Driver *drvthis, int on)
{
}

/*
 * Draws a horizontal bar to the right.
 */
MODULE_EXPORT void
mx5000_hbar (Driver *drvthis, int x, int y, int len, int promille, int options)
{
/* x and y are the start position of the bar.
 * The bar by default grows in the 'right' direction
 * (other direction not yet implemented).
 * len is the number of characters that the bar is long at 100%
 * promille is the number of promilles (0..1000) that the bar should be filled.
 */
    PrivateData *p = drvthis->private_data;
    // The position should be given in pixels
    int px, py;
    //printf("Bar : %d, %d, %d, %d, %d\n", x, y, len, promille, options);
    px = (x - 1) * CELL_WIDTH;
    py = y * CELL_HEIGHT;

    mx5000_sc_add_progress_bar(p->sc,
			       promille * len / 1000,
			       len,
			       STATIC,
			       py,
			       px);
    p->changed = 1;
}

MODULE_EXPORT void
mx5000_num (Driver *drvthis, int x, int num)
{
    PrivateData *p = drvthis->private_data;
    int px, py;
    char text[10];
    px = (x - 1) * CELL_WIDTH;
    py = 33;

    if (num < 10)
    {
        sprintf(text, "%d", num);
    }
    else
    {
        sprintf(text, ":");
	px -= CELL_WIDTH;
    }

    //printf("Display %s on %d, %d\n", text, px, py);

    mx5000_sc_add_text(p->sc, text, -1, STATICHUGE, py, px);
}

MODULE_EXPORT int
mx5000_icon(Driver *drvthis, int x, int y, int icon)
{
    PrivateData *p = drvthis->private_data;
    int px, py;
    enum display_icon mx5000_icon = 0;

    px = (x - 1) * CELL_WIDTH;
    py = y * CELL_HEIGHT;

    switch (icon)
    {
        case ICON_ARROW_UP:
	  mx5000_icon = ARROW_UP;
	  break;
        case ICON_ARROW_DOWN:
	  mx5000_icon = ARROW_DOWN;
	  break;
        case ICON_ARROW_LEFT:
	  mx5000_icon = TRIANGLE_LEFT;
	  break;
        case ICON_ARROW_RIGHT:
	  mx5000_icon = TRIANGLE_RIGHT;
	  break;
        case ICON_PAUSE:
	  mx5000_icon = PAUSE1;
	  break;
        case ICON_PLAY:
	  mx5000_icon = TRIANGLE_RIGHT;
	  break;
        case ICON_NEXT:
	  mx5000_icon = NEXTTRACK1;
	  break;
        case ICON_REC:
	  mx5000_icon = CIRCLE;
	  break;
    }
    if (mx5000_icon)
    {
        mx5000_sc_add_icon(p->sc, mx5000_icon, STATIC, py, px);
        return 0;
    }
    else
    {

        return -1; /* Let the core do other icons */
    }
}

MODULE_EXPORT void
mx5000_heartbeat(Driver *drvthis, int state)
{
}

/*
 * Gets number of custom chars (always NUM_CCs)
 */
MODULE_EXPORT int
mx5000_get_free_chars(Driver *drvthis)
{
    return 0;
}

/*
 * Clears the LCD screen
 */
MODULE_EXPORT void
mx5000_clear (Driver *drvthis)
{
    PrivateData *p = drvthis->private_data;

    // No way yet (as of 0.1.1) to clear the screen in libmx5000
    if (p->sc)
    {
        mx5000_sc_free(p->sc);
    }
    p->sc = mx5000_sc_new_static();
    if (! p->sc)
    {
        report(RPT_ERR, "%s: creation of screen failed (%s)",
	       drvthis->name, strerror(errno));
    }
    p->changed = 1;
}


MODULE_EXPORT const char *
mx5000_get_info (Driver *drvthis)
{
    PrivateData *p = drvthis->private_data;

    memset(p->info, '\0', sizeof(p->info));
    strcpy(p->info, "Logitech MX 5000 Driver");

    return p->info;
}