[go: up one dir, main page]

Menu

[r79]: / releases / 1.1 / test.c  Maximize  Restore  History

Download this file

173 lines (143 with data), 5.1 kB

  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
/*
* $Id: slog.h 6 2008-12-15 10:56:05Z wimalopaan $
*
* scanbd - KMUX scanner button daemon
*
* Copyright (C) 2008 Wilhelm Meier (wilhelm.meier@fh-kl.de)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "scanbd.h"
#include "scanbuttond_loader.h"
#include "scanbuttond_wrapper.h"
#define DEF_POLL_DELAY 333000L
#define MIN_POLL_DELAY 1000L
#define DEF_RETRY_DELAY 2000000L
#define MIN_RETRY_DELAY 10000L
#define BUF_SIZE 256
cfg_t* cfg = NULL;
static char* connection_names[NUM_CONNECTIONS] =
{ "none", "libusb" };
backend_t* backend;
static long poll_delay;
static long retry_delay;
static int killed = 0;
char* scanbtnd_get_connection_name(int connection)
{
return connection_names[connection];
}
// Ensures a graceful exit on SIGHUP/SIGTERM/SIGINT/SIGSEGV
void sighandler(int i)
{
killed = 1;
slog(SLOG_INFO, "received signal %d", i);
scbtn_shutdown();
exit(i == SIGTERM ? EXIT_SUCCESS : EXIT_FAILURE);
}
void list_devices(scanner_t* devices)
{
scanner_t* dev = devices;
while (dev != NULL) {
slog(SLOG_INFO, "found scanner: vendor=\"%s\", product=\"%s\", connection=\"%s\", sane_name=\"%s\"",
dev->vendor, dev->product, scanbtnd_get_connection_name(dev->connection),
backend->scanbtnd_get_sane_device_descriptor(dev));
dev = dev->next;
}
}
int main()
{
int button;
int result;
scanner_t* scanners;
scanner_t* scanner;
slog_init("test");
debug = true;
debug_level = 7;
scanbtnd_set_libdir("./scanbuttond/backends");
if (scanbtnd_loader_init() != 0) {
slog(SLOG_INFO, "Could not initialize module loader!\n");
exit(EXIT_FAILURE);
}
backend = scanbtnd_load_backend("meta");
if (!backend) {
slog(SLOG_INFO, "Unable to load backend library\n");
scanbtnd_loader_exit();
exit(EXIT_FAILURE);
}
if (backend->scanbtnd_init() != 0) {
slog(SLOG_ERROR, "Error initializing backend. Terminating.");
exit(EXIT_FAILURE);
}
scanners = backend->scanbtnd_get_supported_devices();
if (scanners == NULL) {
slog(SLOG_WARN, "no known scanner found yet, " \
"waiting for device to be attached");
}
list_devices(scanners);
signal(SIGTERM, &sighandler);
signal(SIGHUP, &sighandler);
signal(SIGINT, &sighandler);
signal(SIGSEGV, &sighandler);
signal(SIGCHLD, SIG_IGN);
slog(SLOG_INFO, "scanbuttond started");
// main loop
while (killed == 0) {
if (scanners == NULL) {
slog(SLOG_DEBUG, "rescanning devices...");
backend->scanbtnd_rescan();
scanners = backend->scanbtnd_get_supported_devices();
if (scanners == NULL) {
slog(SLOG_DEBUG, "no supported devices found. rescanning in a few seconds...");
usleep(retry_delay);
continue;
}
scanners = backend->scanbtnd_get_supported_devices();
continue;
}
scanner = scanners;
while (scanner != NULL) {
result = backend->scanbtnd_open(scanner);
if (result != 0) {
slog(SLOG_WARN, "scanbtnd_open failed, error code: %d", result);
if (result == -ENODEV) {
// device has been disconnected, force re-scan
slog(SLOG_INFO, "scanbtnd_open returned -ENODEV, device rescan will be performed");
scanners = NULL;
usleep(retry_delay);
break;
}
usleep(retry_delay);
break;
}
slog(SLOG_INFO, "get");
button = backend->scanbtnd_get_button(scanner);
slog(SLOG_INFO, "value: %d", button);
backend->scanbtnd_close(scanner);
if ((button > 0) && (button != scanner->lastbutton)) {
slog(SLOG_INFO, "button %d has been pressed.", button);
scanner->lastbutton = button;
}
if ((button == 0) && (scanner->lastbutton > 0)) {
slog(SLOG_INFO, "button %d has been released.", scanner->lastbutton);
scanner->lastbutton = button;
}
scanner = scanner->next;
}
usleep(poll_delay);
}
slog(SLOG_WARN, "exited main loop!?!");
scbtn_shutdown();
exit(EXIT_SUCCESS);
}