Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <unistd.h>
21 : #include <fcntl.h>
22 :
23 : #include <stdio.h>
24 : #include <string.h>
25 : #include <stdlib.h>
26 : #include <limits.h>
27 : #include <errno.h>
28 : #include <poll.h>
29 : #if defined(FREEBSD) || defined(NETBSD)
30 : #include <sys/types.h>
31 : #include <sys/time.h>
32 : #endif
33 : #include <unx/gtk/gtkdata.hxx>
34 : #include <unx/gtk/gtkinst.hxx>
35 : #include <unx/gtk/gtkframe.hxx>
36 : #include <unx/salobj.h>
37 : #include <generic/geninst.h>
38 : #include <osl/thread.h>
39 : #include <osl/process.h>
40 :
41 : #include "unx/i18n_im.hxx"
42 : #include "unx/i18n_xkb.hxx"
43 : #include <unx/wmadaptor.hxx>
44 :
45 : #include "unx/x11_cursors/salcursors.h"
46 :
47 : #include <vcl/svapp.hxx>
48 :
49 : using namespace vcl_sal;
50 :
51 :
52 : /***************************************************************
53 : * class GtkSalDisplay *
54 : ***************************************************************/
55 : extern "C" {
56 0 : GdkFilterReturn call_filterGdkEvent( GdkXEvent* sys_event,
57 : GdkEvent* event,
58 : gpointer data )
59 : {
60 0 : GtkSalDisplay *pDisplay = (GtkSalDisplay *)data;
61 0 : return pDisplay->filterGdkEvent( sys_event, event );
62 : }
63 : }
64 :
65 0 : GtkSalDisplay::GtkSalDisplay( GdkDisplay* pDisplay ) :
66 : #if !GTK_CHECK_VERSION(3,0,0)
67 : SalDisplay( gdk_x11_display_get_xdisplay( pDisplay ) ),
68 : #endif
69 0 : m_pSys( GtkSalSystem::GetSingleton() ),
70 : m_pGdkDisplay( pDisplay ),
71 0 : m_bStartupCompleted( false )
72 : {
73 0 : for(int i = 0; i < POINTER_COUNT; i++)
74 0 : m_aCursors[ i ] = NULL;
75 : #if !GTK_CHECK_VERSION(3,0,0)
76 0 : m_bUseRandRWrapper = false; // use gdk signal instead
77 0 : Init ();
78 : #endif
79 :
80 : // FIXME: unify this with SalInst's filter too ?
81 0 : gdk_window_add_filter( NULL, call_filterGdkEvent, this );
82 :
83 0 : if ( getenv( "SAL_IGNOREXERRORS" ) )
84 0 : GetGenericData()->ErrorTrapPush(); // and leak the trap
85 :
86 : #if GTK_CHECK_VERSION(3,0,0)
87 : m_bX11Display = GDK_IS_X11_DISPLAY( m_pGdkDisplay );
88 : #else
89 0 : m_bX11Display = true;
90 : #endif
91 0 : }
92 :
93 0 : GtkSalDisplay::~GtkSalDisplay()
94 : {
95 0 : gdk_window_remove_filter( NULL, call_filterGdkEvent, this );
96 :
97 0 : if( !m_bStartupCompleted )
98 0 : gdk_notify_startup_complete();
99 :
100 : #if !GTK_CHECK_VERSION(3,0,0)
101 0 : doDestruct();
102 0 : pDisp_ = NULL;
103 : #endif
104 :
105 0 : for(int i = 0; i < POINTER_COUNT; i++)
106 0 : if( m_aCursors[ i ] )
107 0 : gdk_cursor_unref( m_aCursors[ i ] );
108 0 : }
109 :
110 : extern "C" {
111 :
112 0 : void signalScreenSizeChanged( GdkScreen* pScreen, gpointer data )
113 : {
114 0 : GtkSalDisplay* pDisp = (GtkSalDisplay*)data;
115 0 : pDisp->screenSizeChanged( pScreen );
116 0 : }
117 :
118 0 : void signalMonitorsChanged( GdkScreen* pScreen, gpointer data )
119 : {
120 0 : GtkSalDisplay* pDisp = (GtkSalDisplay*)data;
121 0 : pDisp->monitorsChanged( pScreen );
122 0 : }
123 :
124 : }
125 :
126 0 : GdkFilterReturn GtkSalDisplay::filterGdkEvent( GdkXEvent* sys_event,
127 : GdkEvent* )
128 : {
129 : #if !GTK_CHECK_VERSION(3,0,0)
130 0 : GdkFilterReturn aFilterReturn = GDK_FILTER_CONTINUE;
131 0 : XEvent *pEvent = (XEvent *)sys_event;
132 :
133 : // dispatch all XEvents to event callback
134 0 : if( GetSalData()->m_pInstance->
135 0 : CallEventCallback( pEvent, sizeof( XEvent ) ) )
136 0 : aFilterReturn = GDK_FILTER_REMOVE;
137 :
138 0 : if (GetDisplay() == pEvent->xany.display )
139 : {
140 : // #i53471# gtk has no callback mechanism that lets us be notified
141 : // when settings (as in XSETTING and opposed to styles) are changed.
142 : // so we need to listen for corresponding property notifications here
143 : // these should be rare enough so that we can assume that the settings
144 : // actually change when a corresponding PropertyNotify occurs
145 0 : if( pEvent->type == PropertyNotify &&
146 0 : pEvent->xproperty.atom == getWMAdaptor()->getAtom( WMAdaptor::XSETTINGS ) &&
147 0 : ! m_aFrames.empty()
148 : )
149 : {
150 0 : SendInternalEvent( m_aFrames.front(), NULL, SALEVENT_SETTINGSCHANGED );
151 : }
152 : // let's see if one of our frames wants to swallow these events
153 : // get the frame
154 0 : for( std::list< SalFrame* >::const_iterator it = m_aFrames.begin();
155 0 : it != m_aFrames.end(); ++it )
156 : {
157 0 : GtkSalFrame* pFrame = static_cast<GtkSalFrame*>(*it);
158 0 : if( (GdkNativeWindow)pFrame->GetSystemData()->aWindow == pEvent->xany.window ||
159 0 : ( pFrame->getForeignParent() && pFrame->getForeignParentWindow() == pEvent->xany.window ) ||
160 0 : ( pFrame->getForeignTopLevel() && pFrame->getForeignTopLevelWindow() == pEvent->xany.window )
161 : )
162 : {
163 0 : if( ! pFrame->Dispatch( pEvent ) )
164 0 : aFilterReturn = GDK_FILTER_REMOVE;
165 0 : break;
166 : }
167 : }
168 0 : X11SalObject::Dispatch( pEvent );
169 : }
170 :
171 0 : return aFilterReturn;
172 : #else
173 : (void) sys_event;
174 : #warning FIXME: implement filterGdkEvent ...
175 : return GDK_FILTER_CONTINUE;
176 : #endif
177 : }
178 :
179 0 : void GtkSalDisplay::screenSizeChanged( GdkScreen* pScreen )
180 : {
181 0 : m_pSys->countScreenMonitors();
182 0 : if (pScreen)
183 0 : emitDisplayChanged();
184 0 : }
185 :
186 0 : void GtkSalDisplay::monitorsChanged( GdkScreen* pScreen )
187 : {
188 0 : m_pSys->countScreenMonitors();
189 0 : if (pScreen)
190 0 : emitDisplayChanged();
191 0 : }
192 :
193 : #if !GTK_CHECK_VERSION(3,0,0)
194 : SalDisplay::ScreenData *
195 0 : GtkSalDisplay::initScreen( SalX11Screen nXScreen ) const
196 : {
197 : // choose visual for screen
198 : ScreenData *pSD;
199 0 : if (!(pSD = SalDisplay::initScreen( nXScreen )))
200 0 : return NULL;
201 :
202 : // now set a gdk default colormap matching the chosen visual to the screen
203 0 : GdkScreen* pScreen = gdk_display_get_screen( m_pGdkDisplay, nXScreen.getXScreen() );
204 : // should really use this:
205 : // GdkVisual* pVis = gdk_x11_screen_lookup_visual_get( screen, pSD->m_aVisual.visualid );
206 : // and not this:
207 0 : GdkVisual* pVis = gdkx_visual_get( pSD->m_aVisual.visualid );
208 0 : if( pVis )
209 : {
210 0 : GdkColormap* pDefCol = gdk_screen_get_default_colormap( pScreen );
211 0 : GdkVisual* pDefVis = gdk_colormap_get_visual( pDefCol );
212 0 : if( pDefVis != pVis )
213 : {
214 0 : pDefCol = gdk_x11_colormap_foreign_new( pVis, pSD->m_aColormap.GetXColormap() );
215 0 : gdk_screen_set_default_colormap( pScreen, pDefCol );
216 : SAL_INFO( "vcl.gtk", "set new gdk color map for screen " << nXScreen.getXScreen() );
217 : }
218 : }
219 : else
220 : SAL_INFO( "vcl.gtk", "not GdkVisual for visual id " << pSD->m_aVisual.visualid );
221 :
222 0 : return pSD;
223 : }
224 :
225 0 : long GtkSalDisplay::Dispatch( XEvent* pEvent )
226 : {
227 0 : if( GetDisplay() == pEvent->xany.display )
228 : {
229 : // let's see if one of our frames wants to swallow these events
230 : // get the child frame
231 0 : for( std::list< SalFrame* >::const_iterator it = m_aFrames.begin();
232 0 : it != m_aFrames.end(); ++it )
233 : {
234 0 : if( (GdkNativeWindow)(*it)->GetSystemData()->aWindow == pEvent->xany.window )
235 0 : return static_cast<GtkSalFrame*>(*it)->Dispatch( pEvent );
236 : }
237 : }
238 :
239 0 : return GDK_FILTER_CONTINUE;
240 : }
241 : #endif
242 :
243 : #if GTK_CHECK_VERSION(3,0,0)
244 : namespace
245 : {
246 : //cairo annoyingly won't take raw xbm data unless it fits
247 : //the required cairo stride
248 : unsigned char* ensurePaddedForCairo(const unsigned char *pXBM,
249 : int nWidth, int nHeight, int nStride)
250 : {
251 : unsigned char *pPaddedXBM = const_cast<unsigned char*>(pXBM);
252 :
253 : int bytes_per_row = (nWidth + 7) / 8;
254 :
255 : if (nStride != bytes_per_row)
256 : {
257 : pPaddedXBM = new unsigned char[nStride * nHeight];
258 : for (int row = 0; row < nHeight; ++row)
259 : {
260 : memcpy(pPaddedXBM + (nStride * row),
261 : pXBM + (bytes_per_row * row), bytes_per_row);
262 : memset(pPaddedXBM + (nStride * row) + bytes_per_row,
263 : 0, nStride - bytes_per_row);
264 : }
265 : }
266 :
267 : return pPaddedXBM;
268 : }
269 : }
270 : #endif
271 :
272 0 : GdkCursor* GtkSalDisplay::getFromXBM( const unsigned char *pBitmap,
273 : const unsigned char *pMask,
274 : int nWidth, int nHeight,
275 : int nXHot, int nYHot )
276 : {
277 : #if GTK_CHECK_VERSION(3,0,0)
278 : int cairo_stride = cairo_format_stride_for_width(CAIRO_FORMAT_A1, nWidth);
279 :
280 : unsigned char *pPaddedXBM = ensurePaddedForCairo(pBitmap, nWidth, nHeight, cairo_stride);
281 : cairo_surface_t *s = cairo_image_surface_create_for_data(
282 : pPaddedXBM,
283 : CAIRO_FORMAT_A1, nWidth, nHeight,
284 : cairo_stride);
285 :
286 : cairo_t *cr = cairo_create(s);
287 : unsigned char *pPaddedMaskXBM = ensurePaddedForCairo(pMask, nWidth, nHeight, cairo_stride);
288 : cairo_surface_t *mask = cairo_image_surface_create_for_data(
289 : pPaddedMaskXBM,
290 : CAIRO_FORMAT_A1, nWidth, nHeight,
291 : cairo_stride);
292 : cairo_mask_surface(cr, mask, 0, 0);
293 : cairo_destroy(cr);
294 : cairo_surface_destroy(mask);
295 : if (pPaddedMaskXBM != pMask)
296 : delete [] pPaddedMaskXBM;
297 :
298 : GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface(s, 0, 0, nWidth, nHeight);
299 : cairo_surface_destroy(s);
300 : if (pPaddedXBM != pBitmap)
301 : delete [] pPaddedXBM;
302 :
303 : GdkCursor *cursor = gdk_cursor_new_from_pixbuf(m_pGdkDisplay, pixbuf, nXHot, nYHot);
304 : g_object_unref(pixbuf);
305 :
306 : return cursor;
307 : #else
308 0 : GdkScreen *pScreen = gdk_display_get_default_screen( m_pGdkDisplay );
309 0 : GdkDrawable *pDrawable = GDK_DRAWABLE( gdk_screen_get_root_window (pScreen) );
310 : GdkBitmap *pBitmapPix = gdk_bitmap_create_from_data
311 0 : ( pDrawable, reinterpret_cast<const char*>(pBitmap), nWidth, nHeight );
312 : GdkBitmap *pMaskPix = gdk_bitmap_create_from_data
313 0 : ( pDrawable, reinterpret_cast<const char*>(pMask), nWidth, nHeight );
314 0 : GdkColormap *pColormap = gdk_drawable_get_colormap( pDrawable );
315 :
316 0 : GdkColor aWhite = { 0, 0xffff, 0xffff, 0xffff };
317 0 : GdkColor aBlack = { 0, 0, 0, 0 };
318 :
319 0 : gdk_colormap_alloc_color( pColormap, &aBlack, FALSE, TRUE);
320 0 : gdk_colormap_alloc_color( pColormap, &aWhite, FALSE, TRUE);
321 :
322 : return gdk_cursor_new_from_pixmap
323 : ( pBitmapPix, pMaskPix,
324 0 : &aBlack, &aWhite, nXHot, nYHot);
325 : #endif
326 : }
327 :
328 : #define MAKE_CURSOR( vcl_name, name ) \
329 : case vcl_name: \
330 : pCursor = getFromXBM( name##curs##_bits, name##mask##_bits, \
331 : name##curs_width, name##curs_height, \
332 : name##curs_x_hot, name##curs_y_hot ); \
333 : break
334 : #define MAP_BUILTIN( vcl_name, gdk_name ) \
335 : case vcl_name: \
336 : pCursor = gdk_cursor_new_for_display( m_pGdkDisplay, gdk_name ); \
337 : break
338 :
339 0 : GdkCursor *GtkSalDisplay::getCursor( PointerStyle ePointerStyle )
340 : {
341 0 : if( ePointerStyle >= POINTER_COUNT )
342 0 : return NULL;
343 :
344 0 : if ( !m_aCursors[ ePointerStyle ] )
345 : {
346 0 : GdkCursor *pCursor = NULL;
347 :
348 0 : switch( ePointerStyle )
349 : {
350 0 : MAP_BUILTIN( POINTER_ARROW, GDK_LEFT_PTR );
351 0 : MAP_BUILTIN( POINTER_TEXT, GDK_XTERM );
352 0 : MAP_BUILTIN( POINTER_HELP, GDK_QUESTION_ARROW );
353 0 : MAP_BUILTIN( POINTER_CROSS, GDK_CROSSHAIR );
354 0 : MAP_BUILTIN( POINTER_WAIT, GDK_WATCH );
355 :
356 0 : MAP_BUILTIN( POINTER_NSIZE, GDK_SB_V_DOUBLE_ARROW );
357 0 : MAP_BUILTIN( POINTER_SSIZE, GDK_SB_V_DOUBLE_ARROW );
358 0 : MAP_BUILTIN( POINTER_WSIZE, GDK_SB_H_DOUBLE_ARROW );
359 0 : MAP_BUILTIN( POINTER_ESIZE, GDK_SB_H_DOUBLE_ARROW );
360 :
361 0 : MAP_BUILTIN( POINTER_NWSIZE, GDK_TOP_LEFT_CORNER );
362 0 : MAP_BUILTIN( POINTER_NESIZE, GDK_TOP_RIGHT_CORNER );
363 0 : MAP_BUILTIN( POINTER_SWSIZE, GDK_BOTTOM_LEFT_CORNER );
364 0 : MAP_BUILTIN( POINTER_SESIZE, GDK_BOTTOM_RIGHT_CORNER );
365 :
366 0 : MAP_BUILTIN( POINTER_WINDOW_NSIZE, GDK_TOP_SIDE );
367 0 : MAP_BUILTIN( POINTER_WINDOW_SSIZE, GDK_BOTTOM_SIDE );
368 0 : MAP_BUILTIN( POINTER_WINDOW_WSIZE, GDK_LEFT_SIDE );
369 0 : MAP_BUILTIN( POINTER_WINDOW_ESIZE, GDK_RIGHT_SIDE );
370 :
371 0 : MAP_BUILTIN( POINTER_WINDOW_NWSIZE, GDK_TOP_LEFT_CORNER );
372 0 : MAP_BUILTIN( POINTER_WINDOW_NESIZE, GDK_TOP_RIGHT_CORNER );
373 0 : MAP_BUILTIN( POINTER_WINDOW_SWSIZE, GDK_BOTTOM_LEFT_CORNER );
374 0 : MAP_BUILTIN( POINTER_WINDOW_SESIZE, GDK_BOTTOM_RIGHT_CORNER );
375 :
376 0 : MAP_BUILTIN( POINTER_HSIZEBAR, GDK_SB_H_DOUBLE_ARROW );
377 0 : MAP_BUILTIN( POINTER_VSIZEBAR, GDK_SB_V_DOUBLE_ARROW );
378 :
379 0 : MAP_BUILTIN( POINTER_REFHAND, GDK_HAND2 );
380 0 : MAP_BUILTIN( POINTER_HAND, GDK_HAND2 );
381 0 : MAP_BUILTIN( POINTER_PEN, GDK_PENCIL );
382 :
383 0 : MAP_BUILTIN( POINTER_HSPLIT, GDK_SB_H_DOUBLE_ARROW );
384 0 : MAP_BUILTIN( POINTER_VSPLIT, GDK_SB_V_DOUBLE_ARROW );
385 :
386 0 : MAP_BUILTIN( POINTER_MOVE, GDK_FLEUR );
387 :
388 0 : MAKE_CURSOR( POINTER_NULL, null );
389 0 : MAKE_CURSOR( POINTER_MAGNIFY, magnify_ );
390 0 : MAKE_CURSOR( POINTER_FILL, fill_ );
391 0 : MAKE_CURSOR( POINTER_MOVEDATA, movedata_ );
392 0 : MAKE_CURSOR( POINTER_COPYDATA, copydata_ );
393 0 : MAKE_CURSOR( POINTER_MOVEFILE, movefile_ );
394 0 : MAKE_CURSOR( POINTER_COPYFILE, copyfile_ );
395 0 : MAKE_CURSOR( POINTER_MOVEFILES, movefiles_ );
396 0 : MAKE_CURSOR( POINTER_COPYFILES, copyfiles_ );
397 0 : MAKE_CURSOR( POINTER_NOTALLOWED, nodrop_ );
398 0 : MAKE_CURSOR( POINTER_ROTATE, rotate_ );
399 0 : MAKE_CURSOR( POINTER_HSHEAR, hshear_ );
400 0 : MAKE_CURSOR( POINTER_VSHEAR, vshear_ );
401 0 : MAKE_CURSOR( POINTER_DRAW_LINE, drawline_ );
402 0 : MAKE_CURSOR( POINTER_DRAW_RECT, drawrect_ );
403 0 : MAKE_CURSOR( POINTER_DRAW_POLYGON, drawpolygon_ );
404 0 : MAKE_CURSOR( POINTER_DRAW_BEZIER, drawbezier_ );
405 0 : MAKE_CURSOR( POINTER_DRAW_ARC, drawarc_ );
406 0 : MAKE_CURSOR( POINTER_DRAW_PIE, drawpie_ );
407 0 : MAKE_CURSOR( POINTER_DRAW_CIRCLECUT, drawcirclecut_ );
408 0 : MAKE_CURSOR( POINTER_DRAW_ELLIPSE, drawellipse_ );
409 0 : MAKE_CURSOR( POINTER_DRAW_CONNECT, drawconnect_ );
410 0 : MAKE_CURSOR( POINTER_DRAW_TEXT, drawtext_ );
411 0 : MAKE_CURSOR( POINTER_MIRROR, mirror_ );
412 0 : MAKE_CURSOR( POINTER_CROOK, crook_ );
413 0 : MAKE_CURSOR( POINTER_CROP, crop_ );
414 0 : MAKE_CURSOR( POINTER_MOVEPOINT, movepoint_ );
415 0 : MAKE_CURSOR( POINTER_MOVEBEZIERWEIGHT, movebezierweight_ );
416 0 : MAKE_CURSOR( POINTER_DRAW_FREEHAND, drawfreehand_ );
417 0 : MAKE_CURSOR( POINTER_DRAW_CAPTION, drawcaption_ );
418 0 : MAKE_CURSOR( POINTER_LINKDATA, linkdata_ );
419 0 : MAKE_CURSOR( POINTER_MOVEDATALINK, movedlnk_ );
420 0 : MAKE_CURSOR( POINTER_COPYDATALINK, copydlnk_ );
421 0 : MAKE_CURSOR( POINTER_LINKFILE, linkfile_ );
422 0 : MAKE_CURSOR( POINTER_MOVEFILELINK, moveflnk_ );
423 0 : MAKE_CURSOR( POINTER_COPYFILELINK, copyflnk_ );
424 0 : MAKE_CURSOR( POINTER_CHART, chart_ );
425 0 : MAKE_CURSOR( POINTER_DETECTIVE, detective_ );
426 0 : MAKE_CURSOR( POINTER_PIVOT_COL, pivotcol_ );
427 0 : MAKE_CURSOR( POINTER_PIVOT_ROW, pivotrow_ );
428 0 : MAKE_CURSOR( POINTER_PIVOT_FIELD, pivotfld_ );
429 0 : MAKE_CURSOR( POINTER_PIVOT_DELETE, pivotdel_ );
430 0 : MAKE_CURSOR( POINTER_CHAIN, chain_ );
431 0 : MAKE_CURSOR( POINTER_CHAIN_NOTALLOWED, chainnot_ );
432 0 : MAKE_CURSOR( POINTER_TIMEEVENT_MOVE, timemove_ );
433 0 : MAKE_CURSOR( POINTER_TIMEEVENT_SIZE, timesize_ );
434 0 : MAKE_CURSOR( POINTER_AUTOSCROLL_N, asn_ );
435 0 : MAKE_CURSOR( POINTER_AUTOSCROLL_S, ass_ );
436 0 : MAKE_CURSOR( POINTER_AUTOSCROLL_W, asw_ );
437 0 : MAKE_CURSOR( POINTER_AUTOSCROLL_E, ase_ );
438 0 : MAKE_CURSOR( POINTER_AUTOSCROLL_NW, asnw_ );
439 0 : MAKE_CURSOR( POINTER_AUTOSCROLL_NE, asne_ );
440 0 : MAKE_CURSOR( POINTER_AUTOSCROLL_SW, assw_ );
441 0 : MAKE_CURSOR( POINTER_AUTOSCROLL_SE, asse_ );
442 0 : MAKE_CURSOR( POINTER_AUTOSCROLL_NS, asns_ );
443 0 : MAKE_CURSOR( POINTER_AUTOSCROLL_WE, aswe_ );
444 0 : MAKE_CURSOR( POINTER_AUTOSCROLL_NSWE, asnswe_ );
445 0 : MAKE_CURSOR( POINTER_AIRBRUSH, airbrush_ );
446 0 : MAKE_CURSOR( POINTER_TEXT_VERTICAL, vertcurs_ );
447 :
448 : // #i32329#
449 0 : MAKE_CURSOR( POINTER_TAB_SELECT_S, tblsels_ );
450 0 : MAKE_CURSOR( POINTER_TAB_SELECT_E, tblsele_ );
451 0 : MAKE_CURSOR( POINTER_TAB_SELECT_SE, tblselse_ );
452 0 : MAKE_CURSOR( POINTER_TAB_SELECT_W, tblselw_ );
453 0 : MAKE_CURSOR( POINTER_TAB_SELECT_SW, tblselsw_ );
454 :
455 : // #i20119#
456 0 : MAKE_CURSOR( POINTER_PAINTBRUSH, paintbrush_ );
457 :
458 : default:
459 : SAL_WARN( "vcl.gtk", "pointer " << ePointerStyle << "not implemented" );
460 0 : break;
461 : }
462 0 : if( !pCursor )
463 0 : pCursor = gdk_cursor_new_for_display( m_pGdkDisplay, GDK_LEFT_PTR );
464 :
465 0 : m_aCursors[ ePointerStyle ] = pCursor;
466 : }
467 :
468 0 : return m_aCursors[ ePointerStyle ];
469 : }
470 :
471 0 : int GtkSalDisplay::CaptureMouse( SalFrame* pSFrame )
472 : {
473 0 : GtkSalFrame* pFrame = static_cast<GtkSalFrame*>(pSFrame);
474 :
475 0 : if( !pFrame )
476 : {
477 0 : if( m_pCapture )
478 0 : static_cast<GtkSalFrame*>(m_pCapture)->grabPointer( FALSE );
479 0 : m_pCapture = NULL;
480 0 : return 0;
481 : }
482 :
483 0 : if( m_pCapture )
484 : {
485 0 : if( pFrame == m_pCapture )
486 0 : return 1;
487 0 : static_cast<GtkSalFrame*>(m_pCapture)->grabPointer( FALSE );
488 : }
489 :
490 0 : m_pCapture = pFrame;
491 0 : static_cast<GtkSalFrame*>(pFrame)->grabPointer( TRUE );
492 0 : return 1;
493 : }
494 :
495 :
496 : /**********************************************************************
497 : * class GtkData *
498 : **********************************************************************/
499 :
500 0 : GtkData::GtkData( SalInstance *pInstance )
501 : #if GTK_CHECK_VERSION(3,0,0)
502 : : SalGenericData( SAL_DATA_GTK3, pInstance )
503 : #else
504 0 : : SalGenericData( SAL_DATA_GTK, pInstance )
505 : #endif
506 : {
507 0 : m_pUserEvent = NULL;
508 0 : m_aDispatchMutex = osl_createMutex();
509 0 : m_aDispatchCondition = osl_createCondition();
510 0 : }
511 :
512 : XIOErrorHandler aOrigXIOErrorHandler = NULL;
513 :
514 0 : int XIOErrorHdl(Display *pDisplay)
515 : {
516 0 : if (::osl::Thread::getCurrentIdentifier() != Application::GetMainThreadIdentifier())
517 : {
518 0 : pthread_exit(NULL);
519 : return 0;
520 : }
521 0 : return aOrigXIOErrorHandler ? aOrigXIOErrorHandler(pDisplay) : 0;
522 : }
523 :
524 0 : GtkData::~GtkData()
525 : {
526 0 : Yield( true, true );
527 0 : g_warning ("TESTME: We used to have a stop-timer here, but the central code should do this");
528 :
529 : // sanity check: at this point nobody should be yielding, but wake them
530 : // up anyway before the condition they're waiting on gets destroyed.
531 0 : osl_setCondition( m_aDispatchCondition );
532 :
533 0 : osl_acquireMutex( m_aDispatchMutex );
534 0 : if (m_pUserEvent)
535 : {
536 0 : g_source_destroy (m_pUserEvent);
537 0 : g_source_unref (m_pUserEvent);
538 0 : m_pUserEvent = NULL;
539 : }
540 0 : osl_destroyCondition( m_aDispatchCondition );
541 0 : osl_releaseMutex( m_aDispatchMutex );
542 0 : osl_destroyMutex( m_aDispatchMutex );
543 0 : XSetIOErrorHandler(aOrigXIOErrorHandler);
544 0 : }
545 :
546 0 : void GtkData::Dispose()
547 : {
548 0 : deInitNWF();
549 0 : }
550 :
551 0 : void GtkData::Yield( bool bWait, bool bHandleAllCurrentEvents )
552 : {
553 : /* #i33212# only enter g_main_context_iteration in one thread at any one
554 : * time, else one of them potentially will never end as long as there is
555 : * another thread in in there. Having only one yieldin thread actually dispatch
556 : * fits the vcl event model (see e.g. the generic plugin).
557 : */
558 0 : bool bDispatchThread = false;
559 0 : bool bWasEvent = false;
560 : {
561 : // release YieldMutex (and re-acquire at block end)
562 0 : SalYieldMutexReleaser aReleaser;
563 0 : if( osl_tryToAcquireMutex( m_aDispatchMutex ) )
564 0 : bDispatchThread = true;
565 0 : else if( ! bWait )
566 0 : return; // someone else is waiting already, return
567 :
568 0 : if( bDispatchThread )
569 : {
570 0 : int nMaxEvents = bHandleAllCurrentEvents ? 100 : 1;
571 0 : gboolean wasOneEvent = TRUE;
572 0 : while( nMaxEvents-- && wasOneEvent )
573 : {
574 0 : wasOneEvent = g_main_context_iteration( NULL, FALSE );
575 0 : if( wasOneEvent )
576 0 : bWasEvent = true;
577 : }
578 0 : if( bWait && ! bWasEvent )
579 0 : bWasEvent = g_main_context_iteration( NULL, TRUE ) != 0;
580 : }
581 0 : else if( bWait )
582 : {
583 : /* #i41693# in case the dispatch thread hangs in join
584 : * for this thread the condition will never be set
585 : * workaround: timeout of 1 second a emergency exit
586 : */
587 : // we are the dispatch thread
588 0 : osl_resetCondition( m_aDispatchCondition );
589 0 : TimeValue aValue = { 1, 0 };
590 0 : osl_waitCondition( m_aDispatchCondition, &aValue );
591 0 : }
592 : }
593 :
594 0 : if( bDispatchThread )
595 : {
596 0 : osl_releaseMutex( m_aDispatchMutex );
597 0 : if( bWasEvent )
598 0 : osl_setCondition( m_aDispatchCondition ); // trigger non dispatch thread yields
599 : }
600 : }
601 :
602 0 : void GtkData::Init()
603 : {
604 : int i;
605 : SAL_INFO( "vcl.gtk", "GtkMainloop::Init()" );
606 0 : XrmInitialize();
607 :
608 : #if !GTK_CHECK_VERSION(3,0,0)
609 0 : gtk_set_locale();
610 : #endif
611 :
612 : /*
613 : * open connection to X11 Display
614 : * try in this order:
615 : * o -display command line parameter,
616 : * o $DISPLAY environment variable
617 : * o default display
618 : */
619 :
620 0 : GdkDisplay *pGdkDisp = NULL;
621 :
622 : // is there a -display command line parameter?
623 0 : rtl_TextEncoding aEnc = osl_getThreadTextEncoding();
624 0 : int nParams = osl_getCommandArgCount();
625 0 : OString aDisplay;
626 0 : OUString aParam, aBin;
627 0 : char** pCmdLineAry = new char*[ nParams+1 ];
628 0 : osl_getExecutableFile( &aParam.pData );
629 0 : osl_getSystemPathFromFileURL( aParam.pData, &aBin.pData );
630 0 : pCmdLineAry[0] = g_strdup( OUStringToOString( aBin, aEnc ).getStr() );
631 0 : for (i=0; i<nParams; i++)
632 : {
633 0 : osl_getCommandArg(i, &aParam.pData );
634 0 : OString aBParam( OUStringToOString( aParam, aEnc ) );
635 :
636 0 : if( aParam == "-display" || aParam == "--display" )
637 : {
638 0 : pCmdLineAry[i+1] = g_strdup( "--display" );
639 0 : osl_getCommandArg(i+1, &aParam.pData );
640 0 : aDisplay = OUStringToOString( aParam, aEnc );
641 : }
642 : else
643 0 : pCmdLineAry[i+1] = g_strdup( aBParam.getStr() );
644 0 : }
645 : // add executable
646 0 : nParams++;
647 :
648 0 : g_set_application_name(SalGenericSystem::getFrameClassName());
649 :
650 : // Set consistant name of the root accessible
651 0 : OUString aAppName = Application::GetAppName();
652 0 : if( !aAppName.isEmpty() )
653 : {
654 0 : OString aPrgName = OUStringToOString(aAppName, aEnc);
655 0 : g_set_prgname(aPrgName.getStr());
656 : }
657 :
658 : // init gtk/gdk
659 0 : gtk_init_check( &nParams, &pCmdLineAry );
660 0 : gdk_error_trap_push();
661 0 : aOrigXIOErrorHandler = XSetIOErrorHandler(XIOErrorHdl);
662 :
663 0 : for (i = 0; i < nParams; i++ )
664 0 : g_free( pCmdLineAry[i] );
665 0 : delete [] pCmdLineAry;
666 :
667 : #if OSL_DEBUG_LEVEL > 1
668 : if (g_getenv ("SAL_DEBUG_UPDATES"))
669 : gdk_window_set_debug_updates (TRUE);
670 : #endif
671 :
672 0 : pGdkDisp = gdk_display_get_default();
673 0 : if ( !pGdkDisp )
674 : {
675 0 : OUString aProgramFileURL;
676 0 : osl_getExecutableFile( &aProgramFileURL.pData );
677 0 : OUString aProgramSystemPath;
678 0 : osl_getSystemPathFromFileURL (aProgramFileURL.pData, &aProgramSystemPath.pData);
679 : OString aProgramName = OUStringToOString(
680 : aProgramSystemPath,
681 0 : osl_getThreadTextEncoding() );
682 : fprintf( stderr, "%s X11 error: Can't open display: %s\n",
683 0 : aProgramName.getStr(), aDisplay.getStr());
684 0 : fprintf( stderr, " Set DISPLAY environment variable, use -display option\n");
685 0 : fprintf( stderr, " or check permissions of your X-Server\n");
686 0 : fprintf( stderr, " (See \"man X\" resp. \"man xhost\" for details)\n");
687 0 : fflush( stderr );
688 0 : exit(0);
689 : }
690 :
691 : /*
692 : * if a -display switch was used, we need
693 : * to set the environment accoringly since
694 : * the clipboard build another connection
695 : * to the xserver using $DISPLAY
696 : */
697 0 : OUString envVar("DISPLAY");
698 0 : const gchar *name = gdk_display_get_name( pGdkDisp );
699 0 : OUString envValue(name, strlen(name), aEnc);
700 0 : osl_setEnvironment(envVar.pData, envValue.pData);
701 :
702 0 : GtkSalDisplay *pDisplay = new GtkSalDisplay( pGdkDisp );
703 0 : SetDisplay( pDisplay );
704 :
705 : #if !GTK_CHECK_VERSION(3,0,0)
706 0 : Display *pDisp = gdk_x11_display_get_xdisplay( pGdkDisp );
707 :
708 0 : gdk_error_trap_push();
709 0 : SalI18N_KeyboardExtension *pKbdExtension = new SalI18N_KeyboardExtension( pDisp );
710 0 : bool bErrorOccured = gdk_error_trap_pop() != 0;
711 0 : gdk_error_trap_push();
712 0 : pKbdExtension->UseExtension( bErrorOccured );
713 0 : gdk_error_trap_pop();
714 0 : GetGtkDisplay()->SetKbdExtension( pKbdExtension );
715 : #else
716 : # warning unwind keyboard extension bits
717 : #endif
718 :
719 : // add signal handler to notify screen size changes
720 0 : int nScreens = gdk_display_get_n_screens( pGdkDisp );
721 0 : for( int n = 0; n < nScreens; n++ )
722 : {
723 0 : GdkScreen *pScreen = gdk_display_get_screen( pGdkDisp, n );
724 0 : if( pScreen )
725 : {
726 0 : pDisplay->screenSizeChanged( pScreen );
727 0 : pDisplay->monitorsChanged( pScreen );
728 0 : g_signal_connect( G_OBJECT(pScreen), "size-changed",
729 0 : G_CALLBACK(signalScreenSizeChanged), pDisplay );
730 0 : if( ! gtk_check_version( 2, 14, 0 ) ) // monitors-changed came in with 2.14, avoid an assertion
731 0 : g_signal_connect( G_OBJECT(pScreen), "monitors-changed",
732 0 : G_CALLBACK(signalMonitorsChanged), GetGtkDisplay() );
733 : }
734 0 : }
735 0 : }
736 :
737 0 : void GtkData::ErrorTrapPush()
738 : {
739 0 : gdk_error_trap_push ();
740 0 : }
741 :
742 0 : bool GtkData::ErrorTrapPop( bool bIgnoreError )
743 : {
744 : #if GTK_CHECK_VERSION(3,0,0)
745 : if( bIgnoreError )
746 : {
747 : gdk_error_trap_pop_ignored (); // faster
748 : return false;
749 : }
750 : #else
751 : (void) bIgnoreError;
752 : #endif
753 0 : return gdk_error_trap_pop () != 0;
754 : }
755 :
756 : extern "C" {
757 :
758 : struct SalGtkTimeoutSource {
759 : GSource aParent;
760 : GTimeVal aFireTime;
761 : GtkSalTimer *pInstance;
762 : };
763 :
764 0 : static void sal_gtk_timeout_defer( SalGtkTimeoutSource *pTSource )
765 : {
766 0 : g_get_current_time( &pTSource->aFireTime );
767 0 : g_time_val_add( &pTSource->aFireTime, pTSource->pInstance->m_nTimeoutMS * 1000 );
768 0 : }
769 :
770 0 : static gboolean sal_gtk_timeout_expired( SalGtkTimeoutSource *pTSource,
771 : gint *nTimeoutMS, GTimeVal *pTimeNow )
772 : {
773 0 : glong nDeltaSec = pTSource->aFireTime.tv_sec - pTimeNow->tv_sec;
774 0 : glong nDeltaUSec = pTSource->aFireTime.tv_usec - pTimeNow->tv_usec;
775 0 : if( nDeltaSec < 0 || ( nDeltaSec == 0 && nDeltaUSec < 0) )
776 : {
777 0 : *nTimeoutMS = 0;
778 0 : return TRUE;
779 : }
780 0 : if( nDeltaUSec < 0 )
781 : {
782 0 : nDeltaUSec += 1000000;
783 0 : nDeltaSec -= 1;
784 : }
785 : // if the clock changes backwards we need to cope ...
786 0 : if( (unsigned long) nDeltaSec > 1 + ( pTSource->pInstance->m_nTimeoutMS / 1000 ) )
787 : {
788 0 : sal_gtk_timeout_defer( pTSource );
789 0 : return TRUE;
790 : }
791 :
792 0 : *nTimeoutMS = MIN( G_MAXINT, ( nDeltaSec * 1000 + (nDeltaUSec + 999) / 1000 ) );
793 :
794 0 : return *nTimeoutMS == 0;
795 : }
796 :
797 0 : static gboolean sal_gtk_timeout_prepare( GSource *pSource, gint *nTimeoutMS )
798 : {
799 0 : SalGtkTimeoutSource *pTSource = (SalGtkTimeoutSource *)pSource;
800 :
801 : GTimeVal aTimeNow;
802 0 : g_get_current_time( &aTimeNow );
803 :
804 0 : return sal_gtk_timeout_expired( pTSource, nTimeoutMS, &aTimeNow );
805 : }
806 :
807 0 : static gboolean sal_gtk_timeout_check( GSource *pSource )
808 : {
809 0 : SalGtkTimeoutSource *pTSource = (SalGtkTimeoutSource *)pSource;
810 :
811 : GTimeVal aTimeNow;
812 0 : g_get_current_time( &aTimeNow );
813 :
814 0 : return ( pTSource->aFireTime.tv_sec < aTimeNow.tv_sec ||
815 0 : ( pTSource->aFireTime.tv_sec == aTimeNow.tv_sec &&
816 0 : pTSource->aFireTime.tv_usec < aTimeNow.tv_usec ) );
817 : }
818 :
819 0 : static gboolean sal_gtk_timeout_dispatch( GSource *pSource, GSourceFunc, gpointer )
820 : {
821 0 : SalGtkTimeoutSource *pTSource = (SalGtkTimeoutSource *)pSource;
822 :
823 0 : if( !pTSource->pInstance )
824 0 : return FALSE;
825 :
826 0 : SalData *pSalData = GetSalData();
827 :
828 0 : osl::Guard< comphelper::SolarMutex > aGuard( pSalData->m_pInstance->GetYieldMutex() );
829 :
830 0 : sal_gtk_timeout_defer( pTSource );
831 :
832 0 : ImplSVData* pSVData = ImplGetSVData();
833 0 : if( pSVData->mpSalTimer )
834 0 : pSVData->mpSalTimer->CallCallback();
835 :
836 0 : return TRUE;
837 : }
838 :
839 : static GSourceFuncs sal_gtk_timeout_funcs =
840 : {
841 : sal_gtk_timeout_prepare,
842 : sal_gtk_timeout_check,
843 : sal_gtk_timeout_dispatch,
844 : NULL, NULL, NULL
845 : };
846 : }
847 :
848 : static SalGtkTimeoutSource *
849 0 : create_sal_gtk_timeout( GtkSalTimer *pTimer )
850 : {
851 0 : GSource *pSource = g_source_new( &sal_gtk_timeout_funcs, sizeof( SalGtkTimeoutSource ) );
852 0 : SalGtkTimeoutSource *pTSource = (SalGtkTimeoutSource *)pSource;
853 0 : pTSource->pInstance = pTimer;
854 :
855 : // #i36226# timers should be executed with lower priority
856 : // than XEvents like in generic plugin
857 0 : g_source_set_priority( pSource, G_PRIORITY_LOW );
858 0 : g_source_set_can_recurse( pSource, TRUE );
859 : g_source_set_callback( pSource,
860 : /* unused dummy */ g_idle_remove_by_data,
861 0 : NULL, NULL );
862 0 : g_source_attach( pSource, g_main_context_default() );
863 :
864 0 : sal_gtk_timeout_defer( pTSource );
865 :
866 0 : return pTSource;
867 : }
868 :
869 0 : GtkSalTimer::GtkSalTimer()
870 0 : : m_pTimeout( NULL )
871 : {
872 0 : }
873 :
874 0 : GtkSalTimer::~GtkSalTimer()
875 : {
876 0 : GtkInstance *pInstance = static_cast<GtkInstance *>(GetSalData()->m_pInstance);
877 0 : pInstance->RemoveTimer( this );
878 0 : Stop();
879 0 : }
880 :
881 0 : bool GtkSalTimer::Expired()
882 : {
883 0 : if( !m_pTimeout )
884 0 : return false;
885 :
886 0 : gint nDummy = 0;
887 : GTimeVal aTimeNow;
888 0 : g_get_current_time( &aTimeNow );
889 0 : return !!sal_gtk_timeout_expired( m_pTimeout, &nDummy, &aTimeNow);
890 : }
891 :
892 0 : void GtkSalTimer::Start( sal_uLong nMS )
893 : {
894 0 : m_nTimeoutMS = nMS; // for restarting
895 0 : Stop(); // FIXME: ideally re-use an existing m_pTimeout
896 0 : m_pTimeout = create_sal_gtk_timeout( this );
897 0 : }
898 :
899 0 : void GtkSalTimer::Stop()
900 : {
901 0 : if( m_pTimeout )
902 : {
903 0 : g_source_destroy( (GSource *)m_pTimeout );
904 0 : g_source_unref( (GSource *)m_pTimeout );
905 0 : m_pTimeout = NULL;
906 : }
907 0 : }
908 :
909 0 : gboolean GtkData::userEventFn( gpointer data )
910 : {
911 0 : gboolean bContinue = FALSE;
912 0 : GtkData *pThis = (GtkData *) data;
913 0 : SalGenericData *pData = GetGenericData();
914 0 : osl::Guard< comphelper::SolarMutex > aGuard( pData->m_pInstance->GetYieldMutex() );
915 0 : const SalGenericDisplay *pDisplay = pData->GetDisplay();
916 0 : if (pDisplay)
917 : {
918 : OSL_ASSERT(static_cast<const SalGenericDisplay *>(pThis->GetGtkDisplay()) == pDisplay);
919 0 : pThis->GetGtkDisplay()->EventGuardAcquire();
920 :
921 0 : if( !pThis->GetGtkDisplay()->HasUserEvents() )
922 : {
923 0 : if( pThis->m_pUserEvent )
924 : {
925 0 : g_source_unref (pThis->m_pUserEvent);
926 0 : pThis->m_pUserEvent = NULL;
927 : }
928 0 : bContinue = FALSE;
929 : }
930 : else
931 0 : bContinue = TRUE;
932 :
933 0 : pThis->GetGtkDisplay()->EventGuardRelease();
934 :
935 0 : pThis->GetGtkDisplay()->DispatchInternalEvent();
936 : }
937 :
938 0 : return bContinue;
939 : }
940 :
941 : extern "C" {
942 0 : static gboolean call_userEventFn( void *data )
943 : {
944 0 : SolarMutexGuard aGuard;
945 0 : return GtkData::userEventFn( data );
946 : }
947 : }
948 :
949 : // hEventGuard_ held during this invocation
950 0 : void GtkData::PostUserEvent()
951 : {
952 0 : if (m_pUserEvent)
953 0 : g_main_context_wakeup (NULL); // really needed ?
954 : else // nothing pending anyway
955 : {
956 0 : m_pUserEvent = g_idle_source_new();
957 0 : g_source_set_priority (m_pUserEvent, G_PRIORITY_HIGH);
958 0 : g_source_set_can_recurse (m_pUserEvent, TRUE);
959 : g_source_set_callback (m_pUserEvent, call_userEventFn,
960 0 : (gpointer) this, NULL);
961 0 : g_source_attach (m_pUserEvent, g_main_context_default ());
962 : }
963 0 : }
964 :
965 0 : void GtkSalDisplay::PostUserEvent()
966 : {
967 0 : GetGtkSalData()->PostUserEvent();
968 0 : }
969 :
970 0 : void GtkSalDisplay::deregisterFrame( SalFrame* pFrame )
971 : {
972 0 : if( m_pCapture == pFrame )
973 : {
974 0 : static_cast<GtkSalFrame*>(m_pCapture)->grabPointer( FALSE );
975 0 : m_pCapture = NULL;
976 : }
977 0 : SalGenericDisplay::deregisterFrame( pFrame );
978 0 : }
979 :
980 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|