XUngrabKeyboard problem ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

I try to make a little program based on xlib and xosd, it's a daemon
listening on the keyboard made for example to increase volume level or
other things.

But i have a problem to Ungrab the keyboard, i made a sample of what i
would like to do, if someone see where is the problem 

Thanks ! :)

Romain

(gcc devxosd.c -o devxosd -lxosd)

/*
 * XOSD TEST
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <linux/types.h>
 
#include <xosd.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/Xutil.h>
 
#include <sys/soundcard.h>
 
 
int getSound (void)
{
     int fd_mixer, vol;
      
     if ((fd_mixer = open ("/dev/mixer", O_RDWR)) < 0) {
          perror ("/dev/mixer");
          return -1;
     }
      
     if (ioctl (fd_mixer, SOUND_MIXER_READ_VOLUME, &vol) == -1) {
          perror ("ioctl");
          return -1;
     }
 
     if (close(fd_mixer) == -1) {
          perror("close");
          return -1;
     }
      
     return vol&255;
}

int setSound (int v)
{
     int fd_mixer, vol;
      
     if ((fd_mixer = open ("/dev/mixer", O_RDWR)) < 0) {
          perror ("/dev/mixer");
          return -1;
     }
      
     vol = (v << 8) | v ;
      
     if (ioctl (fd_mixer, SOUND_MIXER_WRITE_VOLUME, &vol) == -1) {
          perror ("ioctl");
          return -1;
     }
                                                                                                                                                          
     if (close(fd_mixer) == -1) {
          perror("close");
          return -1;
     }
                                                                                                                                                          
     return 0;
}

int volume (int level)
{
     Display *disp = NULL;
     Window root;
                                                                                                                                                          
     disp = XOpenDisplay(NULL);
     if (!disp) {
          fprintf(stderr, "Failed to open display\n");
          return -1;
     }
                                                                                                                                                          
     root = DefaultRootWindow(disp);
                                                                                                                                                          
                                                                                                                                                          
     /* Xlib */
     int nbre;
     char chaine[2];
     KeySym touche;
                                                                                                                                                          
     /* Xosd */
     xosd *osd = NULL;
     char *font =
"-adobe-helvetica-bold-r-normal-*-*-240-*-*-p-*-iso8859-1";
     char *title = "Volume";
                                                                                                                                                          
     int isUp = 1;
                                                                                                                                                          
     osd = xosd_create (2);
     if (osd == NULL) {
          perror ("Could not create \"osd\"");
          return -1;
     }
                                                                                                                                                          
     if (xosd_set_font (osd, font)) {
          printf ("Cound not set font\n");
          return -1;
     }
                                                                                                                                                          
     xosd_set_shadow_offset (osd, 2);
     xosd_set_colour (osd, "white");
                                                                                                                                                          
     /* Position */
     xosd_set_pos (osd, XOSD_bottom);
     xosd_set_vertical_offset (osd, 20);
     xosd_set_align (osd, XOSD_center);
     xosd_set_horizontal_offset (osd, 0);
     xosd_set_bar_length(osd, 20);
                                                                                                                                                          
     xosd_display (osd, 0, XOSD_string, title);
     xosd_display (osd, 1, XOSD_percentage, level);
                                                                                                                                                          
     XGrabKeyboard (disp, root, True, GrabModeAsync, GrabModeAsync,
CurrentTime);

     while (isUp) {
          XEvent ev;
                                                                                                                                                          
          XNextEvent (disp, &ev);
          if (ev.type == KeyPress) {
                                                                                                                                                          
               nbre = XLookupString (&ev.xkey, chaine, 2, &touche, 0);
               chaine[nbre] = 0;
                                                                                                                                                          
               switch (touche) {
                                                                                                                                                          
               case XK_q:
               case XK_Q:
               case XK_Escape:
                    isUp = 0;
                    break;
                                                                                                                                                          
               case XK_Right:
                    level = (level > 95 ? 100 : level + 5 );
                    setSound(level);
                    xosd_display (osd, 1, XOSD_percentage, level);
                    break;
                                                                                                                                                          
               case XK_Left:
                    level = (level < 5 ? 0 : level - 5 );
                    setSound(level);
                    xosd_display (osd, 1, XOSD_percentage, level);
                    break;
                                                                                                                                                          
               default:
                    break;
               }
          }
     }
                                                                                                                                                          
     XUngrabKeyboard (disp, CurrentTime);
     xosd_destroy (osd);
     return 0;
}
                                                                                                                                                          
                                                                                                                                                          
int main (int argc, char *argv[])
{
     volume (getSound());
     printf ("I want the keyboard back !\n");
     sleep (20);
     return 1;
}


-- 
Romain <buchu@xxxxxxxxxxxx>
/*
 * XOSD TEST 
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <linux/types.h>

#include <xosd.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/Xutil.h>

#include <sys/soundcard.h>


int getSound (void)
{
     int fd_mixer, vol;
     
     if ((fd_mixer = open ("/dev/mixer", O_RDWR)) < 0) {
	  perror ("/dev/mixer");
	  return -1;
     }
     
     if (ioctl (fd_mixer, SOUND_MIXER_READ_VOLUME, &vol) == -1) {
	  perror ("ioctl");
	  return -1;
     }

     if (close(fd_mixer) == -1) {
	  perror("close");
	  return -1;
     }
     
     return vol&255;
}

int setSound (int v)
{
     int fd_mixer, vol;
     
     if ((fd_mixer = open ("/dev/mixer", O_RDWR)) < 0) {
	  perror ("/dev/mixer");
	  return -1;
     }
     
     vol = (v << 8) | v ;
     
     if (ioctl (fd_mixer, SOUND_MIXER_WRITE_VOLUME, &vol) == -1) {
	  perror ("ioctl");
	  return -1;
     }
     
     if (close(fd_mixer) == -1) {
	  perror("close");
	  return -1;
     }

     return 0;
}


int volume (int level) 
{
     Display *disp = NULL;
     Window root;
     
     disp = XOpenDisplay(NULL);
     if (!disp) {
	  fprintf(stderr, "Failed to open display\n");
	  return -1;
     }
     
     root = DefaultRootWindow(disp);
     
     
     /* Xlib */
     int nbre;
     char chaine[2];
     KeySym touche;

     /* Xosd */
     xosd *osd = NULL;
     char *font = "-adobe-helvetica-bold-r-normal-*-*-240-*-*-p-*-iso8859-1";
     char *title = "Volume";
          
     int isUp = 1;
     
     osd = xosd_create (2);
     if (osd == NULL) {
	  perror ("Could not create \"osd\"");
	  return -1;
     }
     
     if (xosd_set_font (osd, font)) {
	  printf ("Cound not set font\n");
	  return -1;
     }
       
     xosd_set_shadow_offset (osd, 2);
     xosd_set_colour (osd, "white");
     
     /* Position */
     xosd_set_pos (osd, XOSD_bottom);
     xosd_set_vertical_offset (osd, 20);
     xosd_set_align (osd, XOSD_center);
     xosd_set_horizontal_offset (osd, 0);
     xosd_set_bar_length(osd, 20);
          
     xosd_display (osd, 0, XOSD_string, title);  
     xosd_display (osd, 1, XOSD_percentage, level);		    
     
     XGrabKeyboard (disp, root, True, GrabModeAsync, GrabModeAsync, CurrentTime);     
     
     
     while (isUp) {
	  XEvent ev;	  
	  
	  XNextEvent (disp, &ev); 
	  if (ev.type == KeyPress) {
	       	       	       
	       nbre = XLookupString (&ev.xkey, chaine, 2, &touche, 0);	       
	       chaine[nbre] = 0;

	       switch (touche) { 
	       
	       case XK_q:
	       case XK_Q:
	       case XK_Escape:
		    isUp = 0;
		    break;

	       case XK_Right:
		    level = (level > 95 ? 100 : level + 5 );
		    setSound(level);
		    xosd_display (osd, 1, XOSD_percentage, level);
		    break;

	       case XK_Left:
		    level = (level < 5 ? 0 : level - 5 ); 
		    setSound(level);
		    xosd_display (osd, 1, XOSD_percentage, level);
		    break;

	       default:
		    break;
	       }
	  } 
     }	  
     
     XUngrabKeyboard (disp, CurrentTime);         
     xosd_destroy (osd);     
     return 0;
}


int main (int argc, char *argv[])
{
     volume (getSound());
     printf ("I want the keyboard back !\n");
     sleep (20);
     return 1;
}

[Index of Archives]     [X Forum]     [Xorg]     [XFree86 Newbie]     [IETF Announce]     [Security]     [Font Config]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux Kernel]

  Powered by Linux