I have tested the following code on my smartbook. this should run in the background and do what you need if you want the smartbook to suspend on lid close without a desktop running.
You can also put in a similar if statement to do whatever you want on power button presses as well. I believe there should be two values one for button press and one for release, you only want to catch one of them.
I didn't bother with this in the original code on github because I just wanted a simple shutdown on powerbutton press and had the daemon exit after it called system.
Either way I hope this helps you. you can just save it to suspendd.c and do gcc -o suspendd suspendd.c and you will have a suspendd executable. you can put that into /usr/sbin and run it from your rc.local script at boot. you can test it standalone by just running sudo ./suspendd -f (or just let it fork into the background by omitting the -f)
Code:
/* $Id: powerbutton.c,v 1.3 2011/06/23 07:35:36 wschaub Exp wschaub $ */
/* read /dev/input/event0 forever until the lid is closed and then suspend */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <linux/input.h>
/* struct input_event {
struct timeval time;
unsigned short type;
unsigned short code;
unsigned int value;
}; */
int main(int argc, char **argv) {
int opt,foreground;
foreground = 0;
while ((opt = getopt(argc, argv, "f")) != -1) {
switch (opt) {
case 'f':
foreground = 1;
break;
default: /* '?' */
fprintf(stderr, "Usage: %s [ -f ]\n",
argv[0]);
exit(EXIT_FAILURE);
}
}
if(!foreground) {
daemon(0,0);
}
struct input_event event; //Each read of event0 gives us an input_event struct
FILE *fp;
fp = fopen("/dev/input/event0","r");
if(fp == NULL)
{
perror("/dev/input/event0");
abort();
}
//Loop forever reading events and do something only when the lid is closed.
while(1)
{
fread(&event,sizeof(event),1,fp);
//printf("type = %d\n",event.type);
//printf("code = %d\n",event.code);
if(event.type == EV_SW && event.code == SW_LID && event.value == 1) {
printf("lid switch pressed value = %u\n",event.value);
system("pm-suspend");
}
}
}