File: mousebutton.c

package info (click to toggle)
libgiigic 1.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,000 kB
  • ctags: 854
  • sloc: sh: 9,160; ansic: 7,438; makefile: 275
file content (303 lines) | stat: -rw-r--r-- 7,487 bytes parent folder | download | duplicates (3)
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
/* $Id: mousebutton.c,v 1.12 2005/09/15 15:03:17 cegger Exp $
******************************************************************************

   This LibGIC module is a very simple parser for PtrButtonPress Events.

   Copyright (C) 1999 Andrew Apted	[andrew@ggi-project.org]

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************************
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <ggi/internal/gic.h>
#include <ggi/internal/gic_debug.h>

#include <ggi/gg.h>
#include <ggi/internal/gg_replace.h>	/* for snprintf() */


typedef struct mousebutton_data
{
	int button;
  
} MouseButtonData;


static int mbutton_check(gic_handle_t hand, gic_recognizer *ctrl,gii_event *event,
			gic_feature *feature,int recnum);

static int mbutton_get_name(gic_handle_t hand, gic_recognizer *ctrl,
			char *string,size_t maxlen);

static int mbutton_write_pvtdata(gic_handle_t hand, gic_recognizer *ctrl,
			char *string,int maxlen);

static int mbutton_read_pvtdata(gic_handle_t hand, gic_recognizer *ctrl,
				const char *string);

static void mbutton_free_pvtdata(gic_handle_t hand, gic_recognizer *ctrl);

static int mbutton_train(gic_handle_t hand, gic_recognizer **ctrl, gii_event *event);

static int mbutton_check_conflict(gic_handle_t hand, gic_recognizer *ctrl,
			gic_recognizer *ctrl2);

static int mbutton_get_opposite(gic_handle_t hand, gic_recognizer *recognizer,
			gic_recognizer **opposite);



static gic_recognizerdriver mycontrols =
{
	"MouseButton",
	mbutton_check,
	mbutton_get_name,
	mbutton_write_pvtdata,
	mbutton_read_pvtdata,
	mbutton_free_pvtdata,
	mbutton_train,
	mbutton_check_conflict,
	mbutton_get_opposite
};


static int getbutton(gic_handle_t hand, gic_recognizer *ctrl, gii_event *event,gic_feature *feature,int recnum)
{
	MouseButtonData *but = ctrl->privdata;

	DPRINT_LIBS("MouseButton: Button%s %d [want %d].\n",
		(event->any.type == evPtrButtonPress) ?
		"Press" : "Release", event->pbutton.button, but->button);

	if (event->pbutton.button != (unsigned)but->button) {
		return 0;
	}

	gicFeatureActivate(hand, feature,(event->any.type == evPtrButtonPress) ?
		GIC_STATE_MAX : GIC_STATE_MIN , 0, recnum);
	return 1;
}

static int mbutton_check(gic_handle_t hand, gic_recognizer *ctrl,gii_event *event,
			gic_feature *feature,int recnum)
{
	DPRINT_LIBS("MouseButton: Check with %p,%p.\n",ctrl,event);

	if ((event->any.type == evPtrButtonPress) ||
	    (event->any.type == evPtrButtonRelease)) {
		return getbutton(hand, ctrl, event, feature, recnum);
	}

	return 0;
}

static int mbutton_register(gic_handle_t hand, gic_recognizer **ctrl,MouseButtonData *but,gic_state state)
{
	gic_recognizer *new_rec, *rl;
	MouseButtonData *new_but;
	
	for (rl=*ctrl; rl != NULL; rl=rl->next) {

		MouseButtonData *cur = (MouseButtonData *) rl->privdata;

		if (cur->button == but->button) {

			/* Duplicate entry -- just update the confidence
			 * value.
			 */
			 if (state > rl->confidence) {
			 	rl->confidence = state;
				/* !!! MOVE UP */
			}

			return 1;
		}
	}

	new_rec = malloc(sizeof(gic_recognizer));
	if (new_rec == NULL) return GGI_ENOMEM;
	new_but = malloc(sizeof(MouseButtonData));
	if (new_but == NULL) {
		free(new_rec);
		return GGI_ENOMEM;
	}
	
	memcpy(new_but, but, sizeof(MouseButtonData));

	new_rec->driver     = &mycontrols;
	new_rec->confidence = state;
	new_rec->privdata   = new_but;
	
	gicRecognizerTrainAdd(hand, ctrl, new_rec);
	
	return 1;
}


static struct trainingstate {
	int got_button;  /* -1 when no current button */
} trainingstate;

static int mbutton_train(gic_handle_t hand, gic_recognizer **ctrl, gii_event *event)
{
	int rc;
	DPRINT_LIBS("MouseButton: Training with %p,%p.\n",ctrl,event);

	rc=0;
	if (event == NULL) {

		trainingstate.got_button = -1;

		DPRINT_LIBS("MouseButton: Initialized training state.\n");

		return 0;
	}

	DPRINT_LIBS("MouseButton: Analyzing event ...\n");

	if (event->any.type == evPtrButtonPress) {

		trainingstate.got_button = event->pbutton.button;

		DPRINT_LIBS("MouseButton: Remembered button %d ...\n",
			    trainingstate.got_button);
		
		return 0;
	}

	if ((event->any.type == evPtrButtonRelease) &&
	    (event->pbutton.button = trainingstate.got_button)) {

		MouseButtonData but;

		but.button = trainingstate.got_button;

		rc+=mbutton_register(hand, ctrl, &but, GIC_STATE_MAX);

		DPRINT_LIBS("MouseButton: Registered button %d ...\n",
			    trainingstate.got_button);
		
		return rc;
	}

	return rc;
}
 
static int mbutton_write_pvtdata(gic_handle_t hand, gic_recognizer *ctrl,
			char *string,int maxlen)
{
	MouseButtonData *but = ctrl->privdata;

	if (maxlen < 20) {
		*string='\0';
		return GGI_ENOSPACE;
	}

	sprintf(string, "%d", but->button);

	return 0;
}

static int mbutton_read_pvtdata(gic_handle_t hand, gic_recognizer *ctrl,
				const char *string)
{
	MouseButtonData *but;

	but = ctrl->privdata = malloc(sizeof(MouseButtonData));
	if (but == NULL) {
		return GGI_ENOMEM;
	}
	
	sscanf(string, "%i", &but->button);

	return 0;
}

static void mbutton_free_pvtdata(gic_handle_t hand, gic_recognizer *ctrl)
{
	MouseButtonData *but = ctrl->privdata;

	free(but);
	ctrl->privdata = NULL;
	
	return;
}

static int mbutton_get_name(gic_handle_t hand, gic_recognizer *ctrl,
			char *string,size_t maxlen)
{
	MouseButtonData *but = ctrl->privdata;

	char namebuf[40];
	
	if (maxlen>20) maxlen=20;

	if (maxlen<=1) {
		/* The user must be Joking ... */
	} else if (maxlen<=5) {
		snprintf(namebuf, 40, "M%.*s%d", (int)(maxlen-2), "But",but->button);
	} else if (maxlen<=9) {
		snprintf(namebuf, 40, "%.*sBut%d", (int)(maxlen-4), "Mouse", but->button);
	} else {
		snprintf(namebuf, 40, "Mouse%.*s%d", (int)(maxlen-6), "Button", but->button);
	}
	
	ggstrlcpy(string, namebuf, maxlen);

	return 0;
}

static int mbutton_check_conflict(gic_handle_t hand, gic_recognizer *ctrl,
			gic_recognizer *ctrl2)
{
	MouseButtonData *but1 = ctrl ->privdata;
	MouseButtonData *but2 = ctrl2->privdata;

	if (ctrl == ctrl2) {
		return GIC_C_ISSAME;
	}

	if (ctrl->driver != ctrl2->driver) {
		return GIC_C_NOCONFLICT;
	}

	if (but1->button == but2->button) {
		return GIC_C_ISSAME;
	}

	return GIC_C_NOCONFLICT;
}

static int mbutton_get_opposite(gic_handle_t hand, gic_recognizer *recognizer,
			gic_recognizer **opposite)
{
	return GGI_ENOMATCH;	/* FIXME */
}


EXPORTFUNC gic_recognizerdriver *GICdl_mousebutton(void);

gic_recognizerdriver *GICdl_mousebutton(void)
{
	return &mycontrols;
}