MCDE: print a file through McdeSys; dtpaint Print... (D4)
McdeSys gains mcde_sys_print_file(path, printer): it looks for lp(1) (/usr/bin, /usr/local/bin, /usr/sbin, pkg prefix; MCDE_PRINT_COMMAND overrides it for tests), passes -d PRINTER when one is named, and runs it as the existing job (fd into the event loop, finish/cancel). dtpaint links libMcdeSys, adds File > Print... (Ctrl+P): it picks the default queue from mcde_sys_printers (or the first), writes the image to /tmp/dtpaint-PID.bmp, starts the job and removes the file when the job ends; errors and 'no printer is set up' go to the information dialog. Measured under Xvfb with MCDE_PRINT_COMMAND=fake lp: argv '-d HP_LaserJet_M110w_CC1016 /tmp/dtpaint-3543226.bmp', the fake copied a valid 640x480 BMP and the temporary file was gone; no physical page was printed (the real queue is the user's step).
This commit is contained in:
1 parent
fa06dc280a
commit
7c0281f48a
6 files changed
+119
-4
No files matched your search
@@ -732,6 +732,11 @@ int mcde_sys_printers(McdeSysPrinter *list, size_t max);
|
||||
/* the default queue for new jobs (root: needs the administrator) */
|
||||
int mcde_sys_printer_default(const char *name);
|
||||
|
||||
/* Spool PATH to PRINTER (NULL or "" = the default queue) with lp(1), as a
|
||||
* job for the event loop (mcde_sys_job_fd/_finish/_cancel). MCDE_PRINT_COMMAND
|
||||
* names the tool instead, for tests with a fake lp. */
|
||||
McdeSysJob *mcde_sys_print_file(const char *path, const char *printer);
|
||||
|
||||
/* a queue for URI with the driverless model (lpadmin -p NAME -E -v URI -m
|
||||
* everywhere); root, a name without spaces or slashes, an ipp:// URI */
|
||||
int mcde_sys_printer_add(const char *name, const char *uri);
|
||||
|
||||
@@ -142,6 +142,39 @@ mcde_sys_printer_default(const char *name)
|
||||
return MCDE_SYS_OK;
|
||||
}
|
||||
|
||||
McdeSysJob *
|
||||
mcde_sys_print_file(const char *path, const char *printer)
|
||||
{
|
||||
static const char *const where[] = { "/usr/bin/lp",
|
||||
"/usr/local/bin/lp", "/usr/sbin/lp", MCDE_PKG_PREFIX "/bin/lp",
|
||||
NULL };
|
||||
const char *lp = getenv("MCDE_PRINT_COMMAND");
|
||||
char *argv[5];
|
||||
int n = 0;
|
||||
|
||||
if (path == NULL || path[0] == '\0' || access(path, R_OK) != 0) {
|
||||
(void) mcde_sys_fail(MCDE_SYS_ENOTSUP,
|
||||
mcde_sys_msg(MSG_NOT_THERE, "%s is not there."),
|
||||
path != NULL ? path : "");
|
||||
return NULL;
|
||||
}
|
||||
if (lp == NULL)
|
||||
lp = Tool(where);
|
||||
if (lp == NULL) {
|
||||
(void) mcde_sys_fail(MCDE_SYS_ENOTSUP,
|
||||
mcde_sys_msg(MSG_NOT_THERE, "%s is not there."), "lp");
|
||||
return NULL;
|
||||
}
|
||||
argv[n++] = (char *) lp;
|
||||
if (printer != NULL && printer[0] != '\0') {
|
||||
argv[n++] = (char *) "-d";
|
||||
argv[n++] = (char *) printer;
|
||||
}
|
||||
argv[n++] = (char *) path;
|
||||
argv[n] = NULL;
|
||||
return mcde_sys_job_start(argv, NULL);
|
||||
}
|
||||
|
||||
/* the checks every name and URI pass before they reach a command line */
|
||||
static int
|
||||
NameOk(const char *name)
|
||||
|
||||
@@ -5,7 +5,7 @@ bin_PROGRAMS = dtpaint
|
||||
dtpaint_CPPFLAGS = -I$(top_srcdir)/include \
|
||||
-DCDE_INSTALLATION_TOP=\"$(CDE_INSTALLATION_TOP)\"
|
||||
|
||||
dtpaint_LDADD = $(DTCLIENTLIBS) $(XTOOLLIB)
|
||||
dtpaint_LDADD = $(LIBMCDESYS) $(DTCLIENTLIBS) $(XTOOLLIB)
|
||||
|
||||
if SOLARIS
|
||||
dtpaint_LDADD += -lintl
|
||||
|
||||
@@ -36,6 +36,8 @@
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
|
||||
#include <Mcde/McdeSys.h>
|
||||
|
||||
#include <Xm/Xm.h>
|
||||
#include <Xm/CascadeB.h>
|
||||
#include <Xm/DrawingA.h>
|
||||
@@ -108,6 +110,7 @@ static const char *const toolNames[] = {
|
||||
};
|
||||
|
||||
static Display *dpy;
|
||||
static XtAppContext appctx;
|
||||
static Screen *scr;
|
||||
static Visual *vis;
|
||||
static Colormap cmap;
|
||||
@@ -2146,7 +2149,7 @@ SwatchActivate(Widget w, XtPointer data, XtPointer call)
|
||||
/* ------------------------------------------------------------------ menus */
|
||||
|
||||
enum {
|
||||
C_NEW, C_OPEN, C_SAVE, C_SAVEAS, C_EXIT,
|
||||
C_NEW, C_OPEN, C_SAVE, C_SAVEAS, C_PRINT, C_EXIT,
|
||||
C_UNDO, C_CLEAR, C_CLEARSEL, C_SELALL, C_INVERT, C_FLIP,
|
||||
C_TOOLS, C_COLORS, C_STATUS, C_HELP, C_ABOUT, C_ZNORMAL, C_ZLARGE,
|
||||
C_ATTR, C_EDITCOLORS
|
||||
@@ -2189,10 +2192,10 @@ ShowError(int msgid, const char *path)
|
||||
}
|
||||
|
||||
static void
|
||||
ShowInfo(int bodyId, const char *bodyFb, int titleId, const char *titleFb)
|
||||
ShowText(const char *body, int titleId, const char *titleFb)
|
||||
{
|
||||
Widget d = XmCreateInformationDialog(top, "info", NULL, 0);
|
||||
XmString s = Msg(bodyId, bodyFb);
|
||||
XmString s = XmStringCreateLocalized((String) body);
|
||||
|
||||
XtVaSetValues(d, XmNmessageString, s, NULL);
|
||||
XmStringFree(s);
|
||||
@@ -2205,6 +2208,68 @@ ShowInfo(int bodyId, const char *bodyFb, int titleId, const char *titleFb)
|
||||
XtManageChild(d);
|
||||
}
|
||||
|
||||
static void
|
||||
ShowInfo(int bodyId, const char *bodyFb, int titleId, const char *titleFb)
|
||||
{
|
||||
ShowText(PaintMsg(bodyId, bodyFb), titleId, titleFb);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- printing */
|
||||
|
||||
static McdeSysJob *printJob;
|
||||
static char printPath[256];
|
||||
|
||||
static void
|
||||
PrintDone(XtPointer cd, int *fd, XtInputId *id)
|
||||
{
|
||||
McdeSysJob *job = (McdeSysJob *) cd;
|
||||
int rc = mcde_sys_job_finish(job);
|
||||
|
||||
(void) fd;
|
||||
XtRemoveInput(*id);
|
||||
if (rc != MCDE_SYS_OK)
|
||||
ShowText(mcde_sys_error(), 51, "Print");
|
||||
unlink(printPath);
|
||||
printJob = NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
PrintImage(void)
|
||||
{
|
||||
McdeSysPrinter list[32];
|
||||
const char *printer = NULL;
|
||||
int n, i;
|
||||
|
||||
if (printJob != NULL)
|
||||
return;
|
||||
n = mcde_sys_printers(list, 32);
|
||||
if (n <= 0) {
|
||||
ShowInfo(50, "No printer is set up.", 51, "Print");
|
||||
return;
|
||||
}
|
||||
for (i = 0; i < n; i++)
|
||||
if (list[i].is_default) {
|
||||
printer = list[i].name;
|
||||
break;
|
||||
}
|
||||
if (printer == NULL)
|
||||
printer = list[0].name;
|
||||
snprintf(printPath, sizeof printPath, "/tmp/dtpaint-%d.bmp",
|
||||
(int) getpid());
|
||||
if (SaveBMP(printPath) != 0) {
|
||||
ShowError(27, printPath);
|
||||
return;
|
||||
}
|
||||
printJob = mcde_sys_print_file(printPath, printer);
|
||||
if (printJob == NULL) {
|
||||
unlink(printPath);
|
||||
ShowText(mcde_sys_error(), 51, "Print");
|
||||
return;
|
||||
}
|
||||
XtAppAddInput(appctx, mcde_sys_job_fd(printJob),
|
||||
(XtPointer) XtInputReadMask, PrintDone, printJob);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------- image size and colours */
|
||||
|
||||
static void
|
||||
@@ -2526,6 +2591,9 @@ Command(Widget w, XtPointer data, XtPointer call)
|
||||
case C_SAVEAS:
|
||||
FileDialog(1);
|
||||
break;
|
||||
case C_PRINT:
|
||||
PrintImage();
|
||||
break;
|
||||
case C_EXIT:
|
||||
MaybeDiscard(A_EXIT);
|
||||
break;
|
||||
@@ -2791,6 +2859,7 @@ main(int argc, char **argv)
|
||||
selftest = 1;
|
||||
|
||||
top = XtVaAppInitialize(&app, "Dtpaint", NULL, 0, &argc, argv, NULL, NULL);
|
||||
appctx = app;
|
||||
dpy = XtDisplay(top);
|
||||
(void) srand((unsigned) getpid());
|
||||
InitColor();
|
||||
@@ -2884,6 +2953,8 @@ main(int argc, char **argv)
|
||||
AddItem(m, PaintMsg(9, "Save"), "Ctrl<Key>S", "Ctrl+S", C_SAVE, 0, True);
|
||||
AddItem(m, PaintMsg(10, "Save As..."), NULL, NULL, C_SAVEAS, 0, True);
|
||||
AddItem(m, NULL, NULL, NULL, 0, 1, True);
|
||||
AddItem(m, PaintMsg(53, "Print..."), "Ctrl<Key>P", "Ctrl+P",
|
||||
C_PRINT, 0, True);
|
||||
AddItem(m, PaintMsg(11, "Exit"), "Alt<Key>F4", "Alt+F4", C_EXIT, 0, True);
|
||||
m = AddMenu(menubarW, PaintMsg(2, "Edit"));
|
||||
AddItem(m, PaintMsg(12, "Undo"), "Ctrl<Key>Z", "Ctrl+Z", C_UNDO, 0, True);
|
||||
|
||||
@@ -51,3 +51,6 @@ $set 1
|
||||
47 Blue:
|
||||
48 OK
|
||||
49 Cancel
|
||||
50 No printer is set up.
|
||||
51 Print
|
||||
53 Print...
|
||||
@@ -52,3 +52,6 @@ $set 1
|
||||
47 Blau:
|
||||
48 OK
|
||||
49 Abbrechen
|
||||
50 Es ist kein Drucker eingerichtet.
|
||||
51 Drucken
|
||||
53 Drucken...
|
||||
Reference in new issue
Block a user