Data loss bug: WordPad kills utf8 .txts

Windows 7 here.

So I’ve been writing my notes in a bunch of .txt files, and editing those in KWrite and Notepad++. I thought I’d try WordPad as a SDI text editor, since KWrite and Notepad both have issues and Notepad++ is MDI. I opened one of my utf8-encoded .txt files in WordPad and saved it. Then I reopened it and all the Cyrillic characters in my file had turned to question marks. Damn!

I hoped this was at least a reversible change, so I looked at the file with a hex editor to see if any cyrillic data remained, however encoded. But nope, all the Cyrillic had turned into #3F bytes.

So, be warned.


Creating a console programatically in Win32 C++


  • You have a win32 C++ app that doesn’t use the CONSOLE subsystem, meaning it doesn’t automatically create a console. Note that using some specific libraries (such as libcinder I think) pervents you from using the CONSOLE subsystem, even if you want to do so.
  • You want a console though – for example you want to debug your app outside the IDE debugger for more speed, so you want to do debug prints
  • You want your existing code that prints to std::cout to continue working.

Here’s the solution:

#include <fstream>
#include <iostream>

void createConsole()
    // make a console window show up
    // "CONOUT$" is a special filename in Windows
    std::fstream* fs = new std::fstream("CONOUT$");
    // redirect cout to that filestream
    // no need to ever deallocate fs

You can call this somewhere in main(), for example.


  • for AllocConsole you need to include the “windows.h” header, I think

I’m using this on Windows 7 with no problems.

HTML5 Canvas performance: sort drawImage() calls by image

I’d like to share a tip I figured out while working on a HTML5 canvas game.

When you call drawImage lots of times, it can be pretty slow. A fix is to order your draw calls by the image you’re drawing. In other words, if you have the image objects A, B and C, don’t draw in order “A, B, A, C, B”, draw in order “A, A, B, B, C”. That’s it. Drawing becomes much faster. At least it does in my Intel XDK-deployed app on my Android phone.

If you have requirements about which images should cover which when overlapping, this technique might not be fully applicable.

Tips and tricks for installing KDE for Windows

Installing KDE for Windows is pretty easy, but has a few catches. I’m listing those here.

  • by default it’s installed to C:\ProgramData\KDE. If you, like me, want to install it to C:\Program Files\KDE, make sure to run the installer as Administrator.
  • If the first time you didn’t run it as Administrator, you probably got error messages when it started copying the files, and you cancelled it then. Beware that when you retry installing, it’ll pick up where it left off last time, meaning files it left uncopied last time due to errors, will remain uncopied. To avoid that, delete the install dir of KDE before retrying the install.
  • I installed KDE just for KWrite, and KWrite uses the “KDE default” encoding by default, which means that opening a utf-8 file in it doesn’t work out of the box. The fix is to go “KWrite->Settings->Configure Editor->Open/Save” and set Encoding to “Unicode (UTF-8)”.

Connection sharing on Ubuntu just won’t work? Stop NetworkManager.

For days I tried to get a windows laptop to connect to the internet through my linux router (which I wanted to use as an svn server, running Ubuntu Jaunty). But I just couldn’t get ping to work between the two machines.

KB1JWQ from ##linux @ Freenode pointed me to the solution: disable the NetworkManager service. Apparently it’s supposed to make your life easier by automating some networking tasks, but in doing so it tends to trample on your manual settings, so if you know what you’re doing (or you’re following a tutorial like me) you might want to disable it. In my case, this allowed the two machines to ping each other. From there, masquerading (connection sharing) worked with no further hassles.

Note: disabling the NetworkManager service is done via `sudo service NetworkManager stop`. And removing it permanently so it won’t start on reboot is done via `sudo aptitude remove network-manager`.

How to embed mplayer into your SDL/X window (C++)

Here’s a class that does all the dirty work for you!
note: it’s based on a blogpost by Gustavo Barbieri but cleaned up.


#include "video.h"
#include <sstream>
#include <SDL/SDL_syswm.h>
namespace {
	class WindowInfo {
		WindowInfo() {
		~WindowInfo() {;
		Display* dpy() const { return; }
		Window window() const { return; }
		SDL_SysWMinfo info;
	Window createChildWindow(SDL_Rect const& r) {
		WindowInfo info;
		int black = BlackPixel(info.dpy(), DefaultScreen(info.dpy()));
		Window win = XCreateSimpleWindow(info.dpy(), info.window(), r.x, r.y,
			r.w, r.h, 0, black, black);
		XMapWindow(info.dpy(), win);
		XSelectInput(info.dpy(), win, KeyPressMask | ButtonPressMask);
		return win;
	FILE* embedMplayer(std::string filename, Window destWin) {
		std::stringstream cmdline;
		cmdline << "mplayer -wid " << destWin << " -vo x11 -loop 0 -slave "
			<< filename << " > /dev/null 2>&1;";
		return popen(cmdline.str().c_str(), "w");
} // namespace

Video::Video(std::string const& filename, SDL_Rect const& videoRect)
	: m_filename(filename), m_videoRect(videoRect)
	m_isPlaying = false;

Video::~Video() {

void Video::play() {
	m_childWindow = createChildWindow(m_videoRect);
	m_pipe = embedMplayer(m_filename, m_childWindow);
	m_isPlaying = true;

void Video::stop() {
	if(!m_isPlaying) return;
	m_isPlaying = false;
	fprintf(m_pipe, "quit\n");
	XDestroyWindow(WindowInfo().dpy(), m_childWindow);

bool Video::isPlaying() const {
	return m_isPlaying;


#ifndef VIDEO_H
#define VIDEO_H
#include <SDL/SDL.h>
#include <string>
#include <X11/Xlib.h>
class Video {
	Video(std::string const& filename, SDL_Rect const& videoRect);
	void play();
	void stop();
	bool isPlaying() const;
	FILE* m_pipe;
	std::string m_filename;
	SDL_Rect m_videoRect;
	Window m_childWindow;
	bool m_isPlaying;

main.cpp: (example usage)

#include "video.h"
int main() {
	SDL_SetVideoMode(800, 600, 0, 0);
	SDL_Rect rect = { 20, 20, 500, 500 };
	Video video("vid.m2v", rect);;
	for(;;) {
		SDL_Event ev;
		switch(ev.type) {
			case SDL_QUIT: return 0;
				if(video.isPlaying()) video.stop();

(compile with: g++ video.cpp main.cpp -lSDL -lX11)

I would appreciate comments..

(is there any blog hosting that doesnt suck for code??)

How to set leds in X, even on Ubuntu

#include <X11/Xlib.h>
#include <X11/XKBlib.h>

const int CapsLock = 2, NumLock = 16, ScrollLock = 1;

void setLeds(int leds) {
    Display *dpy = XOpenDisplay(0);
    XKeyboardControl values;
    values.led_mode = leds & ScrollLock ? LedModeOn : LedModeOff;
    values.led = 3;
    XChangeKeyboardControl(dpy, KBLedMode, &values);
    XkbLockModifiers(dpy, XkbUseCoreKbd, CapsLock | NumLock,
        leds & (CapsLock | NumLock) );

int main() {
    setLeds(CapsLock | NumLock | ScrollLock);