24 #include <freerdp/config.h>
30 #include <winpr/crt.h>
31 #include <winpr/assert.h>
32 #include <winpr/string.h>
33 #include <winpr/synch.h>
34 #include <winpr/thread.h>
35 #include <winpr/stream.h>
36 #include <winpr/interlocked.h>
37 #include <winpr/path.h>
39 #include <freerdp/channels/rdpdr.h>
40 #include <freerdp/crypto/crypto.h>
41 #include <freerdp/freerdp.h>
43 #include "../printer.h"
45 #include <freerdp/client/printer.h>
47 #include <freerdp/channels/log.h>
49 #define TAG CHANNELS_TAG("printer.client")
63 rdpContext* rdpcontext;
76 static const char* filemap[] = {
"PortDosName",
"PnPName",
"DriverName",
77 "CachedPrinterConfigData" };
79 static char* get_printer_config_path(
const rdpSettings* settings,
const WCHAR* name,
size_t length)
82 char* dir = GetCombinedPath(path,
"printers");
83 char* bname = crypto_base64_encode((
const BYTE*)name, length);
84 char* config = GetCombinedPath(dir, bname);
86 if (config && !winpr_PathFileExists(config))
88 if (!winpr_PathMakePath(config, NULL))
100 static BOOL printer_write_setting(
const char* path, prn_conf_t type,
const void* data,
107 const char* name = filemap[type];
108 char* abs = GetCombinedPath(path, name);
110 if (!abs || (length > INT32_MAX))
116 file = CreateFileA(abs, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
119 if (file == INVALID_HANDLE_VALUE)
124 base64 = crypto_base64_encode(data, length);
131 const size_t b64len = strnlen(base64, 2 * length);
132 rc = WriteFile(file, base64, (UINT32)b64len, &written, NULL);
134 if (b64len != written)
141 (void)CloseHandle(file);
146 static BOOL printer_config_valid(
const char* path)
151 if (!winpr_PathFileExists(path))
157 static BOOL printer_read_setting(
const char* path, prn_conf_t type,
void** data, UINT32* length)
165 const char* name = filemap[type];
166 char* abs = GetCombinedPath(path, name);
171 file = CreateFileA(abs, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
174 if (file == INVALID_HANDLE_VALUE)
177 lowSize = GetFileSize(file, &highSize);
179 if ((lowSize == INVALID_FILE_SIZE) || (highSize != 0))
184 fdata = malloc(lowSize);
189 rc = ReadFile(file, fdata, lowSize, &read, NULL);
196 (void)CloseHandle(file);
198 if (rc && (lowSize <= INT_MAX))
201 crypto_base64_decode(fdata, lowSize, (BYTE**)data, &blen);
203 if (*data && (blen > 0))
204 *length = (UINT32)blen;
221 static BOOL printer_save_to_config(
const rdpSettings* settings,
const char* PortDosName,
222 size_t PortDosNameLen,
const WCHAR* PnPName,
size_t PnPNameLen,
223 const WCHAR* DriverName,
size_t DriverNameLen,
224 const WCHAR* PrinterName,
size_t PrintNameLen,
225 const BYTE* CachedPrinterConfigData,
size_t CacheFieldsLen)
228 char* path = get_printer_config_path(settings, PrinterName, PrintNameLen);
233 if (!printer_write_setting(path, PRN_CONF_PORT, PortDosName, PortDosNameLen))
236 if (!printer_write_setting(path, PRN_CONF_PNP, PnPName, PnPNameLen))
239 if (!printer_write_setting(path, PRN_CONF_DRIVER, DriverName, DriverNameLen))
242 if (!printer_write_setting(path, PRN_CONF_DATA, CachedPrinterConfigData, CacheFieldsLen))
250 static BOOL printer_update_to_config(
const rdpSettings* settings,
const WCHAR* name,
size_t length,
251 const BYTE* data,
size_t datalen)
254 char* path = get_printer_config_path(settings, name, length);
255 rc = printer_write_setting(path, PRN_CONF_DATA, data, datalen);
260 static BOOL printer_remove_config(
const rdpSettings* settings,
const WCHAR* name,
size_t length)
263 char* path = get_printer_config_path(settings, name, length);
265 if (!printer_config_valid(path))
268 rc = winpr_RemoveDirectory(path);
274 static BOOL printer_move_config(
const rdpSettings* settings,
const WCHAR* oldName,
size_t oldLength,
275 const WCHAR* newName,
size_t newLength)
278 char* oldPath = get_printer_config_path(settings, oldName, oldLength);
279 char* newPath = get_printer_config_path(settings, newName, newLength);
281 if (printer_config_valid(oldPath))
282 rc = winpr_MoveFile(oldPath, newPath);
289 static BOOL printer_load_from_config(
const rdpSettings* settings, rdpPrinter* printer,
290 PRINTER_DEVICE* printer_dev)
297 void* DriverName = NULL;
298 UINT32 DriverNameLen = 0;
299 void* PnPName = NULL;
300 UINT32 PnPNameLen = 0;
301 void* CachedPrinterConfigData = NULL;
302 UINT32 CachedFieldsLen = 0;
303 UINT32 PrinterNameLen = 0;
305 if (!settings || !printer || !printer->name)
308 wname = ConvertUtf8ToWCharAlloc(printer->name, &wlen);
314 path = get_printer_config_path(settings, wname, wlen *
sizeof(WCHAR));
316 const size_t plen = wlen *
sizeof(WCHAR);
317 if (plen > UINT32_MAX)
319 PrinterNameLen = (UINT32)plen;
325 if (printer->is_default)
326 flags |= RDPDR_PRINTER_ANNOUNCE_FLAG_DEFAULTPRINTER;
328 if (!printer_read_setting(path, PRN_CONF_PNP, &PnPName, &PnPNameLen))
332 if (!printer_read_setting(path, PRN_CONF_DRIVER, &DriverName, &DriverNameLen))
335 DriverName = ConvertUtf8ToWCharAlloc(printer->driver, &len);
338 const size_t dlen = (len + 1) *
sizeof(WCHAR);
339 if (dlen > UINT32_MAX)
341 DriverNameLen = (UINT32)dlen;
344 if (!printer_read_setting(path, PRN_CONF_DATA, &CachedPrinterConfigData, &CachedFieldsLen))
348 Stream_SetPosition(printer_dev->device.data, 0);
350 if (!Stream_EnsureRemainingCapacity(printer_dev->device.data, 24))
353 Stream_Write_UINT32(printer_dev->device.data, flags);
354 Stream_Write_UINT32(printer_dev->device.data, 0);
355 Stream_Write_UINT32(printer_dev->device.data, PnPNameLen);
356 Stream_Write_UINT32(printer_dev->device.data, DriverNameLen);
357 Stream_Write_UINT32(printer_dev->device.data, PrinterNameLen);
358 Stream_Write_UINT32(printer_dev->device.data, CachedFieldsLen);
360 if (!Stream_EnsureRemainingCapacity(printer_dev->device.data, PnPNameLen))
364 Stream_Write(printer_dev->device.data, PnPName, PnPNameLen);
366 if (!Stream_EnsureRemainingCapacity(printer_dev->device.data, DriverNameLen))
369 Stream_Write(printer_dev->device.data, DriverName, DriverNameLen);
371 if (!Stream_EnsureRemainingCapacity(printer_dev->device.data, PrinterNameLen))
379 backslash.c[0] =
'\\';
380 backslash.c[1] =
'\0';
382 for (WCHAR* wptr = wname; (wptr = _wcschr(wptr, backslash.w));)
384 Stream_Write(printer_dev->device.data, wname, PrinterNameLen);
386 if (!Stream_EnsureRemainingCapacity(printer_dev->device.data, CachedFieldsLen))
389 Stream_Write(printer_dev->device.data, CachedPrinterConfigData, CachedFieldsLen);
396 free(CachedPrinterConfigData);
400 static BOOL printer_save_default_config(
const rdpSettings* settings, rdpPrinter* printer)
404 WCHAR* driver = NULL;
409 if (!settings || !printer || !printer->name || !printer->driver)
412 wname = ConvertUtf8ToWCharAlloc(printer->name, NULL);
417 driver = ConvertUtf8ToWCharAlloc(printer->driver, NULL);
422 wlen = _wcslen(wname) + 1;
423 dlen = _wcslen(driver) + 1;
424 path = get_printer_config_path(settings, wname, wlen *
sizeof(WCHAR));
431 if (!printer_write_setting(path, PRN_CONF_DRIVER, driver, dlen *
sizeof(WCHAR)))
448 static UINT printer_process_irp_create(PRINTER_DEVICE* printer_dev, IRP* irp)
450 rdpPrintJob* printjob = NULL;
452 WINPR_ASSERT(printer_dev);
455 if (printer_dev->printer)
457 WINPR_ASSERT(printer_dev->printer->CreatePrintJob);
459 printer_dev->printer->CreatePrintJob(printer_dev->printer, irp->devman->id_sequence++);
464 Stream_Write_UINT32(irp->output, printjob->id);
468 Stream_Write_UINT32(irp->output, 0);
469 irp->IoStatus = STATUS_PRINT_QUEUE_FULL;
472 return irp->Complete(irp);
480 static UINT printer_process_irp_close(PRINTER_DEVICE* printer_dev, IRP* irp)
482 rdpPrintJob* printjob = NULL;
484 WINPR_ASSERT(printer_dev);
487 if (printer_dev->printer)
489 WINPR_ASSERT(printer_dev->printer->FindPrintJob);
490 printjob = printer_dev->printer->FindPrintJob(printer_dev->printer, irp->FileId);
495 irp->IoStatus = STATUS_UNSUCCESSFUL;
499 printjob->Close(printjob);
502 Stream_Zero(irp->output, 4);
503 return irp->Complete(irp);
511 static UINT printer_process_irp_write(PRINTER_DEVICE* printer_dev, IRP* irp)
515 rdpPrintJob* printjob = NULL;
516 UINT error = CHANNEL_RC_OK;
518 WINPR_ASSERT(printer_dev);
521 if (!Stream_CheckAndLogRequiredLength(TAG, irp->input, 32))
522 return ERROR_INVALID_DATA;
523 Stream_Read_UINT32(irp->input, Length);
524 Stream_Read_UINT64(irp->input, Offset);
527 Stream_Seek(irp->input, 20);
528 const void* ptr = Stream_ConstPointer(irp->input);
529 if (!Stream_SafeSeek(irp->input, Length))
530 return ERROR_INVALID_DATA;
531 if (printer_dev->printer)
533 WINPR_ASSERT(printer_dev->printer->FindPrintJob);
534 printjob = printer_dev->printer->FindPrintJob(printer_dev->printer, irp->FileId);
539 irp->IoStatus = STATUS_UNSUCCESSFUL;
544 error = printjob->Write(printjob, ptr, Length);
549 WLog_ERR(TAG,
"printjob->Write failed with error %" PRIu32
"!", error);
553 Stream_Write_UINT32(irp->output, Length);
554 Stream_Write_UINT8(irp->output, 0);
556 WINPR_ASSERT(irp->Complete);
557 return irp->Complete(irp);
565 static UINT printer_process_irp_device_control(PRINTER_DEVICE* printer_dev, IRP* irp)
567 WINPR_ASSERT(printer_dev);
570 Stream_Write_UINT32(irp->output, 0);
572 WINPR_ASSERT(irp->Complete);
573 return irp->Complete(irp);
581 static UINT printer_process_irp(PRINTER_DEVICE* printer_dev, IRP* irp)
585 WINPR_ASSERT(printer_dev);
588 switch (irp->MajorFunction)
591 if ((error = printer_process_irp_create(printer_dev, irp)))
593 WLog_ERR(TAG,
"printer_process_irp_create failed with error %" PRIu32
"!", error);
600 if ((error = printer_process_irp_close(printer_dev, irp)))
602 WLog_ERR(TAG,
"printer_process_irp_close failed with error %" PRIu32
"!", error);
609 if ((error = printer_process_irp_write(printer_dev, irp)))
611 WLog_ERR(TAG,
"printer_process_irp_write failed with error %" PRIu32
"!", error);
617 case IRP_MJ_DEVICE_CONTROL:
618 if ((error = printer_process_irp_device_control(printer_dev, irp)))
620 WLog_ERR(TAG,
"printer_process_irp_device_control failed with error %" PRIu32
"!",
628 irp->IoStatus = STATUS_NOT_SUPPORTED;
629 WINPR_ASSERT(irp->Complete);
630 return irp->Complete(irp);
633 return CHANNEL_RC_OK;
636 static DWORD WINAPI printer_thread_func(LPVOID arg)
639 PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*)arg;
640 UINT error = CHANNEL_RC_OK;
642 WINPR_ASSERT(printer_dev);
646 HANDLE obj[] = { printer_dev->event, printer_dev->stopEvent };
647 DWORD rc = WaitForMultipleObjects(ARRAYSIZE(obj), obj, FALSE, INFINITE);
649 if (rc == WAIT_FAILED)
651 error = GetLastError();
652 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"!", error);
656 if (rc == WAIT_OBJECT_0 + 1)
658 else if (rc != WAIT_OBJECT_0)
661 (void)ResetEvent(printer_dev->event);
662 irp = (IRP*)InterlockedPopEntrySList(printer_dev->pIrpList);
666 WLog_ERR(TAG,
"InterlockedPopEntrySList failed!");
667 error = ERROR_INTERNAL_ERROR;
671 if ((error = printer_process_irp(printer_dev, irp)))
673 WLog_ERR(TAG,
"printer_process_irp failed with error %" PRIu32
"!", error);
678 if (error && printer_dev->rdpcontext)
679 setChannelError(printer_dev->rdpcontext, error,
"printer_thread_func reported an error");
690 static UINT printer_irp_request(DEVICE* device, IRP* irp)
692 PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*)device;
694 WINPR_ASSERT(printer_dev);
697 if (printer_dev->async)
699 InterlockedPushEntrySList(printer_dev->pIrpList, &(irp->ItemEntry));
700 (void)SetEvent(printer_dev->event);
704 UINT error = printer_process_irp(printer_dev, irp);
707 WLog_ERR(TAG,
"printer_process_irp failed with error %" PRIu32
"!", error);
712 return CHANNEL_RC_OK;
715 static UINT printer_custom_component(DEVICE* device, UINT16 component, UINT16 packetId,
wStream* s)
718 PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*)device;
720 WINPR_ASSERT(printer_dev);
721 WINPR_ASSERT(printer_dev->rdpcontext);
723 const rdpSettings* settings = printer_dev->rdpcontext->settings;
724 WINPR_ASSERT(settings);
726 if (component != RDPDR_CTYP_PRN)
727 return ERROR_INVALID_DATA;
729 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
730 return ERROR_INVALID_DATA;
732 Stream_Read_UINT32(s, eventID);
736 case PAKID_PRN_CACHE_DATA:
739 case RDPDR_ADD_PRINTER_EVENT:
742 UINT32 PnPNameLen = 0;
743 UINT32 DriverNameLen = 0;
744 UINT32 PrintNameLen = 0;
745 UINT32 CacheFieldsLen = 0;
746 const WCHAR* PnPName = NULL;
747 const WCHAR* DriverName = NULL;
748 const WCHAR* PrinterName = NULL;
749 const BYTE* CachedPrinterConfigData = NULL;
751 if (!Stream_CheckAndLogRequiredLength(TAG, s, 24))
752 return ERROR_INVALID_DATA;
754 Stream_Read(s, PortDosName,
sizeof(PortDosName));
755 Stream_Read_UINT32(s, PnPNameLen);
756 Stream_Read_UINT32(s, DriverNameLen);
757 Stream_Read_UINT32(s, PrintNameLen);
758 Stream_Read_UINT32(s, CacheFieldsLen);
760 if (!Stream_CheckAndLogRequiredLength(TAG, s, PnPNameLen))
761 return ERROR_INVALID_DATA;
763 PnPName = Stream_ConstPointer(s);
764 Stream_Seek(s, PnPNameLen);
766 if (!Stream_CheckAndLogRequiredLength(TAG, s, DriverNameLen))
767 return ERROR_INVALID_DATA;
769 DriverName = Stream_ConstPointer(s);
770 Stream_Seek(s, DriverNameLen);
772 if (!Stream_CheckAndLogRequiredLength(TAG, s, PrintNameLen))
773 return ERROR_INVALID_DATA;
775 PrinterName = Stream_ConstPointer(s);
776 Stream_Seek(s, PrintNameLen);
778 if (!Stream_CheckAndLogRequiredLength(TAG, s, CacheFieldsLen))
779 return ERROR_INVALID_DATA;
781 CachedPrinterConfigData = Stream_ConstPointer(s);
782 Stream_Seek(s, CacheFieldsLen);
784 if (!printer_save_to_config(settings, PortDosName,
sizeof(PortDosName), PnPName,
785 PnPNameLen, DriverName, DriverNameLen, PrinterName,
786 PrintNameLen, CachedPrinterConfigData,
788 return ERROR_INTERNAL_ERROR;
792 case RDPDR_UPDATE_PRINTER_EVENT:
794 UINT32 PrinterNameLen = 0;
795 UINT32 ConfigDataLen = 0;
796 const WCHAR* PrinterName = NULL;
797 const BYTE* ConfigData = NULL;
799 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
800 return ERROR_INVALID_DATA;
802 Stream_Read_UINT32(s, PrinterNameLen);
803 Stream_Read_UINT32(s, ConfigDataLen);
805 if (!Stream_CheckAndLogRequiredLength(TAG, s, PrinterNameLen))
806 return ERROR_INVALID_DATA;
808 PrinterName = Stream_ConstPointer(s);
809 Stream_Seek(s, PrinterNameLen);
811 if (!Stream_CheckAndLogRequiredLength(TAG, s, ConfigDataLen))
812 return ERROR_INVALID_DATA;
814 ConfigData = Stream_ConstPointer(s);
815 Stream_Seek(s, ConfigDataLen);
817 if (!printer_update_to_config(settings, PrinterName, PrinterNameLen, ConfigData,
819 return ERROR_INTERNAL_ERROR;
823 case RDPDR_DELETE_PRINTER_EVENT:
825 UINT32 PrinterNameLen = 0;
826 const WCHAR* PrinterName = NULL;
828 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
829 return ERROR_INVALID_DATA;
831 Stream_Read_UINT32(s, PrinterNameLen);
833 if (!Stream_CheckAndLogRequiredLength(TAG, s, PrinterNameLen))
834 return ERROR_INVALID_DATA;
836 PrinterName = Stream_ConstPointer(s);
837 Stream_Seek(s, PrinterNameLen);
838 printer_remove_config(settings, PrinterName, PrinterNameLen);
842 case RDPDR_RENAME_PRINTER_EVENT:
844 UINT32 OldPrinterNameLen = 0;
845 UINT32 NewPrinterNameLen = 0;
846 const WCHAR* OldPrinterName = NULL;
847 const WCHAR* NewPrinterName = NULL;
849 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
850 return ERROR_INVALID_DATA;
852 Stream_Read_UINT32(s, OldPrinterNameLen);
853 Stream_Read_UINT32(s, NewPrinterNameLen);
855 if (!Stream_CheckAndLogRequiredLength(TAG, s, OldPrinterNameLen))
856 return ERROR_INVALID_DATA;
858 OldPrinterName = Stream_ConstPointer(s);
859 Stream_Seek(s, OldPrinterNameLen);
861 if (!Stream_CheckAndLogRequiredLength(TAG, s, NewPrinterNameLen))
862 return ERROR_INVALID_DATA;
864 NewPrinterName = Stream_ConstPointer(s);
865 Stream_Seek(s, NewPrinterNameLen);
867 if (!printer_move_config(settings, OldPrinterName, OldPrinterNameLen,
868 NewPrinterName, NewPrinterNameLen))
869 return ERROR_INTERNAL_ERROR;
874 WLog_ERR(TAG,
"Unknown cache data eventID: 0x%08" PRIX32
"", eventID);
875 return ERROR_INVALID_DATA;
880 case PAKID_PRN_USING_XPS:
884 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
885 return ERROR_INVALID_DATA;
887 Stream_Read_UINT32(s, flags);
889 "Ignoring unhandled message PAKID_PRN_USING_XPS [printerID=%08" PRIx32
890 ", flags=%08" PRIx32
"]",
896 WLog_ERR(TAG,
"Unknown printing component packetID: 0x%04" PRIX16
"", packetId);
897 return ERROR_INVALID_DATA;
900 return CHANNEL_RC_OK;
908 static UINT printer_free(DEVICE* device)
911 PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*)device;
914 WINPR_ASSERT(printer_dev);
916 if (printer_dev->async)
918 (void)SetEvent(printer_dev->stopEvent);
920 if (WaitForSingleObject(printer_dev->thread, INFINITE) == WAIT_FAILED)
922 error = GetLastError();
923 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
928 #ifndef __clang_analyzer__
933 while ((irp = (IRP*)InterlockedPopEntrySList(printer_dev->pIrpList)) != NULL)
935 WINPR_ASSERT(irp->Discard);
939 (void)CloseHandle(printer_dev->thread);
940 (void)CloseHandle(printer_dev->stopEvent);
941 (void)CloseHandle(printer_dev->event);
942 winpr_aligned_free(printer_dev->pIrpList);
945 if (printer_dev->printer)
947 WINPR_ASSERT(printer_dev->printer->ReleaseRef);
948 printer_dev->printer->ReleaseRef(printer_dev->printer);
951 Stream_Free(printer_dev->device.data, TRUE);
953 return CHANNEL_RC_OK;
963 PRINTER_DEVICE* printer_dev = NULL;
964 UINT error = ERROR_INTERNAL_ERROR;
966 WINPR_ASSERT(pEntryPoints);
967 WINPR_ASSERT(printer);
969 printer_dev = (PRINTER_DEVICE*)calloc(1,
sizeof(PRINTER_DEVICE));
973 WLog_ERR(TAG,
"calloc failed!");
974 return CHANNEL_RC_NO_MEMORY;
977 printer_dev->device.data = Stream_New(NULL, 1024);
979 if (!printer_dev->device.data)
982 (void)sprintf_s(printer_dev->port,
sizeof(printer_dev->port),
"PRN%" PRIuz, printer->id);
983 printer_dev->device.type = RDPDR_DTYP_PRINT;
984 printer_dev->device.name = printer_dev->port;
985 printer_dev->device.IRPRequest = printer_irp_request;
986 printer_dev->device.CustomComponentRequest = printer_custom_component;
987 printer_dev->device.Free = printer_free;
988 printer_dev->rdpcontext = pEntryPoints->rdpcontext;
989 printer_dev->printer = printer;
992 FreeRDP_SynchronousStaticChannels))
993 printer_dev->async = TRUE;
995 if (!printer_load_from_config(pEntryPoints->rdpcontext->settings, printer, printer_dev))
998 if (printer_dev->async)
1003 if (!printer_dev->pIrpList)
1005 WLog_ERR(TAG,
"_aligned_malloc failed!");
1006 error = CHANNEL_RC_NO_MEMORY;
1010 InitializeSListHead(printer_dev->pIrpList);
1012 printer_dev->event = CreateEvent(NULL, TRUE, FALSE, NULL);
1013 if (!printer_dev->event)
1015 WLog_ERR(TAG,
"CreateEvent failed!");
1016 error = ERROR_INTERNAL_ERROR;
1020 printer_dev->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1021 if (!printer_dev->stopEvent)
1023 WLog_ERR(TAG,
"CreateEvent failed!");
1024 error = ERROR_INTERNAL_ERROR;
1029 error = pEntryPoints->RegisterDevice(pEntryPoints->devman, &printer_dev->device);
1032 WLog_ERR(TAG,
"RegisterDevice failed with error %" PRIu32
"!", error);
1036 if (printer_dev->async)
1038 printer_dev->thread =
1039 CreateThread(NULL, 0, printer_thread_func, (
void*)printer_dev, 0, NULL);
1040 if (!printer_dev->thread)
1042 WLog_ERR(TAG,
"CreateThread failed!");
1043 error = ERROR_INTERNAL_ERROR;
1048 WINPR_ASSERT(printer->AddRef);
1049 printer->AddRef(printer);
1050 return CHANNEL_RC_OK;
1052 printer_free(&printer_dev->device);
1056 static rdpPrinterDriver* printer_load_backend(
const char* backend)
1058 typedef UINT(VCAPITYPE * backend_load_t)(rdpPrinterDriver**);
1059 PVIRTUALCHANNELENTRY entry = freerdp_load_channel_addin_entry(
"printer", backend, NULL, 0);
1060 backend_load_t func = WINPR_FUNC_PTR_CAST(entry, backend_load_t);
1064 rdpPrinterDriver* printer = NULL;
1065 const UINT rc = func(&printer);
1066 if (rc != CHANNEL_RC_OK)
1077 FREERDP_ENTRY_POINT(
1081 char* driver_name = NULL;
1082 BOOL default_backend = TRUE;
1084 rdpPrinterDriver* driver = NULL;
1085 UINT error = CHANNEL_RC_OK;
1087 if (!pEntryPoints || !pEntryPoints->device)
1088 return ERROR_INVALID_PARAMETER;
1091 name = device->device.Name;
1092 driver_name = _strdup(device->DriverName);
1101 char* sep = strstr(driver_name,
":");
1104 const char* backend = sep + 1;
1106 driver = printer_load_backend(backend);
1107 default_backend = FALSE;
1111 if (!driver && default_backend)
1113 const char* backend =
1114 #if defined(WITH_CUPS)
1116 #elif defined(_WIN32)
1123 driver = printer_load_backend(backend);
1128 WLog_ERR(TAG,
"Could not get a printer driver!");
1129 error = CHANNEL_RC_INITIALIZATION_ERROR;
1133 if (name && name[0])
1135 WINPR_ASSERT(driver->GetPrinter);
1136 rdpPrinter* printer = driver->GetPrinter(driver, name, driver_name, device->IsDefault);
1140 WLog_ERR(TAG,
"Could not get printer %s!", name);
1141 error = CHANNEL_RC_INITIALIZATION_ERROR;
1145 WINPR_ASSERT(printer->ReleaseRef);
1146 if (!printer_save_default_config(pEntryPoints->rdpcontext->settings, printer))
1148 error = CHANNEL_RC_INITIALIZATION_ERROR;
1149 printer->ReleaseRef(printer);
1153 error = printer_register(pEntryPoints, printer);
1154 printer->ReleaseRef(printer);
1157 WLog_ERR(TAG,
"printer_register failed with error %" PRIu32
"!", error);
1163 WINPR_ASSERT(driver->EnumPrinters);
1164 rdpPrinter** printers = driver->EnumPrinters(driver);
1167 for (rdpPrinter** current = printers; *current; ++current)
1169 error = printer_register(pEntryPoints, *current);
1172 WLog_ERR(TAG,
"printer_register failed with error %" PRIu32
"!", error);
1179 WLog_ERR(TAG,
"Failed to enumerate printers!");
1180 error = CHANNEL_RC_INITIALIZATION_ERROR;
1183 WINPR_ASSERT(driver->ReleaseEnumPrinters);
1184 driver->ReleaseEnumPrinters(printers);
1191 WINPR_ASSERT(driver->ReleaseRef);
1192 driver->ReleaseRef(driver);
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.