File: offspot.c

package info (click to toggle)
chinput 3.0.2-19
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 2,140 kB
  • ctags: 2,818
  • sloc: ansic: 52,778; makefile: 164; perl: 23
file content (185 lines) | stat: -rw-r--r-- 4,470 bytes parent folder | download | duplicates (3)
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
181
182
183
184
185
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/Xlocale.h>

#define DEFAULT_FONT    \
"8x16,\
-tlc-song-medium-r-normal--16-*-*-*-*-*-gbk-0"

#define F_SIZE 		16
#define COL    		30
#define ROW    		10

#define W_WIDTH  	(F_SIZE * COL)
#define W_HEIGHT 	(F_SIZE * (ROW + 2))

main(int argc, char *argv[])
{
  Display	*display;
  int		screen;
  GC		gc;
  Window	window;
  XWMHints	hints;
  Atom		protocols;
  XFontSet	fs;
  char		**missing_list;
  int		missing_count;
  char		*def_string;
  XFontSetExtents *fs_ext;
  int		dec;
  XIM		im;
  XIC		ic;
  XRectangle	p_rect, s_rect;
  XVaNestedList	preedit_attr, status_attr;
  XEvent	event;
  unsigned long mask, fevent;
  int		len = 127;
  unsigned char	string[128], s_tab[ROW][128];
  KeySym	keysym;
  int		row = 0, col = 0;
  int		i, count = 0;
  Status	status;
  
  display = XOpenDisplay("");
  screen = DefaultScreen(display);
  gc = DefaultGC(display, screen);
  
  if (setlocale(LC_CTYPE, "") == NULL) {
    fprintf(stderr, "Error : setlocale() !\n");
    exit(0);
  }

  if (! XSupportsLocale() ) {
    fprintf(stderr, "Error : XSupportsLocale() !\n");
    exit(0);
  }

  if( (fs = XCreateFontSet(display, DEFAULT_FONT, &missing_list,
		      &missing_count, &def_string)) == NULL) {
    fprintf(stderr, "Error : XCreateFontSet() !\n");
    exit(0);
  }
  fs_ext = XExtentsOfFontSet(fs);
  dec = fs_ext->max_logical_extent.height + fs_ext->max_logical_extent.y;

  window = XCreateSimpleWindow(display, RootWindow(display, screen),
			       0, 0, W_WIDTH, W_HEIGHT + dec, 2,
			       BlackPixel(display, screen),
			       WhitePixel(display, screen));
  XStoreName(display, window, "XIM Demo - Off The Spot");
  hints.flags = InputHint;
  hints.input = True;
  XSetWMHints(display, window, &hints);
  protocols = XInternAtom(display, "WM_DELETE_WINDOW", True);
  XSetWMProtocols(display, window, &protocols, 1);

#ifdef DEBUG
  XMapWindow(display, window);
  XFlush(display);
#endif

  if ((im = XOpenIM(display, NULL, NULL, NULL)) == NULL) {
    printf("Error : XOpenIM !\n");
    exit(0);
  }

  /* create status area and preedit area xic attributes */
  s_rect.x = 0;
  s_rect.y = F_SIZE * (ROW + 1) + dec;
  s_rect.width = W_WIDTH;
  s_rect.height = F_SIZE;
  status_attr = XVaCreateNestedList(0,
				    XNArea, & s_rect,
				    XNFontSet, fs,
				    NULL);
  p_rect.x = 0;
  p_rect.y = F_SIZE * ROW + dec;
  p_rect.width = W_WIDTH;
  p_rect.height = F_SIZE;

  preedit_attr = XVaCreateNestedList(0,
				     XNArea, & p_rect,
				     XNFontSet, fs,
				     NULL);

  ic = XCreateIC(im,
		 XNInputStyle, XIMPreeditArea | XIMStatusArea,
		 XNClientWindow, window,
		 XNPreeditAttributes, preedit_attr,
		 XNStatusAttributes, status_attr,
		 NULL);
  if (ic == NULL) {
    printf("Error : XCreateIC() ! \n");
    XCloseIM(im);
    exit(0);
  }
  XFree(preedit_attr);
  XFree(status_attr);

  XGetICValues(ic, XNFilterEvents, &fevent, NULL);
  mask = ExposureMask | KeyPressMask | FocusChangeMask;
  XSelectInput(display, window, mask | fevent);
  
  for(i = 0; i < ROW; i++)
    s_tab[i][0] = 0;

  XMapWindow(display, window);
  
  for(;;) {
    XNextEvent(display, &event);
    if( XFilterEvent(&event, None) == True )
      continue;

    switch(event.type)
    {
    case FocusIn:
      XSetICFocus(ic);
      break;
    case FocusOut:
      XUnsetICFocus(ic);
      break;
    case Expose:
      for ( i=0; i < ROW; i++)
	XmbDrawString(display, window, fs, gc, 0,
		      F_SIZE * (i +1), s_tab[i], strlen(s_tab[i]));
      break;
    case KeyPress:
      count = XmbLookupString(ic, (XKeyPressedEvent *) &event,
			      string, len, &keysym, &status);
      string[count] = 0;
      if (status == XLookupBoth && keysym == XK_Return)
      {
	row = (++row) % ROW;
	col = 0;
	s_tab[row][0] = 0;
	XClearArea(display, window, 0, F_SIZE * row + dec,
		   W_WIDTH, F_SIZE, False);
      }
      else if (status = XLookupChars || status == XLookupBoth)
      {
	XmbDrawString(display, window, fs, gc,
		      F_SIZE / 2 * col, F_SIZE * (row + 1),
		      string, count);
	for (i = 0; i < count && col < len && string[i]; i++, col++)
	  s_tab[row][col] = string[i];
	s_tab[row][col] = 0;
      }
      break;

    case ClientMessage:
      if (event.xclient.data.l[0] == protocols)
      {
	XDestroyIC(ic);
	XCloseIM(im);
	XDestroyWindow(display, window);
	XCloseDisplay(display);
	exit(0);
      }
      break;
    defaults:
      break;
    }
  }
}