Prayer Clock GTK3


My first open source project, Prayer Clock, I moved from SourceForge to GitHub recently. Yeah! Everyone should git!!!

And today I just made some changes, and updated to GTK3.

With GTK 3, I removed the title bar. But not yet successfully moving the menu bar to the icon like Evince or Nautilus.

I plan to convert the right hand panel to WebKitGtk. But this will not be the priority yet.

Switching display/monitor/screen in Linux


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)


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!