Skip to content

Commit 01384cf

Browse files
committed
Merge branch 'wip-source-first' into 'master'
Libinsane: normalizers/source_nodes: set the source as soon as options are requested Closes openpaperwork#1 See merge request World/OpenPaperwork/libinsane!14
2 parents d9f7a95 + 26c5418 commit 01384cf

3 files changed

Lines changed: 95 additions & 20 deletions

File tree

subprojects/libinsane/src/normalizers/source_nodes.c

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,6 @@ static enum lis_error lis_sn_dev_get_options(
239239
}
240240

241241

242-
static enum lis_error lis_sn_src_get_options(
243-
struct lis_item *self, struct lis_option_descriptor ***out_descs
244-
)
245-
{
246-
// do not return any option ; that would be redundant with normalizer
247-
// 'all_opts_on_all_sources'
248-
static const struct lis_option_descriptor *descs[] = { NULL };
249-
LIS_UNUSED(self);
250-
*out_descs = (struct lis_option_descriptor **)descs;
251-
return LIS_OK;
252-
}
253-
254-
255242
static enum lis_error set_source(struct lis_sn_item_private *private)
256243
{
257244
struct lis_option_descriptor **opts = NULL;
@@ -294,6 +281,34 @@ static enum lis_error set_source(struct lis_sn_item_private *private)
294281
}
295282

296283

284+
static enum lis_error lis_sn_src_get_options(
285+
struct lis_item *self, struct lis_option_descriptor ***out_descs
286+
)
287+
{
288+
enum lis_error err;
289+
struct lis_sn_item_private *private = LIS_SN_ITEM_PRIVATE(self);
290+
291+
// XXX(Jflesch): HP drivers + sane backend 'net' (+ difference of
292+
// versions):
293+
// This combination is very sensitive: Source must be the first option
294+
// set. This should be a good time to set it.
295+
err = set_source(private);
296+
if (LIS_IS_ERROR(err)) {
297+
lis_log_warning(
298+
"setting source has failed --> scan_start() failed:"
299+
" 0x%x, %s",
300+
err, lis_strerror(err)
301+
);
302+
}
303+
304+
// do not return any option ; that would be redundant with normalizer
305+
// 'all_opts_on_all_sources'
306+
static const struct lis_option_descriptor *descs[] = { NULL };
307+
*out_descs = (struct lis_option_descriptor **)descs;
308+
return LIS_OK;
309+
}
310+
311+
297312
static enum lis_error lis_sn_scan_start(struct lis_item *self, struct lis_scan_session **session)
298313
{
299314
struct lis_sn_item_private *private_item = LIS_SN_ITEM_PRIVATE(self);
@@ -308,7 +323,7 @@ static enum lis_error lis_sn_scan_start(struct lis_item *self, struct lis_scan_s
308323

309324
err = set_source(private_item);
310325
if (LIS_IS_ERROR(err)) {
311-
lis_log_error("Setting source has failed --> scan_start() failed: 0x%X, %s",
326+
lis_log_error("setting source has failed --> scan_start() failed: 0x%x, %s",
312327
err, lis_strerror(err));
313328
return err;
314329
}

subprojects/libinsane/tests/tests_normalizer_source_nodes.c

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,62 @@ static void tests_scan_start(void)
179179
}
180180

181181

182+
static void tests_get_options(void)
183+
{
184+
enum lis_error err;
185+
struct lis_item *item = NULL;
186+
struct lis_item **children = NULL;
187+
struct lis_option_descriptor **opts = NULL;
188+
struct lis_option_descriptor **opts_src = NULL;
189+
int source_opt_idx;
190+
union lis_value value;
191+
192+
LIS_ASSERT_EQUAL(tests_sn_init(), 0);
193+
194+
err = lis_api_normalizer_source_nodes(g_dumb, &g_sn);
195+
LIS_ASSERT_EQUAL(err, LIS_OK);
196+
197+
err = g_sn->get_device(g_sn, LIS_DUMB_DEV_ID_FIRST, &item);
198+
LIS_ASSERT_EQUAL(err, LIS_OK);
199+
200+
err = item->get_options(item, &opts);
201+
LIS_ASSERT_EQUAL(err, LIS_OK);
202+
203+
for (source_opt_idx = 0 ; opts[source_opt_idx] != NULL ; source_opt_idx++) {
204+
if (strcasecmp(opts[source_opt_idx]->name, OPT_NAME_SOURCE) == 0) {
205+
break;
206+
}
207+
}
208+
LIS_ASSERT_NOT_EQUAL(opts[source_opt_idx], NULL);
209+
210+
err = opts[source_opt_idx]->fn.get_value(opts[source_opt_idx], &value);
211+
LIS_ASSERT_EQUAL(err, LIS_OK);
212+
LIS_ASSERT_EQUAL(strcmp(value.string, OPT_VALUE_SOURCE_FLATBED), 0);
213+
214+
err = item->get_children(item, &children);
215+
LIS_ASSERT_EQUAL(err, LIS_OK);
216+
217+
LIS_ASSERT_NOT_EQUAL(children[0], NULL);
218+
LIS_ASSERT_EQUAL(children[0]->name, OPT_VALUE_SOURCE_FLATBED);
219+
LIS_ASSERT_NOT_EQUAL(children[1], NULL);
220+
LIS_ASSERT_EQUAL(children[1]->name, OPT_VALUE_SOURCE_ADF);
221+
LIS_ASSERT_EQUAL(children[2], NULL);
222+
223+
/* should trigger a change of source to make sure we get the correct parameters
224+
*/
225+
err = children[1]->get_options(children[1], &opts_src);
226+
LIS_ASSERT_EQUAL(err, LIS_OK);
227+
228+
err = opts[source_opt_idx]->fn.get_value(opts[source_opt_idx], &value);
229+
LIS_ASSERT_EQUAL(err, LIS_OK);
230+
LIS_ASSERT_EQUAL(strcmp(value.string, OPT_VALUE_SOURCE_ADF), 0);
231+
232+
item->close(item);
233+
234+
LIS_ASSERT_EQUAL(tests_sn_clean(), 0);
235+
}
236+
237+
182238
int register_tests(void)
183239
{
184240
CU_pSuite suite = NULL;
@@ -190,7 +246,8 @@ int register_tests(void)
190246
}
191247

192248
if (CU_add_test(suite, "tests_source_nodes()", tests_source_nodes) == NULL
193-
|| CU_add_test(suite, "tests_scan_start()", tests_scan_start) == NULL) {
249+
|| CU_add_test(suite, "tests_scan_start()", tests_scan_start) == NULL
250+
|| CU_add_test(suite, "tests_get_options()", tests_get_options) == NULL) {
194251
fprintf(stderr, "CU_add_test() has failed\n");
195252
return 0;
196253
}

subprojects/libinsane/tests/tests_workaround_cache.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ static void test_cache_get_value(void)
179179
LIS_ASSERT_NOT_EQUAL(opts, NULL);
180180
LIS_ASSERT_NOT_EQUAL(opts[0], NULL);
181181

182-
LIS_ASSERT_EQUAL(lis_dumb_get_nb_get(g_dumb), 0);
182+
// source node normalizer tried to set the source --> cache
183+
// workaround made a call to get_value() first and saw the
184+
// correct value was already set
185+
LIS_ASSERT_EQUAL(lis_dumb_get_nb_get(g_dumb), 1);
183186
err = opts[0]->fn.get_value(opts[0], &value);
184187
LIS_ASSERT_TRUE(LIS_IS_OK(err));
185188
LIS_ASSERT_EQUAL(strcmp(value.string, OPT_VALUE_SOURCE_FLATBED), 0);
@@ -222,7 +225,7 @@ static void test_cache_set_value(void)
222225
LIS_ASSERT_NOT_EQUAL(opts, NULL);
223226
LIS_ASSERT_NOT_EQUAL(opts[0], NULL);
224227

225-
LIS_ASSERT_EQUAL(lis_dumb_get_nb_get(g_dumb), 0);
228+
LIS_ASSERT_EQUAL(lis_dumb_get_nb_get(g_dumb), 1);
226229
LIS_ASSERT_EQUAL(lis_dumb_get_nb_set(g_dumb), 0);
227230
value.string = OPT_VALUE_SOURCE_ADF;
228231
err = opts[0]->fn.set_value(opts[0], value, &set_flags);
@@ -274,7 +277,7 @@ static void test_cache_set_value_2(void)
274277
LIS_ASSERT_NOT_EQUAL(opts[0], NULL);
275278
LIS_ASSERT_NOT_EQUAL(opts[1], NULL);
276279

277-
LIS_ASSERT_EQUAL(lis_dumb_get_nb_get(g_dumb), 0);
280+
LIS_ASSERT_EQUAL(lis_dumb_get_nb_get(g_dumb), 1);
278281
LIS_ASSERT_EQUAL(lis_dumb_get_nb_set(g_dumb), 0);
279282
value.string = OPT_VALUE_SOURCE_ADF;
280283
err = opts[1]->fn.set_value(opts[1], value, &set_flags);
@@ -284,14 +287,14 @@ static void test_cache_set_value_2(void)
284287
LIS_SET_FLAG_MUST_RELOAD_OPTIONS
285288
);
286289
// cache gets the current value before setting it
287-
LIS_ASSERT_EQUAL(lis_dumb_get_nb_get(g_dumb), 1);
290+
LIS_ASSERT_EQUAL(lis_dumb_get_nb_get(g_dumb), 2);
288291
LIS_ASSERT_EQUAL(lis_dumb_get_nb_set(g_dumb), 1);
289292

290293
// since we got flag 'reload_options', this get_value() will go through
291294
err = opts[1]->fn.get_value(opts[1], &value);
292295
LIS_ASSERT_TRUE(LIS_IS_OK(err));
293296
LIS_ASSERT_EQUAL(strcmp(value.string, OPT_VALUE_SOURCE_ADF), 0);
294-
LIS_ASSERT_EQUAL(lis_dumb_get_nb_get(g_dumb), 2);
297+
LIS_ASSERT_EQUAL(lis_dumb_get_nb_get(g_dumb), 3);
295298
LIS_ASSERT_EQUAL(lis_dumb_get_nb_set(g_dumb), 1);
296299

297300
device->close(device);

0 commit comments

Comments
 (0)