Ejemplo n.º 1
0
Archivo: dock.c Proyecto: Cougar/pwm
static bool do_create_dock(WDock *dock, int x, int y, int flags)
{
	Window win;
	
	flags|=WTHING_UNFOCUSABLE;
	
	dock->x=x;
	dock->y=y;
	dock->w=DOCKWIN_W;
	dock->h=DOCKWIN_H;
	
	win=create_simple_window(x, y, dock->w, dock->h,
							 GRDATA->base_colors.pixels[WCG_PIX_BG]);
	
	dock->flags|=flags;
	dock->win=win;
	dock->dockwin_count=0;
	dock->max_vis_dockwin=1;
	dock->dock_w=1;
	dock->dock_h=1;
	SCREEN->dock=dock;
	
	XSelectInput(wglobal.dpy, win, DOCK_MASK);
	XSaveContext(wglobal.dpy, win, wglobal.win_context, (XPointer)dock);

	grab_bindings(win, ACTX_DOCKWIN|ACTX_GLOBAL);

	add_winobj((WWinObj*)dock, WORKSPACE_STICKY, LVL_KEEP_ON_TOP);
	
	return TRUE;
}
Ejemplo n.º 2
0
int main()
{
    auto engine = std::make_unique<yae::sdl_engine>();
    auto window = engine->create_simple_window();
    auto cv = yae::clipping_volume{ -8.0f, 8.0f, -6.0f, 6.0f, 1.0f, -1.0f };
    window->close_when_keydown();

    auto rwop = SDL_RWFromFile("smiley.png", "rb");
    auto hero_image = IMG_LoadPNG_RW(rwop);
    auto pixels = (GLubyte*)hero_image->pixels;
    auto width = hero_image->w;
    auto height = hero_image->h;
    auto hero_texture = std::make_shared<yae::texture>(pixels, width, height);

    yae::buffer_object_builder<float> b({ -50.0f, -50.0f, 50.0f, -50.0f, 50.0f, 50.0f, -50.0f, 50.0f });
    auto multi_hero = std::make_shared<yae::geometry<float>>(b.get_size() / 2, 2, GL_QUADS);
    multi_hero->set_vertex_positions(b.build());
    multi_hero->set_vertex_tex_coords(b.build());
    auto node = std::make_shared<yae::geometry_node<float>>(multi_hero);
    auto root = std::make_shared<yae::group>();
    root->add(node);
    auto prog = yae::texture_program::create();
    prog->set_texture(hero_texture);

    auto scene = std::make_shared<yae::rendering_scene>();
    auto cam = std::make_shared<yae::parallel_camera>(cv);
    auto bg_color = yae::color4f{ 0.0f, 0.0f, 0.0f, 0.0f };
    auto clear_viewport_cb = yae::clear_viewport_callback(bg_color, scene->get_viewport());
    auto cre = std::make_shared<yae::custom_rendering_element>("clear_viewport", clear_viewport_cb);
    auto nre = std::make_shared<yae::node_rendering_element>("smiley_canvas", root, prog, cam);
    scene->add_element(cre);
    scene->add_element(nre);
    scene->associate_camera<yae::rendering_scene::fit_all_adapter>(cam, window.get(), yae::viewport_relative{ 0.0f, 0.0f, 1.0f, 1.0f });

    window->set_render_callback([&](yae::rendering_context& ctx) {
        cam->rotate_z(0.5);
    });

    window->add_scene(scene);

    engine->run(window.get());

    return 0;
}
Ejemplo n.º 3
0
int 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;			/* GC (graphics context) used for drawing    */
				/*  in our window.			     */

  /* 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 a new GC (graphics context) for drawing in the window. */
  gc = create_gc(display, win, 0);
  XSync(display, False);

  /* draw one pixel near each corner of the window */
  XDrawPoint(display, win, gc, 5, 5);
  XDrawPoint(display, win, gc, 5, height-5);
  XDrawPoint(display, win, gc, width-5, 5);
  XDrawPoint(display, win, gc, width-5, height-5);

  /* draw two intersecting lines, one horizontal and one vertical, */
  /* which intersect at point "50,100".                            */
  XDrawLine(display, win, gc, 50, 0, 50, 200);
  XDrawLine(display, win, gc, 0, 100, 200, 100);

  /* now use the XDrawArc() function to draw a circle whose diameter */
  /* is 30 pixels, and whose center is at location '50,100'.         */
  XDrawArc(display, win, gc, 50-(30/2), 100-(30/2), 30, 30, 0, 360*64);

  {
    XPoint points[] = {
      {0, 0},
      {15, 15},
      {0, 15},
      {0, 0}
    };
    int npoints = sizeof(points)/sizeof(XPoint);

    /* draw a small triangle at the top-left corner of the window. */
    /* the triangle is made of a set of consecutive lines, whose   */
    /* end-point pixels are specified in the 'points' array.       */
    XDrawLines(display, win, gc, points, npoints, CoordModeOrigin);
  }

  /* draw a rectangle whose top-left corner is at '120,150', its width is */
  /* 50 pixels, and height is 60 pixels.                                  */
  XDrawRectangle(display, win, gc, 120, 150, 50, 60);

  /* draw a filled rectangle of the same size as above, to the left of the */
  /* previous rectangle.                                                   */
  XFillRectangle(display, win, gc, 60, 150, 50, 60);

  /* flush all pending requests to the X server. */
  XFlush(display);

  /* make a delay for a short period. */
  sleep(4);

  /* close the connection to the X server. */
  XCloseDisplay(display);
  return(0);
}
Ejemplo n.º 4
0
void drawer () {
    int x,i,j,imagewidth,left = 0,outside,r,k;
	float s,realcentre,imgcentre,realx,imgx;
    
	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;                        /* GC (graphics context) used for drawing    */
    /*  in our window.                           */
    
  	/* open connection with the X server. */
  	display = XOpenDisplay(display_name);
  	if (display == NULL) {
        fprintf(stderr, "cannot connect to X server '%s'\n",display_name);
        exit(EXIT_FAILURE);
  	}
    
  	/* 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 much of the screen's size. */
  	width = (display_width * 0.5);
  	height = (display_height * 0.5);
    // 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 a new GC (graphics context) for drawing in the window. */
  	gc = create_gc(display, win, 0);
  	XSync(display, False);
    
	k = -15;
	imagewidth = 1000;
	x = 1;


    
	r = 2;
	s = (float)(2*r)/imagewidth;
	realcentre = 0.0;
	imgcentre = 0.0;
    
	for(i=0;i<imagewidth;i++){
		for(j=0;j<imagewidth;j++){
			realx = s*(j-imagewidth/2) + realcentre;
			imgx = s*(i-imagewidth/2) + imgcentre;
			outside = testmal(realx,imgx,k);
			if (outside == 0){
                //				printf("%f %f\n",realx,imgx);
				XDrawPoint(display, win, gc, left+j/x, height/2-(imagewidth/2)/x+i/x);
				/*Flush all pending requests to the X server.*/
				XFlush(display);
			}
		}
	}
    
  	/* make a delay for a short period. */
  	sleep(300);
    
  	/* close the connection to the X server. */
  	XCloseDisplay(display);
}
Ejemplo n.º 5
0
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;			/* GC (graphics context) used for drawing    */
				/*  in our window.			     */

  /* 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 a new GC (graphics context) for drawing in the window. */
  gc = create_gc(display, win, 0);
  XSync(display, False);

  {
    /* this variable will contain the ID of the newly created pixmap.    */
    Pixmap bitmap;
    /* these variables will contain the dimensions of the loaded bitmap. */
    unsigned int bitmap_width, bitmap_height;
    /* these variables will contain the location of the hotspot of the   */
    /* loaded bitmap.                                                    */
    int hotspot_x, hotspot_y;

    /* load the bitmap found in the file "icon.bmp", create a pixmap     */
    /* containing its data in the server, and put its ID in the 'bitmap' */
    /* variable.                                                         */
    int rc = XReadBitmapFile(display, win,
                             "icon.bmp",
                             &bitmap_width, &bitmap_height,
                             &bitmap,
                             &hotspot_x, &hotspot_y);
    /* check for failure or success. */
    switch (rc) {
        case BitmapOpenFailed:
            fprintf(stderr, "XReadBitmapFile - could not open file 'icon.bmp'.\n");
	    exit(1);
            break;
        case BitmapFileInvalid:
            fprintf(stderr,
                    "XReadBitmapFile - file '%s' doesn't contain a valid bitmap.\n",
                    "icon.bmp");
	    exit(1);
            break;
        case BitmapNoMemory:
            fprintf(stderr, "XReadBitmapFile - not enough memory.\n");
	    exit(1);
            break;
    }

    /* start drawing the given pixmap on to our window. */
    {
      int i, j;

      for(i=0; i<6; i++) {
        for(j=0; j<6; j++) {
          XCopyPlane(display, bitmap, win, gc,
                    0, 0,
                    bitmap_width, bitmap_height,
                    j*bitmap_width, i*bitmap_height,
                    1);
	  XSync(display, False);
	  usleep(100000);
        }
      }
    }
  }

  /* flush all pending requests to the X server. */
  XFlush(display);

  /* make a delay for a short period. */
  sleep(4);

  /* close the connection to the X server. */
  XCloseDisplay(display);
}
Ejemplo n.º 6
0
int main(int argc, char *argv[])
{
	Display* display;
	int screen_num;
	Window win;
	unsigned int display_width,
				 display_height;
	unsigned int win_width,
				 win_height;
	char *display_name = getenv("DISPLAY"); 

	/*用于绘制的图形环境*/
	GC gc;			

	/*不同xlib函数的返回值,应该就是一个整数*/
	/*int rc;*/
	Status rc;			
	Pixmap bitmap;
	unsigned int bitmap_width, bitmap_height;
	int hotspot_x, hotspot_y;
	int i,j;


	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);
	win_width = (display_width / 3);
	win_height = (display_height / 3);
	win = create_simple_window(display, win_width, win_height, 0, 0);

	/******图形上下文的创建******/
	/*图形上下文环境,提供绘图环境包含前景色背景色线风格等各种属性*/
	gc = create_gc(display, win);

	/*这个函数刷新所有的输出缓存直至所有事件被Xserver收到并处理。
	 *输入参数为False将会不会忽略所有排队事件,
	 如果为True则忽略所有事件包括该函数调用之前的排队事件。
	 * */
	XSync(display, False);

	/******在这里开始绘图******/

	/*使用当前路径的icon.bmp文件,不是windows的bmp,它是个数组,和xpm格式也不同*/
	rc = XReadBitmapFile(display, win,
			"icon.bmp",
			&bitmap_width, &bitmap_height,
			&bitmap,
			&hotspot_x, &hotspot_y);

	/*xpm格式的文件无法读取*/
	/*rc = XReadBitmapFile(display, win,
	  "home.xpm",
	  &bitmap_width, &bitmap_height,
	  &bitmap,
	  &hotspot_x, &hotspot_y);*/
	/*如果读取文件错误的出错处理*/
	switch (rc)
	{
		case BitmapOpenFailed:
			fprintf(stderr, "XReadBitmapFile - could not open file 'icon.bmp'.\n");
			exit(1);
			break;
		case BitmapFileInvalid:
			fprintf(stderr,
					"XReadBitmapFile - file '%s' doesn't contain a valid bitmap.\n",
					"icon.bmp");
			exit(1);
			break;
		case BitmapNoMemory:
			fprintf(stderr, "XReadBitmapFile - not enough memory.\n");
			exit(1);
			break;
	}

	/*不用上面的方法,用下面的方法获得图片也行,这需要把icon.bmp包含成为头文件
	 * 这里面的icon_bitmap_bits就是这个icon.bmp对应的表示图像的数组
	 * 另外宽度和高度这里设成20,如果不是这个数,那么绘制出来的可能乱了*/
	/*bitmap_width  = 20;
	bitmap_height = 20;
	bitmap = XCreateBitmapFromData(display,
			                       win,
			                       icon_bitmap_bits,
			                       bitmap_width,
			                       bitmap_height);
	if (!bitmap)
	{
		    fprintf(stderr, "XCreateBitmapFromData - error creating pixmap\n");
		    exit(1);
	}
	*/


	/*这里绘制了36个图标*/
	for(i=0; i<6; i++)
	{
		for(j=0; j<6; j++)
		{
			/*这里在指定的window上面绘制图片,各个参数如下:
			 * bitmap指定绘制的源(矩形)
			 * win指定绘制的目标(矩形)
			 * gc图形上下文环境,提供绘图环境包含前景色背景色线风格等各种属性
			 * (0,0)表示相对于源的左上角坐标
			 * (bitmap_width,bitmap_height)表示绘制的大小
			 * (j*bitmap_width,i*bitmap_height)表示相对于目标的左上角坐标
			 * 1和色深位相关,不太清楚。
			 * */
			XCopyPlane(display, bitmap, win, gc,
					0, 0,
					bitmap_width, bitmap_height,
					j*bitmap_width, i*bitmap_height,
					1);
			/*刷新输出缓存直至所有的到X的请求被收到并处理
			 *这里如果传入参数False就不会忽略事件队列中的事件了*/
			XSync(display, False);//如果没有这句话不会动态一个个的绘制
			usleep(100000);
		}
	}


	 /*把所有的请求通知给X server,我尝试过了,不用这一句也行,因为前面有XSync*/
	 /*XFlush函数刷新输出缓存,由于输出缓存通过调用XPending,XNextEvent,XWinEvents
	 *根据需要会被自动刷新,大多数客户程序是不用调用它的。
	 *服务端发起的事件可能被放到库事件队列中的.
	 * */
	XFlush(display);


	/******延迟5秒后释放资源,结束程序******/
	sleep(10);
	XCloseDisplay(display);

	return 0;
}
Ejemplo n.º 7
0
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);
}
Ejemplo n.º 8
0
int 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;			/* GC (graphics context) used for drawing    */
				/*  in our window.			     */
  Cursor font_cursor,		/* handles for the cursors we will create.   */
         icon_cursor;

  /* 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 a new GC (graphics context) for drawing in the window. */
  gc = create_gc(display, win, 0);
  XSync(display, False);

  /* play with cursors... */
  {
    /* this variable will contain the ID of the newly created pixmap.    */
    Pixmap bitmap;
    /* these variables will contain the dimensions of the loaded bitmap. */
    unsigned int bitmap_width, bitmap_height;
    /* these variables will contain the location of the hotspot of the   */
    /* loaded bitmap.                                                    */
    int hotspot_x, hotspot_y;

    /* load the bitmap found in the file "icon.bmp", create a pixmap     */
    /* containing its data in the server, and put its ID in the 'bitmap' */
    /* variable.                                                         */
    int rc = XReadBitmapFile(display, win,
                             "icon.bmp",
                             &bitmap_width, &bitmap_height,
                             &bitmap,
                             &hotspot_x, &hotspot_y);
    /* check for failure or success. */
    switch (rc) {
        case BitmapOpenFailed:
            fprintf(stderr, "XReadBitmapFile - could not open file 'icon.bmp'.\n");
	    exit(1);
            break;
        case BitmapFileInvalid:
            fprintf(stderr,
                    "XReadBitmapFile - file '%s' doesn't contain a valid bitmap.\n",
                    "icon.bmp");
	    exit(1);
            break;
        case BitmapNoMemory:
            fprintf(stderr, "XReadBitmapFile - not enough memory.\n");
	    exit(1);
            break;
    }

    /* create a 'watch' cursor. */
    font_cursor = XCreateFontCursor(display, XC_watch);
    /* attach this cursor to our window. */
    XDefineCursor(display, win, font_cursor);
    XSync(display, False);

    /* make a short delay. */
    sleep(3);

    /* detach this cursor from our window. */
    XUndefineCursor(display, win);

    /* create a 'box' cursor. */
    font_cursor = XCreateFontCursor(display, XC_box_spiral);
    /* attach this cursor to our window. */
    XDefineCursor(display, win, font_cursor);
    XSync(display, False);

    /* make a short delay. */
    sleep(3);

    /* detach this cursor from our window. */
    XUndefineCursor(display, win);

    /* create a cursor out of our icon's pixmap. */
    {
      /* first, define forground and background colors for the cursor. */
      XColor cursor_fg, cursor_bg;

      /* access the default color map of our screen. */
      Colormap screen_colormap =
			DefaultColormap(display, DefaultScreen(display));

      /* allocate black and while colors. */
      Status rc = XAllocNamedColor(display,
                                   screen_colormap,
                                   "black",
                                   &cursor_fg,
                                   &cursor_fg);
      if (rc == 0) {
          fprintf(stderr, "XAllocNamedColor - canot allocate 'black' !\n");
          exit(1);
      }
      rc = XAllocNamedColor(display,
                            screen_colormap,
                            "white",
                            &cursor_bg,
                            &cursor_bg);
      if (rc == 0) {
          fprintf(stderr, "XAllocNamedColor - canot allocate 'white' !\n");
          exit(1);
      }

      /* finally, generate the cursor. make the 'hot spot' be close to the */
      /* top-left corner of the cursor - location (x=5, y=4). */
      icon_cursor = XCreatePixmapCursor(display, bitmap, bitmap,
				        &cursor_fg, &cursor_bg,
                                        5, 4);
    }

    /* attach the icon cursor to our window. */
    XDefineCursor(display, win, icon_cursor);
    XSync(display, False);

    /* make a short delay. */
    sleep(4);

    /* detach this cursor from our window. */
    XUndefineCursor(display, win);

    /* finally, free our icon pixmap. */
    XFreePixmap(display, bitmap);
  }

  /* flush all pending requests to the X server. */
  XFlush(display);

  /* make a delay for a short period. */
  sleep(4);

  /* close the connection to the X server. */
  XCloseDisplay(display);
  return(0);
}
Ejemplo n.º 9
0
Archivo: menu.c Proyecto: Cougar/pwm
/* Create a menu window
 */
static WMenu* create_menu(WMenuData *mdata, WThing *context, int x, int y,
						  WMenu *parent)
{
	int w, h, th, eh;
	int flags=0;
	Window win;
	WMenu *menu;
	
	if(mdata->init_func!=NULL && mdata->nref==0)
		mdata->init_func(mdata, context);

	if(mdata->flags&WMENUDATA_CONTEXTUAL){
		if(context==NULL || WTHING_IS(context, WTHING_SCREEN) ||
		   WTHING_IS(context, WTHING_MENU))
			return NULL;
		flags|=(WMENU_NOTITLE|WMENU_CONTEXTUAL);
	}else{
		context=NULL;
	}
	
	if(mdata->nref!=0)
		flags|=(WMENU_NOTITLE|WMENU_CONTEXTUAL);
	
	if(mdata->title==NULL)
		flags|=(WMENU_NOTITLE|WMENU_CONTEXTUAL);
	
	if(parent!=NULL)
		flags|=parent->flags&(WMENU_NOTITLE|WMENU_CONTEXTUAL);

	/* */
	
	w=menu_width(mdata, flags);
	h=menu_height(mdata);	
	
	/* Don't display empty menus */
	if(w==0 || h==0)
		return NULL;
	
	/* */
	
	menu=ALLOC(WMenu);
	
	if(menu==NULL){
		warn_err();
		return NULL;
	}
	
	WTHING_INIT(menu, WTHING_MENU);
	
	th=FONT_HEIGHT(GRDATA->font)+2*CF_MENUTITLE_V_SPACE;
	eh=FONT_HEIGHT(GRDATA->menu_font)+2*CF_MENUENT_V_SPACE;

	if(parent==NULL){
		x-=w/2;
		y+=th/(flags&WMENU_NOTITLE ? 2 : -2 );
	}else if(!(flags&WMENU_NOTITLE)){
		y-=th;
	}

	if(flags&WMENU_NOTITLE)
		th=0;

	h+=th;

	win=create_simple_window(x, y, w, h,
							 GRDATA->base_colors.pixels[WCG_PIX_BG]);
	
	menu->menu_win=win;
	menu->x=x;
	menu->y=y;
	menu->w=w;
	menu->h=h;
	menu->title_height=th;
	menu->entry_height=eh;
	menu->flags=flags;
	menu->selected=NO_ENTRY;
	menu->data=mdata;
	menu->context=context;
	
	if(mdata->nref++==0)
		mdata->inst1=menu;

	XSelectInput(wglobal.dpy, win, MENU_MASK);
	XSaveContext(wglobal.dpy, win, wglobal.win_context, (XPointer)menu);

	if(parent==NULL)
		add_winobj((WWinObj*)menu, WORKSPACE_STICKY, LVL_MENU);
	else
		add_winobj_above((WWinObj*)menu, (WWinObj*)parent);

	map_winobj((WWinObj*)menu);

	return menu;
}
Ejemplo n.º 10
0
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);
}
Ejemplo n.º 11
0
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 win_width,
	       win_height;	/* height and width for the new window.      */
  char *display_name = getenv("DISPLAY");  /* address of the X display.      */
  GC gc;			/* GC (graphics context) used for drawing    */
				/*  in our window.			     */

  /* 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. */
  win_width = (display_width / 3);
  win_height = (display_height / 3);
  printf("window width - '%d'; height - '%d'\n", win_width, win_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, win_width, win_height, 0, 0);

  /* allocate a new GC (graphics context) for drawing in the window. */
  gc = create_gc(display, win, 0);
  XFlush(display);

  sleep(3);

  /* example of resizing a window. */
  {
    int i;

    /* start shrinking our window in a loop. */
    for (i=0; i<40; i++) {
      win_width -= 3;
      win_height -= 3;
      XResizeWindow(display, win, win_width, win_height);
      XFlush(display);
      usleep(20000);
    }

    /* start shrinking our window in a loop. */
    for (i=0; i<40; i++) {
      win_width += 3;
      win_height += 3;
      XResizeWindow(display, win, win_width, win_height);
      XFlush(display);
      usleep(20000);
    }
  }

  sleep(1);

  /* example of moving a window. */
  {
    int i;
    XWindowAttributes win_attr;
    int x, y;
    int scr_x, scr_y;
    Window child_win;
    /* this variable will store the ID of the parent window of our window. */
    Window parent_win;

    /* first, get the current attributes of our window. */
    XGetWindowAttributes(display, win, &win_attr);

    x = win_attr.x;
    y = win_attr.y;

    /* next, find the parent window of our window.      */
    {
      /* this variable will store the ID of the root window of the screen    */
      /* our window is mapped on.                                            */
      Window root_win;
      /* this variable will store an array of IDs of the child windows of    */
      /* our window.                                                         */
      Window* child_windows;
      /* and this one will store the number of child windows our window has. */
      int num_child_windows;

      /* finally, make the query for the above values. */
      XQueryTree(display, win,
                 &root_win,
                 &parent_win,
                 &child_windows, &num_child_windows);

      /* we need to free the list of child IDs, as it was dynamically */
      /* allocated by the XQueryTree function.                        */
      XFree(child_windows);
    }

    /* next, translate the location coordinates to screen coordinates. */
    /* this is done using the root window as the destination window.   */
    /* this works since the root window always spans the entire screen */
    /* area, and thus has its top-left corner always at the top-left   */
    /* corner of the screen.                                           */
    XTranslateCoordinates(display,
			  parent_win, win_attr.root,
			  x, y,
			  &scr_x, &scr_y,
			  &child_win);

    /* start moving the window to the left. */
    for (i=0; i<40; i++) {
      scr_x -= 3;
      XMoveWindow(display, win, scr_x, scr_y);
      XFlush(display);
      usleep(20000);
    }

    /* start moving the window to down. */
    for (i=0; i<40; i++) {
      scr_y += 3;
      XMoveWindow(display, win, scr_x, scr_y);
      XFlush(display);
      usleep(20000);
    }

    /* start moving the window to the right. */
    for (i=0; i<40; i++) {
      scr_x += 3;
      XMoveWindow(display, win, scr_x, scr_y);
      XFlush(display);
      usleep(20000);
    }

    /* start moving the window up. */
    for (i=0; i<40; i++) {
      scr_y -= 3;
      XMoveWindow(display, win, scr_x, scr_y);
      XFlush(display);
      usleep(20000);
    }
  }

  sleep(1);

  /* example of iconifying and de-iconifying a window. */
  {
    /* iconify our window. */
    XIconifyWindow(display, win, DefaultScreen(display));
    XFlush(display);
    sleep(2);
    /* de-iconify our window. */
    XMapWindow(display, win);
    XFlush(display);
    sleep(2);
  }

  /* flush all pending requests to the X server. */
  XFlush(display);

  /* make a delay for a short period. */
  sleep(2);

  /* close the connection to the X server. */
  XCloseDisplay(display);
}
Ejemplo n.º 12
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;
}
Ejemplo n.º 13
0
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;			/* GC (graphics context) used for drawing    */
				/*  in our window.			     */
  Colormap screen_colormap;     /* color map to use for allocating colors.   */
  XColor red, brown, blue, yellow, green;
				/* used for allocation of the given color    */
				/* map entries.                              */
  Status rc;			/* return status of various Xlib functions.  */

  /* 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 a new GC (graphics context) for drawing in the window. */
  gc = create_gc(display, win, 0);
  XSync(display, False);

  /* get access to the screen's color map. */
  screen_colormap = DefaultColormap(display, DefaultScreen(display));

  /* allocate the set of colors we will want to use for the drawing. */
  rc = XAllocNamedColor(display, screen_colormap, "red", &red, &red);
  if (rc == 0) {
    fprintf(stderr, "XAllocNamedColor - failed to allocated 'red' color.\n");
    exit(1);
  }
  rc = XAllocNamedColor(display, screen_colormap, "brown", &brown, &brown);
  if (rc == 0) {
    fprintf(stderr, "XAllocNamedColor - failed to allocated 'brown' color.\n");
    exit(1);
  }
  rc = XAllocNamedColor(display, screen_colormap, "blue", &blue, &blue);
  if (rc == 0) {
    fprintf(stderr, "XAllocNamedColor - failed to allocated 'blue' color.\n");
    exit(1);
  }
  rc = XAllocNamedColor(display, screen_colormap, "yellow", &yellow, &yellow);
  if (rc == 0) {
    fprintf(stderr, "XAllocNamedColor - failed to allocated 'yellow' color.\n");
    exit(1);
  }
  rc = XAllocNamedColor(display, screen_colormap, "green", &green, &green);
  if (rc == 0) {
    fprintf(stderr, "XAllocNamedColor - failed to allocated 'green' color.\n");
    exit(1);
  }
  
  /* draw one pixel near each corner of the window */
  /* draw the pixels in a red color. */
  XSetForeground(display, gc, red.pixel);
  XDrawPoint(display, win, gc, 5, 5);
  XDrawPoint(display, win, gc, 5, height-5);
  XDrawPoint(display, win, gc, width-5, 5);
  XDrawPoint(display, win, gc, width-5, height-5);

  /* draw two intersecting lines, one horizontal and one vertical, */
  /* which intersect at point "50,100".                            */
  /* draw the line in a brown color. */
  XSetForeground(display, gc, brown.pixel);
  XDrawLine(display, win, gc, 50, 0, 50, 200);
  XDrawLine(display, win, gc, 0, 100, 200, 100);

  /* now use the XDrawArc() function to draw a circle whose diameter */
  /* is 30 pixels, and whose center is at location '50,100'.         */
  /* draw the arc in a blue color. */
  XSetForeground(display, gc, blue.pixel);
  XDrawArc(display, win, gc, 50-(30/2), 100-(30/2), 30, 30, 0, 360*64);

  {
    XPoint points[] = {
      {0, 0},
      {15, 15},
      {0, 15},
      {0, 0}
    };
    int npoints = sizeof(points)/sizeof(XPoint);

    /* draw a small triangle at the top-left corner of the window. */
    /* the triangle is made of a set of consecutive lines, whose   */
    /* end-point pixels are specified in the 'points' array.       */
    /* draw the triangle in a yellow color. */
    XSetForeground(display, gc, yellow.pixel);
    XDrawLines(display, win, gc, points, npoints, CoordModeOrigin);
  }

  /* draw a rectangle whose top-left corner is at '120,150', its width is */
  /* 50 pixels, and height is 60 pixels.                                  */
  /* draw the rectangle in a black color. */
  XSetForeground(display, gc, BlackPixel(display, screen_num));
  XDrawRectangle(display, win, gc, 120, 150, 50, 60);

  /* draw a filled rectangle of the same size as above, to the left of the */
  /* previous rectangle.                                                   */
  /* draw the rectangle in a green color. */
  XSetForeground(display, gc, green.pixel);
  XFillRectangle(display, win, gc, 60, 150, 50, 60);

  /* flush all pending requests to the X server. */
  XFlush(display);

  /* make a delay for a short period. */
  sleep(4);

  /* close the connection to the X server. */
  XCloseDisplay(display);
}