getchar

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
vertex
Posts: 5
Joined: Tue 14 Jun 2011, 01:04

getchar

#1 Post by vertex »

I have been working with a program called Graphcan originally written for a Zaurus running Linux by Atilla Vass and ported to use with the Eee PC by Peter Astrand. I made some modification to the program, mainly cosmetic. However, one feature that I could not get to work correctly is keyboard input. If i launch the program directly text input does not work. If I run it from a terminal and I keep the terminal in focus, the text input does work. I'm thinking that Puppy does not think Graphcan is the focus, even in full screen mode. How can I verify what is happening?
Here is the relevant Graphcan code:

if (select(FD_SETSIZE, &fdset, 0, 0, &to) != -1) {
if (FD_ISSET(0, &fdset))
{
c = getchar();


switch (c) {
case 0xa: // ReStart
if (Sleeping) {
ProcessGo = 0;
ContSignal = 1;
Sleeping = 0;
PriusIsPowered = 1;
}
else {
alarm(0);
SaveStat();
Sleeping = 1;
if (Port >= 0) {
sprintf(Message, "C\015"); // Close the CAN channel
if (WriteToPort(Message)) {
fprintf(stderr, "Error writing to port : Close CAN Channel\n");
}
close(Port);
Port = -1;
}

}
break;
case 'q':
ProcessGo = 0;
RealQuit = 1;
break;

case 's':
if (++SI_Measurements > 1)
SI_Measurements = 0;
break;

case 'p':
PriusIsPowered = 0;
Sleeping = 0;
CurrentSpeed = 0;
break;

case 0x7F: // BS
if (++VoiceMode > 1)
VoiceMode = 0;
break;

case ' ':
NeedSynced = 1;
FSRCntr = 1;
break;

}
}
}
#endif

return;

Thanks for your comments in advance.
Howard

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

Re: getchar

#2 Post by Moose On The Loose »

vertex wrote:I have been working with a program called Graphcan originally written for a Zaurus running Linux by Atilla Vass and ported to use with the Eee PC by Peter Astrand.
.......
c = getchar();
fprintf(LogFile,"Got a %x\n",c);

Just to see if you got a character at all. I don't think your keyboard is your standard in at all but this will show if you got anything.

Bruce B

Re: getchar

#3 Post by Bruce B »

int c; ???? was specified first ????

~

akash_rawal
Posts: 229
Joined: Wed 25 Aug 2010, 15:38
Location: ISM Dhanbad, Jharkhand, India

#4 Post by akash_rawal »

getchar actually reads a character from stdin and not from keyboard.

In terminal, stdin is usually keyboard input.

And what's worse, stdin is line buffered, i.e. program won't get anything till you press enter key.

So you have to think of accessing keyboard directly.

I will search and come back after I have found something, useful.

vertex
Posts: 5
Joined: Tue 14 Jun 2011, 01:04

#5 Post by vertex »

Of course c is declared, otherwise it wouldn't even compile.
The program works when run in termnal, without a carriage return.
Doesn't that mean that the keyboard is working throhgg stdin, or is it somehow redirected?

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#6 Post by Moose On The Loose »

vertex wrote:Of course c is declared, otherwise it wouldn't even compile.
The program works when run in termnal, without a carriage return.
Doesn't that mean that the keyboard is working throhgg stdin, or is it somehow redirected?
stdin can be line edited or just character by character. The C functions for reading from stdin are just the same as the ones that read from files but stdin is implied. This is not the same as the keyboard.

User avatar
Dougal
Posts: 2502
Joined: Wed 19 Oct 2005, 13:06
Location: Hell more grotesque than any medieval woodcut

#7 Post by Dougal »

Maybe it's a tty problem? When you run the program from the terminal it will probably be associated with pts/1 etc. while when started from the menu it will be with tty1 (look at "ps ax" output).
What's the ugliest part of your body?
Some say your nose
Some say your toes
But I think it's your mind

Bruce B

#8 Post by Bruce B »

I just made a test using getchar() on the tty and the terminal emulator in Puppy. I used type int and type char for the test.

The results were: The first character entered will become the value for the variable after the enter key is pressed.

~

vertex
Posts: 5
Joined: Tue 14 Jun 2011, 01:04

#9 Post by vertex »

What is "ps ax" output?

Thanks for the help.

Bruce B

#10 Post by Bruce B »

vertex wrote:What is "ps ax" output?

Thanks for the help.
It is what outputs on the display when you type ps ax in the console

ps is the command
ax are the arguments

~

vertex
Posts: 5
Joined: Tue 14 Jun 2011, 01:04

#11 Post by vertex »

Acutally, it is the opposite, when run from the command box, it is pts/0, which works. from the icon it is tty1. What can I do to fix that?

Thanks again.

User avatar
Dougal
Posts: 2502
Joined: Wed 19 Oct 2005, 13:06
Location: Hell more grotesque than any medieval woodcut

#12 Post by Dougal »

vertex wrote:Acutally, it is the opposite, when run from the command box, it is pts/0, which works. from the icon it is tty1. What can I do to fix that?
That's what I said... everything run from the WM ends up under tty1.

As far as I understand, /dev/pts is the old (deprecated) way of doing things, so theoretically you'll need to change th program to use /dev/tty. There might be some way to force it to use /dev/pts when run from the WM (via a special program), but I don't know it...
However, something you can do is write a simple shell-wrapper that will run the program from a terminal:

Code: Select all

#!/bin/sh
exec rxvt -e "graphcan"
then you just need to have that run from the menu entry.
What's the ugliest part of your body?
Some say your nose
Some say your toes
But I think it's your mind

vertex
Posts: 5
Joined: Tue 14 Jun 2011, 01:04

#13 Post by vertex »

That won't help. The problem is that the keyboard input is recognized in the terminal only when the terminal is in focus. If Graphcan is in focus, it doesn't work, even if launched from the terminal.

I probably need to hack the stdin.

Post Reply