Skip to content

Commit a8ced63

Browse files
author
Gustaf Haglund
committed
Better printer detection
1 parent aa7d57c commit a8ced63

File tree

11 files changed

+80
-38
lines changed

11 files changed

+80
-38
lines changed

Makefile

+7-6
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@
1818
## along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
##
2020

21-
DEBUG = -g3 -ggdb -fvar-tracking -fvar-tracking-assignments
22-
CC = gcc -Wall -pedantic -std=gnu99 #$(DEBUG)
23-
LIBS = $(shell pkg-config --cflags --libs libusb-1.0) -ljbig -lcups
21+
DEBUG = -D_DEBUG -g3 -ggdb -fvar-tracking
22+
CC = gcc -Wall -pedantic -std=gnu99 -I/usr/local/include $(DEBUG)
23+
LIBS = $(shell pkg-config --cflags libusb-1.0)
24+
FINAL_LIBS=$(shell pkg-config --libs libusb-1.0) -ljbig -lcups
2425

2526
BIN = bin/gurich
2627
BIN_CBACKEND = bin/gurich_cbackend
2728

28-
CFLAGS = -Isrc $(LIBS) -D_DEBUG #-D_NO_PRINT_USB
29+
CFLAGS = -Isrc $(LIBS) #-D_DEBUG -D_NO_PRINT_USB
2930

3031
CBACKEND_DEPEND = src/prntother.o src/basic.o src/prntcommon.o
3132

3233
all: $(patsubst src/%.c, src/%.o,$(wildcard src/*.c)) $(patsubst src/cbackend/%.c, src/cbackend/%.o,$(wildcard src/cbackend/*.c))
33-
$(CC) $(CFLAGS) -o $(BIN) src/*.o $(LIBS)
34-
$(CC) $(CFLAGS) -o $(BIN_CBACKEND) $(CBACKEND_DEPEND) src/cbackend/*.o $(LIBS)
34+
$(CC) $(CFLAGS) -o $(BIN) src/*.o $(FINAL_LIBS)
35+
$(CC) $(CFLAGS) -o $(BIN_CBACKEND) $(CBACKEND_DEPEND) src/cbackend/*.o $(FINAL_LIBS)
3536

3637
dev:
3738
rm src/*.o

readme

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ Gurich is a free printer driver for Ricoh SP112.
22
Other printers in the Ricoh SP100/Ricoh SP110 series probably work too.
33

44
In order to install this driver, please see the INSTALL file.
5-
Contributions to this project is welcome, but please follow the code style used in this driver.
5+
Contributions to this project are welcome, but please follow the code style used in this driver.
66

77
If you want a driver with warranty, then use Ricoh's driver for Microsoft Windows.
88

9-
I am not affilated with or employed by Ricoh, this project is done in my spare time.
9+
I am not affilated with nor employed by Ricoh, this project is done in my spare time.
1010
All trademarks mentioned in this project are owned by their respective owner.
1111

1212
== Features ==
@@ -29,7 +29,7 @@ For other use cases, e.g seeing how much toner is left:
2929
$ ./bin/gurich -s
3030
[...]
3131
Printer status: GOOD / ENERGY SAVING MODE 2 / IDLE, reference: 0x31
32-
Printer toner: 80 % left
32+
Printer toner: 80 % left
3333
Printed out pages (stats): 301
3434

3535
$ ./bin/gurich -h
@@ -38,7 +38,8 @@ print: ./bin/gurich -p [ps/pdf file, resolution (600|1200), copies, paper type (
3838
status: ./bin/gurich -s
3939
testpage: ./bin/gurich -t
4040

41-
You might need to make a rule for udev in order to avoid running gurich with root privileges.
41+
You might need to make a rule for udev in order to avoid running gurich with root privileges,
42+
on GNU/Linux-based systems.
4243

4344
== Thanks to ==
4445
Serge V Shistarev for the PPD file,

src/basic.c

+40-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
This program is distributed in the hope that it will be useful,
1414
but WITHOUT ANY WARRANTY; without even the implied warranty of
15-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616
GNU General Public License for more details.
1717
1818
You should have received a copy of the GNU General Public License
@@ -22,29 +22,30 @@
2222

2323
#include <gurich.h>
2424

25-
void check_printer_usb(struct gurich_usb * g)
25+
void printer_usb(struct gurich_usb * g, uint64_t idProduct)
2626
{
2727
char libusb_strerror[128];
2828
int libusb_error;
2929

30-
libusb_init(&g->ctx);
3130
libusb_set_debug(g->ctx, PRINTER_LIBUSB_DEBUG);
3231

33-
g->device_handle = libusb_open_device_with_vid_pid(g->ctx, PRINTER_VENDOR_ID, PRINTER_DEVICE_ID);
32+
g->device_handle = libusb_open_device_with_vid_pid(g->ctx, PRINTER_VENDOR_ID, idProduct);
3433

3534
if (g->device_handle == NULL)
3635
{
3736
libusb_close(g->device_handle);
3837
libusb_exit(g->ctx);
39-
printf("Could not find the printer. Quitting.\n\n");
38+
fprintf(stderr, "Something did happen with the printer. Quitting.\n\n");
4039
exit(-1);
4140
}
4241

4342
g->device = libusb_get_device(g->device_handle);
44-
libusb_error = libusb_detach_kernel_driver(g->device_handle, 0);
4543

46-
if (libusb_error != 0) {
47-
goto libusb_fail;
44+
libusb_error = libusb_kernel_driver_active(g->device_handle, 0);
45+
if (libusb_error == 1) {
46+
if ((libusb_error = libusb_detach_kernel_driver(g->device_handle, 0)) < 0) {
47+
goto libusb_fail;
48+
}
4849
}
4950

5051
libusb_error = libusb_claim_interface(g->device_handle, 0);
@@ -68,6 +69,37 @@ void check_printer_usb(struct gurich_usb * g)
6869
exit(-1);
6970
}
7071

72+
void check_printer_usb(struct gurich_usb * g)
73+
{
74+
libusb_device **list;
75+
struct libusb_device_descriptor devdesc;
76+
ssize_t numusbdevs, i, err;
77+
78+
err = libusb_init(&g->ctx);
79+
numusbdevs = libusb_get_device_list(g->ctx, &list);
80+
81+
for (i = 0; i < numusbdevs; ++i)
82+
{
83+
g->device = list[i];
84+
85+
if (libusb_get_device_descriptor(g->device, &devdesc) < 0)
86+
continue;
87+
88+
if (!devdesc.bNumConfigurations || !devdesc.idVendor
89+
|| !devdesc.idProduct)
90+
continue;
91+
92+
if (devdesc.idVendor == PRINTER_VENDOR_ID) {
93+
/* Found a Ricoh printer. */
94+
printer_usb(g, devdesc.idProduct);
95+
}
96+
}
97+
98+
if (numusbdevs >= 0) {
99+
libusb_free_device_list(list, 1);
100+
}
101+
}
102+
71103
void cleanup_usb(struct gurich_usb * g)
72104
{
73105
if (g->initialized)

src/cbackend/cbackend.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
This program is distributed in the hope that it will be useful,
1414
but WITHOUT ANY WARRANTY; without even the implied warranty of
15-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616
GNU General Public License for more details.
1717
1818
You should have received a copy of the GNU General Public License
@@ -26,8 +26,8 @@
2626

2727
static void tell_cups()
2828
{
29-
printf("gurich://RICOH/SP 112 DDST?serial=X025M233243\n");
30-
/* TODO: Tell cups correctly */
29+
printf("direct gurich://RICOH/SP 112 DDST?serial=X025M233243\n");
30+
/* TODO: A better way, this will not work currently */
3131
}
3232

3333
int main(int argc, char ** argv)
@@ -48,7 +48,7 @@ int main(int argc, char ** argv)
4848
return 0;
4949
}
5050

51-
char binbuf[4096];
51+
char binbuf[8192];
5252
size_t readlen;
5353
int fp = 0;
5454

src/gurich.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
This program is distributed in the hope that it will be useful,
1414
but WITHOUT ANY WARRANTY; without even the implied warranty of
15-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616
GNU General Public License for more details.
1717
1818
You should have received a copy of the GNU General Public License
@@ -171,6 +171,11 @@ int main(int argc, char ** argv)
171171
init_msg();
172172
check_printer_usb(&g);
173173

174+
if (!g.initialized) {
175+
fprintf(stderr, "Could not find the printer. Quitting.\n\n");
176+
exit(-1);
177+
}
178+
174179
switch(arg[1])
175180
{
176181
case 'p':

src/gurich.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
33
Gurich
4-
** Ricoh SP110 series driver **
4+
** Ricoh SP110 series driver **
55
66
Copyright (C) 2016, 2017 Gustaf Haglund <[email protected]>
77
@@ -12,7 +12,7 @@
1212
1313
This program is distributed in the hope that it will be useful,
1414
but WITHOUT ANY WARRANTY; without even the implied warranty of
15-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616
GNU General Public License for more details.
1717
1818
You should have received a copy of the GNU General Public License
@@ -48,7 +48,12 @@
4848
#endif
4949

5050
#ifndef GURICH_TEMP_DIR_PERMISSION
51-
#define GURICH_TEMP_DIR_PERMISSION 0644
51+
#define GURICH_TEMP_DIR_PERMISSION 0666
52+
#endif
53+
54+
#ifndef _D_EXACT_NAMLEN
55+
/* For BSD systems. */
56+
#define _D_EXACT_NAMLEN(d) strlen((d)->d_name)
5257
#endif
5358

5459
struct gurich_usb {
@@ -116,7 +121,7 @@ void gurich_jbg(FILE *pbmFp,
116121
struct gurich_pbm * pbm,
117122
struct gurich_jbg_st *jbg);
118123

119-
/* Printer communication functions */
124+
/* "Real" printer functions */
120125
struct gurich_status gurich_status(struct gurich_usb * g);
121126
unsigned short int gurich_toner(struct gurich_usb * g);
122127
size_t gurich_printed(struct gurich_usb * g);

src/prnt.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
This program is distributed in the hope that it will be useful,
1414
but WITHOUT ANY WARRANTY; without even the implied warranty of
15-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616
GNU General Public License for more details.
1717
1818
You should have received a copy of the GNU General Public License

src/prnt.h

+5-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
This program is distributed in the hope that it will be useful,
1414
but WITHOUT ANY WARRANTY; without even the implied warranty of
15-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616
GNU General Public License for more details.
1717
1818
You should have received a copy of the GNU General Public License
@@ -26,7 +26,6 @@
2626
#define __GURICH_PRINT__H
2727

2828
#define PRINTER_VENDOR_ID 0x05ca
29-
#define PRINTER_DEVICE_ID 0x0447
3029
#define PRINTER_LIBUSB_DEBUG 3
3130

3231
#define PRINTER_STANDARD_MEDIATYPE "PLAINRECYCLE"
@@ -35,11 +34,10 @@
3534
#define PRINTER_STANDARD_HOLD "OFF"
3635
#define PRINTER_STANDARD_RESOLUTION "600"
3736

38-
#define PRINTER_START 0x1b, 0x25, 0x2d, 0x31, 0x32, 0x33, 0x34, 0x35, 0x58, 0x40, 0x50, 0x4a, 0x4c, 0x0d, 0x0a
39-
#define PRINTER_START_FORMAT "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"
40-
#define PRINTER_PAGE_END "@PJL SET PAGESTATUS=END\r\n"
41-
#define PRINTER_END "@PJL EOJ\r\n%-12345X\r\n"
42-
#define PRINTER_END_FORMAT "%s"
37+
#define PRINTER_START 0x1b, 0x25, 0x2d, 0x31, 0x32, 0x33, 0x34, 0x35, 0x58, 0x40, 0x50, 0x4a, 0x4c, 0x0d, 0x0a
38+
#define PRINTER_START_FORMAT "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"
39+
#define PRINTER_PAGE_END "@PJL SET PAGESTATUS=END\r\n"
40+
#define PRINTER_END "@PJL EOJ\r\n%-12345X\r\n"
4341

4442
#define PRINTER_PBM_DIR "temp/pbm/"
4543
#define PRINTER_PBM_DIRLEN 9

src/prntcommon.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
This program is distributed in the hope that it will be useful,
1414
but WITHOUT ANY WARRANTY; without even the implied warranty of
15-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616
GNU General Public License for more details.
1717
1818
You should have received a copy of the GNU General Public License

src/prntother.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
This program is distributed in the hope that it will be useful,
1414
but WITHOUT ANY WARRANTY; without even the implied warranty of
15-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616
GNU General Public License for more details.
1717
1818
You should have received a copy of the GNU General Public License

src/shworkaround.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
This program is distributed in the hope that it will be useful,
1414
but WITHOUT ANY WARRANTY; without even the implied warranty of
15-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616
GNU General Public License for more details.
1717
1818
You should have received a copy of the GNU General Public License

0 commit comments

Comments
 (0)