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
|
/* $Id: mouse_matcher.c,v 1.21 2009-09-05 17:32:19 sand Exp $
*
* Copyright (C) 2007-2009 FAUmachine Team <info@faumachine.org>.
* This program is free software. You can redistribute it and/or modify it
* under the terms of the GNU General Public License, either version 2 of
* the License, or (at your option) any later version. See COPYING.
*/
#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include "glue-shm.h"
#include "glue-log.h"
#include "mouse_matcher.h"
#define COMP "mouse_matcher"
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
struct match_coords {
uint16_t x;
uint16_t y;
bool visible;
void * _cpssp;
};
struct cpssp {
/* Config */
/* Ports */
struct sig_string *port_pointerdir;
struct sig_boolean *port_match_state;
struct sig_boolean *port_event;
struct sig_integer *port_x;
struct sig_integer *port_y;
struct sig_match *port_slot0;
struct sig_match *port_slot1;
struct sig_match *port_slot2;
struct sig_match *port_slot3;
struct sig_match *port_slot4;
struct sig_match *port_slot5;
struct sig_match *port_slot6;
struct sig_match *port_slot7;
struct sig_match *port_slot8;
struct sig_match *port_slot9;
struct sig_match *port_slot10;
struct sig_match *port_slot11;
/* Signals */
/* State */
/** maximum used slot number */
unsigned short num_slots_used;
/** matching active */
bool active;
/** match coordinates of each slot */
struct match_coords matches[12];
/** last stored value */
struct match_coords old_coords;
/* Processes */
};
static void
mouse_matcher_match_resolve(
const struct cpssp *cpssp,
uint16_t *x,
uint16_t *y,
bool *visible
)
{
int i;
*x = 0;
*y = 0;
*visible = false;
for (i = 0; i < ARRAY_SIZE(cpssp->matches); i++) {
if (cpssp->matches[i].visible
&& i < cpssp->num_slots_used) {
*visible = true;
*x = cpssp->matches[i].x;
*y = cpssp->matches[i].y;
return;
}
}
}
static void
mouse_matcher_send_result(struct cpssp *cpssp)
{
uint16_t x;
uint16_t y;
bool visible;
if (! cpssp->active) {
return;
}
mouse_matcher_match_resolve(cpssp, &x, &y, &visible);
if ( visible != cpssp->old_coords.visible
|| x != cpssp->old_coords.x
|| y != cpssp->old_coords.y) {
/* important: set x and y first, active next and finally
* event.
* Reason: x and y coordinates should be valid when active
* or event hits expect
*/
sig_integer_set(cpssp->port_x, cpssp, x);
sig_integer_set(cpssp->port_y, cpssp, y);
sig_boolean_set(cpssp->port_match_state, cpssp, visible);
sig_boolean_set(cpssp->port_event, cpssp, 1);
sig_boolean_set(cpssp->port_event, cpssp, 0);
cpssp->old_coords.x = x;
cpssp->old_coords.y = y;
cpssp->old_coords.visible = visible;
}
}
static void
mouse_matcher_match_event(
void *s,
bool visible,
uint16_t x, uint16_t y,
uint16_t w, uint16_t h
)
{
struct match_coords *m = (struct match_coords *)s;
struct cpssp *cpssp = (struct cpssp *)m->_cpssp;
m->x = x;
m->y = y;
m->visible = visible;
mouse_matcher_send_result(cpssp);
}
static int
#ifdef DARWIN
match_ppm_png_name(struct dirent *d)
#else
match_ppm_png_name(const struct dirent *d)
#endif
{
char *str = NULL;
assert(d != NULL);
str = strrchr(d->d_name, '.');
if (! str) {
return 0;
}
return (strcmp(str, ".ppm") == 0) || (strcmp(str, ".png") == 0);
}
static struct sig_match*
mouse_matcher_get_slot(struct cpssp *cpssp, unsigned int idx)
{
switch(idx) {
case 0:
return cpssp->port_slot0;
case 1:
return cpssp->port_slot1;
case 2:
return cpssp->port_slot2;
case 3:
return cpssp->port_slot3;
case 4:
return cpssp->port_slot4;
case 5:
return cpssp->port_slot5;
case 6:
return cpssp->port_slot6;
case 7:
return cpssp->port_slot7;
case 8:
return cpssp->port_slot8;
case 9:
return cpssp->port_slot9;
case 10:
return cpssp->port_slot10;
case 11:
return cpssp->port_slot11;
default:
assert(false);
return NULL;
}
}
static void
mouse_matcher_activate_matching(struct cpssp *cpssp, const char *dir)
{
int n;
unsigned int idx = 0;
struct dirent **namelist = NULL;
char fullname[500];
n = scandir(dir, &namelist, match_ppm_png_name, alphasort);
if (n < 0) {
faum_log(FAUM_LOG_WARNING, COMP, "",
"Couldn't open cursor image directory %s\n",
dir);
return;
}
if (n == 0) {
/* no images */
faum_log(FAUM_LOG_WARNING, COMP, "",
"No cursor images in directory %s\n",
dir);
return;
}
/* got mouse patterns */
while (n--) {
assert(namelist[n] != NULL);
snprintf(fullname, sizeof(fullname), "%s/%s",
dir, namelist[n]->d_name);
fullname[sizeof(fullname) - 1] = '\0';
if (ARRAY_SIZE(cpssp->matches) <= idx) {
faum_log(FAUM_LOG_WARNING, COMP, "",
"ignoring mouse watch %s: out of slots\n",
fullname);
free(namelist[n]);
continue;
}
sig_match_add_match(mouse_matcher_get_slot(cpssp, idx),
&cpssp->matches[idx],
fullname);
idx++;
free(namelist[n]);
}
free(namelist);
cpssp->num_slots_used = idx;
}
static void
mouse_matcher_deactivate_matching(struct cpssp *cpssp)
{
unsigned int i;
for (i = 0; i < cpssp->num_slots_used; i++) {
sig_match_remove_match(mouse_matcher_get_slot(cpssp, i),
&cpssp->matches[i]);
cpssp->matches[i].visible = false;
cpssp->matches[i].x = 0;
cpssp->matches[i].y = 0;
}
mouse_matcher_send_result(cpssp);
cpssp->num_slots_used = 0;
}
static void
mouse_matcher_pointerdir_set(void *_cpssp, const char *str)
{
struct cpssp *cpssp = (struct cpssp *)_cpssp;
if (*str) {
if (cpssp->active) {
faum_log(FAUM_LOG_WARNING, COMP, "",
"matching already active.\n");
return;
}
mouse_matcher_activate_matching(cpssp, str);
cpssp->active = true;
} else {
if (! cpssp->active) {
faum_log(FAUM_LOG_WARNING, COMP, "",
"matching already not active.\n");
return;
}
mouse_matcher_deactivate_matching(cpssp);
cpssp->active = false;
}
}
void *
mouse_matcher_create(
const char *name,
struct sig_manage *port_manage,
struct sig_string *port_pointerdir,
struct sig_boolean *port_match_state,
struct sig_boolean *port_event,
struct sig_integer *port_x,
struct sig_integer *port_y,
struct sig_match *port_slot0,
struct sig_match *port_slot1,
struct sig_match *port_slot2,
struct sig_match *port_slot3,
struct sig_match *port_slot4,
struct sig_match *port_slot5,
struct sig_match *port_slot6,
struct sig_match *port_slot7,
struct sig_match *port_slot8,
struct sig_match *port_slot9,
struct sig_match *port_slot10,
struct sig_match *port_slot11
)
{
static struct sig_string_funcs setf = {
.set = mouse_matcher_pointerdir_set
};
static struct sig_match_funcs matchf = {
.event = mouse_matcher_match_event
};
struct cpssp *cpssp;
int i;
cpssp = malloc(sizeof(*cpssp));
assert(cpssp);
cpssp->active = false;
cpssp->num_slots_used = 0;
memset(cpssp->matches, 0, sizeof(cpssp->matches));
memset(&cpssp->old_coords, 0, sizeof(cpssp->old_coords));
cpssp->port_pointerdir = port_pointerdir;
cpssp->port_match_state = port_match_state;
cpssp->port_event = port_event;
cpssp->port_x = port_x;
cpssp->port_y = port_y;
cpssp->port_slot0 = port_slot0;
cpssp->port_slot1 = port_slot1;
cpssp->port_slot2 = port_slot2;
cpssp->port_slot3 = port_slot3;
cpssp->port_slot4 = port_slot4;
cpssp->port_slot5 = port_slot5;
cpssp->port_slot6 = port_slot6;
cpssp->port_slot7 = port_slot7;
cpssp->port_slot8 = port_slot8;
cpssp->port_slot9 = port_slot9;
cpssp->port_slot10 = port_slot10;
cpssp->port_slot11 = port_slot11;
for (i = 0; i < ARRAY_SIZE(cpssp->matches); i++) {
cpssp->matches[i]._cpssp = cpssp;
}
/* Call */
sig_match_connect(cpssp->port_slot0, &cpssp->matches[0], &matchf);
sig_match_connect(cpssp->port_slot1, &cpssp->matches[1], &matchf);
sig_match_connect(cpssp->port_slot2, &cpssp->matches[2], &matchf);
sig_match_connect(cpssp->port_slot3, &cpssp->matches[3], &matchf);
sig_match_connect(cpssp->port_slot4, &cpssp->matches[4], &matchf);
sig_match_connect(cpssp->port_slot5, &cpssp->matches[5], &matchf);
sig_match_connect(cpssp->port_slot6, &cpssp->matches[6], &matchf);
sig_match_connect(cpssp->port_slot7, &cpssp->matches[7], &matchf);
sig_match_connect(cpssp->port_slot8, &cpssp->matches[8], &matchf);
sig_match_connect(cpssp->port_slot9, &cpssp->matches[9], &matchf);
sig_match_connect(cpssp->port_slot10, &cpssp->matches[10], &matchf);
sig_match_connect(cpssp->port_slot11, &cpssp->matches[11], &matchf);
/* Out */
sig_integer_connect_out(port_x, cpssp, 0);
sig_integer_connect_out(port_y, cpssp, 0);
/* In */
sig_string_connect(port_pointerdir, cpssp, &setf);
return cpssp;
}
void
mouse_matcher_destroy(void *_cpssp)
{
struct cpssp *cpssp = _cpssp;
free(cpssp);
}
|