일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- Splunk
- GitHub
- Numpy
- keras
- Button
- mariadb
- Series
- javascript
- dataframe
- DFS
- mean
- SciPy
- E-P1
- 삼성소프트웨어멤버십
- SPL
- Lotto
- CNN
- GT-S80
- pip
- RNN
- pycharm
- synology
- index
- imread
- 알고리즘
- Python
- install
- pandas
- LSTM
- ipad
- Today
- Total
잠토의 잠망경
linux oo 본문
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include<X11/Xlib.h>
#include<X11/Xutil.h>
#include<X11/cursorfont.h>
void error_handling(char *message);
void Get_MPacket(char *buffer);
void Get_KPacket(char *buffer);
void Mouse_move(int x, int y);
void Mouse_click(int button);
void Mouse_release(int button);
void Mouse_push(int button);
typedef struct MPacket
{
int type;
int etype;
int data1;
int data2;
}MPacket;
int main(void)
{
int serv_sock;
int clnt_sock;
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
int clnt_addr_size;
char buffer[16]={0,};
int str_len=0;
serv_sock=socket(PF_INET, SOCK_STREAM, 0); /* 서버 소켓 생성 */
if(serv_sock == -1)
error_handling("socket() error");
printf("socket\n");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
serv_addr.sin_port=htons(5425);
if( bind(serv_sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr))==-1 ) /* 소켓에 주소 할당 */
error_handling("bind() error");
printf("bind\n");
if( listen(serv_sock, 5)==-1 ) /* 연결 요청 대기 상태로 진입 */
error_handling("listen() error");
printf("listen\n");
clnt_addr_size=sizeof(clnt_addr);
clnt_sock=accept(serv_sock, (struct sockaddr*)&clnt_addr,&clnt_addr_size); /* 연결 요청 수락 */
if(clnt_sock==-1)
error_handling("accept() error");
printf("accept\n");
while(1)
{
str_len=read(clnt_sock, buffer, sizeof(buffer)-1); /* 데이터 전송 */
if(str_len == 15)
Get_MPacket(buffer);
str_len=0;
memset(&buffer,0, sizeof(buffer));
}
close(clnt_sock); /* 연결 종료 */
return 0;
}
void Get_MPacket(char *buffer)
{
MPacket packet;
packet.type =*(int*)buffer;
packet.etype= *(int*)(buffer+4);
packet.data1=*(int*)(buffer+8);
packet.data2=*(int*)(buffer+12);
if(packet.type == 0 && packet.etype==0
&& packet.data1>=0 && packet.data1<=800
&& packet.data2>=0 && packet.data2<=600)
{
Mouse_move(packet.data1, packet.data2);
printf("%d : %d : %d : %d\n",
packet.type, packet.etype, packet.data1, packet.data2);
}
else if(packet.type == 1 &&( packet.etype==1 || packet.etype==2)
&& packet.data1>=0 && packet.data1<=800
&& packet.data2>=0 && packet.data2<=600)
{
Mouse_click(packet.etype);
printf("%d : %d : %d : %d\n",
packet.type, packet.etype, packet.data1, packet.data2);
}
else if(packet.type == 2 &&( packet.etype==1 || packet.etype==2)
&& packet.data1>=0 && packet.data1<=800
&& packet.data2>=0 && packet.data2<=600)
{
Mouse_release(packet.etype);
printf("%d : %d : %d : %d\n",
packet.type, packet.etype, packet.data1, packet.data2);
}
else if(packet.type == 3 &&( packet.etype==1 || packet.etype==2)
&& packet.data1>=0 && packet.data1<=800
&& packet.data2>=0 && packet.data2<=600)
{
Mouse_push(packet.etype);
printf("%d : %d : %d : %d\n",
packet.type, packet.etype, packet.data1, packet.data2);
}
}
void Get_KPacket(char *buffer)
{
KPacket packet;
packet.type = *(int*)buffer;
packet.etype = *(int*)(buffer+4);
packet.keychar = *(char*)(buffer+8);
packet.data1=*(int*)(buffer+9);// if char: Multicode
packet.data2=*(int*)(buffer+13);
packet.data3=*(int*)(buffer+17);
packet.data4=*(int*)(buffer+21);
printf("%d : %d : %c : %d : %d : %d : %d\n",
packet.type, packet.etype, packet.keychar,
packet.data1, packet.data2, packet.data3, packet.data4);
}
void Mouse_move(int x, int y)
{
Display *displayMain = XOpenDisplay(NULL);
Window win;
win=DefaultRootWindow(displayMain);
XWarpPointer(displayMain, None, win, 0, 0, 0, 0, x,y);
XCloseDisplay(displayMain);
}
void Mouse_click(int button)
{
Display *display = XOpenDisplay(NULL);
XEvent event;
if(display == NULL)
{
fprintf(stderr, "Errore nell'apertura del Display !!!\n");
}
memset(&event, 0x00, sizeof(event));
event.type = ButtonPress;
event.xbutton.button = button;
event.xbutton.same_screen = True;
XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
event.xbutton.subwindow = event.xbutton.window;
while(event.xbutton.subwindow)
{
event.xbutton.window = event.xbutton.subwindow;
XQueryPointer(display, event.xbutton.window, &event.xbutton.root, &event.xbutton.subwindow, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
}
if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Errore nell'invio dell'evento !!!\n");
XFlush(display);
usleep(100000);
event.type = ButtonRelease;
event.xbutton.state = 0x100;
if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Errore nell'invio dell'evento !!!\n");
XFlush(display);
XCloseDisplay(display);
}
void Mouse_push(int button)
{
Display *display = XOpenDisplay(NULL);
XEvent event;
if(display == NULL)
{
fprintf(stderr, "Errore nell'apertura del Display !!!\n");
}
memset(&event, 0x00, sizeof(event));
event.type = ButtonPress;
event.xbutton.button = button;
event.xbutton.same_screen = True;
if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Errore nell'invio dell'evento !!!\n");
XFlush(display);
XCloseDisplay(display);
}
void Mouse_release(int button)
{
Display *display = XOpenDisplay(NULL);
XEvent event;
if(display == NULL)
{
fprintf(stderr, "Errore nell'apertura del Display !!!\n");
}
memset(&event, 0x00, sizeof(event));
event.type = ButtonRelease;
event.xbutton.button = button;
event.xbutton.same_screen = True;
XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
event.xbutton.subwindow = event.xbutton.window;
if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Errore nell'invio dell'evento !!!\n");
XFlush(display);
//event.xbutton.state = 0x100;
XCloseDisplay(display);
}
void error_handling(char *message)
{
printf("%s",message);
}