/* scrrenkey.c 
 * Based on a code of crypt2g (Denis Bodor <d.bodor@calixo.net>) for LTP's comm
 * Copyright (C) 2007 Maximilien Cuony <theglu@theglu.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

//Inclusion des fichiers nécessaires
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include <io.h>
#include <sys/perm.h>

//Définition des contantes
#define BASEPORT 0x378


int main(int argc, char *argv[]) {
    	unsigned int lu;
   	unsigned int ancienlu;

	double useruid;
	double groupeuid;

	//Recuperation des informations utilisateurs
	useruid = getuid();
	groupeuid = getgid();

	//Prise temporaire des droits root
	setuid(0);
	setgid(0);
	
	if(ioperm(BASEPORT,3,1)) { //On tente d'ouvrir le port parrale
		perror("ioperm"); //On n'y pas réussi
		exit(1);
	}

	//On remet les droits de l'utilisateur
	setuid(useruid);	
	setgid(groupeuid);	
	
	while(1) {

		lu=inb(BASEPORT+1); //On va lire le port parralle
		lu=lu & 128; 

		if (ancienlu != lu) { //Si le port à changé de valeur

			if (lu == 0) { //Ba soit on active

				system("/usr/bin/gnome-screensaver-command --activate",NULL);
 
			} else { //Soit on désactive l'écran de veile

				system("/usr/bin/gnome-screensaver-command --deactivate",NULL);
			}

			ancienlu = lu;
		}

		sleep(1); //On attend un peut, y'a peut être d'autre trucs plus cools à faire pour le processeur
	}
	return(0);
}
			

