File: XDisplay.cc

package info (click to toggle)
pyxine 0.1alpha2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 852 kB
  • ctags: 2,017
  • sloc: ansic: 7,512; python: 2,224; cpp: 1,810; makefile: 160; sh: 40
file content (180 lines) | stat: -rw-r--r-- 4,201 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* -*-c++-*-
 *
 * $Id: XDisplay.cc,v 1.2 2003/02/12 18:06:19 dairiki Exp $
 *
 * Copyright (C) 2003 Geoffrey T. Dairiki
 *
 * This file is part of Pyxine, Python bindings for xine.
 *
 * Pyxine is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Pyxine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
#pragma implementation
#include "XDisplay.h"

BEGIN_PXLIB_NAMESPACE

class XDisplayLock
{
  Display * display;
public:
  XDisplayLock(Display * _d) : display(_d) { XLockDisplay(display); }
  ~XDisplayLock() { XUnlockDisplay(display); }
};

////////////////////////////////////////////////////////////////


XDisplay::XDisplay (const char * display_name)
  : name(XDisplayName(display_name))
{
  static bool seen = false;

  if (!seen) {
    if (!XInitThreads())
      throw Error("Your Xlib doesn't support threads?");
    seen = true;
  }
  
  display = XOpenDisplay(name.c_str());
  if (!display)
    throw Error("Can't open display");
}

XDisplay::~XDisplay ()
{
  //FIXME:cerr << "XDisplay destroy" << endl;
  XDisplayLock lock(display);
  XCloseDisplay(display);
  //FIXME:cerr << "XDisplay destroyed" << endl;
}

Bool
XDisplay::pick_any_event (Display *, XEvent *, XPointer)
{
  return True;
}

bool
XDisplay::get_event (XEvent * e)
{
  XDisplayLock l(display);
  return XCheckIfEvent(display, e, pick_any_event, 0);
}

void
XDisplay::next_event (XEvent * e)
{
  pthread_testcancel();
  while (! get_event(e)) {
    int fd = ConnectionNumber(display);
    fd_set rfds;
    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    fd_set efds = rfds;
    select(fd+1, &rfds, 0, &efds, 0);
    pthread_testcancel();
  }
}

int
XDisplay::get_screen_number_of_window (Window w)
{
  XWindowAttributes attr;
  XDisplayLock xlock(display);
  if (!XGetWindowAttributes(display, w, &attr))
    throw Error("XGetWindowAttributes failed");
  return XScreenNumberOfScreen(attr.screen);
}

double
XDisplay::get_pixel_aspect(int screen)
{
  XDisplayLock lock(display);
  double res_h = ( (double)DisplayWidth(display, screen)
		   / DisplayWidthMM(display, screen));
  double res_v = ( (double)DisplayHeight(display, screen)
		   / DisplayHeightMM(display, screen));
  return res_v / res_h;
}
  
WindowGeometry
XDisplay::get_window_geometry(Window w)
{
  WindowGeometry g;
  Window _window;
  unsigned width, height, _border_width, _depth;
  
  XDisplayLock lock(display);

  if (!XGetGeometry(display, w,
		    &_window,
		    &g.x0, &g.y0, &width, &height,
		    &_border_width, &_depth))
    throw Error("XGetGeometry failed");
  
  g.width = width;
  g.height = height;
  
  int screen = get_screen_number_of_window(w);
  g.pixel_aspect = get_pixel_aspect(screen);

  return g;
}

WindowGeometry
XDisplay::get_window_geometry(const XConfigureEvent& e)
{
  WindowGeometry g;
  g.width  = e.width;
  g.height = e.height;

  if (e.display != display)
    cerr << "Warning: event.display != display" << endl;
    
#if 0
  // This doesn't seem to work...
  g.x0 = e.x;
  g.y0 = e.y;
#else
  Window tmp_win;
  XDisplayLock lock(e.display);
  XTranslateCoordinates(e.display, e.window, DefaultRootWindow(e.display),
			0, 0, &g.x0, &g.y0, &tmp_win);
#endif

  int screen = get_screen_number_of_window(e.window);
  g.pixel_aspect = get_pixel_aspect(screen);

  return g;
}

void
XDisplay::select_input(Window w, long event_mask)
{
  XDisplayLock xlock(display);
  XSelectInput(display, w, event_mask);
}

int
XDisplay::get_ShmCompletionEvent_type() const
{
  static int SHM_COMPLETION = 0;
  if (SHM_COMPLETION == 0) {
    SHM_COMPLETION = XShmGetEventBase(display) + ShmCompletion;
  }
  return SHM_COMPLETION;
}

END_PXLIB_NAMESPACE