00001 /*************************************************************************** 00002 * file klfdefs_linux.cpp 00003 * This file is part of the KLatexFormula Project. 00004 * Copyright (C) 2011 by Philippe Faist 00005 * philippe.faist at bluewin.ch 00006 * * 00007 * This program is free software; you can redistribute it and/or modify * 00008 * it under the terms of the GNU General Public License as published by * 00009 * the Free Software Foundation; either version 2 of the License, or * 00010 * (at your option) any later version. * 00011 * * 00012 * This program is distributed in the hope that it will be useful, * 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00015 * GNU General Public License for more details. * 00016 * * 00017 * You should have received a copy of the GNU General Public License * 00018 * along with this program; if not, write to the * 00019 * Free Software Foundation, Inc., * 00020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00021 ***************************************************************************/ 00022 /* $Id$ */ 00023 00029 #include <sys/utsname.h> 00030 00031 #include <klfdefs.h> 00032 #include <klfsysinfo.h> 00033 00034 #include <QFileInfo> 00035 #include <QFile> 00036 00037 00038 KLF_EXPORT QString klf_defs_sysinfo_arch() 00039 { 00040 static bool is_64 = (sizeof(void*) == 8); 00041 00042 struct utsname d; 00043 uname(&d); 00044 00045 if (d.machine[0] == 'i' && d.machine[2] == '8' && d.machine[3] == '6') { 00046 // i?86 00047 return QString::fromLatin1("x86"); 00048 } 00049 if (!strcmp(d.machine, "x86_64")) { 00050 // we could still be a 32-bit application run on a 64-bit machine 00051 return is_64 ? QString::fromLatin1("x86_64") : QString::fromLatin1("x86"); 00052 } 00053 if (!strncmp(d.machine, "ppc", strlen("ppc"))) { 00054 return is_64 ? QString::fromLatin1("ppc64") : QString::fromLatin1("ppc"); 00055 } 00056 return QString::fromLatin1("unknown"); 00057 } 00058 00059 00060 00061 00062 // access sys info in /sys/class/power_supply/ADP0/online 00063 00064 #define SYSINFO_FILE "/sys/class/power_supply/ADP0/online" 00065 00066 KLF_EXPORT KLFSysInfo::BatteryInfo _klf_linux_battery_info(bool want_info_onbatterypower) 00067 { 00068 KLFSysInfo::BatteryInfo info; 00069 00070 if (!QFile::exists(SYSINFO_FILE)) { 00071 info.islaptop = false; 00072 info.onbatterypower = false; 00073 return info; 00074 } 00075 00076 if (want_info_onbatterypower) { 00077 QFile file(SYSINFO_FILE); 00078 if (!file.open(QIODevice::ReadOnly)) { 00079 klfWarning("Can't open file "<<SYSINFO_FILE); 00080 return info; 00081 } 00082 QTextStream tstr(&file); 00083 00084 int val; 00085 tstr >> val; 00086 // val == 1 -> power online 00087 // val == 0 -> on battery power 00088 info.onbatterypower = ! (bool)val; 00089 } 00090 00091 info.islaptop = true; 00092 00093 return info; 00094 } 00095 00096 KLF_EXPORT KLFSysInfo::BatteryInfo _klf_linux_battery_info() 00097 { 00098 return _klf_linux_battery_info(true); 00099 } 00100 00101 KLF_EXPORT bool _klf_linux_is_laptop() 00102 { 00103 KLFSysInfo::BatteryInfo info; 00104 info = _klf_linux_battery_info(false); 00105 return info.islaptop; 00106 } 00107 00108 KLF_EXPORT bool _klf_linux_is_on_battery_power() 00109 { 00110 KLFSysInfo::BatteryInfo info; 00111 info = _klf_linux_battery_info(true); 00112 return info.onbatterypower; 00113 } 00114