Thursday, 15 September 2016

Right way to set env variable while exec or execFile in nodejs

According to the official documentation, exec allows you to pass additional environment variables as part of options like this:

This looks fine right? except that it totally isn't! By passing "env" as an option, you are not adding on to existing environment variables, but you are replacing it.  This is not clear from the documentation and can leave you scratching your head for a while as it can seem to break the command for no particular reason at all! So, you need to essentially make a copy of process.env and modify it like follows.


Thanks for stopping by!

Thursday, 28 April 2016

Problem with clipboard on Ubuntu

Clipboard on Ubuntu is "broken". Well, maybe not. But copy-paste is broken in a lot of applications on Ubuntu. Let me give you an example,

  • Open LibreOffice.
  • Write something and copy it.
  • Paste it somewhere else. It works as expected.
  • Now close LibreOffice and try pasting. It won't work.
This is a well known bug - Copy-Paste doesn't work if the source is closed before the paste; affecting a lot of applications on Ubuntu. And they are not so keen on fixing it, atleast not any time soon.
The reason for this is that these applications do not comply with the clipboard specification from FreeDesktop
If a client needs to exit while owning the CLIPBOARD selection, it should request the clipboard manager to take over the ownership of the clipboard, using the SAVE_TARGETS mechanism. If there is no clipboard manager, or if the SAVE_TARGETS conversion fails, the application should simply exit.
Applications need to transfer ownership of the clipboard to clipboard manager before exiting for copied data to perisist after it exits. There is certainly nothing you can do about it, unless offcourse you are willing to modify the source code of each of these applications to make it comply to FreeDesktop specs.

Fix. Well.. workaround.

The reporter/ moderator of the bug report suggests that we should install diodon, klipper, glipper, parcellite or xfce4-clipman as a workaround for this issue.
Working of diodon


Tuesday, 26 April 2016

Ubuntu 16.04 won't wake up from suspend

I recently installed Ubuntu 16.04 LTS Xenial Xerus on my Thinkpad E550. I honestly regretted it not just because it doesn't support AMD proprietary fglrx driver aka AMD Catalyst or AMD Radeon Software but because the suspend feature stopped working.

I initially thought that this had something to do with video drivers that I had installed on Ubuntu 15.10 which were now incompatible with 16.04. I realized that this was not the case as the issue persisted even on opensource drivers that it is compatible with.

Also on a closer observation I realized that it was not that my system was not able to wake up from suspend, but that it was not able to suspend at all. On suspending, the screen would go off but my laptop kept running, heating up and draining battery. This problem existed when I hibernate or shutdown as well.

Now the only possible reason for this is some problem in acpi which is not letting my system to suspend. Ubuntu 16.04 is shipped with kernel 4.4. A quick search on this issue on kernel 4.4 made me realize that this exists across several destros and mostly on thinkpads. So I upgraded to kernel 4.5 and the problem is resolved.

Installing kernel 4.5

32 bit

64bit

Then reboot!

Friday, 8 January 2016

Parsing wav file in node.js

I have worked with wav audio data in python before. Scipy provides a very nice way to do this using scipy.io.wavfile


I wanted to do exactly the same in node.js. There is a module called wav which sort of does it. However I faced several problems.

1. The Reader() method reads the file stream and converts into chunks of Buffer. This is very good while building web apps as you can send chunks of data separately and combine it later. However, I was just writing a script that'd run offline, So I had to put all the buffers to an array and then use the concat method of Buffer.


2. The wav module doesn't do much processing and just throws the raw binary information at us. So, we have to take care of

  • Combining hex data to get amplitudes - One frame can be represented using several blocks of 8-bit hex. The number of blocks per frame is got using blockAlign parameter in the format.
  • Endianness - Data may or may not be  in little endian format. So, while reading the blocks of hex data, we have to take care of this.
  • Handling negative amplitudes - Frames spanning several blocks when negative can be little challenging to handle as they are just stored as their two's complement.
  • Separating channels - The raw binary contains data of all the channels together. Fortunately, they are mentioned one after the other. Using channels parameter in the format, channels can be separated easily.
So, let's see how I handled all the above cases. First, we need to capture the "format" of the audio file that contains information about the file.


Then, on "end" event i.e after all the chunks have been combined, we will handle all the cases mentioned above as follows


Concluding, in this article I showed you how to handle and parse wav files in node.js by resolving several problems such as merging chunks of Buffer, combining hex data to get amplitudes, endianness, handling negative amplitudes and separating channels.

Wednesday, 23 December 2015

Unfortunately the process com.android.phone has stopped in CM 13

I have a good old nexus 4 still trying to keep up with the big boys in the market. After google ditched the plans of releasing the latest android 6.0 - Marshmallow for nexus 4, things didn't look so great. But with the release of CM 13 (mako), even nexus 4 has android 6.0! Previously, I was running CM 12.1 and I dirty flashed CM 13 on top of it. Although it doesn't have all the features that CM 12 had, with time, the awesome guys in XDA and cyanogen will port all the features to CM 13.

After dirty flashing CM 13 on top of CM 12.1, I faced some problems. First of all, things were kinda slow. So I went to Developer options in Settings and set all the Animation scaling to .5x. This considerable made things faster. Secondly and more importantly, my sim card was not getting detected and I was getting a ton loads of popups saying "Unfortunately the process com.android.phone has stopped".


I looked at the logcat to see what is going wrong and I found that this was appearing a lot of times.


Clearly, the problem was with com.android.providers.telephony. Upon looking closely, the problem was related to CursorWindow and Cursor. Hence, the problem was not with CM 13 but with the old database left behind by CM 12.1. 

Solution


Just delete the folders /data/data/com.android.providers.telephony and /data/data/com.android.phone using some tool like Root Explorer or you can do this in adb shell.

rm -rf /data/data/com.android.providers.telephony
rm -rf /data/data/com.android.phone

Then, restart the phone.

Tuesday, 27 October 2015

USB debugging toggle widget

Like I told you few days back in this post, I made a widget for android to toggle USB Debugging - ADB (Android Debug Bridge). Today, I will tell you more about it.



There are many alternatives to this. However they either require you to make the app a system app or they simply open the settings page. As this was not exactly what I wanted, I made one myself that allows me to enable/ disable USB debugging on click. You can get the app from here.

Download ADB Toggle

Fork me on Github


You DON'T have to make this a system app!.


Sunday, 25 October 2015

Using WRITE_SECURE_SETTINGS permission on non system apps

I happen to frequently use an app that doesn't work if USB Debugging - ADB (Android Debug Bridge) is enabled. It is very cumbersome to go to settings and enable or disable the setting every time I need to use the app.
I checked if there are any widgets that could do this in a click and returned empty handed. So, I did what every developer would do - build it myself!

In order to change the adb settings, I had to use WRITE_SECURE_SETTINGS permission. But there was one major problem! This permission is not available for non system apps!!

The easiest solution to this was to move the apk to /system partition. Except that I didn't want to do this as I frequently keep flashing /system partition.

Another solution was to use pm and grant permission to the app like this:

adb pm grant <package name> android.permission.WRITE_SECURE_SETTINGS

This was great! However I didn't want users who were gonna use the app to go through all this pain. I had to figure out a way to do the same programmatically. So I did the next best thing! To run the command after opening a root shell in the code.

Here is how I did it.
This requires root. But that's ok.

Tuesday, 8 September 2015

The Infamous battery re-calibration bug in Lenovo Thinkpad

My brand new Lenovo Thinkpad is a beast and is supposed to give up to 8hrs of battery backup. But there was a weird problem. As soon as the battery remaining reached 30%, it used to drop to 6% in a second. When I looked up on google, I found out that I was not the only one facing this problem. Most of them suggested that my battery was shot and that I had to buy a new one. This is highly improbable as it is brand new. I wanted to give fixing it a try before contacting customer care. And I was successful!

Before - See the battery percentage drop from 30% to 6%

For some reason, I suspected that it was somehow TLP's fault. TLP is the best power management utility for Thinkpad in Linux. It allows you to set max charging threshold so that you will be able to connect your laptop to the plug point without worrying about over charging (among many other features it provides). I felt that it is because of this, battery calibration was getting screwed up. So I set the maximum threshold to 100%.

Open /etc/default/tlp and set following variables.


START_CHARGE_THRESH_BAT0=100
STOP_CHARGE_THRESH_BAT0=100


Then I restarted my laptop to bring this to effect. As the calibration was already offset by a great extent, the only way to fix it was to keep it charged for a long time. I kept it charging for almost a day.


After - Fixed!
 Then to test it I kept discharging it. And to my surprise, it didn't drop from 30% to 6% this time!!
So the problem was with calibration and not because the battery is broken. If you face this problem, you can try this solution once before you spend $$$ on buying a new battery.

Now that I have changed the battery threshold back to 85%, if this problem occurs again, all I have to do is follow the above steps.

Sunday, 6 September 2015

Download only part of a repository in github

Sometimes you would want to download only a part of a repository in Github but you don't want to download / clone the entire repository, specially when the repo is huge or has too many other things that you are not interested in. There are many ways to do this. For example you can do a shallow clone, or you can use Github API (there is a limit on number of requests per hour)..etc. However, none of them are simple and straightforward. 

I recently found out that Github supports svn to some extent. So I tried the age old svn export to download a part of the project I was interested in. And it worked.

svn export https://github.com/<username>/<project name>/trunk/<folder path>

For example:


If you want to download only docs from Bootstrap repository,

svn export https://github.com/twbs/bootstrap/trunk/docs

Now, suppose you want to download it with a different name,

svn export https://github.com/twbs/bootstrap/trunk/docs bootstrap-docs


Monday, 3 August 2015

Material Theme for Google Blogger

Inspired by Google's Material Design principles, this generates Material themes for Google Blogger using sass preprocessor.

Fork me on Github


Here are some of the themes generated with material-blogger

Get themes


Honor Theme


Aqua Theme

Pink Ribbon Theme


Sample Widgets

Button

Search Results
Popular Widget
Widget (Aqua Theme)
Archive


To see how to use it, see this