OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 9:13 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Keyboard prompt sometimes ignores return key?
PostPosted: Tue Dec 13, 2016 5:22 am 
Offline
User avatar

Joined: Mon Dec 12, 2016 6:32 pm
Posts: 3
Location: Alabama
Hello, this is my first post, sorry in advance for any grievances.

I'm playing around with the BareBones tutorial from the wiki to learn, and I've pretty much gotten a keyboard "listener" working, but I'm having trouble with a prompt system I want to implement.

Image

My C/C++ is rusty, but anyways, everything works fine in getScancode(), it's when I pass the char to prompt() that it starts being odd. I am trying to make the loop in prompt() stop when the user hits RETURN. (This also happens if I change the key I want detected to something else) For some reason, the loop doesn't sometimes acknowledge what input is. Did I do something dumb?

Here is the code:

Code:
// I have omitted the terminal.h because it's mostly irrelevant for this, and it works like it should

#if !defined(__cplusplus)
#include <stdbool.h> /* C doesn't have booleans by default. */
#endif
#include <stddef.h>
#include <stdint.h>
#include "terminal.h"

#if defined(__linux__)
#error "You are not using a cross-compiler, you will most certainly run into trouble"
#endif

#if !defined(__i386__)
#error "This needs to be compiled with a ix86-elf compiler"
#endif

unsigned char keyset[128] = {
    0,  27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */
  '9', '0', '-', '=', '\b', /* Backspace */
  '\t',         /* Tab */
  'q', 'w', 'e', 'r',   /* 19 */
  't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */
    0,          /* 29   - Control */
  'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 39 */
'\'', '`',   0,        /* Left shift */
'\\', 'z', 'x', 'c', 'v', 'b', 'n',            /* 49 */
  'm', ',', '.', '/',   0,              /* Right shift */
  '*',
    0,  /* Alt */
  ' ',  /* Space bar */
    0,  /* Caps lock */
    0,  /* 59 - F1 key ... > */
    0,   0,   0,   0,   0,   0,   0,   0,
    0,  /* < ... F10 */
    0,  /* 69 - Num lock*/
    0,  /* Scroll Lock */
    0,  /* Home key */
    0,  /* Up Arrow */
    0,  /* Page Up */
  '-',
    0,  /* Left Arrow */
    0,
    0,  /* Right Arrow */
  '+',
    0,  /* 79 - End key*/
    0,  /* Down Arrow */
    0,  /* Page Down */
    0,  /* Insert Key */
    0,  /* Delete Key */
    0,   0,   0,
    0,  /* F11 Key */
    0,  /* F12 Key */
    0,  /* All other keys are undefined */
};


#if defined(__cplusplus)
extern "C" { /* Use C linkage for kernel_main. */
#endif

static inline void outb(uint16_t port, uint8_t val) {
    asm volatile ( "outb %0, %1" : : "a"(val), "Nd"(port) );
}

static inline uint8_t inb(uint16_t port) {
    uint8_t ret;
    asm volatile ( "inb %1, %0"
                   : "=a"(ret)
                   : "Nd"(port) );
    return ret;
}

// initialize the terminal
Terminal term;

char last = 0;

char getScancode() {
   
    char c=0;
   if(inb(0x60)!=c) {
      c=inb(0x60);
        if (c!= last) {
            last = c;
            if (c>0) {
                if (keyset[c] == '\n') {
                    term.WriteString("(Enter Pressed)");
                } else {
                    term.PutChar(keyset[c]);
                }
                return keyset[c];
            }
      }
   }
}

void prompt() {
   term.PutChar('>');
   
   char input=0;

   while (input != '\n'){

      if (getScancode()) {
            input = getScancode();
      }
   }
    term.PutChar('\n');

}

void kernel_main(void) {
   /* Initialize terminal interface */
   

   term.Initialize();

   /* Newline support is left as an exercise. */
   term.WriteString("|Ready!\n");

    while(true)
        prompt();

}

}

_________________
Just on the border of your waking mind, There lies another time. Where darkness and light are one. As you tread the halls of sanity, You feel so glad to be, Unable to go beyond.
https://github.com/j0shgit
https://en.lichess.org/@/j0sh666


Top
 Profile  
 
 Post subject: Re: Keyboard prompt sometimes ignores return key?
PostPosted: Tue Dec 13, 2016 5:41 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5137
j1sh wrote:
Code:
      if (getScancode()) {
            input = getScancode();
      }

If the first call to getScancode() returns '\n' and the second call to getScancode() returns something else, what happens?

(You have another bug like this elsewhere in your code. Can you spot it?)


Top
 Profile  
 
 Post subject: Re: Keyboard prompt sometimes ignores return key?
PostPosted: Tue Dec 13, 2016 5:53 am 
Offline
User avatar

Joined: Mon Dec 12, 2016 6:32 pm
Posts: 3
Location: Alabama
Oh wow I feel dumb. Thanks very much, it works exactly like it should now.

Now I just gotta find the other bug... :oops:

Edit: Found it! Thanks again.

_________________
Just on the border of your waking mind, There lies another time. Where darkness and light are one. As you tread the halls of sanity, You feel so glad to be, Unable to go beyond.
https://github.com/j0shgit
https://en.lichess.org/@/j0sh666


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 96 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group