home *** CD-ROM | disk | FTP | other *** search
/ PC Shareware 1999 March / PCShareware-3-99.iso / IMPLE / DJGPP.RAR / DJGPP2 / XLIB-SR0.ZIP / SRC / EXTENSIO / XSHM.C < prev    next >
C/C++ Source or Header  |  1991-07-12  |  13KB  |  441 lines

  1. /*
  2.  * $XConsortium: XShm.c,v 1.14 91/07/12 09:54:02 rws Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Bob Scheifler and Keith Packard, MIT X Consortium
  24.  */
  25.  
  26. /* THIS IS NOT AN X CONSORTIUM STANDARD */
  27.  
  28. #define NEED_EVENTS
  29. #define NEED_REPLIES
  30. #include "Xlibint.h"
  31. #include "XShm.h"
  32. #include "shmstr.h"
  33. #include "Xext.h"
  34. #include "extutil.h"
  35.  
  36. static XExtensionInfo _shm_info_data;
  37. static XExtensionInfo *shm_info = &_shm_info_data;
  38. static /* const */ char *shm_extension_name = SHMNAME;
  39.  
  40. #define ShmCheckExtension(dpy,i,val) \
  41.   XextCheckExtension (dpy, i, shm_extension_name, val)
  42.  
  43. /*****************************************************************************
  44.  *                                                                           *
  45.  *               private utility routines                          *
  46.  *                                                                           *
  47.  *****************************************************************************/
  48.  
  49. static int close_display();
  50. static char *error_string();
  51. static Bool wire_to_event();
  52. static Status event_to_wire();
  53. static /* const */ XExtensionHooks shm_extension_hooks = {
  54.     NULL,                /* create_gc */
  55.     NULL,                /* copy_gc */
  56.     NULL,                /* flush_gc */
  57.     NULL,                /* free_gc */
  58.     NULL,                /* create_font */
  59.     NULL,                /* free_font */
  60.     close_display,            /* close_display */
  61.     wire_to_event,            /* wire_to_event */
  62.     event_to_wire,            /* event_to_wire */
  63.     NULL,                /* error */
  64.     error_string,            /* error_string */
  65. };
  66.  
  67. static /* const */ char *shm_error_list[] = {
  68.     "BadShmSeg",            /* BadShmSeg */
  69. };
  70.  
  71. static XEXT_GENERATE_FIND_DISPLAY (find_display, shm_info, shm_extension_name, 
  72.                    &shm_extension_hooks, ShmNumberEvents, NULL)
  73.  
  74. static XEXT_GENERATE_CLOSE_DISPLAY (close_display, shm_info)
  75.  
  76. static XEXT_GENERATE_ERROR_STRING (error_string, shm_extension_name,
  77.                    ShmNumberErrors, shm_error_list)
  78.  
  79.  
  80. static Bool wire_to_event (dpy, re, event)
  81.     Display *dpy;
  82.     XEvent  *re;
  83.     xEvent  *event;
  84. {
  85.     XExtDisplayInfo *info = find_display (dpy);
  86.     XShmCompletionEvent    *se;
  87.     xShmCompletionEvent    *sevent;
  88.  
  89.     ShmCheckExtension (dpy, info, False);
  90.  
  91.     switch ((event->u.u.type & 0x7f) - info->codes->first_event) {
  92.     case ShmCompletion:
  93.     se = (XShmCompletionEvent *) re;
  94.     sevent = (xShmCompletionEvent *) event;
  95.     se->type = sevent->type & 0x7f;
  96.     se->serial = _XSetLastRequestRead(dpy,(xGenericReply *) event);
  97.     se->send_event = (sevent->type & 0x80) != 0;
  98.     se->display = dpy;
  99.     se->drawable = sevent->drawable;
  100.     se->major_code = sevent->majorEvent;
  101.     se->minor_code = sevent->minorEvent;
  102.     se->shmseg = sevent->shmseg;
  103.     se->offset = sevent->offset;
  104.         return True;
  105.     }
  106.     return False;
  107. }
  108.  
  109. static Status event_to_wire (dpy, re, event)
  110.     Display *dpy;
  111.     XEvent  *re;
  112.     xEvent  *event;
  113. {
  114.     XExtDisplayInfo *info = find_display (dpy);
  115.     XShmCompletionEvent    *se;
  116.     xShmCompletionEvent    *sevent;
  117.  
  118.     ShmCheckExtension (dpy, info, 0);
  119.  
  120.     switch ((re->type & 0x7f) - info->codes->first_event) {
  121.     case ShmCompletion:
  122.         se = (XShmCompletionEvent *) re;
  123.     sevent = (xShmCompletionEvent *) event;
  124.         sevent->type = se->type | (se->send_event ? 0x80 : 0);
  125.         sevent->sequenceNumber = se->serial & 0xffff;
  126.         sevent->drawable = se->drawable;
  127.         sevent->majorEvent = se->major_code;
  128.         sevent->minorEvent = se->minor_code;
  129.         sevent->shmseg = se->shmseg;
  130.         sevent->offset = se->offset;
  131.         return True;
  132.     }
  133.     return False;
  134. }
  135.  
  136. /*****************************************************************************
  137.  *                                                                           *
  138.  *            public Shared Memory Extension routines                  *
  139.  *                                                                           *
  140.  *****************************************************************************/
  141.  
  142. Bool XShmQueryExtension (dpy /* event_basep, error_basep */)
  143.     Display *dpy;
  144. /*  int *event_basep, *error_basep; */
  145. {
  146.     XExtDisplayInfo *info = find_display (dpy);
  147.  
  148.     if (XextHasExtension(info)) {
  149. /*    *event_basep = info->codes->first_event;
  150.     *error_basep = info->codes->error_event; */
  151.     return True;
  152.     } else {
  153.     return False;
  154.     }
  155. }
  156.  
  157.  
  158. int XShmGetEventBase(dpy)
  159.     Display *dpy;
  160. {
  161.     XExtDisplayInfo *info = find_display (dpy);
  162.  
  163.     if (XextHasExtension(info)) {
  164.     return info->codes->first_event;
  165.     } else {
  166.     return -1;
  167.     }
  168. }
  169.  
  170.  
  171. Bool XShmQueryVersion(dpy, majorVersion, minorVersion, sharedPixmaps)
  172.     Display *dpy;
  173.     int        *majorVersion, *minorVersion;
  174.     Bool    *sharedPixmaps;
  175. {
  176.     XExtDisplayInfo *info = find_display (dpy);
  177.     xShmQueryVersionReply rep;
  178.     register xShmQueryVersionReq *req;
  179.  
  180.     ShmCheckExtension (dpy, info, False);
  181.  
  182.     LockDisplay(dpy);
  183.     GetReq(ShmQueryVersion, req);
  184.     req->reqType = info->codes->major_opcode;
  185.     req->shmReqType = X_ShmQueryVersion;
  186.     if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
  187.     UnlockDisplay(dpy);
  188.     SyncHandle();
  189.     return False;
  190.     }
  191.     *majorVersion = rep.majorVersion;
  192.     *minorVersion = rep.minorVersion;
  193.     *sharedPixmaps = rep.sharedPixmaps ? True : False;
  194.     UnlockDisplay(dpy);
  195.     SyncHandle();
  196.     return True;
  197. }
  198.  
  199.  
  200. int XShmPixmapFormat(dpy)
  201.     Display *dpy;
  202. {
  203.     XExtDisplayInfo *info = find_display (dpy);
  204.     xShmQueryVersionReply rep;
  205.     register xShmQueryVersionReq *req;
  206.  
  207.     ShmCheckExtension (dpy, info, False);
  208.  
  209.     LockDisplay(dpy);
  210.     GetReq(ShmQueryVersion, req);
  211.     req->reqType = info->codes->major_opcode;
  212.     req->shmReqType = X_ShmQueryVersion;
  213.     if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
  214.     UnlockDisplay(dpy);
  215.     SyncHandle();
  216.     return 0;
  217.     }
  218.     UnlockDisplay(dpy);
  219.     SyncHandle();
  220.     if (rep.sharedPixmaps &&
  221.     (rep.majorVersion > 1 || rep.minorVersion > 0))
  222.     return rep.pixmapFormat;
  223.     return 0;
  224. }
  225.  
  226.  
  227. Status XShmAttach(dpy, shminfo)
  228.     Display *dpy;
  229.     XShmSegmentInfo *shminfo;
  230. {
  231.     XExtDisplayInfo *info = find_display (dpy);
  232.     register xShmAttachReq *req;
  233.  
  234.     ShmCheckExtension (dpy, info, 0);
  235.  
  236.     shminfo->shmseg = XAllocID(dpy);
  237.     LockDisplay(dpy);
  238.     GetReq(ShmAttach, req);
  239.     req->reqType = info->codes->major_opcode;
  240.     req->shmReqType = X_ShmAttach;
  241.     req->shmseg = shminfo->shmseg;
  242.     req->shmid = shminfo->shmid;
  243.     req->readOnly = shminfo->readOnly ? xTrue : xFalse;
  244.     UnlockDisplay(dpy);
  245.     SyncHandle();
  246.     return 1;
  247. }
  248.  
  249.  
  250. Status XShmDetach(dpy, shminfo)
  251.     Display *dpy;
  252.     XShmSegmentInfo *shminfo;
  253. {
  254.     XExtDisplayInfo *info = find_display (dpy);
  255.     register xShmDetachReq *req;
  256.  
  257.     ShmCheckExtension (dpy, info, 0);
  258.  
  259.     LockDisplay(dpy);
  260.     GetReq(ShmDetach, req);
  261.     req->reqType = info->codes->major_opcode;
  262.     req->shmReqType = X_ShmDetach;
  263.     req->shmseg = shminfo->shmseg;
  264.     UnlockDisplay(dpy);
  265.     SyncHandle();
  266.     return 1;
  267. }
  268.  
  269. static int _XShmDestroyImage (ximage)
  270.     XImage *ximage;
  271.  
  272. {
  273.     Xfree((char *)ximage);
  274.     return 1;
  275. }
  276.  
  277. #define ROUNDUP(nbytes, pad) ((((nbytes) + ((pad) - 1)) / (pad)) * (pad))
  278.  
  279. XImage *XShmCreateImage (dpy, visual, depth, format, data, shminfo,
  280.              width, height)
  281.     register Display *dpy;
  282.     register Visual *visual;
  283.     unsigned int depth;
  284.     int format;
  285.     char *data;
  286.     XShmSegmentInfo *shminfo;
  287.     unsigned int width;
  288.     unsigned int height;
  289. {
  290.     register XImage *image;
  291.  
  292.     image = (XImage *)Xcalloc(1, (unsigned)sizeof(XImage));
  293.     if (!image)
  294.     return image;
  295.     image->data = data;
  296.     image->obdata = (char *)shminfo;
  297.     image->width = width;
  298.     image->height = height;
  299.     image->depth = depth;
  300.     image->format = format;
  301.     image->byte_order = dpy->byte_order;
  302.     image->bitmap_unit = dpy->bitmap_unit;
  303.     image->bitmap_bit_order = dpy->bitmap_bit_order;
  304.     image->bitmap_pad = _XGetScanlinePad(dpy, depth);
  305.     image->xoffset = 0;
  306.     if (visual) {
  307.     image->red_mask = visual->red_mask;
  308.     image->green_mask = visual->green_mask;
  309.     image->blue_mask = visual->blue_mask;
  310.     } else {
  311.     image->red_mask = image->green_mask = image->blue_mask = 0;
  312.     }
  313.     if (format == ZPixmap)
  314.     image->bits_per_pixel = _XGetBitsPerPixel(dpy, (int)depth);
  315.     else
  316.     image->bits_per_pixel = 1;
  317.     image->bytes_per_line = ROUNDUP((image->bits_per_pixel * width),
  318.                     image->bitmap_pad) >> 3;
  319.     _XInitImageFuncPtrs(image);
  320.     image->f.destroy_image = _XShmDestroyImage;
  321.     return image;
  322. }
  323.  
  324. Status XShmPutImage (dpy, d, gc, image, src_x, src_y, dst_x, dst_y,
  325.              src_width, src_height, send_event)
  326.     register Display *dpy;
  327.     Drawable d;
  328.     GC gc;
  329.     register XImage *image;
  330.     int src_x, src_y, dst_x, dst_y;
  331.     unsigned int src_width, src_height;
  332.     Bool send_event;
  333. {
  334.     XExtDisplayInfo *info = find_display (dpy);
  335.     XShmSegmentInfo *shminfo = (XShmSegmentInfo *)image->obdata;
  336.     register xShmPutImageReq *req;
  337.  
  338.     ShmCheckExtension (dpy, info, 0);
  339.     if (!shminfo) return 0;
  340.  
  341.     LockDisplay(dpy);
  342.     GetReq(ShmPutImage, req);
  343.     req->reqType = info->codes->major_opcode;
  344.     req->shmReqType = X_ShmPutImage;
  345.     req->drawable = d;
  346.     req->gc = gc->gid;
  347.     req->srcX = src_x;
  348.     req->srcY = src_y;
  349.     req->srcWidth = src_width;
  350.     req->srcHeight = src_height;
  351.     req->dstX = dst_x;
  352.     req->dstY = dst_y;
  353.     req->totalWidth = image->width;
  354.     req->totalHeight = image->height;
  355.     req->depth = image->depth;
  356.     req->format = image->format;
  357.     req->sendEvent = send_event;
  358.     req->shmseg = shminfo->shmseg;
  359.     req->offset = image->data - shminfo->shmaddr;
  360.     UnlockDisplay(dpy);
  361.     SyncHandle();
  362.     return 1;
  363. }
  364.  
  365.  
  366. Status XShmGetImage(dpy, d, image, x, y, plane_mask)
  367.     register Display *dpy;
  368.     Drawable d;
  369.     XImage *image;
  370.     int x, y;
  371.     unsigned long plane_mask;
  372. {
  373.     XExtDisplayInfo *info = find_display (dpy);
  374.     XShmSegmentInfo *shminfo = (XShmSegmentInfo *)image->obdata;
  375.     register xShmGetImageReq *req;
  376.     xShmGetImageReply rep;
  377.     register Visual *visual;
  378.  
  379.     ShmCheckExtension (dpy, info, 0);
  380.     if (!shminfo) return 0;
  381.  
  382.     LockDisplay(dpy);
  383.     GetReq(ShmGetImage, req);
  384.     req->reqType = info->codes->major_opcode;
  385.     req->shmReqType = X_ShmGetImage;
  386.     req->drawable = d;
  387.     req->x = x;
  388.     req->y = y;
  389.     req->width = image->width;
  390.     req->height = image->height;
  391.     req->planeMask = plane_mask;
  392.     req->format = image->format;
  393.     req->shmseg = shminfo->shmseg;
  394.     req->offset = image->data - shminfo->shmaddr;
  395.     if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
  396.     UnlockDisplay(dpy);
  397.     SyncHandle();
  398.     return 0;
  399.     }
  400.     visual = _XVIDtoVisual(dpy, rep.visual);
  401.     if (visual) {
  402.         image->red_mask = visual->red_mask;
  403.         image->green_mask = visual->green_mask;
  404.         image->blue_mask = visual->blue_mask;
  405.     } else {
  406.     image->red_mask = image->green_mask = image->blue_mask = 0;
  407.     }
  408.     UnlockDisplay(dpy);
  409.     SyncHandle();
  410.     return 1;
  411. }
  412.  
  413. Pixmap XShmCreatePixmap (dpy, d, data, shminfo, width, height, depth)
  414.     register Display *dpy;
  415.     Drawable d;
  416.     char *data;
  417.     XShmSegmentInfo *shminfo;
  418.     unsigned int width, height, depth;
  419. {
  420.     XExtDisplayInfo *info = find_display (dpy);
  421.     Pixmap pid;
  422.     register xShmCreatePixmapReq *req;
  423.  
  424.     ShmCheckExtension (dpy, info, 0);
  425.  
  426.     LockDisplay(dpy);
  427.     GetReq(ShmCreatePixmap, req);
  428.     req->reqType = info->codes->major_opcode;
  429.     req->shmReqType = X_ShmCreatePixmap;
  430.     req->drawable = d;
  431.     req->width = width;
  432.     req->height = height;
  433.     req->depth = depth;
  434.     req->shmseg = shminfo->shmseg;
  435.     req->offset = data - shminfo->shmaddr;
  436.     pid = req->pid = XAllocID(dpy);
  437.     UnlockDisplay(dpy);
  438.     SyncHandle();
  439.     return pid;
  440. }
  441.