GList *dt_get_papers(const dt_printer_info_t *printer) { const char *printer_name = printer->name; GList *result = NULL; #if ((CUPS_VERSION_MAJOR == 1) && (CUPS_VERSION_MINOR >= 7)) || CUPS_VERSION_MAJOR > 1 #if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_9 if (cupsConnectDest != NULL && cupsCopyDestInfo != NULL && cupsGetDestMediaCount != NULL && cupsGetDestMediaByIndex != NULL && cupsFreeDestInfo != NULL) #endif { cups_dest_t *dests; int num_dests = cupsGetDests(&dests); cups_dest_t *dest = cupsGetDest(printer_name, NULL, num_dests, dests); int cancel = 0; // important char resource[1024]; if (dest) { http_t *hcon = cupsConnectDest(dest, 0, 2000, &cancel, resource, sizeof(resource), NULL, (void *)NULL); if (hcon) { cups_size_t size; cups_dinfo_t *info = cupsCopyDestInfo (hcon, dest); const int count = cupsGetDestMediaCount(hcon, dest, info, CUPS_MEDIA_FLAGS_DEFAULT); for (int k=0; k<count; k++) { if (cupsGetDestMediaByIndex(hcon, dest, info, k, CUPS_MEDIA_FLAGS_DEFAULT, &size)) { if (size.width!=0 && size.length!=0 && !paper_exists(result, size.media)) { pwg_media_t *med = pwgMediaForPWG (size.media); char common_name[MAX_NAME] = { 0 }; if (med->ppd) g_strlcpy(common_name, med->ppd, sizeof(common_name)); else g_strlcpy(common_name, size.media, sizeof(common_name)); dt_paper_info_t *paper = (dt_paper_info_t*)malloc(sizeof(dt_paper_info_t)); g_strlcpy(paper->name, size.media, sizeof(paper->name)); g_strlcpy(paper->common_name, common_name, sizeof(paper->common_name)); paper->width = (double)size.width / 100.0; paper->height = (double)size.length / 100.0; result = g_list_append (result, paper); dt_print(DT_DEBUG_PRINT, "[print] new media paper %4d %6.2f x %6.2f (%s) (%s)\n", k, paper->width, paper->height, paper->name, paper->common_name); } } } cupsFreeDestInfo(info); httpClose(hcon); } else dt_print(DT_DEBUG_PRINT, "[print] cannot connect to printer %s (cancel=%d)\n", printer_name, cancel); } cupsFreeDests(num_dests, dests); } #endif // check now PPD page sizes const char *PPDFile = cupsGetPPD(printer_name); ppd_file_t *ppd = ppdOpenFile(PPDFile); if (ppd) { ppd_size_t *size = ppd->sizes; for (int k=0; k<ppd->num_sizes; k++) { if (size->width!=0 && size->length!=0 && !paper_exists(result, size->name)) { dt_paper_info_t *paper = (dt_paper_info_t*)malloc(sizeof(dt_paper_info_t)); g_strlcpy(paper->name, size->name, MAX_NAME); g_strlcpy(paper->common_name, size->name, MAX_NAME); paper->width = (double)dt_pdf_point_to_mm(size->width); paper->height = (double)dt_pdf_point_to_mm(size->length); result = g_list_append (result, paper); dt_print(DT_DEBUG_PRINT, "[print] new ppd paper %4d %6.2f x %6.2f (%s) (%s)\n", k, paper->width, paper->height, paper->name, paper->common_name); } size++; } ppdClose(ppd); g_unlink(PPDFile); } result = g_list_sort_with_data (result, (GCompareDataFunc)sort_papers, NULL); return result; }
void dt_get_printer_info(const char *printer_name, dt_printer_info_t *pinfo) { cups_dest_t *dests; int num_dests = cupsGetDests(&dests); cups_dest_t *dest = cupsGetDest(printer_name, NULL, num_dests, dests); if (dest) { const char *PPDFile = cupsGetPPD (printer_name); g_strlcpy(pinfo->name, dest->name, MAX_NAME); ppd_file_t *ppd = ppdOpenFile(PPDFile); if (ppd) { ppdMarkDefaults(ppd); cupsMarkOptions(ppd, dest->num_options, dest->options); // first check if this is turboprint drived printer, two solutions: // 1. ModelName constains TurboPrint // 2. zedoPrinterDriver exists ppd_attr_t *attr = ppdFindAttr(ppd, "ModelName", NULL); if (attr) { pinfo->is_turboprint = strstr(attr->value, "TurboPrint") != NULL; } // hardware margins attr = ppdFindAttr(ppd, "HWMargins", NULL); if (attr) { sscanf(attr->value, "%lf %lf %lf %lf", &pinfo->hw_margin_left, &pinfo->hw_margin_bottom, &pinfo->hw_margin_right, &pinfo->hw_margin_top); pinfo->hw_margin_left = dt_pdf_point_to_mm (pinfo->hw_margin_left); pinfo->hw_margin_bottom = dt_pdf_point_to_mm (pinfo->hw_margin_bottom); pinfo->hw_margin_right = dt_pdf_point_to_mm (pinfo->hw_margin_right); pinfo->hw_margin_top = dt_pdf_point_to_mm (pinfo->hw_margin_top); } // default resolution attr = ppdFindAttr(ppd, "DefaultResolution", NULL); if (attr) { char *x = strstr(attr->value, "x"); if (x) sscanf (x+1, "%ddpi", &pinfo->resolution); else sscanf (attr->value, "%ddpi", &pinfo->resolution); } else pinfo->resolution = 300; while(pinfo->resolution>360) pinfo->resolution /= 2.0; ppdClose(ppd); g_unlink(PPDFile); } } cupsFreeDests(num_dests, dests); }
dt_printer_info_t *dt_get_printer_info(const char *printer_name) { cups_dest_t *dests; int num_dests = cupsGetDests(&dests); cups_dest_t *dest = cupsGetDest(printer_name, NULL, num_dests, dests); dt_printer_info_t *result = NULL; if (dest) { const char *PPDFile = cupsGetPPD (printer_name); result = (dt_printer_info_t *)malloc(sizeof(dt_printer_info_t)); g_strlcpy(result->name, dest->name, MAX_NAME); ppd_file_t *ppd = ppdOpenFile(PPDFile); if (ppd) { ppdMarkDefaults(ppd); cupsMarkOptions(ppd, dest->num_options, dest->options); // hardware margins ppd_attr_t *attr = ppdFindAttr(ppd, "HWMargins", NULL); if (attr) { sscanf(attr->value, "%lf %lf %lf %lf", &result->hw_margin_left, &result->hw_margin_bottom, &result->hw_margin_right, &result->hw_margin_top); result->hw_margin_left = dt_pdf_point_to_mm (result->hw_margin_left); result->hw_margin_bottom = dt_pdf_point_to_mm (result->hw_margin_bottom); result->hw_margin_right = dt_pdf_point_to_mm (result->hw_margin_right); result->hw_margin_top = dt_pdf_point_to_mm (result->hw_margin_top); } // default resolution attr = ppdFindAttr(ppd, "DefaultResolution", NULL); if (attr) { char *x = strstr(attr->value, "x"); if (x) sscanf (x+1, "%ddpi", &result->resolution); else sscanf (attr->value, "%ddpi", &result->resolution); } else result->resolution = 300; while(result->resolution>360) result->resolution /= 2.0; ppdClose(ppd); unlink(PPDFile); } } cupsFreeDests(num_dests, dests); return result; }