Author Topic: how Get the power Switch Pressed Status in SDL? (EDIT2: CODE INCLUDED)  (Read 3246 times)

RodrigoCard (OP)

  • Posts: 314
    • Mac Joystick Apps
hi people  :),

I read somewhere in this forum that the Power Key is mapped to SDLK_UNKNOWN in SDL.
so, SDLK_UNKNOWN = 0 and pressing power does not fire the KEY_DOWN even
and if I press the POWER swith AND another button together, it works, but not alone.
Is this normal?

I want to use the Power switch alone :/

EDIT: Reformulating the topic:

here is my complete code:

dingux.h
Code: [Select]
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "jz4740.h"

#define DINGOO_BUTTON_LEFT          SDLK_LEFT
#define DINGOO_BUTTON_RIGHT         SDLK_RIGHT
#define DINGOO_BUTTON_UP            SDLK_UP
#define DINGOO_BUTTON_DOWN          SDLK_DOWN

#define DINGOO_BUTTON_Y             SDLK_LSHIFT
#define DINGOO_BUTTON_A             SDLK_LCTRL
#define DINGOO_BUTTON_X             SDLK_SPACE
#define DINGOO_BUTTON_B             SDLK_LALT

#define DINGOO_BUTTON_L             SDLK_TAB
#define DINGOO_BUTTON_R             SDLK_BACKSPACE

#define DINGOO_BUTTON_START         SDLK_RETURN
#define DINGOO_BUTTON_SELECT        SDLK_ESCAPE
#define DINGOO_BUTTON_POWER         SDLK_UNKNOWN
#define DINGOO_BUTTON_HOLD            SDLK_PAUSE

enum DingooKeys {
    KLEFT,
    KRIGHT,
    KUP,
    KDOWN,
    
    KY,
    KA,
    KX,
    KB,
    
    KL,
    KR,
    
    KSTART,
    KSELECT,
    KPOWER,
    KHOLD,
    
    KTOTAL // the last item in a sequential enum is its size
};

int key[KTOTAL] = {
    0,0,0,0, 0,0,0,0,
    0,0, 0,0,0,0
};

void set_brightness(int value)
{
    unsigned long dev=open("/proc/jz/lcd_backlight", O_RDWR);
    if (dev)
    {
        char backlight[4];
        if (value > 99) value = 99;
        if (value < 0) value = 0;
        snprintf(backlight, 3, "%d", value);
        backlight[3]='\0';
        write(dev, backlight, 4);
        close(dev);
        sync();
    }
}

int get_brightness(void)
{
    unsigned long dev=open("/proc/jz/lcd_backlight", O_RDWR);
    if (dev)
    {
        char backlight[4]={ 0, 0, 0, 0 };
        read(dev, backlight, 4);
        backlight[3]='\0';
        close(dev);
        return 1+atoi(backlight);
    }
    return 0;
}

main.c
Code: [Select]
/* Dingoo Test Program (incomplete)
    Rodrigo Cardoso
 
 License: Do Whatever you want, no warranties. ;)
*/
  
#include <stdio.h>
#include <SDL/SDL.h>

#include "dingux.h"

#define RGB(r,g,b) SDL_MapRGB(screen->format, r, g, b)

SDL_Surface    *screen;
SDL_Event    event;

void SDL_Initialize()
{
    Uint32 initflags = SDL_INIT_VIDEO;  /* See documentation for details */
    Uint8  video_bpp = 16;
    Uint32 videoflags = SDL_SWSURFACE;
    
    /* Initialize the SDL library */
    if ( SDL_Init(initflags) < 0 ) {
        fprintf(stderr, "Couldn't initialize SDL: %s\n",
                SDL_GetError());
        exit(1);
    }
    
    screen = SDL_SetVideoMode(320,240, video_bpp, videoflags);
    if (screen == NULL) {
        fprintf(stderr, "Couldn't set 320x240x%d video mode: %s\n",
                video_bpp, SDL_GetError());
        SDL_Quit();
        exit(2);
    }    
}

void drawRect(SDL_Surface *dest,
              int x, int y, int w, int h, Uint32 color)
{
    SDL_Rect rect = { x,y,w,h };
    SDL_FillRect(dest, &rect, color);
}

void drawBg(SDL_Surface *bg)
{
    SDL_FillRect(bg, NULL, 0x000000); //BG
    
    // cross
    drawRect(bg, 32, 96, 48, 16, 0xFFFFFF);
    drawRect(bg, 48, 80, 16, 48, 0xFFFFFF);
    
    // face buttons
    // TODO:change to circles
    drawRect(bg, 256, 80, 16, 14, 0xFFFFFF);
    drawRect(bg, 256, 112, 16, 14, 0xFFFFFF);
    drawRect(bg, 240, 96, 16, 14, 0xFFFFFF);
    drawRect(bg, 272, 96, 16, 14, 0xFFFFFF);
    
    // L R
    drawRect(bg, 48, 48, 16, 16, 0xFFFFFF);
    drawRect(bg, 256, 48, 16, 16, 0xFFFFFF);
    
    // start select etc
    drawRect(bg, 48, 144, 16, 10, 0xFFFFFF);
    drawRect(bg, 256, 144, 16, 10, 0xFFFFFF);
    
    // power and hold
    drawRect(bg, 304, 80, 8, 16, 0xFFFFFF);
    drawRect(bg, 304, 112, 8, 16, 0xFFFFFF);
    
    // screen
    drawRect(bg, 104, 72, 112, 80, 0xFFFFDD);
}

int main(int argc, char *argv[])
{
    int            done = 0;
    int            brightness = 50;
    
    SDL_Initialize();
    
    SDL_ShowCursor(SDL_DISABLE);
    
    SDL_Rect    screenRect = {0,0,320,240};
    Uint32        pressedColor = RGB(255,0,0);
    SDL_Surface *bg= SDL_CreateRGBSurface(SDL_SWSURFACE, 320, 240, 16, 0, 0, 0, 0);
    drawBg(bg);
    
    // run while not done
    while ( !done ) {
        
        SDL_UpperBlit(bg, &screenRect , screen, &screenRect);
        
        // directional buttons
        if (key[KLEFT])
            drawRect(screen, 32, 96, 16, 16, pressedColor);
        if (key[KRIGHT])
            drawRect(screen, 64, 96, 16, 16, pressedColor);
        if (key[KUP])
            drawRect(screen, 48, 80, 16, 16, pressedColor);
        if (key[KDOWN])
            drawRect(screen, 48, 112, 16, 16, pressedColor);
        
        // face buttons
        if (key[KY])
            drawRect(screen, 240, 96, 16, 16, pressedColor);
        if (key[KA])
            drawRect(screen, 272, 96, 16, 16, pressedColor);
        if (key[KX])
            drawRect(screen, 256, 80, 16, 16, pressedColor);
        if (key[KB])
            drawRect(screen, 256, 112, 16, 16, pressedColor);
        
        // L R
        if (key[KL])
            drawRect(screen, 48, 48, 16, 16, pressedColor);
        if (key[KR])
            drawRect(screen, 256, 48, 16, 16, pressedColor);
            
                    
        // Start / Select / Power / Hold
        if (key[KSTART])
            drawRect(screen, 256, 144, 16, 16, pressedColor);
        if (key[KSELECT])
            drawRect(screen, 48, 144, 16, 16, pressedColor);
        if (key[KPOWER])
            drawRect(screen, 304, 80, 8, 16, pressedColor);
        if (key[KHOLD])
            drawRect(screen, 304, 112, 8, 16, pressedColor);
        
        // Start+Select Exits the program
        if (key[KSTART] && key[KSELECT]) {
            done = 1;
        }
        
        if (key[KPOWER] && key[KUP]) {
            set_brightness(++brightness);
        }
        
        if (key[KPOWER] && key[KDOWN]) {
            set_brightness(--brightness);
        }
        
        // Draw everything on the screen
        SDL_UpdateRect(screen, 0, 0, 320, 240);  
        

        /* Check for events */
        while ( SDL_PollEvent(&event) ) {
            switch (event.type) {

                // Set the key's pressed state to true
                case SDL_KEYDOWN:
                    switch (event.key.keysym.sym)
                    {
                        case DINGOO_BUTTON_LEFT:
                            key[KLEFT] = 1;
                            break;
                        case DINGOO_BUTTON_RIGHT:
                            key[KRIGHT] = 1;
                            break;
                        case DINGOO_BUTTON_UP:
                            key[KUP] = 1;
                            break;
                        case DINGOO_BUTTON_DOWN:
                            key[KDOWN] = 1;
                            break;
                        case DINGOO_BUTTON_Y:
                            key[KY] = 1;
                            break;
                        case DINGOO_BUTTON_A:
                            key[KA] = 1;
                            break;
                        case DINGOO_BUTTON_X:
                            key[KX] = 1;
                            break;
                        case DINGOO_BUTTON_B:
                            key[KB] = 1;
                            break;
                        case DINGOO_BUTTON_L:
                            key[KL] = 1;
                            break;
                        case DINGOO_BUTTON_R:
                            key[KR] = 1;
                            break;
                        case DINGOO_BUTTON_START:
                            key[KSTART] = 1;
                            break;
                        case DINGOO_BUTTON_SELECT:
                            key[KSELECT] = 1;
                            break;
                        case DINGOO_BUTTON_POWER:
                            printf("POWER DOWN! = %d\n", (int)event.key.keysym.sym);
                            key[KPOWER] = 1;
                            break;
                        case DINGOO_BUTTON_HOLD:
                            key[KHOLD] = 1;
                            //printf("HOLD! = %d\n", (int)event.key.keysym.sym);
                            break;
                        //case SDLK_UNKNOWN:
                        default:
                            printf("DOWN KEYNUM: %d\n", (int)event.key.keysym.sym);
                            break;
                    }
                    break;
                
                // Set the key's pressed state to false
                case SDL_KEYUP:
                    switch (event.key.keysym.sym)
                    {
                        case DINGOO_BUTTON_LEFT:
                            key[KLEFT] = 0;
                            break;
                        case DINGOO_BUTTON_RIGHT:
                            key[KRIGHT] = 0;
                            break;
                        case DINGOO_BUTTON_UP:
                            key[KUP] = 0;
                            break;
                        case DINGOO_BUTTON_DOWN:
                            key[KDOWN] = 0;
                            break;
                        case DINGOO_BUTTON_Y:
                            key[KY] = 0;
                            break;
                        case DINGOO_BUTTON_A:
                            key[KA] = 0;
                            break;
                        case DINGOO_BUTTON_X:
                            key[KX] = 0;
                            break;
                        case DINGOO_BUTTON_B:
                            key[KB] = 0;
                            break;
                        case DINGOO_BUTTON_L:
                            key[KL] = 0;
                            break;
                        case DINGOO_BUTTON_R:
                            key[KR] = 0;
                            break;
                        case DINGOO_BUTTON_START:
                            key[KSTART] = 0;
                            break;
                        case DINGOO_BUTTON_SELECT:
                            key[KSELECT] = 0;
                            break;
                        case DINGOO_BUTTON_POWER:
                            printf("POWER UP! = %d\n", (int)event.key.keysym.sym);
                            key[KPOWER] = 0;
                            break;
                        case DINGOO_BUTTON_HOLD:
                            key[KHOLD] = 0;
                            break;
                        default:
                            printf("UP KEYNUM: %d\n", (int)event.key.keysym.sym);
                            break;
                    }
                    break;
                
                case SDL_QUIT:
                    done = 1; // exit
                    break;
                
                default:
                    printf("EVENT: %d\n", (int)event.type);
                    break;
            }
        }
    }
    
    /* Clean up the SDL library */
    SDL_Quit();
    return(0);
}

Basically, I'm doing a program to test dingoo functions.
Currently you can only test the controls and the brightness adjustment (Power+UP or DOWN).

Can someone point me what to do?

Thanks
« Last Edit: October 14, 2010, 04:59:58 am by RodrigoCard »

RodrigoCard (OP)

  • Posts: 314
    • Mac Joystick Apps
Re: how Get the power Switch Pressed Status in SDL? (EDIT2: CODE INCLUDED)
« Reply #1 on: October 14, 2010, 05:00:45 am »
So, I included the code, may be easier to help =P
« Last Edit: October 14, 2010, 06:43:40 pm by RodrigoCard »

SiENcE

  • Posts: 653
    • Crank Gaming (Dingux Apps)
Re: how Get the power Switch Pressed Status in SDL? (EDIT2: CODE INCLUDED)
« Reply #2 on: October 14, 2010, 08:54:55 am »
:) Can we expect a nice app?

pcercuei

  • Posts: 1732
    • My devblog
Re: how Get the power Switch Pressed Status in SDL? (EDIT2: CODE INCLUDED)
« Reply #3 on: October 14, 2010, 11:50:39 am »
Are you sure it's not SDLK_POWER?
Or it is only for OpenDingux maybe...

RodrigoCard (OP)

  • Posts: 314
    • Mac Joystick Apps
Re: how Get the power Switch Pressed Status in SDL? (EDIT2: CODE INCLUDED)
« Reply #4 on: October 14, 2010, 06:59:52 pm »
:) Can we expect a nice app?

I think so :)
It will be useful for testing dingoos easily all in one single app. :)
I want to include battery info and mic test after I get rid of this power switch problem. And something to check how far you can overclock it with no problems.

I want also to write this code in a didatic way, and include it in the wiki for begginers in dingoo coding. This may encourage some people to get in the dingoo scene and make some games... the scene seems a little slow these days :P

I attached the current binary in this post  :)

Are you sure it's not SDLK_POWER?
Or it is only for OpenDingux maybe...

No, not in the current stable kernel, don't know about opendingux...
The power switch pushed alone does not fire any event at all.

Only in conjunction with another buttons, and it can get stuck if you let the other button off before.

Maybe acessing it directly? I dont know how to do this ... lets see..

joyrider

  • Posts: 220
    • Willems Soft
Re: how Get the power Switch Pressed Status in SDL? (EDIT2: CODE INCLUDED)
« Reply #5 on: October 14, 2010, 10:54:28 pm »
read up on accessing /dev/event0 (io the linux event thingie) it can access all kind of things and it would probably pick up the power switch... Look up the sources i released for the fbgrab deamon it uses /dev/event, so all you need to do is created a little program that does the same but prints out values on each event (button) press, and see if the power switch generates such event(s)

RodrigoCard (OP)

  • Posts: 314
    • Mac Joystick Apps
Re: how Get the power Switch Pressed Status in SDL? (EDIT2: CODE INCLUDED)
« Reply #6 on: October 15, 2010, 01:26:54 am »
Now I'm looking for info about audio recording, looks like the audio in linux is a bit of a mess =P
Is there any app in dingux that uses the mic, so I can look the code?

read up on accessing /dev/event0 (io the linux event thingie) it can access all kind of things and it would probably pick up the power switch... Look up the sources i released for the fbgrab deamon it uses /dev/event, so all you need to do is created a little program that does the same but prints out values on each event (button) press, and see if the power switch generates such event(s)

thanks for the info. I will check this later.
« Last Edit: October 15, 2010, 01:30:03 am by RodrigoCard »

joyrider

  • Posts: 220
    • Willems Soft
Re: how Get the power Switch Pressed Status in SDL? (EDIT2: CODE INCLUDED)
« Reply #7 on: October 15, 2010, 01:18:26 pm »
sorry but i made a mistake, it should be /dev/input0 i think , not /dev/event0 event0 is something else anyway the fbgrab sources are a good start , they should be supplied with the binary in the archive ;)

audio recording can be done and is not that hard... it's also done using a /dev/dsp or /dev/audio (or perhaps another one) interface. you should look in the soundcard.h header file it's all explained there i think
« Last Edit: October 15, 2010, 01:21:43 pm by joyrider »

 

Post a new topic