1 //===========================================
2 // Lumina-DE source code
3 // Copyright (c) 2014, Ken Moore
4 // Available under the 3-clause BSD license
5 // See the LICENSE file for full details
6 //===========================================
7 #ifdef __DragonFly__
8 #include "LuminaOS.h"
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <sys/sysctl.h>
12 #include <sys/sensors.h>
13
14 //can't read xbrightness settings - assume invalid until set
15 static int screenbrightness = -1;
16 static int audiovolume = -1;
17
get_sysctlbyname_int(const char * name,int * res)18 static bool get_sysctlbyname_int(const char *name, int *res) {
19 int r = 0;
20 size_t len = sizeof(r);
21 if (sysctlbyname(name, &r, &len, NULL, 0) == 0) {
22 *res = r;
23 return true;
24 }
25 return false;
26 }
27
28 #if 0
29 static bool get_sysctlbyname_qstr(const char *name, QString &str) {
30 size_t len = 0;
31 sysctlbyname(name, NULL, &len, NULL, 0);
32 if (len > 0) {
33 void *buf = malloc(len);
34 if (buf) {
35 int res = sysctlbyname(name, buf, &len, NULL, 0);
36 if (res == 0) {
37 str = QString((char*) buf);
38 }
39 free(buf);
40 return (res == 0);
41 }
42 }
43 return false;
44 }
45 #endif
46
47 // returns -1 on error.
get_sysctlbyname_int(const char * name)48 static int get_sysctlbyname_int(const char *name) {
49 int res = -1;
50 if (get_sysctlbyname_int(name, &res)) {
51 return res;
52 }
53 return -1;
54 }
55
get_sysctlbyname_uint(const char * name,unsigned int * res)56 static bool get_sysctlbyname_uint(const char *name, unsigned int *res) {
57 unsigned int r = 0;
58 size_t len = sizeof(r);
59 if (sysctlbyname(name, &r, &len, NULL, 0) == 0) {
60 *res = r;
61 return true;
62 }
63 return false;
64 }
65
OSName()66 QString LOS::OSName(){ return "DragonFly BSD"; }
67
68 //OS-specific prefix(s)
69 // NOTE: PREFIX, L_ETCDIR, L_SHAREDIR are defined in the OS-detect.pri project file and passed in
LuminaShare()70 QString LOS::LuminaShare(){ return (L_SHAREDIR+"/lumina-desktop/"); } //Install dir for Lumina share files
AppPrefix()71 QString LOS::AppPrefix(){ return "/usr/local/"; } //Prefix for applications
SysPrefix()72 QString LOS::SysPrefix(){ return "/usr/"; } //Prefix for system
73
74 //OS-specific application shortcuts (*.desktop files)
ControlPanelShortcut()75 QString LOS::ControlPanelShortcut(){ return ""; } //system control panel
AppStoreShortcut()76 QString LOS::AppStoreShortcut(){ return ""; } //graphical app/pkg manager
77 //OS-specific RSS feeds (Format: QStringList[ <name>::::<url> ]; )
RSSFeeds()78 QStringList LOS::RSSFeeds(){
79 QStringList feeds;
80 feeds << "DragonFly BSD Feed::::http://www.dragonflybsd.org/recentchanges/index.rss";
81 return feeds;
82 }
83
84 // ==== ExternalDevicePaths() ====
ExternalDevicePaths()85 QStringList LOS::ExternalDevicePaths(){
86 //Returns: QStringList[<type>::::<filesystem>::::<path>]
87 //Note: <type> = [USB, HDRIVE, DVD, SDCARD, UNKNOWN]
88 QStringList devs = LUtils::getCmdOutput("mount");
89 //Now check the output
90 for(int i=0; i<devs.length(); i++){
91 if(devs[i].startsWith("/dev/")){
92 QString type = devs[i].section(" on ",0,0);
93 type.remove("/dev/");
94 //Determine the type of hardware device based on the dev node
95 if(type.startsWith("da")){ type = "USB"; }
96 else if(type.startsWith("ada")){ type = "HDRIVE"; }
97 else if(type.startsWith("mmsd")){ type = "SDCARD"; }
98 else if(type.startsWith("cd")||type.startsWith("acd")){ type="DVD"; }
99 else{ type = "UNKNOWN"; }
100 //Now put the device in the proper output format
101 devs[i] = type+"::::"+devs[i].section("(",1,1).section(",",0,0)+"::::"+devs[i].section(" on ",1,50).section("(",0,0).simplified();
102 }else{
103 //invalid device - remove it from the list
104 devs.removeAt(i);
105 i--;
106 }
107 }
108 return devs;
109 }
110
111 //Read screen brightness information
ScreenBrightness()112 int LOS::ScreenBrightness(){
113 //Returns: Screen Brightness as a percentage (0-100, with -1 for errors)
114 if(screenbrightness==-1){
115 if(QFile::exists(QString(getenv("XDG_CONFIG_HOME"))+"/lumina-desktop/.currentxbrightness")){
116 int val = LUtils::readFile(QString(getenv("XDG_CONFIG_HOME"))+"/lumina-desktop/.currentxbrightness").join("").simplified().toInt();
117 screenbrightness = val;
118 }
119 }
120 //If it gets to this point, then we have a valid (but new) installation
121 if(screenbrightness<0){ screenbrightness = 100; } //default value for systems
122
123 return screenbrightness;
124 }
125
126 //Set screen brightness
setScreenBrightness(int percent)127 void LOS::setScreenBrightness(int percent){
128 if(percent == -1){ return; } //This is usually an invalid value passed directly to the setter
129 //ensure bounds
130 if(percent<0){percent=0;}
131 else if(percent>100){ percent=100; }
132 //Run the command(s)
133 bool success = false;
134 float pf = percent/100.0; //convert to a decimel
135 //Run the command
136 QString cmd = "xbrightness %1";
137 cmd = cmd.arg( QString::number( int(65535*pf) ) );
138 success = (0 == LUtils::runCmd(cmd) );
139 //Save the result for later
140 if(!success){ screenbrightness = -1; }
141 else{ screenbrightness = percent; }
142 LUtils::writeFile(QString(getenv("XDG_CONFIG_HOME"))+"/lumina-desktop/.currentxbrightness", QStringList() << QString::number(screenbrightness), true);
143 }
144
145 //Read the current volume
audioVolume()146 int LOS::audioVolume(){ //Returns: audio volume as a percentage (0-100, with -1 for errors)
147 int out = audiovolume;
148 if(out < 0){
149 //First time session check: Load the last setting for this user
150 QString info = LUtils::readFile(QString(getenv("XDG_CONFIG_HOME"))+"/lumina-desktop/.currentvolume").join("");
151 if(!info.isEmpty()){
152 out = info.simplified().toInt();
153 audiovolume = out; //unset this internal flag
154 return out;
155 }
156 }
157
158 //probe the system for the current volume (other utils could be changing it)
159 QString info = LUtils::getCmdOutput("mixer -S vol").join(":").simplified(); //ignores any other lines
160 if(!info.isEmpty()){
161 int L = info.section(":",1,1).toInt();
162 int R = info.section(":",2,2).toInt();
163 if(L>R){ out = L; }
164 else{ out = R; }
165 if(out != audiovolume){
166 //Volume changed by other utility: adjust the saved value as well
167 LUtils::writeFile(QString(getenv("XDG_CONFIG_HOME"))+"/lumina-desktop/.currentvolume", QStringList() << QString::number(out), true);
168 }
169 audiovolume = out;
170 }
171
172 return out;
173 }
174
175 //Set the current volume
setAudioVolume(int percent)176 void LOS::setAudioVolume(int percent){
177 if(percent<0){percent=0;}
178 else if(percent>100){percent=100;}
179 QString info = LUtils::getCmdOutput("mixer -S vol").join(":").simplified(); //ignores any other lines
180 if(!info.isEmpty()){
181 int L = info.section(":",1,1).toInt();
182 int R = info.section(":",2,2).toInt();
183 int diff = L-R;
184 if((percent == L) && (L==R)){ return; } //already set to that volume
185 if(diff<0){ R=percent; L=percent+diff; } //R Greater
186 else{ L=percent; R=percent-diff; } //L Greater or equal
187 //Check bounds
188 if(L<0){L=0;}else if(L>100){L=100;}
189 if(R<0){R=0;}else if(R>100){R=100;}
190 //Run Command
191 audiovolume = percent; //save for checking later
192 LUtils::runCmd("mixer vol "+QString::number(L)+":"+QString::number(R));
193 LUtils::writeFile(QString(getenv("XDG_CONFIG_HOME"))+"/lumina-desktop/.currentvolume", QStringList() << QString::number(percent), true);
194 }
195 }
196
197 //Change the current volume a set amount (+ or -)
changeAudioVolume(int percentdiff)198 void LOS::changeAudioVolume(int percentdiff){
199 QString info = LUtils::getCmdOutput("mixer -S vol").join(":").simplified(); //ignores any other lines
200 if(!info.isEmpty()){
201 int L = info.section(":",1,1).toInt() + percentdiff;
202 int R = info.section(":",2,2).toInt() + percentdiff;
203 //Check bounds
204 if(L<0){L=0;}else if(L>100){L=100;}
205 if(R<0){R=0;}else if(R>100){R=100;}
206 //Run Command
207 LUtils::runCmd("mixer vol "+QString::number(L)+":"+QString::number(R));
208 }
209 }
210
211 //Check if a graphical audio mixer is installed
hasMixerUtility()212 bool LOS::hasMixerUtility(){
213 return false; //not implemented yet for DragonFly
214 }
215
216 //Launch the graphical audio mixer utility
startMixerUtility()217 void LOS::startMixerUtility(){
218 //Not implemented yet for DragonFly
219 }
220
221 //Check for user system permission (shutdown/restart)
userHasShutdownAccess()222 bool LOS::userHasShutdownAccess(){
223 return true; //not implemented yet
224 }
225
226 //Check for whether the system is safe to power off (no updates being perfomed)
systemPerformingUpdates()227 bool LOS::systemPerformingUpdates(){
228 return false; //Not implemented yet
229 }
230
231 //Return the details of any updates which are waiting to apply on shutdown
systemPendingUpdates()232 QString LOS::systemPendingUpdates(){
233 return "";
234 }
235
236 //System Shutdown
systemShutdown(bool)237 void LOS::systemShutdown(bool){ //start poweroff sequence
238 //INPUT: skip updates (true/false)
239 QProcess::startDetached("shutdown -p now");
240 }
241
242 //System Restart
systemRestart(bool)243 void LOS::systemRestart(bool){ //start reboot sequence
244 //INPUT: skip updates (true/false)
245 QProcess::startDetached("shutdown -r now");
246 }
247
248 //Check for suspend support
systemCanSuspend()249 bool LOS::systemCanSuspend(){
250 return false;
251 }
252
253 //Put the system into the suspend state
systemSuspend()254 void LOS::systemSuspend(){
255
256 }
257
258 //Battery Availability
hasBattery()259 bool LOS::hasBattery(){
260 return (get_sysctlbyname_int("hw.acpi.battery.units") >= 1);
261 }
262
263 //Battery Charge Level
batteryCharge()264 int LOS::batteryCharge(){ //Returns: percent charge (0-100), anything outside that range is counted as an error
265 int charge = get_sysctlbyname_int("hw.acpi.battery.life");
266 if(charge > 100){ charge = -1; } //invalid charge
267 return charge;
268 }
269
270 //Battery Charging State
batteryIsCharging()271 bool LOS::batteryIsCharging(){
272 return (get_sysctlbyname_int("hw.acpi.battery.state") == 0);
273 }
274
275 //Battery Time Remaining
batterySecondsLeft()276 int LOS::batterySecondsLeft(){ //Returns: estimated number of seconds remaining
277 int time = get_sysctlbyname_int("hw.acpi.battery.time");
278 if (time > 0) {
279 // time is in minutes
280 time *= 60;
281 }
282 return time;
283 }
284
285 //File Checksums
Checksums(QStringList filepaths)286 QStringList LOS::Checksums(QStringList filepaths){ //Return: checksum of the input file
287 QStringList info = LUtils::getCmdOutput("md5 \""+filepaths.join("\" \"")+"\"");
288 for(int i=0; i<info.length(); i++){
289 if( !info[i].contains(" = ") ){ info.removeAt(i); i--; }
290 else{
291 //Strip out the extra information
292 info[i] = info[i].section(" = ",1,1);
293 }
294 }
295 return info;
296 }
297
298 //file system capacity
FileSystemCapacity(QString dir)299 QString LOS::FileSystemCapacity(QString dir) { //Return: percentage capacity as give by the df command
300 QStringList mountInfo = LUtils::getCmdOutput("df \"" + dir+"\"");
301 QString::SectionFlag skipEmpty = QString::SectionSkipEmpty;
302 //we take the 5th word on the 2 line
303 QString capacity = mountInfo[1].section(" ",4,4, skipEmpty);
304 return capacity;
305 }
306
sensor_value_to_degC(int64_t value)307 static float sensor_value_to_degC(int64_t value) {
308 return (value - 273150000) / 1000000.0;
309 }
310
311 //Returns: List containing the temperature of any CPU's ("50C" for example)
CPUTemperatures()312 QStringList LOS::CPUTemperatures(){
313 QStringList temps;
314
315 int mib[5];
316 mib[0] = CTL_HW;
317 mib[1] = HW_SENSORS;
318
319 for (int dev=0; dev < MAXSENSORDEVICES; ++dev) {
320 struct sensordev sensordev;
321 size_t sdlen = sizeof(sensordev);
322
323 mib[2] = dev;
324 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
325 continue;
326 }
327 mib[3] = SENSOR_TEMP;
328 for (int numt=0; numt < sensordev.maxnumt[SENSOR_TEMP]; ++numt) {
329 mib[4] = numt;
330 struct sensor sensor;
331 size_t slen = sizeof(sensor);
332 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
333 continue;
334 }
335
336 // XXX: Filter out non-cpu temperatures
337
338 int degC = (int)sensor_value_to_degC(sensor.value);
339 temps << QString::number(degC) + "C" + "(" + QString(sensordev.xname) + ")";
340 }
341 }
342
343 return temps;
344 }
345
CPUUsagePercent()346 int LOS::CPUUsagePercent(){ //Returns: Overall percentage of the amount of CPU cycles in use (-1 for errors)
347 return -1; //not implemented yet
348 }
349
MemoryUsagePercent()350 int LOS::MemoryUsagePercent(){
351 //SYSCTL: vm.stats.vm.v_<something>_count
352 unsigned int v_page_count = 0;
353 unsigned int v_wire_count = 0;
354 unsigned int v_active_count = 0;
355
356 if (!get_sysctlbyname_uint("vm.stats.vm.v_page_count", &v_page_count)) return -1;
357 if (!get_sysctlbyname_uint("vm.stats.vm.v_wire_count", &v_wire_count)) return -1;
358 if (!get_sysctlbyname_uint("vm.stats.vm.v_active_count", &v_active_count)) return -1;
359
360 //List output: [total, wired, active]
361 double perc = 100.0 * ((long)v_wire_count+(long)v_active_count)/((double)v_page_count);
362 return qRound(perc);
363 }
364
DiskUsage()365 QStringList LOS::DiskUsage(){ //Returns: List of current read/write stats for each device
366 return QStringList(); //not implemented yet
367 }
368 #endif
369