aboutsummaryrefslogtreecommitdiffstats
path: root/src/matchbox-keyboard-key.c
blob: 5f92251d103ed1c2bbac757febc81d7ac83f4b4e (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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#include "matchbox-keyboard.h"

#define MBKB_N_KEY_STATES 5

typedef struct MBKeyboardKeyFace
{
  MBKeyboardKeyFaceType  type;

  union
  {
    void          *image;
    unsigned char *str;
  } u;
} 
MBKeyboardKeyFace;

typedef struct MBKeyboardKeyAction
{
  MBKeyboardKeyActionType  type;
  union 
  {
    unsigned char         *glyph;
    KeySym                 keysym;
    MBKeyboardKeyModType   type;
  } u;
} 
MBKeyboardKeyAction;


/* A key can have 5 different 'states' */

typedef struct MBKeyboardKeyState
{
  MBKeyboardKeyAction action;
  MBKeyboardKeyFace   face;

} MBKeyboardKeyState;

struct MBKeyboardKey
{
  MBKeyboard            *kbd;
  MBKeyboardKeyState    *states[N_MBKeyboardKeyStateTypes];
  MBKeyboardRow         *row;

  int                    alloc_x, alloc_y, alloc_width, alloc_height;

  int                    extra_width_pad;  /* via win resizes */
  int                    extra_height_pad;

  boolean                obeys_caps;
  boolean                fill;	     /* width fills avialble space */
  int                    req_uwidth; /* unit width in 1/1000's */
  boolean                is_blank;   /* 'blank' keys are spacers */
  boolean                extended;   /* only show in landscape */

  MBKeyboardStateType    sets_kbdstate; /* needed */
};

static void
_mb_kbd_key_init_state(MBKeyboardKey           *key,
		       MBKeyboardKeyStateType   state)
{
  key->states[state] = util_malloc0(sizeof(MBKeyboardKeyState));
}

MBKeyboardKey*
mb_kbd_key_new(MBKeyboard *kbd)
{
  MBKeyboardKey *key = NULL;
  int            i;

  key      = util_malloc0(sizeof(MBKeyboardKey));
  key->kbd = kbd;

  for (i=0; i<N_MBKeyboardKeyStateTypes; i++)
    key->states[i] = NULL;

  return key;
}

void
mb_kbd_key_set_obey_caps(MBKeyboardKey  *key, boolean obey)
{
  key->obeys_caps = obey;
}

boolean
mb_kbd_key_get_obey_caps(MBKeyboardKey  *key)
{
  return key->obeys_caps;
}

void
mb_kbd_key_set_req_uwidth(MBKeyboardKey  *key, int uwidth)
{
  key->req_uwidth = uwidth;
}

int
mb_kbd_key_get_req_uwidth(MBKeyboardKey  *key)
{
  return key->req_uwidth;
}

void
mb_kbd_key_set_fill(MBKeyboardKey  *key, boolean fill)
{
  MARK();
  key->fill = fill;
}

boolean
mb_kbd_key_get_fill(MBKeyboardKey  *key)
{
  return key->fill;
}

void
mb_kbd_key_set_blank(MBKeyboardKey  *key, boolean blank)
{
  key->is_blank = blank;
}

boolean
mb_kbd_key_is_blank(MBKeyboardKey  *key)
{
  return key->is_blank;
}


void
mb_kbd_key_set_geometry(MBKeyboardKey  *key,
			int x,
			int y,
			int width,
			int height)
{
  if (x != -1)
    key->alloc_x = x;

  if (y != -1)
    key->alloc_y = y;

  if (width != -1)
    key->alloc_width = width;

  if (height != -1)
    key->alloc_height = height;
}

int 
mb_kbd_key_abs_x(MBKeyboardKey *key) 
{ 
  return mb_kbd_row_x(key->row) + key->alloc_x;
}

int 
mb_kbd_key_abs_y(MBKeyboardKey *key) 
{ 
  return mb_kbd_row_y(key->row) + key->alloc_y;
}

int 
mb_kbd_key_x(MBKeyboardKey *key) 
{ 
  return key->alloc_x;
}

int 
mb_kbd_key_y(MBKeyboardKey *key) 
{ 
  return key->alloc_y;
}

int 
mb_kbd_key_width(MBKeyboardKey *key) 
{ 
  return key->alloc_width;
}

int 
mb_kbd_key_height(MBKeyboardKey *key) 
{ 
  return key->alloc_height;
}

void
mb_kbd_key_set_extra_width_pad(MBKeyboardKey  *key, int pad)
{
  key->alloc_width -= key->extra_width_pad;
  key->extra_width_pad = pad;
  key->alloc_width += key->extra_width_pad;
}

void
mb_kbd_key_set_extra_height_pad(MBKeyboardKey  *key, int pad)
{
  key->alloc_height -= key->extra_height_pad;
  key->extra_height_pad = pad;
  key->alloc_height += key->extra_height_pad;
}

int
mb_kbd_key_get_extra_height_pad(MBKeyboardKey  *key)
{
  return key->extra_height_pad;
}

int
mb_kbd_key_get_extra_width_pad(MBKeyboardKey  *key)
{
  return key->extra_width_pad;
}

void
mb_kbd_key_set_extended(MBKeyboardKey  *key, boolean extend)
{
  key->extended = extend;
}

boolean
mb_kbd_key_get_extended(MBKeyboardKey  *key)
{
  return key->extended;
}


/* URG Nasty - some stuf should be public-ish */
void 
mb_kbd_key_set_row(MBKeyboardKey *key, MBKeyboardRow *row) 
{ 
  key->row = row;
}


boolean
mb_kdb_key_has_state(MBKeyboardKey           *key,
		     MBKeyboardKeyStateType   state)
{
  return (key->states[state] != NULL);
}

void
mb_kbd_key_set_glyph_face(MBKeyboardKey           *key,
			  MBKeyboardKeyStateType   state,
			  const unsigned char     *glyph)
{
  if (key->states[state] == NULL)
    _mb_kbd_key_init_state(key, state);

  key->states[state]->face.type    = MBKeyboardKeyFaceGlyph;
  key->states[state]->face.u.str   = strdup(glyph);
}

const unsigned char*
mb_kbd_key_get_glyph_face(MBKeyboardKey           *key,
			  MBKeyboardKeyStateType   state)
{
  if (key->states[state] 
      && key->states[state]->face.type == MBKeyboardKeyFaceGlyph)
    {
      return key->states[state]->face.u.str;
    }
  return NULL;
}

void
mb_kbd_key_set_image_face(MBKeyboardKey           *key,
			  MBKeyboardKeyStateType   state,
			  void                    *image)
{

  if (key->states[state] == NULL)
    _mb_kbd_key_init_state(key, state);

  key->states[state]->face.type    = MBKeyboardKeyFaceImage;
  key->states[state]->face.u.image = image;
}

void
mb_kbd_key_set_char_action(MBKeyboardKey           *key,
			   MBKeyboardKeyStateType   state,
			   const unsigned char     *glyphs)
{
  if (key->states[state] == NULL)
    _mb_kbd_key_init_state(key, state);
  
  key->states[state]->action.type = MBKeyboardKeyActionGlyph;
  key->states[state]->action.u.glyph = strdup(glyphs);
}

const unsigned char*
mb_kbd_key_get_char_action(MBKeyboardKey           *key,
			   MBKeyboardKeyStateType   state)
{
  if (key->states[state] 
      && key->states[state]->action.type == MBKeyboardKeyActionGlyph)
    return key->states[state]->action.u.glyph;

  return NULL;
}

void
mb_kbd_key_set_keysym_action(MBKeyboardKey           *key,
			     MBKeyboardKeyStateType   state,
			     KeySym                   keysym)
{
  if (key->states[state] == NULL)
    _mb_kbd_key_init_state(key, state);

  key->states[state]->action.type = MBKeyboardKeyActionXKeySym;
  key->states[state]->action.u.keysym = keysym;
}

KeySym
mb_kbd_key_get_keysym_action(MBKeyboardKey           *key,
			     MBKeyboardKeyStateType   state)
{
  if (key->states[state] 
      && key->states[state]->action.type == MBKeyboardKeyActionXKeySym)
    return key->states[state]->action.u.keysym;

  return None;
}


void
mb_kbd_key_set_modifer_action(MBKeyboardKey          *key,
			      MBKeyboardKeyStateType  state,
			      MBKeyboardKeyModType    type)
{
  if (key->states[state] == NULL)
    _mb_kbd_key_init_state(key, state);

  key->states[state]->action.type = MBKeyboardKeyActionModifier;
  key->states[state]->action.u.type   = type;
}

MBKeyboardKeyModType 
mb_kbd_key_get_modifer_action(MBKeyboardKey          *key,
			      MBKeyboardKeyStateType  state)
{
  if (key->states[state] 
      && key->states[state]->action.type == MBKeyboardKeyActionModifier)
    return key->states[state]->action.u.type;

  return 0;
}


MBKeyboardKeyFaceType
mb_kbd_key_get_face_type(MBKeyboardKey           *key,
			 MBKeyboardKeyStateType   state)
{
  if (key->states[state]) 
    return key->states[state]->face.type;

  return 0;
}


MBKeyboardKeyActionType
mb_kbd_key_get_action_type(MBKeyboardKey           *key,
			   MBKeyboardKeyStateType   state)
{
  if (key->states[state]) 
    return key->states[state]->action.type;

  return 0;
}


void
mb_kbd_key_press(MBKeyboardKey *key)
{
  /* XXX what about state handling XXX */
  MBKeyboardKeyStateType state;
  int                    flags = 0;
  boolean                queue_full_kbd_redraw = False;

  if (mb_kbd_key_is_blank(key))
    return;

  state = mb_kbd_keys_current_state(key->kbd);

  if (mb_kbd_has_state(key->kbd, MBKeyboardStateCaps)
      && mb_kbd_key_get_obey_caps(key))
    state = MBKeyboardKeyStateShifted;

  /* XXX below fakekey mods probably better in ui */

  if (state == MBKeyboardKeyStateShifted)
    flags |= FAKEKEYMOD_SHIFT; 	/* does fakekey actually need this ? */

  if (mb_kbd_has_state(key->kbd, MBKeyboardStateControl))
    flags |= FAKEKEYMOD_CONTROL;

  if (mb_kbd_has_state(key->kbd, MBKeyboardStateAlt))
    flags |= FAKEKEYMOD_ALT;

  if (!mb_kdb_key_has_state(key, state))
    {
      if (state == MBKeyboardKeyStateNormal)
	return;  /* keys should at least have a normal state */
      else
        state = MBKeyboardKeyStateNormal;
    }

  switch (mb_kbd_key_get_action_type(key, state))
    {
    case MBKeyboardKeyActionGlyph:
      {
	const unsigned char *key_char;

	if ((key_char = mb_kbd_key_get_char_action(key, state)) != NULL)
	  {
	    mb_kbd_ui_send_press(key->kbd->ui, key_char, flags);
	    mb_kbd_set_held_key(key->kbd, key);
	  }
	break;


      }
    case MBKeyboardKeyActionXKeySym:
      {
	KeySym ks;
	if ((ks = mb_kbd_key_get_keysym_action(key, state)) != None)
	  {
	    mb_kbd_ui_send_keysym_press(key->kbd->ui, ks, flags);
	    mb_kbd_set_held_key(key->kbd, key);
	  }
	break;
      }
    case MBKeyboardKeyActionModifier:
      {
	
	switch ( mb_kbd_key_get_modifer_action(key, state) )
	  {
	  case MBKeyboardKeyModShift:
	    mb_kbd_toggle_state(key->kbd, MBKeyboardStateShifted);
	    queue_full_kbd_redraw = True;
	    break;
	  case MBKeyboardKeyModMod1:
	    mb_kbd_toggle_state(key->kbd, MBKeyboardStateMod1);
	    queue_full_kbd_redraw = True;
	    break;
	  case MBKeyboardKeyModMod2:
	    mb_kbd_toggle_state(key->kbd, MBKeyboardStateMod2);
	    queue_full_kbd_redraw = True;
	    break;
	  case MBKeyboardKeyModMod3:
	    mb_kbd_toggle_state(key->kbd, MBKeyboardStateMod3);
	    queue_full_kbd_redraw = True;
	    break;
	  case MBKeyboardKeyModCaps:
	    mb_kbd_toggle_state(key->kbd, MBKeyboardStateCaps);
	    queue_full_kbd_redraw = True;
	    break;
          case MBKeyboardKeyModControl:
	    mb_kbd_toggle_state(key->kbd, MBKeyboardStateControl);
	    break;
	  case MBKeyboardKeyModAlt:
	    mb_kbd_toggle_state(key->kbd, MBKeyboardStateAlt);
	    break;
	  default:
	    DBG("unknown modifier action");
	    break;
	  }

	/* we dont actually have to send a key sym here - but should we ? 
         *
         * Also we dont set a held key, as we've changed the keyboard 
         * state instead.
	*/
	break;
      }

    default:
      break;
    }  
  
  if (queue_full_kbd_redraw)
    mb_kbd_redraw(key->kbd);
  else
    mb_kbd_redraw_key(key->kbd, key);
}

boolean 
mb_kbd_key_is_held(MBKeyboard *kbd, MBKeyboardKey *key)
{
  MBKeyboardKeyStateType  state;

  if (mb_kbd_get_held_key(key->kbd) ==  key)
    return True;

  /* XXX below should probably go into own func */

  state = mb_kbd_keys_current_state(kbd); 

  if (!mb_kdb_key_has_state(key, state))
    {
      if (state == MBKeyboardKeyStateNormal)
	return False;  /* keys should at least have a normal state */
      else
        state = MBKeyboardKeyStateNormal;
    }

  if (mb_kbd_key_get_action_type(key, state) == MBKeyboardKeyActionModifier)
    {
	switch ( mb_kbd_key_get_modifer_action(key, state) )
	  {
	  case MBKeyboardKeyModShift:
	    if (mb_kbd_has_state(kbd, MBKeyboardStateShifted))
	      return True;
	    break;
	  case MBKeyboardKeyModMod1:
	    if (mb_kbd_has_state(kbd, MBKeyboardStateMod1))
	      return True;
	    break;
	  case MBKeyboardKeyModMod2:
	    if (mb_kbd_has_state(kbd, MBKeyboardStateMod2))
	      return True;
	    break;
	  case MBKeyboardKeyModMod3:
	    if (mb_kbd_has_state(kbd, MBKeyboardStateMod3))
	      return True;
	    break;
	  case MBKeyboardKeyModCaps:
	    if (mb_kbd_has_state(kbd, MBKeyboardStateCaps))
	      return True;
	    break;
          case MBKeyboardKeyModControl:
	    if (mb_kbd_has_state(kbd, MBKeyboardStateControl))
	      return True;
	    break;
	  case MBKeyboardKeyModAlt:
	    if (mb_kbd_has_state(kbd, MBKeyboardStateAlt))
	      return True;
	    break;
	  default:
	    DBG("unknown modifier action");
	    break;
	  }
    }

  return False;
}

void
mb_kbd_key_release(MBKeyboard *kbd)
{
  MBKeyboardKey *key = mb_kbd_get_held_key(kbd);

  mb_kbd_set_held_key(kbd, NULL);

  if (key)
    {
      boolean queue_full_kbd_redraw = False;

      if (mb_kbd_key_get_action_type(key, MBKeyboardKeyStateNormal) != MBKeyboardKeyActionModifier)
	{
	  if (mb_kbd_has_any_state(kbd))
	    {
	      mb_kbd_remove_state(kbd, (MBKeyboardStateShifted|
					MBKeyboardStateMod1|
					MBKeyboardStateMod2|
					MBKeyboardStateMod3|
					MBKeyboardStateControl|
					MBKeyboardStateAlt));
	      queue_full_kbd_redraw = True;
	    }
	}

      if (queue_full_kbd_redraw)
	mb_kbd_redraw(key->kbd);
      else
	mb_kbd_redraw_key(key->kbd, key);

      mb_kbd_ui_send_release(kbd->ui);
    }
}

void
mb_kbd_key_dump_key(MBKeyboardKey *key)
{
  int i;

  char state_lookup[][32] =
    {
      /* MBKeyboardKeyStateNormal  */ "Normal",
      /* MBKeyboardKeyStateShifted */ "Shifted" ,
      /* MBKeyboardKeyStateMod1,    */ "Mod1" ,
      /* MBKeyboardKeyStateMod2,    */ "Mod2" ,
      /* MBKeyboardKeyStateMod3,    */ "Mod3" 
    };

  fprintf(stderr, "-------------------------------\n");
  fprintf(stderr, "Dumping info for key at %p\n", key);

  for (i = 0; i < N_MBKeyboardKeyStateTypes; i++)
    {
      if (mb_kdb_key_has_state(key, i))
	{
	  fprintf(stderr, "state : %s\n", state_lookup[i]);
	  fprintf(stderr, "\tface type %i", mb_kbd_key_get_face_type(key, i));

	  if (mb_kbd_key_get_face_type(key, i) == MBKeyboardKeyFaceGlyph)
	    {
	      fprintf(stderr, ", showing '%s'", 
		      mb_kbd_key_get_glyph_face(key , i));
	    }

	  fprintf(stderr, "\n");

	  fprintf(stderr, "\taction type %i\n", 
		  mb_kbd_key_get_action_type(key, i));

	}
    }

  fprintf(stderr, "-------------------------------\n");

}