diff options
Diffstat (limited to 'backend/escl/escl_capabilities.c')
-rw-r--r-- | backend/escl/escl_capabilities.c | 194 |
1 files changed, 133 insertions, 61 deletions
diff --git a/backend/escl/escl_capabilities.c b/backend/escl/escl_capabilities.c index 690ff1e..fdd5cfe 100644 --- a/backend/escl/escl_capabilities.c +++ b/backend/escl/escl_capabilities.c @@ -45,7 +45,7 @@ struct cap * \fn static SANE_String_Const convert_elements(SANE_String_Const str) * \brief Function that converts the 'color modes' of the scanner (color/gray) to be understood by SANE. * - * \return SANE_VALUE_SCAN_MODE_GRAY / SANE_VALUE_SCAN_MODE_COLOR ; NULL otherwise + * \return SANE_VALUE_SCAN_MODE_GRAY / SANE_VALUE_SCAN_MODE_COLOR / SANE_VALUE_SCAN_MODE_LINEART; NULL otherwise */ static SANE_String_Const convert_elements(SANE_String_Const str) @@ -54,6 +54,10 @@ convert_elements(SANE_String_Const str) return (SANE_VALUE_SCAN_MODE_GRAY); else if (strcmp(str, "RGB24") == 0) return (SANE_VALUE_SCAN_MODE_COLOR); +#if(defined HAVE_POPPLER_GLIB) + else if (strcmp(str, "BlackAndWhite1") == 0) + return (SANE_VALUE_SCAN_MODE_LINEART); +#endif return (NULL); } @@ -137,7 +141,7 @@ memory_callback_c(void *contents, size_t size, size_t nmemb, void *userp) char *str = realloc(mem->memory, mem->size + realsize + 1); if (str == NULL) { - fprintf(stderr, "not enough memory (realloc returned NULL)\n"); + DBG(10, "not enough memory (realloc returned NULL)\n"); return (0); } mem->memory = str; @@ -175,48 +179,61 @@ find_nodes_c(xmlNode *node) * \return 0 */ static int -find_valor_of_array_variables(xmlNode *node, capabilities_t *scanner) +find_valor_of_array_variables(xmlNode *node, capabilities_t *scanner, int type) { const char *name = (const char *)node->name; - if (strcmp(name, "ColorMode") == 0) - scanner->ColorModes = char_to_array(scanner->ColorModes, &scanner->ColorModesSize, (SANE_String_Const)xmlNodeGetContent(node), 1); + if (strcmp(name, "ColorMode") == 0) { + const char *color = (SANE_String_Const)xmlNodeGetContent(node); + if (type == PLATEN || strcmp(color, "BlackAndWhite1")) + scanner->caps[type].ColorModes = char_to_array(scanner->caps[type].ColorModes, &scanner->caps[type].ColorModesSize, (SANE_String_Const)xmlNodeGetContent(node), 1); + } else if (strcmp(name, "ContentType") == 0) - scanner->ContentTypes = char_to_array(scanner->ContentTypes, &scanner->ContentTypesSize, (SANE_String_Const)xmlNodeGetContent(node), 0); + scanner->caps[type].ContentTypes = char_to_array(scanner->caps[type].ContentTypes, &scanner->caps[type].ContentTypesSize, (SANE_String_Const)xmlNodeGetContent(node), 0); else if (strcmp(name, "DocumentFormat") == 0) { int i = 0; - scanner->DocumentFormats = char_to_array(scanner->DocumentFormats, &scanner->DocumentFormatsSize, (SANE_String_Const)xmlNodeGetContent(node), 0); - for(; i < scanner->DocumentFormatsSize; i++) + SANE_Bool have_jpeg = SANE_FALSE, have_png = SANE_FALSE, have_tiff = SANE_FALSE, have_pdf = SANE_FALSE; + scanner->caps[type].DocumentFormats = char_to_array(scanner->caps[type].DocumentFormats, &scanner->caps[type].DocumentFormatsSize, (SANE_String_Const)xmlNodeGetContent(node), 0); + for(; i < scanner->caps[type].DocumentFormatsSize; i++) { - if (scanner->default_format == NULL && !strcmp(scanner->DocumentFormats[i], "image/jpeg")) + if (!strcmp(scanner->caps[type].DocumentFormats[i], "image/jpeg")) { - scanner->default_format = strdup("image/jpeg"); + have_jpeg = SANE_TRUE; } #if(defined HAVE_LIBPNG) - else if(!strcmp(scanner->DocumentFormats[i], "image/png") && (scanner->default_format == NULL || strcmp(scanner->default_format, "image/tiff"))) + else if(!strcmp(scanner->caps[type].DocumentFormats[i], "image/png")) { - if (scanner->default_format) - free(scanner->default_format); - scanner->default_format = strdup("image/png"); + have_png = SANE_TRUE; } #endif #if(defined HAVE_TIFFIO_H) - else if(!strcmp(scanner->DocumentFormats[i], "image/tiff")) + else if(type == PLATEN && !strcmp(scanner->caps[type].DocumentFormats[i], "image/tiff")) + { + have_tiff = SANE_TRUE; + } +#endif +#if(defined HAVE_POPPLER_GLIB) + else if(type == PLATEN && !strcmp(scanner->caps[type].DocumentFormats[i], "application/pdf")) { - if (scanner->default_format) - free(scanner->default_format); - scanner->default_format = strdup("image/tiff"); + have_pdf = SANE_TRUE; } #endif } - fprintf(stderr, "Capability : [%s]\n", scanner->default_format); + if (have_pdf) + scanner->caps[type].default_format = strdup("application/pdf"); + else if (have_tiff) + scanner->caps[type].default_format = strdup("image/tiff"); + else if (have_png) + scanner->caps[type].default_format = strdup("image/png"); + else if (have_jpeg) + scanner->caps[type].default_format = strdup("image/jpeg"); } else if (strcmp(name, "DocumentFormatExt") == 0) - scanner->format_ext = 1; + scanner->caps[type].format_ext = 1; else if (strcmp(name, "Intent") == 0) - scanner->SupportedIntents = char_to_array(scanner->SupportedIntents, &scanner->SupportedIntentsSize, (SANE_String_Const)xmlNodeGetContent(node), 0); + scanner->caps[type].SupportedIntents = char_to_array(scanner->caps[type].SupportedIntents, &scanner->caps[type].SupportedIntentsSize, (SANE_String_Const)xmlNodeGetContent(node), 0); else if (strcmp(name, "XResolution") == 0) - scanner->SupportedResolutions = int_to_array(scanner->SupportedResolutions, &scanner->SupportedResolutionsSize, atoi((const char *)xmlNodeGetContent(node))); + scanner->caps[type].SupportedResolutions = int_to_array(scanner->caps[type].SupportedResolutions, &scanner->caps[type].SupportedResolutionsSize, atoi((const char *)xmlNodeGetContent(node))); return (0); } @@ -230,39 +247,39 @@ find_valor_of_array_variables(xmlNode *node, capabilities_t *scanner) * \return 0 */ static int -find_value_of_int_variables(xmlNode *node, capabilities_t *scanner) +find_value_of_int_variables(xmlNode *node, capabilities_t *scanner, int type) { int MaxWidth = 0; int MaxHeight = 0; const char *name = (const char *)node->name; if (strcmp(name, "MinWidth") == 0) - scanner->MinWidth = atoi((const char*)xmlNodeGetContent(node)); + scanner->caps[type].MinWidth = atoi((const char*)xmlNodeGetContent(node)); else if (strcmp(name, "MaxWidth") == 0) { MaxWidth = atoi((const char*)xmlNodeGetContent(node)); - if (scanner->MaxWidth == 0 || MaxWidth < scanner->MaxWidth) - scanner->MaxWidth = atoi((const char *)xmlNodeGetContent(node)); + if (scanner->caps[type].MaxWidth == 0 || MaxWidth < scanner->caps[type].MaxWidth) + scanner->caps[type].MaxWidth = atoi((const char *)xmlNodeGetContent(node)); } else if (strcmp(name, "MinHeight") == 0) - scanner->MinHeight = atoi((const char*)xmlNodeGetContent(node)); + scanner->caps[type].MinHeight = atoi((const char*)xmlNodeGetContent(node)); else if (strcmp(name, "MaxHeight") == 0) { MaxHeight = atoi((const char*)xmlNodeGetContent(node)); - if (scanner->MaxHeight == 0 || MaxHeight < scanner->MaxHeight) - scanner->MaxHeight = atoi((const char *)xmlNodeGetContent(node)); + if (scanner->caps[type].MaxHeight == 0 || MaxHeight < scanner->caps[type].MaxHeight) + scanner->caps[type].MaxHeight = atoi((const char *)xmlNodeGetContent(node)); } else if (strcmp(name, "MaxScanRegions") == 0) - scanner->MaxScanRegions = atoi((const char *)xmlNodeGetContent(node)); + scanner->caps[type].MaxScanRegions = atoi((const char *)xmlNodeGetContent(node)); else if (strcmp(name, "MaxOpticalXResolution") == 0) - scanner->MaxOpticalXResolution = atoi((const char *)xmlNodeGetContent(node)); + scanner->caps[type].MaxOpticalXResolution = atoi((const char *)xmlNodeGetContent(node)); else if (strcmp(name, "RiskyLeftMargin") == 0) - scanner->RiskyLeftMargin = atoi((const char *)xmlNodeGetContent(node)); + scanner->caps[type].RiskyLeftMargin = atoi((const char *)xmlNodeGetContent(node)); else if (strcmp(name, "RiskyRightMargin") == 0) - scanner->RiskyRightMargin = atoi((const char *)xmlNodeGetContent(node)); + scanner->caps[type].RiskyRightMargin = atoi((const char *)xmlNodeGetContent(node)); else if (strcmp(name, "RiskyTopMargin") == 0) - scanner->RiskyTopMargin = atoi((const char *)xmlNodeGetContent(node)); + scanner->caps[type].RiskyTopMargin = atoi((const char *)xmlNodeGetContent(node)); else if (strcmp(name, "RiskyBottomMargin") == 0) - scanner->RiskyBottomMargin = atoi((const char *)xmlNodeGetContent(node)); - find_valor_of_array_variables(node, scanner); + scanner->caps[type].RiskyBottomMargin = atoi((const char *)xmlNodeGetContent(node)); + find_valor_of_array_variables(node, scanner, type); return (0); } @@ -275,7 +292,7 @@ find_value_of_int_variables(xmlNode *node, capabilities_t *scanner) * \return 0 */ static int -find_true_variables(xmlNode *node, capabilities_t *scanner) +find_true_variables(xmlNode *node, capabilities_t *scanner, int type) { const char *name = (const char *)node->name; if (strcmp(name, "MinWidth") == 0 || @@ -294,7 +311,7 @@ find_true_variables(xmlNode *node, capabilities_t *scanner) strcmp(name, "RiskyTopMargin") == 0 || strcmp(name, "RiskyBottomMargin") == 0 || strcmp(name, "DocumentFormatExt") == 0) - find_value_of_int_variables(node, scanner); + find_value_of_int_variables(node, scanner, type); return (0); } @@ -305,21 +322,67 @@ find_true_variables(xmlNode *node, capabilities_t *scanner) * \return 0 */ static int -print_xml_c(xmlNode *node, capabilities_t *scanner) +print_xml_c(xmlNode *node, capabilities_t *scanner, int type) { while (node) { if (node->type == XML_ELEMENT_NODE) { - if (find_nodes_c(node)) - find_true_variables(node, scanner); + if (find_nodes_c(node) && type != -1) + find_true_variables(node, scanner, type); } - print_xml_c(node->children, scanner); + if (!strcmp((const char *)node->name, "PlatenInputCaps")) { + scanner->Sources[PLATEN] = (SANE_String_Const)strdup(SANE_I18N ("Flatbed")); + scanner->SourcesSize++; + scanner->source = PLATEN; + print_xml_c(node->children, scanner, PLATEN); + scanner->caps[PLATEN].duplex = 0; + } + else if (!strcmp((const char *)node->name, "AdfSimplexInputCaps")) { + scanner->Sources[ADFSIMPLEX] = (SANE_String_Const)strdup(SANE_I18N("ADF")); + scanner->SourcesSize++; + if (scanner->source == -1) scanner->source = ADFSIMPLEX; + print_xml_c(node->children, scanner, ADFSIMPLEX); + scanner->caps[ADFSIMPLEX].duplex = 0; + } + else if (!strcmp((const char *)node->name, "AdfDuplexInputCaps")) { + scanner->Sources[ADFDUPLEX] = (SANE_String_Const)strdup(SANE_I18N ("ADF Duplex")); + scanner->SourcesSize++; + if (scanner->source == -1) scanner->source = ADFDUPLEX; + print_xml_c(node->children, scanner, ADFDUPLEX); + scanner->caps[ADFDUPLEX].duplex = 1; + } + else + print_xml_c(node->children, scanner, type); node = node->next; } return (0); } +static void +_reduce_color_modes(capabilities_t *scanner) +{ + int type = 0; + for (type = 0; type < 3; type++) { + if (scanner->caps[type].ColorModesSize) { + if (scanner->caps[type].default_format && + strcmp(scanner->caps[type].default_format, "application/pdf")) { + if (scanner->caps[type].ColorModesSize == 3) { + free(scanner->caps[type].ColorModes); + scanner->caps[type].ColorModes = NULL; + scanner->caps[type].ColorModesSize = 0; + scanner->caps[type].ColorModes = char_to_array(scanner->caps[type].ColorModes, + &scanner->caps[type].ColorModesSize, + (SANE_String_Const)SANE_VALUE_SCAN_MODE_GRAY, 0); + scanner->caps[type].ColorModes = char_to_array(scanner->caps[type].ColorModes, + &scanner->caps[type].ColorModesSize, + (SANE_String_Const)SANE_VALUE_SCAN_MODE_COLOR, 0); + } + } + } + } +} + /** - * \fn capabilities_t *escl_capabilities(SANE_String_Const name, SANE_Status *status) + * \fn capabilities_t *escl_capabilities(const ESCL_Device *device, SANE_Status *status) * \brief Function that finally recovers all the capabilities of the scanner, using curl. * This function is called in the 'sane_open' function and it's the equivalent of * the following curl command : "curl http(s)://'ip':'port'/eSCL/ScannerCapabilities". @@ -327,18 +390,18 @@ print_xml_c(xmlNode *node, capabilities_t *scanner) * \return scanner (the structure that stocks all the capabilities elements) */ capabilities_t * -escl_capabilities(SANE_String_Const name, SANE_Status *status) +escl_capabilities(const ESCL_Device *device, SANE_Status *status) { capabilities_t *scanner = (capabilities_t*)calloc(1, sizeof(capabilities_t)); CURL *curl_handle = NULL; struct cap *var = NULL; xmlDoc *data = NULL; xmlNode *node = NULL; + int i = 0; const char *scanner_capabilities = "/eSCL/ScannerCapabilities"; - char tmp[PATH_MAX] = { 0 }; *status = SANE_STATUS_GOOD; - if (name == NULL) + if (device == NULL) *status = SANE_STATUS_NO_MEM; var = (struct cap *)calloc(1, sizeof(struct cap)); if (var == NULL) @@ -346,32 +409,41 @@ escl_capabilities(SANE_String_Const name, SANE_Status *status) var->memory = malloc(1); var->size = 0; curl_handle = curl_easy_init(); - strcpy(tmp, name); - strcat(tmp, scanner_capabilities); - DBG( 1, "Get Capabilities : %s\n", tmp); - curl_easy_setopt(curl_handle, CURLOPT_URL, tmp); - if (strncmp(name, "https", 5) == 0) { - DBG( 1, "Ignoring safety certificates, use https\n"); - curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0L); - curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0L); - } + escl_curl_url(curl_handle, device, scanner_capabilities); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, memory_callback_c); curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)var); - if (curl_easy_perform(curl_handle) != CURLE_OK) { - DBG( 1, "The scanner didn't respond.\n"); + CURLcode res = curl_easy_perform(curl_handle); + if (res != CURLE_OK) { + DBG( 1, "The scanner didn't respond: %s\n", curl_easy_strerror(res)); *status = SANE_STATUS_INVAL; + goto clean_data; } + DBG( 10, "XML Capabilities[\n%s\n]\n", var->memory); data = xmlReadMemory(var->memory, var->size, "file.xml", NULL, 0); - if (data == NULL) + if (data == NULL) { *status = SANE_STATUS_NO_MEM; + goto clean_data; + } node = xmlDocGetRootElement(data); - if (node == NULL) + if (node == NULL) { *status = SANE_STATUS_NO_MEM; - print_xml_c(node, scanner); + goto clean; + } + + scanner->source = 0; + scanner->Sources = (SANE_String_Const *)malloc(sizeof(SANE_String_Const) * 4); + for (i = 0; i < 4; i++) + scanner->Sources[i] = NULL; + print_xml_c(node, scanner, -1); + _reduce_color_modes(scanner); +clean: xmlFreeDoc(data); +clean_data: xmlCleanupParser(); xmlMemoryDump(); curl_easy_cleanup(curl_handle); - free(var->memory); + if (var) + free(var->memory); + free(var); return (scanner); } |