From 86cd46f6808e8c20335da0bc1161cef5a1e6ea57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Thu, 12 Sep 2002 21:38:19 +0000 Subject: [PATCH] Initial checkin git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1019 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/bin/shutdown.cpp | 394 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 394 insertions(+) create mode 100644 src/apps/bin/shutdown.cpp diff --git a/src/apps/bin/shutdown.cpp b/src/apps/bin/shutdown.cpp new file mode 100644 index 0000000000..562836c1a0 --- /dev/null +++ b/src/apps/bin/shutdown.cpp @@ -0,0 +1,394 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// LDFLAGS="-lbe" make + +//from bdb-ing /bin/shutdown: +#define B_SYSTEM_SHUTDOWN 0x12d +#define B_SYSTEM_REBOOT 0x12e + +const char *roster_sig = "application/x-vnd.Be-ROST"; + +extern "C" void _kshutdown_(long arg); +#define SHUTDOWN_HALT 0 +#define SHUTDOWN_REBOOT 1 + +// globals +const char kIconBits[] = { +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0x00, +0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, +0x00, 0x00, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0x00, +0x3f, 0x3f, 0x3f, 0x00, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0x00, 0x3f, 0x3f, +0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0x00, 0x3f, 0x3f, 0x3f, 0x00, +0x00, 0x00, 0x00, 0x00, 0x3f, 0x3f, 0x3f, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 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, 0x3f, 0x00, 0x00, 0x3f, 0x3f, 0x00, +0x00, 0x00, 0x00, 0x00, 0x3f, 0x3f, 0x00, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0xff, +0xff, 0xff, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0x00, 0x3f, +0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0xff, +0xff, 0xff, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, +0x00, 0x3f, 0x00, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0xff, 0xff, +0xff, 0xff, 0xff, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, +0x00, 0x00, 0x00, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0x00, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0x00, 0x00, +0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, +0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff +}; + +unsigned long time_to_sleep = 0; // -d +int reboot = 0; // -r + + +class SView : public BView +{ + public: + SView(BRect frame); + ~SView(); + + void Draw(BRect updateRect); + void Pulse(); + + BButton *fButton; + BString *fTitleStr; + BStringView *fTitle; + + BBitmap *fIcon; + + int32 fWidth; + int32 fHeight; + + int32 fLeftMargin; + static const int32 fTopMargin = 8; + unsigned long fTimeLeft; +}; + +class ShutdownWindow : public BWindow +{ + public: + ShutdownWindow(thread_id id); + ~ShutdownWindow(); + void MessageReceived(BMessage *msg); + bool QuitRequested(); + void Quit(); + private: + SView *fView; + thread_id fShutdownThread; +}; + +class ShutdownApp : public BApplication +{ + public: + ShutdownApp(thread_id id); + ~ShutdownApp(); + private: + ShutdownWindow *fWindow; +}; + +SView::SView(BRect frame) + :BView(frame, "SView", B_FOLLOW_LEFT_RIGHT, B_PULSE_NEEDED | B_WILL_DRAW), + fButton(NULL), + fTitleStr(NULL), + fTitle(NULL), + fIcon(NULL), + fWidth(0), + fHeight(0), + fLeftMargin(B_LARGE_ICON + 8), + fTimeLeft(time_to_sleep) +{ + SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); + SetLowColor(ViewColor()); + SetDrawingMode(B_OP_OVER); + fIcon = new BBitmap(BRect(0, 0, B_LARGE_ICON-1, B_LARGE_ICON-1), B_CMAP8); + fIcon->SetBits(kIconBits, B_LARGE_ICON*B_LARGE_ICON, 0, B_CMAP8); + fTitleStr = new BString; + if (reboot) + *fTitleStr << "Rebooting in " << (int32)fTimeLeft << " seconds !"; + else + *fTitleStr << "Shutting down in " << (int32)fTimeLeft << " seconds !"; +// fTitle = new BStringView(BRect( fLeftMargin, fTopMargin, fLeftMargin, fTopMargin), "", fTitleStr->String()); + fTitle = new BStringView(BRect(55, 10, 260, 35), "", fTitleStr->String()); + fButton = new BButton(BRect(159, 42, 259, 66), "quit", "Cancel", new BMessage('_QRQ')); + fButton->MakeDefault(true); + AddChild(fTitle); + AddChild(fButton); +} + +SView::~SView() +{ + if (fButton != NULL) + delete fButton; + + if (fTitle != NULL) + delete fTitle; + + if (fIcon != NULL) + delete fIcon; +} + +void +SView::Pulse() +{ + fTimeLeft--; + fTitleStr->SetTo(""); + if (reboot) + *fTitleStr << "Rebooting in " << (int32)fTimeLeft << " seconds !"; + else + *fTitleStr << "Shutting down in " << (int32)fTimeLeft << " seconds !"; + fTitle->SetText(fTitleStr->String()); + Invalidate(); +} + +void +SView::Draw(BRect updateRect) +{ + BRect bounds = Bounds(); + + // left margin background + SetHighColor(tint_color(ViewColor(), B_HIGHLIGHT_BACKGROUND_TINT)); + FillRect(BRect(0, 0, B_LARGE_ICON, bounds.bottom)); + + // fIcon if any + if(fIcon != NULL) + { + DrawBitmap(fIcon, BPoint( (B_LARGE_ICON / 2), fTopMargin)); + } +} + +ShutdownWindow::ShutdownWindow(thread_id id) + :BWindow(BRect( 139, 81, 407, 156), "Shutdown", B_TITLED_WINDOW_LOOK, + B_NORMAL_WINDOW_FEEL, B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_NOT_MINIMIZABLE | B_ASYNCHRONOUS_CONTROLS), + fView(NULL) +{ + fShutdownThread = id; + SetPulseRate(1000000); + fView = new SView(Bounds()); + AddChild(fView); +} + +ShutdownWindow::~ShutdownWindow() +{ + fView->LockLooper(); + fView->RemoveSelf(); + delete fView; +} + +void +ShutdownWindow::MessageReceived(BMessage *msg) +{ + switch(msg->what) + { + default: + BWindow::MessageReceived(msg); + break; + } +} + +bool +ShutdownWindow::QuitRequested() +{ + SetPulseRate(0); + kill(fShutdownThread, SIGUSR1); + return true; +} + +void +ShutdownWindow::Quit() +{ + + // quit app on window close +// be_app->PostMessage(B_QUIT_REQUESTED); + + // call default Quit() + BWindow::Quit(); +} + +ShutdownApp::ShutdownApp(thread_id id) + :BApplication("application/x-vnd.ShutDown-app") +{ + fWindow = new ShutdownWindow(id); + fWindow->SetWorkspaces(0xFFFFFFFF); + fWindow->Show(); +} + +ShutdownApp::~ShutdownApp() +{ +} + +int32 app_thread(void *arg) +{ + thread_id tid = (thread_id) arg; + (new ShutdownApp(tid))->Run(); + delete be_app; + return B_OK; +} + +/***************************/ + +void handle_usr1(int sig) +{ + while (0); +} + +int main(int argc, char **argv) +{ + int i; + int quick = 0; // -q + int alert = 0; // -a + BMessenger msgr(roster_sig); + + for (i = 1; i < argc; i++) { + if (!strcmp(argv[i], "-q")) + quick = 1; + else if (!strcmp(argv[i], "-r")) + reboot = 1; + else if (!strcmp(argv[i], "-c")) { + team_info ti; + thread_info thi; + int32 team_cookie = 0; + int32 thread_cookie = 0; + team_id myteam; + + get_thread_info(find_thread(NULL), &thi); + myteam = thi.team; + while (get_next_team_info(&team_cookie, &ti) == B_OK) { + if (strstr(ti.args, "shutdown") != NULL && (ti.team != myteam)) { + thread_cookie = 0; + while (get_next_thread_info(ti.team, &thread_cookie, &thi) == B_OK) { + if (!strcmp(thi.name, "shutdown")) { + kill(thi.thread, SIGUSR1); + } + } + } + } + exit (0); + } else if (!strcmp(argv[i], "-a")) { + alert = 1; + } else if (!strcmp(argv[i], "-d") && (i+1 < argc)) { + i++; + time_to_sleep = strtol(argv[i], &argv[i], 10); + } else { + fprintf(stderr, +"usage: %s [-r] [-q] [-c] [-a] [-d time] + + -r reboot, + -q quick shutdown (don't broadcast apps), + -c cancel a running shutdown, + -a alerts user while waiting, + -d delay shutdown by