Configuring input languages in KDE

If you need Russian language, for example, in KDE you go to System Settings->Input Devices->Keyboard, press Add button and select it:

KDE switch language

(more…)

How to make Shift+NumPad keys select text and Ctrl+Ins/Shfit+Ins copy/paste in KDE (like as in MS Windows)

I worked a long time with MS Windows before, and when I started to work with KDE, the most annoying thing was that in KDE’s default configuration I was unable to use Shift+NumPad arrows/End/Home to select the text in a text edtor (QT Creator, for example) and copy/paste with Ctrl+Ins/Shift+Ins. This way of copying and pasting is the favorite for me, because Ctrl and Shift are big buttons located at the left side of the keyboard and Ins is one of the biggest right button, so they can be quickly used by both hands. Fortunately there is built-in setting in KDE that enables all my shortcuts:

KDE Settings

Running PHP 5.3/5.6 in a Docker container with Nginx on Ubuntu 16.04

The docker installation procedure depends on the system architecture that can be determined with the following commands:

cat /proc/cpuinfo
dpkg --print-architecture

After I installed Docker, I successfully run its test application:

docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b04784fba78d: Pull complete
Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:

docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/

(more…)

Using “apitrace” with an OpenGL application

The basic commands for using apitrace with an OpenGL application are:

apitrace trace --api=gl MyApp
qapitrace MyApp.trace
apitrace dump --blobs MyApp.trace 

The last command generates blobs that can be inspected with the following command, for example:

tail -c $((12*10)) ~/repos/MyApp/build/blob_call2325.bin | xxd -g1

Or by writing a simple C++ program that converts them into CVS format:

(more…)

Securing Nginx with Let’s Encrypt on Ubuntu 16.04

First we need to install certbot utility:

apt-get install software-properties-common
add-apt-repository ppa:certbot/certbot
apt-get update
apt-get install python-certbot-nginx

After that, we can easily generate SSL certificates for all the domains listed in Nginx ‘server_name’ attributes in alive (working) virtual hosts. The examples are:

certbot --nginx certonly -d slogpost.ru -d www.slogpost.ru
certbot --nginx certonly -d psiholog-s-vami.ru -d www.psiholog-s-vami.ru
certbot certonly --nginx --cert-name sharlines.com -d sharlines.com -d www.sharlines.com
certbot certonly --nginx --cert-name developernote.com -d developernote.com -d www.developernote.com -d herb.developernote.com -d mastermag.developernote.com -d geographx.developernote.com -d geographx.net -d www.geographx.net -d xn--80acc2atiigge7h.xn--p1ai -d www.xn--80acc2atiigge7h.xn--p1ai -d git.developernote.com -d gitweb.developernote.com

(do not forget to run the commands above each time you add or remove a subdomain)

We cannot use wildcard domains line *.developernote.com with Let’s Encrypt, so we should list all the subdomains. And I do not see anything wrong in combining multiple domains in a single certificate.

To remove the certificate we do something like this:

certbot revoke --cert-path /etc/letsencrypt/live/developernote.com/fullchain.pem
certbot delete --cert-name developernote.com

Updating all the generated certificates:

certbot renew

After changing the website URL from HTTP to HTTPS, probably it makes a sense to update all the hyperlinks in MySQL database:

show tables;
show columns from wp_posts;
SELECT ID, post_title, post_date, post_name FROM wp_posts WHERE INSTR(post_content, 'http://slogpost.ru') <> 0;
UPDATE wp_posts SET post_content=REPLACE(post_content, 'http://slogpost.ru', 'https://slogpost.ru') WHERE INSTR(post_content, 'http://slogpost.ru') <> 0;
UPDATE wp_posts SET post_content=REPLACE(post_content, 'http://developernote.com', 'https://developernote.com') WHERE INSTR(post_content, 'http://developernote.com') <> 0;

The final step is adding certbot-renew.sh file to /etc/cron.monthly with the following content:

certbot renew
service squid reload

It seems like the service … command is completely ignored. Nothing in syslog, nothing in nginx logs. I switched to using

certbot renew
systemctl reload squid

instead, and this seems to work.

Comparison of std::mutex and std::atomic performance

The following C++ code compares the performance of std::atomic and std::mutex:

#include <atomic>
#include <mutex>
#include <iostream>
#include <chrono>
#include <thread>

const size_t size = 100000000;
std::mutex mutex;
bool var = false;

typedef std::chrono::high_resolution_clock Clock;

void testA()
{
    std::atomic<bool> sync(true);
    const auto start_time = Clock::now();
    for (size_t counter = 0; counter < size; counter++)
    {
        var = sync.load();
        //sync.store(true);
        //sync.exchange(true);
    }
    const auto end_time = Clock::now();
    std::cout << 1e-6*std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count() << " s\n";
}

(more…)

Backing up and restoring all the PHP MySQL websites on a Linux server

All the following commands assumes we saved MySQL root password into MROOTPASS variable:

export MROOTPASS=<mysql root password>

The most straight forward method to backup all the MySQL databases and all the website files (PHP scripts, images, etc..) stored in the /home directory is the following:

mysqldump --all-databases -u root -p$MROOTPASS | gzip > all-databases-$(date '+%Y-%m-%d_%H-%M-%S').sql.gz
tar -cvzf home.tar.gz /home

If we backup some individual database (probably not as root) and change its user while restoring it, it might make a sense to remove DEFINER from the output script:

sed -e 's/*]*\*/\*/'

The following commands restore all the websites from the archives:

gunzip -c all-databases-2017-05-23_15-31-00.sql.gz | mysql -u root -p$MROOTPASS
cd /
sudo tar -xvzf home.tar.gz

After migration from MySQL version  14.14 Distrib 5.5.54 to 14.14 Distrib 5.7.18 (I do not know what is the difference between them) I got the following error: “ERROR 1805 (HY000) at line 1: Column count of mysql.user is wrong. Expected 45, found 42. The table is probably corrupted” while trying to drop some user, and fixed it by running:

mysql_upgrade -u root -p$MROOTPASS
service mysql restart

How to rename multiple files in Linux

The following bash script renames all the files matching src_name.* with dst_name.*:

src_name=$1
dst_name=$2
unset $1
cp $src_name.* ~/temp/
cd ~/temp/
rename "s/$src_name\.([a-z]+)/$dst_name\.\$1/" *
cd -
mv ~/temp/$dst_name.* .

If you have some files like src1.h and src1.cpp, you can save this script into dupsource.sh file, for example, and then rename those files with src2.h and src2.cpp using the following command:

dupsource.sh src1 src2

The script requires ~/temp directory to exist and be writable.

What packages QAudioDecoder may require on Ubuntu and CentOS?

If QAudioDecoder does not decode mp3, reporting a format error (GStreamer; Unable to start decoding process), the following package can help:
On Ubuntu:

apt-get install gstreamer0.10-fluendo-mp3

On CentOS:

yum -y install http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
yum install gstreamer{,1}-plugins-ugly

(more…)

How I learned OpenGL

To learn OpenGL I wrote a simple game that utilizes the most of basic OpenGL ES 2.0 (and partially 3.0) techniques:

  • MVP matrix. In 3D version of the game the projection matrix is calculated depending on the window size (viewport).
  • All the objects including the board, balls, path arrows, sparkles, explosion particles sit in vertex buffers and are rendered with vertex/fragment shaders.
  • The light is implemented with common fragment shader calculating ambient, specular and diffuse coefficients. The sparkle position is found by iterating over the normals and searching the maximum light coefficient.
  • The antialiasing is achieved by rendering the scene first to 4 times bigger offscreen buffer and then rendering the buffer to the screen with multisampling.
  • Color coding is used to determine what object the user has clicked on. The board and the balls are rendered first with alpha containing the object identifier (that is why the board size is limited with 15×15), then alpha is erased and the background is rendered over it with a specific blending operation.
  • Textures are used for rendering the background images, sparkles and particles.
  • All the rendering is done on a separate thread and the swaps (flips) are synchronized with the monitor refresh cycles. This prevents screen tearing and saves the battery power.
(more…)