Difference between revisions of "Решения"

From Ilianko
Line 44: Line 44:
 
     printf("%ls \n", words[i]);
 
     printf("%ls \n", words[i]);
 
      
 
      
 +
  return 0;
 +
}
 +
</pre></code>
 +
 +
 +
<code><pre>#include <stdio.h>
 +
#include <stdlib.h>
 +
#include <string.h>
 +
#include <unistd.h>
 +
#include <fcntl.h>
 +
#include <errno.h>
 +
#include <linux/input.h>
 +
#include <linux/uinput.h>
 +
 +
 +
int main(void)
 +
{
 +
    int                    fd, kb_fd;
 +
    struct uinput_user_dev uidev;
 +
    struct input_event    ev, kb_ev;
 +
    int                    dx, dy;
 +
   
 +
 +
    fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
 +
   
 +
    if (fd == -1) {
 +
      puts("Can't open file");
 +
    return 1;
 +
    }
 +
   
 +
   
 +
    ioctl(fd, UI_SET_EVBIT, EV_KEY);
 +
    ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
 +
    ioctl(fd, UI_SET_EVBIT, EV_REL) ;
 +
    ioctl(fd, UI_SET_RELBIT, REL_X);
 +
    ioctl(fd, UI_SET_RELBIT, REL_Y);
 +
 
 +
    memset(&uidev, 0, sizeof(uidev));
 +
    snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
 +
    uidev.id.bustype = BUS_USB;
 +
    uidev.id.vendor  = 0x1;
 +
    uidev.id.product = 0x1;
 +
    uidev.id.version = 1;
 +
 +
    if(write(fd, &uidev, sizeof(uidev)) < 0)
 +
        {
 +
      puts("Can't open file");
 +
    return 1;
 +
    }
 +
   
 +
if(ioctl(fd, UI_DEV_CREATE) < 0)
 +
      {
 +
      puts("Can't open file");
 +
    return 1;
 +
    }
 +
 +
    kb_fd = open("/dev/input/event3", O_RDWR);
 +
    if (kb_fd < 0)
 +
    { 
 +
puts("cant open kb");
 +
return 1;
 +
    }
 +
   
 +
    while (1)
 +
    {
 +
 +
    read(kb_fd, &kb_ev, sizeof(struct input_event));
 +
 +
    if( kb_ev.type == 1 && (kb_ev.value == 2 || kb_ev.value == 1))
 +
    {
 +
 +
memset(&ev, 0, sizeof(ev));
 +
    gettimeofday(&ev.time, NULL);
 +
    usleep(100);
 +
switch (kb_ev.code)
 +
  {
 +
    case 105:
 +
      ev.type = EV_REL;
 +
          ev.code = REL_X;
 +
          ev.value = -5;
 +
    break;
 +
   
 +
    case 106:
 +
      ev.type = EV_REL;
 +
          ev.code = REL_X;
 +
          ev.value = 5;
 +
    break;
 +
   
 +
    case 103:
 +
      ev.type = EV_REL;
 +
          ev.code = REL_Y;
 +
          ev.value = -5;
 +
    break;
 +
   
 +
    case 108:
 +
      ev.type = EV_REL;
 +
          ev.code = REL_Y;
 +
          ev.value = 5;
 +
    break;
 +
   
 +
        default:
 +
          ev.type = EV_REL;
 +
          ev.code = REL_X;
 +
          ev.value = -5;
 +
      }
 +
 +
    write(fd, &ev, sizeof(ev));
 +
    usleep(100);
 +
   
 +
    gettimeofday(&ev.time, NULL);
 +
    ev.type = 0;
 +
    ev.code = 0;
 +
    ev.value = 0;
 +
    write(fd, &ev, sizeof(ev));
 +
    }
 +
  } 
 +
   
 +
   
 +
 +
  close(fd);
 
   return 0;
 
   return 0;
 
}
 
}
 
</pre></code>
 
</pre></code>

Revision as of 19:49, 13 April 2011

Задача 5.8

/*********************************************************************\
 * Title: wide char string bubble sort
 * Author: ilianko
 * 
\*********************************************************************/

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

#define BROI_DUMI 4
#define DYLZHINA_NA_DUMA 16 // max broj simvoli

int main ()
{
 
  setlocale(LC_CTYPE, ""); // Зареждане на локалните параметри
  
  //wchar_t words[DUMI][DYLZHINA_NA_DUMА] = {L"strawberry",L"orange",L"mango",L"apple"};
  wchar_t words[BROI_DUMI][DYLZHINA_NA_DUMA] = {L"ягода",L"портокал",L"ябълка",L"манго"};
  
  printf(" %ls \n\n", words[0]);
  
  int i,k;
  wchar_t temp[16];
  
  for(k=1;k < BROI_DUMI;k++)
  {
    for(i = BROI_DUMI-1; i > 0; i--)
    {
	  if( wcscmp(words[i],words[i-1]) < 0)
	  {
		wcscpy ( temp, words[i]);
		wcscpy ( words[i], words[i-1]);
		wcscpy ( words[i-1], temp);
	  }
    }		  
  }
   
  //Izvezhdane na rezultata
  for (i = 0 ; i < BROI_DUMI; i++) 
    printf("%ls \n", words[i]);
    
  return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <linux/uinput.h>


int main(void)
{
    int                    fd, kb_fd;
    struct uinput_user_dev uidev;
    struct input_event     ev, kb_ev;
    int                    dx, dy;
    

    fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
    
    if (fd == -1) {
      puts("Can't open file");
    return 1;
    }
    
    
    ioctl(fd, UI_SET_EVBIT, EV_KEY);
    ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
    ioctl(fd, UI_SET_EVBIT, EV_REL) ;
    ioctl(fd, UI_SET_RELBIT, REL_X);
    ioctl(fd, UI_SET_RELBIT, REL_Y);
   
    memset(&uidev, 0, sizeof(uidev));
    snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
    uidev.id.bustype = BUS_USB;
    uidev.id.vendor  = 0x1;
    uidev.id.product = 0x1;
    uidev.id.version = 1;

    if(write(fd, &uidev, sizeof(uidev)) < 0)
        {
      puts("Can't open file");
    return 1;
    }
    
	if(ioctl(fd, UI_DEV_CREATE) < 0)
       {
      puts("Can't open file");
    return 1;
    }

    kb_fd = open("/dev/input/event3", O_RDWR);
    if (kb_fd < 0)
    {  
	puts("cant open kb");
	return 1;
    }
    
    while (1)
    {
		
    read(kb_fd, &kb_ev, sizeof(struct input_event));

    if( kb_ev.type == 1 && (kb_ev.value == 2 || kb_ev.value == 1))
    {
		
		memset(&ev, 0, sizeof(ev));
    gettimeofday(&ev.time, NULL);
    usleep(100);
		 switch (kb_ev.code)
	  {
	    case 105: 
	      ev.type = EV_REL;
          ev.code = REL_X;
          ev.value = -5;
	    break;
	    
	    case 106: 
	      ev.type = EV_REL;
          ev.code = REL_X;
          ev.value = 5;
	    break;
	    
	    case 103: 
	      ev.type = EV_REL;
          ev.code = REL_Y;
          ev.value = -5;
	    break;
	    
	    case 108: 
	      ev.type = EV_REL;
          ev.code = REL_Y;
          ev.value = 5;
	    break;
	    
        default:
          ev.type = EV_REL;
          ev.code = REL_X;
          ev.value = -5;
      }
		
    write(fd, &ev, sizeof(ev));
    usleep(100);
    
    gettimeofday(&ev.time, NULL);
    ev.type = 0;
    ev.code = 0;
    ev.value = 0;
    write(fd, &ev, sizeof(ev));
    }
  }  
    
    

  close(fd);
  return 0;
}