void main(int argc, char* argv[]) { Display* display; /* pointer to X Display structure. */ int screen_num; /* number of screen to place the window on. */ Window win; /* pointer to the newly created window. */ unsigned int display_width, display_height; /* height and width of the X display. */ unsigned int width, height; /* height and width for the new window. */ char *display_name = getenv("DISPLAY"); /* address of the X display. */ GC gc, rev_gc; /* GC (graphics context) used for drawing */ /* in our window. */ short pixels[1000][1000]; /* used to store pixels on screen that were */ /* explicitly drawn or erased by the user. */ /* initialize the 'pixels' array to contain 0 values. */ { int x, y; for (x=0; x<1000; x++) for (y=0; y<1000; y++) pixels[x][y] = 0; } /* open connection with the X server. */ display = XOpenDisplay(display_name); if (display == NULL) { fprintf(stderr, "%s: cannot connect to X server '%s'\n", argv[0], display_name); exit(1); } /* get the geometry of the default screen for our display. */ screen_num = DefaultScreen(display); display_width = DisplayWidth(display, screen_num); display_height = DisplayHeight(display, screen_num); /* make the new window occupy 1/9 of the screen's size. */ width = (display_width / 3); height = (display_height / 3); printf("window width - '%d'; height - '%d'\n", width, height); /* create a simple window, as a direct child of the screen's */ /* root window. Use the screen's white color as the background */ /* color of the window. Place the new window's top-left corner */ /* at the given 'x,y' coordinates. */ win = create_simple_window(display, width, height, 0, 0); /* allocate two new GCs (graphics contexts) for drawing in the window. */ /* the first is used for drawing black over white, the second is used */ /* for drawing white over black. */ gc = create_gc(display, win, 0); rev_gc = create_gc(display, win, 1); /* subscribe to the given set of event types. */ XSelectInput(display, win, ExposureMask | KeyPressMask | ButtonPressMask | Button1MotionMask | Button2MotionMask | Button3MotionMask | StructureNotifyMask); /* perform an events loop */ { int done = 0; XEvent an_event; while (!done) { XNextEvent(display, &an_event); switch (an_event.type) { case Expose: /* redraw our window. */ handle_expose(display, gc, rev_gc, (XExposeEvent*)&an_event.xexpose, width, height, pixels); break; case ConfigureNotify: /* update the size of our window, for expose events. */ width = an_event.xconfigure.width; height = an_event.xconfigure.height; break; case ButtonPress: /* invert the pixel under the mouse pointer. */ handle_button_down(display, gc, rev_gc, (XButtonEvent*)&an_event.xbutton, width, height, pixels); break; case MotionNotify: /* invert the pixel under the mouse pointer. */ handle_drag(display, gc, rev_gc, (XButtonEvent*)&an_event.xbutton, width, height, pixels); break; case KeyPress: /* exit the application by braking out of the events loop. */ done = 1; break; default: /* ignore any other event types. */ break; } /* end switch on event type */ } /* end while events handling */ } /* free the GCs. */ XFreeGC(display, gc); XFreeGC(display, rev_gc); /* close the connection to the X server. */ XCloseDisplay(display); }
int main(int argc, char* argv[]) { int sockfd, oldsockfd, portno, n; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; struct hostent *server; pthread_t recv_thread; Colormap map; Display* display; int screen_num; Window win; unsigned int display_width, display_height; unsigned int width, height; char *display_name = getenv("DISPLAY"); GC gc, his_gc, refresh_gc; if(argc == 2) { // We are server sending = 1; if(argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } oldsockfd = socket(AF_INET, SOCK_STREAM, 0); if(oldsockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); int on = 1; if(setsockopt(oldsockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)) < 0) error("ERROR on sockopt"); if(bind(oldsockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(oldsockfd,5); clilen = sizeof(cli_addr); sockfd = accept(oldsockfd, (struct sockaddr *) &cli_addr, &clilen); if (sockfd < 0) error("ERROR on accept"); } else if(argc == 3) { // We are client sending = 1; portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); server = gethostbyname(argv[1]); if(server == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)(server->h_addr), (char *)(&serv_addr.sin_addr.s_addr), server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting"); } else { server = 0; } display = XOpenDisplay(display_name); if (display == NULL) { fprintf(stderr, "%s: cannot connect to X server '%s'\n", argv[0], display_name); exit(1); } screen_num = DefaultScreen(display); display_width = DisplayWidth(display, screen_num); display_height = DisplayHeight(display, screen_num); width = (display_width / 3); height = (display_height / 3); Atom wm_delete; win = create_simple_window(display, &wm_delete, width, height, 0, 0); XGCValues values; values.graphics_exposures = 0; gc = create_gc(display, win); his_gc = create_gc(display, win); refresh_gc = XCreateGC(display, win, GCGraphicsExposures, &values); XSetForeground(display, refresh_gc, WhitePixel(display, screen_num)); XSetBackground(display, refresh_gc, WhitePixel(display, screen_num)); map = DefaultColormap(display, 0); XParseColor(display, map, "#FFFFFF", &cols[0]); XAllocColor(display, map, &cols[0]); XParseColor(display, map, "#000000", &cols[1]); XAllocColor(display, map, &cols[1]); XParseColor(display, map, "#FF0000", &cols[2]); XAllocColor(display, map, &cols[2]); XParseColor(display, map, "#00FF00", &cols[3]); XAllocColor(display, map, &cols[3]); XParseColor(display, map, "#0000FF", &cols[4]); XAllocColor(display, map, &cols[4]); XParseColor(display, map, "#FFFF00", &cols[5]); XAllocColor(display, map, &cols[5]); XParseColor(display, map, "#FF00FF", &cols[6]); XAllocColor(display, map, &cols[6]); XParseColor(display, map, "#00FFFF", &cols[7]); XAllocColor(display, map, &cols[7]); XParseColor(display, map, "#999999", &cols[8]); XAllocColor(display, map, &cols[8]); XParseColor(display, map, "#666666", &cols[9]); XAllocColor(display, map, &cols[9]); set_title(display, win); back_buffer = XCreatePixmap(display, win, MAX_WIDTH, MAX_HEIGHT, DefaultDepth(display, screen_num)); XFillRectangle(display, back_buffer, refresh_gc, 0, 0, MAX_WIDTH, MAX_HEIGHT); if(sending) { recv_info_t info; info.display = display; info.window = win; info.gc = gc; info.his_gc = his_gc; info.refresh_gc = refresh_gc; info.sockfd = sockfd; pthread_create(&recv_thread, NULL, recv_line, &info); } XSelectInput(display, win, ExposureMask | KeyPressMask | ButtonPressMask | Button1MotionMask | StructureNotifyMask); XEvent an_event; while(1) { XNextEvent(display, &an_event); switch(an_event.type) { case ClientMessage: if(an_event.xclient.data.l[0] == wm_delete) quitme(display, gc, his_gc, refresh_gc, sockfd, 0); break; case Expose: handle_expose(display, refresh_gc, (XExposeEvent*)&an_event.xexpose); break; case ConfigureNotify: width = an_event.xconfigure.width; height = an_event.xconfigure.height; break; case ButtonPress: handle_button_down(display, gc, (XButtonEvent*)&an_event.xbutton, width, height); break; case MotionNotify: handle_drag(display, gc, (XButtonEvent*)&an_event.xbutton, width, height, sockfd); break; case KeyPress: handle_key(display, win, gc, his_gc, refresh_gc, map, (XKeyEvent*)&an_event.xkey, sockfd); break; default: break; } } quitme(display, gc, his_gc, refresh_gc, sockfd, 0); }
int main(int argc, char* argv[]) { Display* display; Window win; char *display_name = getenv("DISPLAY"); GC gc, rev_gc; Atom wmDelete; struct CEventDescriptor event = { .Type = EVENT_NULL }; InitGUIServer(); ClearScreen(); while (!ProcessGUIServer(&event)) ; display = XOpenDisplay(display_name); if (display == NULL) { fprintf(stderr, "%s: cannot connect to X server '%s'\n", argv[0], display_name); exit(1); } win = create_simple_window(display, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0); wmDelete = XInternAtom(display, "WM_DELETE_WINDOW", True); XSetWMProtocols(display, win, &wmDelete, 1); gc = create_gc(display, win, 0); rev_gc = create_gc(display, win, 1); XSelectInput(display, win, ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask); { int done = 0; XEvent an_event; while (!done) { XNextEvent(display, &an_event); switch (an_event.type) { case Expose: handle_expose(display, gc, rev_gc, (XExposeEvent*)&an_event.xexpose); break; case ButtonPress: handle_button_down(display, gc, rev_gc, (XButtonEvent*)&an_event.xbutton); break; case KeyPress: done = handle_key_down(display, gc, rev_gc, (XKeyEvent*)&an_event.xkey); break; case ClientMessage: done = True; break; default: break; } } } XFreeGC(display, gc); XFreeGC(display, rev_gc); XCloseDisplay(display); return 0; }