You are not logged in.
Pages: 1
Topic closed
hi im new to c++ and to programming in general arch though ive been using for a few years, (finally decided i should bother to learn) anyway... To change the brightness on a laptop at the CLI you have to echo your chosen value to the brightness file through the graphics card symbolic link which is in sys/class/backlight my issue is that the symbolic link name changes with each graphics card so my question is how can i get the symbolic links name without using external tools like ls?
A task is only as large as you perceive it to be.
Offline
Hi castler and welcome to the Forums ![]()
I think you can use the function realpath along with dirent to retrieve the absolute path to which the symlink points to. Since you are new to programming i'll post a little example that maybe can be helpful:
#include <dirent.h>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
string getBacklightSymlink() {
string pname = "/sys/class/backlight";
DIR *parent = NULL; // parent directory to search in
struct dirent *child = NULL; // child file or directory
DIR *linkdir = NULL; // symlink directory
struct dirent *linkchild = NULL; // symlink child
string backlight = string();
char bl_found = false;
// try to open the parent directory ...
if ((parent = opendir(pname.c_str())) != NULL)
{
// ... and search for the backlight symlink.
while (((child = readdir(parent)) != NULL) && !bl_found)
{
// converting the directory name to an std string
string child_name = string(child->d_name);
// if the child is a symlink ...
if (child->d_type == DT_LNK)
{
// ... then get its full path ...
string symlink_path = pname+"/"+child_name;
// ... and the directory to wich it points to
char *abs_link_path = realpath(symlink_path.c_str(), NULL);
// If child contains a file named 'brightness' ...
if ((linkdir = opendir(abs_link_path)) != NULL)
{
while ((linkchild = readdir(linkdir)) != NULL)
{
if (linkchild->d_name == "brightness");
{
// then we found the backlight!
backlight = string(abs_link_path);
bl_found = true;
break;
}
}
closedir(linkdir);
}
}
}
closedir (parent);
}
else
{
/* ... this should not happen in a linux environment
* since /sys/class/backlight should always exists, but
* if it fails then we ask poletely to the user if he is
* actually running linux ...
*/
cerr << "Aren't you using linux?" << std::endl;
}
return backlight;
}
int main(void) {
cout << getBacklightSymlink() << endl;
}Offline
Thanks mauritiusdadd! I appreciate the help I shall study this in great detail over the next day and report back how I get on ![]()
A task is only as large as you perceive it to be.
Offline
hi castler looks like its taking you way too long to report it back
haha.
Last edited by sketch.exe (Yesterday 15:36:08)
Offline
Moderator Note
Closing this old thread.
Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.
clean chroot building not flexible enough ?
Try clean chroot manager by graysky
Offline
Pages: 1
Topic closed