source: lumina/libLumina/LuminaOS-FreeBSD.cpp @ 733da14

releng/10.0.3releng/10.1releng/10.1.1releng/10.1.2
Last change on this file since 733da14 was 733da14, checked in by Ken Moore <ken@…>, 9 months ago

Move all the OS-specific functions into the LuminaOS class (LOS), and create the shell for the Linux version with all the previous submissions to make it work on Linux.

  • Property mode set to 100644
File size: 4.5 KB
Line 
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#include "LuminaOS.h"
8#include <unistd.h>
9
10static int screenbrightness = -1;
11
12// ==== ExternalDevicePaths() ====
13QStringList LOS::ExternalDevicePaths(){
14    //Returns: QStringList[<type>::::<filesystem>::::<path>]
15      //Note: <type> = [USB, HDRIVE, DVD, SDCARD, UNKNOWN]
16  QStringList devs = LUtils::getCmdOutput("mount");
17  //Now check the output
18  for(int i=0; i<devs.length(); i++){
19    if(devs[i].startsWith("/dev/")){
20      QString type = devs[i].section(" on ",0,0);
21        type.remove("/dev/");
22      //Determine the type of hardware device based on the dev node
23      if(type.startsWith("da")){ type = "USB"; }
24      else if(type.startsWith("ada")){ type = "HDRIVE"; }
25      else if(type.startsWith("mmsd")){ type = "SDCARD"; }
26      else if(type.startsWith("cd")||type.startsWith("acd")){ type="DVD"; }
27      else{ type = "UNKNOWN"; }
28      //Now put the device in the proper output format
29      devs[i] = type+"::::"+devs[i].section("(",1,1).section(",",0,0)+"::::"+devs[i].section(" on ",1,50).section("(",0,0).simplified();
30    }else{
31      //invalid device - remove it from the list
32      devs.removeAt(i);
33      i--;
34    }
35  }
36  return devs;
37}
38
39//Read screen brightness information
40int LOS::ScreenBrightness(){
41  //Returns: Screen Brightness as a percentage (0-100, with -1 for errors)
42  return screenbrightness;     
43}
44
45//Set screen brightness
46void LOS::setScreenBrightness(int percent){
47  //ensure bounds
48  if(percent<0){percent=0;}
49  else if(percent>100){ percent=100; }
50  float pf = percent/100.0; //convert to a decimel
51  //Run the command
52  QString cmd = "xbrightness  %1";
53  cmd = cmd.arg( QString::number( int(65535*pf) ) );
54  int ret = LUtils::runCmd(cmd);
55  //Save the result for later
56  if(ret!=0){ screenbrightness = -1; }
57  else{ screenbrightness = percent; }
58}
59
60//Read the current volume
61int LOS::audioVolume(){ //Returns: audio volume as a percentage (0-100, with -1 for errors)
62  QString info = LUtils::getCmdOutput("mixer -S vol").join(":").simplified(); //ignores any other lines
63  int out = -1;
64  if(!info.isEmpty()){
65    int L = info.section(":",1,1).toInt();
66    int R = info.section(":",2,2).toInt();
67    if(L>R){ out = L; }
68    else{ out = R; }
69  }
70  return out;
71}
72
73//Set the current volume
74void LOS::setAudioVolume(int percent){
75  if(percent<0){percent=0;}
76  else if(percent>100){percent=100;}
77  QString info = LUtils::getCmdOutput("mixer -S vol").join(":").simplified(); //ignores any other lines
78  if(!info.isEmpty()){
79    int L = info.section(":",1,1).toInt();
80    int R = info.section(":",2,2).toInt();
81    int diff = L-R;
82    if(diff<0){ R=percent; L=percent+diff; } //R Greater
83    else{ L=percent; R=percent-diff; } //L Greater or equal
84    //Check bounds
85    if(L<0){L=0;}else if(L>100){L=100;}
86    if(R<0){R=0;}else if(R>100){R=100;}
87    //Run Command
88    LUtils::runCmd("mixer vol "+QString::number(L)+":"+QString::number(R));
89  }     
90}
91
92//Change the current volume a set amount (+ or -)
93void LOS::changeAudioVolume(int percentdiff){
94  QString info = LUtils::getCmdOutput("mixer -S vol").join(":").simplified(); //ignores any other lines
95  if(!info.isEmpty()){
96    int L = info.section(":",1,1).toInt() + percentdiff;
97    int R = info.section(":",2,2).toInt() + percentdiff;
98    //Check bounds
99    if(L<0){L=0;}else if(L>100){L=100;}
100    if(R<0){R=0;}else if(R>100){R=100;}
101    //Run Command
102    LUtils::runCmd("mixer vol "+QString::number(L)+":"+QString::number(R));
103  }     
104}
105
106//System Shutdown
107void LOS::systemShutdown(){ //start poweroff sequence
108  QProcess::startDetached("shutdown -p now");
109}
110
111//System Restart
112void LOS::systemRestart(){ //start reboot sequence
113  QProcess::startDetached("shutdown -r now");
114}
115
116//Battery Availability
117bool LOS::hasBattery(){
118  int val = LUtils::getCmdOutput("apm -l").join("").toInt();
119  return (val >= 0 && val <= 100);
120}
121
122//Battery Charge Level
123int LOS::batteryCharge(){ //Returns: percent charge (0-100), anything outside that range is counted as an error
124  int charge = LUtils::getCmdOutput("apm -l").join("").toInt();
125  if(charge > 100){ charge = -1; } //invalid charge
126  return charge;       
127}
128
129//Battery Charging State
130bool LOS::batteryIsCharging(){
131  return (LUtils::getCmdOutput("apm -a").join("").simplified() == "1");
132}
133
134//Battery Time Remaining
135int LOS::batterySecondsLeft(){ //Returns: estimated number of seconds remaining
136  return LUtils::getCmdOutput("apm -t").join("").toInt();
137}
Note: See TracBrowser for help on using the repository browser.