bool PowerX11::GetPowerInfo_Linux_proc_acpi() { String node; DirAccess *dirp = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); bool have_battery = false; bool have_ac = false; bool charging = false; this->nsecs_left = -1; this->percent_left = -1; this->power_state = OS::POWERSTATE_UNKNOWN; dirp->change_dir(proc_acpi_battery_path); Error err = dirp->list_dir_begin(); if (err != OK) { return false; /* can't use this interface. */ } else { node = dirp->get_next(); while (node != "") { check_proc_acpi_battery(node.utf8().get_data(), &have_battery, &charging /*, seconds, percent*/); node = dirp->get_next(); } memdelete(dirp); } dirp->change_dir(proc_acpi_ac_adapter_path); err = dirp->list_dir_begin(); if (err != OK) { return false; /* can't use this interface. */ } else { node = dirp->get_next(); while (node != "") { check_proc_acpi_ac_adapter(node.utf8().get_data(), &have_ac); node = dirp->get_next(); } memdelete(dirp); } if (!have_battery) { this->power_state = OS::POWERSTATE_NO_BATTERY; } else if (charging) { this->power_state = OS::POWERSTATE_CHARGING; } else if (have_ac) { this->power_state = OS::POWERSTATE_CHARGED; } else { this->power_state = OS::POWERSTATE_ON_BATTERY; } return true; /* definitive answer. */ }
void ExportTemplateManager::_uninstall_template_confirm() { DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); Error err = d->change_dir(EditorSettings::get_singleton()->get_templates_dir()); ERR_FAIL_COND(err != OK); err = d->change_dir(to_remove); ERR_FAIL_COND(err != OK); Vector<String> files; d->list_dir_begin(); bool isdir; String c = d->get_next(&isdir); while (c != String()) { if (!isdir) { files.push_back(c); } c = d->get_next(&isdir); } d->list_dir_end(); for (int i = 0; i < files.size(); i++) { d->remove(files[i]); } d->change_dir(".."); d->remove(to_remove); _update_template_list(); }
void FindInFiles::_scan_dir(String path, PoolStringArray &out_folders) { DirAccess *dir = DirAccess::open(path); if (dir == NULL) { print_line("Cannot open directory! " + path); return; } dir->list_dir_begin(); for (int i = 0; i < 1000; ++i) { String file = dir->get_next(); if (file == "") break; // Ignore special dirs and hidden dirs (such as .git and .import) if (file == "." || file == ".." || file.begins_with(".")) continue; if (dir->current_is_dir()) out_folders.append(file); else { String file_ext = file.get_extension(); if (_extension_filter.has(file_ext)) { _files_to_scan.push_back(path.plus_file(file)); } } } }
void RotatedFileLogger::clear_old_backups() { int max_backups = max_files - 1; // -1 for the current file String basename = base_path.get_file().get_basename(); String extension = "." + base_path.get_extension(); DirAccess *da = DirAccess::open(base_path.get_base_dir()); if (!da) { return; } da->list_dir_begin(); String f = da->get_next(); Set<String> backups; while (f != String()) { if (!da->current_is_dir() && f.begins_with(basename) && f.ends_with(extension) && f != base_path.get_file()) { backups.insert(f); } f = da->get_next(); } da->list_dir_end(); if (backups.size() > max_backups) { // since backups are appended with timestamp and Set iterates them in sorted order, // first backups are the oldest int to_delete = backups.size() - max_backups; for (Set<String>::Element *E = backups.front(); E && to_delete > 0; E = E->next(), --to_delete) { da->remove(E->get()); } } memdelete(da); }
MainLoop *test() { print_line("this is test io"); DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); da->change_dir("."); print_line("Opening current dir " + da->get_current_dir()); String entry; da->list_dir_begin(); while ((entry = da->get_next()) != "") { print_line("entry " + entry + " is dir: " + Variant(da->current_is_dir())); }; da->list_dir_end(); RES texture = ResourceLoader::load("test_data/rock.png"); ERR_FAIL_COND_V(texture.is_null(), NULL); ResourceSaver::save("test_data/rock.xml", texture); print_line("localize paths"); print_line(ProjectSettings::get_singleton()->localize_path("algo.xml")); print_line(ProjectSettings::get_singleton()->localize_path("c:\\windows\\algo.xml")); print_line(ProjectSettings::get_singleton()->localize_path(ProjectSettings::get_singleton()->get_resource_path() + "/something/something.xml")); print_line(ProjectSettings::get_singleton()->localize_path("somedir/algo.xml")); { FileAccess *z = FileAccess::open("test_data/archive.zip", FileAccess::READ); int len = z->get_len(); Vector<uint8_t> zip; zip.resize(len); z->get_buffer(&zip[0], len); z->close(); memdelete(z); FileAccessMemory::register_file("a_package", zip); FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_RESOURCES); FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_FILESYSTEM); FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_USERDATA); print_line("archive test"); }; print_line("test done"); return memnew(TestMainLoop); }
void ProjectManager::_files_dropped(StringArray p_files, int p_screen) { Set<String> folders_set; DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); for (int i = 0; i < p_files.size(); i++) { String file = p_files[i]; folders_set.insert(da->dir_exists(file) ? file : file.get_base_dir()); } memdelete(da); if (folders_set.size()>0) { StringArray folders; for (Set<String>::Element *E=folders_set.front();E;E=E->next()) { folders.append(E->get()); } bool confirm = true; if (folders.size()==1) { DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); if (dir->change_dir(folders[0])==OK) { dir->list_dir_begin(); String file = dir->get_next(); while(confirm && file!=String()) { if (!da->current_is_dir() && file.ends_with("engine.cfg")) { confirm = false; } file = dir->get_next(); } dir->list_dir_end(); } memdelete(dir); } if (confirm) { multi_scan_ask->get_ok()->disconnect("pressed", this, "_scan_multiple_folders"); multi_scan_ask->get_ok()->connect("pressed", this, "_scan_multiple_folders", varray(folders)); multi_scan_ask->set_text(vformat(TTR("You are about the scan %s folders for existing Godot projects. Do you confirm?"), folders.size())); multi_scan_ask->popup_centered_minsize(); } else { _scan_multiple_folders(folders); } } }
void ExportTemplateManager::_update_template_list() { while (current_hb->get_child_count()) { memdelete(current_hb->get_child(0)); } while (installed_vb->get_child_count()) { memdelete(installed_vb->get_child(0)); } DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); Error err = d->change_dir(EditorSettings::get_singleton()->get_templates_dir()); d->list_dir_begin(); Set<String> templates; if (err == OK) { bool isdir; String c = d->get_next(&isdir); while (c != String()) { if (isdir && !c.begins_with(".")) { templates.insert(c); } c = d->get_next(&isdir); } } d->list_dir_end(); memdelete(d); String current_version = VERSION_FULL_CONFIG; Label *current = memnew(Label); current->set_h_size_flags(SIZE_EXPAND_FILL); current_hb->add_child(current); if (templates.has(current_version)) { current->add_color_override("font_color", get_color("success_color", "Editor")); Button *redownload = memnew(Button); redownload->set_text(TTR("Re-Download")); current_hb->add_child(redownload); redownload->connect("pressed", this, "_download_template", varray(current_version)); Button *uninstall = memnew(Button); uninstall->set_text(TTR("Uninstall")); current_hb->add_child(uninstall); current->set_text(current_version + " " + TTR("(Installed)")); uninstall->connect("pressed", this, "_uninstall_template", varray(current_version)); } else { current->add_color_override("font_color", get_color("error_color", "Editor")); Button *redownload = memnew(Button); redownload->set_text(TTR("Download")); redownload->connect("pressed", this, "_download_template", varray(current_version)); current_hb->add_child(redownload); current->set_text(current_version + " " + TTR("(Missing)")); } for (Set<String>::Element *E = templates.back(); E; E = E->prev()) { HBoxContainer *hbc = memnew(HBoxContainer); Label *version = memnew(Label); version->set_modulate(get_color("disabled_font_color", "Editor")); String text = E->get(); if (text == current_version) { text += " " + TTR("(Current)"); } version->set_text(text); version->set_h_size_flags(SIZE_EXPAND_FILL); hbc->add_child(version); Button *uninstall = memnew(Button); uninstall->set_text(TTR("Uninstall")); hbc->add_child(uninstall); uninstall->connect("pressed", this, "_uninstall_template", varray(E->get())); installed_vb->add_child(hbc); } }
MainLoop* test() { print_line("this is test io"); DirAccess* da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); da->change_dir("."); print_line("Opening current dir "+ da->get_current_dir()); String entry; da->list_dir_begin(); while ( (entry = da->get_next()) != "") { print_line("entry "+entry+" is dir: " + Variant(da->current_is_dir())); }; da->list_dir_end(); RES texture = ResourceLoader::load("test_data/rock.png"); ERR_FAIL_COND_V(texture.is_null(), NULL); ResourceSaver::save("test_data/rock.xml",texture); print_line("localize paths"); print_line(Globals::get_singleton()->localize_path("algo.xml")); print_line(Globals::get_singleton()->localize_path("c:\\windows\\algo.xml")); print_line(Globals::get_singleton()->localize_path(Globals::get_singleton()->get_resource_path()+"/something/something.xml")); print_line(Globals::get_singleton()->localize_path("somedir/algo.xml")); { FileAccess* z = FileAccess::open("test_data/archive.zip", FileAccess::READ); int len = z->get_len(); Vector<uint8_t> zip; zip.resize(len); z->get_buffer(&zip[0], len); z->close(); memdelete(z); FileAccessMemory::register_file("a_package", zip); FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_RESOURCES); FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_FILESYSTEM); FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_USERDATA); print_line("archive test"); #if 0 Archive arch; Archive::get_singleton()->add_package("a_package"); FileAccessArchive f; print_line("opening for read"); f._open("file.txt", FileAccess::READ); int pos = f.get_pos(); printf("file has %i bytes, initial pos %i\n", (int)f.get_len(), pos); do { printf("%c", f.get_8()); } while (!f.eof_reached()); print_line("opening for stored seek"); f.open("seek.bin", FileAccess::READ); pos = f.get_pos(); printf("byte at pos %i is %i\n", pos, (int)f.get_8()); f.seek(128); pos = f.get_pos(); printf("byte at pos %i is %i\n", pos, (int)f.get_8()); print_line("opening for deflated seek"); f.open("seek_deflated.bin", FileAccess::READ); pos = f.get_pos(); printf("byte at pos %i is %i\n", pos, (int)f.get_8()); f.seek(128); pos = f.get_pos(); printf("byte at pos %i is %i\n", pos, (int)f.get_8()); pos = f.get_pos(); printf("byte at pos %i is %i\n", pos, (int)f.get_8()); pos = f.get_pos(); printf("byte at pos %i is %i\n", pos, (int)f.get_8()); f.seek(256); pos = f.get_pos(); printf("byte at pos %i is %i\n", pos, (int)f.get_8()); pos = f.get_pos(); printf("byte at pos %i is %i\n", pos, (int)f.get_8()); pos = f.get_pos(); printf("byte at pos %i is %i\n", pos, (int)f.get_8()); f.seek(4); pos = f.get_pos(); printf("byte at pos %i is %i\n", pos, (int)f.get_8()); pos = f.get_pos(); printf("byte at pos %i is %i\n", pos, (int)f.get_8()); pos = f.get_pos(); printf("byte at pos %i is %i\n", pos, (int)f.get_8()); f.close(); DirAccessArchive d; String dir = "../blah1/blah2/blahask/../blah3/.//blah4/"; printf("changing dir to %s\n", dir.utf8().get_data()); d.change_dir(dir); printf("current dir is %s\n", d.get_current_dir().utf8().get_data()); FileAccessMemory::cleanup(); #endif }; print_line("test done"); return memnew( TestMainLoop ); }
void EditorPluginSettings::update_plugins() { plugin_list->clear(); DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); Error err = da->change_dir("res://addons"); if (err!=OK) { memdelete(da); return; } updating=true; TreeItem *root = plugin_list->create_item(); da->list_dir_begin(); String d = da->get_next(); Vector<String> plugins; while(d!=String()) { bool dir = da->current_is_dir(); String path = "res://addons/"+d+"/plugin.cfg"; if (dir && FileAccess::exists(path)) { plugins.push_back(d); } d = da->get_next(); } da->list_dir_end(); memdelete(da); plugins.sort(); Vector<String> active_plugins = GlobalConfig::get_singleton()->get("plugins/active"); for(int i=0;i<plugins.size();i++) { Ref<ConfigFile> cf; cf.instance(); String path = "res://addons/"+plugins[i]+"/plugin.cfg"; Error err = cf->load(path); if (err!=OK) { WARN_PRINTS("Can't load plugin config: "+path); } else if (!cf->has_section_key("plugin","name")) { WARN_PRINTS("Plugin misses plugin/name: "+path); } else if (!cf->has_section_key("plugin","author")) { WARN_PRINTS("Plugin misses plugin/author: "+path); } else if (!cf->has_section_key("plugin","version")) { WARN_PRINTS("Plugin misses plugin/version: "+path); } else if (!cf->has_section_key("plugin","description")) { WARN_PRINTS("Plugin misses plugin/description: "+path); } else if (!cf->has_section_key("plugin","script")) { WARN_PRINTS("Plugin misses plugin/script: "+path); } else { String d = plugins[i]; String name = cf->get_value("plugin","name"); String author = cf->get_value("plugin","author"); String version = cf->get_value("plugin","version"); String description = cf->get_value("plugin","description"); String script = cf->get_value("plugin","script"); TreeItem *item = plugin_list->create_item(root); item->set_text(0,name); item->set_tooltip(0,"Name: "+name+"\nPath: "+path+"\nMain Script: "+script); item->set_metadata(0,d); item->set_text(1,version); item->set_metadata(1,script); item->set_text(2,author); item->set_metadata(2,description); item->set_cell_mode(3,TreeItem::CELL_MODE_RANGE); item->set_range_config(3,0,1,1); item->set_text(3,"Inactive,Active"); item->set_editable(3,true); if (EditorNode::get_singleton()->is_addon_plugin_enabled(d)) { item->set_custom_color(3,Color(0.2,1,0.2)); item->set_range(3,1); } else { item->set_custom_color(3,Color(1,0.2,0.2)); item->set_range(3,0); } } } updating=false; }
void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir,const ScanProgress& p_progress) { uint64_t current_mtime = FileAccess::get_modified_time(p_dir->get_path()); bool updated_dir=false; //print_line("dir: "+p_dir->get_path()+" MODTIME: "+itos(p_dir->modified_time)+" CTIME: "+itos(current_mtime)); if (current_mtime!=p_dir->modified_time) { updated_dir=true; p_dir->modified_time=current_mtime; //ooooops, dir changed, see what's going on //first mark everything as veryfied for(int i=0;i<p_dir->files.size();i++) { p_dir->files[i]->verified=false; } for(int i=0;i<p_dir->subdirs.size();i++) { p_dir->get_subdir(i)->verified=false; } //then scan files and directories and check what's different DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); String cd = p_dir->get_path(); da->change_dir(cd); da->list_dir_begin(); while (true) { bool isdir; String f = da->get_next(&isdir); if (f=="") break; if (isdir) { if (f.begins_with(".")) //ignore hidden and . / .. continue; int idx = p_dir->find_dir_index(f); if (idx==-1) { if (FileAccess::exists(cd.plus_file(f).plus_file("engine.cfg"))) // skip if another project inside this continue; EditorFileSystemDirectory *efd = memnew( EditorFileSystemDirectory ); efd->parent=p_dir; efd->name=f; DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES); d->change_dir(cd.plus_file(f)); _scan_new_dir(efd,d,p_progress.get_sub(1,1)); memdelete(d); ItemAction ia; ia.action=ItemAction::ACTION_DIR_ADD; ia.dir=p_dir; ia.file=f; ia.new_dir=efd; scan_actions.push_back(ia); } else { p_dir->subdirs[idx]->verified=true; } } else { String ext = f.extension().to_lower(); if (!valid_extensions.has(ext)) continue; //invalid int idx = p_dir->find_file_index(f); if (idx==-1) { //never seen this file, add actition to add it EditorFileSystemDirectory::FileInfo *fi = memnew( EditorFileSystemDirectory::FileInfo ); fi->file=f; String path = cd.plus_file(fi->file); fi->modified_time=FileAccess::get_modified_time(path); fi->meta=_get_meta(path); fi->type=ResourceLoader::get_resource_type(path); { ItemAction ia; ia.action=ItemAction::ACTION_FILE_ADD; ia.dir=p_dir; ia.file=f; ia.new_file=fi; scan_actions.push_back(ia); } //take the chance and scan sources if (_check_meta_sources(fi->meta)) { ItemAction ia; ia.action=ItemAction::ACTION_FILE_SOURCES_CHANGED; ia.dir=p_dir; ia.file=f; scan_actions.push_back(ia); } } else { p_dir->files[idx]->verified=true; } } } da->list_dir_end(); memdelete(da); } for(int i=0;i<p_dir->files.size();i++) { if (updated_dir && !p_dir->files[i]->verified) { //this file was removed, add action to remove it ItemAction ia; ia.action=ItemAction::ACTION_FILE_REMOVE; ia.dir=p_dir; ia.file=p_dir->files[i]->file; scan_actions.push_back(ia); continue; } if (_check_meta_sources(p_dir->files[i]->meta)) { ItemAction ia; ia.action=ItemAction::ACTION_FILE_SOURCES_CHANGED; ia.dir=p_dir; ia.file=p_dir->files[i]->file; scan_actions.push_back(ia); } } for(int i=0;i<p_dir->subdirs.size();i++) { if (updated_dir && !p_dir->subdirs[i]->verified) { //this directory was removed, add action to remove it ItemAction ia; ia.action=ItemAction::ACTION_DIR_REMOVE; ia.dir=p_dir->subdirs[i]; scan_actions.push_back(ia); continue; } _scan_fs_changes(p_dir->get_subdir(i),p_progress); } }
void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, const ScanProgress &p_progress) { uint64_t current_mtime = FileAccess::get_modified_time(p_dir->get_path()); bool updated_dir = false; String cd = p_dir->get_path(); //print_line("dir: "+p_dir->get_path()+" MODTIME: "+itos(p_dir->modified_time)+" CTIME: "+itos(current_mtime)); if (current_mtime != p_dir->modified_time) { updated_dir = true; p_dir->modified_time = current_mtime; //ooooops, dir changed, see what's going on //first mark everything as veryfied for (int i = 0; i < p_dir->files.size(); i++) { p_dir->files[i]->verified = false; } for (int i = 0; i < p_dir->subdirs.size(); i++) { p_dir->get_subdir(i)->verified = false; } //then scan files and directories and check what's different DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); da->change_dir(cd); da->list_dir_begin(); while (true) { bool isdir; String f = da->get_next(&isdir); if (f == "") break; if (isdir) { if (f.begins_with(".")) //ignore hidden and . / .. continue; int idx = p_dir->find_dir_index(f); if (idx == -1) { if (FileAccess::exists(cd.plus_file(f).plus_file("project.godot"))) // skip if another project inside this continue; if (FileAccess::exists(cd.plus_file(f).plus_file(".gdignore"))) // skip if another project inside this continue; EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory); efd->parent = p_dir; efd->name = f; DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES); d->change_dir(cd.plus_file(f)); _scan_new_dir(efd, d, p_progress.get_sub(1, 1)); memdelete(d); ItemAction ia; ia.action = ItemAction::ACTION_DIR_ADD; ia.dir = p_dir; ia.file = f; ia.new_dir = efd; scan_actions.push_back(ia); } else { p_dir->subdirs[idx]->verified = true; } } else { String ext = f.get_extension().to_lower(); if (!valid_extensions.has(ext)) continue; //invalid int idx = p_dir->find_file_index(f); if (idx == -1) { //never seen this file, add actition to add it EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo); fi->file = f; String path = cd.plus_file(fi->file); fi->modified_time = FileAccess::get_modified_time(path); fi->import_modified_time = 0; fi->type = ResourceLoader::get_resource_type(path); { ItemAction ia; ia.action = ItemAction::ACTION_FILE_ADD; ia.dir = p_dir; ia.file = f; ia.new_file = fi; scan_actions.push_back(ia); } if (import_extensions.has(ext)) { //if it can be imported, and it was added, it needs to be reimported print_line("REIMPORT: file was not found before, reimport"); print_line("at dir: " + p_dir->get_path() + " file: " + f); for (int i = 0; i < p_dir->files.size(); i++) { print_line(itos(i) + ": " + p_dir->files[i]->file); } ItemAction ia; ia.action = ItemAction::ACTION_FILE_REIMPORT; ia.dir = p_dir; ia.file = f; scan_actions.push_back(ia); } } else { p_dir->files[idx]->verified = true; } } } da->list_dir_end(); memdelete(da); } for (int i = 0; i < p_dir->files.size(); i++) { if (updated_dir && !p_dir->files[i]->verified) { //this file was removed, add action to remove it ItemAction ia; ia.action = ItemAction::ACTION_FILE_REMOVE; ia.dir = p_dir; ia.file = p_dir->files[i]->file; scan_actions.push_back(ia); continue; } if (import_extensions.has(p_dir->files[i]->file.get_extension().to_lower())) { //check here if file must be imported or not String path = cd.plus_file(p_dir->files[i]->file); uint64_t mt = FileAccess::get_modified_time(path); bool reimport = false; if (mt != p_dir->files[i]->modified_time) { print_line("REIMPORT: modified time changed, reimport"); reimport = true; //it was modified, must be reimported. } else if (!FileAccess::exists(path + ".import")) { print_line("REIMPORT: no .import exists, reimport"); reimport = true; //no .import file, obviously reimport } else { uint64_t import_mt = FileAccess::get_modified_time(path + ".import"); //print_line(itos(import_mt) + " vs " + itos(p_dir->files[i]->import_modified_time)); if (import_mt != p_dir->files[i]->import_modified_time) { print_line("REIMPORT: import modified changed, reimport"); reimport = true; } else if (!_check_missing_imported_files(path)) { print_line("REIMPORT: imported files removed"); reimport = true; } } if (reimport) { ItemAction ia; ia.action = ItemAction::ACTION_FILE_REIMPORT; ia.dir = p_dir; ia.file = p_dir->files[i]->file; scan_actions.push_back(ia); } } EditorResourcePreview::get_singleton()->check_for_invalidation(p_dir->get_file_path(i)); } for (int i = 0; i < p_dir->subdirs.size(); i++) { if (updated_dir && !p_dir->subdirs[i]->verified) { //this directory was removed, add action to remove it ItemAction ia; ia.action = ItemAction::ACTION_DIR_REMOVE; ia.dir = p_dir->subdirs[i]; scan_actions.push_back(ia); continue; } _scan_fs_changes(p_dir->get_subdir(i), p_progress); } }
bool PowerX11::GetPowerInfo_Linux_sys_class_power_supply(/*PowerState *state, int *seconds, int *percent*/) { const char *base = sys_class_power_supply_path; String name; DirAccess *dirp = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); dirp->change_dir(base); Error err = dirp->list_dir_begin(); if (err != OK) { return false; } this->power_state = OS::POWERSTATE_NO_BATTERY; /* assume we're just plugged in. */ this->nsecs_left = -1; this->percent_left = -1; name = dirp->get_next(); while (name != "") { bool choose = false; char str[64]; OS::PowerState st; int secs; int pct; if ((name == ".") || (name == "..")) { name = dirp->get_next(); continue; //skip these, of course. } else { if (!read_power_file(base, name.utf8().get_data(), "type", str, sizeof(str))) { name = dirp->get_next(); continue; // Don't know _what_ we're looking at. Give up on it. } else { if (String(str) != "Battery\n") { name = dirp->get_next(); continue; // we don't care about UPS and such. } } } /* some drivers don't offer this, so if it's not explicitly reported assume it's present. */ if (read_power_file(base, name.utf8().get_data(), "present", str, sizeof(str)) && (String(str) == "0\n")) { st = OS::POWERSTATE_NO_BATTERY; } else if (!read_power_file(base, name.utf8().get_data(), "status", str, sizeof(str))) { st = OS::POWERSTATE_UNKNOWN; /* uh oh */ } else if (String(str) == "Charging\n") { st = OS::POWERSTATE_CHARGING; } else if (String(str) == "Discharging\n") { st = OS::POWERSTATE_ON_BATTERY; } else if ((String(str) == "Full\n") || (String(str) == "Not charging\n")) { st = OS::POWERSTATE_CHARGED; } else { st = OS::POWERSTATE_UNKNOWN; /* uh oh */ } if (!read_power_file(base, name.utf8().get_data(), "capacity", str, sizeof(str))) { pct = -1; } else { pct = String(str).to_int(); pct = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */ } if (!read_power_file(base, name.utf8().get_data(), "time_to_empty_now", str, sizeof(str))) { secs = -1; } else { secs = String(str).to_int(); secs = (secs <= 0) ? -1 : secs; /* 0 == unknown */ } /* * We pick the battery that claims to have the most minutes left. * (failing a report of minutes, we'll take the highest percent.) */ if ((secs < 0) && (this->nsecs_left < 0)) { if ((pct < 0) && (this->percent_left < 0)) { choose = true; /* at least we know there's a battery. */ } else if (pct > this->percent_left) { choose = true; } } else if (secs > this->nsecs_left) { choose = true; } if (choose) { this->nsecs_left = secs; this->percent_left = pct; this->power_state = st; } name = dirp->get_next(); } memdelete(dirp); return true; /* don't look any further*/ }