Typing ASCII or accented characters in Linux rdesktop

My thin-client desktop is running Debian Linux with the Gnome window manager. When I use it to connect to my Windows Server VM using rdesktop, I usually get a problem when trying to type ASCII or accented (French) characters. For example, when typing Alt 130, I am expecting to get the é character. Instead, it’s only printing 30 (the 1 character was omitted).

It drives me crazy since I have to type a lot of these accented, especially French, characters. Fortunately, later I found that turning off the Num Lock fixed this problem. Yes, for some reason, I can’t type the ASCII characters with the keyboard’s Num Lock on.

Actually, working with Linux rdesktop to connect to a Windows machine is very annoying. Aside from the problem I just described, I still have more issues that yet to be solved:

  1. I can’t set the window title bar to auto-hide like the Windows’ remote desktop functionality. This title bar takes some very valuable screen estate. And it looks ugly having two cascading title bars: from the Linux rdesktop program and from the Windows applications.
    Sure I can maximize the rdesktop window to full screen, but then, it doesn’t work very well with my multi-monitor configuration.
  2. Sometimes, the mouse pointer will break and the pointer’s image get corrupted. I haven’t found a quick way to fix or restore it aside from restarting the window manager or the OS session.
  3. The Ctrl+Alt key combination sometimes behaves erratically that messes up the subsequent key presses. I have to change the focus outside the rdesktop window if this happens. I tried to remove all key bindings from the host Linux OS but still no improvement.

I love how this just works in Windows.

A singleton base class to implement the singleton pattern in C#

The singleton design pattern is one of the most controversial pattern. Some people even call it anti-pattern, evil, or stupid. I won’t go into the debate now, but I just want to share something for those who need to use singletons.

Although it is one of the easiest pattern to understand, it is also very easy to code it poorly, leading to unexpected side effects such as poor performance or being not thread-safe.

Jon Skeet has written a very good article describing different implementations of the singleton pattern in C#. He gives a nice example on how you can write a singleton code that is thread-safe and lazy-instantiating. The code is less than 20 lines and you can use it as a template for your singletons.

Since in some projects I have created several singleton objects, it started to get repetitive. Therefore I decided to write a base class based on this pattern as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Globalization;
using System.Reflection;
 
public abstract class SingletonBase<t> where T : class
{
  /// <summary>
  /// A protected constructor which is accessible only to the sub classes.
  /// </summary>
  protected SingletonBase() { }
 
  /// <summary>
  /// Gets the singleton instance of this class.
  /// </summary>
  public static T Instance
  {
    get { return SingletonFactory.Instance; }
  }
 
  /// <summary>
  /// The singleton class factory to create the singleton instance.
  /// </summary>
  class SingletonFactory
  {
    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static SingletonFactory() { }
 
    // Prevent the compiler from generating a default constructor.
    SingletonFactory() { }
 
	internal static readonly T Instance = GetInstance();
 
    static T GetInstance()
    {
      var theType = typeof(T);
 
      T inst;
 
      try
      {
        inst = (T)theType
          .InvokeMember(theType.Name,
            BindingFlags.CreateInstance | BindingFlags.Instance
            | BindingFlags.NonPublic,
            null, null, null,
            CultureInfo.InvariantCulture);
      }
      catch (MissingMethodException ex)
      {
        throw new TypeLoadException(string.Format(
          CultureInfo.CurrentCulture,
          "The type '{0}' must have a private constructor to " +
          "be used in the Singleton pattern.", theType.FullName)
          , ex);
      }
 
      return inst;
    }
  }
}

For an example of usage, we will create a singleton class that generates a sequence number:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Must seal the class so no sub-classes can be defined.
public sealed class SequenceGeneratorSingleton
  : SingletonBase<sequenceGeneratorSingleton>
{
  // Must have a private constructor so no instance can be created externally.
  SequenceGeneratorSingleton()
  {
    _number = 0;
  }
 
  private int _number;
 
  public int GetSequenceNumber()
  {
	return _number++;
  }
}

Then, a console application will call the sequence generator class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Program
{
  static void Main(string[] args)
  {
    Console.WriteLine("Sequence: " + SequenceGeneratorSingleton.Instance
	  .GetSequenceNumber().ToString());  // Print "Sequence: 0"
 
    Console.WriteLine("Sequence: " + SequenceGeneratorSingleton.Instance
	  .GetSequenceNumber().ToString());  // Print "Sequence: 1"
 
    Console.WriteLine("Sequence: " + SequenceGeneratorSingleton.Instance
	  .GetSequenceNumber().ToString());  // Print "Sequence: 2"
  }
}

To recap, in order to create a singleton class using the singleton base class, you need to do the following:

  1. Define a sealed class which derives from SingletonBase<T>, where T is the class name you are defining. It ensures that you cannot create subclasses from this singleton class.
  2. Define a single parameterless private constructor inside the class. It ensures that no instances of this class can be created externally.
  3. Access the class’ singleton instance and public members by calling the Instance property.

I have also included the code in LiquidSilver, which you can view or download from the repository.

LiquidSilver switches to Mercurial on CodePlex

As you may have noticed, CodePlex now supports Mercurial as the source control. Mercurial is a free and distributed version control system. It’s currently gaining a high momentum that big projects like Mozilla, OpenSolaris, OpenOffice.org, Symbian OS, and many others are now using it. Aside from CodePlex, other project hosting sites like Google Code, bitbucket, and SourceForge also offer Mercurial support.

Because DVCS has many advantages over traditional centralized VCS such as Subversion or CVS, I also decided to switch to Mercurial for managing the source code of LiquidSilver.

Setting Up Mercurial on CodePlex

If you’re starting a new project, CodePlex will give you the option to use Mercurial as your version control system. For existing projects, unfortunately, you cannot switch to Mercurial by yourself. Instead, if you are a project owner, you can contact CodePlex using the contact form and tell your intention to switch to Mercurial. Ensure that you’re logged in when doing this and mention your project name, user name, and the email address associated to your user name.

For me, it took less than 24 hours before CodePlex responded to my request and LiquidSilver had been migrated to Mercurial. All the versions and labels were intact and I could clone the repository successfully. Thanks, CodePlex.

Using Mercurial

If you are just starting and not familiar with Mercurial or any DVCS in general, it can be confusing at first since the concept is different. Fortunately, Joel Spolsky has composed a wonderful tutorial site called Hg Init that can very quickly and greatly help you understand DVCS or Mercurial in particular. I highly recommend you to visit it. You can also learn Mercurial from its official learning site or the book.

Although you can use Mercurial entirely from the command line interface, you may find it easier or more comfortable to use TortoiseHg that provides a friendly and familiar (if you have used TortoiseCVS or TortoiseSVN before) interface for Windows users.

Visual Studio users can benefit from VisualHG, a Visual Studio extension that can give you real-time visual status updates on your files from within the Visual Studio Solution Explorer window. VisualHG depends on TortoiseHg to execute the Mercurial commands.

Now, have fun with Mercurial.