void VlcWindowlessXCB::drawBackground(xcb_drawable_t drawable)
{
    /* Obtain the background color */
    xcb_colormap_t colormap = m_screen->default_colormap;
    unsigned r = 0, g = 0, b = 0;
    HTMLColor2RGB(get_options().get_bg_color().c_str(), &r, &g, &b);
    xcb_alloc_color_reply_t *reply = xcb_alloc_color_reply(m_conn,
            xcb_alloc_color(m_conn, colormap,
                            (uint16_t) r << 8,
                            (uint16_t) g << 8,
                            (uint16_t) b << 8), NULL);
    uint32_t colorpixel = reply->pixel;
    free(reply);

    /* Prepare to fill the background */
    xcb_gcontext_t background = xcb_generate_id(m_conn);
    uint32_t        mask       = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
    uint32_t        values[2]  = {colorpixel, 0};
    xcb_create_gc(m_conn, background, drawable, mask, values);
    xcb_rectangle_t rect;
    rect.x = 0;
    rect.y = 0;
    rect.width = npwindow.width;
    rect.height = npwindow.height;

    /* Fill the background */
    xcb_poly_fill_rectangle(m_conn, drawable, background, 1, &rect);
    xcb_free_gc(m_conn, background);
}
Example #2
0
int
main ()
{
    xcb_connection_t*       connection;
    xcb_screen_t*           screen;
    xcb_window_t            root_window;
    xcb_alloc_color_reply_t *color_reply;
    xcb_colormap_t          colormap = { 0 };
    uint32_t                params[1];
    uint16_t                r,g,b;

    connection = xcb_connect(NULL, NULL);
    if (!connection) {
        fprintf(stderr, "ERROR: can't connect to an X server\n");
        return -1;
    }

    screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;
    if (!screen)
    {
        fprintf(stderr, "ERROR: can't setup a screen\n");
        xcb_disconnect(connection);
        return -1;
    }

    root_window = screen->root;
    colormap = screen->default_colormap;

    r = 60000, g = 20000, b = 20000;
    color_reply = xcb_alloc_color_reply(connection,
            xcb_alloc_color(connection, colormap, r, g, b), NULL);

    if(!color_reply)
    {
        fprintf(stderr, "ERROR: can't alloc an color\n");
        xcb_disconnect(connection);
        return 0;
    }

    params[0] = color_reply->pixel;

    // sets the root_window back_pixel to pixel(params)
    xcb_change_window_attributes(connection, root_window, XCB_CW_BACK_PIXEL, params);

    // free color resources
    free(color_reply);

    // waits all things are send??
    xcb_flush(connection);

    // xcb_destroy_window(connection, window);
    xcb_disconnect(connection);

    return 0;
}
Example #3
0
File: nilwm.c Project: nqv/nilwm
static
int get_color(const char *str, uint32_t *color)
{
    if (str[0] == '#') {        /* in hex format */
        uint16_t r, g, b;
        xcb_alloc_color_cookie_t cookie;
        xcb_alloc_color_reply_t *reply;

        if (sscanf(str + 1, "%2hx%2hx%2hx", &r, &g, &b) != 3) {
            NIL_ERR("color format %s", str);
            return -1;
        }
        cookie = xcb_alloc_color(nil_.con, nil_.scr->default_colormap,
            r << 8, g << 8, b << 8);
        reply = xcb_alloc_color_reply(nil_.con, cookie, 0);
        if (!reply) {
            NIL_ERR("no color %s", str);
            return -1;
        }
        *color = reply->pixel;
        free(reply);
    } else {
        xcb_alloc_named_color_cookie_t cookie;
        xcb_alloc_named_color_reply_t *reply;

        cookie = xcb_alloc_named_color(nil_.con, nil_.scr->default_colormap,
            strlen(str), str);
        reply = xcb_alloc_named_color_reply(nil_.con, cookie, 0);
        if (!reply) {
            NIL_ERR("no color %s", str);
            return -1;
        }
        *color = reply->pixel;
        free(reply);
    }
    return 0;
}
Example #4
0
ZLEwlViewWidget::ZLEwlViewWidget(ZLApplication *application, ZLView::Angle initialAngle) : ZLViewWidget(initialAngle) {
	myApplication = application;
	w = 600;
	h = 800;

	xcb_screen_iterator_t screen_iter;
	const xcb_setup_t    *setup;
	xcb_generic_event_t  *e;
	xcb_generic_error_t  *error;
	xcb_void_cookie_t     cookie_window;
	xcb_void_cookie_t     cookie_map;
	uint32_t              mask;
	uint32_t              values[2];
	int                   screen_number;
	uint8_t               is_hand = 0;
	xcb_rectangle_t     rect_coord = { 0, 0, 600, 800};

	/* getting the connection */
	connection = xcb_connect (NULL, &screen_number);
	if (xcb_connection_has_error(connection)) {
		fprintf (stderr, "ERROR: can't connect to an X server\n");
		exit(-1);
	}

	screen = xcb_aux_get_screen (connection, screen_number);

	gc = xcb_generate_id (connection);
	mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
	values[0] = screen->black_pixel;
	values[1] = 0; /* no graphics exposures */
	xcb_create_gc (connection, gc, screen->root, mask, values);

	bgcolor = xcb_generate_id (connection);
	mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
	values[0] = screen->white_pixel;
	values[1] = 0; /* no graphics exposures */
	xcb_create_gc (connection, bgcolor, screen->root, mask, values);

	/* creating the window */
	window = xcb_generate_id(connection);
	mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
	values[0] = screen->white_pixel;
	values[1] =
		XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE |
		XCB_EVENT_MASK_BUTTON_PRESS |
		XCB_EVENT_MASK_EXPOSURE |
		XCB_EVENT_MASK_POINTER_MOTION |
		XCB_EVENT_MASK_VISIBILITY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY |
		XCB_EVENT_MASK_FOCUS_CHANGE      | XCB_EVENT_MASK_PROPERTY_CHANGE;

	uint8_t depth = xcb_aux_get_depth (connection, screen);
	xcb_create_window(connection,
			depth,
			window, screen->root,
			0, 0, 600, 800,
			0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
			screen->root_visual,
			mask, values);

	rect = xcb_generate_id (connection);
	xcb_create_pixmap (connection, depth,
			rect, window,
			600, 800);

	xcb_map_window(connection, window);

	xcb_colormap_t    colormap;
	colormap = screen->default_colormap;

	xcb_alloc_color_reply_t *rep;
	rep = xcb_alloc_color_reply (connection, xcb_alloc_color (connection, colormap, 0, 0, 0), NULL);
	pal_[0] = rep->pixel;
	free(rep);
	rep = xcb_alloc_color_reply (connection, xcb_alloc_color (connection, colormap, 0x55<<8, 0x55<<8, 0x55<<8), NULL);
	pal_[1] = rep->pixel;
	free(rep);
	rep = xcb_alloc_color_reply (connection, xcb_alloc_color (connection, colormap, 0xaa<<8, 0xaa<<8, 0xaa<<8), NULL);
	pal_[2] = rep->pixel;
	free(rep);
	rep = xcb_alloc_color_reply (connection, xcb_alloc_color (connection, colormap, 0xff<<8, 0xff<<8, 0xff<<8), NULL);
	pal_[3] = rep->pixel;
	free(rep);

	pal = pal_;

	xcb_shm_query_version_reply_t *rep_shm;

	rep_shm = xcb_shm_query_version_reply (connection,
			xcb_shm_query_version (connection),
			NULL);
	if(rep_shm) {
		xcb_image_format_t format;
		int shmctl_status;

		if (rep_shm->shared_pixmaps &&
				(rep_shm->major_version > 1 || rep_shm->minor_version > 0))
			format = (xcb_image_format_t)rep_shm->pixmap_format;
		else
			format = (xcb_image_format_t)0;

		im = xcb_image_create_native (connection, 600, 800,
				format, depth, NULL, ~0, NULL);
		assert(im);

		shminfo.shmid = shmget (IPC_PRIVATE,
				im->stride*im->height,
				IPC_CREAT | 0777);
		assert(shminfo.shmid != -1);
		shminfo.shmaddr = (uint8_t*)shmat (shminfo.shmid, 0, 0);
		assert(shminfo.shmaddr);
		im->data = shminfo.shmaddr;

		shminfo.shmseg = xcb_generate_id (connection);
		xcb_shm_attach (connection, shminfo.shmseg,
				shminfo.shmid, 0);
		shmctl_status = shmctl(shminfo.shmid, IPC_RMID, 0);
		assert(shmctl_status != -1);
		free (rep_shm);
	}

	xcb_flush(connection);
}
int main(int argc, char *argv[])
{

	int screenNumber;
	xcb_connection_t *connection = xcb_connect (NULL, &screenNumber); // DISPLAY=NULL -> uses environment DISPLAY
	const xcb_setup_t *setup = xcb_get_setup(connection);
	xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
	for (int i = 0; i < screenNumber; i++)	xcb_screen_next(&iter);
	xcb_screen_t *screen = iter.data;
	xcb_window_t root = screen->root;

	xcb_drawable_t window;
	uint32_t mask;
	uint32_t value[2];

	xcb_generic_error_t *error;
	xcb_generic_event_t *event;


	// color maps
	// use existing screen's color map
	xcb_colormap_t currentColorMap = screen->default_colormap;
	uint16_t colorRed = 255;
	// uint16_t colorRed = 65535;
	uint16_t colorGreen = 0;
	uint16_t colorBlue = 0;
	xcb_alloc_color_cookie_t allocColorCookie = xcb_alloc_color(
													connection,
													currentColorMap,
													colorRed,
													colorGreen,
													colorBlue
	);
	xcb_alloc_color_reply_t *allocColorReply = xcb_alloc_color_reply(
													connection,
													allocColorCookie,
													&error
	);

	uint32_t pixel = allocColorReply->pixel;

	delete allocColorReply;

	// create a window
	window = xcb_generate_id(connection);	// ask for an ID like in OpenGL
	std::string windowTitle = "My first window";
	std::string windowTitleIconified = "My first window (iconified)";
	mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
	value[0] = pixel;
	// value[0] = screen->white_pixel;
	value[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_KEY_RELEASE;
	xcb_create_window(connection,
					XCB_COPY_FROM_PARENT,				// depth same as parent window
					window,								// create a window using this ID
					screen->root,						// setting root as parent window
					100, 100,							// (x,y) offset from top left corner
					600, 600,							// window's width and height
					10,									// window's border
					XCB_WINDOW_CLASS_INPUT_OUTPUT,		// (uint16t) "_class" (TODO: check this out)
					screen->root_visual,				// (xcb_visualid_t) "visual" (TODO: check this out)
					mask,									// (uint32t) "value_mask" (TODO: check this out)
					value								// (const uint32_t) "*value_list" (TODO: check this out)
	);
	xcb_void_cookie_t changePropertyCookie = xcb_change_property_checked(
												connection,
												XCB_PROP_MODE_REPLACE,
												window,
												XCB_ATOM_WM_NAME,
												XCB_ATOM_STRING,
												8,
												windowTitle.size(),
												windowTitle.c_str()
	);
	if ((error = xcb_request_check(connection, changePropertyCookie))) {
		std::cerr << "ERROR: trying to change window's property" << std::endl;
		delete error;
	} else {
		std::cout << "changing window's property ....SUCCESS" << std::endl;
	}

	xcb_window_t window2 = xcb_generate_id(connection);	// ask for an ID like in OpenGL
	mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
	value[0] = screen->white_pixel;
	value[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_KEY_RELEASE;
	xcb_create_window(connection,
					XCB_COPY_FROM_PARENT,				// depth same as parent window
					window2,								// create a window using this ID
					window,						// setting root as parent window
					10, 10,							// (x,y) offset from top left corner
					400, 400,							// window's width and height
					10,									// window's border
					XCB_WINDOW_CLASS_INPUT_OUTPUT,		// (uint16t) "_class" (TODO: check this out)
					screen->root_visual,				// (xcb_visualid_t) "visual" (TODO: check this out)
					mask,									// (uint32t) "value_mask" (TODO: check this out)
					value								// (const uint32_t) "*value_list" (TODO: check this out)
	);

	xcb_map_window(connection, window);
	xcb_map_window(connection, window2);
	xcb_flush(connection);

	bool escPressed = false;
	bool windowIsMapped = true;

	while (!escPressed) {
		if ( (event = xcb_poll_for_event(connection)) ) {
			// event is NULL if there is no event. if error occurs, erros will have an error status
			// std::cout << "before switch..." << std::endl;
			switch(event->response_type & ~0x80) {
				case XCB_EXPOSE:
					{
					xcb_expose_event_t *ev = (xcb_expose_event_t *) event;
					break;
					}
				case XCB_BUTTON_PRESS:
					{
					xcb_button_press_event_t *ev = (xcb_button_press_event_t *) event;
					break;
					}
				case XCB_KEY_RELEASE:
					{
					xcb_key_release_event_t *ev = (xcb_key_release_event_t *) event;
					break;
					}
				default:
					std::cout << "unknown event" <<std::endl;
					break;
			}
			delete event;
		}

		// std::cout << "after processing events..." << std::endl;
	}
	// std::cout << "after while true..." << std::endl;

	// pause();

	// shut down connection
	xcb_disconnect(connection);
	return 0;
}
Example #6
0
static DATA8 *
x_color_alloc_rgb(int               nr,
		  int               ng,
		  int               nb,
		  xcb_connection_t *conn,
		  xcb_colormap_t    cmap,
		  xcb_visualtype_t *v)
{
   int    r, g, b, i;
   DATA8 *color_lut;
   int    sig_mask = 0;
   int    delt = 0;

   for (i = 0; i < v->bits_per_rgb_value; i++) sig_mask |= (0x1 << i);
   sig_mask <<= (16 - v->bits_per_rgb_value);
   i = 0;
   color_lut = malloc((nr) * (ng) * (nb));
   if (!color_lut) return NULL;
   delt = 0x0101 * 3;
   /* FIXME: remove the round-trip ? */
   for (r = 0; r < (nr); r++)
     {
	for (g = 0; g < (ng); g++)
	  {
	     for (b = 0; b < (nb); b++)
	       {
		  xcb_coloritem_t          xcl;
		  xcb_coloritem_t          xcl_in;
		  xcb_alloc_color_reply_t *rep;
		  int                      val;
                  int                      dr, dg, db;

                  val = (int)(((r * 255) / ((nr) - 1)));
                  val = (val << 8) | val;
		  xcl.red = (uint16_t)(val);
		  val = (int)(((g * 255) / ((ng) - 1)));
                  val = (val << 8) | val;
		  xcl.green = (uint16_t)(val);
		  val = (int)(((b * 255) / ((nb) - 1)));
                  val = (val << 8) | val;
		  xcl.blue = (uint16_t)(val);
		  xcl_in = xcl;
		  rep = xcb_alloc_color_reply(conn,
                                              xcb_alloc_color_unchecked(conn,
                                                                        cmap,
                                                                        xcl.red,
                                                                        xcl.green,
                                                                        xcl.blue),
                                              0);
                  dr = (int)xcl_in.red - (int)xcl.red;
                  if (dr < 0) dr = -dr;
                  dg = (int)xcl_in.green - (int)xcl.green;
                  if (dg < 0) dg = -dg;
                  db = (int)xcl_in.blue - (int)xcl.blue;
                  if (db < 0) db = -db;
/*
                 printf("ASK [%i]: %04x %04x %04x = %04x %04x %04x | dif = %04x / %04x\n",
                        ret,
                        xcl_in.red, xcl_in.green, xcl_in.blue,
                        xcl.red, xcl.green, xcl.blue,
                        (dr + dg +db), delt);
 */

		  /* TODO: XAllocColor tries to approach the color */
		  /* in case the allocation fails */
		  /* XCB does not that (i think). It should be done */
		  /* So if rep == NULL, the other following tests */
		  /* should be always satisfied */
		  if ((!rep) ||
                      ((dr + dg + db) > delt)
                      /*
		      ((xcl_in.red   & sig_mask) != (xcl.red   & sig_mask)) ||
		      ((xcl_in.green & sig_mask) != (xcl.green & sig_mask)) ||
		      ((xcl_in.blue  & sig_mask) != (xcl.blue  & sig_mask))
                      */
                      )
		    {
		       uint32_t pixels[256];
		       int      j;

		       if (i > 0)
			 {
			    for (j = 0; j < i; j++)
			      pixels[j] = (uint32_t)color_lut[j];
			    xcb_free_colors(conn, cmap, 0, i, pixels);
			 }
		       free(color_lut);
		       return NULL;
		    }
		  color_lut[i] = rep->pixel;
		  i++;
		  free(rep);
	       }
	  }
     }
   return color_lut;
}
Example #7
0
static DATA8 *
x_color_alloc_gray(int               ng,
		   xcb_connection_t *conn,
		   xcb_colormap_t    cmap,
		   xcb_visualtype_t *v)
{
   int g, i;
   DATA8 *color_lut;
   int sig_mask = 0;

   for (i = 0; i < v->bits_per_rgb_value; i++) sig_mask |= (0x1 << i);
   sig_mask <<= (16 - v->bits_per_rgb_value);
   i = 0;
   color_lut = malloc(ng);
   if (!color_lut) return NULL;
   /* FIXME: remove the round-trip ? */
   for (g = 0; g < (ng); g++)
     {
	xcb_coloritem_t          xcl;
	xcb_coloritem_t          xcl_in;
	int                      val;
	xcb_alloc_color_reply_t *rep;

	val = (int)(((g * 255) / ((ng) - 1)));
        val = (val << 8) | val;
	xcl.red = (uint16_t)(val);
	xcl.green = (uint16_t)(val);
	xcl.blue = (uint16_t)(val);
	xcl_in = xcl;
	rep = xcb_alloc_color_reply(conn,
                                    xcb_alloc_color_unchecked(conn,
                                                              cmap,
                                                              xcl.red,
                                                              xcl.green,
                                                              xcl.blue),
                                    0);
	/* FIXME: XAllocColor tries to approach the color */
	/* in case the allocation fails */
	/* XCB does not that (i think). It should be done */
	/* So if rep == NULL, the other following tests */
	/* should be always satisfied */
	if ((!rep) ||
	    ((xcl_in.red   & sig_mask) != (xcl.red   & sig_mask)) ||
	    ((xcl_in.green & sig_mask) != (xcl.green & sig_mask)) ||
	    ((xcl_in.blue  & sig_mask) != (xcl.blue  & sig_mask)))
	  {
	     uint32_t pixels[256];
	     int      j;

	     if (i > 0)
	       {
		  for (j = 0; j < i; j++)
		    pixels[j] = (uint32_t) color_lut[j];
		  xcb_free_colors(conn, cmap, 0, i, pixels);
	       }
	     free(color_lut);
	     return NULL;
	  }
	color_lut[i] = rep->pixel;
	i++;
	free(rep);
     }
   return color_lut;
}
Example #8
0
void xcbosd_blend(xcbosd *osd, vo_overlay_t *overlay)
{
  xcb_alloc_color_cookie_t alloc_color_cookie;
  xcb_alloc_color_reply_t *alloc_color_reply;

  if (osd->clean==UNDEFINED)
    xcbosd_clear(osd);	/* Workaround. Colorkey mode needs sc data before the clear. */

  if (overlay->rle) {
    int i, x, y, len, width;
    int use_clip_palette, max_palette_colour[2];
    uint32_t palette[2][OVL_PALETTE_SIZE];

    max_palette_colour[0] = -1;
    max_palette_colour[1] = -1;

    for (i=0, x=0, y=0; i<overlay->num_rle; i++) {
      len = overlay->rle[i].len;

      while (len > 0) {
        use_clip_palette = 0;
        if (len > overlay->width) {
          width = overlay->width;
          len -= overlay->width;
        }
        else {
          width = len;
          len = 0;
        }
        if ((y >= overlay->hili_top) && (y <= overlay->hili_bottom) && (x <= overlay->hili_right)) {
          if ((x < overlay->hili_left) && (x + width - 1 >= overlay->hili_left)) {
            width -= overlay->hili_left - x;
            len += overlay->hili_left - x;
          }
          else if (x > overlay->hili_left)  {
            use_clip_palette = 1;
            if (x + width - 1 > overlay->hili_right) {
              width -= overlay->hili_right - x;
              len += overlay->hili_right - x;
            }
          }
        }

        if (overlay->rle[i].color > max_palette_colour[use_clip_palette]) {
          int j;
          clut_t *src_clut;
          uint8_t *src_trans;

          if (use_clip_palette) {
            src_clut = (clut_t *)&overlay->hili_color;
            src_trans = (uint8_t *)&overlay->hili_trans;
          }
          else {
            src_clut = (clut_t *)&overlay->color;
            src_trans = (uint8_t *)&overlay->trans;
          }
          for (j=max_palette_colour[use_clip_palette]+1; j<=overlay->rle[i].color; j++) {
            if (src_trans[j]) {
              if (1) {
                int red, green, blue;
                int y, u, v, r, g, b;

                y = saturate(src_clut[j].y, 16, 235);
                u = saturate(src_clut[j].cb, 16, 240);
                v = saturate(src_clut[j].cr, 16, 240);
                y = (9 * y) / 8;
                r = y + (25 * v) / 16 - 218;
                red = (65536 * saturate(r, 0, 255)) / 256;
                g = y + (-13 * v) / 16 + (-25 * u) / 64 + 136;
                green = (65536 * saturate(g, 0, 255)) / 256;
                b = y + 2 * u - 274;
                blue = (65536 * saturate(b, 0, 255)) / 256;

                alloc_color_cookie = xcb_alloc_color(osd->connection, osd->cmap, red, green, blue);
                alloc_color_reply = xcb_alloc_color_reply(osd->connection, alloc_color_cookie, NULL);

                palette[use_clip_palette][j] = alloc_color_reply->pixel;
                free(alloc_color_reply);
              }
              else {
                if (src_clut[j].y > 127) {
                  palette[use_clip_palette][j] = osd->screen->white_pixel;
                }
                else {
                  palette[use_clip_palette][j] = osd->screen->black_pixel;
                }
              }
            }
            else {
              palette[use_clip_palette][j] = TRANSPARENT;
            }
          }
          max_palette_colour[use_clip_palette] = overlay->rle[i].color;
        }

        if(palette[use_clip_palette][overlay->rle[i].color] != TRANSPARENT) {
          xcb_change_gc(osd->connection, osd->gc, XCB_GC_FOREGROUND, &palette[use_clip_palette][overlay->rle[i].color]);
          xcb_rectangle_t rectangle = { overlay->x + x, overlay->y + y, width, 1 };
          xcb_poly_fill_rectangle(osd->connection, osd->bitmap, osd->gc, 1, &rectangle);
	  if(osd->mode==XCBOSD_SHAPED)
            xcb_poly_fill_rectangle(osd->connection, osd->u.shaped.mask_bitmap, osd->u.shaped.mask_gc, 1, &rectangle);
        }

        x += width;
        if (x == overlay->width) {
          x = 0;
          y++;
        }
      }
    }
    osd->clean = DRAWN;
  }
}