summaryrefslogtreecommitdiffstats
path: root/kxkb/layoutmap.cpp
blob: 7b5136e2ad8b79ab0a2803eca03023d1ba28002d (plain)
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
//
// C++ Implementation: layoutmap
//
// Description: 
//
//
// Author: Andriy Rysin <rysin@kde.org>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include "layoutmap.h"

#include "x11helper.h"


LayoutMap::LayoutMap(const KxkbConfig& kxkbConfig_):
	m_kxkbConfig(kxkbConfig_),
	m_currentWinId( X11Helper::UNKNOWN_WINDOW_ID )
{
}

// private
void LayoutMap::clearMaps()
{
	m_appLayouts.clear();
	m_winLayouts.clear();
	m_globalLayouts.clear();
	//setCurrentWindow( -1 );
}

void LayoutMap::reset()
{
	clearMaps();
	setCurrentWindow( X11Helper::UNKNOWN_WINDOW_ID );
}



void LayoutMap::setCurrentWindow(WId winId)
{
	m_currentWinId = winId;
	if( m_kxkbConfig.m_switchingPolicy == SWITCH_POLICY_WIN_CLASS )
		m_currentWinClass = X11Helper::getWindowClass(winId, tqt_xdisplay());
}

// private
//LayoutQueue& 
TQPtrQueue<LayoutState>& LayoutMap::getCurrentLayoutQueueInternal(WId winId)
{
	if( winId == X11Helper::UNKNOWN_WINDOW_ID )
		return m_globalLayouts;
	
	switch( m_kxkbConfig.m_switchingPolicy ) {
		case SWITCH_POLICY_WIN_CLASS: {
//			TQString winClass = X11Helper::getWindowClass(winId, tqt_xdisplay());
			return m_appLayouts[ m_currentWinClass ];
		}
		case SWITCH_POLICY_WINDOW:
			return m_winLayouts[ winId ];

		default:
			return m_globalLayouts;
	}
}

// private
//LayoutQueue& 
TQPtrQueue<LayoutState>& LayoutMap::getCurrentLayoutQueue(WId winId)
{
	TQPtrQueue<LayoutState>& layoutQueue = getCurrentLayoutQueueInternal(winId);
	if( layoutQueue.count() == 0 ) {
		initLayoutQueue(layoutQueue);
		kdDebug() << "map: Created queue for " << winId << " size: " << layoutQueue.count() << endl;
	}
	
	return layoutQueue;
}

LayoutState& LayoutMap::getCurrentLayout() {
	return *getCurrentLayoutQueue(m_currentWinId).head();
}

LayoutState& LayoutMap::getNextLayout() {
	LayoutQueue& layoutQueue = getCurrentLayoutQueue(m_currentWinId);
	LayoutState* layoutState = layoutQueue.dequeue();
	layoutQueue.enqueue(layoutState);
	
	kdDebug() << "map: Next layout: " << layoutQueue.head()->layoutUnit.toPair() 
			  << " for " << m_currentWinId << endl;
	
	return *layoutQueue.head();
}

void LayoutMap::setCurrentLayout(const LayoutUnit& layoutUnit) {
	LayoutQueue& layoutQueue = getCurrentLayoutQueue(m_currentWinId);
	kdDebug() << "map: Storing layout: " << layoutUnit.toPair() 
			  << " for " << m_currentWinId << endl;
	
	int queueSize = (int)layoutQueue.count();
	for(int ii=0; ii<queueSize; ii++) {
		if( layoutQueue.head()->layoutUnit == layoutUnit )
			return;	// if present return when it's in head
		
		LayoutState* layoutState = layoutQueue.dequeue();
		if( ii < queueSize - 1 ) {
			layoutQueue.enqueue(layoutState);
		}
		else {
			delete layoutState;
			layoutQueue.enqueue(new LayoutState(layoutUnit));
		}
	}
	for(int ii=0; ii<queueSize - 1; ii++) {
		LayoutState* layoutState = layoutQueue.dequeue();
		layoutQueue.enqueue(layoutState);
	}
}

// private
void LayoutMap::initLayoutQueue(LayoutQueue& layoutQueue) {
	int queueSize = ( m_kxkbConfig.m_stickySwitching ) 
			? m_kxkbConfig.m_stickySwitchingDepth : m_kxkbConfig.m_layouts.count();
	for(int ii=0; ii<queueSize; ii++) {
		layoutQueue.enqueue( new LayoutState(m_kxkbConfig.m_layouts[ii]) );
	}
}