Check-in a small print transport add-on for printers featuring an HP JetDirect port.

Like my all new HP deskjet 5850 ;-)


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@5566 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Philippe Houdoin 2003-12-04 20:56:59 +00:00
parent d50d38a519
commit fd17f6d4b5
8 changed files with 595 additions and 0 deletions

View File

@ -8,3 +8,4 @@ SubInclude OBOS_TOP src add-ons print transports parallel_port ;
SubInclude OBOS_TOP src add-ons print transports print_to_file ;
SubInclude OBOS_TOP src add-ons print transports serial_port ;
SubInclude OBOS_TOP src add-ons print transports usb_port ;
SubInclude OBOS_TOP src add-ons print transports hp_jetdirect ;

View File

@ -0,0 +1,104 @@
/*****************************************************************************/
// HP JetDirect (TCP/IP only) transport add-on,
//
// Author
// Philippe Houdoin
//
// This application and all source files used in its construction, except
// where noted, are licensed under the MIT License, and have been written
// and are:
//
// Copyright (c) 2001-2003 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#include <stdio.h>
#include <Alert.h>
#include <Message.h>
#include <Directory.h>
#include <SupportKit.h>
#include <String.h>
#include <NetEndpoint.h>
#include "HPJetDirectTransport.h"
#include "SetupWindow.h"
// Implementation of HPJetDirectPort
HPJetDirectPort::HPJetDirectPort(BDirectory* printer, BMessage *msg)
: fPort(9100), fEndpoint(NULL), fReady(false)
{
fHost[0] = '\0';
const char *spool_path = msg->FindString("printer_file");
if (spool_path && *spool_path) {
BDirectory dir(spool_path);
dir.ReadAttr("hp_jetdirect:host", B_STRING_TYPE, 0, fHost, sizeof(fHost));
if (fHost[0] == '\0') {
SetupWindow *setup = new SetupWindow(&dir);
if (setup->Go() == B_ERROR)
return;
}
dir.ReadAttr("hp_jetdirect:host", B_STRING_TYPE, 0, fHost, sizeof(fHost));
dir.ReadAttr("hp_jetdirect:port", B_INT16_TYPE, 0, &fPort, sizeof(fPort));
};
fEndpoint = new BNetEndpoint(SOCK_STREAM);
if (fEndpoint->InitCheck() != B_NO_ERROR) {
BAlert *alert = new BAlert("", "Fail to create the NetEndpoint!", "Damn");
alert->Go();
return;
};
if (fEndpoint->Connect(fHost, fPort) == B_OK) {
printf("Connected to HP JetDirect printer port at %s:%d\n", fHost, fPort);
fReady = true;
} else {
BAlert *alert = new BAlert("", "Can't connect to HP JetDirect printer port!", "Bad luck");
alert->Go();
};
}
HPJetDirectPort::~HPJetDirectPort()
{
delete fEndpoint;
}
ssize_t
HPJetDirectPort::Read(void* buffer, size_t size)
{
// printf("HPJetDirectPort::Read(%ld bytes)\n", size);
return (ssize_t) fEndpoint->Receive(buffer, size);
}
ssize_t
HPJetDirectPort::Write(const void* buffer, size_t size)
{
// printf("HPJetDirectPort::Write(%ld bytes)\n", size);
return (ssize_t) fEndpoint->Send(buffer, size);
}

View File

@ -0,0 +1,56 @@
/*****************************************************************************/
// HP JetDirect (TCP/IP only) transport add-on,
//
// Author
// Philippe Houdoin
//
// This application and all source files used in its construction, except
// where noted, are licensed under the MIT License, and have been written
// and are:
//
// Copyright (c) 2001-2003 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#ifndef HP_JETDIRECT_TRANSPORT_H
#define HP_JETDIRECT_TRANSPORT_H
#include <Message.h>
#include <Directory.h>
#include <NetEndpoint.h>
class HPJetDirectPort : public BDataIO {
public:
HPJetDirectPort(BDirectory* printer, BMessage* msg);
~HPJetDirectPort();
bool Ready() { return fReady; }
ssize_t Read(void* buffer, size_t size);
ssize_t Write(const void* buffer, size_t size);
private:
char fHost[256];
uint16 fPort; // default is 9100
BNetEndpoint *fEndpoint;
bool fReady;
};
#endif

View File

@ -0,0 +1,32 @@
SubDir OBOS_TOP src add-ons print transports hp_jetdirect ;
SubDirHdrs [ FDirName $(OBOS_TOP) src add-ons print transports shared ] ;
<<<<<<< Jamfile
Addon HP\ JetDirect : print transport :
print_transport.cpp
HPJetDirectTransport.cpp
SetupWindow.cpp
=======
UsePrivateHeaders net ;
Addon LPR
: print transport
: Lpr.cpp
LprSetupDlg.cpp
LprTransport.cpp
LpsClient.cpp
Socket.o
SocketStream2.o
DbgMsg.o
>>>>>>> 1.4
;
# LinkSharedOSLibs HP\ JetDirect : be netapi ; # for net_server
LinkSharedOSLibs HP\ JetDirect : be bnetapi ; # for BONE
<<<<<<< Jamfile
=======
LinkSharedOSLibs LPR : be net stdc++.r4 ;
>>>>>>> 1.4

View File

@ -0,0 +1,206 @@
#include <stdio.h>
#include <stdlib.h>
#include <Button.h>
#include <Rect.h>
#include <TextControl.h>
#include <View.h>
#include <Directory.h>
#include <Alert.h>
#include <String.h>
#include <NetEndpoint.h>
#include "SetupWindow.h"
#define DLG_WIDTH 370
#define DLG_HEIGHT 100
#define BUTTON_WIDTH 70
#define BUTTON_HEIGHT 20
#define SERVER_H 10
#define SERVER_V 10
#define SERVER_WIDTH (DLG_WIDTH - SERVER_H - SERVER_H)
#define SERVER_HEIGHT 20
#define SERVER_TEXT "Printer host name"
#define QUEUE_H 10
#define QUEUE_V SERVER_V + SERVER_HEIGHT + 2
#define QUEUE_WIDTH (DLG_WIDTH - QUEUE_H - QUEUE_H)
#define QUEUE_HEIGHT 20
#define QUEUE_TEXT "Port"
#define OK_H (DLG_WIDTH - BUTTON_WIDTH - 11)
#define OK_V (DLG_HEIGHT - BUTTON_HEIGHT - 11)
#define OK_TEXT "OK"
#define CANCEL_H (OK_H - BUTTON_WIDTH - 12)
#define CANCEL_V OK_V
#define CANCEL_TEXT "Cancel"
const BRect SERVER_RECT(
SERVER_H,
SERVER_V,
SERVER_H + SERVER_WIDTH,
SERVER_V + SERVER_HEIGHT);
const BRect QUEUE_RECT(
QUEUE_H,
QUEUE_V,
QUEUE_H + QUEUE_WIDTH,
QUEUE_V + QUEUE_HEIGHT);
const BRect OK_RECT(
OK_H,
OK_V,
OK_H + BUTTON_WIDTH,
OK_V + BUTTON_HEIGHT);
const BRect CANCEL_RECT(
CANCEL_H,
CANCEL_V,
CANCEL_H + BUTTON_WIDTH,
CANCEL_V + BUTTON_HEIGHT);
enum MSGS {
M_CANCEL = 1,
M_OK
};
class SetupView : public BView {
public:
SetupView(BRect, BDirectory *);
~SetupView() {}
virtual void AttachedToWindow();
bool UpdateViewData();
private:
BTextControl *server;
BTextControl *queue;
BDirectory *dir;
};
SetupView::SetupView(BRect frame, BDirectory *d)
: BView(frame, "", B_FOLLOW_ALL, B_WILL_DRAW), dir(d)
{
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
}
void SetupView::AttachedToWindow()
{
float width = MAX(StringWidth(SERVER_TEXT), StringWidth(QUEUE_TEXT)) + 10;
/* server name box */
server = new BTextControl(SERVER_RECT, "", SERVER_TEXT, "", NULL);
AddChild(server);
server->SetDivider(width);
/* queue name box */
queue = new BTextControl(QUEUE_RECT, "", QUEUE_TEXT, "", NULL);
AddChild(queue);
queue->SetDivider(width);
/* cancel */
BButton *button = new BButton(CANCEL_RECT, "", CANCEL_TEXT, new BMessage(M_CANCEL));
AddChild(button);
/* ok */
button = new BButton(OK_RECT, "", OK_TEXT, new BMessage(M_OK));
AddChild(button);
button->MakeDefault(true);
}
bool SetupView::UpdateViewData()
{
if (*server->Text() && *queue->Text()) {
BNetEndpoint *ep = new BNetEndpoint(SOCK_STREAM);
if (ep->InitCheck() == B_NO_ERROR) {
int port = atoi(queue->Text());
if (! port)
port = 9100;
if (ep->Connect(server->Text(), atoi(queue->Text())) != B_OK) {
BString text;
text << "Fail to connect to " << server->Text() << ":" << port << "!";
BAlert *alert = new BAlert("", text.String(), "Damn");
alert->Go();
return false;
};
char str[256];
sprintf(str, "%s:%s", server->Text(), queue->Text());
dir->WriteAttr("hp_jetdirect:host", B_STRING_TYPE, 0, server->Text(), strlen(server->Text()) + 1);
dir->WriteAttr("hp_jetdirect:port", B_INT16_TYPE, 0, &port, 2);
return true;
};
};
BAlert *alert = new BAlert("", "please input parameters.", "OK");
alert->Go();
return false;
}
SetupWindow::SetupWindow(BDirectory *dir)
: BWindow(BRect(100, 100, 100 + DLG_WIDTH, 100 + DLG_HEIGHT),
"HP JetDirect Setup", B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL,
B_NOT_RESIZABLE | B_NOT_MINIMIZABLE | B_NOT_ZOOMABLE)
{
result = 0;
Lock();
SetupView *view = new SetupView(Bounds(), dir);
AddChild(view);
Unlock();
semaphore = create_sem(0, "SetupWindowSem");
}
bool SetupWindow::QuitRequested()
{
result = B_ERROR;
release_sem(semaphore);
return true;
}
void SetupWindow::MessageReceived(BMessage *msg)
{
bool success;
switch (msg->what) {
case M_OK:
Lock();
success = ((SetupView *)ChildAt(0))->UpdateViewData();
Unlock();
if (success) {
result = B_NO_ERROR;
release_sem(semaphore);
}
break;
case M_CANCEL:
result = B_ERROR;
release_sem(semaphore);
break;
default:
BWindow::MessageReceived(msg);
break;
}
}
int SetupWindow::Go()
{
Show();
acquire_sem(semaphore);
delete_sem(semaphore);
int value = result;
Lock();
Quit();
return value;
}

View File

@ -0,0 +1,21 @@
#ifndef SETUPWINDOW_H
#define SETUPWINDOW_H
#include <Window.h>
class BDirectory;
class SetupWindow : public BWindow {
public:
SetupWindow(BDirectory *);
~SetupWindow() {}
virtual bool QuitRequested();
virtual void MessageReceived(BMessage *message);
int Go();
private:
int result;
long semaphore;
};
#endif // SETUPWINDOW_H

View File

@ -0,0 +1,101 @@
## BeOS Generic Makefile v2.0 ##
## Fill in this file to specify the project being created, and the referenced
## makefile-engine will do all of the hard work for you. This handles both
## Intel and PowerPC builds of the BeOS.
## Application Specific Settings ---------------------------------------------
# specify the name of the binary
NAME= HP\ JetDirect
# specify the type of binary
# APP: Application
# SHARED: Shared library or add-on
# STATIC: Static library archive
# DRIVER: Kernel Driver
TYPE= SHARED
# specify the source files to use
# full paths or paths relative to the makefile can be included
# all files, regardless of directory, will have their object
# files created in the common object directory.
# Note that this means this makefile will not work correctly
# if two source files with the same name (source.c or source.cpp)
# are included from different directories. Also note that spaces
# in folder names do not work well with this makefile.
SRCS= \
print_transport.cpp \
HPJetDirectTransport.cpp \
SetupWindow.cpp
# specify the resource files to use
# full path or a relative path to the resource file can be used.
RSRCS=
# specify additional libraries to link against
# there are two acceptable forms of library specifications
# - if your library follows the naming pattern of:
# libXXX.so or libXXX.a you can simply specify XXX
# library: libbe.so entry: be
#
# - if your library does not follow the standard library
# naming scheme you need to specify the path to the library
# and it's name
# library: my_lib.a entry: my_lib.a or path/my_lib.a
# LIBS= be netapi # for net_server
LIBS= be bnetapi # for BONE
# specify additional paths to directories following the standard
# libXXX.so or libXXX.a naming scheme. You can specify full paths
# or paths relative to the makefile. The paths included may not
# be recursive, so include all of the paths where libraries can
# be found. Directories where source files are found are
# automatically included.
LIBPATHS=
# additional paths to look for system headers
# thes use the form: #include <header>
# source file directories are NOT auto-included here
SYSTEM_INCLUDE_PATHS =
# additional paths to look for local headers
# thes use the form: #include "header"
# source file directories are automatically included
LOCAL_INCLUDE_PATHS =
# specify the level of optimization that you desire
# NONE, SOME, FULL
OPTIMIZE=
# specify any preprocessor symbols to be defined. The symbols
# will be set to a value of 1. For example specify DEBUG if you want
# DEBUG=1 to be set when compiling.
DEFINES=
# specify special warning levels
# if unspecified default warnings will be used
# NONE = supress all warnings
# ALL = enable all warnings
WARNINGS = ALL
# specify whether image symbols will be created
# so that stack crawls in the debugger are meaningful
# if TRUE symbols will be created
SYMBOLS =
# specify debug settings
# if TRUE will allow application to be run from
# a source-level debugger
DEBUGGER =
# specify additional compiler flags for all files
COMPILER_FLAGS =
# specify additional linker flags
LINKER_FLAGS =
## include the makefile-engine
include /boot/develop/etc/makefile-engine

View File

@ -0,0 +1,74 @@
/*****************************************************************************/
// HP JetDirect (TCP/IP only) transport add-on,
//
// Author
// Philippe Houdoin
//
// This application and all source files used in its construction, except
// where noted, are licensed under the MIT License, and have been written
// and are:
//
// Copyright (c) 2001-2003 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#include <Message.h>
#include <Directory.h>
#include <SupportKit.h>
#include "HPJetDirectTransport.h"
static BDataIO * g_transport = NULL;
// Implementation of transport add-on interface
extern "C" _EXPORT BDataIO * init_transport(BMessage *msg)
{
if (msg == NULL)
return NULL;
if (g_transport)
return NULL;
const char* printer_name = msg->FindString("printer_file");
if (printer_name && *printer_name != '\0') {
BDirectory printer(printer_name);
if (printer.InitCheck() == B_OK) {
HPJetDirectPort * transport = new HPJetDirectPort(&printer, msg);
if (transport->Ready()) {
g_transport = transport;
return g_transport;
};
delete transport;
};
};
return NULL;
}
extern "C" _EXPORT void exit_transport()
{
if (g_transport)
delete g_transport;
g_transport = NULL;
}