mirror of
				https://github.com/rad4day/Waybar.git
				synced 2025-10-28 06:52:29 +01:00 
			
		
		
		
	Merge pull request #3 from marcplustwo/addbluetoothmodule
Add bluetooth module
This commit is contained in:
		| @@ -66,6 +66,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const { | ||||
|     if (ref == "temperature") { | ||||
|       return new waybar::modules::Temperature(id, config_[name]); | ||||
|     } | ||||
|     if (ref == "bluetooth") { | ||||
|       return new waybar::modules::Bluetooth(id, config_[name]); | ||||
|     } | ||||
|     if (ref.compare(0, 7, "custom/") == 0 && ref.size() > 7) { | ||||
|       return new waybar::modules::Custom(ref.substr(7), id, config_[name]); | ||||
|     } | ||||
|   | ||||
							
								
								
									
										41
									
								
								src/modules/bluetooth.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								src/modules/bluetooth.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | ||||
| #include "modules/bluetooth.hpp" | ||||
| #include "util/rfkill.hpp" | ||||
| #include <linux/rfkill.h> | ||||
|  | ||||
| #include <time.h> | ||||
|  | ||||
| waybar::modules::Bluetooth::Bluetooth(const std::string& id, const Json::Value& config) | ||||
|     : ALabel(config, "bluetooth", id, "{status}", 10), | ||||
|       status_("disabled") { | ||||
|   thread_ = [this] { | ||||
|     dp.emit(); | ||||
|     auto now = std::chrono::system_clock::now(); | ||||
|     auto timeout = std::chrono::floor<std::chrono::seconds>(now + interval_); | ||||
|     auto diff = std::chrono::seconds(timeout.time_since_epoch().count() % interval_.count()); | ||||
|     thread_.sleep_until(timeout - diff); | ||||
|   }; | ||||
|   //dp.emit(); | ||||
| } | ||||
|  | ||||
| auto waybar::modules::Bluetooth::update() -> void { | ||||
|   status_ = "enabled"; | ||||
|   if (waybar::util::rfkill::isDisabled(RFKILL_TYPE_BLUETOOTH)) { | ||||
|     status_ = "disabled"; | ||||
|   } else { | ||||
|     status_ = "enabled"; | ||||
|   } | ||||
|  | ||||
|   label_.set_markup( | ||||
|       fmt::format(format_, fmt::arg("status", status_), fmt::arg("icon", getIcon(0, status_)))); | ||||
|   label_.get_style_context()->add_class(status_); | ||||
|  | ||||
|   //if (tooltipEnabled()) { | ||||
|     //if (config_["tooltip-format"].isString()) { | ||||
|       //auto tooltip_format = config_["tooltip-format"].asString(); | ||||
|       ////auto tooltip_text = fmt::format(tooltip_format, localtime); | ||||
|       //label_.set_tooltip_text(tooltip_text); | ||||
|     //} else { | ||||
|       //label_.set_tooltip_text(text); | ||||
|     //} | ||||
|   //} | ||||
| } | ||||
| @@ -4,12 +4,8 @@ | ||||
| #include <fstream> | ||||
| #include <cassert> | ||||
| #include "util/format.hpp" | ||||
|  | ||||
| #include <unistd.h> | ||||
| //#include <stdlib.h> | ||||
| #include <cstring> | ||||
| #include "util/rfkill.hpp" | ||||
| #include <linux/rfkill.h> | ||||
| #include <fcntl.h> | ||||
|  | ||||
| namespace { | ||||
|  | ||||
| @@ -228,58 +224,15 @@ void waybar::modules::Network::worker() { | ||||
|  | ||||
| const std::string waybar::modules::Network::getNetworkState() const { | ||||
|   if (ifid_ == -1) { | ||||
| 		if (isDisabled(RFKILL_TYPE_WLAN)) | ||||
| 			return "disabled"; | ||||
| 		return "disconnected"; | ||||
| 	} | ||||
|     if (waybar::util::rfkill::isDisabled(RFKILL_TYPE_WLAN)) | ||||
|       return "disabled"; | ||||
|     return "disconnected"; | ||||
|   } | ||||
|   if (ipaddr_.empty()) return "linked"; | ||||
|   if (essid_.empty()) return "ethernet"; | ||||
|   return "wifi"; | ||||
| } | ||||
|  | ||||
| bool waybar::modules::Network::isDisabled(enum rfkill_type rfkill_type) const { | ||||
|   struct rfkill_event event; | ||||
|   ssize_t len; | ||||
|   int fd; | ||||
|   int ret; | ||||
|   ret = false; | ||||
|  | ||||
|   fd = open("/dev/rfkill", O_RDONLY); | ||||
|   if (fd < 0) { | ||||
|     perror("Can't open RFKILL control device"); | ||||
|     return false; | ||||
|   } | ||||
|  | ||||
|   if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { | ||||
|     perror("Can't set RFKILL control device to non-blocking"); | ||||
|     close(fd); | ||||
|     return false; | ||||
|   } | ||||
|  | ||||
|   while(true) { | ||||
|     len = read(fd, &event, sizeof(event)); | ||||
|     if (len < 0) { | ||||
|       if (errno == EAGAIN) | ||||
|         return 1; | ||||
|       perror("Reading of RFKILL events failed"); | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     if (len != RFKILL_EVENT_SIZE_V1) { | ||||
|       fprintf(stderr, "Wrong size of RFKILL event\n"); | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     if(event.type == rfkill_type) { | ||||
|       ret = event.soft || event.hard; | ||||
|       break; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   close(fd); | ||||
|   return ret; | ||||
| } | ||||
|  | ||||
| auto waybar::modules::Network::update() -> void { | ||||
|   std::lock_guard<std::mutex> lock(mutex_); | ||||
|   std::string                 tooltip_format; | ||||
|   | ||||
							
								
								
									
										49
									
								
								src/util/rfkill.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								src/util/rfkill.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,49 @@ | ||||
| #include "util/rfkill.hpp" | ||||
| #include <unistd.h> | ||||
| #include <stdlib.h> | ||||
| #include <cstring> | ||||
| #include <fcntl.h> | ||||
| #include <cerrno> | ||||
|  | ||||
| bool waybar::util::rfkill::isDisabled(enum rfkill_type rfkill_type) { | ||||
|   struct rfkill_event event; | ||||
|   ssize_t len; | ||||
|   int fd; | ||||
|   int ret; | ||||
|   ret = false; | ||||
|  | ||||
|   fd = open("/dev/rfkill", O_RDONLY); | ||||
|   if (fd < 0) { | ||||
|     //perror("Can't open RFKILL control device"); | ||||
|     return false; | ||||
|   } | ||||
|  | ||||
|   if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { | ||||
|     //perror("Can't set RFKILL control device to non-blocking"); | ||||
|     close(fd); | ||||
|     return false; | ||||
|   } | ||||
|  | ||||
|   while(true) { | ||||
|     len = read(fd, &event, sizeof(event)); | ||||
|     if (len < 0) { | ||||
|       if (errno == EAGAIN) | ||||
|         return 1; | ||||
|       //perror("Reading of RFKILL events failed"); | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     if (len != RFKILL_EVENT_SIZE_V1) { | ||||
|       //fprintf(stderr, "Wrong size of RFKILL event\n"); | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     if(event.type == rfkill_type) { | ||||
|       ret = event.soft || event.hard; | ||||
|       break; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   close(fd); | ||||
|   return ret; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Marc Radau
					Marc Radau