www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

sourisPS2.c (7749B)


      1 #include <module.h>
      2 #include <types.h>
      3 #include <pc/sourisPS2/sourisPS2.h>
      4 #include <pc/idt/idt.h>
      5 #include <pc/irq/irq.h>
      6 #include <pc/kbc/kbc.h>
      7 #include <pc/io/io.h>
      8 #include <interfaceUtilisateur/console/console.h> /* DEBUG */
      9 #include <pc/vesaGraphique/vesaGraphique.h> /* DEBUG */
     10 #include <interfaceUtilisateur/police/police.h> /* DEBUG */
     11 #include <math.h> /* DEBUG */
     12 
     13 MODULE(SourisPS2);
     14 
     15 DEPENDANCE_MODULE(SourisPS2, Kbc);
     16 DEPENDANCE_MODULE(SourisPS2, Clavier); /* DEBUG */
     17 DEPENDANCE_MODULE(SourisPS2, VesaGraphique); /* DEBUG */
     18 
     19 
     20 void envoyerCommandeSourisPS2(uint8 commande) {
     21 	/* TODO : ne pas partir dans une attente infinie en cas de bug matériel */
     22 	attenteKbc();
     23 	outb(PORT_KBC_COMMANDE, KBC_DEMMANDE_ECRITURE_SOURIS);
     24 	
     25 	attenteKbc();
     26 	outb(PORT_KBC_COMMANDE_SOURIS, commande);
     27 }
     28 
     29 void gestionnairePaquetSourisPS2(uint8 val) {
     30 	etatSourisPS2->paquets[etatSourisPS2->paquet++] = val;
     31 	
     32 	if (((etatSourisPS2->mouseID == 0) && (etatSourisPS2->paquet == 3)) || (etatSourisPS2->paquet >= 4)) {
     33 		etatSourisPS2->paquet = 0;
     34 		etatSourisPS2->etatGestionnaireSourisPS2 = ETAT_GESTIONNAIRE_SOURIS_PS2_ATTENTE;
     35 		
     36 		/* DEBUG */
     37 		//afficherChaineZ(" p");
     38 		//afficherEntierEnHexa(*(uint32*)(void*)&(etatSourisPS2->paquets));
     39 		
     40 		PaquetSourisPS2_1 p1 = CAST_INT_TO_STRUCT(etatSourisPS2->paquets[0], PaquetSourisPS2_1);
     41 		int xsgn = p1.xNegatif ? 0xffffff00 : 0;
     42 		int ysgn = p1.yNegatif ? 0xffffff00 : 0;
     43 		
     44 		if (p1.boutonGauche)
     45 			etatSourisPS2->couleur = 0xffffffff;
     46 		else if (p1.boutonDroite)
     47 			etatSourisPS2->couleur = 0;
     48 		else if (p1.boutonMilieu)
     49 			etatSourisPS2->couleur = 0xff8800ff;
     50 		
     51 		etatSourisPS2->curseur_x += etatSourisPS2->paquets[1] | xsgn;
     52 		etatSourisPS2->curseur_y -= etatSourisPS2->paquets[2] | ysgn;
     53 		etatSourisPS2->curseur_x = clip(etatSourisPS2->curseur_x, 0, 1023);
     54 		etatSourisPS2->curseur_y = clip(etatSourisPS2->curseur_y, 0, 767);
     55 		// setPixel32(etatSourisPS2->curseur_x, etatSourisPS2->curseur_y, etatSourisPS2->couleur);
     56 		policeAfficherCaractereCouleurPosition(0, etatSourisPS2->couleur, 0x00000000, etatSourisPS2->curseur_x, etatSourisPS2->curseur_y);
     57 	}
     58 }
     59 
     60 void gestionnaireDialogueSourisPS2(uint8 val) {
     61 	// afficherChaineZ(" gestionnaireDialogueSourisPS2");
     62 	// afficherChaineZ(" r");
     63 	// afficherEntierEnHexa(val); /* DEBUG */
     64 	// afficherChaineZ(" ");
     65 	// afficherEntierEnDecimal(etatSourisPS2->etapeDialogue);
     66 	// afficherChaineZ(" ");
     67 	// afficherEntierEnDecimal(etatSourisPS2->dialogue.longueur);
     68 	// afficherChaineZ(" ");
     69 	
     70 	
     71 	DialogueSourisPS2 dialogue = etatSourisPS2->dialogue;
     72 	int etape;
     73 	uint8  action, valeur;
     74 	uint8* parametre = dialogue.parametre;
     75 	
     76 	
     77 	boucle:
     78 		etape = etatSourisPS2->etapeDialogue++;
     79 		action = dialogue.sequence[etape * 2];
     80 		valeur = dialogue.sequence[etape * 2 + 1];
     81 		
     82 		if (etape >= dialogue.longueur)
     83 			goto fin;
     84 		
     85 		if (action == RECEVOIR) {
     86 			if (val != valeur) {
     87 				etatSourisPS2->etatGestionnaireSourisPS2 = ETAT_GESTIONNAIRE_SOURIS_PS2_ATTENTE;
     88 				dialogue.rappel(TRUE);
     89 				return;
     90 			}
     91 		} else if (action == RECEVOIR_V) {
     92 			*parametre = val;
     93 		} else if (action == RECEVOIR_OU_PAS) {
     94 			if (val != valeur)
     95 				goto boucle;
     96 		}
     97 	/* Fin boucle */
     98 	
     99 	etape = etatSourisPS2->etapeDialogue++;
    100 	while (etape < dialogue.longueur) {
    101 		action = dialogue.sequence[etape * 2];
    102 		valeur = dialogue.sequence[etape * 2 + 1];
    103 		
    104 		if (action == ENVOYER) {
    105 			/* DEBUG */
    106 			// afficherChaineZ(" s");
    107 			// afficherEntierEnHexa(valeur);
    108 			envoyerCommandeSourisPS2(valeur);
    109 		} else if (action == ENVOYER_V) {
    110 			/* DEBUG */
    111 			// afficherChaineZ(" s");
    112 			// afficherEntierEnHexa(*parametre);
    113 			envoyerCommandeSourisPS2(*parametre);
    114 		} else if (action == COMMANDE) {
    115 			attenteKbc();
    116 			outb(PORT_KBC_COMMANDE, valeur);
    117 		} else {
    118 			break;
    119 		}
    120 		etape = etatSourisPS2->etapeDialogue++;
    121 	}
    122 	
    123 	fin:
    124 	if (etape >= dialogue.longueur) {
    125 		etatSourisPS2->etatGestionnaireSourisPS2 = ETAT_GESTIONNAIRE_SOURIS_PS2_ATTENTE;
    126 		dialogue.rappel(FALSE);
    127 	}
    128 }
    129 
    130 bool dialoguer(const uint8* sequence, int longueur, uint8* parametre, FonctionRappelSourisPS2 rappel) {
    131 	// afficherChaineZ(" dialoguer");
    132 	desactiverInterruptions();
    133 	
    134 	longueur /= 2;
    135 	
    136 	if (etatSourisPS2->etatGestionnaireSourisPS2 != ETAT_GESTIONNAIRE_SOURIS_PS2_ATTENTE) {
    137 		activerInterruptions();
    138 		return FALSE;
    139 	}
    140 	
    141 	uint8  action, valeur;
    142 	int etape;
    143 	for (etape = 0; etape < longueur; etape++) {
    144 		action = sequence[etape * 2];
    145 		valeur = sequence[etape * 2 + 1];
    146 		
    147 		if (action == ENVOYER) {
    148 			/* DEBUG */
    149 			// afficherChaineZ(" s");
    150 			// afficherEntierEnHexa(valeur);
    151 			envoyerCommandeSourisPS2(valeur);
    152 // afficherChaineZ(" "); afficherEntierEnDecimal(etape); afficherChaineZ(" "); afficherEntierEnDecimal(longueur); afficherChaineZ(" ");
    153 		} else if (action == ENVOYER_V) {
    154 			/* DEBUG */
    155 			// afficherChaineZ(" s");
    156 			// afficherEntierEnHexa(*parametre);
    157 			envoyerCommandeSourisPS2(*parametre);
    158 // afficherChaineZ(" "); afficherEntierEnDecimal(etape); afficherChaineZ(" "); afficherEntierEnDecimal(longueur); afficherChaineZ(" ");
    159 		} else
    160 			break;
    161 	}
    162 
    163 	if (etape >= longueur) {
    164 		rappel(FALSE);
    165 	} else {
    166 		etatSourisPS2->dialogue.sequence = sequence;
    167 		etatSourisPS2->dialogue.longueur = longueur;
    168 		etatSourisPS2->dialogue.parametre = parametre;
    169 		etatSourisPS2->dialogue.rappel = rappel;
    170 		etatSourisPS2->etapeDialogue = etape;
    171 		etatSourisPS2->etatGestionnaireSourisPS2 = ETAT_GESTIONNAIRE_SOURIS_PS2_DIALOGUE;
    172 	}
    173 	
    174 	activerInterruptions();
    175 	return TRUE;
    176 }
    177 
    178 void gestionnaireSourisPS2() {
    179 	// afficherChaineZ(" gs ");
    180 	uint8 val;
    181 	val = inb(PORT_KBC_SOURIS);
    182 	// afficherEntierEnHexa(val);
    183 	
    184 	switch (etatSourisPS2->etatGestionnaireSourisPS2) {
    185 		case ETAT_GESTIONNAIRE_SOURIS_PS2_ATTENTE :
    186 		case ETAT_GESTIONNAIRE_SOURIS_PS2_PAQUET :
    187 			gestionnairePaquetSourisPS2(val);
    188 			break;
    189 		case ETAT_GESTIONNAIRE_SOURIS_PS2_DIALOGUE :
    190 			gestionnaireDialogueSourisPS2(val);
    191 			break;
    192 	}
    193 }
    194 
    195 void fonctionRappelVide(bool erreur) {
    196 	erreur = erreur;
    197 	// afficherChaineZ(" rappel "); /* DEBUG */
    198 	// afficherEntierEnDecimal(erreur);
    199 }
    200 
    201 
    202 void definirResolution(uint8 res) {
    203 	etatSourisPS2->resolution = res;
    204 	dialoguer(DialogueDefinirResolution, LONGUEUR(DialogueDefinirResolution), &(etatSourisPS2->resolution), fonctionRappelVide);
    205 }
    206 
    207 void demanderPaquet() {
    208 	// afficherChaineZ(" demanderPaquet"); /* DEBUG */
    209 	dialoguer(DialogueDemanderPaquet, LONGUEUR(DialogueDemanderPaquet), NULL, fonctionRappelVide);
    210 }
    211 
    212 void autoPaquets() {
    213 	// afficherChaineZ(" autoPaquets"); /* DEBUG */
    214 	dialoguer(DialogueAutoPaquets, LONGUEUR(DialogueAutoPaquets), NULL, fonctionRappelVide);
    215 }
    216 
    217 uint8 obtenirCompaqStatusByte() {
    218 	// afficherChaineZ(" getcsb"); /* DEBUG */
    219 	
    220 	attenteKbc();
    221 	outb(PORT_KBC_COMMANDE, KBC_DEMMANDE_LECTURE_COMPAQ_STATUS_BYTE);
    222 	
    223 	// attenteLectureKbc();
    224 	return inb(PORT_KBC_DONNEES);
    225 }
    226 
    227 void definirCompaqStatusByte(uint8 csb) {
    228 	attenteKbc();
    229 	outb(PORT_KBC_COMMANDE, KBC_DEMMANDE_ECRITURE_COMPAQ_STATUS_BYTE);
    230 	
    231 	attenteKbc();
    232 	outb(PORT_KBC_DONNEES, csb);
    233 }
    234 
    235 void activerIrq12() {
    236 	definirGestionnaireRequeteInterruption(LIGNE_IRQ_SOURIS, gestionnaireSourisPS2);
    237 	activerLigneRequeteInterruption(LIGNE_IRQ_SOURIS);
    238 	
    239 	uint8 csb = obtenirCompaqStatusByte();
    240 	// afficherEntierEnHexa(csb); afficherChaineZ(" "); /* DEBUG */
    241 	csb |= 2; // Activer Irq 12
    242 	csb &= ~0x20; // Désactiver l'horloge souris
    243 	// afficherEntierEnHexa(csb); /* DEBUG */
    244 	definirCompaqStatusByte(csb);
    245 }
    246 
    247 void initSourisPS2 (void** etat) {
    248 	*etat = etatSourisPS2;
    249 	
    250 	etatSourisPS2->etapeDialogue = 0;
    251 	etatSourisPS2->etatGestionnaireSourisPS2 = ETAT_GESTIONNAIRE_SOURIS_PS2_ATTENTE;
    252 	
    253 	etatSourisPS2->curseur_x = 1024/2;
    254 	etatSourisPS2->curseur_y = 768/2;
    255 	
    256 	activerIrq12();
    257 	
    258 	autoPaquets();
    259 	
    260 	etatSourisPS2->couleur = 0xffffffff; /* DEBUG */
    261 }
    262 
    263 void deinitSourisPS2 (void** etat) {
    264 	*etat = NULL;
    265 }