summaryrefslogtreecommitdiff
path: root/libxklavier/xklavier_evt_xkb.c
blob: 87142b81c6fc6868bc62be8723b98d9afc93168e (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
#include <time.h>

#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xlibint.h>

#include "xklavier_private.h"
#include "xklavier_private_xkb.h"

/**
 * XKB event handler
 */
int _XklXkbEventHandler( XEvent *xev )
{
#ifdef XKB_HEADERS_PRESENT
  int i;
  unsigned bit;
  unsigned inds;
  XkbEvent *kev = (XkbEvent*)xev;

  if( xev->type != _xklXkbEventType )
    return 0;

  if( !( _xklListenerType &
         ( XKLL_MANAGE_WINDOW_STATES | XKLL_TRACK_KEYBOARD_STATE ) ) )
    return 0;

  XklDebug( 150, "Xkb event detected\n" );
  
  switch ( kev->any.xkb_type )
  {
    /**
     * Group is changed!
     */
    case XkbStateNotify:
#define GROUP_CHANGE_MASK \
    ( XkbGroupStateMask | XkbGroupBaseMask | XkbGroupLatchMask | XkbGroupLockMask )

      XklDebug( 150,
                "XkbStateNotify detected, changes: %X/(mask %X), new group %d\n",
                kev->state.changed, GROUP_CHANGE_MASK,
                kev->state.locked_group );

      if( kev->state.changed & GROUP_CHANGE_MASK )
        _XklStateModificationHandler( GROUP_CHANGED, 
                                      kev->state.locked_group, 
                                      0, 
                                      False );
      else /* ...not interested... */
      {
        XklDebug( 200,
                  "This type of state notification is not regarding groups\n" );
        if ( kev->state.locked_group != _xklCurState.group )
          XklDebug( 0, 
                    "ATTENTION! Currently cached group %d is not equal to the current group from the event: %d\n!",
                    _xklCurState.group,
                    kev->state.locked_group );
      }

      break;

    /**
     * Indicators are changed!
     */
    case XkbIndicatorStateNotify:

      XklDebug( 150, "XkbIndicatorStateNotify\n" );

      inds = _xklCurState.indicators;

      ForPhysIndicators( i, bit ) if( kev->indicators.changed & bit )
      {
        if( kev->indicators.state & bit )
          inds |= bit;
        else
          inds &= ~bit;
      }

      _XklStateModificationHandler( INDICATORS_CHANGED, 
                                    0, 
                                    inds, 
                                    True );
      break;

    /**
     * The configuration is changed!
     */
    case XkbIndicatorMapNotify:
    case XkbControlsNotify:
    case XkbNamesNotify:
    case XkbNewKeyboardNotify:
      XklDebug( 150, "%s\n",
                _XklXkbGetXkbEventName( kev->any.xkb_type ) );
      _XklFreeAllInfo();
      _XklLoadAllInfo();
      break;

    /**
     * ...Not interested...
     */
    default:
      XklDebug( 150, "Unknown XKB event %d [%s]\n", 
                kev->any.xkb_type, _XklXkbGetXkbEventName( kev->any.xkb_type ) );
      return 0;
  }
  return 1;
#else
  return 0;
#endif
}

void _XklXkbSetIndicators( const XklState *windowState )
{
#ifdef XKB_HEADERS_PRESENT
  int i;
  unsigned bit;

  ForPhysIndicators( i,
                     bit ) if( _xklXkb->names->indicators[i] != None )
  {
    Bool status;
    status = _XklSetIndicator( i,
                               ( windowState->indicators & bit ) != 0 );
    XklDebug( 150, "Set indicator \"%s\"/%d to %d: %d\n",
              _xklIndicatorNames[i],
              _xklXkb->names->indicators[i],
              windowState->indicators & bit,
              status );
  }
#endif
}