From 06827774b601f23b86d36be61bd658202557b28d Mon Sep 17 00:00:00 2001 From: Simon Nivault Date: Fri, 9 Sep 2022 09:13:37 +0200 Subject: [PATCH] Manage default printer for the win backend (#8177) * printer_win: Retrieve default printer name * printer_win: Declare the local default printer as default * printer_cups: Retrieve default printer name * printer_cups: Declare the local default printer as default --- channels/printer/client/cups/printer_cups.c | 20 ++++++++++----- channels/printer/client/win/printer_win.c | 28 +++++++++++++++++---- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/channels/printer/client/cups/printer_cups.c b/channels/printer/client/cups/printer_cups.c index 8c4a41ee3..1d1455495 100644 --- a/channels/printer/client/cups/printer_cups.c +++ b/channels/printer/client/cups/printer_cups.c @@ -46,6 +46,7 @@ typedef struct int id_sequence; size_t references; + char* defaultPrinter; } rdpCupsPrinterDriver; typedef struct @@ -276,7 +277,7 @@ static void printer_cups_release_ref_printer(rdpPrinter* printer) } static rdpPrinter* printer_cups_new_printer(rdpCupsPrinterDriver* cups_driver, const char* name, - const char* driverName, BOOL is_default) + const char* driverName) { rdpCupsPrinter* cups_printer; @@ -298,7 +299,7 @@ static rdpPrinter* printer_cups_new_printer(rdpCupsPrinterDriver* cups_driver, c if (!cups_printer->printer.driver) goto fail; - cups_printer->printer.is_default = is_default; + cups_printer->printer.is_default = strcmp(name, cups_driver->defaultPrinter) == 0; cups_printer->printer.CreatePrintJob = printer_cups_create_printjob; cups_printer->printer.FindPrintJob = printer_cups_find_printjob; @@ -351,8 +352,8 @@ static rdpPrinter** printer_cups_enum_printers(rdpPrinterDriver* driver) { if (dest->instance == NULL) { - rdpPrinter* current = printer_cups_new_printer((rdpCupsPrinterDriver*)driver, - dest->name, NULL, dest->is_default); + rdpPrinter* current = + printer_cups_new_printer((rdpCupsPrinterDriver*)driver, dest->name, NULL); if (!current) { printer_cups_release_enum_printers(printers); @@ -383,8 +384,7 @@ static rdpPrinter* printer_cups_get_printer(rdpPrinterDriver* driver, const char rdpCupsPrinterDriver* cups_driver = (rdpCupsPrinterDriver*)driver; WINPR_ASSERT(cups_driver); - return printer_cups_new_printer(cups_driver, name, driverName, - cups_driver->id_sequence == 1 ? TRUE : FALSE); + return printer_cups_new_printer(cups_driver, name, driverName); } static void printer_cups_add_ref_driver(rdpPrinterDriver* driver) @@ -407,6 +407,7 @@ static void printer_cups_release_ref_driver(rdpPrinterDriver* driver) { if (uniq_cups_driver == cups_driver) uniq_cups_driver = NULL; + free(cups_driver->defaultPrinter); free(cups_driver); } else @@ -430,6 +431,13 @@ rdpPrinterDriver* cups_freerdp_printer_client_subsystem_entry(void) uniq_cups_driver->driver.ReleaseRef = printer_cups_release_ref_driver; uniq_cups_driver->id_sequence = 1; + uniq_cups_driver->defaultPrinter = _strdup(cupsGetDefault()); + + if (!uniq_cups_driver->defaultPrinter) + { + free(uniq_cups_driver); + return NULL; + } } WINPR_ASSERT(uniq_cups_driver->driver.AddRef); diff --git a/channels/printer/client/win/printer_win.c b/channels/printer/client/win/printer_win.c index 48702c2b0..eb5b26489 100644 --- a/channels/printer/client/win/printer_win.c +++ b/channels/printer/client/win/printer_win.c @@ -52,6 +52,7 @@ typedef struct size_t id_sequence; size_t references; + LPWSTR defaultPrinter; } rdpWinPrinterDriver; typedef struct @@ -233,7 +234,7 @@ static void printer_win_release_ref_printer(rdpPrinter* printer) } static rdpPrinter* printer_win_new_printer(rdpWinPrinterDriver* win_driver, const WCHAR* name, - const WCHAR* drivername, BOOL is_default) + const WCHAR* drivername) { rdpWinPrinter* win_printer; DWORD needed = 0; @@ -251,7 +252,7 @@ static rdpPrinter* printer_win_new_printer(rdpWinPrinterDriver* win_driver, cons if (!win_printer->printer.name) goto fail; - win_printer->printer.is_default = is_default; + win_printer->printer.is_default = _wcscmp(name, win_driver->defaultPrinter) == 0; win_printer->printer.CreatePrintJob = printer_win_create_printjob; win_printer->printer.FindPrintJob = printer_win_find_printjob; @@ -345,7 +346,7 @@ static rdpPrinter** printer_win_enum_printers(rdpPrinterDriver* driver) { rdpPrinter* current = printers[num_printers]; current = printer_win_new_printer((rdpWinPrinterDriver*)driver, prninfo[i].pPrinterName, - prninfo[i].pDriverName, 0); + prninfo[i].pDriverName); if (!current) { printer_win_release_enum_printers(printers); @@ -385,8 +386,7 @@ static rdpPrinter* printer_win_get_printer(rdpPrinterDriver* driver, const char* return NULL; } - myPrinter = printer_win_new_printer(win_driver, nameW, driverNameW, - win_driver->id_sequence == 1 ? TRUE : FALSE); + myPrinter = printer_win_new_printer(win_driver, nameW, driverNameW); free(driverNameW); free(nameW); @@ -408,6 +408,7 @@ static void printer_win_release_ref_driver(rdpPrinterDriver* driver) rdpWinPrinterDriver* win = (rdpWinPrinterDriver*)driver; if (win->references <= 1) { + free(win->defaultPrinter); free(win); win_driver = NULL; } @@ -417,6 +418,8 @@ static void printer_win_release_ref_driver(rdpPrinterDriver* driver) rdpPrinterDriver* win_freerdp_printer_client_subsystem_entry(void) { + DWORD size; + if (!win_driver) { win_driver = (rdpWinPrinterDriver*)calloc(1, sizeof(rdpWinPrinterDriver)); @@ -432,6 +435,21 @@ rdpPrinterDriver* win_freerdp_printer_client_subsystem_entry(void) win_driver->driver.ReleaseRef = printer_win_release_ref_driver; win_driver->id_sequence = 1; + + GetDefaultPrinter(NULL, &size); + if (size) + { + win_driver->defaultPrinter = (LPWSTR)calloc(size, sizeof(WCHAR)); + + if (!win_driver->defaultPrinter) + { + free(win_driver); + return NULL; + } + + if (!GetDefaultPrinter(win_driver->defaultPrinter, &size)) + win_driver->defaultPrinter[0] = "\0"; + } } win_driver->driver.AddRef(&win_driver->driver);