diff --git a/headers/private/bluetooth/bluetoothserver_p.h b/headers/private/bluetooth/bluetoothserver_p.h index 45861bea5e..34b86fd085 100644 --- a/headers/private/bluetooth/bluetoothserver_p.h +++ b/headers/private/bluetooth/bluetoothserver_p.h @@ -3,6 +3,7 @@ #define BLUETOOTH_SIGNATURE "application/x-vnd.Be-bluetooth_server" +#define BLUETOOTH_APP_SIGNATURE "application/x-vnd.haiku-BluetoothPrefs" /* Kit Comunication */ @@ -21,5 +22,7 @@ #define BT_MSG_INQUIRY_ERROR 'IqER' #define BT_MSG_INQUIRY_DEVICE 'IqDE' +// Server +#define BT_MSG_SERVER_SHOW_CONSOLE 'btsc' #endif diff --git a/src/preferences/bluetooth/BluetoothMain.cpp b/src/preferences/bluetooth/BluetoothMain.cpp index 6ad576f9ab..76c30111c2 100644 --- a/src/preferences/bluetooth/BluetoothMain.cpp +++ b/src/preferences/bluetooth/BluetoothMain.cpp @@ -44,9 +44,10 @@ BluetoothApplication::AboutRequested() " - Frederik H, Tom S, Ferry B,\n" " - Greg G, David F, Richard S, Martin W:\n\n" "With patches:\n" + " - Michael Weirauch\n" " - Fredrik Ekdahl\n" " - Raynald Lesieur\n" - " - Andreas Färber\n\n" + " - Andreas Färber\n" " - Jörg Meyer\n" "Testing:\n" " - Petter H. Juliussen\n" diff --git a/src/preferences/bluetooth/BluetoothWindow.cpp b/src/preferences/bluetooth/BluetoothWindow.cpp index bcf57602ac..69286e3c5f 100644 --- a/src/preferences/bluetooth/BluetoothWindow.cpp +++ b/src/preferences/bluetooth/BluetoothWindow.cpp @@ -110,16 +110,16 @@ BluetoothWindow::MessageReceived(BMessage *message) */ break; case kMsgStartServices: printf("kMsgStartServices\n"); - if (!be_roster->IsRunning("application/x-vnd.Be-bluetooth_server")) + if (!be_roster->IsRunning(BLUETOOTH_SIGNATURE)) { - printf("kMsgStopServices: %s\n", strerror(be_roster->Launch("application/x-vnd.Be-bluetooth_server"))); + printf("kMsgStopServices: %s\n", strerror(be_roster->Launch(BLUETOOTH_SIGNATURE))); } break; case kMsgStopServices: printf("kMsgStopServices\n"); - if (be_roster->IsRunning("application/x-vnd.Be-bluetooth_server")) + if (be_roster->IsRunning(BLUETOOTH_SIGNATURE)) { - printf("kMsgStopServices: %s\n", strerror(BMessenger("application/x-vnd.Be-bluetooth_server").SendMessage(B_QUIT_REQUESTED))); + printf("kMsgStopServices: %s\n", strerror(BMessenger(BLUETOOTH_SIGNATURE).SendMessage(B_QUIT_REQUESTED))); } break; diff --git a/src/preferences/bluetooth/Jamfile b/src/preferences/bluetooth/Jamfile index ab0cee3922..b68c83c05c 100644 --- a/src/preferences/bluetooth/Jamfile +++ b/src/preferences/bluetooth/Jamfile @@ -1,5 +1,6 @@ SubDir HAIKU_TOP src preferences bluetooth ; +UsePrivateHeaders bluetooth ; UsePrivateHeaders shared ; AddResources Bluetooth : bluetooth-pref.rdef ; diff --git a/src/preferences/bluetooth/defs.h b/src/preferences/bluetooth/defs.h index 0ad9f0e0d6..b859e7b984 100644 --- a/src/preferences/bluetooth/defs.h +++ b/src/preferences/bluetooth/defs.h @@ -3,7 +3,8 @@ #include -#define BLUETOOTH_APP_SIGNATURE "application/x-vnd.haiku-BluetoothPrefs" +#include + #define APPLY_SETTINGS 'aply' #define REVERT_SETTINGS 'rvrt' diff --git a/src/servers/bluetooth/BluetoothServer.cpp b/src/servers/bluetooth/BluetoothServer.cpp index 664c4d2635..97a02eada0 100644 --- a/src/servers/bluetooth/BluetoothServer.cpp +++ b/src/servers/bluetooth/BluetoothServer.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -22,8 +23,9 @@ #include #include -#include "LocalDeviceImpl.h" #include "BluetoothServer.h" +#include "DeskbarReplicant.h" +#include "LocalDeviceImpl.h" #include "Output.h" @@ -60,15 +62,11 @@ BluetoothServer::BluetoothServer() : BApplication(BLUETOOTH_SIGNATURE) Output::Instance()->AddTab("Device Manager", BLACKBOARD_DEVICEMANAGER); Output::Instance()->AddTab("Kit", BLACKBOARD_KIT); - ShowWindow(Output::Instance()); - fDeviceManager = new DeviceManager(); fLocalDevicesList.MakeEmpty(); fEventListener2 = new BluetoothPortListener(BT_USERLAND_PORT_NAME, (BluetoothPortListener::port_listener_func)&DispatchEvent); - - } bool BluetoothServer::QuitRequested(void) @@ -82,6 +80,8 @@ bool BluetoothServer::QuitRequested(void) != NULL) delete lDeviceImpl; + _RemoveDeskbarIcon(); + printf("Accepting quitting of the application\n"); return BApplication::QuitRequested(); } @@ -109,6 +109,9 @@ void BluetoothServer::ReadyToRun(void) else Output::Instance()->Post("Bluetooth server Ready\n", BLACKBOARD_GENERAL); + ShowWindow(Output::Instance()); + + _InstallDeskbarIcon(); } @@ -190,6 +193,10 @@ void BluetoothServer::MessageReceived(BMessage *message) } return; } + + case BT_MSG_SERVER_SHOW_CONSOLE: + ShowWindow(Output::Instance()); + break; default: BApplication::MessageReceived(message); @@ -434,6 +441,35 @@ BluetoothServer::ShowWindow(BWindow* pWindow) } +void +BluetoothServer::_InstallDeskbarIcon() +{ + app_info appInfo; + be_app->GetAppInfo(&appInfo); + + BDeskbar deskbar; + + if (deskbar.HasItem(kDeskbarItemName)) { + _RemoveDeskbarIcon(); + } + + status_t res = deskbar.AddItem(&appInfo.ref); + if (res != B_OK) { + printf("Failed adding deskbar icon: %ld\n", res); + } +} + + +void +BluetoothServer::_RemoveDeskbarIcon() +{ + BDeskbar deskbar; + status_t res = deskbar.RemoveItem(kDeskbarItemName); + if (res != B_OK) { + printf("Failed removing Deskbar icon: %ld: \n", res); + } +} + #if 0 #pragma mark - #endif diff --git a/src/servers/bluetooth/BluetoothServer.h b/src/servers/bluetooth/BluetoothServer.h index 703efe4ed1..2eff155e64 100644 --- a/src/servers/bluetooth/BluetoothServer.h +++ b/src/servers/bluetooth/BluetoothServer.h @@ -72,6 +72,9 @@ private: void ShowWindow(BWindow* pWindow); + void _InstallDeskbarIcon(); + void _RemoveDeskbarIcon(); + LocalDevicesList fLocalDevicesList; diff --git a/src/servers/bluetooth/DeskbarReplicant.cpp b/src/servers/bluetooth/DeskbarReplicant.cpp new file mode 100644 index 0000000000..3ca90d980a --- /dev/null +++ b/src/servers/bluetooth/DeskbarReplicant.cpp @@ -0,0 +1,272 @@ +/* + * Copyright 2009, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Michael Weirauch, dev@m-phasis.de + */ + + +#include "DeskbarReplicant.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +extern "C" _EXPORT BView *instantiate_deskbar_item(void); +status_t our_image(image_info& image); + +const uint32 kMsgShowBluetoothServerConsole = 'sbtc'; +const uint32 kMsgOpenBluetoothPreferences = 'obtp'; +const uint32 kMsgQuitBluetoothServer = 'qbts'; + +const char* kDeskbarItemName = "BluetoothServerReplicant"; +const char* kClassName = "DeskbarReplicant"; + +// #pragma mark - + + +DeskbarReplicant::DeskbarReplicant(BRect frame, int32 resizingMode) + : BView(frame, kDeskbarItemName, resizingMode, + B_WILL_DRAW | B_FRAME_EVENTS) +{ + _Init(); +} + + +DeskbarReplicant::DeskbarReplicant(BMessage* archive) + : BView(archive) +{ + _Init(); +} + + +DeskbarReplicant::~DeskbarReplicant() +{ +} + + +void +DeskbarReplicant::_Init() +{ + fIcon = NULL; + + image_info info; + if (our_image(info) != B_OK) + return; + + BFile file(info.name, B_READ_ONLY); + if (file.InitCheck() < B_OK) + return; + + BResources resources(&file); +#ifdef HAIKU_TARGET_PLATFORM_HAIKU + if (resources.InitCheck() < B_OK) + return; +#endif + + size_t size; + const void* data = resources.LoadResource(B_VECTOR_ICON_TYPE, + "BEOS:ICON", &size); + if (data != NULL) { + BBitmap* icon = new BBitmap(Bounds(), B_RGBA32); + if (icon->InitCheck() == B_OK + && BIconUtils::GetVectorIcon((const uint8 *)data, + size, icon) == B_OK) { + fIcon = icon; + } else + delete icon; + } +} + + +DeskbarReplicant * +DeskbarReplicant::Instantiate(BMessage* archive) +{ + if (!validate_instantiation(archive, kClassName)) + return NULL; + + return new DeskbarReplicant(archive); +} + + +status_t +DeskbarReplicant::Archive(BMessage* archive, bool deep) const +{ + status_t status = BView::Archive(archive, deep); + if (status == B_OK) + status = archive->AddString("add_on", BLUETOOTH_SIGNATURE); + if (status == B_OK) + status = archive->AddString("class", kClassName); + + return status; +} + + +void +DeskbarReplicant::AttachedToWindow() +{ + BView::AttachedToWindow(); + if (Parent()) + SetViewColor(Parent()->ViewColor()); + else + SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); + + SetLowColor(ViewColor()); +} + + +void +DeskbarReplicant::Draw(BRect updateRect) +{ + if (!fIcon) { + /* At least display something... */ + rgb_color lowColor = LowColor(); + SetLowColor(0, 113, 187, 255); + FillRoundRect(Bounds().InsetBySelf(3.f, 0.f), 5.f, 7.f, B_SOLID_LOW); + SetLowColor(lowColor); + } else { + SetDrawingMode(B_OP_ALPHA); + DrawBitmap(fIcon); + SetDrawingMode(B_OP_COPY); + } +} + + +void +DeskbarReplicant::MessageReceived(BMessage* msg) +{ + switch (msg->what) { + case kMsgShowBluetoothServerConsole: + _ShowBluetoothServerConsole(); + break; + + case kMsgOpenBluetoothPreferences: + _OpenBluetoothPreferences(); + break; + + case kMsgQuitBluetoothServer: + _QuitBluetoothServer(); + break; + + default: + BView::MessageReceived(msg); + } +} + + +void +DeskbarReplicant::MouseDown(BPoint where) +{ + BPoint point; + uint32 buttons; + GetMouse(&point, &buttons); + if (!(buttons & B_SECONDARY_MOUSE_BUTTON)) { + return; + } + + BPopUpMenu* menu = new BPopUpMenu(B_EMPTY_STRING, false, false); + + menu->AddItem(new BMenuItem("Open Preferences"B_UTF8_ELLIPSIS, + new BMessage(kMsgOpenBluetoothPreferences))); + + // TODO show list of known/paired devices + + /* The code below is for development purposes only, but doesn't + * hurt to be enabled as long as in alpha state. + */ + menu->AddSeparatorItem(); + + menu->AddItem(new BMenuItem("Show Server Console" B_UTF8_ELLIPSIS, + new BMessage(kMsgShowBluetoothServerConsole))); + + menu->AddItem(new BMenuItem("Stop Server", + new BMessage(kMsgQuitBluetoothServer))); + + menu->SetTargetForItems(this); + ConvertToScreen(&point); + menu->Go(point, true, true, true); +} + + +void +DeskbarReplicant::_OpenBluetoothPreferences() +{ + status_t status = be_roster->Launch(BLUETOOTH_APP_SIGNATURE); + if (status < B_OK) { + _ShowErrorAlert("Launching the Bluetooth preflet failed.", status); + } +} + + +void +DeskbarReplicant::_ShowBluetoothServerConsole() +{ + if (!be_roster->IsRunning(BLUETOOTH_SIGNATURE)) { + return; + } + status_t status = BMessenger(BLUETOOTH_SIGNATURE).SendMessage( + BT_MSG_SERVER_SHOW_CONSOLE); + if (status < B_OK) { + _ShowErrorAlert("Showing the Bluetooth Server Console failed.", status); + } +} + + +void +DeskbarReplicant::_QuitBluetoothServer() +{ + if (!be_roster->IsRunning(BLUETOOTH_SIGNATURE)) { + return; + } + status_t status = BMessenger(BLUETOOTH_SIGNATURE).SendMessage( + B_QUIT_REQUESTED); + if (status < B_OK) { + _ShowErrorAlert("Stopping the Bluetooth Server failed.", status); + } +} + + +void +DeskbarReplicant::_ShowErrorAlert(BString msg, status_t status) +{ + msg << "\n\nError: " << strerror(status); + BAlert* alert = new BAlert("Bluetooth Error", msg.String(), "Ok"); + alert->Go(NULL); +} + +// #pragma mark - + + +extern "C" _EXPORT BView * +instantiate_deskbar_item(void) +{ + return new DeskbarReplicant(BRect(0, 0, 15, 15), + B_FOLLOW_LEFT | B_FOLLOW_TOP); +} + +// #pragma mark - + + +status_t +our_image(image_info& image) +{ + int32 cookie = 0; + while (get_next_image_info(B_CURRENT_TEAM, &cookie, &image) == B_OK) { + if ((char *)our_image >= (char *)image.text + && (char *)our_image <= (char *)image.text + image.text_size) + return B_OK; + } + + return B_ERROR; +} diff --git a/src/servers/bluetooth/DeskbarReplicant.h b/src/servers/bluetooth/DeskbarReplicant.h new file mode 100644 index 0000000000..ed17884c04 --- /dev/null +++ b/src/servers/bluetooth/DeskbarReplicant.h @@ -0,0 +1,46 @@ +/* + * Copyright 2009, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Michael Weirauch, dev@m-phasis.de + */ +#ifndef DESKBAR_REPLICANT_H +#define DESKBAR_REPLICANT_H + + +#include + + +extern const char* kDeskbarItemName; + + +class DeskbarReplicant : public BView { + public: + DeskbarReplicant(BRect frame, int32 resizingMode); + DeskbarReplicant(BMessage* archive); + virtual ~DeskbarReplicant(); + + static DeskbarReplicant* Instantiate(BMessage* archive); + virtual status_t Archive(BMessage* archive, bool deep = true) const; + + virtual void AttachedToWindow(); + + virtual void Draw(BRect updateRect); + + virtual void MessageReceived(BMessage* message); + virtual void MouseDown(BPoint where); + + private: + void _Init(); + void _OpenBluetoothPreferences(); + + void _ShowBluetoothServerConsole(); + void _QuitBluetoothServer(); + + void _ShowErrorAlert(BString msg, status_t status); + + BBitmap* fIcon; +}; + +#endif // DESKBAR_REPLICANT_H diff --git a/src/servers/bluetooth/Jamfile b/src/servers/bluetooth/Jamfile index 706a6ca049..7cf3b54204 100644 --- a/src/servers/bluetooth/Jamfile +++ b/src/servers/bluetooth/Jamfile @@ -3,12 +3,14 @@ SubDir HAIKU_TOP src servers bluetooth ; SetSubDirSupportedPlatformsBeOSCompatible ; UsePrivateHeaders shared bluetooth net kernel ; +UseLibraryHeaders icon ; AddResources bluetooth_server : server-bluetooth.rdef ; Server bluetooth_server : BluetoothServer.cpp + DeskbarReplicant.cpp DeviceManager.cpp HCIControllerAccessor.cpp HCITransportAccessor.cpp