Workaround to no backlight

Since i have this problem of the back light not working in Linux and NOTHING worked, i decided to share my workaround to this problem. I hope no one needs to do the same but this may help someone.

My issues were the brightness and night mode, two important things to me.
What i did was using python scripts and 2 text files. Here are the steps i did:

Create this files “brightness-up, brightness-down, load-brightness, night-mode, current-brightness.txt, night-mode-switch.txt”, python files don’t need extensions because Linux can read them at the beggining of the file.

NOTE: when creating current-brightness.txt write “1.0” inside it and write “false” in night-mode-switch.txt

Copy and paste to the following:
brightness-up:

#!/bin/python

import os
brightnessPath = "/home/lighttigerxiv/Scripts/current-brightness.txt" #replace this lines with current-brightness.txt location
nightModePath = "/home/lighttigerxiv/Scripts/night-mode-switch.txt" #replace this lines with night-mode-switch.txt location
display = "DP-4" #Change this to your  according display, you can find it with xrandr command

#Opens the brightness text file:
fileCurrentBrightness = open( brightnessPath, "r" )
fileNightModeSwitch = open( nightModePath, "r" )

#Reads the brigtness:
currentBrightness = fileCurrentBrightness.readline()
nightModeSwitch = fileNightModeSwitch.readline()

#Changes the brightness value:
brightnessFloat = float(currentBrightness)
fileCurrentBrightness.close()
fileNightModeSwitch.close()

if brightnessFloat >= 1:
    pass

else:

    brightnessFloat = round(brightnessFloat + 0.1, 2) 
    fileCurrentBrightness = open(brightnessPath, "w")

    new_brightness = str(brightnessFloat)
    fileCurrentBrightness.write(new_brightness)

    #Night mode
    if nightModeSwitch == "true":

        bashCommand = "xrandr --output " + display + " --brightness " + new_brightness + " --gamma 1:1:0.8"

    #Normal mode
    else:

        bashCommand = "xrandr --output " + display + " --brightness " + new_brightness + " --gamma 1:1:1"
    

    os.system(bashCommand)

brightness-down:

#!/bin/python


import os
brightnessPath = "/home/lighttigerxiv/Scripts/current-brightness.txt" #replace this lines with current-brightness.txt location
nightModePath = "/home/lighttigerxiv/Scripts/night-mode-switch.txt" #replace this lines with night-mode-switch.txt location
display = "DP-4" #Change this to your  according display, you can find it with xrandr command


#Opens the brightness text file:
fileCurrentBrightness = open(brightnessPath, "r")
fileNightModeSwitch = open( nightModePath, "r" )

#Reads the brigtness:
currentBrightness = fileCurrentBrightness.readline()
nightModeSwitch = fileNightModeSwitch.readline()

#Changes the brightness value:
brightnessFloat = float(currentBrightness)

fileCurrentBrightness.close()
fileNightModeSwitch.close()

if brightnessFloat <= 0.4:
    pass

else:

    brightnessFloat = round(brightnessFloat - 0.1, 2) 
    fileCurrentBrightness = open(brightnessPath, "w")

    new_brightness = str(brightnessFloat)
    fileCurrentBrightness.write(new_brightness)


    if nightModeSwitch == "true":

        bashCommand = "xrandr --output " + display + " --brightness " + new_brightness + " --gamma 1:1:0.8"

    else:

        bashCommand = "xrandr --output " + display + " --brightness " + new_brightness + " --gamma 1:1:1"
    

    os.system(bashCommand)

load-brightness:

#!/bin/python

import os
import time

brightnessPath = "/home/lighttigerxiv/Scripts/current-brightness.txt" #replace this lines with current-brightness.txt location
nightModePath = "/home/lighttigerxiv/Scripts/night-mode-switch.txt" #replace this lines with night-mode-switch.txt location
display = "DP-4" #Change this to your  according display, you can find it with xrandr command

#Opens the brightness text file:
fileCurrentBrightness = open( brightnessPath, "r" )
fileNightModeSwitch = open( nightModePath, "r" )

#Reads the brigtness:
currentBrightness = fileCurrentBrightness.readline()
nightModeSwitch = fileNightModeSwitch.readline()

#Changes the brightness value:
fileCurrentBrightness.close()
fileNightModeSwitch.close()

time.sleep(1) #Change this timer to more, like 2 3 4,  if your pc is slow at starting up

if nightModeSwitch == "true":

    bashCommand = "xrandr --output " + display + " --brightness " + currentBrightness + " --gamma 1:1:0.8" 

else:

    bashCommand = "xrandr --output " + display + " --brightness " + currentBrightness + " --gamma 1:1:1"

os.system(bashCommand)

night-mode:

#!/bin/python

import os
brightnessPath = "/home/lighttigerxiv/Scripts/current-brightness.txt"
nightModePath = "/home/lighttigerxiv/Scripts/night-mode-switch.txt"
display = "DP-4"

fileNightModeSwitch = open( nightModePath, "r" )
fileCurrentBrightness = open( brightnessPath, "r" )

nightModeSwitch = fileNightModeSwitch.readline()
currentBrightness = fileCurrentBrightness.readline()

fileNightModeSwitch.close()
fileCurrentBrightness.close()

if nightModeSwitch == "false":
    
    command = "xrandr --output " + display + " --brightness " + currentBrightness + " --gamma 1:1:0.8"
    os.system(command)
    
    fileNightModeSwitch = open( nightModePath, "w" )
    fileNightModeSwitch.write("true")
    fileNightModeSwitch.close()

    
else:

    command = "xrandr --output " + display + " --brightness " + currentBrightness + " --gamma 1:1:1"
    os.system(command)

    fileNightModeSwitch = open( nightModePath, "w" )
    fileNightModeSwitch.write("false")
    fileNightModeSwitch.close()

    

Now that every file is properly created, you need to change the permissions of the files in order to run.
Open the terminal and go to folder where you created all this files. Now do:

chmod +x brightness-down brightness-up load-brightness night-mode

Next step is moving them to /usr/local/bin/

sudo mv brightness-down brightness-up load-brightness night-mode /usr/local/bin/

Now that you have everything the only two things left to do. The next one is to keybind those scripts. Since i am in kde i just need to go to custom shortcuts and add them (i recommend using the normal keybindings with your pc):

With brightness up you just need to run “brightness-up” (since the scripts are /usr/local/bin/ you don’t need to add a path). With brightness down, the same philosophy “brightness-down” and with night mode, “night-mode” (i am using super-n) to activate it.

The last step to do is to add load-brightness to the autostart. In kde just write autostart and it should show an option to add a login script. Select the “load-brightness” in /usr/local/bin/ and you should be good to go :smiley:

1 Like

If anyone found a bug or a way to optimize i will appreciate it if you tell me

Don’t use Python :wink:

Fair enough but i can’t stand bash script XD

I was like that some 2-3 years ago.

Whenever I needed to automate anything in the terminal, I would just code it in C++.

Then one day, I decided to learn Bash. It’s actually pretty easy. Do not resist it, eventually you’re going to need it. Yes, it’s ugly…

2 Likes

What laptop make & model is this just out of curiosity?

                     ./o.                  
                   ./sssso-                ---------------- 
                 `:osssssss+-              OS: EndeavourOS Linux x86_64 
               `:+sssssssssso/.            Host: 82JU Legion 5 15ACH6H 
             `-/ossssssssssssso/.          Kernel: 5.14.11-arch1-1 
           `-/+sssssssssssssssso+:`        Uptime: 27 mins 
         `-:/+sssssssssssssssssso+/.       Packages: 1455 (pacman), 6 (flatpak) 
       `.://osssssssssssssssssssso++-      Shell: bash 5.1.8 
      .://+ssssssssssssssssssssssso++:     Resolution: 1920x1080 
    .:///ossssssssssssssssssssssssso++:    DE: Plasma 5.22.5 
  `:////ssssssssssssssssssssssssssso+++.   WM: KWin 
`-////+ssssssssssssssssssssssssssso++++-   WM Theme: Chameleon (Dark) 
 `..-+oosssssssssssssssssssssssso+++++/`   Theme: Breeze Dark [Plasma], Windows-10-Dark-3.2-dark [GTK2/3] 
   ./++++++++++++++++++++++++++++++/:.     Icons: Win11-dark [Plasma], Win11-dark [GTK2/3] 
  `:::::::::::::::::::::::::------``       Terminal: konsole 
                                           CPU: AMD Ryzen 5 5600H with Radeon Graphics (12) @ 3.300GHz 
                                           GPU: NVIDIA GeForce RTX 3060 Mobile / Max-Q 
                                           Memory: 2210MiB / 7802MiB 

                                                                   
                                                                   

I understand but i just can’t stand the spaces stuff. If i do an if with no space or with more it doesn’t compile lol. For initializing variables the same behaviour xD

Well, it doesn’t compile anyway, it’s a shell script but I know what you mean, it’s annoying. Any language in which invisible characters can create a bug is awful (Python is actually worse than shell in that regard, in my opinion).

However, think of it this way: in the interactive shell session, space is used to delimit the arguments of the command. So, whenever you write a space, that’s a different argument. It makes sense in an interactive context, but not in a programming context. So just think of it as an interactive shell session. Once you get used to it, it’s not a big deal.

1 Like

On that regard it makes sense.