~ubuntu-branches/debian/lenny/italc/lenny

« back to all changes in this revision

Viewing changes to client/demoviewer/shm.c

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Winnertz
  • Date: 2008-06-17 13:46:54 UTC
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20080617134654-2y5m7ki93r5c1ysf
Tags: upstream-1.0.9~rc3
ImportĀ upstreamĀ versionĀ 1.0.9~rc3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
3
 
 *
4
 
 *  This is free software; you can redistribute it and/or modify
5
 
 *  it under the terms of the GNU General Public License as published by
6
 
 *  the Free Software Foundation; either version 2 of the License, or
7
 
 *  (at your option) any later version.
8
 
 *
9
 
 *  This software is distributed in the hope that it will be useful,
10
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 *  GNU General Public License for more details.
13
 
 *
14
 
 *  You should have received a copy of the GNU General Public License
15
 
 *  along with this software; if not, write to the Free Software
16
 
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
17
 
 *  USA.
18
 
 */
19
 
 
20
 
/*
21
 
 * shm.c - code to set up shared memory extension.
22
 
 */
23
 
 
24
 
#include <demoviewer.h>
25
 
#include <sys/ipc.h>
26
 
#include <sys/shm.h>
27
 
#include <X11/extensions/XShm.h>
28
 
static XShmSegmentInfo shminfo;
29
 
 
30
 
static Bool caughtShmError = False;
31
 
static Bool needShmCleanup = False;
32
 
 
33
 
void
34
 
ShmCleanup()
35
 
{
36
 
  fprintf(stderr,"ShmCleanup called\n");
37
 
  if (needShmCleanup) {
38
 
    shmdt(shminfo.shmaddr);
39
 
    shmctl(shminfo.shmid, IPC_RMID, 0);
40
 
    needShmCleanup = False;
41
 
  }
42
 
}
43
 
 
44
 
static int
45
 
ShmCreationXErrorHandler(Display *dpy, XErrorEvent *error)
46
 
{
47
 
  caughtShmError = True;
48
 
  return 0;
49
 
}
50
 
 
51
 
XImage *
52
 
CreateShmImage()
53
 
{
54
 
  XImage *image;
55
 
  XErrorHandler oldXErrorHandler;
56
 
 
57
 
  if (!XShmQueryExtension(dpy))
58
 
    return NULL;
59
 
 
60
 
  image = XShmCreateImage(dpy, vis, visdepth, ZPixmap, NULL, &shminfo,
61
 
                          si.framebufferWidth, si.framebufferHeight);
62
 
  if (!image) return NULL;
63
 
 
64
 
  shminfo.shmid = shmget(IPC_PRIVATE,
65
 
                         image->bytes_per_line * image->height,
66
 
                         IPC_CREAT|0777);
67
 
 
68
 
  if (shminfo.shmid == -1) {
69
 
    XDestroyImage(image);
70
 
    return NULL;
71
 
  }
72
 
 
73
 
  shminfo.shmaddr = image->data = shmat(shminfo.shmid, 0, 0);
74
 
 
75
 
  if (shminfo.shmaddr == (char *)-1) {
76
 
    XDestroyImage(image);
77
 
    shmctl(shminfo.shmid, IPC_RMID, 0);
78
 
    return NULL;
79
 
  }
80
 
 
81
 
  shminfo.readOnly = True;
82
 
 
83
 
  oldXErrorHandler = XSetErrorHandler(ShmCreationXErrorHandler);
84
 
  XShmAttach(dpy, &shminfo);
85
 
  XSync(dpy, False);
86
 
  XSetErrorHandler(oldXErrorHandler);
87
 
 
88
 
  if (caughtShmError) {
89
 
    XDestroyImage(image);
90
 
    shmdt(shminfo.shmaddr);
91
 
    shmctl(shminfo.shmid, IPC_RMID, 0);
92
 
    return NULL;
93
 
  }
94
 
 
95
 
  needShmCleanup = True;
96
 
 
97
 
  fprintf(stderr,"Using shared memory PutImage\n");
98
 
 
99
 
  return image;
100
 
}