aboutsummaryrefslogtreecommitdiffstats
path: root/src/matchbox-stroke-mode.c
blob: e6fc9c5388d8a7a612356868b10acfaf3f7f1aec (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
#include "matchbox-stroke.h"


struct MBStrokeMode
{
  char           *name;
  UtilHash       *exact_match_actions; 
  MBStrokeRegex  *fuzzy_matches;

  /*
    UtilRegexps *fuzzy_matches;
  */

  MBStroke       *stroke_app;
  MBStrokeMode  *next;
};

MBStrokeMode*
mb_stroke_mode_new(MBStroke *stroke_app, const char *name)
{
  MBStrokeMode *mode = NULL;

  mode = util_malloc0(sizeof(MBStrokeMode));

  /* extact_match_actions hash */

  mode->exact_match_actions = util_hash_new();
  mode->name                = strdup(name);
  mode->stroke_app          = stroke_app;

  return mode;
}

void
mb_stroke_add_mode(MBStroke     *stroke, 
		   MBStrokeMode *mode)
{
  MBStrokeMode *mode_item = NULL;

  if (stroke->modes == NULL)
    {
      stroke->modes = mode;
      return;
    }

  mode_item = stroke->modes;

  while (mb_stroke_mode_next(mode_item) != NULL) 
    mode_item = mb_stroke_mode_next(mode_item);
 
 
  mode_item->next = mode;
}


void
mb_stroke_mode_add_exact_match(MBStrokeMode   *mode,
			       const char     *match_str,
			       MBStrokeAction *action)
{
  DBG("Adding match for mode:%s, '%s'", mode->name, match_str);
  util_hash_insert(mode->exact_match_actions, 
		   strdup(match_str), 
		   (pointer)action);
}

boolean
mb_stroke_mode_add_fuzzy_match(MBStrokeMode   *mode,
			       const char     *match_str,
			       MBStrokeAction *action)
{
  MBStrokeRegex  *last_match = mode->fuzzy_matches, *new_regex = NULL;

  DBG("Adding fuzzy match for mode:%s, '%s'", mode->name, match_str);
  
  if ((new_regex = mb_stroke_regex_new(match_str, NULL)) == NULL)
    return False; 		/* XXX return error ? */

  mb_stroke_regex_set_action(new_regex, action);

  if (last_match == NULL)
    {
      mode->fuzzy_matches = new_regex;
      return True;
    }
  
  while (mb_stroke_regex_next(last_match) != NULL)
    last_match = mb_stroke_regex_next(last_match);

  mb_stroke_regex_set_next(last_match, new_regex);

  return True;
}

MBStrokeAction*
mb_stroke_mode_match_seq(MBStrokeMode   *mode,
			 char           *match_seq)
{
  MBStrokeRegex  *reg_item =  mode->fuzzy_matches;
  MBStrokeAction *action   = NULL;

  action = util_hash_lookup(mode->exact_match_actions, match_seq);

  if (action)
    return action;

  /* fun bit */

  while (reg_item != NULL)
    {
      if (mb_stroke_regex_match(reg_item, match_seq))
	{
	  action = mb_stroke_regex_get_action(reg_item);

	  /* cache the seq in the exact hash so we dont need to 
           * hit the regexps again for this.
           *
           * XXX Does it make sense to cache this data on disk or
           *     some such for the user ?
	  */
	  mb_stroke_mode_add_exact_match(mode, match_seq, action);

	  return action;
	}

      reg_item = mb_stroke_regex_next(reg_item);
    }

  return NULL;
}

const char*
mb_stroke_mode_name(MBStrokeMode   *mode)
{
  return mode->name;
}

MBStrokeMode*
mb_stroke_mode_next(MBStrokeMode   *mode)
{
  return mode->next;
}