Low Level X Window Programming
Ross J. Maloney Low Level X Window Programming An Introduction by Examples 123
Dr. Ross J. Maloney Yenolam Corporation Booragoon, WA Australia ISBN 978-3-319-74249-6 ISBN 978-3-319-74250-2 (ebook) https://doi.org/10.1007/978-3-319-74250-2 Library of Congress Control Number: 2018931452 Springer International Publishing AG, part of Springer Nature 2017 This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, express or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations. Printed on acid-free paper This Springer imprint is published by the registered company Springer International Publishing AG part of Springer Nature The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
Preface This book is the missing part of most X Window programming books, the part which others either neglect or skip over quickly. Those omissions are the subject material of this book. Most books on X Window programming cover Xlib in passing. They pass on to use of toolkits for it is they which are most commonly used to write X Window programs. Such toolkits include Athena, Xt, Motif, GTK, Qt, among a number of others. Toolkits are used for they produce a finished graphics result in less time and can be used without entirely understanding what is going on behind the scenes. Low-level programming in X Window is analogous to assembly language programming. Whereas in assembler programming a knowledge of the computer hardware is required, in low-level X Window programming a knowledge of how the X Window System operated is required. From the perspective taken here, X toolkits are high-level languages and are not considered here. This contrasts to the standard X Window programming book. With respect to these levels, brief consideration is given to the protocol underlying X Window. This is the equivalent of machine language. This is not a practical way of coding an X Window program but is covered as foundation material. An increased level of abstraction and removal from hardware detail is provided by Xlib which occupies most of this book. An overview of Xcb which has recently appeared as a replacement of Xlib is also included. The philosophy in this book is to link the programming which produces outcomes for the program s user to the operation of X Window. The Xlib library functions have a direct connection to the messages passed between the client and server, the two main elements of an X Window system. This message connection means being able to write graphics programs which can perform the graphics operations the fastest possible under X. But to achieve such speed requires more knowledge. Without that knowledge, the required speed increase above that obtainable via a toolkit may not be obtained, let alone nothing appearing at all. More time is required in coding using Xlib than a toolkit. Whether a possible increase in execution speed balances out the longer coding time is a value judgement. v
vi Preface Knowledge is acquired here. Writing a program in Xlib is exercising knowledge of how the X Window system works; how the pieces are connected together to produce the total outcome. The aim of this book is to assist the reader in acquiring knowledge of using the Xlib library. A discuss and show me style is used here. A discussion of the concepts is given, and then, those discussions are used to write a Xlib program. The output produced is shown together with the listing of the program. Exercises are then included to extend the discussion and to encourage the reader to reflect on the concepts just covered. Each program is written in a standard style and is as short in length as possible. The aim is to equip the reader to produce Xlib programs to support many needs. X Window is present on computers ranging in performance from supercomputers to personal computers. The programmer engaged in different computing environments, application realms, and target end-users needs to bring knowledge specificto that environment, realm, and end-user type, to be successful. A toolbox (nor kit) of displaying colours, patterns, geometric shapes, text, and input control by mouse and keyboard each in an efficient and effective manner will help their adaptation. All these tasks can be done using a toolkit but done the way the toolkit is set up to do it. The reader will be exposed to handling all those tasks in this book to do them in an individual way. Although all the programs contained in this book were developed on a laptop running Linux, they should carry over to all X Window environments. Each chapter is designed to stand alone although there are some cross-references within the sections of a chapter and between chapters. The chapters are arranged in the order of increasing complexity. This book does not provide a reference to the functions of the Xlib library, nor does it use all those functions. It shows how to combine those Xlib functions to produce functioning programs. References such as Xlib Reference Manual edited by Adrian Nye and published by O Reilly & Associates, Inc. in 1993 provide detail of all Xlib functions together with their parameters, description of purpose and error returns. The reader should have access to such a source while reading and working through the examples contained here for obtaining a greater depth of knowledge. The book Xlib Programming Manual by Adrian Nye, published by O Reilly & Associates, Inc. in 1995, could be used to advantage to put Xlib into the context of the overall X Window system s component parts. This book takes a subset of the components covered in the Xlib Programming Manual and puts them together into working programs. As with those references, this book considers release of the X Window software.
Preface vii Thank You The existence of this book is a result of the open source initiative. All text and programs were written using vim. The programs were converted from source code to executable code using the gcc compiler and associated libraries. The photo editor xv was used to obtain the screenshots which show the programs operating. All programs, text preparation and associated experimentation were done on a Linux system. The prepared text was typeset using LATEX through many iterations. Without the X Window system and its associated libraries and utility programs, there would have been no subject matter and environment to elaborate upon. To the countless people who brought those elements into existence, maintain them, and make them available, may I express my thanks. Reader Background Assumed This book is aimed at those readers interested in understanding how to program X Window at a low level. The majority of that level considered here is Xlib with the addition of Xcb and the X Window protocol itself. It is assumed the reader knows the C language and has used X Windows to run application programs. A programming knowledge of one or more of the X toolkits available would be a further advantage so as to offer a contrast to using Xlib. Familiarity with the contents of, and access to a copy of Xlib Programming Manual for Version 11 by Adrian Nye, published in 1995 by O Reilly & Associates, Inc. ISBN 1-56592-002-3, and Xlib Reference Manual edited by Adrian Nye, published by O Reilly & Associates, Inc. in 1993, ISBN 1-56592-006-6, is assumed. These volumes provide essential auxiliary information and detail. The best advantage of this material is obtained by writing programs and debugging those programs. The examples and the exercises are starting points. The reader should have access to an X Window system which can be used for this practice. Perth, Australia November 2017 Ross J. Maloney
Contents 1 Preliminaries... 1 1.1 The Place of the X Protocol... 2 1.2 X Window Programming Gotchas... 4 1.3 Programming in X Window... 5 2 Getting Something to Show... 7 2.1 Basic Xlib Programming Code Blocks... 7 2.2 Creating a Single Window... 8 2.2.1 Open Connection to the Server... 9 2.2.2 Top-Level Window... 10 2.2.3 Exercises... 13 2.3 Smallest Xlib Program to Produce a Window... 13 2.3.1 Exercises... 15 2.4 A Simple but Useful Xlib Program... 15 2.4.1 Exercises... 16 2.5 A Moving Window... 16 2.5.1 Exercises... 21 2.6 Parts of Windows Can Disappear from View... 21 2.6.1 Testing Overlay Services Available from an X Server... 22 2.6.2 Consequences of No Server Overlay Services... 24 2.6.3 Exercises... 29 2.7 Changing a Window s Properties... 30 2.8 Summary... 32 3 Windows and Events Produce Menus... 35 3.1 Colour... 36 3.1.1 Exercises... 39 3.2 A Button to Click... 39 ix
x Contents 3.3 Events... 44 3.3.1 Exercises... 49 3.4 Menus... 49 3.4.1 Text Labelled Menu Buttons... 50 3.4.2 Exercises... 57 3.5 Further Consideration of Mouse Events... 57 3.5.1 Exercises... 67 3.6 A Mouse Behaviour Application... 68 3.6.1 Exercises... 71 3.7 Implementing Hierarchical Menus... 71 3.7.1 Exercises... 81 3.8 Which Window Gets the Event?... 82 3.8.1 Exercises... 84 3.9 Summary... 84 4 Pattern Maps and Labels... 85 4.1 The Pixmap Resource... 86 4.2 Pattern Patches... 86 4.3 Bitmap Patterns... 87 4.3.1 Exercises... 92 4.4 A Bitmap Cursor... 92 4.4.1 Exercises... 97 4.5 A Partially Transparent Pixmap... 97 4.6 Using Postscript to Create Labels... 101 4.7 Changing the Colour of a Pixmap... 105 4.8 Reducing Server Client Interaction by Images... 108 4.8.1 Exercises... 112 4.9 Creating Menus by Using the Image Format... 112 4.9.1 Exercises... 118 4.10 Forming Text Messages from Bitmap Glyphs... 118 4.10.1 Accessing X11 Standard Bitmap Fonts... 119 4.10.2 How to Use the Bitmap Fonts... 123 4.10.3 Exercises... 131 4.11 Using Pixmaps to Colour a Window s Background... 131 4.11.1 Exercises... 137 4.12 Summary... 138 5 Keyboard Entry and Displaying Text... 139 5.1 Elementary Keyboard Text X Entry... 140 5.1.1 Exercises... 144 5.2 What Fonts Are Available... 144 5.3 Keyboard Echoing on Windows... 146 5.3.1 Exercises... 152
Contents xi 5.4 Putting Lines of Text in a Window... 152 5.4.1 Exercises... 156 5.5 Insertion Cursor... 156 5.5.1 Exercises... 162 5.6 Moving Between Text Input Windows Using Keys... 163 5.6.1 Exercises... 168 5.7 A Slider Bar... 168 5.7.1 Exercises... 173 5.8 Scrolling Text.... 173 5.8.1 Scrolling Horizontally.... 175 5.8.2 Scrolling Vertically... 179 5.8.3 Exercises... 184 5.9 Summary... 185 6 Classic Drawing... 187 6.1 Limit on Multiple Objects in a Request... 188 6.2 Drawing Lines, Circles, and a Coloured-In Square... 190 6.2.1 Exercises... 194 6.3 A Symbol Composed from Circle Parts... 194 6.3.1 Exercises... 198 6.4 A Circle Bouncing off Plain Edges... 198 6.4.1 Exercises... 203 6.5 Displaying the Multi Colours of a Photograph... 203 6.5.1 Exercises... 207 6.6 Summary... 208 7 Extensions... 209 7.1 Multi-colour XPM Pixmaps... 210 7.1.1 Exercises... 216 7.2 Network Connecting Client to Server... 216 7.2.1 Exercises... 219 7.3 Scalable Fonts... 219 7.3.1 Exercises... 223 7.4 Summary... 223 8 The Xcb Alternative... 225 8.1 Starting and Finishing with Xcb... 226 8.2 Creating and Using a Window... 227 8.3 Communicating with the Window Manager... 230 8.4 Events... 231 8.5 A Consolidation Program... 234 8.5.1 Exercises... 236 8.6 Colour, Fonts, then Text... 236 8.6.1 Exercises... 240
xii Contents 8.7 A Classic Program Converted to Xcb... 240 8.7.1 Exercises... 244 8.8 Summary... 244 9 Closer to the X Protocol... 245 9.1 The X Window Environment... 246 9.1.1 Exercises... 247 9.2 Client/Server Interaction... 248 9.2.1 Exercises... 252 9.3 More than a Protocol is Required... 252 9.3.1 Exercises... 257 9.4 Summary... 258 References... 259 Index... 261