New Qt project: qbar

Hello there! I’m still looking for an elegant window manager for developers under GNU/Linux. I think that mcwm is one of the nearest to my concept of design. No window borders, small RAM resources, XCB based.

When I downloaded mcwm project I noticed the TODO file containing an interesting feature request: snap borders. So in a couple of day I produced a patch and submitted to the author.  Unfortunately the author’s git repository doesn’t have a web interface, so, if you’re curious, you can see the patch as follows:

$ git clone git://hack.org/mcwm
$ cd mcwm
$ git diff 864bc76d d20afb19

Well, now that the patch is merged, It’s time to develop a general purpose toolbar. A toolbar to display values, a toolbar to perform actions, a toolbar to rule them all! 🙂

And…in a couple of days I developed qbar that uses stdin streaming to fill itself with objects like: buttons, labels, menu, progress bar. And who knows about the future? You can write you “applet” in standard bash script and redirect the stdout to qbar through a pipe. As you can imagine there is a protocol for the arguments given in the stdin. The protocol is the following:

LABEL
[L][<refresh>]<text>

BUTTON
[B][<refresh>][<icon_path>][<command>]<text>

MENU
[M][<icon_path>][<text>][<icon_path][<command>]<text>*[<icon_path>][<command>]<text>...

SEPARATOR
[S][<size_mode>]<size>

PROGRESS BAR
[P][<refresh>][<size>]<text>

<refresh> can be S (static) or R (refresh)
<size_mode> can be E (expanding) or F (fixed)

qbar is developed in Qt (4.8), so you can use a simple CSS stylesheet to develop a new theme. Let me show you how:

QMainWindow {
	background: #333333;
	color: #fff;
}

QLabel {
	color: #fff;
}

QMenu {
	background: #333333;
	color: #fff;
}

QPushButton {
	border-radius: 10px;
	padding-left: 5px;
	padding-right: 5px;
	color: #fff;
}

QPushButton:hover, QMenu::item:selected{
	background: #666;
}

#ITEM_2 {
	width: 150px;
}

QProgressBar {
     border: 2px solid rgba(0,0,0,0);
     border-radius: 5px;
     text-align: center;
     background-color: #666;
     color: #fff;
 }

 QProgressBar::chunk {
     background-color: #05B8CC;
 }

As you can see from the code, you can refer to the n-th element using the CSS selector <#ITEM_> followed by the element id. And, you can use the following command line parameters:

qbar [-a <l|c|r>] [-p <t|b>] [-t] [-h <pixels>] [-stylesheet <file>]

-a alignment
   l = left
   c = center
   r = right
-p position
   t = top
   b = bottom
-t set transparent background
-h height in pixels
-stylesheet css file

qbar is published at Gitorious:

git://gitorious.org/qt-projects/qbar.git

and at Qt-apps.org. What’s missing? Ah…yes, screenshot!

qbar

If you liked it, please vote for it on qt-apps.org, and if you want please submit patches and feature requests, all the code is released under the terms of GNU/GPL.

Annunci

jQuery short answers quiz

I’m developing a website containing my deutsch course lessons. Our teacher given us some exercises, and I had the need to render it on an interactive web page. How to accomplish this mission? Obviously using jQuery.
The main issue is to write in a simple way your “short answers” form, and leave the hard job to jQuery code. So the form need to looks like:

<form>
    The sky is <input type="text" answ="blue"/>.
    The pen is <input type="text" answ="on"/> the table.<br />
    Yes I, <input type="text" answ="am"/>.<br />
    <input type="button" class="subm" value="Verify"/>
</form>

As you can see, each <input> contains the attribute answ with the correct answer and the element you want to use to check needs to have class=”subm”. That’s all!

The JS code resizes all the text inputs using the answer’s length, and changes CSS class (answ_error, answ_ok) of the input element. Finally a couple of attributes (tot and ok) will be added to the form, containing the number of text inputs and the number correct answers.

Code is available here.

SA_RESTART and select syscall on Linux

Poco tempo fa mi sono imbattuto in un comportamento anomalo relativamente all’uso di una select() con timeout e un handler per il segnale SIGALRM. Il comportamento anomalo (documentato in parte su man 2 select) prevede che nel caso in cui la select() venisse interrotta da un segnale (SIGALRM nel mio caso) avrebbe come conseguenza la modifica del timeout passato come riferimento. Altra anomalia è data dal fatto che anche impostando la flag SA_RESTART, che forza la ri-esecuzione di una syscall interrotta da un segnale, la select() non si comporta come dovrebbe.

Fornisco un codice che ho prodotto per spiegare meglio la situazione…

#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

typedef void (*sighandler_t)(int);

void alarm_hand()
{
 // do something
}

int main(int argc, char **argv)
{
 int res;
 int alarm_timeout, select_timeout;
 fd_set fset;
 struct timeval tv;
 struct sigaction act;

 if(argc != 3) {
  printf("usage: %s <alarm timeout> <select timeout>\n");
  exit(EXIT_FAILURE);
 }

 alarm_timeout = atoi(argv[1]);
 select_timeout = atoi(argv[2]);

 /* signal definition */
 act.sa_handler = alarm_hand;
 sigemptyset(&act.sa_mask);
 /* this flag should restart the interrupted syscall */
 act.sa_flags = SA_RESTART;
 sigaction(SIGALRM, &act, NULL);

 /* starting SIGALRM countdown */
 alarm(alarm_timeout);

 tv.tv_sec = select_timeout;
 tv.tv_usec = 0;

 FD_ZERO(&fset);
 FD_SET(STDIN_FILENO, &fset);

 /* this is a blocking syscall */
 res = select(STDIN_FILENO + 1, &fset, NULL, NULL, &tv);

 /* select error */
 if (res == -1) {
  printf("select error: %d seconds left!\n", tv.tv_sec);
  exit(EXIT_FAILURE);
 }
 /* select timeout */
 else if (res == 0) {
  printf("select timeout!\n");
  exit(EXIT_FAILURE);
 }
 /* there are something in stdin */
 else if (FD_ISSET(STDIN_FILENO, &fset)) {
  printf("stdin has you!\n");

  int n;
  char a;
  do {
   n = read(STDIN_FILENO, &a, 1);
   if(n!=0)
    printf("%c", a);
  } while((n!=0) && (a != '\n'));
 }

 exit(EXIT_SUCCESS);

}

Dopo averlo compilato con

$ gcc example.c -o example

il programmino che accetta un input da tastiera viene lanciato attraverso i due parametri alarm timeout e select timeout:

$ ./example <alarm timeout> <select timeout>

Si possono verificare i seguenti scenari:

  • Si fornisce un input prima dello scadere di uno dei due timeout (Es. “ciao” seguito da RETURN): il programma termina correttamente e stampa l’input.
  • Si inserisce alarm timeout maggiore di select timeout (Es. ./example 5 2): la select() termina correttamente essendo scaduto il suo timeout.
  • Si inserisce select timeout maggiore di alarm timeout (Es. ./example 1 20): il processo riceve SIGALRM e la select termina in maniera anomala (non viene considerato il SA_RESTART).

Quindi se fate uso di select per la gestione di socket, file, ecc.. in concomitanza di un handler per SIGALRM prestate molta attenzione e riavviate “a mano” la select. Uno dei modi più eleganti è impostare una flag (di tipo sig_atomic_t!) dentro il gestore associato al segnale e gestirla all’interno del controllo d’errore della select().