Donnerstag, 16. August 2012

FlashDevelop: CamelCase cursor hopping

As a keyboard person I really like to navigate text fast. I use CRTL-Left and CRTL-Right in any text editor to jump between words. Eclipse added another feature onto the word hopping. It recognizes the CamelCase nature of code. So if I have

myLayoutManager

it will position the cursor at the beginning of my on the first press, on the L of Layout on the second press and on the M of Manager on the thrid press, before jumping to the next word or character. For editing code this is simply great. Saves so much time and single curor movement.

Because I have not found an official name for this behavior I will call it CamelCase cursor hopping (CCCH).

FlashDevelop does not support CCCH, but you can install plugins. Mørkeulv wrote the Wordshifter plugin that enables it in FlashDevelop. Just download the binary from his blog and install it. He even provides the source code for the plugin.

You can customize the jumping behavior by providing your own regular expression and which keys should be used to hop around the code. This way you can reenable the old word jumping and have CCCH along each other.

Thanks for the addon!

Links:



Mittwoch, 15. August 2012

Android: Install USB drivers for HP Touchpad

If you have a HP Touchpad running CyanogenMod Android, you can use it to develop and test applications on the native device. But before you can use the tools from the Android SDK on a Windows PC, you have to install the USB drivers.

The Android SDK provides USB drivers for Windows in <sdk-directory>/extras/google/usb-drivers. But those drivers don't include the needed information to identifiy a CyanogenMod Android. CyanogenMod has a patched android_winusb.inf file and the instructions on how to install it available in their wiki.

The patched driver information file does not contain information for the HP Touchpad. You have to patch the patched INF file again for the Touchpad.

So here are the steps needed to connect the Touchpad to a Windows PC to start developing/debugging:


1. Turn on MTP, which is disabled by default, on the Touchpad.
Go to Settings -> Storage -> click on the three small squares in the upper right corner -> USB computer connection -> enable Media Device (MTP)
2. Add HP Touchpad Vendor and Product IDs to the INF file
Edit the android_winusb.inf either from the CyanodgenMod wiki or the original file from the Android SDK. Add these lines at the end of the sections for [Google.NTx86] and [Google.NTamd64].

; HP TouchPad
%SingleAdbInterface%    = USB_Install, USB\VID_0BB4&PID_6860&REV_0227&MI_01
%CompositeAdbInterface% = USB_Install, USB\VID_0BB4&PID_6860&MI_01


3. Update driver in Windows Device Manager
Open the Device Manager in Windows, selection the unknown USB device and update drivers. Select the android_winusb.inf file you just modified.

Dienstag, 14. August 2012

FlashDevelop: Curly braces and CodeFormatter

I have been using FlashDevelop lately. It is a great IDE for ActionScript development. But coming from an Eclipse background I had to relearn a lot of keyboard shortcuts. One feature I really like in an IDE is using a code formatter. FlashDevelop contains such a feature and it can be accessed by right clicking in the editor and selecting Refactor->Code Formatter or by using the keyboard shortcut CRTL-Shift-2.

But as always every programmer hates it when the automatic code formatter destroys lovingly crafted indentation. The default behavior of the FlashDevelop formatter is to put the curly braces on a new line. I have struggled a long time with myself over whether to put them on a new line or keep them on the same line. The later option won. And now it just feels awkward to see them on a line of their own.

Of course FlashDevelop provides a solution - a little bit hidden. Go to

Tools -> Program Settings -> CodeFormatter -> BraceStyle

and change it to "OnLine".

Mittwoch, 4. Juli 2012

Windows: Create/Delete a service

Create a new service:
sc create SERVICENAME binpath= "<path to executable> -arg1 -arg2"

The excutable is installed as a service. See my other post about building your own service. Please note that there is a space after binpath=.DIf the service needs arguments (which will be passed to the ServiceMain function), you can add them (arg1, arg2 above). Just be sure to surround the executable and the arguments with quotes.

Delete an existing service:
sc delete SERVICENAME

The service will be deleted after the next restart.

Dienstag, 3. Juli 2012

C++: Create a Windows service

Creating a Windows service is fairly simple. You start by writing you main() function as you would normally. In the main() function you have to call StartServiceCrtlDispatcher(). The programm has to make this call at least 30 seconds after it has launched. Otherwise Windows will assume the service failed to start up. So the simplest main() function for a service will look something like this:

int main(int argc, char **argv) {
  SERVICE_TABLE_ENTRY serviceTable[2];
  serviceTable[0].lpServiceName = "ServiceTest";
  serviceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION) ServiceMain;

  serviceTable[1].lpServiceName = NULL;
  serviceTable[1].lpServiceProc = NULL;
  StartServiceCtrlDispatcher(serviceTable);  
}

StartServiceCrtlDispatcher() takes an array of SERVICE_TABLE_ENTRYs. Each defines the name and callback function of one service it should start. The callback function is run in its own thread and StartServiceCrtlDispatcher() will only return after all services it has started have been stopped.

As the name indicates, the callback function of the service is the real main function of the service. Here the service should do whatever it is supposed to do. In order to by managed by the Services interface of Windows the service has to implement a callback to a control handler with the RegisterServiceCrtlHandler() function. The callback is invoked every time the service should change its state (Stop for example).

The service should use the SetServiceStatus() function to report its current status to the system. The following code can be used as a skeleton for your own service.

SERVICE_STATUS ServiceStatus; 
SERVICE_STATUS_HANDLE hServiceStatus; 

// Control handler function callback
void ControlHandler(DWORD request) 
{ 
  switch(request) { 
    case SERVICE_CONTROL_STOP: 
      // Service has been stopped
      ServiceStatus.dwWin32ExitCode = 0; 
      ServiceStatus.dwCurrentState  = SERVICE_STOPPED; 
      SetServiceStatus (hServiceStatus, &ServiceStatus);
      return; 
 
    case SERVICE_CONTROL_SHUTDOWN: 
      ServiceStatus.dwWin32ExitCode = 0; 
      ServiceStatus.dwCurrentState  = SERVICE_STOPPED; 
      SetServiceStatus (hServiceStatus, &ServiceStatus);
      return; 
        
    default:
      break;
  } 
 
  // Report current status
  SetServiceStatus (ServiceStatusHandle,  &ServiceStatus);
} 

// Service main function callback
void ServiceMain(int argc, char **argv) {
  int count = 0;
  int result;
 
  ServiceStatus.dwServiceType = SERVICE_WIN32; 
  ServiceStatus.dwCurrentState = SERVICE_START_PENDING; 
  ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | 
                                       SERVICE_ACCEPT_SHUTDOWN;
  ServiceStatus.dwWin32ExitCode = 0; 
  ServiceStatus.dwServiceSpecificExitCode = 0; 
  ServiceStatus.dwCheckPoint = 0; 
  ServiceStatus.dwWaitHint = 0; 

  hServiceStatus= RegisterServiceCtrlHandler(
                    "ServiceTest", 
                    (LPHANDLER_FUNCTION) ControlHandler); 
  if (hServiceStatus == (SERVICE_STATUS_HANDLE) 0) { 
    // Registering Control Handler failed
    return; 
  }  

  ServiceStatus.dwCurrentState = SERVICE_RUNNING; 
  SetServiceStatus (hServiceStatus, &ServiceStatus);

  while (ServiceStatus.dwCurrentState == SERVICE_RUNNING) {
    // Do you important service stuff here
    Sleep(5000);
  }
}

This service will run as long as its state is SERVICE_RUNNING. Once the state changes the ServiceMain function will return and the service will terminate.

Visit these links for more examples and explanations:



Donnerstag, 26. April 2012

Shell: Determine lines of code

Use this command to determine the number of lines of code you have written for your C++ project.

find ../src -name "*.cpp" -or -name "*.h" | xargs wc -l

Mittwoch, 14. März 2012

Windows: Shutdown via command line

You can shutdown a computer running Windows using the command line. Use the shutdown command to force a shutdown, reboot or hibernate.

Shutdown computer
shutdown /s

Restart computer
shutdown /r

Hibernate computer
shutdown /h

The shutdown command is available since Windows XP. On older versions of Windows use this command:

Restarting the computer
RUNDLL.EXE user.exe,exitwindowsexec

Shut down the computer
RUNDLL32.EXE user,exitwindows