xinit.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <X11/Xlib.h>
  4. #include <X11/Xutil.h>
  5. #include <X11/Xos.h>
  6. #include <X11/Xatom.h>
  7. #include <X11/keysym.h>
  8. #include <X11/Intrinsic.h>
  9. #include "xinit.h"
  10. /**
  11. * create an xwindow
  12. */
  13. void initdisplay(char* file, int x, int y){
  14. int i;
  15. char bitmap_data=0;
  16. XColor black={0,0,0,0};
  17. Cursor cursor;
  18. Pixmap bitmap;
  19. Atom atom_motif_wm_hints;
  20. XSetWindowAttributes win_attr;
  21. XClassHint class_hints;
  22. dis = XOpenDisplay(NULL);
  23. if(dis==NULL){
  24. printf("Cannot connect to X server.\n");
  25. fflush(stdout);
  26. exit(1);
  27. }
  28. scr = DefaultScreen(dis);
  29. vsl = DefaultVisual(dis,scr);
  30. if(vsl->bits_per_rgb!=8){
  31. printf("Not a TrueColor visual. Please use DefaultDepth 24 mode.\n");
  32. fflush(stdout);
  33. exit(1);
  34. }
  35. /*
  36. XF86VidModeQueryVersion(dis, &vidModeMajorVersion, &vidModeMinorVersion);
  37. XF86VidModeGetAllModeLines(dis, scr, &modeNum, &modes);
  38. oldMode = *modes[0];
  39. for (i=0;i<modeNum;i++) if(modes[i]->hdisplay==x && modes[i]->vdisplay==y) bestMode=i;
  40. XF86VidModeSwitchToMode(dis, scr, modes[bestMode]);
  41. XF86VidModeSetViewPort(dis, scr, 0, 0);
  42. XFree(modes);
  43. */
  44. colormap = DefaultColormap(dis, scr);
  45. red_shift=green_shift=blue_shift=24;
  46. for (i=vsl->red_mask;!(i&0x80000000);i<<=1) red_shift--;
  47. for (i=vsl->green_mask;!(i&0x80000000);i<<=1) green_shift--;
  48. for (i=vsl->blue_mask;!(i&0x80000000);i<<=1) blue_shift--;
  49. win = XCreateSimpleWindow(dis, RootWindow(dis, scr), 1, 1, x, y, 0, BlackPixel (dis, 0), BlackPixel(dis, 0));
  50. XMapWindow(dis, win);
  51. XRaiseWindow(dis, win);
  52. //XMoveWindow(dis, win, 0, 0);
  53. bitmap=XCreateBitmapFromData(dis, win, &bitmap_data, 1, 1);
  54. cursor=XCreatePixmapCursor(dis, bitmap, bitmap, &black, &black, 1, 1);
  55. //XDefineCursor(dis, win, cursor);
  56. sprintf(title,"%s",file); titlelen=strlen(title);
  57. XSetStandardProperties(dis,win,title,title,None,NULL,0,NULL);
  58. /*
  59. //hide decorations
  60. win_attr.override_redirect=False;
  61. XChangeWindowAttributes(dis,win,CWOverrideRedirect,&win_attr);
  62. class_hints.res_name=class_hints.res_class="FULLSCREEN";
  63. XSetWMProperties(dis,win,NULL,NULL,NULL,0,NULL,NULL,&class_hints);
  64. atom_motif_wm_hints=XInternAtom(dis,"_MOTIF_WM_HINTS",False);
  65. if(atom_motif_wm_hints!=0){
  66. motif_wm_hints.flags=MOTIF_WM_HINTS_DECORATIONS;
  67. motif_wm_hints.decorations=0;
  68. XChangeProperty(dis,win,atom_motif_wm_hints,atom_motif_wm_hints,32,PropModeReplace,(unsigned char *)&motif_wm_hints,5);
  69. }
  70. */
  71. gc = XCreateGC(dis, win, 0, 0);
  72. #if DEBUG==FALSE
  73. XSelectInput(dis, win, ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask);
  74. #else
  75. XSelectInput(dis, win, 0xFFFFFF-ResizeRedirectMask);
  76. #endif
  77. XFlush(dis);
  78. }
  79. /**
  80. * destroy window
  81. */
  82. void closedisplay()
  83. {
  84. /*
  85. XF86VidModeSwitchToMode(dis, scr, &oldMode);
  86. XF86VidModeSetViewPort(dis, scr, 0, 0);
  87. */
  88. XDestroyWindow(dis,win);
  89. XCloseDisplay(dis);
  90. }
  91. /**
  92. * Copy ximage subimage
  93. */
  94. void CopyImage(int mode, XImage *bg, int x1, int y1, XImage* ximage, int x2, int y2, int w, int h)
  95. {
  96. unsigned int dx,dy;
  97. unsigned long pix;
  98. for(dy=0;dy<h;dy++){
  99. for(dx=0;dx<w;dx++){
  100. pix=XGetPixel(ximage,dx+x2,dy+y2);
  101. if(mode==0 || pix!=0)
  102. XPutPixel(bg,dx+x1,dy+y1,pix);
  103. }
  104. }
  105. }
  106. /**
  107. * Create rgb colors
  108. */
  109. unsigned long int MkColor(int r,int g,int b)
  110. {
  111. if (red_shift>=0) r<<=red_shift; else r>>=-red_shift;
  112. if (green_shift>=0) g<<=green_shift; else g>>=-green_shift;
  113. if (blue_shift>=0) b<<=blue_shift; else b>>=-blue_shift;
  114. r&=vsl->red_mask;
  115. g&=vsl->green_mask;
  116. b&=vsl->blue_mask;
  117. return r+g+b;
  118. }
  119. unsigned long int MkColorTGA(int r,int g,int b)
  120. {
  121. return (r&0xFF)+((g&0xFF)<<8)+((b&0xFF)<<16);
  122. }
  123. void DestroyImage(XImage *ximage){
  124. free(ximage->data);
  125. ximage->data=NULL;
  126. XDestroyImage(ximage);
  127. }
  128. void waitforevent(XImage *bg)
  129. {
  130. report.type=None;
  131. while(report.type!=KeyPress && report.type!=ButtonRelease) {
  132. XNextEvent(dis, &report);
  133. //if (report.type==ButtonRelease) playsound("click.raw");
  134. if(report.type==Expose){
  135. XPutImage(dis, win, gc, bg,
  136. report.xexpose.x,report.xexpose.y,
  137. report.xexpose.x,report.xexpose.y,
  138. report.xexpose.width, report.xexpose.height);
  139. XSync(dis,FALSE);
  140. }
  141. }
  142. }
  143. /**
  144. * fade in our out an ximage
  145. */
  146. void fadeimage(int fadetype, int x, int y, XImage *ximage){
  147. byte *data2;
  148. byte step, start, end;
  149. byte b;
  150. int i;
  151. data2=(byte *)malloc(ximage->bytes_per_line*ximage->height+12);
  152. bcopy(ximage->data,data2,ximage->bytes_per_line*ximage->height);
  153. //fade in
  154. if (fadetype==0 || fadetype==2){
  155. for(step=8;step>0;step--){
  156. for(i=0;i<ximage->bytes_per_line*ximage->height;i++){
  157. b=memToVal(data2+i,1); b=b/step;
  158. valToMem(b,ximage->data+i,1);
  159. }
  160. XPutImage(dis, win, gc, ximage, 0, 0, x, y, ximage->width, ximage->height);
  161. XSync(dis,FALSE);
  162. usleep(10000);
  163. }
  164. }
  165. //restore original image
  166. bcopy(data2,ximage->data,ximage->bytes_per_line*ximage->height);
  167. //wait for event
  168. if(fadetype==2){
  169. waitforevent(ximage);
  170. }
  171. //fade out
  172. if (fadetype==1 || fadetype==2 || fadetype==3){
  173. if(fadetype==3) end=4; else end=8;
  174. for(step=1;step!=end;step++){
  175. for(i=0;i<ximage->bytes_per_line*ximage->height;i++){
  176. b=memToVal(data2+i,1); b=b/step;
  177. valToMem(b,ximage->data+i,1);
  178. }
  179. XPutImage(dis, win, gc, ximage, 0, 0, x, y, ximage->width, ximage->height);
  180. XSync(dis,FALSE);
  181. usleep(10000);
  182. }
  183. }
  184. //restore original image
  185. if(fadetype!=3)
  186. bcopy(data2,ximage->data,ximage->bytes_per_line*ximage->height);
  187. free(data2);
  188. }
  189. /**
  190. * fade background and display a subtitle on it
  191. */
  192. void subtitle(int x, int y, XImage *ximage, XImage *bg){
  193. byte *data2;
  194. byte step, start, end;
  195. byte b;
  196. int i;
  197. data2=(byte *)malloc(bg->bytes_per_line*bg->height+12);
  198. bcopy(ximage->data,data2,ximage->bytes_per_line*ximage->height);
  199. //fade in
  200. for(step=4;step>0;step--){
  201. for(i=0;i<ximage->bytes_per_line*ximage->height;i++){
  202. b=memToVal(data2+i,1); b=b/step;
  203. valToMem(b,ximage->data+i,1);
  204. }
  205. CopyImage(1,bg,x,y,ximage,0,0,ximage->width,ximage->height);
  206. XPutImage(dis, win, gc, bg, 0, 0, 0, 0, bg->width, bg->height);
  207. XSync(dis,FALSE);
  208. usleep(10000);
  209. }
  210. //restore original image
  211. bcopy(data2,ximage->data,ximage->bytes_per_line*ximage->height);
  212. //wait for event
  213. waitforevent(ximage);
  214. //fade out
  215. bcopy(bg->data,data2,bg->bytes_per_line*bg->height);
  216. for(step=1;step!=8;step++){
  217. for(i=0;i<bg->bytes_per_line*bg->height;i++){
  218. b=memToVal(data2+i,1); b=b/step;
  219. valToMem(b,bg->data+i,1);
  220. }
  221. XPutImage(dis, win, gc, bg, 0, 0, 0, 0, bg->width, bg->height);
  222. XSync(dis,FALSE);
  223. usleep(10000);
  224. }
  225. free(data2);
  226. }
  227. /**
  228. * mask out ximage
  229. */
  230. void dopause(int x, int y, int legend, XImage *pause, XImage *bg){
  231. byte *data;
  232. XImage *tempimg;
  233. byte *data2;
  234. byte step, start, end;
  235. byte b;
  236. int i;
  237. data2=(byte *)malloc(bg->bytes_per_line*bg->height+12);
  238. bcopy(bg->data,data2,bg->bytes_per_line*bg->height);
  239. //Create Pause image
  240. tempimg = XCreateImage(dis, vsl, DefaultDepth(dis,scr), ZPixmap, 0, NULL, pause->width, pause->height, 8, pause->width * 4);
  241. data= (byte *) malloc(((pause->width * pause->height)) * 4);
  242. tempimg->data= (char *)data;
  243. tempimg->byte_order= LSBFirst;
  244. tempimg->bits_per_pixel=32;
  245. CopyImage(0,tempimg,0,0,bg,x,y,tempimg->width,tempimg->height);
  246. CopyImage(1,tempimg,0,0,pause,0,0,tempimg->width,tempimg->height);
  247. for(i=0;i<tempimg->bytes_per_line*tempimg->height;i++){
  248. b=memToVal(tempimg->data+i,1);
  249. if(b==0x010000 || b==0x000001) b=0;
  250. valToMem(b,tempimg->data+i,1);
  251. }
  252. //fade out bg
  253. for(step=1;step<8;step++){
  254. for(i=0;i<bg->bytes_per_line*(bg->height-legend);i++){
  255. b=memToVal(data2+i,1); if(step==7) b=1; else b=b/step;
  256. valToMem(b,bg->data+i,1);
  257. }
  258. CopyImage(1,bg,x,y,tempimg,0,0,tempimg->width,tempimg->height);
  259. XPutImage(dis, win, gc, bg, 0, 0, 0, 0, bg->width, bg->height-legend);
  260. XSync(dis,FALSE);
  261. usleep(10000);
  262. }
  263. //wait for event
  264. waitforevent(bg);
  265. //fade in bg
  266. for(step=8;step>0;step--){
  267. for(i=0;i<bg->bytes_per_line*(bg->height-legend);i++){
  268. b=memToVal(data2+i,1); b=b/step;
  269. valToMem(b,bg->data+i,1);
  270. }
  271. CopyImage(1,bg,x,y,tempimg,0,0,tempimg->width,tempimg->height);
  272. XPutImage(dis, win, gc, bg, 0, 0, 0, 0, bg->width, bg->height-legend);
  273. XSync(dis,FALSE);
  274. usleep(10000);
  275. }
  276. DestroyImage(tempimg);
  277. //copy back original screen
  278. bcopy(bg->data,data2,bg->bytes_per_line*bg->height);
  279. XPutImage(dis, win, gc, bg, 0, 0, 0, 0, bg->width, bg->height);
  280. XSync(dis,FALSE);
  281. free(data2);
  282. }