RSS Feed

Category Archives: linux

Switching display/monitor/screen in Linux

Posted on

Because I am using the Openbox (window manager), and I believe that the laptop Fn+F8 (or whatever combination with Fn) doesn’t work properly on Linux. Because the combination is detected as Super+p (aka Win+p). As a result, I wrote a Perl script to solve the switching display/monitor/screen issue on my laptop.

#!/usr/bin/perl

# This script requires xrandr, and several bash script created by arandr

use strict;
use warnings;

# Edit these global variables based on your setting
my $primary = 'eDP1';
my $secondary = 'HDMI1';

my %scripts = (default => 'default.sh',
               external_only => 'large_only.sh',
               clone => 'clone.sh',
               dual => 'dual.sh');
my $script_path = '~/.screenlayout';
# End edit

sub get_xrandr {
    return `xrandr`;
}

sub is_active {
    my ($monitor) = @_;
    for my $i (0 .. (scalar @$monitor - 1)) {
        my $line = $monitor->[$i];
        if ($line =~ /\*/) {
            return 1;
        }
    }
    return 0;
}

sub is_left {
    my ($monitor) = @_;
    my $line = $monitor->[0];
    if ($line =~ /\d+x\d+\+(\d+)\+\d+/) {
        if ($1 > 0) {
            return 0;
        }
    }
    return 1;
}

sub is_default {
    my ($primary, $secondary) = @_;
    return &is_active($primary) && !&is_active($secondary);
}

sub is_external_only {
    my ($primary, $secondary) = @_;
    return !&is_active($primary) && &is_active($secondary);
}

sub is_clone {
    my ($primary, $secondary) = @_;
    return &is_active($primary) && &is_active($secondary) &&
        &is_left($primary) && &is_left($secondary);;
}

sub is_dual {
    my ($primary, $secondary) = @_;
    return &is_active($primary) && &is_active($secondary) &&
        &is_left($primary) && !&is_left($secondary);;
}

sub get_monitor_style {
    my ($primary, $secondary) = &get_monitor_details;
    if (&is_default($primary, $secondary)) {
        return 'default';
    }
    elsif (&is_clone($primary, $secondary)) {
        return 'clone';
    }
    elsif (&is_dual($primary, $secondary)) {
        return 'dual';
    }
    elsif (&is_external_only($primary, $secondary)) {
        return 'external_only';
    }
    return 'unknown';
}

sub set_monitor_style {
    my ($style) = @_;
    my $script =  join('/', $script_path, $scripts{$style});
    my $cmd = "sh $script";
    `$cmd`;
}

sub switch_next_monitor_style {
    my $current_style = &get_monitor_style;
    if ($current_style eq 'default') {
        &set_monitor_style('external_only');
    }
    elsif ($current_style eq 'external_only') {
        &set_monitor_style('dual');
    }
    elsif ($current_style eq 'dual') {
        &set_monitor_style('clone');
    }
    elsif ($current_style eq 'clone') {
        &set_monitor_style('default');
    }
    else {
        print STDERR "Unknown monitor style";
    }
}

sub switch_prev_monitor_style {
    my $current_style = &get_monitor_style;
    if ($current_style eq 'default') {
        &set_monitor_style('clone');
    }
    elsif ($current_style eq 'external_only') {
        &set_monitor_style('default');
    }
    elsif ($current_style eq 'dual') {
        &set_monitor_style('external_only');
    }
    elsif ($current_style eq 'clone') {
        &set_monitor_style('dual');
    }
    else {
        print STDERR "Unknown monitor style";
    }
}

sub switch_monitor_style {
    my ($prev) = @_;
    if ($prev) {
        &switch_prev_monitor_style;
    }
    else {
        &switch_next_monitor_style;
    }
}

sub get_monitor_details {
    my $xrandr = &get_xrandr;
    my @lines = split(/\n/, $xrandr);

    my @primary_lines;
    my @secondary_lines;
    my $current_block;
    for my $i (0 .. $#lines) {
        my $line = $lines[$i];
        if ($i == 0) {
            next;  # not "continue"
        }
        if ($line =~ /^${primary}/) {
            $current_block = 'primary';
        }
        elsif ($line =~ /^${secondary}/) {
            $current_block = 'secondary';
        }
        if ($current_block eq 'primary') {
            push @primary_lines, $line;
        }
        elsif ($current_block eq 'secondary') {
            push @secondary_lines, $line;
        }
    }
    return (\@primary_lines, \@secondary_lines);
}

sub main {
    my ($prev) = @_;
    &switch_monitor_style($prev);
}

&main(@ARGV);

The script requires “xrandr” command. Furthermore, you need to have some actual switching monitor bash script, which can be created by using ARandR. Example of the script

#!/bin/sh
xrandr --output HDMI1 --primary --mode 1920x1080 --pos 0x0 --rotate normal --output VIRTUAL1 --off --output eDP1 --off

So, my Perl script will detect existing screen setup, whether it is laptop only (“default”), external only (“external_only”), laptop with external monitor at the right side (“dual”), or clone (“clone”) for both monitor sharing same screen. Therefore, we need to create four bash scripts using ARandR for these settings.

To invoke the script,

perl /path/to/monitor_switch.pl

This will switch to the screen to the “next” setting, in this order: default -> external_only -> dual -> clone -> default.

In order to switch between default and external_only, I extended the script with an argument.

perl /path/to/monitor_switch.pl prev

When passing with an argument (any argument), the monitor setup will switch in the reverse order: default -> clone -> dual -> external_only -> default. By this, we can switch between default and external_only easily.

Next, just apply the keybinding (aka hotkey or shortcut) to your preferred combination, then you can switch the screen with your favourite key combination.

Yeay!

P/S: The reason I wrote this script is, when I show my screen on external only, and the power is cut, the screen doesn’t switch to laptop automatically. That means, I cannot see anything to change my screen display. Before the script is written, I blindly use the Terminal, Ctrl+R, and type the keyword and press Enter to switch back. But this is extreemly impractical.

AAC audio file and ID3 tag


I just found that, if I have an AAC audio file (technically M4A), and if I added the ID3 tag 2 (aka ID3v2), then the audio file will failed to be converted by ffmpeg.

It can be either converted to mp3

  • using DeadBeef audio player, or
  • remove the ID3v2 tag then convert

So, how to add the metadata like ID3 tag? Use the Kid3 and add the Tag 3 (aka APE tag). This will not affect how ffmpeg to read the file.

Dell Vostro 5459 hibernation


In the previous post (1 year ago), I mentioned the hibernation issue. I believed that it was related to the NVidia graphic card. Related forum can be found here.

But these few days, I notice that whenever I shut down the laptop, it will show the systemd messages. Previously, if I did suspend my laptop, then resume, then shut down will show only black blank screen, until the power off. I believe that the graphic card issue is being fixed with the recent update.

I am now using linux-lts 4.9.13-1 and nvidia-dkms 378.13-2

Linux no sound after boot into Windows


Just now was trying to boot into Windows, by plugged in the HDMI cable, which supports for video only. But then, I rebooted into Linux without plugged out the HDMI cable. Then, it caused no sound in Linux.

Try 1: I killed the pulseaudio, then delete the ~/.config/pulse, and rebooted Linux. Failed.

Try 2: Run alsamixer, turned on everything, and do Try 1 again. Failed.

Then I guessed it was caused by the HDMI cable, which the Windows may assume there is audio, then caused my computer somehow ouptut the audio through the HDMI, even I rebooted into Linux.

Try 3: Plugged out the HDMI, booted into Windows. I checked the audio in Windows. It plays well. Then rebooted into Linux without plugged in HDMI. Yeah! It works. Passed.

Next level

Then I found that, though the sound works fine, my headphones doesn’t work. Tried out many methods as I asked in forum.

At the end, it is solved by shutdown, and boot again.

Openbox + tint2


Previously I was using Xfce4. Then, because of the heavy working environment, I tried the lighter desktop environment, LXDE. But still, it has some limitations that made me choose to use Openbox window manager only.

Pros and cons of Xfce4

Xfce4 is lightweight comparing to GNOME or KDE. I like it, because of the conventional design like the task manager. Furthermore another thing I like is the “aerosnap” feature like Windows, which I can view the two windows side by side. However, when running Windows in VirtualBox and other applications, I can feel the obvious slowness in the computer. It is really reducing my working performance. That is why I decided to change to LXDE.

Pros and cons of LXDE

LXDE is lighter than Xfce4. So, running a lot of heavy applications does not slow down the computer like Xfce4. But there was one issue I faced. The LXDE pager (workspace) does not allow me to drag and drop the applications to move among the workspaces.

As a result, I decided to use something lighter than Xfce4 and I can drag and drop the applications among the workspaces easily.

Openbox and tint2

I had experienced Openbox with tint2 when I was using my old laptop. Openbox is nice and highly customisable. Tint2 allows me to set number of workspaces, and easily to move the applications to other workspaces. However, tint2 does not have applets or plugins like Xfce4 or LXDE.

There is one feature I need, that is to see CPU usage, so that I know whether there is any application causes high CPU usage. As a result, I installed Conky and display the CPU usage at the corner of the desktop.

Openbox + tint2

Openbox + tint2

Conky

For the Conky, the following is the conky.conf

conky.config = {
    alignment = 'bottom_right',
    background = false,
    border_width = 0,
    cpu_avg_samples = 2,
	default_color = 'white',
    default_outline_color = 'BBBBBB',
    default_shade_color = '444444',
    draw_borders = false,
    draw_graph_borders = false,
    draw_outline = false,
    draw_shades = false,
    use_xft = true,
    font = 'DejaVu Sans Mono:size=1', --by size 1 then only there will have no space after cpugraph
    gap_x = 0,
    gap_y = 2,
    minimum_height = 5,
	minimum_width = 5,
    net_avg_samples = 2,
    no_buffers = true,
    out_to_console = false,
    out_to_stderr = false,
    extra_newline = false,
    own_window = true,
    own_window_class = 'Conky',
    own_window_type = 'desktop',
	own_window_transparent = true,
	own_window_argb_visual = true,
	own_window_argb_value = 255,
    stippled_borders = 0,
    update_interval = 1.0,
    uppercase = false,
    use_spacer = 'none',
    show_graph_scale = false,
    show_graph_range = false,
	double_buffer = true,
	imlib_cache_size = 10,
}

conky.text = [[
	${cpugraph 27,40 000000 FFFFFF -l}
]]

Windows – Linux Edition (opinion only)

Posted on

This is just an opinion, not the fact.

Not long ago, there is an announcement that Windows can run Bash on Ubuntu on Windows. Not only that, there is Windows subsystem for Linux.

Surely, Linux and UNIX have a lot of powerful features, especially developer oriented. That is why Windows has to adopt them. Because they really solves a lot of problems, such as bash and other commands like grep, find, and vi. Cloud computing is emerging, command-line is much more efficient than GUI. Text file for collaborative development is much more efficient (using git) than working on binary file, as you cannot diff and patch.

Since Windows now supports Linux subsystem, in my opinion, Windows may come out Windows – Linux Edition that

  • uses the UNIX filesystem hierarchy standard
  • supports commercial device drivers (like printers)
  • supports Linux container virtualisation
  • can run Windows applications natively (without WINE)
  • run X window customised with Windows theme
  • uses its own package manager with package repository
  • is cheaper price

I believe, there will be a group of users interested in it!

Dell Vostro 5459 review and setup


Due to the changes of the career, I invested on Dell Vostro 5459, after a long survey. I chose it because it is compatible to Linux. The compatibility to Linux meaning that, all the hardware devices are accessible by Linux. Besides that, it has the NVIDIA graphic card, which allows me to use GPGPU to do my research and study. Moreover, I can play high quality 3D graphics games.

Ubuntu

So, the laptop I bought was pre-installed with Ubuntu. And, I was impressed by the Ubuntu first boot video, which can be found here.

Partition and bootloader

It is new laptop, thus the hard disk partitioning table is using GPT format instead of MBR format. And the laptop uses UEFI boot system by default. It will be very convenient to have multiboot with several OSes.

In order to do partitioning, I used my favourite tool, SystemRescueCD. But I failed to run the X11 window, due to the very new NVIDIA graphic card. (I believe I can run the X11 now.)

Before partitioning and formatting the hard disk, I checked the xorg.conf from the existing Ubuntu, believed that it can help me to figure out how to start X11 window in SystemRescueCD. Then I discovered that /etc/X11/xorg.conf does not exist. This is a very important point.

Consequently, I used the command-line interface to re-partitioning the hard disk. Because I was going to install Windows and Arch Linux on it.

Windows

Though I prefer Linux, Ubuntu not my primary OS. So, I have to install the Arch Linux. Before installing Arch Linux, due to the luxurious hardware specification, I intended to install Windows 7. The laptop does not have USB2.0, but only USB3.0, and I didn’t want to install Windows 7 through external DVD-ROM, because I didn’t want to burn Windows 7 to a DVD. As a result, Windows 7 with USB3.0 cannot be installed. Looking for the solution, extra drivers are required. Thus, I gave up Windows 7 and tried Windows 8.1. (Sorry, Windows 8 and 10 are not my favourite.)

Great. Windows 8.1 is not bad after installation. I came back to Windows drivers later, since Windows is my secondary OS, for the purpose of… gaming.

Arch Linux

Installing Arch Linux needs some skills, and Internet connection is very important. So, I installed all the packages based on the old laptop, just following the powerful Arch Linux Wiki.

NVIDIA

NVIDIA GeForce 930M is very troublesome. After installing Arch Linux, to fully utilise my graphic card, I decided to use NVIDIA driver instead of Nouveau.

However, nvidia-xconfig generated xorg.conf file does not work. I failed to run the X11 window as in the SystemRescueCD.

As a result, I removed the NVIDIA related section in the xorg.conf according to the Arch Linux forum here. And the pre-installed Ubuntu does not have the xorg.conf as well. After removing the file, X11 window works fine.

After running the Arch Linux and configuring my preferences, I found that some of the screensavers (from xscreensaver) showed the error message,

Xlib: extension "GLX" missing on display ":0"

So, I knew that there is something wrong with my Xorg configuration and the NVIDIA.

Keep doing the trial and error, then I discovered something called bumblebee. Actually I am still very confused with this bumblebee, only know that it is related to NVIDA Optimus. And, I also don’t know what the hell is NVIDIA Optimus, only know that it is something wonderful.

After installing bumblebee, then the nvidia-libgl package is replaced by mesa-libgl. And the xscreensaver did not show the error message above anymore.

Since installed bumblebee, I supposed I can use Optimus with optirun command. Running

optirun glxgears -info

I got the new error,

libGL error: No matching fbConfigs or visuals found
libGL error: failed to load driver: swrast
...

Then, I thought may be my NVIDIA is too new, so I tried to install nvidia-beta. But it did not solve the problem as well.

As a result, I tried nvidia-dkms. Yeah! It works. “optirun” works fine now.

Windows 8.1

I love Dell, because the drivers are available online. I just download all the important drivers, graphic card, sound card, WiFi drivers, etc. Then I booted into Windows 8.1, and installed all the drivers. I just wonder, Arch Linux can use the WiFi device immediately during the installation, but Windows cannot? That means, if I have Windows and WiFi only, but does not have the driver, then how can I download the drivers?

After installed all the drivers, then I booted into Arch Linux, but failed. Because I failed to mount the Windows partition in Linux after installing the drivers. Then I found that, it is because of the Fast Startup feature in Windows 8. (Solution is here, look for the Fast Startup.) Because Fast Startup causes the partition “not clean”, so that Linux cannot mount it.

After disabling the Fast Startup, then everything works fine now.

Data transfer

Transferring vast amount of data between computers is very time consuming. I previously used an external hard disk. But transferring data from a 500G laptop to a 1T laptop, using an external hard disk is not applicable, since I don’t have extra empty hard disk.

At the end, I used the ethernet cable to transfer the data. (This is what I learnt from my student previously.) In Linux, I used the Network Manager to share the wired connection. Then directly connect two laptops with a single ethernet cable, and router is not required. Ethernet cable is faster than WiFi, and I can transfer whatever data I want from A to B or vice versa.

But still, I have to use the SSH to mount the target laptop.

This is a time consuming process.

Hibernation and resume issue

Now the only problem is resume from hibernation in Arch Linux. The resuming from hibernation works inconsistently. I have tried to install Linux LTS version, but it is worse because I cannot use Fn key after booting in Linux LTS.

I am still figuring out how to solve this problem.