| |
| /pliant/graphic/filter/ijs.pli |
| |
| 1 |
abstract | |
| 2 |
[An interface enabling to use any IJS compatible driver.] | |
| 3 |
| |
| 4 |
# Copyright Hubert Tonneau hubert.tonneau@pliant.cx | |
| 5 |
# | |
| 6 |
# This program is free software; you can redistribute it and/or | |
| 7 |
# modify it under the terms of the GNU General Public License version 2 | |
| 8 |
# as published by the Free Software Foundation. | |
| 9 |
# | |
| 10 |
# This program is distributed in the hope that it will be useful, | |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 |
# GNU General Public License for more details. | |
| 14 |
# | |
| 15 |
# You should have received a copy of the GNU General Public License | |
| 16 |
# version 2 along with this program; if not, write to the Free Software | |
| 17 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 18 |
| |
| 19 |
| |
| 20 |
module "/pliant/language/compiler.pli" | |
| 21 |
module "/pliant/language/stream.pli" | |
| 22 |
module "/pliant/language/stream/pipe.pli" | |
| 23 |
module "/pliant/admin/execute.pli" | |
| 24 |
module "prototype.pli" | |
| 25 |
module "/pliant/graphic/color/gamut.pli" | |
| 26 |
| |
| 27 |
constant verbose false | |
| 28 |
| |
| 29 |
| |
| 30 |
constant ijs_cmd_ack 0 | |
| 31 |
constant ijs_cmd_nak 1 | |
| 32 |
constant ijs_cmd_ping 2 | |
| 33 |
constant ijs_cmd_pong 3 | |
| 34 |
constant ijs_cmd_open 4 | |
| 35 |
constant ijs_cmd_close 5 | |
| 36 |
constant ijs_cmd_begin_job 6 | |
| 37 |
constant ijs_cmd_end_job 7 | |
| 38 |
constant ijs_cmd_cancel_job 8 | |
| 39 |
constant ijs_cmd_query_status 9 | |
| 40 |
constant ijs_cmd_list_params 10 | |
| 41 |
constant ijs_cmd_enum_param 11 | |
| 42 |
constant ijs_cmd_set_param 12 | |
| 43 |
constant ijs_cmd_get_param 13 | |
| 44 |
constant ijs_cmd_begin_page 14 | |
| 45 |
constant ijs_cmd_send_data_block 15 | |
| 46 |
constant ijs_cmd_end_page 16 | |
| 47 |
constant ijs_cmd_exit 17 | |
| 48 |
| |
| 49 |
function ijs_int i -> s | |
| 50 |
arg Int i ; arg Str s | |
| 51 |
s := "...." | |
| 52 |
s:characters map uInt32_hi := i | |
| 53 |
| |
| 54 |
function ijs_send cmd num parameters | |
| 55 |
arg_rw Stream cmd ; arg Int num ; arg Str parameters | |
| 56 |
var uInt32_hi i32 := num ; cmd raw_write addressof:i32 4 | |
| 57 |
var uInt32_hi i32 := 8+parameters:len ; cmd raw_write addressof:i32 4 | |
| 58 |
cmd writechars parameters | |
| 59 |
cmd flush anytime | |
| 60 |
| |
| 61 |
function ijs_receive ack num parameters | |
| 62 |
arg_rw Stream ack ; arg_w Int num ; arg_w Str parameters | |
| 63 |
ack raw_read addressof:(var uInt32_hi cmd) 4 | |
| 64 |
ack raw_read addressof:(var uInt32_hi len) 4 | |
| 65 |
num := cmd | |
| 66 |
parameters := repeat (min (max (cast len Int)-8 0) 2^16) " " | |
| 67 |
ack raw_read parameters:characters parameters:len | |
| 68 |
| |
| 69 |
function ijs_command cmd ack num parameters -> err | |
| 70 |
arg_rw Stream cmd ack ; arg Int num ; arg Str parameters ; arg Int err | |
| 71 |
ijs_send cmd num parameters | |
| 72 |
ijs_receive ack (var Int n) (var Str p) | |
| 73 |
err := n | |
| 74 |
if verbose | |
| 75 |
console " ijs command " num " -> " n " " p | |
| 76 |
if err<>0 and p:len=4 | |
| 77 |
console " (" (cast (p:characters map uInt32_hi) Int) ")" | |
| 78 |
console eol | |
| 79 |
| |
| 80 |
function ijs_set cmd ack param value -> err | |
| 81 |
arg_rw Stream cmd ack ; arg Str param value ; arg Int err | |
| 82 |
if verbose | |
| 83 |
console " " param " = " string:value | |
| 84 |
err := ijs_command cmd ack ijs_cmd_set_param ijs_int:0+(ijs_int param:len+1+value:len)+param+"[0]"+value | |
| 85 |
| |
| 86 |
| |
| 87 |
type ImageWriteFilterIjs | |
| 88 |
field Pointer:Stream stream | |
| 89 |
field Stream cmd ack | |
| 90 |
field Int line_size | |
| 91 |
field FastSem eot | |
| 92 |
| |
| 93 |
ImageWriteFilter maybe ImageWriteFilterIjs | |
| 94 |
| |
| 95 |
| |
| 96 |
method f open stream options h -> status | |
| 97 |
arg_rw ImageWriteFilterIjs f ; arg_rw Stream stream ; arg Str options ; arg ImagePrototype h ; arg ExtendedStatus status | |
| 98 |
var Str model := options option "model" Str | |
| 99 |
if model="" | |
| 100 |
return failure:"You must specify the printer model" | |
| 101 |
f stream :> stream | |
| 102 |
f line_size := h line_size | |
| 103 |
stream_pipe (var Str pipe1_in) (var Str pipe1_out) | |
| 104 |
stream_pipe (var Str pipe2_in) (var Str pipe2_out) | |
| 105 |
stream_pipe (var Str data_in) (var Str data_out) | |
| 106 |
thread | |
| 107 |
var Str program := options option "ijs_server" Str "hpijs" | |
| 108 |
var Str root := shunt (file_query "file:/bin/"+program standard)=success "file:/" "embedded:/" | |
| 109 |
execute program root root path root input pipe1_in output pipe2_out error data_out | |
| 110 |
f:eot request | |
| 111 |
thread | |
| 112 |
share f | |
| 113 |
(var Stream output) open data_in in+safe | |
| 114 |
while not output:atend | |
| 115 |
output read_available (var Address adr) (var Int size) | |
| 116 |
f:stream raw_write adr size | |
| 117 |
f:eot release | |
| 118 |
f:cmd open pipe1_out out+safe | |
| 119 |
f:ack open pipe2_in in+safe | |
| 120 |
f:cmd writechars "IJS[lf]"+character:170+"v1[lf]" ; f:cmd flush anytime | |
| 121 |
var Str l := f:ack readline | |
| 122 |
if l<>"IJS" | |
| 123 |
return (failure "No IJS server ack (line 1 is '"+l+"')") | |
| 124 |
var Str l := f:ack readline | |
| 125 |
if l<>character:171+"v1" | |
| 126 |
return failure:"No IJS server ack (line 2)" | |
| 127 |
ijs_command f:cmd f:ack ijs_cmd_open "" | |
| 128 |
ijs_command f:cmd f:ack ijs_cmd_begin_job ijs_int:0 | |
| 129 |
# ijs_command f:cmd f:ack ijs_cmd_list_params ijs_int:0 | |
| 130 |
ijs_set f:cmd f:ack "OutputFD" "2" | |
| 131 |
ijs_set f:cmd f:ack "DeviceManufacturer" (options option "manufacturer" Str "HP") | |
| 132 |
if (ijs_set f:cmd f:ack "DeviceModel" model)<>0 # "hp color LaserJet" | |
| 133 |
return failure:"unknown model" | |
| 134 |
ijs_set f:cmd f:ack "Dpi" string:(cast h:size_x/(h:x1-h:x0)*25.4 Int)+"x"+string:(cast h:size_y/(h:y1-h:y0)*25.4 Int) | |
| 135 |
ijs_set f:cmd f:ack "Width" (string h:size_x) | |
| 136 |
ijs_set f:cmd f:ack "Height" (string h:size_y) | |
| 137 |
ijs_set f:cmd f:ack "BitsPerSample" "8" | |
| 138 |
ijs_set f:cmd f:ack "NumChan" (string h:gamut:pixel_size) | |
| 139 |
ijs_set f:cmd f:ack "ColorSpace" (shunt h:gamut:pixel_size=1 "DeviceGray" h:gamut:pixel_size=3 (shunt (options option "sRGB") "sRGB" "DeviceRGB") "") | |
| 140 |
ijs_set f:cmd f:ack "Quality:Quality" string:(options option "ijs_quality" Int 2) | |
| 141 |
ijs_set f:cmd f:ack "Quality:ColorMode" string:(options option "ijs_colormode" Int 2) | |
| 142 |
ijs_set f:cmd f:ack "Quality:MediaType" string:(options option "ijs_mediatype" Int 0) | |
| 143 |
ijs_set f:cmd f:ack "Quality:PenSet" string:(options option "ijs_penset" Int 2) | |
| 144 |
ijs_set f:cmd f:ack "PaperSize" (options option "ijs_papersize" Str "8.27x11.69") # default is A4 | |
| 145 |
ijs_command f:cmd f:ack ijs_cmd_begin_page ijs_int:0 | |
| 146 |
status := success | |
| 147 |
| |
| 148 |
method f writeline adr -> status | |
| 149 |
arg_rw ImageWriteFilterIjs f ; arg Address adr ; arg Status status | |
| 150 |
ijs_send f:cmd ijs_cmd_send_data_block ijs_int:0+(ijs_int f:line_size) | |
| 151 |
f:cmd raw_write adr f:line_size | |
| 152 |
f:cmd flush anytime | |
| 153 |
ijs_receive f:ack (var Int n) (var Str p) | |
| 154 |
status := shunt f:cmd=success and f:ack=success and n=ijs_cmd_ack success failure | |
| 155 |
| |
| 156 |
method f close -> status | |
| 157 |
arg_rw ImageWriteFilterIjs f ; arg ExtendedStatus status | |
| 158 |
ijs_command f:cmd f:ack ijs_cmd_end_page ijs_int:0 | |
| 159 |
ijs_command f:cmd f:ack ijs_cmd_end_job ijs_int:0 | |
| 160 |
ijs_command f:cmd f:ack ijs_cmd_close "" | |
| 161 |
ijs_command f:cmd f:ack ijs_cmd_exit "" | |
| 162 |
f:cmd close ; f:ack close | |
| 163 |
f:eot request ; f:eot release | |
| 164 |
status := success | |
| 165 |
| |
| 166 |
image_record_filters ".ijs" Void false ImageWriteFilterIjs false | |
| |