diff options
| author | Manolo Gouy <Manolo> | 2015-03-12 15:24:15 +0000 |
|---|---|---|
| committer | Manolo Gouy <Manolo> | 2015-03-12 15:24:15 +0000 |
| commit | 2b952dda54654022db8f595e23815fbbc4260ba7 (patch) | |
| tree | 1a901aa4d273fb7d3edb0a3f2726df8e499a0f10 /src | |
| parent | 1c3163f206606070c112c8f09a758990a8f2e142 (diff) | |
Linux/unix: added support for BSD-style printing, that is, using lpq/lpr instead of lpstat/lp .
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@10619 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src')
| -rw-r--r-- | src/Fl_PostScript.cxx | 9 | ||||
| -rw-r--r-- | src/print_panel.cxx | 58 | ||||
| -rw-r--r-- | src/print_panel.h | 3 |
3 files changed, 51 insertions, 19 deletions
diff --git a/src/Fl_PostScript.cxx b/src/Fl_PostScript.cxx index 3eed623ea..c5ef92528 100644 --- a/src/Fl_PostScript.cxx +++ b/src/Fl_PostScript.cxx @@ -1561,7 +1561,7 @@ int Fl_PostScript_Printer::start_job(int pages, int *firstpage, int *lastpage) { // first test version for print dialog if (!print_panel) make_print_panel(); - print_load(); + printing_style style = print_load(); print_selection->deactivate(); print_all->setonly(); print_all->do_callback(); @@ -1635,9 +1635,10 @@ int Fl_PostScript_Printer::start_job(int pages, int *firstpage, int *lastpage) { // Print: pipe the output into the lp command... char command[1024]; - snprintf(command, sizeof(command), "lp -s -d %s -n %d -t '%s' -o media=%s", - printer, print_collate_button->value() ? 1 : (int)(print_copies->value() + 0.5), - "FLTK", media); + if (style == SystemV) snprintf(command, sizeof(command), "lp -s -d %s -n %d -t '%s' -o media=%s", + printer, print_collate_button->value() ? 1 : (int)(print_copies->value() + 0.5), "FLTK", media); + else snprintf(command, sizeof(command), "lpr -h -P %s -# %d -T FLTK ", + printer, print_collate_button->value() ? 1 : (int)(print_copies->value() + 0.5)); Fl_PostScript_Graphics_Driver *ps = driver(); ps->output = popen(command, "w"); diff --git a/src/print_panel.cxx b/src/print_panel.cxx index 3a4392c5b..948ee12b5 100644 --- a/src/print_panel.cxx +++ b/src/print_panel.cxx @@ -513,10 +513,11 @@ void print_cb(Fl_Return_Button *, void *) { print_panel->hide(); } -void print_load() { +printing_style print_load() { // return whether SystemV or BSD printing style is used FILE *lpstat; - char line[1024], name[1024], *nptr, qname[2048], *qptr, defname[1024]; + char line[1024], name[1024], *nptr, qname[2048], *qptr, defname[1024], *p; int i; + printing_style style = SystemV; if (print_choice->size() > 1) { for (i = 1; print_choice->text(i); i ++) { @@ -531,8 +532,8 @@ void print_load() { print_start = 0; defname[0] = '\0'; - - if ((lpstat = popen("LC_MESSAGES=C LANG=C lpstat -p -d", "r")) != NULL) { +// get names of all printers and of default one + if ((lpstat = popen("LC_MESSAGES=C LANG=C /bin/sh -c '(lpstat -p -d ) 2>&-'", "r")) != NULL) { // try first with SystemV printing system while (fgets(line, sizeof(line), lpstat)) { if (!strncmp(line, "printer ", 8) && sscanf(line + 8, "%s", name) == 1) { @@ -548,18 +549,40 @@ void print_load() { } pclose(lpstat); } + fclose(stderr); + + if (print_choice->size() == 2 && (lpstat = fopen("/etc/printcap", "r"))) { // try next with BSD printing system + while (fgets(line, sizeof(line),lpstat)) { // get names of all known printers + if (*line == '#' || (p = strchr(line, '|')) == NULL) continue; + *p = 0; + print_choice->add(line, 0, 0, (void *)strdup(line), 0); + style = BSD; + *p = '|'; + while (1) { + p = line + strlen(line) - 1; + if (*p == '\n' && p > line) p--; + if (*p != '\\') break; + fgets(line, sizeof(line),lpstat); + } + } + fclose(lpstat); + p = getenv("PRINTER"); // get name of default printer + if (p == NULL) p = (char*)"lp"; + strcpy(defname, p); + } - if (defname[0]) { + if (print_choice->size() > 2) print_choice->value(1); + if (defname[0]) { // select default printer in menu for (i = 1; print_choice->text(i); i ++) { if (!strcmp((char *)print_choice->menu()[i].user_data(), defname)) { print_choice->value(i); break; } } - } else if (print_choice->size() > 2) print_choice->value(1); + } print_update_status(); - + return style; } // print_load() void print_update_status() { @@ -568,14 +591,21 @@ void print_update_status() { static char status[1024]; const char *printer = (const char *)print_choice->menu()[print_choice->value()].user_data(); + status[0] = 0; if (print_choice->value()) { - snprintf(command, sizeof(command), "lpstat -p '%s'", printer); - if ((lpstat = popen(command, "r")) != NULL) { - if (fgets(status, sizeof(status), lpstat)==0) { /* ignore */ } - pclose(lpstat); - } else strcpy(status, "printer status unavailable"); - } else status[0] = '\0'; - + strcpy(status, "printer status unavailable"); + snprintf(command, sizeof(command), "/bin/sh -c \"(lpstat -p '%s' ) 2>&-\" ", printer); // try first with SystemV printing system + if ((lpstat = popen(command, "r")) != NULL) { + if (fgets(status, sizeof(status), lpstat) == 0) { // if no reply + pclose(lpstat); + snprintf(command, sizeof(command), "lpq -P%s", printer); // try next with BSD printing system + if ((lpstat = popen(command, "r")) != NULL) { + fgets(status, sizeof(status), lpstat); + } + } + pclose(lpstat); + } + } print_status->label(status); char name[1024]; diff --git a/src/print_panel.h b/src/print_panel.h index 72668fbd9..35ead8ad7 100644 --- a/src/print_panel.h +++ b/src/print_panel.h @@ -35,9 +35,10 @@ #include <FL/Fl_Check_Button.H> #include <FL/Fl_Return_Button.H> #include <FL/Fl_Progress.H> +enum printing_style {SystemV, BSD}; static Fl_Double_Window* make_print_panel(); static void print_cb(Fl_Return_Button *, void *); -static void print_load(); +static printing_style print_load(); static void print_update_status(); #endif |
