int event_handler(struct razer_chroma *chroma, struct razer_chroma_event *event)
{
  struct razer_pos pos;
	#ifdef USE_DEBUGGING
		printf("input_handler called\n");
	#endif
	if(event->type != RAZER_CHROMA_EVENT_TYPE_KEYBOARD || !event->sub_type)
		return(1);
	keys_history[keys_history_index++] = (long)event->value;
  razer_convert_keycode_to_pos((long)event->value, &pos);
  printf("Keycode %d, X: %d, Y: %d\n", (long)event->value, pos.x, pos.y);
	if(keys_history_index==keys_max)
		keys_history_index = 0;
	return(1);
}
void effect(struct razer_chroma *chroma)
{
	int r,g,b;
	int rnd=random();
	int rnd2=random();
	int rnd3=random();
	int count = 0;
	int count_dir = 1;
	float s = 0.1f;
	int x,y;
	struct razer_rgb col;
	struct razer_pos pos;
	col.r = 255;
	col.g = 0;
	col.b = 0;
	while(running)
	{
		for(x=0;x<chroma->active_device->columns_num;x++)
			for(y=0;y<chroma->active_device->rows_num;y++)
			{
				r = (cos((count+((rnd%4)*90)+y)*s)+sin(count+x)*s)*255;
				g = (cos((count+((rnd2%4)*90)+y)*s)+sin(count+x)*s)*255;
				b = (cos((count+((rnd3%4)*90)+y)*s)+sin(count+x)*s)*255;
				chroma->active_device->leds->rows[y]->column[x].r = (unsigned char)r;
				chroma->active_device->leds->rows[y]->column[x].g = (unsigned char)g;
				chroma->active_device->leds->rows[y]->column[x].b = (unsigned char)b;
				chroma->active_device->leds->update_mask |= 1<<y;
			}

		for(int i=0;i<keys_max;i++)
			if(keys_history[i]!=-1)
			{
				razer_convert_keycode_to_pos(keys_history[i],&pos);
				razer_set_led_pos(chroma->active_device->leds,&pos,&col);
			}
		razer_update_leds(chroma,chroma->active_device->leds);
		count+=count_dir;
		if(count<=0 || count>=30)
		{
			count_dir=-count_dir;
			rnd = random();
			rnd2 = random();
			rnd3 = random();
		}
		razer_update(chroma);
		razer_frame_limiter(chroma,13);
	}
}
Beispiel #3
0
void effect(struct razer_chroma *chroma)
{
	int r = 128, g = 0, b = 0;
	int count = 1;
	int count_dir = 1;
	int x,y;
	int span = 20;
	double c = 0;
	struct razer_rgb col;
	struct razer_rgb green = {.r=0,.g=255,.b=0}; //define a green color
	struct razer_pos pos;
	col.r = 0;
	col.g = 255;
	col.b = 0;
	while(running){


		//r += rand_span(span);
		r = count;		
		r = fixed_range(r, 255);

		g += rand_span(span);
		g = fixed_range(g, 255);
		g = 0;		

		b += rand_span(span);
		b = fixed_range(b, 255);
		b = 0;

		for(x=0;x<22;x++){
				for(y=0;y<6;y++){	
					
					g = fixed_range(r - (x * 10), 255);				
					
					chroma->keys->rows[y].column[x].r = (unsigned char)r;
					chroma->keys->rows[y].column[x].g = (unsigned char)g;
					chroma->keys->rows[y].column[x].b = (unsigned char)b;
					chroma->keys->update_mask |= 1<<y;
				}
			}

		
		col.g = (sin(c) + 1)/2 * 225;
		col.b = 255 - col.g;
		c += 0.05;

		pos.x = 2;
		pos.y = 3;
		razer_set_key_pos(chroma->keys,&pos,&green);
		pos.x += 1;
		razer_set_key_pos(chroma->keys,&pos,&green);
		pos.y -= 1;
		razer_set_key_pos(chroma->keys,&pos,&green);
		pos.y += 1; 
		pos.x += 1;
		razer_set_key_pos(chroma->keys,&pos,&green);
		
		for(int i=0;i<keys_max;i++)
			if(keys_history[i]!=-1){
				
				razer_convert_keycode_to_pos(keys_history[i],&pos);							
				razer_set_key_pos(chroma->keys,&pos,&col);
				
				if(pos.x == 17 && pos.y == 0){
					for(int i=0;i<keys_max;i++){
						keys_history[i] = -1;
					}
				}
				
			}
	

		razer_update_keys(chroma,chroma->keys);
		count+=count_dir;
		if(count<=0 || count>255){
			count_dir=-count_dir;
		}
		razer_update(chroma);
		razer_frame_limiter(chroma,13);
	
	}
}

#pragma GCC diagnostic pop

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"

void stop(int sig)
{
	#ifdef USE_DEBUGGING
		printf("Stopping input example\n");
	#endif
	running = 0;
}
void razer_update(struct razer_chroma *chroma)
{
	struct timeval select_tv;
	struct razer_chroma_event chroma_event;
	int keycode = -1;
	fd_set keyboard_rs;
	fd_set mouse_rs;
	select_tv.tv_sec = 0;
	select_tv.tv_usec = 0;
	int r;
	char buf[2048];
	if(!chroma->keyboard_input_file)
		razer_open_keyboard_input_file(chroma);
	if(!chroma->mouse_input_file)
		razer_open_mouse_input_file(chroma);
	chroma->update_ms = razer_get_ticks();
	long diff_ms = chroma->update_ms - chroma->last_update_ms;
	chroma->update_dt = (float)diff_ms / 1000.0f;

	if(chroma->keyboard_input_file)
	{
		FD_ZERO(&keyboard_rs);
		FD_SET(chroma->keyboard_input_file,&keyboard_rs);
		r = select(chroma->keyboard_input_file+1,&keyboard_rs,0,0,&select_tv);
		if(r && FD_ISSET(chroma->keyboard_input_file,&keyboard_rs))
		{
			int n=2048;
			n = read(chroma->keyboard_input_file,buf,2048);
			if(n<0)
			{
				/*if(errno != EAGAIN)
				{
					razer_close_input_file(chroma);
					return;
				}*/
			}
			else if(n==0)
			{
				razer_close_keyboard_input_file(chroma);
				return;
			}
			else				
			{
				unsigned int i;
				for(i=0;i<n/sizeof(struct input_event);i++)
				{
					struct input_event *event = (struct input_event*)(buf+(i*sizeof(struct input_event)));
					if(event->type==EV_KEY)
					{
						keycode = event->code;
						if(event->value==1)
						{
							/*if(keycode!=last_keycode)
							if(keycode==183)
								actual_mode=0;
							if(keycode==184)
								actual_mode=1;
							if(keycode==185)
								actual_mode=2;
							if(keycode==186)
								actual_mode=3;
							if(keycode==187)
								actual_mode=4;
							*/
							razer_copy_pos(&chroma->key_pos,&chroma->last_key_pos);
							razer_convert_keycode_to_pos(event->code,&chroma->key_pos);							
							chroma->keys->heatmap[chroma->key_pos.y][chroma->key_pos.x]+=1;
							chroma_event.key = "Key Down";
						}
						else
							chroma_event.key = "Key Up";
						if(chroma->input_handler)
						{
							chroma_event.type = RAZER_CHROMA_EVENT_TYPE_KEYBOARD;
							chroma_event.sub_type = event->value;
							chroma_event.value = keycode;

							chroma->input_handler(chroma,&chroma_event);
						}
						long key_diff_ms = chroma->update_ms - chroma->last_key_event_ms;
						chroma->key_event_dt = (float)key_diff_ms / 1000.0f;
						chroma->last_key_event_ms = chroma->update_ms;			
					}
				}
			}
		}	
	}

	if(chroma->mouse_input_file)
	{
		FD_ZERO(&mouse_rs);
		FD_SET(chroma->mouse_input_file,&mouse_rs);
		select_tv.tv_sec = 0;
		select_tv.tv_usec = 0;
		r = select(chroma->mouse_input_file+1,&mouse_rs,0,0,&select_tv);
		if(r && FD_ISSET(chroma->mouse_input_file,&mouse_rs))
		{
			int n=2048;
			n = read(chroma->mouse_input_file,buf,2048);
			if(n<0)
			{
				/*if(errno != EAGAIN)
				{
					razer_close_input_file(chroma);
					return;
				}*/
			}
			else if(n==0)
			{
				razer_close_mouse_input_file(chroma);
				return;
			}
			else				
			{
				unsigned int i;
				unsigned int event_size = sizeof(struct input_event);
				for(i=0;i<n/event_size;i++)
				{
					struct input_event *event = (struct input_event*)(buf+(i*sizeof(struct input_event)));
						if(chroma->input_handler)
						{
							chroma_event.type = RAZER_CHROMA_EVENT_TYPE_MOUSE;
	        				switch(event->type)
	        				{
	        					case EV_MSC:
	        						break;
	        					case EV_SYN:
	        						break;
	        					case EV_KEY:
	        						switch(event->code)
	        						{
	        							case BTN_LEFT:
											chroma_event.value = RAZER_CHROMA_EVENT_BUTTON_LEFT;
											break;
		    							case BTN_MIDDLE:
											chroma_event.value = RAZER_CHROMA_EVENT_BUTTON_MIDDLE;
											break;
		    							case BTN_RIGHT:
											chroma_event.value = RAZER_CHROMA_EVENT_BUTTON_RIGHT;
											break;
		    							case BTN_EXTRA:
											chroma_event.value = RAZER_CHROMA_EVENT_BUTTON_EXTRA;
											break;
										default:
											#ifdef USE_DEBUGGING
												printf("uknown button event: type:%d,code:%d,value:%d\n",event->type,event->code,event->value);
											#endif
											break;
	        						}
	        						switch(event->value)
	        						{
	        							case 0 :
											chroma_event.sub_type = RAZER_CHROMA_EVENT_SUBTYPE_MOUSE_BUTTON_UP;
											break;
	        							case 1 :
											chroma_event.sub_type = RAZER_CHROMA_EVENT_SUBTYPE_MOUSE_BUTTON_DOWN;
											break;

	        						}
		       						break;
								case EV_REL :
									switch(event->code)
									{
										case 0 : 
											chroma_event.sub_type = RAZER_CHROMA_EVENT_SUBTYPE_MOUSE_X_AXIS_MOVEMENT;
											chroma_event.value = event->value;
											break;
										case 1 : 
											chroma_event.sub_type = RAZER_CHROMA_EVENT_SUBTYPE_MOUSE_Y_AXIS_MOVEMENT;
											chroma_event.value = event->value;
											break;
										case 8 : 
											chroma_event.sub_type = RAZER_CHROMA_EVENT_SUBTYPE_MOUSE_WHEEL_MOVEMENT;
											chroma_event.value = event->value;
											break;
										default:
											#ifdef USE_DEBUGGING
												printf("uknown relative movement event: type:%d,code:%d,value:%d\n",event->type,event->code,event->value);
											#endif
											break;
									}
									break;
								default:
									#ifdef USE_DEBUGGING
										printf("uknown event: type:%d,code:%d,value:%d\n",event->type,event->code,event->value);
									#endif
									break;
							}
							chroma->input_handler(chroma,&chroma_event);
						}
						long key_diff_ms = chroma->update_ms - chroma->last_key_event_ms;
						chroma->key_event_dt = (float)key_diff_ms / 1000.0f;
						chroma->last_key_event_ms = chroma->update_ms;			
				}
			}
		}	
	}

}