summaryrefslogtreecommitdiffstats
path: root/apps/examples/nx/nx_kbdin.c
blob: 66d54b04692c5c0189de8138a11b5789ddcfdcf6 (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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/****************************************************************************
 * examples/nx/nx_kbdin.c
 *
 *   Copyright (C) 2008, 2010-2011 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <debug.h>
#include <errno.h>

#include <nuttx/nx.h>
#include <nuttx/nxtk.h>
#include <nuttx/nxfonts.h>

#include "nx_internal.h"

#ifdef CONFIG_NX_KBD

/****************************************************************************
 * Definitions
 ****************************************************************************/

/* Select renderer -- Some additional logic would be required to support
 * pixel depths that are not directly addressable (1,2,4, and 24).
 */

#if CONFIG_EXAMPLES_NX_BPP == 1
#  define RENDERER nxf_convert_1bpp
#elif CONFIG_EXAMPLES_NX_BPP == 2
#  define RENDERER nxf_convert_2bpp
#elif CONFIG_EXAMPLES_NX_BPP == 4
#  define RENDERER nxf_convert_4bpp
#elif CONFIG_EXAMPLES_NX_BPP == 8
#  define RENDERER nxf_convert_8bpp
#elif CONFIG_EXAMPLES_NX_BPP == 16
#  define RENDERER nxf_convert_16bpp
#elif CONFIG_EXAMPLES_NX_BPP == 24
#  define RENDERER nxf_convert_24bpp
#elif  CONFIG_EXAMPLES_NX_BPP == 32
#  define RENDERER nxf_convert_32bpp
#else
#  error "Unsupported CONFIG_EXAMPLES_NX_BPP"
#endif

/****************************************************************************
 * Private Types
 ****************************************************************************/

/****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/****************************************************************************
 * Private Data
 ****************************************************************************/

/****************************************************************************
 * Public Data
 ****************************************************************************/

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: nxeg_fillchar
 ****************************************************************************/

static void nxeg_fillchar(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
                          FAR const struct nxeg_bitmap_s *bm)
{
  FAR void *src = (FAR void *)bm->glyph->bitmap;
  struct nxgl_rect_s intersection;
  int ret;

  /* Handle the special case of spaces which have no glyph bitmap */

  if (src)
    {
      /* Get the intersection of the redraw region and the character bitmap */

      nxgl_rectintersect(&intersection, rect, &bm->bounds);
      if (!nxgl_nullrect(&intersection))
        {
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
          ret = nxtk_bitmapwindow((NXTKWINDOW)hwnd, &intersection, (FAR const void **)&src,
                                  &bm->bounds.pt1,
                                  (unsigned int)bm->glyph->stride);
          if (ret < 0)
            {
              message("nxeg_fillchar: nxtk_bitmapwindow failed: %d\n", errno);
            }
#else
          ret = nx_bitmap((NXWINDOW)hwnd, &intersection, &src,
                          &bm->bounds.pt1,
                          (unsigned int)bm->glyph->stride);
          if (ret < 0)
            {
              message("nxeg_fillchar: nx_bitmapwindow failed: %d\n", errno);
            }
#endif
        }
    }
}

/****************************************************************************
 * Name: nxeg_renderglyph
 ****************************************************************************/

static inline FAR const struct nxeg_glyph_s *
nxeg_renderglyph(FAR struct nxeg_state_s *st,
                 FAR const struct nx_fontbitmap_s *bm, uint8_t ch)
{
  FAR struct nxeg_glyph_s *glyph = NULL;
  FAR nxgl_mxpixel_t *ptr;
#if CONFIG_EXAMPLES_NX_BPP < 8
  nxgl_mxpixel_t pixel;
#endif
  int bmsize;
  int row;
  int col;
  int ret;

  /* Make sure that there is room for another glyph */

  message("nxeg_renderglyph: ch=%02x\n", ch);
  if (st->nglyphs < NXTK_MAXKBDCHARS)
    {
      /* Allocate the glyph */

      glyph         = &st->glyph[st->nglyphs];
      glyph->code   = ch;

      /* Get the dimensions of the glyph */

      glyph->width  = bm->metric.width + bm->metric.xoffset;
      glyph->height = bm->metric.height + bm->metric.yoffset;

      /* Allocate memory to hold the glyph with its offsets */

      glyph->stride = (glyph->width * CONFIG_EXAMPLES_NX_BPP + 7) / 8;
      bmsize        =  glyph->stride * glyph->height;
      glyph->bitmap = (FAR uint8_t *)malloc(bmsize);

      if (glyph->bitmap)
        {
          /* Initialize the glyph memory to the background color */

#if CONFIG_EXAMPLES_NX_BPP < 8
          pixel  = st->color[0];
#  if CONFIG_EXAMPLES_NX_BPP == 1
          /* Pack 1-bit pixels into a 2-bits */

          pixel &= 0x01;
          pixel  = (pixel) << 1 |pixel;
#  endif
#  if CONFIG_EXAMPLES_NX_BPP < 4
          /* Pack 2-bit pixels into a nibble */

          pixel &= 0x03;
          pixel  = (pixel) << 2 |pixel;
#  endif

          /* Pack 4-bit nibbles into a byte */

          pixel &= 0x0f;
          pixel  = (pixel) << 4 | pixel;

          ptr    = (FAR nxgl_mxpixel_t *)glyph->bitmap;
          for (row = 0; row < glyph->height; row++)
            {
              for (col = 0; col < glyph->stride; col++)
                {
                  /* Transfer the packed bytes into the buffer */

                  *ptr++ = pixel;
                }
            }

#elif CONFIG_EXAMPLES_NX_BPP == 24
# error "Additional logic is needed here for 24bpp support"

#else /* CONFIG_EXAMPLES_NX_BPP = {8,16,32} */

          ptr = (FAR nxgl_mxpixel_t *)glyph->bitmap;
          for (row = 0; row < glyph->height; row++)
            {
              /* Just copy the color value into the glyph memory */

              for (col = 0; col < glyph->width; col++)
                {
                  *ptr++ = st->color[0];
                }
            }
#endif

          /* Then render the glyph into the allocated memory */

          ret = RENDERER((FAR nxgl_mxpixel_t*)glyph->bitmap,
                          glyph->height, glyph->width, glyph->stride,
                          bm, CONFIG_EXAMPLES_NX_FONTCOLOR);
          if (ret < 0)
            {
              /* Actually, the RENDERER never returns a failure */

              message("nxeg_renderglyph: RENDERER failed\n");
              free(glyph->bitmap);
              glyph->bitmap = NULL;
              glyph         = NULL;
            }
          else
            {
               /* Make it permanent */

               st->nglyphs++;
            }
        }
    }

  return glyph;
}

/****************************************************************************
 * Name: nxeg_addspace
 ****************************************************************************/

static inline FAR const struct nxeg_glyph_s *
nxeg_addspace(FAR struct nxeg_state_s *st, uint8_t ch)
{
  FAR struct nxeg_glyph_s *glyph = NULL;

  /* Make sure that there is room for another glyph */

  if (st->nglyphs < NXTK_MAXKBDCHARS)
    {
      /* Allocate the NULL glyph */

      glyph        = &st->glyph[st->nglyphs];
      memset(glyph, 0, sizeof(struct nxeg_glyph_s));

      glyph->code  = ' ';
      glyph->width = st->spwidth;

      st->nglyphs++;
    }
  return glyph;
}

/****************************************************************************
 * Name: nxeg_findglyph
 ****************************************************************************/

static FAR const struct nxeg_glyph_s *
nxeg_findglyph(FAR struct nxeg_state_s *st, uint8_t ch)
{
  int i;

  /* First, try to find the glyph in the cache of pre-rendered glyphs */

   for (i = 0; i < st->nglyphs; i++)
    {
      if (st->glyph[i].code == ch)
        {
          return &st->glyph[i];
        }
    }
  return NULL;
}

/****************************************************************************
 * Name: nxeg_getglyph
 ****************************************************************************/

static FAR const struct nxeg_glyph_s *
nxeg_getglyph(FAR struct nxeg_state_s *st, uint8_t ch)
{
  FAR const struct nxeg_glyph_s *glyph;
  FAR const struct nx_fontbitmap_s *bm;

  /* First, try to find the glyph in the cache of pre-rendered glyphs */

  glyph = nxeg_findglyph(st, ch);
  if (!glyph)
    {
      /* No, it is not cached... Does the code map to a glyph? */

      bm = nxf_getbitmap(ch, NXFONT_DEFAULT);
      if (!bm)
        {
          /* No, there is no glyph for this code.  Use space */

          glyph = nxeg_findglyph(st, ' ');
          if (!glyph)
            {
              /* There isn't fake glyph for ' ' yet... create one */

              glyph = nxeg_addspace(st, ' ');
            }
        }
      else
        {
          glyph =  nxeg_renderglyph(st, bm, ch);
        }
    }
  return glyph;
}

/****************************************************************************
 * Name: nxeg_addchar
 ****************************************************************************/

static FAR const struct nxeg_bitmap_s *
nxeg_addchar(FAR struct nxeg_state_s *st, uint8_t ch)
{
  FAR struct nxeg_bitmap_s *bm = NULL;
  FAR struct nxeg_bitmap_s *bmleft;
  nxgl_coord_t leftx;

  /* Is there space for another character on the display? */

  if (st->nchars < NXTK_MAXKBDCHARS)
    {
       /* Yes, setup the bitmap */

       bm = &st->bm[st->nchars];

       /* Find the matching glyph */

       bm->glyph = nxeg_getglyph(st, ch);
       if (!bm->glyph)
         {
           return NULL;
         }

       /* Set up the bounds for the bitmap */

       if (st->nchars <= 0)
         {
            /* The first character is one space from the left */

            leftx  = st->spwidth;
         }
       else
         {
            /* Otherwise, it is to the left of the preceding char */

            bmleft = &st->bm[st->nchars-1];
            leftx  = bmleft->bounds.pt2.x + 1;
         }

       bm->bounds.pt1.x = leftx;
       bm->bounds.pt1.y = 2;
       bm->bounds.pt2.x = leftx + bm->glyph->width - 1;
       bm->bounds.pt2.y = 2 + bm->glyph->height - 1;

       st->nchars++;
    }
  return bm;
}

/****************************************************************************
 * Name: nxeg_addchars
 ****************************************************************************/

static inline void nxeg_addchars(NXWINDOW hwnd, FAR struct nxeg_state_s *st,
                                 uint8_t nch, FAR const uint8_t *ch)
{
  FAR const struct nxeg_bitmap_s *bm;

  while (nch--)
    {
      bm = nxeg_addchar(st, *ch++);
      if (bm)
        {
          nxeg_fillchar(hwnd, &bm->bounds, bm);
        }
    }
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: nxeg_kbdin
 ****************************************************************************/

void nxeg_kbdin(NXWINDOW hwnd, uint8_t nch, FAR const uint8_t *ch,
                FAR void *arg)
{
  FAR struct nxeg_state_s *st = (FAR struct nxeg_state_s *)arg;
  message("nxeg_kbdin%d: hwnd=%p nch=%d\n", st->wnum, hwnd, nch);
  nxeg_addchars(hwnd, st, nch, ch);
}

/****************************************************************************
 * Name: nxeg_tbkbdin
 ****************************************************************************/

#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
void nxeg_tbkbdin(NXWINDOW hwnd, uint8_t nch, const uint8_t *ch, FAR void *arg)
{
  FAR struct nxeg_state_s *st = (FAR struct nxeg_state_s *)arg;
  message("nxeg_tbkbdin: ERROR -- toolbar should not received keyboard input\n");
  message("nxeg_tbkbdin%d: hwnd=%p nch=%d\n", st->wnum, hwnd, nch);
}
#endif

/****************************************************************************
 * Name: nxeg_filltext
 ****************************************************************************/

void nxeg_filltext(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
                   FAR struct nxeg_state_s *st)
{
  int i;

  /* Fill each character on the display (Only the characters within rect
   * will actually be redrawn).
   */

  for (i = 0; i < st->nchars; i++)
    {
      nxeg_fillchar(hwnd, rect, &st->bm[i]);
    }
}

#endif /* CONFIG_NX_KBD */