Find the answer to your Linux question:
Results 1 to 3 of 3
This is actually for mips embedded linux. If I'm posting to the wrong place, I aplogize.... I'm trying to code a simple x application, but when I call the XNextEvent() ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2006
    Posts
    5

    Xlib - XNextEvent never sees an event


    This is actually for mips embedded linux. If I'm posting to the wrong place, I aplogize....

    I'm trying to code a simple x application, but when I call the XNextEvent() method, I never get any events from the keyboard.

    Now, the application is threaded, but all the X stuff is done in the same thread. Code is C.

    ok.

    Here's the initialization:

    Code:
    g_display = XOpenDisplay(":0.0");
        if (g_display == NULL) {
     	EVODRV_log_post( IDERR(ERRCODE_FAILURE_DURING_INIT), "init_Xlib::cannot connect X server") ;
    	return FALSE;
        }
        screen_num = DefaultScreen(g_display);
        g_display_width = DisplayWidth(g_display, screen_num);
        g_display_height = DisplayHeight(g_display, screen_num);
        /* find bit depth */
        g_vis = DefaultVisual (g_display, screen_num);
        g_win = XCreateSimpleWindow(g_display, RootWindow(g_display, screen_num),
                                0, 0, g_display_width, g_display_height, 0,555,1221672);
        XMapWindow(g_display, g_win);
        XFlush(g_display);
        create_gc(g_display, g_win, 0);	
        XSelectInput(g_display, g_win, KeyPressMask | KeyReleaseMask | ButtonPressMask | StructureNotifyMask | ExposureMask);
        XMapWindow(g_display, g_win);
        oriImg = XGetImage(g_display,g_win,0,0,g_display_width,g_display_height,AllPlanes,ZPixmap);
    oPlane.oPixmap.format = PIX_32_8888;
        oPlane.oPlaneInfo.format = GFX_PLANE_FORMAT_RGBA;
        /* allocate the offline buffer */
        g_memSize = oriImg->width * oriImg->height * oriImg->bits_per_pixel/8;
        oPlane.oPixmap.pixbuf = (char *) mem_alloc (g_memSize);
       /* Clear the double buffer */
        memset(oPlane.oPixmap.pixbuf, 0, g_memSize);
        /* create offline screen buffer */
        g_img = XCreateImage (g_display,g_vis, oriImg->depth,oriImg->format,oriImg->xoffset,(
    	char *) oPlane.oPixmap.pixbuf,oriImg->width,oriImg->height,oriImg->bitmap_pad, oriImg->bytes_per_line);
    /* Fill the rest of the pixmap structure */
        oPlane.oPixmap.width = oriImg->width;
        oPlane.oPixmap.height = oriImg->height;
        oPlane.oPixmap.pitch = oriImg->width * oriImg->bits_per_pixel/8;
    
        /* Store any other required information */
        oDisplayInfo.res_width = oriImg->width;
        oDisplayInfo.res_height = oriImg->height;
    
        oPlane.oPlaneInfo.flags = GFX_PLANE_FLAG_DRAWABLE;
        oPlane.oPlaneInfo.type = GFX_PLANE_ONSCREEN_GRAPHICS;
        oPlane.oPlaneInfo.width = oriImg->width;
        oPlane.oPlaneInfo.height = oriImg->height;
        oPlane.oPlaneInfo.view.width = oPlane.oPlaneInfo.width;
        oPlane.oPlaneInfo.view.height = oPlane.oPlaneInfo.height;
        oPlane.oPlaneInfo.drawprims = get_gfx_draw_functions_for(&(oPlane.oPixmap));
    
        /* Fill in the draw context */
        oDrawCtx.pixmap = &(oPlane.oPixmap);
        oPlane.oPlaneInfo.drawprims->init_gdc(&oDrawCtx, NULL);
        oPlane.oPlaneInfo.drawprims->set_color(&oDrawCtx, 0);
        oPlane.oPlaneInfo.drawprims->set_alpha_rule(&oDrawCtx, SRC);
        XUnlockDisplay(g_display);
    You can sell I have the masks for key press in the XSelectEvent() call.

    Then, the event handling portion:

    Code:
    if (XEventsQueued(g_display, QueuedAfterReading) > 0)
    	{
    		XNextEvent(g_display, &report);
    		switch (report.type) 
    		{
    	   		case KeyPress:
                                      fprintf( stderr, "Key Press Event\n");
    		  		 /* Respond to buttonpress, probably depending on
    		   		 * which window is reported in report.xbutton.window */
    	   		break;
    	   		case KeyRelease:
                                      fprintf( stderr, "Key Release Event\n");
    		  		 /* Respond to buttonpress, probably depending on
    		   		 * which window is reported in report.xbutton.window */
    	   		break;
    			default:
    	   			fprintf( stderr, "Event being thrown away\n");
    			   /* All *Notify events except ConfigureNotify will
    			    * be thrown away; they are not needed by most
    			    * applications but are sent because ConfigureNotify
    			    * canít be selected independently */
    	   		break;
    		}
    No matter what I do, I never see the events. Am I missing something? Is there anything else I can do?

  2. #2
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    Please place your code in [code] sections next time!

    I'll critique your code now...
    Code:
    You should probably pass NULL instead of ":0.0"...it's more general.
    g_display = XOpenDisplay(":0.0");
    if (g_display == NULL) {
    	EVODRV_log_post( IDERR(ERRCODE_FAILURE_DURING_INIT), "init_Xlib::cannot connect X server") ;
    	return FALSE;
    }
    screen_num = DefaultScreen(g_display);
    g_display_width = DisplayWidth(g_display, screen_num);
    g_display_height = DisplayHeight(g_display, screen_num);
    /* find bit depth */
    g_vis = DefaultVisual (g_display, screen_num);
    g_win = XCreateSimpleWindow(g_display, RootWindow(g_display, screen_num),
    		0, 0, g_display_width, g_display_height, 0,555,1221672);
    XMapWindow(g_display, g_win);
    You don't need this here because XNextEvent flushes the queue
    XFlush(g_display);
    create_gc(g_display, g_win, 0);
    XSelectInput(g_display, g_win, KeyPressMask | KeyReleaseMask | ButtonPressMask | StructureNotifyMask | ExposureMask);
     You already mapped it! (This won't cause an error though)
    XMapWindow(g_display, g_win);
    oriImg = XGetImage(g_display,g_win,0,0,g_display_width,g_di splay_height,AllPlanes,ZPixmap);
    oPlane.oPixmap.format = PIX_32_8888;
    oPlane.oPlaneInfo.format = GFX_PLANE_FORMAT_RGBA;
    /* allocate the offline buffer */
    g_memSize = oriImg->width * oriImg->height * oriImg->bits_per_pixel/8;
    oPlane.oPixmap.pixbuf = (char *) mem_alloc (g_memSize);
    /* Clear the double buffer */
    memset(oPlane.oPixmap.pixbuf, 0, g_memSize);
    /* create offline screen buffer */
    g_img = XCreateImage (g_display,g_vis, oriImg->depth,oriImg->format,oriImg->xoffset,(
    			char *) oPlane.oPixmap.pixbuf,oriImg->width,oriImg->height,oriImg->bitmap_pad, oriImg->bytes_per_line);
    /* Fill the rest of the pixmap structure */
    oPlane.oPixmap.width = oriImg->width;
    oPlane.oPixmap.height = oriImg->height;
    oPlane.oPixmap.pitch = oriImg->width * oriImg->bits_per_pixel/8;
    
    /* Store any other required information */
    oDisplayInfo.res_width = oriImg->width;
    oDisplayInfo.res_height = oriImg->height;
    
    oPlane.oPlaneInfo.flags = GFX_PLANE_FLAG_DRAWABLE;
    oPlane.oPlaneInfo.type = GFX_PLANE_ONSCREEN_GRAPHICS;
    oPlane.oPlaneInfo.width = oriImg->width;
    oPlane.oPlaneInfo.height = oriImg->height;
    oPlane.oPlaneInfo.view.width = oPlane.oPlaneInfo.width;
    oPlane.oPlaneInfo.view.height = oPlane.oPlaneInfo.height;
    oPlane.oPlaneInfo.drawprims = get_gfx_draw_functions_for(&(oPlane.oPixmap));
    
    /* Fill in the draw context */
    oDrawCtx.pixmap = &(oPlane.oPixmap);
    oPlane.oPlaneInfo.drawprims->init_gdc(&oDrawCtx, NULL);
    oPlane.oPlaneInfo.drawprims->set_color(&oDrawCtx, 0);
    oPlane.oPlaneInfo.drawprims->set_alpha_rule(&oDrawCtx, SRC);
    XUnlockDisplay(g_display);
    
    I'd use XPending in a while loop, inside of an infinite while loop
    if (XEventsQueued(g_display, QueuedAfterReading) > 0)
    {
    	XNextEvent(g_display, &report);
    	switch (report.type)
    	{
    		case KeyPress:
    			fprintf( stderr, "Key Press Event\n");
    			/* Respond to buttonpress, probably depending on
    			 * which window is reported in report.xbutton.window */
    			break;
    		case KeyRelease:
    			fprintf( stderr, "Key Release Event\n");
    			/* Respond to buttonpress, probably depending on
    			 * which window is reported in report.xbutton.window */
    			break;
    		default:
    			fprintf( stderr, "Event being thrown away\n");
    			/* All *Notify events except ConfigureNotify will
    			 * be thrown away; they are not needed by most
    			 * applications but are sent because ConfigureNotify
    			 * canŅt be selected independently */
    			break;
    	}
    }
    I hope this helps!
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

  3. #3
    Just Joined!
    Join Date
    Apr 2006
    Posts
    5
    Thanks for the reply. I edited the post to wrap the code with the tags. Kind of late, but oh well.

    I tried using NULL when I did the XOpenDisplay, but the application would crash. Not sure why, but maybe the MIPS platform doesn't like it.....

    Nice nickname... javasnob... I'm a java guy as well, and prefer it over c/c++.

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •