2 * Copyright 2002-2005 Haiku
3 * Distributed under the terms of the MIT license.
5 * Updated by Sikosis (beos@gravity24hr.com)
7 * Copyright 1999, Be Incorporated. All Rights Reserved.
8 * This file may be used under the terms of the Be Sample Code License.
10 * Written by: Daniel Switkin
30 #include "PulseWindow.h"
31 #include "DeskbarPulseView.h"
33 #undef B_TRANSLATION_CONTEXT
34 #define B_TRANSLATION_CONTEXT "PulseApp"
37 PulseApp::PulseApp(int argc
, char **argv
)
38 : BApplication(APP_SIGNATURE
)
42 int mini
= false, deskbar
= false, normal
= false;
43 uint32 framecolor
= 0, activecolor
= 0, idlecolor
= 0;
47 static struct option long_options
[] = {
48 {"deskbar", 0, &deskbar
, true},
50 {"framecolor", 1, 0, 0},
51 {"activecolor", 1, 0, 0},
52 {"idlecolor", 1, 0, 0},
53 {"mini", 0, &mini
, true},
54 {"normal", 0, &normal
, true},
58 int c
= getopt_long(argc
, argv
, "hw:", long_options
, &option_index
);
64 switch (option_index
) {
65 case 2: /* framecolor */
66 case 3: /* activecolor */
67 case 4: /* idlecolor */
68 uint32 rgb
= strtoul(optarg
, NULL
, 0);
72 switch (option_index
) {
87 prefs
->deskbar_icon_width
= atoi(optarg
);
88 if (prefs
->deskbar_icon_width
< GetMinimumViewWidth())
89 prefs
->deskbar_icon_width
= GetMinimumViewWidth();
90 else if (prefs
->deskbar_icon_width
> 50) prefs
->deskbar_icon_width
= 50;
97 printf("?? getopt returned character code 0%o ??\n", c
);
103 prefs
->window_mode
= DESKBAR_MODE
;
104 if (activecolor
!= 0)
105 prefs
->deskbar_active_color
= activecolor
;
107 prefs
->deskbar_idle_color
= idlecolor
;
109 prefs
->deskbar_frame_color
= framecolor
;
111 prefs
->window_mode
= MINI_WINDOW_MODE
;
112 if (activecolor
!= 0)
113 prefs
->mini_active_color
= activecolor
;
115 prefs
->mini_idle_color
= idlecolor
;
117 prefs
->mini_frame_color
= framecolor
;
119 prefs
->window_mode
= NORMAL_WINDOW_MODE
;
127 PulseApp::BuildPulse()
129 // Remove this case for Deskbar add on API
131 // If loading the replicant fails, launch the app instead
132 // This allows having the replicant and the app open simultaneously
133 if (prefs
->window_mode
== DESKBAR_MODE
&& LoadInDeskbar()) {
134 PostMessage(new BMessage(B_QUIT_REQUESTED
));
136 } else if (prefs
->window_mode
== DESKBAR_MODE
)
137 prefs
->window_mode
= NORMAL_WINDOW_MODE
;
139 PulseWindow
*pulseWindow
= NULL
;
141 if (prefs
->window_mode
== MINI_WINDOW_MODE
)
142 pulseWindow
= new PulseWindow(prefs
->mini_window_rect
);
144 pulseWindow
= new PulseWindow(prefs
->normal_window_rect
);
146 pulseWindow
->MoveOnScreen(B_MOVE_IF_PARTIALLY_OFFSCREEN
);
151 PulseApp::~PulseApp()
153 // Load the replicant after we save our preferences so they don't
154 // get overwritten by DeskbarPulseView's instance
156 if (prefs
->window_mode
== DESKBAR_MODE
)
164 PulseApp::AboutRequested()
166 PulseApp::ShowAbout(true);
171 PulseApp::ShowAbout(bool asApplication
)
173 // static version to be used in replicant mode
176 name
= B_TRANSLATE_SYSTEM_NAME("Pulse");
178 name
= B_TRANSLATE("Pulse");
180 BString message
= B_TRANSLATE(
181 "%s\n\nBy David Ramsey and Arve Hjønnevåg\n"
182 "Revised by Daniel Switkin\n");
183 message
.ReplaceFirst("%s", name
);
184 BAlert
*alert
= new BAlert(B_TRANSLATE("Info"),
185 message
.String(), B_TRANSLATE("OK"));
187 BTextView
* view
= alert
->TextView();
190 view
->SetStylable(true);
191 view
->GetFont(&font
);
194 font
.SetFace(B_BOLD_FACE
);
195 view
->SetFontAndColor(0, name
.Length(), &font
);
196 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
197 // Use the asynchronous version so we don't block the window's thread
204 /** Make sure we don't disable the last CPU - this is needed by
205 * descendants of PulseView for the popup menu and for CPUButton
206 * both as a replicant and not.
210 LastEnabledCPU(unsigned int my_cpu
)
212 system_info sys_info
;
213 get_system_info(&sys_info
);
214 if (sys_info
.cpu_count
== 1)
217 for (unsigned int x
= 0; x
< sys_info
.cpu_count
; x
++) {
220 if (_kern_cpu_enabled(x
) == 1)
227 /** Ensure that the mini mode and deskbar mode always show an indicator
228 * for each CPU, at least one pixel wide.
232 GetMinimumViewWidth()
234 system_info sys_info
;
235 get_system_info(&sys_info
);
236 return (sys_info
.cpu_count
* 2) + 1;
243 printf(B_TRANSLATE("Usage: Pulse [--mini] [-w width] [--width=width]\n"
244 "\t[--deskbar] [--normal] [--framecolor 0xrrggbb]\n"
245 "\t[--activecolor 0xrrggbb] [--idlecolor 0xrrggbb]\n"));
253 PulseApp
*pulseapp
= (PulseApp
*)be_app
;
254 BDeskbar
*deskbar
= new BDeskbar();
255 // Don't allow two copies in the Deskbar at once
256 if (deskbar
->HasItem("DeskbarPulseView")) {
261 // Must be 16 pixels high, the width is retrieved from the Prefs class
262 int width
= pulseapp
->prefs
->deskbar_icon_width
;
263 int min_width
= GetMinimumViewWidth();
264 if (width
< min_width
) {
265 pulseapp
->prefs
->deskbar_icon_width
= min_width
;
269 BRect
rect(0, 0, width
- 1, 15);
270 DeskbarPulseView
*replicant
= new DeskbarPulseView(rect
);
271 status_t err
= deskbar
->AddItem(replicant
);
276 snprintf(message
.LockBuffer(512), 512,
277 B_TRANSLATE("Installing in Deskbar failed\n%s"), strerror(err
));
278 message
.UnlockBuffer();
279 BAlert
*alert
= new BAlert(B_TRANSLATE("Error"),
280 message
.String(), B_TRANSLATE("OK"));
281 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
291 main(int argc
, char **argv
)
293 PulseApp
*pulseapp
= new PulseApp(argc
, argv
);