Skip to content

Commit

Permalink
Send events for various state and config changes (Issue #191)
Browse files Browse the repository at this point in the history
Also implement XXX-All-Printers operations in system object and
Pause-After-Currnet-Job operation for printers.
  • Loading branch information
michaelrsweet committed May 30, 2022
1 parent fc950a1 commit 588fdca
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changes in PAPPL
Changes in v1.2b1
-----------------

- Added IPP notifications support with `papplSystemAddEvent` and
`papplSubscriptionXxx` functions (Issue #191)
- Added `papplPrinterDisable` and `papplPrinterEnable` functions and proper
support for the IPP "printer-is-accepting-jobs" attribute.
- Updated `papplPrinterSetReadyMedia` to support up to `PAPPL_MAX_SOURCE`
Expand Down
14 changes: 14 additions & 0 deletions pappl/job-process.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ _papplJobProcessRaster(

papplLogJob(job, PAPPL_LOGLEVEL_INFO, "Page %u raster data is %ux%ux%u (%s)", page, header.cupsWidth, header.cupsHeight, header.cupsBitsPerPixel, cups_cspace_string(header.cupsColorSpace));

papplSystemAddEvent(printer->system, printer, job, PAPPL_EVENT_JOB_PROGRESS, NULL);

// Set options for this page...
papplJobDeletePrintOptions(options);
options = papplJobCreatePrintOptions(job, (unsigned)job->impressions, header.cupsBitsPerPixel > 8);
Expand Down Expand Up @@ -876,6 +878,8 @@ finish_job(pappl_job_t *job) // I - Job

_papplJobRemoveFile(job);

_papplSystemAddEventNoLock(job->system, job->printer, job, PAPPL_EVENT_JOB_COMPLETED, NULL);

pthread_rwlock_unlock(&job->rwlock);

if (printer->is_stopped)
Expand All @@ -900,6 +904,8 @@ finish_job(pappl_job_t *job) // I - Job
if (!job->system->clean_time)
job->system->clean_time = time(NULL) + 60;

_papplSystemAddEventNoLock(printer->system, printer, NULL, PAPPL_EVENT_PRINTER_STATE_CHANGED, NULL);

pthread_rwlock_unlock(&printer->rwlock);

_papplSystemConfigChanged(printer->system);
Expand Down Expand Up @@ -954,6 +960,8 @@ start_job(pappl_job_t *job) // I - Job

pthread_rwlock_unlock(&job->rwlock);

_papplSystemAddEventNoLock(printer->system, printer, job, PAPPL_EVENT_JOB_STATE_CHANGED, NULL);

// Open the output device...
while (!printer->device && !printer->is_deleted && !job->is_canceled && papplSystemIsRunning(printer->system))
{
Expand All @@ -978,15 +986,21 @@ start_job(pappl_job_t *job) // I - Job
}

if (!papplSystemIsRunning(printer->system))
{
job->state = IPP_JSTATE_PENDING;

papplSystemAddEvent(job->system, job->printer, job, PAPPL_EVENT_JOB_STATE_CHANGED, NULL);
}

if (printer->device)
{
// Move the printer to the 'processing' state...
printer->state = IPP_PSTATE_PROCESSING;
printer->state_time = time(NULL);
}

_papplSystemAddEventNoLock(printer->system, printer, NULL, PAPPL_EVENT_PRINTER_STATE_CHANGED, NULL);

pthread_rwlock_unlock(&printer->rwlock);

return (printer->device != NULL);
Expand Down
4 changes: 3 additions & 1 deletion pappl/job.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// Job object for the Printer Application Framework
//
// Copyright © 2019-2021 by Michael R Sweet.
// Copyright © 2019-2022 by Michael R Sweet.
// Copyright © 2010-2019 by Apple Inc.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
Expand Down Expand Up @@ -53,6 +53,8 @@ papplJobCancel(pappl_job_t *job) // I - Job
job->system->clean_time = time(NULL) + 60;

pthread_rwlock_unlock(&job->rwlock);

papplSystemAddEvent(job->system, job->printer, job, PAPPL_EVENT_JOB_COMPLETED, NULL);
}


Expand Down
6 changes: 6 additions & 0 deletions pappl/printer-accessors.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ papplPrinterDisable(
if (printer)
{
printer->is_accepting = false;
papplSystemAddEvent(printer->system, printer, NULL, PAPPL_EVENT_PRINTER_STATE_CHANGED, NULL);
}
}

Expand All @@ -70,6 +71,7 @@ papplPrinterEnable(
if (printer)
{
printer->is_accepting = true;
papplSystemAddEvent(printer->system, printer, NULL, PAPPL_EVENT_PRINTER_STATE_CHANGED, NULL);
}
}

Expand Down Expand Up @@ -793,6 +795,8 @@ papplPrinterPause(
else
printer->state = IPP_PSTATE_STOPPED;

_papplSystemAddEventNoLock(printer->system, printer, NULL, PAPPL_EVENT_PRINTER_STATE_CHANGED | PAPPL_EVENT_PRINTER_STOPPED, NULL);

pthread_rwlock_unlock(&printer->rwlock);
}

Expand All @@ -815,6 +819,8 @@ papplPrinterResume(
printer->is_stopped = false;
printer->state = IPP_PSTATE_IDLE;

_papplSystemAddEventNoLock(printer->system, printer, NULL, PAPPL_EVENT_PRINTER_STATE_CHANGED, NULL);

pthread_rwlock_unlock(&printer->rwlock);

_papplPrinterCheckJobs(printer);
Expand Down
13 changes: 8 additions & 5 deletions pappl/printer-ipp.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,7 @@ _papplPrinterProcessIPP(
break;

case IPP_OP_PAUSE_PRINTER :
case IPP_OP_PAUSE_PRINTER_AFTER_CURRENT_JOB :
ipp_pause_printer(client);
break;

Expand Down Expand Up @@ -1146,31 +1147,31 @@ _papplPrinterSetAttributes(
if (ippGetStatusCode(client->response) != IPP_STATUS_OK)
{
cupsFreeOptions(num_vendor, vendor);
return (0);
return (false);
}

// Now apply changes...
if (do_defaults && !papplPrinterSetDriverDefaults(printer, &driver_data, num_vendor, vendor))
{
papplClientRespondIPP(client, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, "One or more attribute values were not supported.");
cupsFreeOptions(num_vendor, vendor);
return (0);
return (false);
}

cupsFreeOptions(num_vendor, vendor);

if (do_ready && !papplPrinterSetReadyMedia(printer, driver_data.num_source, driver_data.media_ready))
{
papplClientRespondIPP(client, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, "One or more attribute values were not supported.");
return (0);
return (false);
}

if (do_wifi)
{
if (!(printer->system->wifi_join_cb)(printer->system, printer->system->wifi_cbdata, wifi_ssid, wifi_password))
{
papplClientRespondIPP(client, IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES, "Unable to join Wi-Fi network '%s'.", wifi_ssid);
return (0);
return (false);
}
}

Expand All @@ -1189,7 +1190,9 @@ _papplPrinterSetAttributes(
if (org_unit)
papplPrinterSetGeoLocation(printer, org_unit);

return (1);
papplSystemAddEvent(printer->system, printer, NULL, PAPPL_EVENT_PRINTER_CONFIG_CHANGED, NULL);

return (true);
}


Expand Down
1 change: 1 addition & 0 deletions pappl/printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ papplPrinterCreate(
IPP_OP_GET_NOTIFICATIONS,
IPP_OP_ENABLE_PRINTER,
IPP_OP_DISABLE_PRINTER,
IPP_OP_PAUSE_PRINTER_AFTER_CURRENT_JOB,
IPP_OP_CANCEL_CURRENT_JOB,
IPP_OP_CANCEL_JOBS,
IPP_OP_CANCEL_MY_JOBS,
Expand Down
164 changes: 157 additions & 7 deletions pappl/system-ipp.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ typedef struct _pappl_attr_s // Input attribute structure
// Local functions...
//

static void ipp_create_printer(pappl_client_t *client);
static void ipp_delete_printer(pappl_client_t *client);
static void ipp_get_printers(pappl_client_t *client);
static void ipp_get_system_attributes(pappl_client_t *client);
static void ipp_set_system_attributes(pappl_client_t *client);
static void ipp_shutdown_all_printers(pappl_client_t *client);

static void ipp_create_printer(pappl_client_t *client);
static void ipp_delete_printer(pappl_client_t *client);
static void ipp_disable_all_printers(pappl_client_t *client);
static void ipp_enable_all_printers(pappl_client_t *client);
static void ipp_get_printers(pappl_client_t *client);
static void ipp_get_system_attributes(pappl_client_t *client);
static void ipp_pause_all_printers(pappl_client_t *client);
static void ipp_resume_all_printers(pappl_client_t *client);
static void ipp_set_system_attributes(pappl_client_t *client);
static void ipp_shutdown_all_printers(pappl_client_t *client);

//
// '_papplSystemProcessIPP()' - Process an IPP System request.
Expand Down Expand Up @@ -76,6 +79,23 @@ _papplSystemProcessIPP(
ipp_set_system_attributes(client);
break;

case IPP_OP_DISABLE_ALL_PRINTERS :
ipp_disable_all_printers(client);
break;

case IPP_OP_ENABLE_ALL_PRINTERS :
ipp_enable_all_printers(client);
break;

case IPP_OP_PAUSE_ALL_PRINTERS :
case IPP_OP_PAUSE_ALL_PRINTERS_AFTER_CURRENT_JOB :
ipp_pause_all_printers(client);
break;

case IPP_OP_RESUME_ALL_PRINTERS :
ipp_resume_all_printers(client);
break;

case IPP_OP_SHUTDOWN_ALL_PRINTERS :
ipp_shutdown_all_printers(client);
break;
Expand Down Expand Up @@ -248,6 +268,8 @@ ipp_create_printer(
return;
}

papplSystemAddEvent(printer->system, printer, NULL, PAPPL_EVENT_PRINTER_CREATED, NULL);

if (!_papplPrinterSetAttributes(client, printer))
return;

Expand Down Expand Up @@ -291,6 +313,8 @@ ipp_delete_printer(
return;
}

papplSystemAddEvent(client->system, client->printer, NULL, PAPPL_EVENT_PRINTER_DELETED, NULL);

if (!client->printer->processing_job)
papplPrinterDelete(client->printer);
else
Expand All @@ -300,6 +324,68 @@ ipp_delete_printer(
}


//
// 'ipp_disable_all_printers()' - Disable all printers.
//

static void
ipp_disable_all_printers(
pappl_client_t *client) // I - Client
{
pappl_system_t *system = client->system;
// System
pappl_printer_t *printer; // Current printer
http_status_t auth_status; // Authorization status


// Verify the connection is authorized...
if ((auth_status = papplClientIsAuthorized(client)) != HTTP_STATUS_CONTINUE)
{
papplClientRespond(client, auth_status, NULL, NULL, 0, 0);
return;
}

// Loop through the printers...
pthread_rwlock_rdlock(&system->rwlock);
for (printer = (pappl_printer_t *)cupsArrayGetFirst(system->printers); printer; printer = (pappl_printer_t *)cupsArrayGetNext(system->printers))
{
papplPrinterDisable(printer);
}
pthread_rwlock_unlock(&system->rwlock);
}


//
// 'ipp_enable_all_printers()' - Enable all printers.
//

static void
ipp_enable_all_printers(
pappl_client_t *client) // I - Client
{
pappl_system_t *system = client->system;
// System
pappl_printer_t *printer; // Current printer
http_status_t auth_status; // Authorization status


// Verify the connection is authorized...
if ((auth_status = papplClientIsAuthorized(client)) != HTTP_STATUS_CONTINUE)
{
papplClientRespond(client, auth_status, NULL, NULL, 0, 0);
return;
}

// Loop through the printers...
pthread_rwlock_rdlock(&system->rwlock);
for (printer = (pappl_printer_t *)cupsArrayGetFirst(system->printers); printer; printer = (pappl_printer_t *)cupsArrayGetNext(system->printers))
{
papplPrinterEnable(printer);
}
pthread_rwlock_unlock(&system->rwlock);
}


//
// 'ipp_get_printers()' - Get printers.
//
Expand Down Expand Up @@ -554,6 +640,68 @@ ipp_get_system_attributes(
}


//
// 'ipp_pause_all_printers()' - all printers.
//

static void
ipp_pause_all_printers(
pappl_client_t *client) // I - Client
{
pappl_system_t *system = client->system;
// System
pappl_printer_t *printer; // Current printer
http_status_t auth_status; // Authorization status


// Verify the connection is authorized...
if ((auth_status = papplClientIsAuthorized(client)) != HTTP_STATUS_CONTINUE)
{
papplClientRespond(client, auth_status, NULL, NULL, 0, 0);
return;
}

// Loop through the printers...
pthread_rwlock_rdlock(&system->rwlock);
for (printer = (pappl_printer_t *)cupsArrayGetFirst(system->printers); printer; printer = (pappl_printer_t *)cupsArrayGetNext(system->printers))
{
papplPrinterPause(client->printer);
}
pthread_rwlock_unlock(&system->rwlock);
}


//
// 'ipp_resume_all_printers()' - all printers.
//

static void
ipp_resume_all_printers(
pappl_client_t *client) // I - Client
{
pappl_system_t *system = client->system;
// System
pappl_printer_t *printer; // Current printer
http_status_t auth_status; // Authorization status


// Verify the connection is authorized...
if ((auth_status = papplClientIsAuthorized(client)) != HTTP_STATUS_CONTINUE)
{
papplClientRespond(client, auth_status, NULL, NULL, 0, 0);
return;
}

// Loop through the printers...
pthread_rwlock_rdlock(&system->rwlock);
for (printer = (pappl_printer_t *)cupsArrayGetFirst(system->printers); printer; printer = (pappl_printer_t *)cupsArrayGetNext(system->printers))
{
papplPrinterResume(client->printer);
}
pthread_rwlock_unlock(&system->rwlock);
}


//
// 'ipp_set_system_attributes()' - Set system attributes.
//
Expand Down Expand Up @@ -674,6 +822,8 @@ ipp_set_system_attributes(

pthread_rwlock_unlock(&system->rwlock);

papplSystemAddEvent(system, NULL, NULL, PAPPL_EVENT_SYSTEM_CONFIG_CHANGED, NULL);

papplClientRespondIPP(client, IPP_STATUS_OK, NULL);
}

Expand Down
Loading

0 comments on commit 588fdca

Please sign in to comment.