Stories

Detail Return Return

【Linux】《how linux work》第十四章 Linux 桌面簡介 - Stories Detail

Chapter 14. A Brief Survey of the Linux Desktop(Linux 桌面簡介)

This chapter is a quick introduction to the components found in a typical Linux desktop system. Of all of the different kinds of software that you can find on Linux systems, the desktop arena is one of the wildest and most colorful because there are so many environments and applications to choose from, and most distributions make it relatively easy for you to try them out.

本章是對典型Linux桌面系統中的組件進行快速介紹。

在Linux系統中,桌面領域是最為豐富多樣的,因為有很多不同的環境和應用程序可供選擇,並且大多數發行版都相對容易讓您嘗試它們。

Unlike other parts of a Linux system, such as storage and networking, there isn’t much of a hierarchy of layers involved in creating a desktop structure. Instead, each component performs a specific task, communicating with other components as necessary. Some components do share common building blocks (in particular, libraries for graphical toolkits), and these can be thought of as simple abstraction layers, but that’s about as deep as it goes.

與Linux系統的其他部分(如存儲和網絡)不同,創建桌面結構並沒有涉及太多層次的層次結構。

相反,每個組件都執行特定的任務,並在必要時與其他組件進行通信。

一些組件確實共享共同的構建模塊(特別是用於圖形工具包的庫),可以將其視為簡單的抽象層,但僅限於此。

This chapter offers a high-level discussion of desktop components in general, but we’ll look at two pieces in a little more detail: the X Window System, which is the core infrastructure behind most desktops, and D-Bus, an interprocess communication service used in many parts of the system. We’ll limit the hands-on discussion and examples to a few diagnostic utilities that, while not terribly useful day-to-day (most GUIs don’t require you to enter shell commands in order to interact with them), will help you understand the underlying mechanics of the system and perhaps provide some entertainment along the way. We’ll also take a quick look at printing.

本章對桌面組件進行了高層次的討論,但我們將更詳細地研究兩個組件:X Window System(大多數桌面背後的核心基礎設施)和D-Bus(系統中許多部分使用的進程間通信服務)。

我們將限制實踐討論和示例,僅介紹一些診斷實用工具,雖然這些工具在日常使用中並不是非常有用(大多數圖形界面不需要您輸入shell命令來與它們交互),但它們將幫助您瞭解系統的基本原理,並提供一些娛樂。

我們還會簡要介紹打印功能。

14.1 Desktop Components(桌面組件)

Linux desktop configurations offer a great deal of flexibility. Most of what the Linux user experiences (the “look and feel” of the desktop) comes from applications or building blocks of applications. If you don’t like a particular application, you can usually find an alternative. And if what you’re looking for doesn’t exist, you can write it yourself. Linux developers tend to have a wide variety of preferences for how a desktop should act, which makes for a lot of choices.

Linux桌面配置提供了很大的靈活性。

Linux用户所體驗到的大部分內容(即桌面的“外觀和感覺”)來自應用程序或應用程序的構建模塊。

如果您不喜歡某個特定的應用程序,通常可以找到替代品。

而且,如果您所需要的東西不存在,您可以自己編寫。

Linux開發人員對於桌面應該如何運作有各種各樣的偏好,這就帶來了很多選擇。

In order to work together, all applications need to have something in common, and at the core of nearly everything on most Linux desktops is the X (X Window System) server. Think of X as sort of the “kernel” of the desktop that manages everything from rendering windows to configuring displays to handling input from devices such as keyboards and mice. The X server is also the one component that you won’t easily find a replacement for (see 14.4 The Future of X).

為了能夠協同工作,所有應用程序都需要有一些共同點,而在大多數Linux桌面上的幾乎所有東西的核心是X(X Window System)服務器。

可以將X視為桌面的“內核”,它負責管理從渲染窗口到配置顯示器再到處理鍵盤和鼠標等設備輸入的所有內容。

X服務器也是您不容易找到替代品的一個組件(參見14.4節《X的未來》)。

The X server is just a server and does not dictate the way anything should act or appear. Instead, X client programs handle the user interface. Basic X client applications, such as terminal windows and web browsers, make connections to the X server and ask to draw windows. In response, the X server figures out where to place the windows and renders them. The X server also channels input back to the client when appropriate.

X服務器只是一個服務器,不會規定任何東西的行為或外觀。

相反,X客户端程序處理用户界面。基本的X客户端應用程序,例如終端窗口和Web瀏覽器,會連接到X服務器並請求繪製窗口。

作為響應,X服務器會確定窗口的放置位置並對其進行渲染。

當適當時,X服務器還會將輸入傳遞迴客户端。

14.1.1 Window Managers(Win 管理器)

X clients don’t have to act like windowed user applications; they can act as services for other clients or provide other interface functions. A window manager is perhaps the most important client service application because it figures out how to arrange windows on screen and provides interactive decorations like title bars that allow the user to move and minimize windows. These elements are central to the user experience.

X客户端不必像窗口化的用户應用程序那樣行動;它們可以作為其他客户端的服務或提供其他接口功能。

窗口管理器可能是最重要的客户端服務應用程序,因為它能夠確定如何在屏幕上排列窗口,並提供交互裝飾,如標題欄,允許用户移動和最小化窗口。

這些元素對用户體驗至關重要。

There are many window manager implementations. Examples such as Mutter/GNOME Shell and Compiz are meant to be more or less standalone, while others are built into environments such as Xfce. Most window managers included in the standard Linux distributions strive for maximum user comfort, but others provide specific visual effects or take a minimalist approach. There’s not likely to ever be a standard Linux window manager because user tastes and requirements are diverse and constantly changing; as a result, new window managers appear all the time.

有許多窗口管理器的實現。像Mutter/GNOME Shell和Compiz這樣的例子,意味着它們更或多或少是獨立的,而其他一些則內置於環境中,如Xfce。

大多數包含在標準Linux發行版中的窗口管理器都力求為用户提供最大的舒適性,但其他一些則提供特定的視覺效果或採取極簡主義的方法。

不太可能會有一個標準的Linux窗口管理器,因為用户的口味和需求是多樣化且不斷變化的;因此,新的窗口管理器一直在不斷出現。

14.1.2 Toolkits(工具包)

Desktop applications include certain common elements, such as buttons and menus, called widgets. To speed up development and provide a common look, programmers use graphical toolkits to provide these elements. On operating systems such as Windows or Mac OS X, the vendor provides a common toolkit, and most programmers use that. On Linux, the GTK+ toolkit is one of the most common, but you’ll also frequently see widgets built on the Qt framework and others. Toolkits usually consist of shared libraries and support files such as images and theme information.

桌面應用程序包括一些常見的元素,如按鈕和菜單,稱為小部件。

為了加快開發速度並提供統一的外觀,程序員使用圖形工具包來提供這些元素。

在諸如Windows或Mac OS X等操作系統上,供應商提供了一個常用的工具包,大多數程序員使用它。

在Linux上,GTK+工具包是最常見的之一,但你也經常會看到基於Qt框架和其他工具包構建的小部件。

工具包通常由共享庫和支持文件(如圖像和主題信息)組成。

14.1.3 Desktop Environments(桌面環境)

Although toolkits provide the user with a uniform outward appearance, some details of a desktop require a degree of cooperation between different applications. For example, one application may wish to share data with another or update a common notification bar on a desktop. To provide for these needs, toolkits and other libraries are bundled into larger packages called desktop environments. GNOME, KDE, Unity, and Xfce are some common Linux desktop environments.

儘管工具包為用户提供了統一的外觀,但桌面的一些細節需要不同應用程序之間的合作。

例如,一個應用程序可能希望與另一個應用程序共享數據,或者更新桌面上的公共通知欄。

為了滿足這些需求,工具包和其他庫被捆綁到稱為桌面環境的更大的軟件包中。

GNOME、KDE、Unity和Xfce是一些常見的Linux桌面環境。

Toolkits are at the core of most desktop environments, but to create a unified desktop, environments must also include numerous support files, such as icons and configurations, that make up themes. All of this is bound together with documents that describe design conventions, such as how application menus and titles should appear and how applications should react to certain system events.

工具包是大多數桌面環境的核心,但為了創建一個統一的桌面,環境還必須包括許多支持文件,如圖標和配置文件,這些文件組成了主題。

所有這些都與描述設計約定的文檔綁定在一起,例如應用程序菜單和標題的外觀以及應用程序對某些系統事件的反應方式。

14.1.4 Applications(應用程序)

At the top of the desktop are applications, such as web browsers and the terminal window. X applications can range from crude (such as the ancient xclock program) to complex (such as the Chrome web browser and LibreOffice suite). These applications normally stand alone, but they often use interprocess communication to become aware of pertinent events. For example, an application can express interest when you attach a new storage device or when you receive new email or an instant message. This communication usually occurs over D-Bus, described in 14.5 D-Bus

在桌面的頂部有應用程序,如網絡瀏覽器和終端窗口。

X應用程序的範圍可以從簡單粗糙(如古老的xclock程序)到複雜(如Chrome網絡瀏覽器和LibreOffice套件)。

這些應用程序通常是獨立的,但它們經常使用進程間通信來了解相關事件。

例如,當您連接新的存儲設備或收到新的電子郵件或即時消息時,應用程序可以表達興趣。

這種通信通常是通過D-Bus進行的,詳見14.5 D-Bus。

14.2 A Closer Look at the X Window System(近距離觀察 X 窗口系統)

The X Window System (http://www.x.org/) has historically been very large, with the base distribution including the X server, client support libraries, and clients. Due to the emergence of desktop environments such as GNOME and KDE, the role of the X distribution has changed over time, with the focus now more on the core server that manages rendering and input devices, as well as a simplified client library.

X Window系統(http://www.x.org/)一直以來都非常龐大,基本發行版包括X服務器、客户端支持庫和客户端。

由於出現了GNOME和KDE等桌面環境,X發行版的角色隨着時間的推移發生了變化,現在更加關注管理渲染和輸入設備的核心服務器,以及一個簡化的客户端庫。

The X server is easy to identify on your system. It’s called X. Check for it in a process listing; you’ll usually see it running with a number of options like this:

/usr/bin/X :0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -
novtswitch

The :0 shown here is called the display, an identifier representing one or more monitors that you access with a common keyboard and/or mouse. Usually, the display just corresponds to the single monitor you attach to your computer, but you can put multiple monitors under the same display. When using an X session, the DISPLAY environment variable is set to the display identifier.

這裏顯示的:0被稱為顯示器,它是一個表示一個或多個監視器的標識符,您可以通過共同的鍵盤和/或鼠標訪問它們。

通常,顯示器只對應您連接到計算機上的單個監視器,但您也可以將多個監視器放在同一個顯示器下。

在使用X會話時,DISPLAY環境變量被設置為顯示器標識符。

NOTE Displays can be further subdivided into screens, such as :0.0 and :0.1, but this has become increasingly rare because X extensions, such as RandR, can combine multiple monitors into one larger virtual screen.

注意 顯示器可以進一步細分為屏幕,例如:0.0和:0.1,但這種情況越來越少見,因為X擴展(如RandR)可以將多個監視器組合成一個更大的虛擬屏幕。

On Linux, an X server runs on a virtual terminal. In this example, the vt7 argument tells us that it’s been told to run on /dev/tty7 (normally, the server starts on the first virtual terminal available). You can run more than one X server at a time on Linux by running them on separate virtual terminals, but if you do, each server needs a unique display identifier. You can switch between the servers with the CTRL-ALT-FN keys or the chvt command.

在Linux上,X服務器在虛擬終端上運行。在這個例子中,vt7參數告訴我們它被告知在/dev/tty7上運行(通常,服務器會在可用的第一個虛擬終端上啓動)。

您可以通過在不同的虛擬終端上運行它們來同時運行多個X服務器,但如果這樣做,每個服務器都需要一個唯一的顯示器標識符。

您可以使用CTRL-ALT-FN鍵或chvt命令在服務器之間切換。

在您的系統上很容易識別X服務器。它被稱為X。在進程列表中查找它;通常您會看到它以如下所示的一些選項運行:

/usr/bin/X :0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -
novtswitch

14.2.1 Display Managers(顯示管理人員)

You normally don’t start the X server with a command line because starting the server doesn’t define any clients that are supposed to run on the server. If you start the server by itself, you’ll just get a blank screen. Instead, the most common way to start an X server is with a display manager, a program that starts the server and puts a login box on the screen. When you log in, the display manager starts a set of clients, such as a window manager and file manager, so that you can start to use the machine.

通常情況下,你不會通過命令行來啓動X服務器,因為僅僅啓動服務器並不會定義任何應該在服務器上運行的客户端。

如果僅僅啓動服務器,你將只會看到一個空白屏幕。

相反,最常見的啓動X服務器的方式是使用顯示管理器,它是一個程序,用於啓動服務器並在屏幕上顯示登錄框。

當你登錄時,顯示管理器會啓動一組客户端,例如窗口管理器和文件管理器,以便你開始使用計算機。

There are many different display managers, such as gdm (for GNOME) and kdm (for KDE). The lightdm in the argument list for the X server invocation above is a cross-platform display manager meant to be able to start GNOME or KDE sessions.

To start an X session from a virtual console instead of using a display manager, you can run the startx or xinit command. However, the session you get will likely be a very simple one that looks completely unlike that of a display manager, because the mechanics and startup files are different.

有許多不同的顯示管理器,例如gdm(用於GNOME)和kdm(用於KDE)。

上述X服務器調用的參數列表中的lightdm是一個跨平台的顯示管理器,旨在能夠啓動GNOME或KDE會話。

如果你想從虛擬控制枱而不是使用顯示管理器啓動X會話,可以運行startx或xinit命令。

然而,你得到的會話可能會非常簡單,完全不像顯示管理器的外觀,因為機制和啓動文件是不同的。

14.2.2 Network Transparency(網絡透明性)

One feature of X is network transparency. Because clients talk to the server using a protocol, it’s possible to run clients across a network to a server running on a different machine directly over the network, with the X server listening for TCP connections on port 6000. Clients connecting to that port could authenticate, then send windows to the server.

X 的一個特性是網絡透明性。

因為客户端使用協議與服務器通信,所以可以通過網絡在不同機器上直接運行客户端到運行在另一台機器上的服務器,X 服務器監聽端口 6000 上的 TCP 連接。

連接到該端口的客户端可以進行身份驗證,然後將窗口發送到服務器。

Unfortunately, this method does not normally offer any encryption and is insecure as a result. To close this hole, most distributions now disable the X server’s network listener (with the -nolisten tcp option to the server, as seen in Note). However, you can still run X clients from a remote machine with SSH tunneling, as described in Chapter 10, by connecting the X server’s Unix domain socket to a socket on the remote machine.

不幸的是,這種方法通常不提供任何加密,因此是不安全的。

為了關閉這個漏洞,大多數發行版現在會禁用 X 服務器的網絡監聽器(通過向服務器添加 -nolisten tcp 選項,如註釋中所示)。

然而,您仍然可以通過 SSH 隧道運行來自遠程機器的 X 客户端,方法在第 10 章中有描述,通過將 X 服務器的 Unix 域套接字連接到遠程機器上的套接字。

14.3 Exploring X Clients(探索 X Clients)

Although one doesn’t normally think of working with a graphical user interface from the command line, there are several utilities that allow you to explore the parts of the X Window System. In particular, you can inspect clients as they run.

儘管人們通常不會考慮從命令行與圖形用户界面進行交互,但有幾個實用程序可以讓您探索 X 窗口系統的各個部分。特別是,您可以在運行時檢查客户端。

One of the simplest tools is xwininfo. When run without any arguments, it asks you to click on a window:

其中一個最簡單的工具是 xwininfo。當沒有任何參數運行時,它會要求您點擊一個窗口:

$ xwininfo
xwininfo: Please select the window about which you
 would like information by clicking the
 mouse in that window.
After you click, it prints a list of information about the window, such as its location and size:
xwininfo: Window id: 0x5400024 "xterm"
 Absolute upper-left X: 1075
 Absolute upper-left Y: 594
--snip--

Notice the window ID here—the X server and window managers use this identifier to keep track of windows. To get a list of all window IDs and clients, use the xlsclients -l command.

請注意這裏的窗口 ID —— X 服務器和窗口管理器使用此標識符來跟蹤窗口。要獲取所有窗口 ID 和客户端的列表,請使用命令 xlsclients -l。

NOTE There is a special window called the root window; it’s the background on the display. However, you may never see this window (see Desktop Background).

注意:有一個特殊的窗口稱為根窗口;它是顯示器上的背景。但是,您可能永遠看不到這個窗口(請參見桌面背景)。

14.3.1 X Events(X 事件)

X clients get their input and other information about the state of the server through a system of events. X events work like other asynchronous interprocess communication events such as udev events and D-Bus events: The X server receives information from a source such as an input device, then redistributes that input as an event to any interested X client.

X 客户端通過一種事件系統獲取關於服務器狀態的輸入和其他信息。

X 事件的工作方式類似於其他異步進程間通信事件,比如 udev 事件和 D-Bus 事件:X 服務器從諸如輸入設備之類的源接收信息,然後將該輸入作為事件重新分發給任何感興趣的 X 客户端。

You can experiment with events with the xev command. Running it opens a new window that you can mouse into, click, and type. As you do so, xev generates output describing the X events that it receives from the server. For example, here’s sample output for mouse movement:

您可以使用 xev 命令來嘗試事件。運行該命令會打開一個新窗口,您可以將鼠標移動到其中,點擊和輸入。

在此過程中,xev 會生成描述其從服務器接收的 X 事件的輸出。例如,這是鼠標移動的示例輸出:

$ xev
--snip--
MotionNotify event, serial 36, synthetic NO, window 0x6800001,
 root 0xbb, subw 0x0, time 43937883, (47,174), root:(1692,486),
 state 0x0, is_hint 0, same_screen YES
MotionNotify event, serial 36, synthetic NO, window 0x6800001,
 root 0xbb, subw 0x0, time 43937891, (43,177), root:(1688,489),
 state 0x0, is_hint 0, same_screen YES

Notice the coordinates in parentheses. The first pair represents the x-and y-coordinates of the mouse pointer inside the window, and the second (root:) is the location of the pointer on the entire display.

注意括號中的座標。第一對錶示窗口內鼠標指針的 x 和 y 座標,第二對 (根:) 是指針在整個顯示器上的位置。

Other low-level events include key presses and button clicks, but a few more advanced ones indicate whether the mouse has entered or exited the window, or if the window has gained or lost focus from the window manager. For example, here are corresponding exit and unfocus events:

其他低級事件包括按鍵和按鈕點擊,但還有一些更高級的事件指示鼠標是否進入或離開窗口,或者窗口是否從窗口管理器獲得或失去焦點。

例如,這裏是相應的退出和失焦事件:

LeaveNotify event, serial 36, synthetic NO, window 0x6800001,
 root 0xbb, subw 0x0, time 44348653, (55,185), root:(1679,420),
 mode NotifyNormal, detail NotifyNonlinear, same_screen YES,
 focus YES, state 0
FocusOut event, serial 36, synthetic NO, window 0x6800001,
 mode NotifyNormal, detail NotifyNonlinear

One common use of xev is to extract keycodes and key symbols for different keyboards when remapping the keyboard. Here’s the output from pressing the L key; the keycode here is 46:

xev 的一個常見用途是在重新映射鍵盤時提取不同鍵盤的鍵碼和鍵符號。

這是按下 L 鍵時的輸出;這裏的鍵碼是 46:

KeyPress event, serial 32, synthetic NO, window 0x4c00001,
 root 0xbb, subw 0x0, time 2084270084, (131,120), root:(197,172),
 state 0x0, keycode 46 (keysym 0x6c, l), same_screen YES,
 XLookupString gives 1 bytes: (6c) "l"
 XmbLookupString gives 1 bytes: (6c) "l"
 XFilterEvent returns: False

You can also attach xev to an existing window ID with the -id id option. (Use the ID that you get from xwininfo as id) or monitor the root window with -root.)

您還可以使用 -id id 選項將 xev 附加到現有窗口 ID(使用從 xwininfo 獲取的 ID 作為 id)或使用 -root 監視根窗口。

14.3.2 Understanding X Input and Preference Settings(瞭解 X 輸入和首選項設置)

One of the most potentially baffling characteristics of X is that there’s often more than one way to set preferences, and some methods may not work. For example, one common keyboard preference on Linux systems is to remap the Caps Lock key to a Control key. There are a number of ways to do this, from making small adjustments with the old xmodmap command to providing an entirely new keyboard map with the setxkbmap utility. How do you know which ones (if any) to use? It’s a matter of knowing which pieces of the system have responsibility, but determining this can be difficult. Keep in mind that a desktop environment may provide its own settings and overrides.

X 最具潛在困擾性的特徵之一是通常有多種設置偏好的方式,而其中一些方法可能不起作用。

例如,在 Linux 系統上,一種常見的鍵盤偏好是將 Caps Lock 鍵重新映射為 Control 鍵。

有許多方法可以實現這一點,從使用舊的 xmodmap 命令進行小的調整到使用 setxkbmap 實用程序提供全新的鍵盤映射。

你如何知道應該使用哪種(如果有)?

這取決於瞭解系統的哪些部分負責,但確定這一點可能會很困難。

請記住,桌面環境可能提供自己的設置和覆蓋。

With this said, here are a few pointers on the underlying infrastructure

説到這裏,以下是關於基礎設施的一些要點

Input Devices (General)

The X server uses the X Input Extension to manage input from many different devices. There are two basic types of input device—keyboard and pointer (mouse)—and you can attach as many devices as you like. In order to use more than one of the same type of device simultaneously, the X Input Extension creates a “virtual core” device that funnels device input to the X server. The core device is called the master; the physical devices that you plug in to the machine become slaves.

X 服務器使用 X 輸入擴展來管理來自許多不同設備的輸入。

有兩種基本類型的輸入設備——鍵盤和指針(鼠標)——你可以連接任意多個設備。

為了同時使用多個相同類型的設備,X 輸入擴展會創建一個“虛擬核心”設備,將設備輸入引導到 X 服務器。

核心設備被稱為主設備;你插入機器的物理設備則成為從設備。

To see the device configuration on your machine, try running the xinput --list command:

要查看你的機器上的設備配置,請嘗試運行 xinput --list 命令:

$ xinput --list
 Virtual core pointer id=2 [master pointer (3)]
 Virtual core XTEST pointer id=4 [slave pointer (2)]
 Logitech Unifying Device id=8 [slave pointer (2)]
 Virtual core keyboard id=3 [master keyboard (2)]
 Virtual core XTEST keyboard id=5 [slave keyboard (3)]
 Power Button id=6 [slave keyboard (3)]
 Power Button id=7 [slave keyboard (3)]
 Cypress USB Keyboard id=9 [slave keyboard (3)]

Each device has an associated ID that you can use with xinput and other commands. In this output, IDs 2 and 3 are the core devices, and IDs 8 and 9 are the real devices. Notice that the power buttons on the machine are also treated as X input devices.

每個設備都有一個相關聯的ID,你可以在xinput和其他命令中使用。

在這個輸出中,ID 2和3是核心設備,ID 8和9是真實設備。請注意,機器上的電源按鈕也被視為X輸入設備。

Most X clients listen for input from the core devices, because there is no reason for them to be concerned about which particular device originates an event. In fact, most clients know nothing about the X Input Extension. However, a client can use the extension to single out a particular device.

Each device has a set of associated properties. To view the properties, use xinput with the device number, as in this example:

大多數X客户端都監聽來自核心設備的輸入,因為它們不需要關心事件的具體來源設備。

事實上,大多數客户端對X輸入擴展一無所知。然而,客户端可以使用該擴展來單獨識別特定設備。

每個設備都有一組相關聯的屬性。要查看屬性,請使用設備編號運行xinput,就像這個例子中那樣。

$ xinput --list-props 8
Device 'Logitech Unifying Device. Wireless PID:4026':
 Device Enabled (126): 1
 Coordinate Transformation Matrix (128): 1.000000, 0.000000, 
0.000000,
0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000
 Device Accel Profile (256): 0
 Device Accel Constant Deceleration (257): 1.000000
 Device Accel Adaptive Deceleration (258): 1.000000
 Device Accel Velocity Scaling (259): 10.000000
--snip--

As you can see, there are a number of very interesting properties that you can change with the --set-prop option (See the xinput(1) manual page for more information.)

正如您所看到的,您可以使用--set-prop選項更改許多非常有趣的屬性(有關更多信息,請參閲xinput(1)手冊頁。)

Mouse(鼠標)

You can manipulate device-related settings with the xinput command, and many of the most useful pertain to the mouse (pointer). You can change many settings directly as properties, but it’s usually easier with the specialized --set-ptr-feedback and --set-button-map options to xinput. For example, if you have a three-button mouse at dev on which you’d like to reverse the order of buttons (this is handy for lefthanded users), try this:

您可以使用xinput命令操縱與設備相關的設置,其中許多最有用的設置與鼠標(指針)有關。

您可以直接將許多設置更改為屬性,但通常使用專門的--set-ptr-feedback和--set-button-map選項更容易。

例如,如果您有一個三按鈕鼠標設備,在該設備上您想要顛倒按鈕的順序(這對左撇子用户很方便),請嘗試以下操作:

$ xinput --set-button-map dev 3 2 1

Keyboard(鍵盤)

The many different keyboard layouts available internationally present particular difficulties for integration into any windowing system. X has always had an internal keyboard-mapping capability in its core protocol that you can manipulate with the xmodmap command, but any reasonably modern system uses the XKB (the X keyboard extension) to gain finer control.

國際上提供的許多不同鍵盤佈局在集成到任何窗口系統中都會帶來特定的困難。

X一直在其核心協議中具有內部鍵盤映射功能,您可以使用xmodmap命令進行操作,但是任何相當現代的系統都使用XKB(X鍵盤擴展)來獲得更精細的控制。

XKB is complicated, so much so that many people still use xmodmap when they need to make quick changes. The basic idea behind XKB is that you can define a keyboard map and compile it with the xkbcomp command, then load and activate that map in the X server with the setxkbmap command. Two especially interesting features of the system are these:

XKB非常複雜,以至於許多人在需要快速更改時仍然使用xmodmap。

XKB背後的基本思想是,您可以定義一個鍵盤映射,然後使用xkbcomp命令編譯它,最後使用setxkbmap命令在X服務器中加載和激活該映射。

系統中兩個特別有趣的特性是:

o You can define partial maps to supplement existing maps. This is especially handy for tasks such as changing your Caps Lock key into a Control key, and it is used by many graphical keyboard preference utilities in desktop environments.
o You can define individual maps for each attached keyboard

  • 您可以定義部分映射以補充現有映射。這對於諸如將大寫鎖定鍵更改為控制鍵之類的任務特別方便,並且被許多桌面環境中的圖形鍵盤首選項實用程序使用。
  • 您可以為每個連接的鍵盤定義單獨的映射。

    Desktop Background(桌面背景)

The old X command xsetroot allows you to set the background color and other characteristics of the root window, but it produces no effect on most machines because the root window is never visible. Instead, most desktop environments place a big window in the back of all of your other windows in order to enable features such as “active wallpaper” and desktop file browsing. There are ways to change the background from the command line (for example, with the gsettings command in some GNOME installations), but if you actually want to do this, you probably have too much time on your hands.

舊的 X 命令 xsetroot 允許你設置根窗口的背景顏色和其他特性,但在大多數機器上卻沒有任何效果,因為根窗口從未可見。

相反,大多數桌面環境會在所有其他窗口的後面放置一個大窗口,以啓用諸如“活動壁紙”和桌面文件瀏覽等功能。

有一些方法可以通過命令行更改背景(例如,在某些 GNOME 安裝中使用 gsettings 命令),但如果你真的想這樣做,那你可能有太多的空閒時間了。

xset

Probably the oldest preference command is xset. It’s not used much anymore, but you can run a quick xset q to get the status of a few features. Perhaps the most useful are the screensaver and Display Power Management Signaling (DPMS) settings.

可能是最古老的偏好命令是 xset。

它現在用得不多了,但你可以運行快速的 xset q 來獲取一些功能的狀態。

也許最有用的是屏幕保護程序和顯示電源管理信號(DPMS)設置。

14.4 The Future of X(X的未來)

As you were reading the preceding discussion, you may have gotten the feeling that X is a really old system that’s been poked at a lot in order to get it to do new tricks. You wouldn’t be far off. The X Window System was first developed in the 1980s. Although its evolution over the years has been significant (flexibility was an important part of its original design), you can push the original architecture only so far

當您閲讀前面的討論時,您可能會覺得X是一個非常古老的系統,經過了很多改動才能做出新的功能。您的想法並沒有錯。

X Window 系統最初是在1980年代開發的。

儘管多年來它的發展是顯著的(靈活性是其原始設計的重要部分),但您只能讓原始架構走得這麼遠。

One sign of the age of the X Window System is that the server itself supports an extremely large number of libraries, many for backward compatibility. But perhaps more significantly, the idea of having a server manage clients, their windows, and act as an intermediary for the window memory has become a burden on performance. It’s much faster to allow applications to render the contents of their windows directly in the display memory, with a lighter-weight window manager, called a compositing window manager, to arrange the windows and do minimal management of the display memory.

X Window 系統年代久遠的一個跡象是服務器本身支持數量極多的庫,其中很多是為了向後兼容。

但更重要的是,讓服務器管理客户端、它們的窗口,並作為窗口內存的中介已經成為性能的負擔。

讓應用程序直接在顯示內存中呈現窗口內容,輔以輕量級窗口管理器(稱為合成窗口管理器)來排列窗口並最小化顯示內存的管理,會更快速。

A new standard based on this idea, Wayland, has started to gain traction. The most significant piece of Wayland is a protocol that defines how clients talk to the compositing window manager. Other pieces include input device management and an X-compatibility system. As a protocol, Wayland also maintains the idea of network transparency. Many pieces of the Linux desktop now support Wayland, such as GNOME and KDE.

基於這個想法的一個新標準,Wayland,已經開始受到關注。

Wayland最重要的部分是定義客户端如何與合成窗口管理器交流的協議。

其他部分包括輸入設備管理和一個X兼容系統。

作為一個協議,Wayland也保持了網絡透明性的概念。

許多Linux桌面的部分現在支持Wayland,比如GNOME和KDE。

But Wayland isn’t the only alternative to X. As of this writing, another project, Mir, has similar goals, though its architecture takes a somewhat different approach. At some point, there will be widespread adoption of at least one system, which may or may not be one of these.

但Wayland並不是X的唯一替代方案。

截至目前,另一個項目Mir也有類似的目標,儘管其架構採取了稍有不同的方式。

在某個時候,至少會有一種系統被廣泛採用,可能是這些系統之一,也可能不是。

These new developments are significant because they won’t be limited to the Linux desktop. Due to its poor performance and gigantic footprint, the X Window System is not suitable for environments such as tablets and smartphones, so manufacturers have so far used alternative systems to drive embedded Linux displays. However, standardized direct rendering can make for a more cost-effective way to support these displays.

這些新發展是重要的,因為它們不會僅限於Linux桌面。

由於X Window系統性能差且佔用空間巨大,不適合平板電腦和智能手機等環境,因此製造商迄今為止一直使用替代系統來驅動嵌入式Linux顯示器。

然而,標準化的直接渲染可以提供更具成本效益的方式來支持這些顯示器。

14.5 D-Bus

One of the most important developments to come out of the Linux desktop is the Desktop Bus (D-Bus), a message-passing system. D-Bus is important because it serves as an interprocess communication mechanism that allows desktop applications to talk to each other, and because most Linux systems use it to notify processes of system events, such as inserting a USB drive.

Linux 桌面環境中最重要的發展之一是桌面總線(D-Bus),一個消息傳遞系統。

D-Bus 很重要,因為它作為一個進程間通信機制,允許桌面應用程序相互通信,並且因為大多數 Linux 系統使用它來通知進程系統事件,比如插入 USB 驅動器。

D-Bus itself consists of a library that standardizes interprocess communication with a protocol and supporting functions for any two processes to talk to each other. By itself, this library doesn’t offer much more than a fancy version of normal IPC facilities such as Unix domain sockets. What makes D-Bus useful is a central “hub” called dbus-daemon. Processes that need to react to events can connect to dbus-daemon and register to receive certain kinds of events. Processes also create the events. For example, the process udisksdaemon listens to ubus for disk events and sends them to dbus-daemon, which then retransmits the events to applications interested in disk events.

D-Bus 本身包括一個庫,標準化了進程間通信的協議和支持函數,使得任意兩個進程可以相互通信。

單獨來看,這個庫並沒有提供比普通 IPC 設施(比如 Unix 域套接字)更多的功能。

使 D-Bus 有用的是一個稱為 dbus-daemon 的中央“樞紐”。需要對事件做出反應的進程可以連接到 dbus-daemon 並註冊以接收某種類型的事件。進程也會創建事件。例如,進程 udisks-daemon 監聽 ubus 的磁盤事件並將其發送到 dbus-daemon,後者然後將事件轉發給對磁盤事件感興趣的應用程序。

14.5.1 System and Session Instances(系統和會話實例)

D-Bus has become a more integral part of the Linux system, and it now goes beyond the desktop. For example, both systemd and Upstart have D-Bus channels of communication. However, adding dependencies to desktop tools inside the core system goes against a core design rule of Linux.

D-Bus 已經成為 Linux 系統更為重要的一部分,現在已經超越了桌面。例如,systemd 和 Upstart 都有 D-Bus 通信渠道。

然而,在核心繫統中添加桌面工具的依賴關係違反了 Linux 的核心設計原則。

To address this problem, there are actually two kinds of dbus-daemon instances (processes) that can run. The first is the system instance, which is started by init at boot time with the --system option. The system instance usually runs as a D-Bus user, and its configuration file is /etc/dbus-1/system.conf (though you probably shouldn’t change the configuration). Processes can connect to the system instance through the /var/run/dbus/system_bus_socket Unix domain socket.

為了解決這個問題,實際上有兩種可以運行的 dbus-daemon 實例(進程)。

第一種是系統實例,它由 init 在啓動時使用 --system 選項啓動。

系統實例通常以 D-Bus 用户身份運行,其配置文件是 /etc/dbus-1/system.conf(儘管您可能不應更改配置)。

進程可以通過 /var/run/dbus/system_bus_socket Unix 域套接字連接到系統實例。

Independent of the system D-Bus instance, there is an optional session instance that runs only when you start a desktop session. Desktop applications that you run connect to this instance.

與系統 D-Bus 實例無關,還有一個可選的會話實例,只有在啓動桌面會話時才會運行。

您運行的桌面應用程序會連接到這個實例。

14.5.2 Monitoring D-Bus Messages(監控 D-Bus 消息)

One of the best ways to see the difference between the system and session dbus-daemon instances is to monitor the events that go over the bus. Try using the dbus-monitor utility in system mode like this:

要了解系統和會話 dbus-daemon 實例之間的區別,最好的方法之一是監視通過總線傳輸的事件。

嘗試像這樣在系統模式下使用 dbus-monitor 實用程序:

$ dbus-monitor --system
signal sender=org.freedesktop.DBus -> dest=:1.952 serial=2 path=/org/
freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired
 string ":1.952"

The startup message here indicates that the monitor connected and acquired a name. You shouldn’t see much activity when you run it like this, because the system instance usually isn’t very busy. To see something happen, try plugging in a USB storage device.

這裏的啓動消息表示監視器已連接並獲取了一個名稱。

當您像這樣運行它時,您不應該看到太多活動,因為系統實例通常不太忙碌。

要看到一些活動,請嘗試插入一個 USB 存儲設備。

By comparison, session instances have much more to do. Assuming you’ve logged in to a desktop session, try this:

相比之下,會話實例要做的事情要多得多。

假設您已經登錄到一個桌面會話,請嘗試這樣做:

$ dbus-monitor --session

Now move your mouse around to different windows; if your desktop is D-Bus aware, you should get a flurry of messages indicating activated windows.

現在移動鼠標到不同的窗口中;如果您的桌面支持 D-Bus,您應該會收到一系列指示已激活窗口的消息。

14.6 Printing(打印)

Printing a document on Linux is a multistage process. It goes like this:

在 Linux 上打印文檔是一個多階段的過程。步驟如下:

  1. The program doing the printing usually converts the document into PostScript form. This step is optional.
  2. The program sends the document to a print server.
  3. The print server receives the document and places it on a print queue.
  4. When the document’s turn in the queue arrives, the print server sends the document to a print filter.
  5. If the document is not in PostScript form, a print filter might perform a conversion.
  6. If the destination printer does not understand PostScript, a printer driver converts the document to a printer-compatible format.
  7. The printer driver adds optional instructions to the document, such as paper tray and duplexing options.
  8. The print server uses a backend to send the document to the printer.
  9. 進行打印的程序通常會將文檔轉換為 PostScript 格式。這一步驟是可選的。
  10. 程序將文檔發送到打印服務器。
  11. 打印服務器接收文檔並將其放置在打印隊列中。
  12. 當文檔在隊列中輪到時,打印服務器會將文檔發送到打印過濾器。
  13. 如果文檔不是以 PostScript 形式存在,打印過濾器可能會執行轉換。
  14. 如果目標打印機不理解 PostScript,打印機驅動程序會將文檔轉換為打印機兼容的格式。
  15. 打印機驅動程序會向文檔添加可選的指令,例如紙盒和雙面打印選項。
  16. 打印服務器使用後端將文檔發送到打印機。

The most confusing part of this process is why so much revolves around PostScript. PostScript is actually a programming language, so when you print a file using it, you’re sending a program to the printer. PostScript serves as a standard for printing in Unix-like systems, much as the .tar format serves as an archiving standard. (Some applications now use PDF output, but this is relatively easy to convert.)

這個過程最令人困惑的部分是為什麼這麼多環節與 PostScript 有關。

PostScript 實際上是一種編程語言,因此當你使用它打印文件時,實際上是將一個程序發送到打印機。

PostScript 在類 Unix 系統中用作打印的標準,就像 .tar 格式用作存檔標準一樣。

(一些應用程序現在使用 PDF 輸出,但這相對容易轉換。)

We’ll talk more about the print format later; first, let’s look at the queuing system.

我們稍後會詳細討論打印格式;首先,讓我們看看隊列系統。

14.6.1 CUPS

The standard printing system in Linux is CUPS (http://www.cups.org/), which is the same system used on Mac OS X. The CUPS server daemon is called cupsd, and you can use the lpr command as a simple client to send files to the daemon.

Linux 中的標準打印系統是 CUPS(http://www.cups.org/),這也是 Mac OS X 上使用的系統。

CUPS 服務器守護程序稱為 cupsd,您可以使用 lpr 命令作為簡單的客户端將文件發送到守護程序。

One significant feature of CUPS is that it implements Internet Print Protocol (IPP), a system that allows for HTTP-like transactions among clients and servers on TCP port 631. In fact, if you have CUPS running on your system, you can probably connect to http://localhost:631/ to see your current configuration and check on any printer jobs. Most network printers and print servers support IPP, as does Windows, which can make setting up remote printers a relatively simple task.

CUPS 的一個重要特性是它實現了 Internet 打印協議(IPP),這是一種允許客户端和服務器在 TCP 端口 631 上進行類似 HTTP 的交易的系統。

事實上,如果您的系統上運行着 CUPS,您可能可以連接到 http://localhost:631/ 查看當前配置並檢查任何打印作業。

大多數網絡打印機和打印服務器都支持 IPP,Windows 也支持,這可以使設置遠程打印機相對簡單。

You probably won’t be able to administer the system from the web interface, because the default setup isn’t very secure. Instead, your distribution likely has a graphical settings interface to add and modify printers. These tools manipulate the configuration files, normally found in /etc/cups. It’s usually best to let these tools do the work for you, because configuration can be complicated. And even if you do run into a problem and need to configure manually, it’s usually best to create a printer using the graphical tools so that you have somewhere to start.

您可能無法通過 Web 界面管理系統,因為默認設置並不太安全。

相反,您的發行版可能有一個圖形化設置界面來添加和修改打印機。

這些工具操作配置文件,通常位於 /etc/cups。

最好讓這些工具為您完成工作,因為配置可能會很複雜。

即使遇到問題需要手動配置,最好還是使用圖形化工具創建打印機,這樣您就有了一個起點。

14.6.2 Format Conversion and Print Filters(格式轉換和打印過濾器)

Many printers, including nearly all low-end models, do not understand PostScript or PDF. In order for Linux to support one of these printers, it must convert documents to a format specific to the printer. CUPS sends the document to a Raster Image Processor (RIP) to produce a bitmap. The RIP almost always uses the Ghostscript (gs) program to do most of the real work, but it’s somewhat complicated because the bitmap must fit the format of the printer. Therefore, the printer drivers that CUPS uses consult the PostScript Printer Definition (PPD) file for the specific printer to figure out settings such as resolution and paper sizes

許多打印機,包括幾乎所有低端型號,不理解 PostScript 或 PDF。為了讓 Linux 支持其中一台打印機,必須將文檔轉換為特定於打印機的格式。

CUPS 將文檔發送到光柵圖像處理器(RIP)以生成位圖。RIP 幾乎總是使用 Ghostscript(gs)程序來完成大部分實際工作,但由於位圖必須符合打印機的格式,所以有些複雜。

因此,CUPS 使用的打印機驅動程序會查閲特定打印機的 PostScript 打印機定義(PPD)文件,以確定分辨率和紙張尺寸等設置。

14.7 Other Desktop Topics(其他桌面主題)

One interesting characteristic of the Linux desktop environment is that you can generally choose which pieces you want to use and stop using the ones that you dislike. For a survey of many of the desktop projects, have a look at the mailing lists and project links for the various projects at http://www.freedesktop.org/. Elsewhere, you’ll find other desktop projects, such as Ayatana, Unity, and Mir. Another major development in the Linux desktop is the Chromium OS open source project and its Google Chrome OS counterpart found on Chromebook PCs. This is a Linux system that uses much of the desktop technology described in this chapter but is centered around the Chromium/ Chrome web browsers. Much of what’s found on a traditional desktop has been stripped away in Chrome OS.

Linux 桌面環境的一個有趣特點是,通常您可以選擇要使用的組件,並停止使用您不喜歡的組件。

要了解許多桌面項目的概況,請查看 http://www.freedesktop.org/ 上各項目的郵件列表和項目鏈接。

此外,您還會發現其他桌面項目,如 Ayatana、Unity 和 Mir。

Linux 桌面的另一個重大發展是 Chromium OS 開源項目及其在 Chromebook 個人電腦上找到的 Google Chrome OS 對應項目。

這是一個使用本章描述的許多桌面技術的 Linux 系統,但其核心是圍繞 Chromium/Chrome 網頁瀏覽器。

Chrome OS 剝離了傳統桌面上的許多內容。

user avatar kerrywu Avatar sherlocked93 Avatar snower Avatar litongjava Avatar beiyinglunkuo Avatar
Favorites 5 users favorite the story!
Favorites

Add a new Comments

Some HTML is okay.